RiS3-Lab/DICE-DMA-Emulation
GitHub: RiS3-Lab/DICE-DMA-Emulation
针对嵌入式固件动态分析中因缺失 DMA 支持而导致的覆盖率瓶颈,DICE 提供了一套自动识别与模拟 DMA 输入通道的解决方案,显著提升了漏洞挖掘的深度。
Stars: 64 | Forks: 14
# DICE:用于动态固件分析的 DMA 输入通道自动模拟
这是我们将发表于第 42 届 IEEE 安全与隐私研讨会 (Oakland'21) 论文 DICE 的代码仓库。
除了 DICE 之外,我们还有一系列关于固件重托管(rehosting)的研究工作,包括 *P2IM: Scalable and Hardware-independent Firmware Testing via Automatic Peripheral Interface Modeling*(USENIX Security 2020 论文,源代码发布在[此处](https://github.com/RiS3-Lab/p2im))以及 *AIM: Automatic Interrupt Modeling for Dynamic Firmware Analysis*(IEEE Trans. on Dependable and Secure Computing 2023 论文,源代码发布在[此处](https://github.com/bofeng17/AIM-Interrupt-Modeling))。DICE、P2IM 和 AIM 均旨在不支持外设的模拟器中启用固件测试。它们分别针对外设使用的 DMA、MMIO 和中断通道进行自动化建模。
# 演讲预览
IEEE S&P YouTube 频道的演讲预览视频:https://www.youtube.com/watch?v=i7o9fsj2AkI
# 引用我们的论文
如果您的研究发现 DICE 的一个或多个组件有用,请使用以下引用格式:
```
@inproceedings{dice,
abstract = {Microcontroller-based embedded devices are at the core of Internet-of-Things (IoT) and Cyber-Physical Systems (CPS). The security of these devices is of paramount importance. Among the approaches to securing embedded devices, dynamic firmware analysis (e.g., vulnerability detection) gained great attention lately, thanks to its offline nature and low false- positive rates. However, regardless of the analysis and emulation techniques used, existing dynamic firmware analyzers share a major limitation, namely the inability to handle firmware using DMA (Direct Memory Access). It severely limits the types of devices supported and firmware code coverage.
We present DICE, a drop-in solution for firmware analyzers to emulate DMA input channels and generate or manipulate DMA inputs (from peripherals to firmware). DICE is designed to be hardware-independent (i.e., no actual peripherals or DMA controllers needed) and compatible with common MCU firmware (i.e., no firmware-specific DMA usages assumed) and embedded architectures. The high-level idea behind DICE is the identi- fication and emulation of the abstract DMA input channels, rather than the highly diverse peripherals and controllers. DICE identifies DMA input channels as the firmware writes the source and destination DMA transfer pointers into the DMA controller. Then DICE manipulates the input transferred through DMA on behalf of the firmware analyzer. DICE does not require firmware source code or additional features from firmware analyzers. We integrated DICE to the recently proposed firmware an- alyzer P2IM (for ARM Cortex-M architecture) and a PIC32 emulator (for MIPS M4K/M-Class architecture). We evaluated it on 83 benchmarks and sample firmware, representing 9 different DMA controllers from 5 different vendors. DICE detected 33 out of 37 DMA input channels, with 0 false positives. It correctly supplied DMA inputs to 21 out of 22 DMA buffers that firmware actually use, which previous firmware analyzers cannot achieve due to the lack of DMA emulation. DICE’s overhead is fairly low, it adds 3.4% on average to P2IM execution time. We also fuzz-tested 7 real-world firmware using DICE and compared the results with the original P2IM. DICE uncovered tremendously more execution paths (as much as 79X) and found 5 unique previously-unknown bugs that are unreachable without DMA emulation.},
author = {Mera, Alejandro and Feng, Bo and Lu, Long and Kirda, Engin},
booktitle = {Proceedings of the 42nd IEEE Symposium on Security and Privacy},
month = {May},
series = {S&P/Oakland'21},
title = {DICE: Automatic Emulation of DMA Input Channels for Dynamic Firmware Analysis},
year = {2021}
}
```
# 快速入门
DICE 需要 Ubuntu 16.04 或 18.04 64 位 LTS。我们意识到 Ubuntu 20.04 存在一些问题,因此不支持该版本。
DICE 基于许多开源项目(请参阅论文中的参考文献),特别是它需要 [P2IM 框架](https://github.com/RiS3-Lab/p2im)
用于 ARM Cortex-M 的 Fuzz 测试和基准测试。使用以下命令克隆此代码库并初始化所有必需的子模块:
```
git clone https://github.com/RiS3-Lab/DICE-DMA-Emulation.git
cd DICE-DMA-Emulation
git submodule update --init --recursive
```
## DICE 目录结构
克隆并初始化所有子模块后,目录结构将如下所示:
```
.
├── DICE-Evaluation
│ ├── ARM
│ │ ├── DICE-P2IM-Utilities # DICE-P2IM scripts for model instantiation
│ │ ├── Fuzzing # Firmware binaries, source code and scripts for fuzz testing with DICE-P2IM
│ │ └── Unit-Test # Firmware binaries, source code and scripts for unit test with DICE-P2IM
│ └── MIPS
│ └── Unit-Test # Firmware binaries, source code and scripts for unit test with DICE-MIPS-emulator
├── DICE-Patches # DICE patches (Add-ons) for P2IM and MIPS-emulator
├── DICE-precompiled
│ ├── ARM_bin # Precompiled QEMU-DICE-P2IM binaries with DMA input channels identification and emulation for the ARM Cortex-M
│ ├── ARM_bin_Disabled # Precompiled QEMU-DICE-P2IM binaries with DMA input channels identification for the ARM Cortex-M
│ └── MIPS_bin # Precompiled QEMU-DICE-MIPS-emulator with DMA input channels identification for the MIPS M4K/M-class
├── mips-emulator # Vanilla MIPS emulator submodule
└── p2im # Vanilla P2IM framework submodule
```
## 将 DICE 补丁(Add-on)应用到 QEMU P2IM
使用 DICE “p2im” 目录中的源代码编译 QEMU
以及[此处](https://github.com/RiS3-Lab/p2im/blob/master/docs/build_qemu.md)提供的说明。
如果 vanilla QEMU P2IM 编译失败,请勿继续。在继续之前,您必须解决任何依赖项
或编译问题。
编译完成后,从 DICE 目录结构的根目录执行以下命令,并忽略任何警告消息:
```
git apply ./DICE-Patches/DICE-P2IM.patch --unsafe-paths --directory ./p2im/qemu/src/qemu.git/
```
再次编译 QEMU 以完成 DICE 和 QEMU P2IM 的集成。
编译后的 QEMU 二进制文件将位于:
`./p2im/qemu/src/install/debian64/qemu/bin`
## 编译 AFL
从 DICE 目录的根目录执行以下命令:
```
cd p2im
make -C afl/
```
编译后的 AFL 二进制文件将位于:
`./p2im/afl`
## 将 DICE 补丁(Add-on)应用到 QEMU MIPS-emulator
从 DICE 目录的根目录执行以下命令,并忽略任何警告消息:
```
git apply ./DICE-Patches/DICE-MIPS-EMULATOR.patch --unsafe-paths --directory ./mips-emulator
cd mips-emulator
./configure --prefix=/usr/local/qemu-mips --target-list=mipsel-softmmu --disable-werror --disable-xen
make
```
# DICE:ARM Cortex-M 固件单元测试
我们提供了一个辅助[脚本](./DICE-Evaluation/ARM/Unit-Test/run.py)来自动准备 P2IM 所需的[配置文件](https://github.com/RiS3-Lab/p2im#preparing-the-configuration-file)。
该脚本启动 DICE-P2IM 来识别 DMA 输入通道并实例化外设模型,直到固件执行不再访问新的未知接口。
```
./run.py f103 ./Firmware/Binaries-DICE/F103_ADC_SingleConversion_TriggerTimer_DMA.elf ./output
```
要验证自动识别的 DMA 输入通道以及对先前示例中 DMA 缓冲区的访问,请执行以下命令:
```
cd DICE-Evaluation/ARM/Unit-Test
./DMAtrace.py ./output/dma_trace ./dma.txt
```
输出文件 `dma.txt` 包含固件 DMA 操作的过滤后执行跟踪。
例如:
```
DMA Stream configuration identified: *0x40020000 p_A:*0x40020010->*0x4001244c p_B:*0x40020014->*0x200000a4
```
其中:
`0x40020000` 是 STM32F103 MCU 中 DMA 控制器的映射基地址
`p_A:*0x40020010->*0x4001244c` 是指向外设寄存器的指针(源),以及
`p_B:*0x40020014->*0x200000a4` 是指向 RAM 中缓冲区的指针(目标)
除了此示例外,过滤后的跟踪文件还包含更多与 DMA 缓冲区大小推断和通过 DMA 输入通道进行数据消耗相关的自我解释消息。
我们还提供了 [runbatch.py](DICE-Evaluation/ARM/Unit-Test/runbatch.py) 脚本,用于执行 DICE 论文中描述的整个单元测试。此脚本可能需要几分钟或几小时才能完成。
# DICE:Fuzzing 现实世界的 ARM Cortex-M 固件
使用 DICE-P2IM 进行 Fuzzing 遵循为 P2IM 框架描述的相同[工作流程](https://github.com/RiS3-Lab/p2im/blob/master/README.md#fuzzing)。因此,P2IM
文档对于 DICE-P2IM 集成仍然相关,您应该查看它以获取更多详细信息。
除了相似之处外,我们在此代码库中提供了一组经过轻微修改的脚本,并采用特定的目录结构,以便轻松复现我们论文中展示的实验。
要 Fuzz 真实固件,请按照以下步骤操作:
1. 创建一个基础目录来存储 Fuzzing 工作文件。我们提供了一个方便的[脚本](DICE-Evaluation/ARM/Fuzzing/CreateBaseDir.py),它会
为您完成此操作。我们建议在您 home 目录的根目录下创建基础目录,否则
QEMU 可能会抱怨路径过长并停止执行。
例如:
```
./DICE-Evaluation/ARM/Fuzzing/CreateBaseDir.py -B ~/FuzzBase -R 1.0
```
此脚本为 7 个测试固件创建一个完整的工作树和默认种子输入,如下所示:
```
FuzzBase
├── GPSReceiver
│ └── 1.0
│ ├── inputs
│ │ └── input.data
│ └── outputs
├── GuitarPedal
│ └── 1.0
│ ├── inputs
│ │ └── input.data
│ └── outputs
:
:
└── StepperMotor
└── 1.0
├── inputs
│ └── input.data
└── outputs
```
2. 根据 AFL 要求配置您的 Linux 内核。以 root 身份执行以下命令:
```
su -
echo core >/proc/sys/kernel/core_pattern
cd /sys/devices/system/cpu
echo performance | tee cpu*/cpufreq/scaling_governor
exit
```
3. 使用提供的配置文件和先前创建的基础目录启动 Fuzzing 活动。
例如:
```
cd ./DICE-Evaluation/ARM/Fuzzing
export FUZZDIR=/home/$USER/FuzzBase
./fuzz.py -c ./Configs/Modbus.cfg
```
4. 使用为 P2IM 提供的相同[说明](https://github.com/RiS3-Lab/p2im/blob/master/README.md#analyzing-fuzzing-results)分析 Fuzzing 结果。
# DICE:MIPS M4K/M-Class 单元测试
在启动单元测试之前,您需要解压缩 BSD 发行版的文件系统映像
```
cd ./DICE-Evaluation/MIPS/Unit-Test/Unix/Filesystem
unzip ./filesystem.zip
cd ../../
```
要运行 MIPS 单元测试,请执行[脚本](DICE-Evaluation/MIPS/Unit-Test/run.py)并传递相应的配置文件。
例如:
```
cd ./DICE-Evaluation/MIPS/Unit-Test
./run.py -c ./Configs/runBSD-Lite-PIC32MZ.cfg -o ./dma.txt
```
此脚本将执行 QEMU DICE-Mips-emulator 10 秒钟,然后自动停止。输出 `dma.txt` 文件将包含与 ARM Cortex-M 单元测试描述类似的跟踪。值得注意的是,某些固件未启用 DMA,可能不会产生任何与 DMA 相关的跟踪,这是预期的结果(无假阳性)。
# 更多文档
请参考我们的[论文](https://www.longlu.org/publication/dice/dice.pdf)。
# 问题、疑问与支持
请在 GitHub 代码库中开启一个新 issue,并包含您具体询问的详细信息。
标签:CPS安全, DMA仿真, DMA输入, IEEE S&P, 固件Rehosting, 固件分析, 固件逆向, 外设建模, 客户端加密, 客户端加密, 寄存器传输级, 嵌入式安全, 嵌入式系统, 物联网安全, 硬件安全, 符号执行, 网络安全, 身份验证强制, 逆向工具, 隐私保护