文章目录

  • Towards Automating Code-Reuse Attacks Using Synthesized Gadget Chains
  • abstract
  • Introduction
  • shortcomings of state-of-the-art approaches
  • Design
  • gadgets
  • Logical encoding
  • precoditions and postconditions
  • Formula Generation
  • Algorithm configuration
  • Implementation
  • Evaluation
  • Setup
  • Finding a chain
  • Real-World Applicability
  • Target-Specific Constraints
  • Chain statistics
  • Limitation


Towards Automating Code-Reuse Attacks Using Synthesized Gadget Chains

ESORICS 2021

abstract

In the arms race between binary exploitation techniques and mitigation schemes, code-reuse attacks have been proven indispensable.

arm race 军备竞赛
在军备竞赛中的利用技术和缓解方案中,代码重用攻击必不可少。

Typically, one of the initial hurdles is that an attacker cannot execute their own code due to countermeasures such as data execution prevention(DEP).

一种初始障碍是攻击者不能在对抗措施下执行代码

While this technique is powerful, the task of finding and correctly chaining gadgets remains cumbersome.

查找和链接gadgets仍然较为繁琐。

Although various methods automating this task have been proposed, they either rely on hard-coded heuristics or make specific assumptions about the gadgets semantics.

现有的方法仍然依靠硬编码启发式算法或者对gadgets做出假设

This not only drastically limits the search space but also sacrifices their capability to find valid chains unless specific gadgets can be located.

As a result, they often produce no chain or an incorrect chain that crashes the program.

In this paper, we present SGC, the first generic approach to identify gadget chains in an automated manner without imposing restrictions on the gadgets or limiting its applicability to specific exploitation scenarios.

提出通用的方法去识别gadgets chain不需要极大的限制

Instead of using heuristics to find a gadget chain, we offload this task to an SMT solver.

没有使用启发式方法而是交给SMT求解器。

More specifically, we build a logical formula that encodes the CPU and memory state at the time when the attacker can divert execution flow to the gadget chain, as well as the attacker’s desired program state that the gadget chain should construct.

In combination with a logical encoding of the data flow between gadgets, we query an SMT solver whether a valid gadget chain exists.

If successful, the solver provides a proof of existence in the form of a synthesized gadget chain.

This way, we remain fully flexible w.r.t to the gadgets.

w.r.t关于

In empirical tests, we find that the solver often uses all types of control-flow transfer instructions and even gadgets with side effects.

发现求解器经常使用所有类型的控制流传输指令,甚至带有副作用的gadgets

Our evaluation shows that SGC successfully finds working gadget chains for real-world exploitation scenarios within minutes, even when all state-of-the-art approaches fail.

Introduction

DEP的引入使得注入代码的执行变得不可能,因为内存被标记为可写或可执行。这使得攻击者开发新的技术来重用现有代码。(例如ret2libc)

作为额外的防线,现代操作系统随机化程序的地址空间布局(ASLR)。

尽管如此,单个信息泄露或非随即部分仍会为攻击者提供发动攻击的能力。

控制流完整性CFI强制执行仅执行程序所需的良性集合内的合法控制流转换的属性,这虽然会极大的限制攻击者链接任意代码片段的自由,但代码重用攻击在实践中仍是可行的。

最初尝试利用基于模式匹配的策略来识别chain,后来利用符号执行对gadget进行分类并识别不良副作用,例如将值写入内存。

但迄今为止最先进的方法也依赖于各种启发式方法限制大型搜索空间。

这些启发式方法试图找到通用chain来跨多个目标工作,但在某些情况下并不存在这样的chain。

作者提出一种不修剪搜索空间的情况下有效查找gadgets的方法。SMT求解器,检查是否可以满足一组逻辑公式(约束),通过建立一个逻辑公式来描述:

  • 执行第一个gadgets之前CPU和内存的状态
  • 攻击者所需要的CPU和内存的状态
  • gadgets之间的数据流

