APDU模糊器
apdu-fuzzer的Python项目详细描述
PIP安装
$> pip install apdu-fuzzer $> apdu-fuzz --help usage: apdu-fuzz [-h] [--start_ins START_INS] [--end_ins END_INS] [--output OUTPUT_FILE] [--no-trust] Fuzz smartcard api. optional arguments: -h, --help show this help message and exit --start_ins START_INS Instruction to start fuzzing at --end_ins END_INS Instruction to stop fuzzing at --output OUTPUT_FILE File to output results to --no-trust $> apdu-afl-fuzz --help
在基于Debian的Linux上安装
我们需要https://github.com/mit-ll/LL-Smartcard才能让fuzzer工作 及其依赖关系:
git clone https://github.com/mit-ll/LL-Smartcard cd LL-Smartcard ./install_dependencies.sh python2 setup.py install
在MacOS上安装
我们需要https://github.com/mit-ll/LL-Smartcard才能让fuzzer工作 及其依赖关系:
brew install swig brew install pcsc-lite pip install llsmartcard-ph4
pip
# Create virtual environment python -m venv --upgrade venv cd python # Install all project dependencies ../venv/bin/pip install --find-links=. --no-cache . # Install AFL deps (cython required) # Mac: brew install afl-fuzz # Others: cd /tmp wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz tar -xzvf afl-latest.tgz cd afl-* make sudo make install # Install python dependencies ../venv/bin/pip install --find-links=. --no-cache .[afl]
AFL模糊
AFL <-> Client <-> Server <-> Card +----------------------------------+ | AFL | | | | | +-------------------+ | | | +----------------+ | +------------------+ | | | | | stdin | | | socket | | | +---+ | | | +----------| Client |------------ Server -------- |-|-| Card | | | | | | | | | +---+ | | | +------+ +--------|-------+ | +------------------+ | | | +-| SHM |------------+ | +-------------------+ | +------+ | | | +----------------------------------+
(ascii byhttps://textik.com/)
注意:
- 服务器首先启动,连接到卡并侦听 原始数据发送到卡的套接字。不处理输入数据 无论如何。
- 服务器存储从卡到数据文件的原始响应。
- 如果出现问题,服务器可以重新连接到卡。
- 客户机由AFL启动。AFL通过stdin发送输入数据,分叉 每个新模糊输入的客户端。PCSC不喜欢用AFL叉菜 此服务器/客户端架构是必需的。
- 在python初始化之后,afl将派生客户机。插座罐 在叉前或叉后打开。叉子安全之后 因为每个fuzz输入都有一个新的连接,但速度稍慢。打开 叉子前的插座也能工作,但需要特别注意 断管异常-需要重新连接逻辑。这不是 现在实施。
- 客户后期处理由AFL生成的输入数据,例如, 生成长度字段,可以执行TLV等操作。
服务器/客户端之间的通信:
- 客户端发送[0, buffer]。缓冲区是要发送的原始数据结构 到卡片上。0是类型/状态
- 服务器响应: status 1B | SW1 1B | SW2 1B | timing 2B | data 0-NB
+----+----+----+--------+------------------------+ | | | | | | | 0 | SW | SW | timing | response data | | | 1 | 2 | | | +----+----+----+--------+------------------------+
然后,客户机从套接字获取响应,并使用modified python-afl-ph4添加跟踪 到共享内存段,稍后由afl分析以确定 此模糊输入是否导致不同于 上一个。
当前跟踪位图是按以下方式完成的:
afl.trace_offset(hashxx(bytes([sw1,sw2])))afl.trace_offset(hashxx(timing))afl.trace_offset(hashxx(bytes(data)))
在afl.trace_buff中使用的fowler noll vo哈希函数不是很好 关于零缓冲区。时间安排通常不影响 位图,因此我们切换到非常快速的哈希函数hashxx 偏移计算。
运行
启动卡上的服务器:
python main_afl.py --server
测试客户端是否工作:
echo -n '0000' | ../venv/bin/python main_afl.py --client --output ydat.json --log ylog.txt cat ylog.txt
带分叉的AFL和与服务器的TCP通信:
../venv/bin/py-afl-fuzz -m 500 -t 5000 -o result/ -i inputs/ -- ../venv/bin/python main_afl.py --client --output ydat.json --log ylog.txt
地方发展
apdu_fuzzer包正在使用相对导入。为了发展和 因此,加载主包需要在本地目录中进行调试 apdu_fuzzer先。否则会出现以下错误:
Traceback (most recent call last): File "../apdu_fuzzer/main_afl.py", line 17, in <module> from .utils.card_interactor import CardInteractor ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package
对于本地执行,请使用主目录中的包装器:
../venv/bin/python ../main_afl.py --help