forked from JonathanSeals/CBPatcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCBPatch.c
2066 lines (1789 loc) · 103 KB
/
CBPatch.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
//
// CBPatch.c
// CBPatcher
//
// Created by JonathanSeals on 11/18/18.
// Copyright © 2018 JonathanSeals. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mac_policy.h"
#include <mach-o/nlist.h>
#include <mach-o/dyld.h>
#include <mach-o/fat.h>
#include <mach/vm_types.h>
#include <dlfcn.h>
#include <sys/mman.h>
#define PatchLog(...) printf("[CBPatch] " __VA_ARGS__)
struct uint48
{
unsigned long long v : 48;
} __attribute__((packed));
/* Some stuff from https://github.com/kpwn/yalu/blob/master/data/dyldmagic/libxnuexp.m, courtesy of qwertyoruiop */
/* Find start of a section in a macho */
struct section *find_section(struct segment_command *seg, const char *name)
{
struct section *sect, *fs = NULL;
uint32_t i = 0;
for (i = 0, sect = (struct section *)((uintptr_t)seg + (uintptr_t)sizeof(struct segment_command));
i < seg->nsects;
i++, sect = (struct section *)((uintptr_t)sect + sizeof(struct section)))
{
if (!strcmp(sect->sectname, name))
{
fs = sect;
break;
}
}
return fs;
}
/* Find start of a load command in a macho */
struct load_command *find_load_command(struct mach_header *mh, uint32_t cmd)
{
struct load_command *lc, *flc;
lc = (struct load_command *)((uintptr_t)mh + sizeof(struct mach_header));
while (1)
{
if ((uintptr_t)lc->cmd == cmd)
{
flc = (struct load_command *)(uintptr_t)lc;
break;
}
lc = (struct load_command *)((uintptr_t)lc + (uintptr_t)lc->cmdsize);
}
return flc;
}
/* Find start of a segment in a macho */
struct segment_command *find_segment(struct mach_header *mh, const char *segname)
{
struct load_command *lc;
struct segment_command *s, *fs = NULL;
lc = (struct load_command *)((uintptr_t)mh + sizeof(struct mach_header));
while ((uintptr_t)lc < (uintptr_t)mh + (uintptr_t)mh->sizeofcmds)
{
if (lc->cmd == LC_SEGMENT)
{
s = (struct segment_command *)lc;
if (!strcmp(s->segname, segname))
{
fs = s;
break;
}
}
lc = (struct load_command *)((uintptr_t)lc + (uintptr_t)lc->cmdsize);
}
return fs;
}
/* Find offset of an exported symbol in a macho */
void *find_sym(struct mach_header *mh, const char *name)
{
struct segment_command *first = (struct segment_command *)find_load_command(mh, LC_SEGMENT);
struct symtab_command *symtab = (struct symtab_command *)find_load_command(mh, LC_SYMTAB);
vm_address_t vmaddr_slide = (vm_address_t)mh - (vm_address_t)first->vmaddr;
char *sym_str_table = (char *)(((char *)mh) + symtab->stroff);
struct nlist *sym_table = (struct nlist *)(((char *)mh) + symtab->symoff);
for (int i = 0; i < symtab->nsyms; i++)
{
if (sym_table[i].n_value && !strcmp(name, &sym_str_table[sym_table[i].n_un.n_strx]))
{
return (void *)(uintptr_t)(sym_table[i].n_value + vmaddr_slide);
}
}
return 0;
}
/* The rest */
/* Find the VM base for prelinked kexts in a kernelcache */
uint32_t find_kextbase(void *kernelcache, size_t size)
{
if (!(*(uint32_t *)&kernelcache[0] == 0xFEEDFACE))
{
PatchLog("This doesn't look like a kernelcache\n");
return 0;
}
struct mach_header *mh = kernelcache;
struct segment_command *sc = (kernelcache + sizeof(struct mach_header));
for (int i = 0; i < mh->ncmds; i++)
{
if (!strcmp(sc->segname, "__PRELINK_TEXT"))
{
uint32_t ret = (sc->vmaddr - sc->fileoff);
return ret;
}
uintptr_t next = (uintptr_t)sc->cmdsize + (void *)sc - kernelcache;
if (next + (uintptr_t)kernelcache > mh->sizeofcmds + (uintptr_t)kernelcache)
{
break;
}
sc = kernelcache + next;
}
return 0;
}
/* Find the beginning of a kext's _TEXT section */
uint32_t find_kext_text_section(void *kernelcache, size_t size, const char *name)
{
if (!(*(uint32_t *)&kernelcache[0] == 0xFEEDFACE))
{
PatchLog("This doesn't look like a kernelcache\n");
return 0;
}
struct mach_header *mh = kernelcache;
struct segment_command *sc = (kernelcache + sizeof(struct mach_header));
for (int i = 0; i < mh->ncmds; i++)
{
if (!strcmp(sc->segname, "__PRELINK_INFO"))
{
uint32_t fileOffToBegin = sc->fileoff;
uint32_t fileSize = sc->filesize;
if (fileSize > size || (fileSize + fileOffToBegin > size))
{
PatchLog("Bounds check error\n");
return 0;
}
void *prelinkInfo = (void *)(kernelcache + fileOffToBegin);
uint32_t requestedKextStart = 0;
/* XML parsing for dummies. Don't do this, kids */
for (int c = 0; c < fileSize; c++)
{
if (!memcmp(prelinkInfo + c, name, strlen(name)))
{
for (int d = c; d < fileSize; d++)
{
if (!memcmp(prelinkInfo + d, "_PrelinkExecutableLoadAddr", strlen("_PrelinkExecutableLoadAddr")))
{
for (int e = d; e < (d + 0x40); e++)
{
if (*(uint16_t *)&prelinkInfo[e] == 0x7830)
{
char addr[0x10];
bzero(&addr, 0x10);
memcpy(&addr, prelinkInfo + e + 2, 0x8);
*(uint8_t *)&addr[0xB - 2] = 0x0;
uint32_t addre = (uint32_t)strtoul(addr, NULL, 16);
if (!(addre < 0x80000000 + fileOffToBegin && addre > 0x80000000))
{
goto done;
}
requestedKextStart = addre;
goto done;
}
}
}
}
}
}
done:;
if (!requestedKextStart)
{
PatchLog("Failed to find beginning of requested kext __text section\n");
return 0;
}
return requestedKextStart;
}
nextSC:;
uintptr_t next = (uintptr_t)sc->cmdsize + (void *)sc - kernelcache;
if (next + (uintptr_t)kernelcache > mh->sizeofcmds + (uintptr_t)kernelcache)
{
break;
}
sc = kernelcache + next;
}
return 0;
}
/*
* Patch an armv7/armv7s kernelcache for use in a jailbreak.
* buf should be a buffer containing a kernelcache macho, len
* should be the size of the macho, version should be a c-string
* containing the iOS version number (such as "9.3.3").
*
* Note: You must disable KASLR to use the sbops patch.
*/
int kernPatOld(void *buf, size_t len, char *version, int nukesb)
{
int i = 0;
int ii = 0;
long versionInt = strtol(version, 0, 0);
float versionFloat = strtof(version, 0);
if (versionInt < (int)8)
{
versionFloat = (float)versionInt;
}
uint32_t kextbase = find_kextbase(buf, len) - 0x80001000;
if (!kextbase)
{
PatchLog("Error finding kextbase\n");
return -1;
}
while ((uint32_t)i < (uint32_t)(len - 0x8000))
{
if (ii == 0)
{
if (versionFloat == (float)9.3)
{
if (*(uint64_t *)&buf[i] == 0x2501d1030f01f01b && *(uint32_t *)&buf[i + 0x8] == 0x2501e016)
{
PatchLog("Found mount_common at 0x%x\n", i + 0x5);
//change conditional branch to unconditional
*(uint8_t *)&buf[i + 0x5] = 0xe0;
ii++;
i = 0;
}
/* Since our expected offset is always on a multiple of 2, add 1 to the cursor to save cycles */
else
{
i += 1;
}
}
else if (versionFloat <= (float)9.0 && versionFloat != 4.0 && versionFloat != 7.0 && versionInt != 8)
{
if ((*(uint64_t *)&buf[i] & 0x00ffffffffffffff) == 0xd4d0060f01f010)
{
PatchLog("Found mount_common at 0x%x\n", i + 0x5);
//change conditional branch to unconditional
*(uint8_t *)&buf[i + 0x5] = 0xe0;
ii++;
i = 0;
}
/* Since our expected offset is always on a multiple of 2, add 1 to the cursor to save cycles */
/* PS: iOS is weird — no guarantees with this mount_common. */
else
{
i += 1;
}
}
else if (versionFloat <= (float)4.0)
{
i = 0;
ii++;
/* from what I know, becuase iOS 4 uses GPT instead of LWVM there is no reason to patch mount_common. But I may be wrong. */
}
else
{
if (*(uint32_t *)&buf[i] == 0x0f01f010 && *(uint8_t *)&buf[i + 0x5] == 0xd0 && *(uint32_t *)&buf[i + 0xe] == 0x0f40f010 && *(uint8_t *)&buf[i + 0x13] == 0xd0)
{
PatchLog("Found mount_common at 0x%x\n", i + 0x5);
//change conditional branch to unconditional
*(uint8_t *)&buf[i + 0x5] = 0xe0;
ii++;
i = 0;
}
/* Since our expected offset is always on a multiple of 2, add 1 to the cursor to save cycles */
else
{
i += 1;
}
}
}
if (ii == 1)
{
/*
* _mapForIO / 9.3.5/j2
* A0 6D LDR R0, [R4,#0x58]
* 40 44 ADD R0, R8
* Offset: 0xbb0726
* Address: 0x80bfa726
*
* Change the lwvm kext's xref to PE_i_can_has_kernel_configuration to _mapForIO
*/
/* This is only enforced on the first LwVM device before 9.3.2. Since we use the third device, skip */
if (versionFloat < (float)9.0)
{
ii++;
i = 0;
continue;
}
i = (uint32_t)find_sym(buf, "_PE_i_can_has_kernel_configuration") - (uint32_t)buf;
if (!i)
{
break;
}
PatchLog("Found PE_i_can_has_kernel_configuration offset at 0x%x\n", i);
uint32_t PE_i_can_has_kernel_configuration = (i + 0x80001000 + 0x1);
i = 0;
int iii = 0;
uint32_t osMallocTagFree = 0;
uint32_t mapforIO = 0;
for (uint32_t a = 0; (uint32_t)a < (uint32_t)(len - 0x8000); a++)
{
if (!iii)
{
a = (uint32_t)find_sym(buf, "_OSMalloc_Tagfree") - (int)buf;
if (!a)
{
break;
}
PatchLog("Found OSMalloc_Tagfree at 0x%x\n", a);
osMallocTagFree = (a + 0x80001000 + 0x1);
a = 0;
iii++;
}
if (iii == 1)
{
if (*(uint64_t *)&buf[a] == 0xf010798044406da0 && *(uint32_t *)&buf[a + 0x8] == 0xd0060f01 && *(uint16_t *)&buf[a + 0xC] == 0x4620)
{
PatchLog("Found _mapForIO at 0x%x\n", a);
mapforIO = ((a + 0x80001000 + kextbase) + 0x1);
iii++;
a = 0;
}
}
if (iii == 2)
{
if (*(uint32_t *)&buf[a] == osMallocTagFree)
{
if (*(uint32_t *)&buf[a + 0x4] == PE_i_can_has_kernel_configuration)
{
PatchLog("Found LwVM call to PE_i_can_has_kernel_configuration at 0x%x\n", a + 0x4);
// Change PE_I_can_has_kernel_configuration call and jumping over partition->isWriteProtected check
*(uint32_t *)&buf[a + 0x4] = mapforIO;
iii++;
a = 0;
}
}
}
if (iii == 3)
{
break;
}
}
if (iii != 3)
{
PatchLog("One or more patches not found %d at line %d\n", iii, __LINE__);
return -1;
}
ii++;
i = 0;
}
if (ii == 2)
{
i = (uint32_t)find_sym(buf, "_PE_i_can_has_debugger") - (uint32_t)buf;
if (!i)
{
break;
}
PatchLog("Found PE_i_can_has_debugger offset at 0x%x\n", i);
for (i = i; i < (i + 0x100); i += 0x2)
{
if (*(uint16_t *)(buf + i) == 0x4770)
{
PatchLog("Found BX LR at 0x%x\n", i);
*(uint32_t *)&buf[i - 0x4] = 0x20012001;
ii++;
i = 0;
break;
}
}
}
if (ii == 3)
{
if (versionFloat == (float)4.0)
{
if (*(uint16_t *)&buf[i] == 0x4620 && *(uint32_t *)&buf[i + 10] == 0xf7bcd053 && *(uint32_t *)&buf[i + 0x10] == 0xf9969000)
{
PatchLog("Found tfp0 at 0x%x\n", i);
for (int a = i; a > (i - 0x40); a -= 2)
{
if (*(uint16_t *)&buf[a] == 0xb5f0)
{
PatchLog("Found tfp0 PUSH at 0x%x\n", a);
for (int e = a; e < (a + 0x20); e += 2)
{
if (*(uint16_t *)&buf[e] == 0xb948)
{
PatchLog("Found tfp0 CBNZ, #0 at 0x%x\n", e);
*(uint32_t *)&buf[e] = 0xe009; //Change CBNZ to B.
goto tfpout;
}
}
}
}
}
}
else if (versionFloat == (float)5.0)
{
if (*(uint16_t *)&buf[i] == 0x4650 && *(uint32_t *)&buf[i + 10] == 0xd04b2d00 && *(uint32_t *)&buf[i + 0x10] == 0xf0002405)
{
PatchLog("Found tfp0 at 0x%x\n", i);
for (int a = i; a > (i - 0x40); a -= 2)
{
if (*(uint16_t *)&buf[a] == 0xb5f0)
{
PatchLog("Found tfp0 PUSH at 0x%x\n", a);
for (int e = a; e < (a + 0x20); e += 2)
{
if (*(uint32_t *)&buf[e] == 0x0f00f1ba)
{
PatchLog("Found tfp0 CMP.W SL, #0 at 0x%x\n", e);
*(uint32_t *)&buf[e + 0x6] = 0xe006; //Change BNE to B.
goto tfpout;
}
}
}
}
}
}
else if (versionFloat == (float)6.0)
{
if (*(uint16_t *)&buf[i] == 0x4650 && *(uint64_t *)&buf[i + 6] == 0xd04e2d0024054605 && *(uint32_t *)&buf[i + 0x10] == 0xf85ef000)
{
PatchLog("Found tfp0 at 0x%x\n", i);
for (int a = i; a > (i - 0x40); a -= 2)
{
if (*(uint16_t *)&buf[a] == 0xb5f0)
{
PatchLog("Found tfp0 PUSH at 0x%x\n", a);
for (int e = a; e < (a + 0x20); e += 2)
{
if (*(uint32_t *)&buf[e] == 0x0f00f1ba)
{
PatchLog("Found tfp0 CMP.W SL, #0 at 0x%x\n", e);
*(uint32_t *)&buf[e + 0x6] = 0xe006; //Change BEQ to B.
goto tfpout;
}
}
}
}
}
}
else if (versionFloat == (float)7.0)
{
if (*(uint16_t *)&buf[i] == 0x4650 && *(uint64_t *)&buf[i + 6] == 0x8110f00028002505 && *(uint32_t *)&buf[i + 0x10] == 0x2500000c)
{
PatchLog("Found tfp0 at 0x%x\n", i);
for (int a = i; a > (i - 0x45); a -= 2)
{
if (*(uint16_t *)&buf[a] == 0xb5f0)
{
PatchLog("Found tfp0 PUSH at 0x%x\n", a);
for (int e = a; e < (a + 0x20); e += 2)
{
if (*(uint32_t *)&buf[e] == 0x0f00f1bb)
{
PatchLog("Found tfp0 CMP.W FL, #0 at 0x%x\n", e);
*(uint32_t *)&buf[e + 0x4] = 0xbf00bf00; //Change BEQ to NOP.
goto tfpout;
}
}
}
}
}
}
else
{
if (*(uint16_t *)&buf[i] == 0x4630 && *(uint64_t *)&buf[i + 6] == 0xf0000f00f1ba4682 && *(uint32_t *)&buf[i + 0x10] == 0xf0014650)
{
PatchLog("Found tfp0 at 0x%x\n", i);
for (int a = i; a > (i - 0x30); a -= 2)
{
if (*(uint16_t *)&buf[a] == 0xb5f0)
{
PatchLog("Found tfp0 PUSH at 0x%x\n", a);
for (int e = a; e < (a + 0x20); e += 2)
{
if (*(uint16_t *)&buf[e] == 0x2e00)
{
PatchLog("Found tfp0 CMP R6, #0 at 0x%x\n", e);
*(uint32_t *)&buf[e + 0x2] = 0xbf00bf00;
goto tfpout;
}
}
}
continue;
tfpout:
ii++;
i = 0;
break;
}
}
}
}
if (ii == 4)
{
/*
* amfi's call to memcmp is being replaced with a jump to a MOV R0, #0 then BX LR.
* This makes any memcmp performed by amfi return 0 (match).
*/
/*
* iOS 4 and iOS 5 approach AMFI ops differently. There will always be a BEQ followed later by a BLX. We patch these out...
*
*/
if (versionFloat == (float)4.0)
{
int iiii = 0;
if (*(uint16_t *)&buf[i] == 0xE000 && *(uint16_t *)&buf[i + 0x6] == 0xe92d && *(uint16_t *)&buf[i + 0x1c] == 0xd056)
{
PatchLog("Found AMFI BEQ at 0x%x\n", i + 0x2e);
*(uint16_t *)&buf[i + 0x1c] = 0xbf00;
}
if (*(uint32_t *)&buf[i] == 0x80015FA1 && *(uint16_t *)&buf[i + 0x4] == 0xb5f0 && *(uint16_t *)&buf[i + 0x30] == 0x4798)
{
PatchLog("Found AMFI BLX at 0x%x\n", i + 0x38);
*(uint16_t *)&buf[i + 0x38] = 0x2000;
ii++;
i = 0;
}
}
else if (versionFloat == (float)5.0)
{
int iiii = 0;
if (*(uint16_t *)&buf[i] == 0x8022 && *(uint16_t *)&buf[i + 0x6] == 0xb5f0 && *(uint16_t *)&buf[i + 0x2e] == 0xd067)
{
PatchLog("Found AMFI BEQ at 0x%x\n", i + 0x2e);
*(uint16_t *)&buf[i + 0x2e] = 0xbf00;
}
if (*(uint32_t *)&buf[i] == 0x80016339 && *(uint16_t *)&buf[i + 0x4] == 0xb5f0 && *(uint16_t *)&buf[i + 0x38] == 0x47d0)
{
PatchLog("Found AMFI BLX at 0x%x\n", i + 0x38);
*(uint16_t *)&buf[i + 0x38] = 0x2000;
ii++;
i = 0;
}
}
else
{
uint32_t memcmpAddress = 0;
i = (uint32_t)find_sym(buf, "_memcmp") - (uint32_t)buf;
if (!i)
{
break;
}
PatchLog("Found memcmp at 0x%x\n", i);
memcmpAddress = (i + (0x80001000 + 1));
uint32_t mach_msg_rpc_from_kernel_proper = 0;
i = 0;
int iii = 0;
uint32_t bxlrGadget = 0;
mach_msg_rpc_from_kernel_proper = (uint32_t)find_sym(buf, "_mach_msg_rpc_from_kernel_proper") - (uint32_t)buf + 0x1;
if (!mach_msg_rpc_from_kernel_proper)
{
break;
}
while ((uint32_t)i < (uint32_t)(len - 0x8000))
{
if (iii == 0)
{
if (*(uint32_t *)&buf[i] == 0x47702000)
{
PatchLog("Found MOV R0, #0 gadget at 0x%x\n", i);
bxlrGadget = ((i + 0x80001000) + 0x1);
iii++;
i = 0;
}
}
if (iii == 1)
{
if (*(uint32_t *)&buf[i] == (uint32_t)mach_msg_rpc_from_kernel_proper + (0x80001000))
{
if (*(uint32_t *)&buf[i + 4] == memcmpAddress)
{
PatchLog("Found AMFI call to memcmp at 0x%x\n", i + 0x4);
*(uint32_t *)&buf[i + 0x4] = bxlrGadget;
iii++;
i = 0;
}
}
}
if (iii == 2)
{
break;
}
i++;
}
if (iii != 2)
{
PatchLog("One or more patches not found %d at line %d\n", iii, __LINE__);
return -1;
}
ii++;
i = 0;
}
}
if (ii == 5)
{
uint32_t seatbeltSandboxPolicyStr = 0;
uint32_t sbops = 0;
if (!memcmp(buf + i, "Seatbelt sandbox policy", strlen("Seatbelt sandbox policy")))
{
seatbeltSandboxPolicyStr = i;
}
else
{
i++;
continue;
}
if (seatbeltSandboxPolicyStr)
{
uint32_t strRef = (seatbeltSandboxPolicyStr + 0x80001000 + kextbase);
PatchLog("Found seatbelt sandbox policy at 0x%x\n", i);
uint32_t strXref = 0;
for (int af = 0; af < (len - 0x10000); af += 1)
{
if (*(uint32_t *)&buf[af] == strRef)
{
strXref = af;
break;
}
}
if (!strXref)
{
break;
}
for (uint32_t cur = seatbeltSandboxPolicyStr; cur > seatbeltSandboxPolicyStr - 0x10000; cur--)
{
if (*(uint32_t *)&buf[cur] == 0xFEEDFACE)
{
PatchLog("Found mach-o header at 0x%x\n", cur);
uint32_t off = 0x0;
for (uint32_t cur1 = strXref; cur1 < (strXref + 0x10); cur1 += 0x1)
{
if (*(uint32_t *)&buf[cur1] == 0x1)
{
off = cur1 + 0x4;
break;
}
}
if (!off)
{
return -1;
}
uint32_t sbops_offset = *(uint32_t *)&buf[off];
sbops_offset = (sbops_offset - (kextbase - 0x80001000)) - 0x2000;
PatchLog("Found sbops at 0x%x\n", sbops_offset);
sbops = sbops_offset;
}
}
if (!sbops)
{
break;
}
if (nukesb)
{
/* 9.2.1+ use mac_policy_ops v46, older 9.x use v37 (xnu/security/mac_policy.h) */
if (versionFloat >= (float)9.2 && strcmp(version, "9.2"))
{
PatchLog("%f Sandbox 9.2 patch\n\n", versionFloat);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_accepted)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_deliver)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_kqfilter)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_select)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_listen)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_received)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_setsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_getsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_link)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_fsgetpath)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_mount_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_ioctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_label_associate_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_label_associate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_socket_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_mount_check_remount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_mount_check_fsctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_mount_check_mount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_rename)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_access)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_chroot)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_mount_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_proc_check_get_task)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_deleteextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_exchangedata)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_exec)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_getattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_getextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_listextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_open)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_readlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_revoke)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setflags)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setmode)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setowner)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_setutimes)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_truncate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_unlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_notify_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_uipc_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_vnode_check_uipc_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_proc_check_setauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_proc_check_getauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_proc_check_fork)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops9, mpo_file_check_mmap)), 0x4);
}
else if (versionFloat >= (float)4.0 && versionFloat < (float)5.0)
{
PatchLog("%f Sandbox patch\n", versionFloat);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_accepted)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_deliver)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_kqfilter)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_select)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_listen)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_received)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_setsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_getsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_link)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_mount_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_ioctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_label_associate_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_label_associate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_socket_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_mount_check_remount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_mount_check_fsctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_mount_check_mount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_access)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_chroot)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_mount_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_proc_check_get_task)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_deleteextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_exchangedata)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_exec)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_getattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_getextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_listextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_open)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_readlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_revoke)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setflags)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setmode)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setowner)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_setutimes)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_truncate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_unlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_notify_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_uipc_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_vnode_check_uipc_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_proc_check_setauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_proc_check_getauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_proc_check_fork)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops4, mpo_file_check_mmap)), 0x4);
}
else if (versionFloat >= (float)5.0 && versionFloat < (float)6.0)
{
PatchLog("%f Sandbox patch\n", versionFloat);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_accepted)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_deliver)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_kqfilter)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_select)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_listen)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_received)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_setsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_getsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_link)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_fsgetpath)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_mount_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_ioctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_label_associate_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_label_associate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_socket_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_mount_check_remount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_mount_check_fsctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_mount_check_mount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_access)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_chroot)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_mount_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_proc_check_get_task)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_deleteextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_exchangedata)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_exec)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_getattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_getextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_listextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_open)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_readlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_revoke)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setattrlist)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setextattr)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setflags)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setmode)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setowner)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_setutimes)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_stat)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_truncate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_unlink)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_notify_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_uipc_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_vnode_check_uipc_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_proc_check_setauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_proc_check_getauid)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_proc_check_fork)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops5, mpo_file_check_mmap)), 0x4);
}
else if (versionFloat >= (float)6.0 && versionFloat < (float)7.0)
{
PatchLog("%f Sandbox patch\n", versionFloat);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_accepted)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_bind)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_connect)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_create)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_deliver)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_kqfilter)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_select)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_listen)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_received)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_setsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_getsockopt)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_vnode_check_link)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_vnode_check_fsgetpath)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_mount_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_vnode_check_ioctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_label_associate_accept)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_label_associate)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_socket_check_label_update)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_mount_check_remount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_mount_check_fsctl)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_mount_check_mount)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_vnode_check_access)), 0x4);
bzero(buf + (sbops + offsetof(struct mac_policy_ops6, mpo_vnode_check_chroot)), 0x4);