

Rex:栈溢出之Exploit自动生成
source link: https://www.freebuf.com/vuls/195514.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

*本文原创作者:xiaohan0x00,本文属FreeBuf原创奖励计划,未经许可禁止转载
Rex 是由 Shellphish 开发的自动化漏洞利用引擎,设计初衷在于参加 Cyber Grand Challenge 。本文以栈溢出为例,展示 Rex 自动生成 Exploit 的能力。测试样例为 Linux 下可执行程序 vuln_stacksmash ,其中存在栈溢出漏洞,通过 Rex 自动生成 rop2system、rop2text、jmpesp 三种 Exploit。
0×00 vuln_stacksmash 栈溢出漏洞
使用 radare2 简要分析 vuln_stacksmash。vuln() 函数中,调用 read(int fd, void * buf, size_t count) 时未检查缓冲区大小,导致栈溢出。
使用 GDB 调试 vuln_stacksmash,运行至溢出点,程序状态如下:
计算偏移为 0×44,构造 PoC 并输入。单步执行并检查栈帧情况,可见 EBP 已被 “\x41\x41\x41\x41” 覆盖,后续四个字节为 EIP 值,已被覆盖为 “\x42\x42\x42\x42”。
跟踪至 vuln() 返回,触发异常,EIP 被劫持为 “\x42\x42\x42\x42”。
上文简要分析了 vuln_stacksmash 中存在的栈溢出漏洞,下文将介绍如何利用 Rex 自动生成 Exploit。
0×01 Rex 脚本
Rex 的实现基于 Angr,主要采用混合符号执行技术,对原理感兴趣的同学可以阅读论文 《(State of) The Art of War: Offensive Techniques in Binary Analysis》 。由于封装的原因,整体代码看起来较为简洁,从漏洞复现、漏洞类型判定、Exploit 生成到 Exploit 有效性验证,共计 10 行代码。其中,Crash 类用以复现漏洞并返回漏洞类型,Exploit 类用以判定漏洞的可利用性,并生成 Exploit。171 ~ 174 行用以验证 Exploit 的有效性。
0×02 漏洞复现
Rex 在实现混合符号执行时,首先使用 QEMU 进行 Concrete Execution,在获取到 Crash 状态后,使用 Angr 进行 Concolic Execution,相关功能封装在 Crash 类中。
1、Concrete Execution
使用 QEMU 加载 vuln_stacksmash,以 PoC 为输入运行,获取程序崩溃时的状态。执行结果如下:
2、Concolic Execution
在获取到 Crash state 后,基于 Angr 实现 Concolic Execution。首先设置程序初始状态。
使用 ‘posix’、’preconstrainer’ 插件辅助分析:
SimSystemPosix()
Data storage and interaction mechanisms for states with an environment conforming to posix. Available as “state.posix“.
SimStatePreconstrainer()
This state plugin manages the concept of preconstraining – adding constraints which you would like to remove later. :param constrained_addrs : SimActions for memory operations whose addresses should be constrained during crash analysis
使用 ‘Tracer’、’Oppologist’ 两种 Exploration_techniques:
设置 simulation_manager :
执行结果:
0×03 漏洞类型判定
Crash 类中的 _triage_crash() 方法对漏洞类型进行判定。
执行结果为 IP_OVERWRITE:
0×04 Exploit 生成
代码中设置了 rop_leak_memory、rop_set_register、 explore_for_exploit 三种利用方式。通过阅读源码可知,rop_leak_memory、rop_set_register 均是针对 CGC 格式文件的利用技术。在 Rex 现有的 exploit_technique 中,call_jmp_sp_shellcode、call_shellcode、rop_to_system 三种技术用以针对 ELF 文件。执行结果如下:
0×05 Verified
对三种生成结果进行确认。
0×06 小结
通过学习 Rex 源码以及混合符号执行,成功对栈溢出漏洞生成 Exploit。文中所针对的漏洞非常简单,且未涉及 ASLR 等安全机制的绕过。由于弟才疏学浅,文中难免存在理解不当之处,望各位师傅批评指正。
*本文原创作者:xiaohan0x00,本文属FreeBuf原创奖励计划,未经许可禁止转载
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK