This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
forked from mineek/Kernel64Patcher
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Kernel64Patcher.c
686 lines (561 loc) · 28.1 KB
/
Kernel64Patcher.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
/*
* Copyright 2020, @Ralph0045
* gcc Kernel64Patcher.c -o Kernel64Patcher
*/
#ifdef __gnu_linux__
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "patchfinder64.c"
#define GET_OFFSET(kernel_len, x) (x - (uintptr_t) kernel_buf)
static uint32_t arm64_branch_instruction(uintptr_t from, uintptr_t to) {
return from > to ? 0x18000000 - (from - to) / 4 : 0x14000000 + (to - from) / 4;
}
// iOS 15 "%s: firmware validation failed %d\" @%s:%d SPU Firmware Validation Patch
int get_SPUFirmwareValidation_patch(void *kernel_buf, size_t kernel_len) {
printf("%s: Entering ...\n",__FUNCTION__);
char rootvpString[43] = "\"%s: firmware validation failed %d\" @%s:%d";
void* ent_loc = memmem(kernel_buf,kernel_len,rootvpString,42);
if(!ent_loc) {
printf("%s: Could not find \"%%s: firmware validation failed %%d\" @%%s:%%d string\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"%%s: firmware validation failed %%d\" @%%s:%%d\" str loc at %p\n",__FUNCTION__,GET_OFFSET(kernel_len,ent_loc));
addr_t xref_stuff = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!xref_stuff) {
printf("%s: Could not find \"%%s: firmware validation failed %%d\" @%%s:%%d xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"%%s: firmware validation failed %%d\" @%%s:%%d\" ref at %p\n",__FUNCTION__,(void*)xref_stuff);
addr_t beg_func = bof64(kernel_buf,0,xref_stuff);
if(!beg_func) {
printf("%s: Could not find firmware validation function start\n",__FUNCTION__);
return -1;
}
xref_stuff = xref64code(kernel_buf,0,(addr_t)GET_OFFSET(kernel_len, beg_func), beg_func);
if(!xref_stuff) {
printf("%s: Could not find previous xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found function xref at %p\n",__FUNCTION__,(void*)xref_stuff);
addr_t next_bl = step64_back(kernel_buf, xref_stuff, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find previous bl\n",__FUNCTION__);
return -1;
}
next_bl = step64_back(kernel_buf, (next_bl - 0x4), 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find previous bl\n",__FUNCTION__);
return -1;
}
next_bl = step64_back(kernel_buf, (next_bl - 0x4), 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find previous bl\n",__FUNCTION__);
return -1;
}
beg_func = bof64(kernel_buf,0,next_bl);
if(!beg_func) {
printf("%s: Could not find start of firmware validation function\n",__FUNCTION__);
return -1;
}
printf("%s: Patching SPU Firmware Validation at %p\n\n", __FUNCTION__,(void*)(beg_func));
*(uint32_t *) (kernel_buf + beg_func) = 0xD65F03C0;
return 0;
}
// iOS 15 rootvp not authenticated after mounting Patch
int get_RootVPNotAuthenticatedAfterMounting_patch(void *kernel_buf, size_t kernel_len) {
printf("%s: Entering ...\n",__FUNCTION__);
char rootVPString[40] = "rootvp not authenticated after mounting";
char md0String[3] = "md0";
void* ent_loc = memmem(kernel_buf,kernel_len,md0String,3);
if(!ent_loc) {
printf("%s: Could not find \"md0\" string\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"md0\" str loc at %p\n",__FUNCTION__,GET_OFFSET(kernel_len,ent_loc));
addr_t xref_stuff = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!xref_stuff) {
printf("%s: Could not find \"md0\" xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"md0\" ref at %p\n",__FUNCTION__,(void*)xref_stuff);
addr_t next_bl = step64(kernel_buf, xref_stuff + 0x8, 100, INSN_CALL);
if(!next_bl) {
// Newer devices will fail here, so using another string is required
printf("%s: Failed to use \"md0\", swapping to \"rootvp not authenticated after mounting\"\n",__FUNCTION__);
ent_loc = memmem(kernel_buf,kernel_len,rootVPString,39);
if(!ent_loc) {
printf("%s: Could not find \"rootvp not authenticated after mounting\" string\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"rootvp not authenticated after mounting\" str loc at %p\n",__FUNCTION__,GET_OFFSET(kernel_len,ent_loc));
xref_stuff = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!xref_stuff) {
printf("%s: Could not find \"rootvp not authenticated after mounting\" xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"rootvp not authenticated after mounting\" str xref at %p\n",__FUNCTION__,(void*)xref_stuff);
addr_t beg_func = bof64(kernel_buf,0,xref_stuff);
if(!beg_func) {
printf("%s: Could not find function start\n",__FUNCTION__);
return -1;
}
beg_func = beg_func + 0xA98;
printf("%s: Found function start at %p\n",__FUNCTION__,(void*)beg_func);
next_bl = step64(kernel_buf, beg_func, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
} else {
next_bl = step64(kernel_buf, next_bl + 0x8, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
next_bl = step64(kernel_buf, next_bl + 0x8, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
next_bl = step64(kernel_buf, next_bl + 0x8, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
next_bl = step64(kernel_buf, next_bl + 0x8, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
}
printf("%s: Patching ROOTVP at %p\n\n", __FUNCTION__,(void*)(next_bl + 0x4));
*(uint32_t *) (kernel_buf + next_bl + 0x4) = 0xD503201F;
return 0;
}
// iOS 15 AMFI Kernel Patch
int get_AMFIInitializeLocalSigningPublicKey_patch(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n",__FUNCTION__);
char AMFIString[52] = "\"AMFI: %s: unable to obtain local signing public key";
void* ent_loc = memmem(kernel_buf,kernel_len,AMFIString,51);
if(!ent_loc) {
printf("%s: Could not find \"AMFI: %%s: unable to obtain local signing public key\" string\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"AMFI: %%s: unable to obtain local signing public key\" str loc at %p\n",__FUNCTION__,GET_OFFSET(kernel_len,ent_loc));
addr_t xref_stuff = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!xref_stuff) {
printf("%s: Could not find \"AMFI: %%s: unable to obtain local signing public key\" xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"AMFI: %%s: unable to obtain local signing public key ref at %p\n",__FUNCTION__,(void*)xref_stuff);
printf("%s: Patching \"Local Signing Public Key\" at %p\n\n", __FUNCTION__,(void*)(xref_stuff + 0x4));
*(uint32_t *) (kernel_buf + xref_stuff + 0x4) = 0xD503201F;
return 0;
}
//iOS 14 AppleFirmwareUpdate img4 signature check
int get_AppleFirmwareUpdate_img4_signature_check(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n",__FUNCTION__);
char img4_sig_check_string[56] = "%s::%s() Performing img4 validation outside of workloop";
void* ent_loc = memmem(kernel_buf,kernel_len,img4_sig_check_string,55);
if(!ent_loc) {
printf("%s: Could not find \"%%s::%%s() Performing img4 validation outside of workloop\" string\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"%%s::%%s() Performing img4 validation outside of workloop\" str loc at %p\n",__FUNCTION__,GET_OFFSET(kernel_len,ent_loc));
addr_t ent_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!ent_ref) {
printf("%s: Could not find \"%%s::%%s() Performing img4 validation outside of workloop\" xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"%%s::%%s() Performing img4 validation outside of workloop\" xref at %p\n",__FUNCTION__,(void*)ent_ref);
printf("%s: Patching \"%%s::%%s() Performing img4 validation outside of workloop\" at %p\n\n", __FUNCTION__,(void*)(ent_ref + 0xc));
*(uint32_t *) (kernel_buf + ent_ref + 0xc) = 0xD2800000;
return 0;
}
static addr_t
cbz_ref64_back(const uint8_t *buf, addr_t start, size_t length) {
//find cbz/cbnz
uint32_t cbz_mask = 0x7E000000;
uint32_t instr = 0;
uint32_t imm = 0;
addr_t cbz = start;
while (cbz) {
instr = *(uint32_t *) (buf + cbz);
if ((instr & cbz_mask) == 0x34000000) {
imm = ((instr & 0x00FFFFFF) >> 5) << 2;
if (cbz + imm == start)
return cbz;
}
cbz -= 4;
}
return 0;
}
//iOS 15 "could not authenticate personalized root hash!" patch
int get_could_not_authenticate_personalized_root_hash_patch(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n", __FUNCTION__);
//get target offset for new branch
char roothash_authenticated_string[sizeof("successfully validated on-disk root hash")] = "successfully validated on-disk root hash";
unsigned char *roothash_authenticated_loc = memmem(kernel_buf, kernel_len, roothash_authenticated_string, sizeof("successfully validated on-disk root hash") - 1);
if(!roothash_authenticated_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, roothash_authenticated_string);
return -1;
}
for (; *roothash_authenticated_loc != 0; roothash_authenticated_loc--);
roothash_authenticated_loc++;
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, roothash_authenticated_string, GET_OFFSET(kernel_len, roothash_authenticated_loc));
addr_t roothash_authenticated_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, roothash_authenticated_loc));
if(!roothash_authenticated_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, roothash_authenticated_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, roothash_authenticated_string, (void*) roothash_authenticated_ref);
//get previous cbz
addr_t branch_target = step64_back(kernel_buf, roothash_authenticated_ref, 20 * 4, 0x34000000, 0x7E000000);
if(!branch_target) {
printf("%s: Could not find previous cbz\n",__FUNCTION__);
return -1;
}
printf("%s: Found previous cbz at %p\n",__FUNCTION__, (void*) branch_target);
branch_target++;
//get patching offset for new branch
char roothash_failed_string[sizeof("could not authenticate personalized root hash!")] = "could not authenticate personalized root hash!";
unsigned char *roothash_failed_loc = memmem(kernel_buf, kernel_len, roothash_failed_string, sizeof("could not authenticate personalized root hash!") - 1);
if(!roothash_failed_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, roothash_failed_string);
return -1;
}
for (; *roothash_failed_loc != 0; roothash_failed_loc--);
roothash_failed_loc++;
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, roothash_failed_string, GET_OFFSET(kernel_len, roothash_failed_loc));
addr_t roothash_failed_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, roothash_failed_loc));
if(!roothash_failed_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, roothash_failed_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, roothash_failed_string, (void*) roothash_failed_ref);
addr_t patch_loc = 0;
for (int i = 0; i < 16; i++, roothash_failed_ref -= 4) {
if (cbz_ref64_back(kernel_buf, roothash_failed_ref, roothash_failed_ref)) {
printf("%s: Found cbz target at %p\n", __FUNCTION__, (void*) roothash_failed_ref);
patch_loc = roothash_failed_ref;
break;
}
}
if (!patch_loc) {
printf("%s: Could not find cbz target\n",__FUNCTION__);
return -1;
}
printf("%s: Patching root hash check at %p\n",__FUNCTION__, (void*) patch_loc);
*((uint32_t *) (kernel_buf + patch_loc)) = arm64_branch_instruction((uintptr_t) patch_loc, (uintptr_t) branch_target);
return 0;
}
int get_root_volume_seal_is_broken_patch(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n", __FUNCTION__);
char roothash_authenticated_string[sizeof("\"root volume seal is broken %p\\n\"")] = "\"root volume seal is broken %p\\n\"";
unsigned char *roothash_authenticated_loc = memmem(kernel_buf, kernel_len, roothash_authenticated_string, sizeof("\"root volume seal is broken %p\\n\"") - 1);
if(!roothash_authenticated_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, roothash_authenticated_string);
return -1;
}
for (; *roothash_authenticated_loc != 0; roothash_authenticated_loc--);
roothash_authenticated_loc++;
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, roothash_authenticated_string, GET_OFFSET(kernel_len, roothash_authenticated_loc));
addr_t roothash_authenticated_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, roothash_authenticated_loc));
if(!roothash_authenticated_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, roothash_authenticated_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, roothash_authenticated_string, (void*) roothash_authenticated_ref);
addr_t tbnz_ref = step64_back(kernel_buf, roothash_authenticated_ref, 20 * 4, 0x36000000, 0x7E000000);
if(!tbnz_ref) {
printf("%s: Could not find tbnz\n",__FUNCTION__);
return -1;
}
printf("%s: Found tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref);
printf("%s: Patching tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref);
*((uint32_t *) (kernel_buf + tbnz_ref)) = 0xd503201f;
printf("%s: Patched tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref);
return 0;
}
int get_update_rootfs_rw_patch(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n", __FUNCTION__);
char update_rootfs_rw_string[sizeof("%s:%d: %s Updating mount to read/write mode is not allowed")] = "%s:%d: %s Updating mount to read/write mode is not allowed";
unsigned char *update_rootfs_rw_loc = memmem(kernel_buf, kernel_len, update_rootfs_rw_string, sizeof("%s:%d: %s Updating mount to read/write mode is not allowed") - 1);
if(!update_rootfs_rw_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, update_rootfs_rw_string);
return -1;
}
for (; *update_rootfs_rw_loc != 0; update_rootfs_rw_loc--);
update_rootfs_rw_loc++;
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, update_rootfs_rw_string, GET_OFFSET(kernel_len, update_rootfs_rw_loc));
addr_t update_rootfs_rw_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, update_rootfs_rw_loc));
if(!update_rootfs_rw_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, update_rootfs_rw_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, update_rootfs_rw_string, (void*) update_rootfs_rw_ref);
addr_t tbnz_ref = step64_back(kernel_buf, update_rootfs_rw_ref, 200 * 4, 0x36000000, 0x7E000000);
if(!tbnz_ref) {
printf("%s: Could not find tbnz\n",__FUNCTION__);
return -1;
}
printf("%s: Found tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref);
addr_t tbnz_ref2 = step64_back(kernel_buf, tbnz_ref - 4, 200 * 4, 0x36000000, 0x7E000000);
if(!tbnz_ref2) {
printf("%s: Could not find tbnz\n",__FUNCTION__);
return -1;
}
printf("%s: Found tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref2);
printf("%s: Patching tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref2);
*((uint32_t *) (kernel_buf + tbnz_ref2)) = 0xd503201f;
printf("%s: Patched tbnz at %p\n",__FUNCTION__, (void*) tbnz_ref2);
return 0;
}
int get_amfi_out_of_my_way_patch(void* kernel_buf,size_t kernel_len) {
printf("%s: Entering ...\n",__FUNCTION__);
void* xnu = memmem(kernel_buf,kernel_len,"root:xnu-",9);
int kernel_vers = atoi(xnu+9);
printf("%s: Kernel-%d inputted\n",__FUNCTION__, kernel_vers);
char amfiString[33] = "entitlements too small";
int stringLen = 22;
if (kernel_vers >= 7938) { // Using "entitlements too small" fails on iOS 15 Kernels
strncpy(amfiString, "Internal Error: No cdhash found.", 33);
stringLen = 32;
}
void* ent_loc = memmem(kernel_buf,kernel_len,amfiString,stringLen);
if(!ent_loc) {
printf("%s: Could not find %s string\n",__FUNCTION__, amfiString);
return -1;
}
printf("%s: Found %s str loc at %p\n",__FUNCTION__,amfiString,GET_OFFSET(kernel_len,ent_loc));
addr_t ent_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, ent_loc));
if(!ent_ref) {
printf("%s: Could not find %s xref\n",__FUNCTION__,amfiString);
return -1;
}
printf("%s: Found %s str ref at %p\n",__FUNCTION__,amfiString,(void*)ent_ref);
addr_t next_bl = step64(kernel_buf, ent_ref, 100, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
next_bl = step64(kernel_buf, next_bl+0x4, 200, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
if(kernel_vers>3789) {
next_bl = step64(kernel_buf, next_bl+0x4, 200, INSN_CALL);
if(!next_bl) {
printf("%s: Could not find next bl\n",__FUNCTION__);
return -1;
}
}
addr_t function = follow_call64(kernel_buf, next_bl);
if(!function) {
printf("%s: Could not find function bl\n",__FUNCTION__);
return -1;
}
printf("%s: Patching AMFI at %p\n",__FUNCTION__,(void*)function);
*(uint32_t *)(kernel_buf + function) = 0x320003E0;
*(uint32_t *)(kernel_buf + function + 0x4) = 0xD65F03C0;
return 0;
}
int is_root_hash_authentication_required_ios_patch(void* kernel_buf,size_t kernel_len) {
char authentication_required_string[sizeof("is_root_hash_authentication_required_ios")] = "is_root_hash_authentication_required_ios";
unsigned char *authentication_required_loc = memmem(kernel_buf, kernel_len, authentication_required_string, sizeof("is_root_hash_authentication_required_ios") - 1);
if(!authentication_required_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, authentication_required_string);
return -1;
}
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, authentication_required_string, GET_OFFSET(kernel_len, authentication_required_loc));
addr_t authentication_required_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, authentication_required_loc));
if(!authentication_required_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, authentication_required_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, authentication_required_string, (void*) authentication_required_ref);
addr_t function = bof64(kernel_buf, 0, authentication_required_ref);
printf("%s: Patching is_root_hash_authentication_required_ios at %p\n",__FUNCTION__,(void*)function);
*(uint32_t *)(kernel_buf + function) = 0xD2800000;
*(uint32_t *)(kernel_buf + function + 0x4) = 0xD65F03C0;
return 0;
}
int launchd_path_patch(void* kernel_buf,size_t kernel_len) {
char launchd_path_string[sizeof("/sbin/launchd")] = "/sbin/launchd";
unsigned char *launchd_path_loc = memmem(kernel_buf, kernel_len, launchd_path_string, sizeof("/sbin/launchd") - 1);
if(!launchd_path_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, launchd_path_string);
return -1;
}
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, launchd_path_string, GET_OFFSET(kernel_len, launchd_path_loc));
addr_t addr = (addr_t)GET_OFFSET(kernel_len, launchd_path_loc);
printf("%s: Patching launchd at %p\n",__FUNCTION__,(void*)addr);
*(uint32_t *)(launchd_path_loc) = 0x69626A2F;
*(uint32_t *)(launchd_path_loc + 0x4) = 0x616C2F6E;
*(uint32_t *)(launchd_path_loc + 0x8) = 0x68636E75;
*(uint32_t *)(launchd_path_loc + 0x12) = 0x64;
return 0;
}
int tfp0_patch(void* kernel_buf,size_t kernel_len) {
char pineapple_pizza_string[sizeof("Just like pineapple on pizza, this task/thread port doesn't belong here.")] = "Just like pineapple on pizza, this task/thread port doesn't belong here.";
unsigned char *pineapple_pizza_loc = memmem(kernel_buf, kernel_len, pineapple_pizza_string, sizeof("Just like pineapple on pizza, this task/thread port doesn't belong here.") - 1);
if(!pineapple_pizza_loc) {
printf("%s: Could not find \"%s\" string\n", __FUNCTION__, pineapple_pizza_string);
return -1;
}
printf("%s: Found \"%s\" str loc at %p\n", __FUNCTION__, pineapple_pizza_string, GET_OFFSET(kernel_len, pineapple_pizza_loc));
addr_t pineapple_pizza_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, pineapple_pizza_loc));
if(!pineapple_pizza_ref) {
printf("%s: Could not find \"%s\" xref\n",__FUNCTION__, pineapple_pizza_string);
return -1;
}
printf("%s: Found \"%s\" xref at %p\n",__FUNCTION__, pineapple_pizza_string, (void*) pineapple_pizza_ref);
addr_t beq = step64(kernel_buf, pineapple_pizza_ref, 100, INSN_BEQ);
if(!beq) {
printf("%s: Could not find b.eq\n",__FUNCTION__);
return -1;
}
printf("%s: Found b.eq at %p\n",__FUNCTION__, (void*) beq);
addr_t caller_equals_victim = beq - 0x4;
printf("%s: Patching tfp0 at %p\n",__FUNCTION__,(void*)caller_equals_victim);
*(uint32_t *)(kernel_buf + caller_equals_victim) = 0xEB1F03FF;
return 0;
}
int force_developer_mode(void* kernel_buf,size_t kernel_len) {
char devmode_status_string[sizeof("AMFI: trying to get developer mode status from ACM\n")] = "AMFI: trying to get developer mode status from ACM\n";
unsigned char *devmode_status_loc = memmem(kernel_buf, kernel_len, devmode_status_string, sizeof("AMFI: trying to get developer mode status from ACM\n") - 1);
if(!devmode_status_loc) {
printf("%s: Could not find \"AMFI: trying to get developer mode status from ACM\"\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"AMFI: trying to get developer mode status from ACM\" str loc at %p\n",__FUNCTION__, (void*) GET_OFFSET(kernel_len, devmode_status_loc));
addr_t devmode_status_ref = xref64(kernel_buf,0,kernel_len,(addr_t)GET_OFFSET(kernel_len, devmode_status_loc));
if(!devmode_status_ref) {
printf("%s: Could not find \"AMFI: trying to get developer mode status from ACM\" xref\n",__FUNCTION__);
return -1;
}
printf("%s: Found \"AMFI: trying to get developer mode status from ACM\" xref at %p\n",__FUNCTION__, (void*) devmode_status_ref);
addr_t cbz = step64_back(kernel_buf, devmode_status_ref, 100, INSN_CBZ);
if(!cbz) {
printf("%s: Could not find cbz\n",__FUNCTION__);
return -1;
}
printf("%s: Patching developer mode at %p\n",__FUNCTION__,(void*)cbz);
*(uint32_t *)(kernel_buf + cbz) = 0x14000000 + ((*(uint32_t *)(kernel_buf + cbz) & 0x03ffffff) >> 5); // force branching
return 0;
}
int main(int argc, char **argv) {
printf("%s: Starting...\n", __FUNCTION__);
FILE* fp = NULL;
if(argc < 4){
printf("Version: " VERSION "\n");
printf("Usage: %s <kernel_in> <kernel_out> <args>\n",argv[0]);
printf("\t-a\t\tPatch AMFI\n");
printf("\t-f\t\tPatch AppleFirmwareUpdate img4 signature check\n");
printf("\t-s\t\tPatch SPUFirmwareValidation (iOS 15 Only)\n");
printf("\t-r\t\tPatch RootVPNotAuthenticatedAfterMounting (iOS 15 Only)\n");
printf("\t-o\t\tPatch could_not_authenticate_personalized_root_hash (iOS 15 Only)\n");
printf("\t-e\t\tPatch root volume seal is broken (iOS 15 Only)\n");
printf("\t-u\t\tPatch update_rootfs_rw (iOS 15 Only)\n");
printf("\t-p\t\tPatch AMFIInitializeLocalSigningPublicKey (iOS 15 Only)\n");
printf("\t-h\t\tPatch is_root_hash_authentication_required_ios (iOS 16 only)\n");
printf("\t-l\t\tPatch launchd path\n");
printf("\t-t\t\tPatch tfp0\n");
printf("\t-d\t\tPatch developer mode\n");
return 0;
}
void* kernel_buf;
size_t kernel_len;
fp = fopen(argv[1], "rb");
if(!fp) {
printf("%s: Error opening %s!\n", __FUNCTION__, argv[1]);
return -1;
}
fseek(fp, 0, SEEK_END);
kernel_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
kernel_buf = (void*)malloc(kernel_len);
if(!kernel_buf) {
printf("%s: Out of memory!\n", __FUNCTION__);
fclose(fp);
return -1;
}
fread(kernel_buf, 1, kernel_len, fp);
fclose(fp);
if(memmem(kernel_buf,kernel_len,"KernelCacheBuilder",18)) {
printf("%s: Detected IMG4/IM4P, you have to unpack and decompress it!\n",__FUNCTION__);
return -1;
}
if (*(uint32_t*)kernel_buf == 0xbebafeca) {
printf("%s: Detected fat macho kernel\n",__FUNCTION__);
memmove(kernel_buf,kernel_buf+28,kernel_len);
}
for(int i=0;i<argc;i++) {
if(strcmp(argv[i], "-a") == 0) {
printf("Kernel: Adding AMFI_get_out_of_my_way patch...\n");
get_amfi_out_of_my_way_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-f") == 0) {
printf("Kernel: Adding AppleFirmwareUpdate img4 signature check patch...\n");
get_AppleFirmwareUpdate_img4_signature_check(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-s") == 0) {
printf("Kernel: Adding SPUFirmwareValidation patch...\n");
get_SPUFirmwareValidation_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-p") == 0) {
printf("Kernel: Adding AMFIInitializeLocalSigningPublicKey patch...\n");
get_AMFIInitializeLocalSigningPublicKey_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-r") == 0) {
printf("Kernel: Adding RootVPNotAuthenticatedAfterMounting patch...\n");
get_RootVPNotAuthenticatedAfterMounting_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-o") == 0) {
printf("Kernel: Adding could_not_authenticate_personalized_root_hash patch...\n");
get_could_not_authenticate_personalized_root_hash_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-e") == 0) {
printf("Kernel: Adding root volume seal is broken patch...\n");
get_root_volume_seal_is_broken_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-u") == 0) {
printf("Kernel: Adding update_rootfs_rw patch...\n");
get_update_rootfs_rw_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-h") == 0) {
printf("Kernel: Adding is_root_hash_authentication_required_ios patch...\n");
is_root_hash_authentication_required_ios_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-l") == 0) {
printf("Kernel: Adding launchd patch...\n");
launchd_path_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-t") == 0) {
printf("Kernel: Adding tfp0 patch...\n");
tfp0_patch(kernel_buf,kernel_len);
}
if(strcmp(argv[i], "-d") == 0) {
printf("Kernel: Adding force developer mode patch...\n");
force_developer_mode(kernel_buf,kernel_len);
}
}
/* Write patched kernel */
printf("%s: Writing out patched file to %s...\n", __FUNCTION__, argv[2]);
fp = fopen(argv[2], "wb+");
if(!fp) {
printf("%s: Unable to open %s!\n", __FUNCTION__, argv[2]);
free(kernel_buf);
return -1;
}
fwrite(kernel_buf, 1, kernel_len, fp);
fflush(fp);
fclose(fp);
free(kernel_buf);
printf("%s: Quitting...\n", __FUNCTION__);
return 0;
}