open-crs/attack_surface_approximation
GitHub: open-crs/attack_surface_approximation
这是一个用于发现易受攻击程序攻击面的模块,通过静态分析和模糊测试识别输入机制并优化参数触发。
Stars: 2 | Forks: 4
# 攻击面近似 🤺
- [描述](#description)
- [局限性](#limitations)
- [工作原理](#how-it-works)
- [设置](#setup)
- [使用方法](#usage)
- [作为命令行工具](#as-a-cli-tool)
- [为参数生成字典](#generate-dictionary-for-arguments)
- [输入流检测](#detect-input-streams)
- [参数模糊测试](#fuzz-arguments)
- [获取帮助](#get-help)
- [作为 Python 模块](#as-a-python-module)
- [输入流检测](#detect-input-streams-1)
- [参数模糊测试](#fuzz-arguments-1)
## 描述
`attack_surface_approximation` 是一个 CRS 模块,用于处理易受攻击程序中攻击面的近似。
一些输入机制被忽略了:用户界面元素、信号、设备和中断。目前支持的机制如下:
- 文件
- 命令行参数
- 标准输入
- 网络
- 环境变量
此外,实现了一个自定义的模糊测试器,以发现能触发不同代码覆盖率的参数。
它从字典中获取参数,该字典可以手工制作,也可以使用公开的命令生成,并实现了启发式方法。
参数字典的示例可在 `examples/dictionaries` 中找到:
- `man.txt`:使用 `man_parsing` 启发式方法生成,包含 6605 个条目
- `generation.txt`:使用 `generation` 启发式方法生成,包含 62 个条目
### 局限性
- ELF 格式
- x86 架构
- 动态二进制文件(不支持静态二进制文件)
- 存在符号(即未进行剥离)
- 未涉及混淆技术
## 工作原理
该模块通过自动化 [Ghidra](https://ghidra-sre.org/) 进行静态二进制分析。
它提取信息并应用启发式方法来确定是否存在给定的输入流。
这类启发式方法的例子有:
- 对于标准输入:对 `getc()` 和 `gets()` 的调用
- 对于网络:对 `recv()` 和 `recvfrom()` 的调用
- 对于命令行参数:`main()` 中 `argc` 和 `argv` 的出现
参数模糊测试器使用 [Docker](https://www.docker.com/) 进行运行,并使用 [QBDI](https://qbdi.quarkslab.com/) 来检测基本块覆盖率。
## 设置
1. 确保你已根据[顶层说明](https://github.com/open-crs#requirements)设置了代码仓库和 Python 环境。
即:
- Docker 已安装并正常运行。
使用以下命令检查:
docker version
docker ps -a
docker run --rm hello-world
这些命令应能无误运行。
- 当前模块仓库和所有其他模块仓库(特别是 [`dataset` 仓库](https://github.com/open-crs/dataset)和 [`commons` 仓库](https://github.com/open-crs/commons))已克隆到同一目录。
- 你正在一个 Python 虚拟环境中运行所有命令。
你的命令提示符前应带有 `(.venv)` 前缀。
- 你已在虚拟环境中安装了 Poetry。
如果你运行:
which poetry
你应该会得到一个以 `.venv/bin/poetry` 结尾的路径。
2. 禁用 Python Keyring:
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring
这可能在某些情况下出现问题,阻止 Poetry 获取软件包。
3. 使用 Poetry 安装所需的软件包(基于 `pyprojects.toml`):
poetry install --only main
4. 使用 [`commons` 仓库中的说明](https://github.com/open-crs/commons?tab=readme-ov-file#setup)创建 `ghidra` 和 `qbdi_args_fuzzing` Docker 镜像。
5. 可选地,使用 [`dataset` 仓库中的说明](https://github.com/open-crs/dataset)生成可执行文件。
## 使用方法
你可以将 `attack_surface_approximation` 模块作为独立的命令行工具使用,或者作为 Python 模块集成到 Python 应用程序中。
### 作为命令行工具
作为命令行工具,你可以使用 `cli.py` 模块:
```
python attack_surface_approximation/cli.py
```
或者使用 Poetry 接口:
```
poetry run attack_surface_approximation
```
#### 为参数生成字典
```
$ poetry run attack_surface_approximation generate --heuristic man_parsing --output args.txt --top 100
Successfully generated dictionary with 10 arguments
$ head args.txt
--allow-unrelated-histories
--analysis-display-unstable-clusters
--auto-area-segmentation
--backup-dir
--callstack-filter
--cidfile
--class
--codename
--column
--contained
```
#### 检测输入流
使用 ELF i386(32 位)可执行文件作为检测输入流的目标。
例如,你可以使用在 [`dataset` 仓库](https://github.com/open-crs/dataset)中生成的可执行文件之一:
```
$ ../dataset/executables/toy_test_suite_1.elf
Gimme two lines of input:
aaa
bbb
```
现在,进行攻击面近似:
```
$ poetry run attack_surface_approximation detect --elf $(pwd)/../dataset/executables/toy_test_suite_1.elf
Several input mechanisms were detected for the given program:
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓
┃ Stream ┃ Present ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩
│ STDIN │ Yes │
│ ARGUMENTS │ Yes │
│ FILES │ Yes │
│ ENVIRONMENT_VARIABLE │ Yes │
│ NETWORKING │ Yes │
└──────────────────────┴─────────┘
```
所使用的可执行文件使用了所有潜在的输入流。
#### 模糊测试参数
```
$ poetry run attack_surface_approximation fuzz --elf $(pwd)/../dataset/executables/toy_test_suite_1.elf --dictionary args.txt
Several arguments were detected for the given program:
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ Argument ┃ Role ┃
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ - │ FLAG │
│ --re │ FLAG │
│ --re string │ STRING_ENABLER │
│ -mmusl │ FLAG │
└─────────────┴────────────────┘
```
#### 获取帮助
```
$ poetry run attack_surface_approximation
Usage: attack_surface_approximation [OPTIONS] COMMAND [ARGS]...
Discovers the attack surface of vulnerable programs.
Options:
--help Show this message and exit.
Commands:
analyze Analyze with all methods.
detect Statically detect what input streams are used by an executable.
fuzz Fuzz the arguments of an executable.
generate Generate dictionaries with arguments, based on heuristics.
```
### 作为 Python 模块
#### 检测输入流
```
from attack_surface_approximation.static_input_streams_detection import \
InputStreamsDetector
detector = InputStreamsDetector(elf_filename)
streams_list = detector.detect_all()
```
#### 模糊测试参数
```
from attack_surface_approximation.arguments_fuzzing import ArgumentsFuzzer
fuzzer = ArgumentsFuzzer(elf_filename, fuzzed_arguments)
detected_arguments = fuzzer.get_all_valid_arguments()
```
标签:ELF格式, Ghidra, Python模块, x86架构, 二进制分析, 云安全监控, 云安全运维, 云资产清单, 代码覆盖, 动态二进制文件, 参数模糊测试, 命令行参数, 安全, 安全测试, 攻击性安全, 攻击面评估, 标准输入, 漏洞分析, 环境变量, 网络, 网络安全, 自动化安全工具, 请求拦截, 超时处理, 路径探测, 输入流检测, 逆向工具, 逆向工程, 隐私保护, 静态分析