liboffsetfinder64.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // offsetfinder64.hpp
  3. // offsetfinder64
  4. //
  5. // Created by tihmstar on 10.01.18.
  6. // Copyright © 2018 tihmstar. All rights reserved.
  7. //
  8. #ifndef offsetfinder64_hpp
  9. #define offsetfinder64_hpp
  10. #include <string>
  11. #include <stdint.h>
  12. #include <mach-o/loader.h>
  13. #include <mach-o/nlist.h>
  14. #include <mach-o/dyld_images.h>
  15. #include <vector>
  16. #include <stdlib.h>
  17. typedef uint64_t offset_t;
  18. namespace tihmstar {
  19. class exception : public std::exception{
  20. std::string _err;
  21. int _code;
  22. public:
  23. exception(int code, std::string err) : _err(err), _code(code) {};
  24. exception(std::string err) : _err(err), _code(0) {};
  25. exception(int code) : _code(code) {};
  26. const char *what(){return _err.c_str();}
  27. int code(){return _code;}
  28. };
  29. namespace patchfinder64{
  30. typedef uint8_t* loc_t;
  31. class patch{
  32. bool _slideme;
  33. void(*_slidefunc)(class patch *patch, uint64_t slide);
  34. public:
  35. const loc_t _location;
  36. const void *_patch;
  37. const size_t _patchSize;
  38. patch(loc_t location, const void *patch, size_t patchSize, void(*slidefunc)(class patch *patch, uint64_t slide) = NULL) : _location(location), _patchSize(patchSize), _slidefunc(slidefunc){
  39. _patch = malloc(_patchSize);
  40. memcpy((void*)_patch, patch, _patchSize);
  41. _slideme = (_slidefunc) ? true : false;
  42. }
  43. patch(const patch& cpy) : _location(cpy._location), _patchSize(cpy._patchSize){
  44. _patch = malloc(_patchSize);
  45. memcpy((void*)_patch, cpy._patch, _patchSize);
  46. _slidefunc = cpy._slidefunc;
  47. _slideme = cpy._slideme;
  48. }
  49. void slide(uint64_t slide){
  50. if (!_slideme)
  51. return;
  52. printf("sliding with %p\n",(void*)slide);
  53. _slidefunc(this,slide);
  54. _slideme = false; //only slide once
  55. }
  56. ~patch(){
  57. free((void*)_patch);
  58. }
  59. };
  60. }
  61. class offsetfinder64 {
  62. public:
  63. struct text_t{
  64. patchfinder64::loc_t map;
  65. size_t size;
  66. patchfinder64::loc_t base;
  67. bool isExec;
  68. };
  69. private:
  70. bool _freeKernel;
  71. uint8_t *_kdata;
  72. size_t _ksize;
  73. offset_t _kslide;
  74. patchfinder64::loc_t _kernel_entry;
  75. std::vector<text_t> _segments;
  76. struct symtab_command *__symtab;
  77. void loadSegments(uint64_t slide);
  78. __attribute__((always_inline)) struct symtab_command *getSymtab();
  79. public:
  80. offsetfinder64(const char *filename);
  81. offsetfinder64(void* buf, size_t size, uint64_t base);
  82. const void *kdata();
  83. patchfinder64::loc_t find_entry();
  84. const std::vector<text_t> &segments(){return _segments;};
  85. patchfinder64::loc_t memmem(const void *little, size_t little_len);
  86. patchfinder64::loc_t find_sym(const char *sym);
  87. patchfinder64::loc_t find_syscall0();
  88. uint64_t find_register_value(patchfinder64::loc_t where, int reg, patchfinder64::loc_t startAddr = 0);
  89. /*------------------------ v0rtex -------------------------- */
  90. patchfinder64::loc_t find_zone_map();
  91. patchfinder64::loc_t find_kernel_map();
  92. patchfinder64::loc_t find_kernel_task();
  93. patchfinder64::loc_t find_realhost();
  94. patchfinder64::loc_t find_bzero();
  95. patchfinder64::loc_t find_bcopy();
  96. patchfinder64::loc_t find_copyout();
  97. patchfinder64::loc_t find_copyin();
  98. patchfinder64::loc_t find_ipc_port_alloc_special();
  99. patchfinder64::loc_t find_ipc_kobject_set();
  100. patchfinder64::loc_t find_ipc_port_make_send();
  101. patchfinder64::loc_t find_chgproccnt();
  102. patchfinder64::loc_t find_kauth_cred_ref();
  103. patchfinder64::loc_t find_osserializer_serialize();
  104. uint32_t find_vtab_get_external_trap_for_index();
  105. uint32_t find_vtab_get_retain_count();
  106. uint32_t find_iouserclient_ipc();
  107. uint32_t find_ipc_space_is_task();
  108. uint32_t find_proc_ucred();
  109. uint32_t find_task_bsd_info();
  110. uint32_t find_vm_map_hdr();
  111. uint32_t find_task_itk_self();
  112. uint32_t find_task_itk_registered();
  113. uint32_t find_sizeof_task();
  114. patchfinder64::loc_t find_rop_add_x0_x0_0x10();
  115. patchfinder64::loc_t find_rop_ldr_x0_x0_0x10();
  116. /*------------------------ kernelpatches -------------------------- */
  117. patchfinder64::patch find_i_can_has_debugger_patch_off();
  118. patchfinder64::patch find_lwvm_patch_offsets();
  119. patchfinder64::patch find_remount_patch_offset();
  120. std::vector<patchfinder64::patch> find_nosuid_off();
  121. patchfinder64::patch find_proc_enforce();
  122. patchfinder64::patch find_amfi_patch_offsets();
  123. patchfinder64::patch find_cs_enforcement_disable_amfi();
  124. patchfinder64::patch find_amfi_substrate_patch();
  125. // patchfinder64::patch find_sandbox_patch();
  126. patchfinder64::loc_t find_sbops();
  127. patchfinder64::patch find_nonceEnabler_patch();
  128. /*------------------------ KPP bypass -------------------------- */
  129. patchfinder64::loc_t find_gPhysBase();
  130. patchfinder64::loc_t find_kernel_pmap();
  131. patchfinder64::loc_t find_cpacr_write();
  132. patchfinder64::loc_t find_idlesleep_str_loc();
  133. patchfinder64::loc_t find_deepsleep_str_loc();
  134. /*------------------------ Util -------------------------- */
  135. patchfinder64::loc_t find_rootvnode();
  136. ~offsetfinder64();
  137. };
  138. using segment_t = std::vector<tihmstar::offsetfinder64::text_t>;
  139. namespace patchfinder64{
  140. loc_t find_literal_ref(segment_t segemts, offset_t kslide, loc_t pos);
  141. }
  142. }
  143. #endif /* offsetfinder64_hpp */