您的位置:首页 >> 编程开发 >> C/C++ >> 正文
C/C++ RSS
 

【好玩】缓冲区溢出攻击实验

http://www.rdxx.com 05年09月13日 19:30 ChinaUnix.net 我要投稿

 

这是出自《深入理解计算机系统》的一道题目:

[code:1:d0aaabbf15]
/* Bomb program that is solved using a buffer overflow attack */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* 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);
    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;
}
[/code:1:d0aaabbf15]

题目的要求是,在getbuf函数中也许“显然”地会返回1,通过输入一个数据使这个函数返回0xdeadbeef,就是在test函数中地printf中打印地是0xdeadbeef.

我大概有了思路,做着玩玩,晚上回来再说。

共2页  第1页 第2页


 
 
打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站