作者对小工具链进行建模,综合作为可达性问题,并使用SMT求解器解决,这种方法类似于有界模型检查,这是一种用于确定系统是否满足给定要求的软件验证技术。它结合了一组在执行前必须保持的假设,和一组再执行后保持程序语义的逻辑编码,然后查询SMT求解器,如果返回可满足则会提供一个模型,表示满足给定约束的具体变量分配。如果不可满足则SMT求解器在数学上证明不能满足约束。

作者演示了一个小例子:为dnsmasq中基于堆栈的buffer overflow生成shell,执行execve(&"/bin/sh", 0, 0)生成rop chain,无需任何其他信息。

shortcomings of state-of-the-art approaches

现有的gadgets chain可以分为两类:

  • 硬编码的链接规则:Ropper和ROPgadget属于这一类,需要基于正则表达式的硬编码来连接gadgets,这些工具显然是不灵活的
  • 符号探索:angrop和ROPium是对gadgets的中间表示进行操作,这允许它们符号方式确定副作用并进行分类。小工具首先被提升,然后被分析最后被串联起来。后者通常涉及一种算法,如深度优先搜索(ROPium)或广度优先搜索(angrop),以确定符合攻击者规范的gadgets序列。缺乏对更复杂约束的支持。

avamar 出现the system has data integrity alerts the system alarm has occurred_安全

Design

gadgets

  1. 从目标程序提取gadgets:(作者为了简洁起见,并未在文中给出具体方法,反而是引用了若干文献,笔者选择了一篇简略查看)
  • Jump-oriented programming: a new class of code-reuse attack
  • 简单的反汇编并搜索间接跳转调用指令,算法如下
  • 设定一系列约束条件:如反汇编每次向后走一字节,gadgets的语义发生变化则淘汰,跳转的目标地址在运行时不可能是有效、可写的位置等等
  1. 由于gadgets通常具有副作用(例如mul rbx饮食修改rdx、rax和rflags寄存器),因此作者将gadgets提升为具有显式副作用的中间表示(IR)。

avamar 出现the system has data integrity alerts the system alarm has occurred_寄存器_02

上图a中即是将汇编指令转为中间表示

Logical encoding

gadget chain将初始程序状态(前置条件)转为所需的程序状态(后置条件),因此需要对gadget和chain的语义进行逻辑编码。

gadget的语义,指令之间的数据流和gadgets之间的数据流进行建模,编码并将其组合为一个公式。

作者使用静态单一赋值SSA形式使变量赋值成为有状态的,为了区分gadget,作者在SSA变量名称前加上每个gadget的唯一标识符。如果某个gadgets使用了之前未定义的变量,则在其后添加IN,表示gadget的输入。

图1b为示例

连接gadgets,作者不对gadgets的顺序和使用的特定gadgets作出假设,并且允许gadget在链中多次出现,因此下一步是对小工具之间的逻辑流进行逻辑编码。

需要保证i+1位置的gadget使用i位置的gadget导出值,因此必须合并chain中i位置的所有gadgets的状态,以便可以用作后续位置的小工具输入。

precoditions and postconditions

前提条件:允许攻击者可以将执行流转移到gadget chain的时候设置初始状态,限制了gadget chain的第一个位置的输入。

后置条件:描述了程序在执行gadget chain后应该达到的理想状态,更具体的说,可以将任何寄存器或内存地址设置为一个特定的值,例如想执行的系统调用及其参数。

该方法可以强制要求在寄存器和内存位置之间执行任意的约束,例如强制要求某些寄存器的值必须是技术,寄存器的总和必须等于一个特定的值等等。

Formula Generation

avamar 出现the system has data integrity alerts the system alarm has occurred_SMT_03

最终的公式由三个主要部分构成:前提条件、gadget chain、后置条件。

前提条件描述初始状态,用来作为chain的初始gadget输入

gadget chain 包含单个指令的编码,gadget内指令之间的数据流以及gadget之间的数据流。

后置条件定义了执行gadget chain后应该达到的状态。

将公式传递给SMT求解器,给出满足赋值的相关模型。

Algorithm configuration

