-
Notifications
You must be signed in to change notification settings - Fork 31
/
Kernel64Patcher.c
303 lines (271 loc) · 11.6 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
/*
* Copyright 2020, @Ralph0045
* gcc Kernel64Patcher.c -o Kernel64Patcher
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "patchfinder64.c"
#define GET_OFFSET(kernel_len, x) (x - (uintptr_t) kernel_buf)
// 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;
}
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 main(int argc, char **argv) {
printf("%s: Starting...\n", __FUNCTION__);
FILE* fp = NULL;
if(argc < 4){
printf("Usage: %s <kernel_in> <kernel_out> <args>\n",argv[0]);
printf("\t-a\t\tPatch AMFI\n");
printf("\t-s\t\tPatch SPUFirmwareValidation (iOS 15 Only)\n");
printf("\t-r\t\tPatch RootVPNotAuthenticatedAfterMounting (iOS 15 Only)\n");
printf("\t-p\t\tPatch AMFIInitializeLocalSigningPublicKey (iOS 15 Only)\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], "-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);
}
}
/* 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;
}