home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / basic / heap2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-04  |  1.0 KB  |  45 lines

  1. /* demonstrates static overflow in bss (uninitialized data) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. #define ERROR -1
  10. #define BUFSIZE 16
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.    u_long diff;
  15.  
  16.    int oversize;
  17.    static char buf1[BUFSIZE], buf2[BUFSIZE];
  18.  
  19.    if (argc <= 1)
  20.    {
  21.       fprintf(stderr, "Usage: %s <numbytes>\n", argv[0]);
  22.       fprintf(stderr, "[Will overflow static buffer by <numbytes>]\n");
  23.  
  24.       exit(ERROR);
  25.    }
  26.  
  27.    diff = (u_long)buf2 - (u_long)buf1;
  28.  
  29.    printf("buf1 = %p, buf2 = %p, diff = 0x%x (%d) bytes\n\n", 
  30.           buf1, buf2, diff, diff);
  31.  
  32.    memset(buf2, 'A', BUFSIZE - 1), memset(buf1, 'B', BUFSIZE - 1);
  33.    buf1[BUFSIZE - 1] = '\0', buf2[BUFSIZE - 1] = '\0';
  34.  
  35.    printf("before overflow: buf1 = %s, buf2 = %s\n", buf1, buf2);
  36.  
  37.    oversize = diff + atoi(argv[1]);
  38.    memset(buf1, 'B', oversize);
  39.  
  40.    buf1[BUFSIZE - 1] = '\0', buf2[BUFSIZE - 1] = '\0';
  41.    printf("after overflow: buf1 = %s, buf2 = %s\n\n", buf1, buf2);
  42.  
  43.    return 0;
  44. }
  45.