一些参数定义了该方法的性能:

  • SMT求解器在大量gadget空间中搜索较为费时,因此为了减少运行时间,作者对gadget的一小部分进行采样
  • 由于逻辑编码,必须在利用前定义chain的长度,看起来似乎不太灵活,但拒作者评估表明,不同的chain的长度在实践中是可行的,使用更短的链则SMT求解器将语义无操作作为gadget放在链中
  • 为了避免过长的运行时间,作者对gadget的提取和smt求解的时间上限进行了限制。

Implementation

5000行代码

https://github.com/RUB-SysSec/gadget_synthesis

gadget的提取依赖于Binary Ninja(V2.3.2660)

所有进一步的步骤都建立在Miasm之上 https://github.com/cea-sec/miasm

然后交给SMT求解器 Boolector

Evaluation

作者尝试解决以下问题

Based on the prototype implementation of SGC, we answer the following
questions:
1. Is SGC capable of automatically finding valid gadget chains in diverse exploitation
scenarios? How does it compare to state-of-the-art tools?
2. How does SGC perform in real-world exploitation scenarios?
3. How flexible and target-specific are SGC’s chains in comparison to other
approaches?
4. In what regard do SGC’s generated gadget chains differ from the ones found
by state-of-the-art tools?

Setup

x86-64架构,为了便于分析,禁用了ASLR。

使用不同的程序集,chromium,apache2,nginx和openssl的最新版本,这些目标都是动态链接的,为工具配置忽略共享库,模拟只知道主可执行文件的基地址,但不知道库位置的场景。

为了涵盖存在libc的场景,作者创建了一个与glibc2.31版静态链接的空包装程序。

为了评估是否可用于现实世界的漏洞,使用了dnsmasq版本2.77评估

Finding a chain

当前攻击者主要针对调用库函数,例如mprotect更改内存区域的保护标志,mmap映射可以放置其shellcode的RWX页面或在执行系统调用是选择带有参数的execve。

作者选择三个示例性攻击目标:

  • mrotect(addr, len, prot) 三个参数
  • mmap(addr, length, prot, flags,fd, offset)六个参数
  • execve(path, argv, envp) 四个参数,还有一个是系统调用号,以及将字符串放入内存中

在x86-64架构下,这些参数是通过寄存器传递的。结果如下图所示

avamar 出现the system has data integrity alerts the system alarm has occurred_寄存器_04

Real-World Applicability

作者使用dnsmasq中CVE-2017-14494漏洞进行实验,该漏洞是基于stack的buffer overflow,攻击者可以制造一个恶意的DHCPv6数据包,当dnsmasq的DHCP服务器接收到该数据包时,会触发dhcp6中继功能的一处,其中memcpy的长度和数据可以由攻击者掌控。

作者的目标是制作一个调用execve(&“bin/sh”, 0, 0)生成shell的gadget chain,rax需要保存execve系统调用号0x3b,而寄存器rdi、rsi和rdx将参数传递给execve。

作者使用GDB和相应寄存器值转储CPU状态,定义前置条件和后置条件。

Target-Specific Constraints

坏字节,在底层程序中充当终止符,例如C字符串中的\x00,作者可以通过添加攻击者控制缓冲区中的每个字节与特定字节值不同的约束来避免使用坏字节。

作者通过坏字节设置来比较相关工具之间的灵活性。

Chain statistics

链的统计分析

avamar 出现the system has data integrity alerts the system alarm has occurred_反汇编_05

耗时分析:

avamar 出现the system has data integrity alerts the system alarm has occurred_反汇编_06

反汇编时间较长,依赖于Binary Ninja和Miasm的组合,首先分析整个二进制,然后在Miasm中反汇编单个函数。

Limitation

  1. 反汇编方法幼稚,只考虑了常规的指令偏移量,作为一种改进可以搜索不对齐的小工具,因为任何字节序列都可以呗解释为x86-64的指令
  2. SMT求解器运行时间过长

DEP无法防御代码重用,ASLR可以通过基址泄露绕过,CFI防止控制流重定向到任意的代码位置,但可以在SMT中加入约束条件,使SMT只选择通过CFI执行策略的rop chain。