CSAPP LAB---buflab-handout(缓冲区溢出实验)

LAB5

基础准备:

题目解析:

首先阅读那长达11页的英文文档,获悉本实验为模拟缓冲区溢出。

使用tar xvf命令解压文件后,会有3个可执行的二进制文件bufbomb,hex2raw,makecookie。

bufbomb运行时会进入getbuf函数,其中通过调用Gets函数读取字符串。要求在已知缓冲区大小的情况下对输入的字符串进行定制完成特定溢出操作。getbuf函数如下所示:

/* Buffer size forgetbuf */
#defineNORMAL_BUFFER_SIZE 32
 
int getbuf()
{
char buf[NORMAL_BUFFER_SIZE];
Gets(buf);
return 1;
}


       在这个文件中,gets函数用来获取数据到缓冲区,而gets函数不会对输入的数据进行边界检查。所以当我们的输入超过了缓冲区的大小(这里是32)时,超过缓冲区的数据就会覆盖内存中用作其它用途的数据,从而改变程序的行为。至于为什么会有hex2raw文件,是因为如果gets从终端读取时,无法输入一些不可打印的数据,比如想输入控制字符0x09,于是就有了hex2raw这个程序,这个程序将16进制表示的字节转换成二进制字节数据。

至于makecookie就很明显了,就是防止同学作弊。让每个同学都有一个唯一的提交作业userid,从而计算出不通的cookie值。这里我使用./makecookie 201308060111得出cookie值0x5ffdc4e4

1.     Level0: Candle (10 pts)

英文PDF说明中写到:

Your task is to get BUFBOMB to execute the code forsmoke when getbuf executes its return statement,rather than returning to test.Note that your exploit string may also corrupt parts of the stack notdirectlyrelated to this stage, but this will not cause a problem, since smokecauses the program to exit directly.

题意解析:这里就是要让test运行完后,不直接返回退出。而是跳到smoke函数处,继续运行,而当smoke运行完毕后,才退出。

Test源码:

void test() { 
  int val; 
  /* Put canary on stack to detect possiblecorruption */ 
  volatile int local = uniqueval(); 
 
  val = getbuf(); 
 
  /* Check for corruption stack */ 
  if (local != uniqueval()) { 
    printf("Sabotaged!: the stack has beencorrupted\n"); 
  } 
  else if (val == cookie) { 
    printf("Boom!: getbuf returned0x%x\n", val); 
    validate(3); 
  } else { 
    printf("Dud: getbuf returned0x%x\n", val); 
  } 
} 
Smoke源码:
void smoke(){ 
  puts("Smoke!: You calledsmoke()"); 
  validate(0); 
  exit(0); 
} 


解答:

所以,首先反汇编getbuf函数:

 

   0x08049268 <+6>:      lea   -0x28(%ebp),%eax

   0x0804926b <+9>:      mov   %eax,(%esp)

   0x0804926e <+12>:   call   0x8048c32<Gets>

从上面三句,可以看到lea把buf的指针地址(-0x28(%ebp))传给了Gets(),也就是buf距离return address有0x28 + 4(%ebp的字节数)=0x2c(44)个字节的距离(依据栈结构)。于是只要在buf开始处随便填入44字节(由于Gets是通过换行符\n(ASCII值0x0a)界定输入终止的,所以前44字节只要不是0a就可以),并在return address中填入smoke的地址就行了。

可以通过下面的图进行理解:

接下来找到smoke地址:


为0x08048e0a

所以构造0.txt文件:

30 30 30 30 30 3030 30 30 30 30 30 30 30 30 30 30 30 30 30

31 31 31 31 31 3131 31 31 31 31 31 31 31 31 31 31 31 31 31

32 32 32 32

00 08 04 8e 0a

发现没有通过。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值