分类: C/C++
2008-05-13 11:39:02
/* Bomb program that is solved using a buffer overflow attack */
#include
#include
#include
/* Like gets, except that characters are typed as pairs of hex digits.
Nondigit characters are ignored. Stops when encounters newline */
char *getxs(char *dest)
{
int c;
int even = 1; /* Have read even number of digits */
int otherd = 0; /* Other hex digit of pair */
char *sp = dest;
while ((c = getchar()) != EOF && c != '\n') {
if (isxdigit(c)) {
int val;
if ('0' <= c && c <= '9')
val = c - '0';
else if ('A' <= c && c <= 'F')
val = c - 'A' + 10;
else
val = c - 'a' + 10;
if (even) {
otherd = val;
even = 0;
} else {
*sp++ = otherd * 16 + val;
even = 1;
}
}
}
*sp++ = '\0';
return dest;
}
/* $begin getbuf-c */
int getbuf()
{
char buf[12];
getxs(buf);
// printf("getbuf returned %s\n", buf);
return 1;
}
void test()
{
int val;
printf("Type Hex string:");
val = getbuf();
printf("getbuf returned 0x%x\n", val);
}
/* $end getbuf-c */
int main()
{
int buf[16];
/* This little hack is an attempt to get the stack to be in a
stable position
*/
int offset = (((int) buf) & 0xFFF);
int *space = (int *) alloca(offset);
*space = 0; /* So that don't get complaint of unused variable */
test();
return 0;
}
bufbomb.exe: file format pei-i386
Disassembly of section .text:
...00401050 <_getxs>:
401050: 55 push %ebp
401051: 89
e5 mov %esp,%ebp
401053: 83
ec 18 sub $0x18,%esp
401056: c7
40105d: c7
401064: 8b 45 08 mov 0x8(%ebp),%eax
401067: 89
401072: 89 04 24 mov %eax,(%esp)
401075: e8 56 05 00 00 call 4015d0 <_getc>
40107d: 83 7d fc ff cmpl $0xffffffff,0xfffffffc(%ebp)
401081:
401087: 83
7d fc
40108b: 75 05 jne 401092
<_getxs+0x42>
40108d: e9
84 00 00 00 jmp 401116
<_getxs+0xc6>
401092: 8b
45 fc mov
0xfffffffc(%ebp),%eax
401095: 03 05
40109b: 40 inc %eax
4010aa: 7e 11 jle 4010bd
<_getxs+0x6d>
4010b0:
4010b2: 8b 45 fc mov 0xfffffffc(%ebp),%eax
4010b5: 83 e8 30 sub $0x30,%eax
4010b8: 89 45 ec mov %eax,0xffffffec(%ebp)
4010bb: eb 20 jmp 4010dd <_getxs+0x8d>
4010bd: 83 7d fc 40 cmpl $0x40,0xfffffffc(%ebp)
4010cc: 83 e8 37 sub $0x37,%eax
4010cf: 89 45 ec mov %eax,0xffffffec(%ebp)
4010d2: eb 09 jmp 4010dd <_getxs+0x8d>
4010d4: 8b 45 fc mov 0xfffffffc(%ebp),%eax
4010d7: 83 e8 57 sub $0x57,%eax
4010da: 89 45 ec mov %eax,0xffffffec(%ebp)
4010dd: 83 7d f8 00 cmpl $0x0,0xfffffff8(%ebp)
4010e1: 74 12 je
4010e3: 8b 45 ec mov 0xffffffec(%ebp),%eax
4010e6: 89
4010e9: c7
4010fa: 8b
4010fd: c1 e0 04 shl $0x4,%eax
401100: 02 45 ec add 0xffffffec(%ebp),%al
401103: 88 02 mov %al,(%edx)
401105: 8d
401108: ff 00 incl (%eax)
401111: e9 54 ff ff ff jmp
401116: 8b
401119: c6 00 00 movb $0x0,(%eax)
401121: 8b 45 08 mov 0x8(%ebp),%eax
401124: c9 leave
401125: c3 ret
00401126 <_getbuf>:
401126: 55 push
%ebp
401127: 89
e5 mov %esp,%ebp
401129: 83
ec 28 sub $0x28,%esp
401132: e8 19 ff ff ff call 401050 <_getxs>
401137: b8 01 00 00 00 mov $0x1,%eax
40113d: c3 ret
40113e: 54 push %esp
401141: 65 20 48 65 and %cl,%gs:0x65(%eax)
401145: 78 20 js 401167 <_test+0x2>
401147: 73 74 jae 4011bd <_main+0x29>
401149: 72 69 jb 4011b4 <_main+0x20>
40114b: 6e outsb %ds:(%esi),(%dx)
401150: 65 gs
401151: 74 62 je 4011b5 <_main+0x21>
401153: 75 66 jne 4011bb <_main+0x27>
401155: 20 72 65 and %dh,0x65(%edx)
401158: 74
75 je 4011cf
<_main+0x3b>
401160: 78 25 js 401187 <_test+0x22>
401162: 78
...
00401165 <_test>:
401165: 55 push %ebp
401166: 89 e5 mov %esp,%ebp
401168: 83 ec 18 sub
$0x18,%esp
40116b: c7
04 24 3e 11 40 00 movl $0x40113e,(%esp)
401172: e8 39 04 00 00 call 4015b0 <_printf>
401177: e8 aa ff ff ff call 401126 <_getbuf>
401182: 89 44 24 04 mov %eax,0x4(%esp)
401186: c7 04 24
40118d: e8 1e 04 00 00 call 4015b0 <_printf>
401192: c9 leave
401193: c3 ret
00401194 <_main>:
401194: 55 push %ebp
401195: 89 e5 mov %esp,%ebp
401197: 83 ec 58 sub $0x58,%esp
40119d: b8 00 00 00 00 mov $0x0,%eax
4011ad: e8 ee 03 00 00 call
4011b2: 8d 45 b8 lea 0xffffffb8(%ebp),%eax
4011b5: 25 ff
4011ba: 89 45 b4 mov %eax,0xffffffb4(%ebp)
4011bd: 8b 45 b4 mov 0xffffffb4(%ebp),%eax
4011cc: 8b
4011cf: e8
4011d4: 89 e0 mov %esp,%eax
4011d6: 89 45 b0 mov %eax,0xffffffb0(%ebp)
4011d9: 8b 45 b0 mov 0xffffffb0(%ebp),%eax
4011dc: c7 00 00 00 00 00 movl $0x0,(%eax)
4011e2: e8 7e ff ff ff call 401165 <_test>
4011e7: b8 00 00 00 00 mov $0x0,%eax
4011ec: c9 leave
4011ed: c3 ret
4011ee: 90 nop
4011ef: 90 nop