对Wiki上的几个例子复现
一、stack_example
点击下载文件stack_example.c
![14899865-b61f85d8677ea529.png](https://img-blog.csdnimg.cn/img_convert/fbf6cc40661e2dbf71a048f211eac979.png)
无success入口的情况下,尝试进入success()
对c文件进行编译
![14899865-ca6eea9ed3277347.png](https://img-blog.csdnimg.cn/img_convert/16606aace4d7e7eee163ea2655e9cebe.png)
提示gets函数为危险函数,可以导致栈溢出
用checksec工具查编译出文件的保护程度
![14899865-fca20db0d955194f.png](https://img-blog.csdnimg.cn/img_convert/13f5dae6ab728bc75c5cc638e52c88af.png)
在这之前已把ASLR和PIE关闭
经过IDA的反编译查看刚刚有调用危险函数gets的函数vulnerable的伪代码
![14899865-14d8da403e812b16.png](https://img-blog.csdnimg.cn/img_convert/b375f072ed8ff2c90dcac7c5d1bebcfd.png)
该字符对ebp距离为14的十六进制0x14
对栈结构进行分析。
![14899865-17e6bf42396c4feb.png](https://img-blog.csdnimg.cn/img_convert/8408fa9b69b8f27e0aa279d33bdbf24e.png)
接下来只要想办法将success函数的地址覆盖掉retaddr
只要将读取字符串栈溢出越界,saved ebp中为4个字符量大小
想要到达retaddr就是读取 0x14+4的字符量
在IDA查询success函数的起始地址是0x08048456 如图
![14899865-2dc784696d3ece07.png](https://img-blog.csdnimg.cn/img_convert/87940ccaf4686f10750112de19d358f6.png)
因为在我们的调试环境中,“内存数据”中的 DWORD 和我们逻辑上使用的
“数值数据”是按字节序逆序过的。
所以地址应该要按两个一组化成十六进制逆序排放
\x56\x84\x04\x08
这些字符可能无法用键盘完全输入
需要利用pwntools工具与程序交互
![14899865-649c16f1b4a6dbb5.png](https://img-blog.csdnimg.cn/img_convert/0d72588dbcd6870a047ba940b434dc94.png)
并在虚拟机里面执行一波
![14899865-179f0df4b9cdc220.png](https://img-blog.csdnimg.cn/img_convert/e5e2947a1003e683582f65e018df5571.png)
成功的将success函数里面的puts运行显示
二、ret2text
点击下载文件ret2text
获得文件ret2text,先检查保护
![14899865-f8219e8fc122b5cd.png](https://img-blog.csdnimg.cn/img_convert/96297d708757716c00a11bd9c5d40023.png)
无canary无PIE
IDA伪代码
![14899865-82d9dc0a101dd0be.png](https://img-blog.csdnimg.cn/img_convert/3de24aaa595ce4d52d207f355ecaf6cb.png)
发现危险函数gets
检查其他函数发现
![14899865-1d4eef89d1d89d2c.png](https://img-blog.csdnimg.cn/img_convert/6df986b834daf82168f11c07bb95b73d.png)
![14899865-0aa70380a127fec4.png](https://img-blog.csdnimg.cn/img_convert/ab4b1ac1bb7dc17a16b8cbeba0db626d.png)
又发现secure中有调用system("/bin//sh")
![14899865-7e30bb36f1901db4.png](https://img-blog.csdnimg.cn/img_convert/40af8a311c544ea66eec3f867e825e4a.png)
断点,查看esp,ebp
由之前学习知道,存放ebp上方为返回地址,只需要将返回地址覆盖为调用system的地址即能进入我们/bin/sh
esp 0xffffcfa0 esp中s相对esp为1CH,s地址为0xffffcfbc
ebp 如显示为0xffffd028
计算s相对ebp偏移d028-cfbc=6c
对ebp偏移为6c,所以对返回地址偏移6c+4
payload
![14899865-baf6deaa26939c41.png](https://img-blog.csdnimg.cn/img_convert/f0400351db89df27931af7c5beea362a.png)
![14899865-2d7f064ae232b71d.png](https://img-blog.csdnimg.cn/img_convert/a22e604979a266076110bb499d002369.png)
三、ret2shellcode
点击下载文件ret2shellcode
检测保护
![14899865-97770b259c50d567.png](https://img-blog.csdnimg.cn/img_convert/63e0cd8de1c0f311d31608e93a820ebe.png)
基本无保护NOPIE NO canary found并且NX disabled
IDA打开
伪代码
![14899865-a010a70be9d0f1d3.png](https://img-blog.csdnimg.cn/img_convert/bbfbbd86e2230fc1ca515333013e39e0.png)
发现gets,不同的是,这次将s复制给了buf2
进入buf2
![14899865-a99c1663ddebb980.png](https://img-blog.csdnimg.cn/img_convert/5d83c771bb51a5b1ab3c94f88afaad80.png)
发现为.bss段
gdb调用一下文件
![14899865-8aa7d30d1ee76074.png](https://img-blog.csdnimg.cn/img_convert/a344acca830f49d7362f8d03dda39438.png)
![14899865-2dae486ec2f4bceb.png](https://img-blog.csdnimg.cn/img_convert/dc7f3e66a3d89ead2bed02c240c61865.png)
运行所处位置为
![14899865-ae631a6026560fa6.png](https://img-blog.csdnimg.cn/img_convert/cbcbdbc798f32f6cadd3957b3275c0b8.png)
进行内存分析vmmap
![14899865-66c534a669ec5350.png](https://img-blog.csdnimg.cn/img_convert/a0b101d564b497d8df5bac0be43c40d7.png)
该.bss段具有可执行权限
偏移量计算
esp 初始Oxffffcf80
ebp 0xffffd008
d008-vf80=0x88
计算setvbuf()相对ebp偏移
0x88-1ch=6c
所以相对反对地址的偏移为0x6c+4
十进制为112
具有可执行权限的.bss段地址 0x0804A080
payload
![14899865-e7be587513e6cfd5.png](https://img-blog.csdnimg.cn/img_convert/c02d797c6e4871ea25b7439fa7ba996f.png)
总结
栈溢出重要步骤:
1.找出危险函数
2.利用危险函数覆盖函数返回地址或bss段某个变量