symbols.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/sysctl.h>
  4. #include <sys/utsname.h>
  5. #include "symbols.h"
  6. #include "kmem.h"
  7. #include "kutils.h"
  8. // the offsets are unlikely to change between similar models and builds, but the symbol addresses will
  9. // the offsets are required to get the kernel r/w but the symbols aren't
  10. int* offsets = NULL;
  11. /* iOS 11.1.2 */
  12. int kstruct_offsets_15B202[] = {
  13. 0xb, // KSTRUCT_OFFSET_TASK_LCK_MTX_TYPE,
  14. 0x10, // KSTRUCT_OFFSET_TASK_REF_COUNT,
  15. 0x14, // KSTRUCT_OFFSET_TASK_ACTIVE,
  16. 0x20, // KSTRUCT_OFFSET_TASK_VM_MAP,
  17. 0x28, // KSTRUCT_OFFSET_TASK_NEXT,
  18. 0x30, // KSTRUCT_OFFSET_TASK_PREV,
  19. 0x308, // KSTRUCT_OFFSET_TASK_ITK_SPACE
  20. 0x368, // KSTRUCT_OFFSET_TASK_BSD_INFO,
  21. 0x0, // KSTRUCT_OFFSET_IPC_PORT_IO_BITS,
  22. 0x4, // KSTRUCT_OFFSET_IPC_PORT_IO_REFERENCES,
  23. 0x40, // KSTRUCT_OFFSET_IPC_PORT_IKMQ_BASE,
  24. 0x50, // KSTRUCT_OFFSET_IPC_PORT_MSG_COUNT,
  25. 0x60, // KSTRUCT_OFFSET_IPC_PORT_IP_RECEIVER,
  26. 0x68, // KSTRUCT_OFFSET_IPC_PORT_IP_KOBJECT,
  27. 0x90, // KSTRUCT_OFFSET_IPC_PORT_IP_CONTEXT,
  28. 0xa0, // KSTRUCT_OFFSET_IPC_PORT_IP_SRIGHTS,
  29. 0x10, // KSTRUCT_OFFSET_PROC_PID,
  30. 0x20, // KSTRUCT_OFFSET_IPC_SPACE_IS_TABLE
  31. 0x180, // KSTRUCT_OFFSET_THREAD_BOUND_PROCESSOR
  32. 0x188, // KSTRUCT_OFFSET_THREAD_LAST_PROCESSOR
  33. 0x190, // KSTRUCT_OFFSET_THREAD_CHOSEN_PROCESSOR
  34. 0x408, // KSTRUCT_OFFSET_THREAD_CONTEXT_DATA
  35. 0x410, // KSTRUCT_OFFSET_THREAD_UPCB
  36. 0x418, // KSTRUCT_OFFSET_THREAD_UNEON
  37. 0x420, // KSTRUCT_OFFSET_THREAD_KSTACKPTR
  38. 0x54, // KSTRUCT_OFFSET_PROCESSOR_CPU_ID
  39. 0x28, // KSTRUCT_OFFSET_CPU_DATA_EXCEPSTACKPTR
  40. 0X78, // KSTRUCT_OFFSET_CPU_DATA_CPU_PROCESSOR
  41. };
  42. int koffset(enum kstruct_offset offset) {
  43. if (offsets == NULL) {
  44. printf("need to call symbols_init() prior to querying offsets\n");
  45. return 0;
  46. }
  47. return offsets[offset];
  48. }
  49. // this is the base of the kernel, not the kernelcache
  50. uint64_t kernel_base_electra = 0;
  51. uint64_t* symbols = NULL;
  52. uint64_t kaslr_slide = 0;
  53. uint64_t ksym(enum ksymbol sym) {
  54. if (kernel_base_electra == 0) {
  55. if (!have_kmem_read()) {
  56. printf("attempted to use symbols prior to gaining kernel read\n");
  57. return 0;
  58. }
  59. kernel_base_electra = find_kernel_base();
  60. kaslr_slide = find_kernel_base() - 0xFFFFFFF007004000;
  61. }
  62. //return symbols[sym] + kernel_base;
  63. return symbols[sym] + kaslr_slide;
  64. }
  65. int have_syms = 0;
  66. int probably_have_correct_symbols() {
  67. return have_syms;
  68. }
  69. void offsets_init() {
  70. size_t size = 32;
  71. char build_id[size];
  72. memset(build_id, 0, size);
  73. int err = sysctlbyname("kern.osversion", build_id, &size, NULL, 0);
  74. if (err == -1) {
  75. printf("failed to detect version (sysctlbyname failed\n");
  76. return;
  77. }
  78. printf("build_id: %s\n", build_id);
  79. struct utsname u = {0};
  80. uname(&u);
  81. printf("sysname: %s\n", u.sysname);
  82. printf("nodename: %s\n", u.nodename);
  83. printf("release: %s\n", u.release);
  84. printf("version: %s\n", u.version);
  85. printf("machine: %s\n", u.machine);
  86. // set the offsets
  87. if (strcmp(build_id, "15B93") == 0 || strcmp(build_id, "15B150") == 0 || strcmp(build_id, "15B202") == 0) {
  88. offsets = kstruct_offsets_15B202;
  89. } else {
  90. offsets = kstruct_offsets_15B202;
  91. printf("unknown kernel build. If this is iOS 11 it might still be able to get tfp0, trying anyway\n");
  92. have_syms = 0;
  93. return;
  94. }
  95. }