-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchfinder.c
1224 lines (914 loc) · 32.1 KB
/
patchfinder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "patchfinder.h"
#include "mach_vm.h"
#include "kernel_memory.h"
#include "tasks.h"
#include "slide.h"
#include "gadget.h"
#define UCHAR_MAX 255
#define INSN_RET 0xD65F03C0, 0xFFFFFFFF
#define INSN_CALL 0x94000000, 0xFC000000
#define INSN_B 0x14000000, 0xFC000000
#define INSN_CBZ 0x34000000, 0x7F000000
#define INSN_ADRP 0x90000000, 0x9F000000
#define INSN_STR8 0xF9000000 | 8, 0xFFC00000 | 0x1F
#define INSN_POPS 0xA9407BFD, 0xFFC07FFF
uint8_t *kernel = NULL;
size_t kernel_size = 0;
mach_vm_address_t kern_entry_point = 0;
mach_vm_address_t kerndumpbase = -1;
void *kernel_mh = NULL;
uint8_t* needle_haystack_memmem(const uint8_t *haystack, size_t hlen,
const uint8_t *needle, size_t nlen)
{
size_t last, scan = 0;
size_t bad_char_skip[UCHAR_MAX + 1];
if(nlen <= 0 || !haystack || !needle)
return NULL;
for(scan = 0; scan <= UCHAR_MAX; scan = scan + 1)
bad_char_skip[scan] = nlen;
last = nlen - 1;
for(scan = 0; scan < last; scan = scan + 1)
bad_char_skip[needle[scan]] = last - scan;
while(hlen >= nlen)
{
for(scan = last; haystack[scan] == needle[scan]; scan = scan - 1)
if(scan == 0)
return (void*) haystack;
hlen -= bad_char_skip[haystack[last]];
haystack += bad_char_skip[haystack[last]];
}
return NULL;
}
bool auth_ptrs = false;
bool monolithic_kernel = false;
static mach_vm_address_t xnucore_base = 0;
static mach_vm_address_t xnucore_size = 0;
static mach_vm_address_t ppl_base = 0;
static mach_vm_address_t ppl_size = 0;
static mach_vm_address_t prelink_base = 0;
static mach_vm_address_t prelink_size = 0;
static mach_vm_address_t cstring_base = 0;
static mach_vm_address_t cstring_size = 0;
static mach_vm_address_t pstring_base = 0;
static mach_vm_address_t pstring_size = 0;
static mach_vm_address_t oslstring_base = 0;
static mach_vm_address_t oslstring_size = 0;
static mach_vm_address_t data_base = 0;
static mach_vm_address_t data_size = 0;
static mach_vm_address_t data_const_base = 0;
static mach_vm_address_t data_const_size = 0;
static mach_vm_address_t const_base = 0;
static mach_vm_address_t const_size = 0;
#define IS64(image) (*(uint8_t *)(image) & 1)
bool patchfinder_init(char *gadget_file)
{
bool ok;
uint8_t buf[0x4000];
unsigned int i, j;
const struct mach_header *hdr = (struct mach_header*) buf;
const uint8_t *q;
mach_vm_address_t min = -1;
mach_vm_address_t max = 0;
int is64 = false;
ok = construct_gadgets(gadget_file);
if(!ok)
{
fprintf(stderr, "Could not construct_gadgets()\n");
exit(-1);
}
ok = kernel_read(kernel_base, buf, sizeof(buf));
if(!ok)
return ok;
if(IS64(buf))
is64 = 4;
q = buf + sizeof(struct mach_header) + is64;
for(i = 0; i < hdr->ncmds; i++)
{
const struct load_command *cmd = (struct load_command*)q;
if(cmd->cmd == LC_SEGMENT_64)
{
const struct segment_command_64 *seg = (struct segment_command_64*)q;
if(min > seg->vmaddr && seg->vmsize > 0)
min = seg->vmaddr;
if(max < seg->vmaddr + seg->vmsize && seg->vmsize > 0)
max = seg->vmaddr + seg->vmsize;
const int prot = VM_PROT_READ | VM_PROT_EXECUTE;
if ((seg->initprot & prot) == prot || (seg->maxprot & prot) == prot)
{
void *data;
uint64_t vmaddr = seg->vmaddr;
size_t vmsize = seg->vmsize;
data = malloc(vmsize);
kernel_read(vmaddr, data, vmsize);
find_gadgets(data, vmaddr, vmsize);
free(data);
}
if(!strcmp(seg->segname, "__TEXT"))
{
xnucore_base = seg->vmaddr;
xnucore_size = seg->filesize;
} else if (!strcmp(seg->segname, "__PPLDATA")) {
auth_ptrs = true;
} else if (!strcmp(seg->segname, "__PPLTEXT")) {
ppl_base = seg->vmaddr;
ppl_size = seg->filesize;
} else if (!strcmp(seg->segname, "__PLK_TEXT_EXEC")) {
prelink_base = seg->vmaddr;
prelink_size = seg->filesize;
} else if (!strcmp(seg->segname, "__TEXT")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__cstring")) {
cstring_base = sec[j].addr;
cstring_size = sec[j].size;
} else if (!strcmp(sec[j].sectname, "__os_log")) {
oslstring_base = sec[j].addr;
oslstring_size = sec[j].size;
} else if (!strcmp(sec[j].sectname, "__const")) {
const_base = sec[j].addr;
const_size = sec[j].size;
}
}
} else if (!strcmp(seg->segname, "__PRELINK_TEXT")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__text")) {
pstring_base = sec[j].addr;
pstring_size = sec[j].size;
}
}
} else if (!strcmp(seg->segname, "__DATA_CONST")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__const")) {
data_const_base = sec[j].addr;
data_const_size = sec[j].size;
}
}
} else if (!strcmp(seg->segname, "__DATA")) {
const struct section_64 *sec = (struct section_64 *)(seg + 1);
for (j = 0; j < seg->nsects; j++) {
if (!strcmp(sec[j].sectname, "__data")) {
data_base = sec[j].addr;
data_size = sec[j].size;
}
}
}
} else if (cmd->cmd == LC_UNIXTHREAD) {
uint32_t *ptr = (uint32_t *)(cmd + 1);
uint32_t flavor = ptr[0];
struct {
uint64_t x[29]; /* General purpose registers x0-x28 */
uint64_t fp; /* Frame pointer x29 */
uint64_t lr; /* Link register x30 */
uint64_t sp; /* Stack pointer x31 */
uint64_t pc; /* Program counter */
uint32_t cpsr; /* Current program status register */
} *thread = (void *)(ptr + 2);
if (flavor == 6) {
kern_entry_point = thread->pc;
}
}
q = q + cmd->cmdsize;
}
if(prelink_size == 0)
{
monolithic_kernel = true;
prelink_base = xnucore_base;
prelink_size = xnucore_size;
pstring_base = cstring_base;
pstring_size = cstring_size;
}
kerndumpbase = min;
xnucore_base -= kerndumpbase;
cstring_base -= kerndumpbase;
ppl_base -= kerndumpbase;
oslstring_base -= kerndumpbase;
data_const_base -= kerndumpbase;
data_base -= kerndumpbase;
const_base -= kerndumpbase;
kernel_size = max - min;
kernel = malloc(kernel_size);
if(!kernel)
return false;
ok = kernel_read(kerndumpbase, kernel, kernel_size);
if(!ok)
{
fprintf(stderr, "could not read from kernel dump base %llu\n", kerndumpbase);
free(kernel);
kernel = NULL;
return false;
}
kernel_mh = kernel + kernel_base - min;
return true;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
mach_vm_address_t step64(const uint8_t *buf,
mach_vm_address_t start,
size_t length,
uint32_t what,
uint32_t mask)
{
mach_vm_address_t end = start + length;
while (start < end) {
uint32_t x = *(uint32_t *)(buf + start);
if ((x & mask) == what) {
return start;
}
start += 4;
}
return 0;
}
mach_vm_address_t step64_back(const uint8_t *buf,
mach_vm_address_t start,
size_t length,
uint32_t what,
uint32_t mask)
{
mach_vm_address_t end = start - length;
while (start >= end) {
uint32_t x = *(uint32_t *)(buf + start);
if ((x & mask) == what) {
return start;
}
start -= 4;
}
return 0;
}
mach_vm_address_t step_adrp_to_reg(const uint8_t *buf,
mach_vm_address_t start,
size_t length,
int reg)
{
return step64(buf, start, length, 0x90000000 | (reg&0x1F), 0x9F00001F);
}
mach_vm_address_t xref64(const uint8_t *buf,
mach_vm_address_t start,
mach_vm_address_t end,
mach_vm_address_t what)
{
mach_vm_address_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for(i = start & ~3; i < end; i += 4)
{
uint32_t op = *(uint32_t*)(buf + i);
unsigned reg = op & 0x1F;
if ((op & 0x9F000000) == 0x90000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
continue; // XXX should not XREF on its own?
/*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
unsigned rd = op & 0x1F;
unsigned rm = (op >> 16) & 0x1F;
//printf("%llx: MOV X%d, X%d\n", i, rd, rm);
value[rd] = value[rm];*/
} else if ((op & 0xFF000000) == 0x91000000) {
unsigned rn = (op >> 5) & 0x1F;
if (rn == 0x1f) {
// ignore ADD Xn, SP
value[reg] = 0;
continue;
}
unsigned shift = (op >> 22) & 3;
unsigned imm = (op >> 10) & 0xFFF;
if (shift == 1) {
imm <<= 12;
} else {
//assert(shift == 0);
if (shift > 1) continue;
}
//printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
value[reg] = value[rn] + imm;
} else if ((op & 0xF9C00000) == 0xF9400000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
/*} else if ((op & 0xF9C00000) == 0xF9000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[rn] = value[rn] + imm; // XXX address, not actual value*/
} else if ((op & 0x9F000000) == 0x10000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
value[reg] = ((long long)adr >> 11) + i;
} else if ((op & 0xFF000000) == 0x58000000) {
unsigned adr = (op & 0xFFFFE0) >> 3;
//printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
value[reg] = adr + i; // XXX address, not actual value
} else if ((op & 0xFC000000) == 0x94000000) {
// BL addr
signed imm = (op & 0x3FFFFFF) << 2;
if (op & 0x2000000) {
imm |= 0xf << 28;
}
unsigned adr = (unsigned)(i + imm);
if (adr == what) {
return i;
}
}
// Don't match SP as an offset
if (value[reg] == what && reg != 0x1f) {
return i;
}
}
return 0;
}
mach_vm_address_t bof64(const uint8_t *buf, mach_vm_address_t start, mach_vm_address_t where)
{
for (; where >= start; where -= 4) {
uint32_t op = *(uint32_t *)(buf + where);
if ((op & 0xFFC003FF) == 0x910003FD) {
unsigned delta = (op >> 10) & 0xFFF;
//printf("0x%llx: ADD X29, SP, #0x%x\n", where + kerndumpbase, delta);
if ((delta & 0xF) == 0) {
mach_vm_address_t prev = where - ((delta >> 4) + 1) * 4;
uint32_t au = *(uint32_t *)(buf + prev);
//printf("0x%llx: (%llx & %llx) == %llx\n", prev + kerndumpbase, au, 0x3BC003E0, au & 0x3BC003E0);
if ((au & 0x3BC003E0) == 0x298003E0) {
//printf("%x: STP x, y, [SP,#-imm]!\n", prev);
return prev;
} else if ((au & 0x7F8003FF) == 0x510003FF) {
//printf("%x: SUB SP, SP, #imm\n", prev);
return prev;
}
for (mach_vm_address_t diff = 4; diff < delta/4+4; diff+=4) {
uint32_t ai = *(uint32_t *)(buf + where - diff);
// SUB SP, SP, #imm
//printf("0x%llx: (%llx & %llx) == %llx\n", where - diff + kerndumpbase, ai, 0x3BC003E0, ai & 0x3BC003E0);
if ((ai & 0x7F8003FF) == 0x510003FF) {
return where - diff;
}
// Not stp and not str
if (((ai & 0xFFC003E0) != 0xA90003E0) && (ai&0xFFC001F0) != 0xF90001E0) {
break;
}
}
// try something else
while (where > start) {
where -= 4;
au = *(uint32_t *)(buf + where);
// SUB SP, SP, #imm
if ((au & 0xFFC003FF) == 0xD10003FF && ((au >> 10) & 0xFFF) == delta + 0x10) {
return where;
}
// STP x, y, [SP,#imm]
if ((au & 0xFFC003E0) != 0xA90003E0) {
where += 4;
break;
}
}
}
}
}
return 0;
}
mach_vm_address_t calc64(const uint8_t *buf, mach_vm_address_t start, mach_vm_address_t end, int which)
{
mach_vm_address_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
unsigned reg = op & 0x1F;
if ((op & 0x9F000000) == 0x90000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
/*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
unsigned rd = op & 0x1F;
unsigned rm = (op >> 16) & 0x1F;
//printf("%llx: MOV X%d, X%d\n", i, rd, rm);
value[rd] = value[rm];*/
} else if ((op & 0xFF000000) == 0x91000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned shift = (op >> 22) & 3;
unsigned imm = (op >> 10) & 0xFFF;
if (shift == 1) {
imm <<= 12;
} else {
//assert(shift == 0);
if (shift > 1) continue;
}
//printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
value[reg] = value[rn] + imm;
} else if ((op & 0xF9C00000) == 0xF9400000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
} else if ((op & 0xF9C00000) == 0xF9000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[rn] = value[rn] + imm; // XXX address, not actual value
} else if ((op & 0x9F000000) == 0x10000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
value[reg] = ((long long)adr >> 11) + i;
} else if ((op & 0xFF000000) == 0x58000000) {
unsigned adr = (op & 0xFFFFE0) >> 3;
//printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
value[reg] = adr + i; // XXX address, not actual value
} else if ((op & 0xF9C00000) == 0xb9400000) { // 32bit
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 2;
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
}
}
return value[which];
}
mach_vm_address_t find_call64(const uint8_t *buf, mach_vm_address_t start, size_t length)
{
return step64(buf, start, length, INSN_CALL);
}
mach_vm_address_t follow_call64(const uint8_t *buf, mach_vm_address_t call)
{
long long w;
w = *(uint32_t *)(buf + call) & 0x3FFFFFF;
w <<= 64 - 26;
w >>= 64 - 26 - 2;
return call + w;
}
mach_vm_address_t follow_stub(const uint8_t *buf, mach_vm_address_t call)
{
mach_vm_address_t stub = follow_call64(buf, call);
if (!stub) return 0;
if (monolithic_kernel) {
return stub + kerndumpbase;
}
mach_vm_address_t target_function_offset = calc64(buf, stub, stub+4*3, 16);
if (!target_function_offset) return 0;
return *(mach_vm_address_t*)(buf + target_function_offset);
}
mach_vm_address_t follow_cbz(const uint8_t *buf, mach_vm_address_t cbz)
{
return cbz + ((*(int *)(buf + cbz) & 0x3FFFFE0) << 10 >> 13);
}
mach_vm_address_t remove_pac(mach_vm_address_t addr)
{
if (addr >= kerndumpbase) return addr;
if (addr >> 56 == 0x80) {
return (addr&0xffffffff) + kerndumpbase;
}
return addr |= 0xfffffff000000000;
}
#pragma GCC diagnostic pop
mach_vm_address_t find_reference(mach_vm_address_t to, int n, enum text_bases text_base)
{
mach_vm_address_t ref, end;
mach_vm_address_t base = xnucore_base;
size_t size = xnucore_size;
switch(text_base)
{
case text_xnucore_base:
break;
case text_ppl_base:
if(ppl_base != 0-kerndumpbase)
{
base = ppl_base;
size = ppl_size;
}
break;
default:
fprintf(stdout, "Unknown base %d\n", text_base);
return 0;
break;
}
if(n <= 0)
n = 1;
end = base + size;
to -= kerndumpbase;
do
{
ref = xref64(kernel, base, end, to);
if(!ref)
return 0;
base = ref + 4;
} while(--n > 0);
return ref + kerndumpbase;
}
mach_vm_address_t find_strref(const char *string, int n, enum string_bases string_base, bool full_match, bool ppl_base)
{
uint8_t *str;
mach_vm_address_t base;
mach_vm_address_t size;
enum text_bases text_base = ppl_base ? text_ppl_base : text_xnucore_base;
switch(string_base)
{
case string_base_const:
base = const_base;
size = const_size;
break;
case string_base_data:
base = data_base;
size = data_size;
break;
case string_base_oslstring:
base = oslstring_base;
size = oslstring_size;
break;
case string_base_cstring:
default:
base = cstring_base;
size = cstring_size;
break;
}
mach_vm_address_t offset = 0;
while((str = needle_haystack_memmem(kernel + base + offset, size - offset, (uint8_t*)string, strlen(string))))
{
if((str == kernel + base || *(str-1) == '\0') && (!full_match || strcmp((char*)str, string) == 0))
break;
offset = str - (kernel + base) + 1;
}
if(!str)
return 0;
return find_reference(str - kernel + kerndumpbase, n, text_base);
}
mach_vm_address_t find_string(const char *string)
{
uint8_t *str = needle_haystack_memmem(kernel, kernel_size, (uint8_t *)string, strlen(string));
if (!str) return 0;
return str - kernel + kerndumpbase;
}
mach_vm_address_t find_symbol(const char *symbol)
{
if(!symbol)
return 0;
unsigned i;
const struct mach_header *hdr = kernel_mh;
const uint8_t *q;
int is64 = 0;
// apparently this only works on a decrypted kernel, but kernelcaches have symbols stripped!
// so idk if this will ever work. the undecimus code never uses this anyways
do { return 0; } while(0);
if(IS64(hdr))
is64 = 4;
q = (uint8_t*)(hdr + 1) + is64;
for(i = 0; i < hdr->ncmds; i++)
{
const struct load_command *cmd = (struct load_command*) q;
if(cmd->cmd == LC_SYMTAB)
{
const struct symtab_command *sym = (struct symtab_command*) q;
const char *stroff = (const char*) kernel_mh + sym->stroff;
if(is64)
{
const struct nlist_64 *s = (struct nlist_64*) kernel_mh + sym->symoff;
for(uint32_t k = 0; k < sym->nsyms; k++)
{
if(s[k].n_type & N_STAB)
continue;
if(s[k].n_value && (s[k].n_type & N_TYPE) != N_INDR)
{
if(!strcmp(symbol, stroff + s[k].n_un.n_strx))
return s[k].n_value;
}
}
}
}
q = q + cmd->cmdsize;
}
return 0;
}
mach_vm_address_t find_paciza_pointer__l2tp_domain_module_start()
{
uint64_t string = (uint64_t)needle_haystack_memmem(kernel + data_base, data_size, (const uint8_t *)"com.apple.driver.AppleSynopsysOTGDevice", strlen("com.apple.driver.AppleSynopsysOTGDevice")) - (uint64_t)kernel;
if (!string) return 0;
return string + kerndumpbase - 0x20;
}
mach_vm_address_t find_paciza_pointer__l2tp_domain_module_stop()
{
uint64_t string = (uint64_t)needle_haystack_memmem(kernel + data_base, data_size, (const uint8_t *)"com.apple.driver.AppleSynopsysOTGDevice", strlen("com.apple.driver.AppleSynopsysOTGDevice")) - (uint64_t)kernel;
if (!string) return 0;
return string + kerndumpbase - 0x18;
}
mach_vm_address_t find_sysctl__net_ppp_l2tp()
{
uint64_t ref = find_strref("L2TP domain init\n", 1, string_base_cstring, false, false);
if (!ref) return 0;
ref -= kerndumpbase;
uint64_t addr = calc64(kernel, ref, ref + 32, 8);
if (!addr) return 0;
return addr + kerndumpbase;
}
mach_vm_address_t find_sysctl_unregister_oid()
{
uint64_t ref = find_strref("L2TP domain terminate : PF_PPP domain does not exist...\n", 1, string_base_cstring, false, false);
if (!ref) return 0;
ref -= kerndumpbase;
ref += 4;
uint64_t addr = calc64(kernel, ref, ref + 28, 0);
if (!addr) return 0;
return addr + kerndumpbase;
}
mach_vm_address_t find_mov_x0_x4__br_x5()
{
uint32_t bytes[] = {
0xaa0403e0, // mov x0, x4
0xd61f00a0 // br x5
};
uint64_t addr = (uint64_t)needle_haystack_memmem((uint8_t *)((uint64_t)kernel + xnucore_base), xnucore_size, (const uint8_t *)bytes, sizeof(bytes));
if (!addr) return 0;
return addr - (mach_vm_address_t)kernel + kerndumpbase;
}
mach_vm_address_t find_mov_x9_x0__br_x1()
{
uint32_t bytes[] = {
0xaa0003e9, // mov x9, x0
0xd61f0020 // br x1
};
uint64_t addr = (uint64_t)needle_haystack_memmem((unsigned char *)((uint64_t)kernel + xnucore_base), xnucore_size, (const unsigned char *)bytes, sizeof(bytes));
if (!addr) return 0;
return addr - (uint64_t)kernel + kerndumpbase;
}
mach_vm_address_t find_mov_x10_x3__br_x6()
{
uint32_t bytes[] = {
0xaa0303ea, // mov x10, x3
0xd61f00c0 // br x6
};
uint64_t addr = (uint64_t)needle_haystack_memmem((unsigned char *)((uint64_t)kernel + xnucore_base), xnucore_size, (const unsigned char *)bytes, sizeof(bytes));
if (!addr) return 0;
return addr - (uint64_t)kernel + kerndumpbase;
}
mach_vm_address_t find_kernel_forge_pacia_gadget()
{
uint32_t bytes[] = {
0xdac10149, // paci
0xf9007849 // str x9, [x2, #240]
};
uint64_t addr = (uint64_t)needle_haystack_memmem((uint8_t *)((uint64_t)kernel + xnucore_base), xnucore_size, (const uint8_t *)bytes, sizeof(bytes));
if (!addr) return 0;
return addr - (mach_vm_address_t)kernel + kerndumpbase;
}
mach_vm_address_t find_kernel_forge_pacda_gadget()
{
uint32_t bytes[] = {
0xdac10949, // pacd x9
0xf9007449 // str x9, [x2, #232]
};
uint64_t addr = (uint64_t)needle_haystack_memmem((unsigned char *)((uint64_t)kernel + xnucore_base), xnucore_size, (const unsigned char *)bytes, sizeof(bytes));
if (!addr) return 0;
return addr - (uint64_t)kernel + kerndumpbase;
}
mach_vm_address_t find_zone_map_ref()
{
mach_vm_address_t ref = find_strref("\"Nothing being freed to the zone_map. start = end = %p\\n\"", 1, string_base_cstring, false, false);
if(!ref)
return 0;
uint64_t val = kerndumpbase;
ref -= val;
// skip add & adrp for panic str
ref -= 8;
// adrp xX, #_zone_map@PAGE
ref = step64_back(kernel, ref, 30, INSN_ADRP);
if(!ref)
return 0;
uint32_t *insn = (uint32_t*)(kernel + ref);
// get pc
val += ((uint8_t*)(insn) - kernel) & ~0xfff;
uint8_t xm = *insn & 0x1f;
// he wrote this at 5am, not sure wtf is going on here lol
val += (*insn << 9 & 0x1ffffc000) | (*insn >> 17 & 0x3000);
// ldr x, [xX, #_zone_map@PAGEOFF]
++insn;
if(((*insn & 0xF9C00000) != 0xF9400000))
return 0;
// xd == xX, xn == xX
if ((*insn & 0x1f) != xm || ((*insn >> 5) & 0x1f) != xm)
return 0;
val += ((*insn >> 10) & 0xFFF) << 3;
return val;
}
mach_vm_address_t find_OSUnserializeXML()
{
mach_vm_address_t ref = find_strref("OSUnserializeXML: %s near line %d\n", 1, string_base_cstring, false, false);
if(!ref)
return 0;
ref -= kerndumpbase;
uint64_t start = bof64(kernel, xnucore_base, ref);
if(!start)
return 0;
if(monolithic_kernel)
{
ref = find_reference(start + kerndumpbase, 1, false);
if(!ref) return 0;
ref -= kerndumpbase;
start = bof64(kernel, xnucore_base, ref);
if(!start) return 0;
}
return start + kerndumpbase;
}
mach_vm_address_t find_gPhysBase()
{
mach_vm_address_t ret, val;
mach_vm_address_t ref = find_strref("\"pmap_map_high_window_bd: insufficient pages", 1, string_base_cstring, false, false);
if(!ref) return 0;
ref -= kerndumpbase;
ret = step64(kernel, ref, 64, INSN_RET);
if(!ret)
{
ref = step64(kernel, ref, 1024, INSN_RET);
if(!ret) return 0;
ret = step64(kernel, ref + 4, 64, INSN_RET);
if(!ret) return 0;
}
val = calc64(kernel, ref, ret, 8);
if(!val) return 0;
return val + kerndumpbase;
}
mach_vm_address_t find_kernel_pmap()
{
mach_vm_address_t call, bof, val;
mach_vm_address_t ref = find_strref("\"pmap_map_bd\"", 1, string_base_cstring, false, false);
if(!ref) return 0;
ref -= kerndumpbase;
call = step64_back(kernel, ref, 64, INSN_CALL);
if(!call) return 0;
bof = bof64(kernel, xnucore_base, call);
if(!bof) return 0;
val = calc64(kernel, bof, call, 2);
if(!val) return 0;
return val + kerndumpbase;
}
mach_vm_address_t find_trustcache()
{
mach_vm_address_t ref;
if (auth_ptrs)
{
mach_vm_address_t ref = find_strref("\"loadable trust cache buffer too small (%ld) for entries claimed (%d)\"", 1, string_base_cstring, false, true);
if (!ref) return 0;
ref -= kerndumpbase;
mach_vm_address_t val = calc64(kernel, ref-32*4, ref-24*4, 8);
if (!val) return 0;
return val + kerndumpbase;
}
mach_vm_address_t call, func, val, adrp;
int reg;
uint32_t op;
ref = find_strref("%s: only allowed process can check the trust cache", 1, string_base_pstring, false, false); // Trying to find AppleMobileFileIntegrityUserClient::isCdhashInTrustCache
if (!ref) return 0;
ref -= kerndumpbase;
call = step64_back(kernel, ref, 11 * 4, INSN_CALL);
if (!call) return 0;
func = follow_call64(kernel, call);
if (!func) return 0;
call = step64(kernel, func, 8 * 4, INSN_CALL);
if (!call) return 0;
func = follow_call64(kernel, call);
if (!func) return 0;
call = step64(kernel, func, 8 * 4, INSN_CALL);
if (!call) return 0;
call = step64(kernel, call + 4, 8 * 4, INSN_CALL);
if (!call) return 0;
func = follow_call64(kernel, call);