Chinaunix首页 | 论坛 | 博客
  • 博客访问: 621039
  • 博文数量: 105
  • 博客积分: 5389
  • 博客等级: 准将
  • 技术积分: 1290
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-29 11:13
文章分类

全部博文(105)

文章存档

2010年(23)

2009年(20)

2008年(62)

分类: C/C++

2008-05-13 11:39:02

源代码见后面,现把GDB调试以及攻击过程图示如下:
用这些图展示,应该很方便地展示了攻击的过程,
应该指出的是,这个是攻击方式的一种,并没有嵌入代码。






 

 

 

/* 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 45 f8 01 00 00 00    movl   $0x1,0xfffffff8(%ebp)

  40105d:      c7 45 f4 00 00 00 00    movl   $0x0,0xfffffff4(%ebp)

  401064:      8b 45 08                    mov    0x8(%ebp),%eax

  401067:      89 45 f0                    mov    %eax,0xfffffff0(%ebp)

  40106a:       e8 51 05 00 00         call   4015c0 <___getreent>

  40106f:       8b 40 04                    mov    0x4(%eax),%eax

  401072:      89 04 24                    mov    %eax,(%esp)

  401075:      e8 56 05 00 00         call   4015d0 <_getc>

  40107a:       89 45 fc                    mov    %eax,0xfffffffc(%ebp)

  40107d:      83 7d fc ff            cmpl   $0xffffffff,0xfffffffc(%ebp)

  401081:      0f 84 8f 00 00 00       je     401116 <_getxs+0xc6>

  401087:      83 7d fc 0a          cmpl   $0xa,0xfffffffc(%ebp)

  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 9c 40 40 00      add    0x40409c,%eax

  40109b:      40                        inc    %eax

  40109c:      0f be 00             movsbl (%eax),%eax

  40109f:       83 e0 44                    and    $0x44,%eax

  4010a2:       85 c0                      test   %eax,%eax

  4010a4:       74 c4                      je     40106a <_getxs+0x1a>

  4010a6:       83 7d fc 2f            cmpl   $0x2f,0xfffffffc(%ebp)

  4010aa:       7e 11                       jle    4010bd <_getxs+0x6d>

  4010ac:       83 7d fc 39          cmpl   $0x39,0xfffffffc(%ebp)

  4010b0:      7f 0b                       jg     4010bd <_getxs+0x6d>

  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)

  4010c1:      7e 11                       jle    4010d4 <_getxs+0x84>

  4010c3:      83 7d fc 46          cmpl   $0x46,0xfffffffc(%ebp)

  4010c7:      7f 0b                       jg     4010d4 <_getxs+0x84>

  4010c9:      8b 45 fc                    mov    0xfffffffc(%ebp),%eax

  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     4010f5 <_getxs+0xa5>

  4010e3:       8b 45 ec                    mov    0xffffffec(%ebp),%eax

  4010e6:       89 45 f4                    mov    %eax,0xfffffff4(%ebp)

  4010e9:       c7 45 f8 00 00 00 00    movl   $0x0,0xfffffff8(%ebp)

  4010f0:       e9 75 ff ff ff            jmp    40106a <_getxs+0x1a>

  4010f5:       8b 45 f0                    mov    0xfffffff0(%ebp),%eax

  4010f8:       89 c2                      mov    %eax,%edx

  4010fa:       8b 45 f4                    mov    0xfffffff4(%ebp),%eax

  4010fd:       c1 e0 04                    shl    $0x4,%eax

  401100:       02 45 ec                    add    0xffffffec(%ebp),%al

  401103:       88 02                      mov    %al,(%edx)

  401105:       8d 45 f0                    lea    0xfffffff0(%ebp),%eax

  401108:       ff 00                       incl   (%eax)

  40110a:       c7 45 f8 01 00 00 00    movl   $0x1,0xfffffff8(%ebp)

  401111:       e9 54 ff ff ff            jmp    40106a <_getxs+0x1a>

  401116:       8b 45 f0                    mov    0xfffffff0(%ebp),%eax

  401119:       c6 00 00                    movb   $0x0,(%eax)

  40111c:       8d 45 f0                    lea    0xfffffff0(%ebp),%eax

  40111f:       ff 00                       incl   (%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

  40112c:       8d 45 e8                    lea    0xffffffe8(%ebp),%eax

  40112f:       89 04 24                    mov    %eax,(%esp)

  401132:       e8 19 ff ff ff            call   401050 <_getxs>

  401137:       b8 01 00 00 00       mov    $0x1,%eax

  40113c:       c9                        leave 

  40113d:       c3                        ret   

  40113e:       54                        push   %esp

  40113f:       79 70                      jns    4011b1 <_main+0x1d>

  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)

  40114c:       67 3a 00                    addr16 cmp (%bx,%si),%al

  40114f:       67                        addr16

  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>

  40115a:       72 6e                      jb     4011ca <_main+0x36>

  40115c:       65 64 20 30          and    %dh,%fs:%gs:(%eax)

  401160:       78 25                      js     401187 <_test+0x22>

  401162:       78 0a                      js     40116e <_test+0x9>

       ...

 

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>

  40117c:       89 45 fc                    mov    %eax,0xfffffffc(%ebp)

  40117f:       8b 45 fc                    mov    0xfffffffc(%ebp),%eax

  401182:       89 44 24 04          mov    %eax,0x4(%esp)

  401186:       c7 04 24 4f 11 40 00     movl   $0x40114f,(%esp)

  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

  40119a:       83 e4 f0             and    $0xfffffff0,%esp

  40119d:       b8 00 00 00 00       mov    $0x0,%eax

  4011a2:       89 45 ac                    mov    %eax,0xffffffac(%ebp)

  4011a5:       8b 45 ac                    mov    0xffffffac(%ebp),%eax

  4011a8:       e8 63 03 00 00         call   401510 <___chkstk>

  4011ad:       e8 ee 03 00 00         call   4015a0 <___main>

  4011b2:       8d 45 b8                    lea    0xffffffb8(%ebp),%eax

  4011b5:       25 ff 0f 00 00          and    $0xfff,%eax

  4011ba:       89 45 b4                    mov    %eax,0xffffffb4(%ebp)

  4011bd:       8b 45 b4                    mov    0xffffffb4(%ebp),%eax

  4011c0:       83 c0 0f                    add    $0xf,%eax

  4011c3:       c1 e8 04                    shr    $0x4,%eax

  4011c6:       c1 e0 04                    shl    $0x4,%eax

  4011c9:       89 45 a8                    mov    %eax,0xffffffa8(%ebp)

  4011cc:       8b 45 a8                    mov    0xffffffa8(%ebp),%eax

  4011cf:       e8 3c 03 00 00         call   401510 <___chkstk>

  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   

 

参考链接:
对论贴中钻研本题的兄弟表示感谢!~
阅读(2402) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~