Rex:栈溢出之Exploit自动生成

admin 2022年5月29日14:13:24评论40 views字数 1927阅读6分25秒阅读模式

Rex:栈溢出之Exploit自动生成


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) 时未检查缓冲区大小,导致栈溢出。


Rex:栈溢出之Exploit自动生成


使用 GDB 调试 vuln_stacksmash,运行至溢出点,程序状态如下:


Rex:栈溢出之Exploit自动生成


计算偏移为 0×44,构造 PoC 并输入。单步执行并检查栈帧情况,可见 EBP 已被 “x41x41x41x41” 覆盖,后续四个字节为 EIP 值,已被覆盖为 “x42x42x42x42”。


Rex:栈溢出之Exploit自动生成


跟踪至 vuln() 返回,触发异常,EIP 被劫持为 “x42x42x42x42”。


Rex:栈溢出之Exploit自动生成


上文简要分析了 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 

的有效性。


Rex:栈溢出之Exploit自动生成


0×02 漏洞复现

Rex 在实现混合符号执行时,首先使用 QEMU 进行 Concrete Execution,在获取到 Crash 状态后,使用 Angr 进行 Concolic Execution,相关功能封装在 Crash 类中。

1、Concrete Execution

使用 QEMU 加载 vuln_stacksmash,以 PoC 为输入运行,获取程序崩溃时的状态。执行结果如下: 


Rex:栈溢出之Exploit自动生成


2、Concolic Execution

在获取到 Crash state 后,基于 Angr 实现 Concolic Execution。首先设置程序初始状态。


Rex:栈溢出之Exploit自动生成


Rex:栈溢出之Exploit自动生成


使用 ‘posix’、’preconstrainer’ 插件辅助分析:


Rex:栈溢出之Exploit自动生成


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:


Rex:栈溢出之Exploit自动生成

Rex:栈溢出之Exploit自动生成


设置 simulation_manager :


Rex:栈溢出之Exploit自动生成


执行结果: 


Rex:栈溢出之Exploit自动生成


0×03 漏洞类型判定

Crash 类中的 _triage_crash() 方法对漏洞类型进行判定。


Rex:栈溢出之Exploit自动生成


执行结果为 IP_OVERWRITE:


Rex:栈溢出之Exploit自动生成


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 文件。执行结果如下: 


Rex:栈溢出之Exploit自动生成


0×05 Verified

对三种生成结果进行确认。


Rex:栈溢出之Exploit自动生成


Rex:栈溢出之Exploit自动生成


0×06 小结

通过学习 Rex 源码以及混合符号执行,成功对栈溢出漏洞生成 Exploit。文中所针对的漏洞非常简单,且未涉及 ASLR 等安全机制的绕过。由于弟才疏学浅,文中难免存在理解不当之处,望各位师傅批评指正。


Rex:栈溢出之Exploit自动生成


本文始发于微信公众号(疯猫网络):Rex:栈溢出之Exploit自动生成

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年5月29日14:13:24
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Rex:栈溢出之Exploit自动生成http://cn-sec.com/archives/511672.html

发表评论

匿名网友 填写信息