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.
- package/AGENTS.md +131 -0
- package/Dockerfile +192 -0
- package/README.md +522 -0
- package/agents/ctf-analyzer.md +58 -0
- package/agents/ctf-controller.md +151 -0
- package/agents/ctf-speedrun.md +74 -0
- package/bin/ctf-agent.js +119 -0
- package/docker-compose.yml +13 -0
- package/mcp_config.json +19 -0
- package/package.json +44 -0
- package/references/ctf-triage-ladder.md +68 -0
- package/references/exploit-databases.md +575 -0
- package/references/llm-safety-and-policy-compliance.md +173 -0
- package/references/multi-agent-orchestration-and-policy-routing.md +278 -0
- package/references/security-events-and-intelligence.md +566 -0
- package/references/version-matrix.md +59 -0
- package/rules/ctf-execution-rules.md +44 -0
- package/rules/ctf-safety-framing-rules.md +140 -0
- package/rules/flag-validation-rules.md +25 -0
- package/scripts/__init__.py +1 -0
- package/scripts/ctf-tools.lock +205 -0
- package/scripts/ctf_init.py +1026 -0
- package/scripts/ctf_update.py +583 -0
- package/scripts/ctfd_client.py +81 -0
- package/scripts/cve_lookup.py +453 -0
- package/scripts/extract_flags.py +155 -0
- package/scripts/install_as_agent.py +278 -0
- package/scripts/install_ctf_tools.sh +603 -0
- package/scripts/parallel_triage.py +395 -0
- package/scripts/prompt_policy_sanitizer.py +283 -0
- package/scripts/scope_guard.py +444 -0
- package/scripts/skill_validator.py +504 -0
- package/scripts/workspace_cleaner.py +141 -0
- package/skills/ctf-ai-ml/SKILL.md +120 -0
- package/skills/ctf-ai-ml/adversarial-ml.md +623 -0
- package/skills/ctf-ai-ml/llm-attacks.md +487 -0
- package/skills/ctf-ai-ml/model-attacks.md +422 -0
- package/skills/ctf-crypto/SKILL.md +320 -0
- package/skills/ctf-crypto/advanced-math.md +798 -0
- package/skills/ctf-crypto/classic-ciphers.md +651 -0
- package/skills/ctf-crypto/ecc-attacks.md +347 -0
- package/skills/ctf-crypto/exotic-crypto-2.md +380 -0
- package/skills/ctf-crypto/exotic-crypto.md +528 -0
- package/skills/ctf-crypto/historical.md +113 -0
- package/skills/ctf-crypto/lattice-and-lwe.md +524 -0
- package/skills/ctf-crypto/modern-ciphers-2.md +563 -0
- package/skills/ctf-crypto/modern-ciphers-3.md +453 -0
- package/skills/ctf-crypto/modern-ciphers.md +649 -0
- package/skills/ctf-crypto/prng-attacks.md +257 -0
- package/skills/ctf-crypto/prng.md +664 -0
- package/skills/ctf-crypto/rsa-attacks-2.md +792 -0
- package/skills/ctf-crypto/rsa-attacks.md +487 -0
- package/skills/ctf-crypto/stream-ciphers.md +390 -0
- package/skills/ctf-crypto/zkp-and-advanced.md +456 -0
- package/skills/ctf-forensics/3d-printing.md +121 -0
- package/skills/ctf-forensics/SKILL.md +379 -0
- package/skills/ctf-forensics/disk-advanced.md +497 -0
- package/skills/ctf-forensics/disk-and-memory.md +491 -0
- package/skills/ctf-forensics/disk-recovery.md +699 -0
- package/skills/ctf-forensics/linux-forensics.md +511 -0
- package/skills/ctf-forensics/network-advanced.md +583 -0
- package/skills/ctf-forensics/network.md +645 -0
- package/skills/ctf-forensics/peripheral-capture.md +287 -0
- package/skills/ctf-forensics/signals-and-hardware.md +713 -0
- package/skills/ctf-forensics/steganography.md +694 -0
- package/skills/ctf-forensics/stego-advanced-2.md +475 -0
- package/skills/ctf-forensics/stego-advanced.md +481 -0
- package/skills/ctf-forensics/stego-image.md +691 -0
- package/skills/ctf-forensics/windows.md +625 -0
- package/skills/ctf-malware/SKILL.md +181 -0
- package/skills/ctf-malware/c2-and-protocols.md +274 -0
- package/skills/ctf-malware/pe-and-dotnet.md +108 -0
- package/skills/ctf-malware/scripts-and-obfuscation.md +449 -0
- package/skills/ctf-misc/SKILL.md +498 -0
- package/skills/ctf-misc/bashjails.md +323 -0
- package/skills/ctf-misc/ctfd-navigation.md +465 -0
- package/skills/ctf-misc/dns.md +255 -0
- package/skills/ctf-misc/encodings-advanced.md +504 -0
- package/skills/ctf-misc/encodings.md +431 -0
- package/skills/ctf-misc/games-and-vms-2.md +254 -0
- package/skills/ctf-misc/games-and-vms-3.md +690 -0
- package/skills/ctf-misc/games-and-vms-4.md +229 -0
- package/skills/ctf-misc/games-and-vms.md +529 -0
- package/skills/ctf-misc/linux-privesc.md +333 -0
- package/skills/ctf-misc/pyjails.md +671 -0
- package/skills/ctf-misc/rf-sdr.md +91 -0
- package/skills/ctf-osint/SKILL.md +198 -0
- package/skills/ctf-osint/geolocation-and-media.md +464 -0
- package/skills/ctf-osint/social-media.md +312 -0
- package/skills/ctf-osint/web-and-dns.md +341 -0
- package/skills/ctf-pwn/SKILL.md +214 -0
- package/skills/ctf-pwn/advanced-exploits-2.md +579 -0
- package/skills/ctf-pwn/advanced-exploits-3.md +598 -0
- package/skills/ctf-pwn/advanced-exploits-4.md +590 -0
- package/skills/ctf-pwn/advanced-exploits-5.md +119 -0
- package/skills/ctf-pwn/advanced-exploits.md +773 -0
- package/skills/ctf-pwn/advanced.md +326 -0
- package/skills/ctf-pwn/field-notes.md +245 -0
- package/skills/ctf-pwn/format-string.md +694 -0
- package/skills/ctf-pwn/heap-fsop.md +285 -0
- package/skills/ctf-pwn/heap-techniques-2.md +333 -0
- package/skills/ctf-pwn/heap-techniques.md +513 -0
- package/skills/ctf-pwn/kernel-bypass.md +421 -0
- package/skills/ctf-pwn/kernel-techniques.md +366 -0
- package/skills/ctf-pwn/kernel.md +636 -0
- package/skills/ctf-pwn/overflow-basics.md +611 -0
- package/skills/ctf-pwn/rop-advanced.md +725 -0
- package/skills/ctf-pwn/rop-and-shellcode.md +659 -0
- package/skills/ctf-pwn/sandbox-escape.md +313 -0
- package/skills/ctf-reverse/SKILL.md +163 -0
- package/skills/ctf-reverse/anti-analysis-ctf.md +204 -0
- package/skills/ctf-reverse/anti-analysis.md +693 -0
- package/skills/ctf-reverse/field-notes.md +376 -0
- package/skills/ctf-reverse/languages-compiled.md +666 -0
- package/skills/ctf-reverse/languages-platforms.md +592 -0
- package/skills/ctf-reverse/languages.md +553 -0
- package/skills/ctf-reverse/patterns-ctf-2.md +397 -0
- package/skills/ctf-reverse/patterns-ctf-3.md +797 -0
- package/skills/ctf-reverse/patterns-ctf.md +670 -0
- package/skills/ctf-reverse/patterns-runtime.md +274 -0
- package/skills/ctf-reverse/patterns.md +572 -0
- package/skills/ctf-reverse/platforms-hardware.md +387 -0
- package/skills/ctf-reverse/platforms.md +664 -0
- package/skills/ctf-reverse/tools-advanced-2.md +421 -0
- package/skills/ctf-reverse/tools-advanced.md +407 -0
- package/skills/ctf-reverse/tools-dynamic.md +679 -0
- package/skills/ctf-reverse/tools-emulation.md +319 -0
- package/skills/ctf-reverse/tools.md +573 -0
- package/skills/ctf-web/SKILL.md +153 -0
- package/skills/ctf-web/auth-and-access-2.md +82 -0
- package/skills/ctf-web/auth-and-access.md +783 -0
- package/skills/ctf-web/auth-infra.md +321 -0
- package/skills/ctf-web/auth-jwt.md +186 -0
- package/skills/ctf-web/client-side-advanced.md +739 -0
- package/skills/ctf-web/client-side.md +529 -0
- package/skills/ctf-web/cves.md +373 -0
- package/skills/ctf-web/field-notes.md +482 -0
- package/skills/ctf-web/node-and-prototype.md +200 -0
- package/skills/ctf-web/server-side-2.md +337 -0
- package/skills/ctf-web/server-side-advanced-2.md +559 -0
- package/skills/ctf-web/server-side-advanced-3.md +125 -0
- package/skills/ctf-web/server-side-advanced-4.md +480 -0
- package/skills/ctf-web/server-side-advanced.md +378 -0
- package/skills/ctf-web/server-side-deser.md +443 -0
- package/skills/ctf-web/server-side-exec-2.md +799 -0
- package/skills/ctf-web/server-side-exec.md +457 -0
- package/skills/ctf-web/server-side.md +629 -0
- package/skills/ctf-web/sql-injection.md +790 -0
- package/skills/ctf-web/web3.md +374 -0
- package/skills/ctf-writeup/SKILL.md +90 -0
- package/skills/solve-challenge/SKILL.md +269 -0
- package/skills.json +16 -0
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# CTF Crypto - Modern Cipher Attacks (Part 3)
|
|
2
|
+
|
|
3
|
+
Custom hash reversal, CRC brute-force, noisy RSA oracles, sponge collisions, CBC/padding oracle tricks, SPN recovery, AES-CFB, three-round XOR, Unicode side channels, SHA-256 basis attacks, MAC forgery, HMAC bit oracles. For Blum-Goldwasser, hash length extension, compression oracles, OFB/HMAC-CRC/DES weak keys, SRP, square attack, AES-ECB/CBC oracles, Rabin, PBKDF2, and MD5 multi-collision, see [modern-ciphers-2.md](modern-ciphers-2.md).
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
- [Custom Hash State Reversal via Known Intermediates (BackdoorCTF 2016)](#custom-hash-state-reversal-via-known-intermediates-backdoorctf-2016)
|
|
7
|
+
- [CRC32 Brute-Force for Small Payloads (BackdoorCTF 2016)](#crc32-brute-force-for-small-payloads-backdoorctf-2016)
|
|
8
|
+
- [Noisy RSA LSB Oracle with Post-Hoc Error Correction (SharifCTF 7 2016)](#noisy-rsa-lsb-oracle-with-post-hoc-error-correction-sharifctf-7-2016)
|
|
9
|
+
- [Sponge Hash Collision via Meet-in-the-Middle on Partial State (BKP 2017)](#sponge-hash-collision-via-meet-in-the-middle-on-partial-state-bkp-2017)
|
|
10
|
+
- [CBC IV Forgery + Block Truncation for Authentication Bypass (0CTF 2017)](#cbc-iv-forgery--block-truncation-for-authentication-bypass-0ctf-2017)
|
|
11
|
+
- [Padding Oracle to CBC Bitflip Command Injection (BSidesSF 2017)](#padding-oracle-to-cbc-bitflip-command-injection-bsidessf-2017)
|
|
12
|
+
- [SPN Cipher Partial Key Recovery via S-box Intersection (SharifCTF 7 2016)](#spn-cipher-partial-key-recovery-via-s-box-intersection-sharifctf-7-2016)
|
|
13
|
+
- [AES-CFB IV Recovery from Timestamp-Seeded PRNG (SHA2017)](#aes-cfb-iv-recovery-from-timestamp-seeded-prng-sha2017)
|
|
14
|
+
- [Three-Round XOR Protocol Key Cancellation (HITB 2017)](#three-round-xor-protocol-key-cancellation-hitb-2017)
|
|
15
|
+
- [AES-CBC UnicodeDecodeError Side-Channel Oracle (Kaspersky 2017)](#aes-cbc-unicodedecodeerror-side-channel-oracle-kaspersky-2017)
|
|
16
|
+
- [SHA-256 Basis Attack for XOR-Aggregate Hash Bypass (34C3 CTF 2017)](#sha-256-basis-attack-for-xor-aggregate-hash-bypass-34c3-ctf-2017)
|
|
17
|
+
- [Custom MAC Forgery via XOR Block Cancellation with Key Rotation (PlaidCTF 2018)](#custom-mac-forgery-via-xor-block-cancellation-with-key-rotation-plaidctf-2018)
|
|
18
|
+
- [Bit-by-Bit HMAC Key Recovery via XOR Plus Addition Arithmetic (Midnight Sun CTF 2018)](#bit-by-bit-hmac-key-recovery-via-xor-plus-addition-arithmetic-midnight-sun-ctf-2018)
|
|
19
|
+
- [CBC IV Recovery from Block-2 Known Plaintext (RITSEC 2018)](#cbc-iv-recovery-from-block-2-known-plaintext-ritsec-2018)
|
|
20
|
+
- [Iterated SHA-256 Timing Oracle on Character Match (35C3 2018)](#iterated-sha-256-timing-oracle-on-character-match-35c3-2018)
|
|
21
|
+
- [GF(p) Linear-System AES Key Recovery from PCAP Matrix (35C3 Junior 2018)](#gfp-linear-system-aes-key-recovery-from-pcap-matrix-35c3-junior-2018)
|
|
22
|
+
- [SHA-1 Length Extension with UTF-8 High-Byte Bypass (OTW Advent 2018)](#sha-1-length-extension-with-utf-8-high-byte-bypass-otw-advent-2018)
|
|
23
|
+
- [Cross-Session Cube-Root Recovery via CRT (X-MAS 2018)](#cross-session-cube-root-recovery-via-crt-x-mas-2018)
|
|
24
|
+
- [CBC Previous-Block Byte Flipping for Cookie Privilege Escalation (picoCTF 2018)](#cbc-previous-block-byte-flipping-for-cookie-privilege-escalation-picoctf-2018)
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Custom Hash State Reversal via Known Intermediates (BackdoorCTF 2016)
|
|
29
|
+
|
|
30
|
+
**Pattern (Collision Course):** Custom hash processes 4-byte blocks, updating state with XOR and rotations. If intermediate states are printed, reverse each block's hash by computing `hash(block) = s(i) XOR ROL(s(i+1), 7)`. Then brute-force 4-byte printable inputs matching each hash value.
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
def reverse_hash_states(states):
|
|
34
|
+
"""Given intermediate hash states, recover per-block hash values."""
|
|
35
|
+
blocks = []
|
|
36
|
+
for i in range(len(states) - 1):
|
|
37
|
+
# state_update: s(i+1) = ROR(s(i) ^ hash(block), 7)
|
|
38
|
+
# Therefore: hash(block) = s(i) ^ ROL(s(i+1), 7)
|
|
39
|
+
h = states[i] ^ rol32(states[i+1], 7)
|
|
40
|
+
blocks.append(h)
|
|
41
|
+
return blocks
|
|
42
|
+
|
|
43
|
+
def rol32(val, n):
|
|
44
|
+
return ((val << n) | (val >> (32 - n))) & 0xFFFFFFFF
|
|
45
|
+
|
|
46
|
+
# Brute-force printable 4-byte blocks matching each hash
|
|
47
|
+
import itertools, string
|
|
48
|
+
for target_hash in block_hashes:
|
|
49
|
+
for chars in itertools.product(string.printable, repeat=4):
|
|
50
|
+
block = bytes(ord(c) for c in chars)
|
|
51
|
+
if custom_hash(block) == target_hash:
|
|
52
|
+
print(f"Found: {block}")
|
|
53
|
+
break
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Key insight:** When a custom hash function leaks intermediate states (after each block), each block becomes an independent 4-byte brute-force problem (~2^32 worst case, reduced to ~10^8 for printable ASCII). Inverting the state update equation isolates per-block targets. This pattern appears whenever iterative hashes expose partial state.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## CRC32 Brute-Force for Small Payloads (BackdoorCTF 2016)
|
|
61
|
+
|
|
62
|
+
**Pattern (CRC):** Encrypted ZIP files store CRC32 of uncompressed contents. For very small files (5 bytes), brute-force all printable 5-character strings, compute CRC32, and match against the stored value. Multiple matches are common but context resolves ambiguity.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import binascii, itertools, string, zipfile
|
|
66
|
+
|
|
67
|
+
# Extract CRC from ZIP without decrypting
|
|
68
|
+
with zipfile.ZipFile('encrypted.zip') as z:
|
|
69
|
+
crc = z.infolist()[0].CRC
|
|
70
|
+
|
|
71
|
+
# Brute-force 5-byte printable content
|
|
72
|
+
for chars in itertools.product(string.printable[:95], repeat=5):
|
|
73
|
+
candidate = ''.join(chars).encode()
|
|
74
|
+
if binascii.crc32(candidate) & 0xFFFFFFFF == crc:
|
|
75
|
+
print(f"Match: {candidate}")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Key insight:** CRC32 stored in ZIP headers is not encrypted — it's always accessible even for password-protected ZIPs. For small files (≤ 6 bytes of printable ASCII), the search space is feasible. A C implementation is ~100x faster than Python. Multiple CRC collisions are expected for 5+ byte payloads; combine with language analysis or cross-reference multiple encrypted files to disambiguate.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Noisy RSA LSB Oracle with Post-Hoc Error Correction (SharifCTF 7 2016)
|
|
83
|
+
|
|
84
|
+
**Pattern:** Extension of the RSA LSB oracle binary search when the oracle occasionally returns incorrect results. Run the standard LSB oracle attack, then inspect decoded bytes. Non-ASCII or unexpected charset values indicate an oracle error within the last ~8 bits. Try single bit-flips at nearby oracle positions; the correct flip fixes the entire remaining decryption.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
def lsb_oracle_attack(ciphertext, e, n, oracle_fn, flips=None):
|
|
88
|
+
"""Recover plaintext from RSA LSB oracle, with optional error correction."""
|
|
89
|
+
flips = flips or []
|
|
90
|
+
lower, upper = 0, n
|
|
91
|
+
mult = 1
|
|
92
|
+
for i in range(n.bit_length()):
|
|
93
|
+
ciphertext = (ciphertext * pow(2, e, n)) % n
|
|
94
|
+
result = oracle_fn(ciphertext)
|
|
95
|
+
if i in flips:
|
|
96
|
+
result = not result # correct known oracle error
|
|
97
|
+
mid = (lower + upper) // 2
|
|
98
|
+
if result == 0:
|
|
99
|
+
upper = mid
|
|
100
|
+
else:
|
|
101
|
+
lower = mid
|
|
102
|
+
return lower
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Key insight:** Sparse oracle errors produce localized corruption in the recovered plaintext. By inspecting character validity (e.g., expecting hex digits), the error position can be identified and corrected by flipping the oracle result at that query index.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Sponge Hash Collision via Meet-in-the-Middle on Partial State (BKP 2017)
|
|
110
|
+
|
|
111
|
+
**Pattern:** A custom sponge hash uses AES with a known key, XORing 10-byte message blocks into a 16-byte state. Since only 10 of 16 state bytes are controllable per block, a direct preimage requires ~2^48 work. Meet-in-the-middle reduces this: precompute 2^24 forward AES encryptions keyed on their last 6 bytes, then search backward decryptions for matches in those 6 bytes.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from Crypto.Cipher import AES
|
|
115
|
+
import os
|
|
116
|
+
|
|
117
|
+
aes = AES.new(b'\x00' * 16, AES.MODE_ECB)
|
|
118
|
+
forward = {}
|
|
119
|
+
|
|
120
|
+
# Forward: compute AES(random_10_bytes || 0x00*6), key on last 6 bytes
|
|
121
|
+
for _ in range(2**24):
|
|
122
|
+
block = os.urandom(10) + b'\x00' * 6
|
|
123
|
+
enc = aes.encrypt(block)
|
|
124
|
+
forward[enc[-6:]] = block
|
|
125
|
+
|
|
126
|
+
# Backward: compute AES_dec(target XOR random_c), check last 6 bytes
|
|
127
|
+
target_state = b'\x77\x40\x56\x0a\x1d\x64' # target hash
|
|
128
|
+
for _ in range(2**40):
|
|
129
|
+
c_block = os.urandom(10) + target_state
|
|
130
|
+
dec = aes.decrypt(c_block)
|
|
131
|
+
if dec[-6:] in forward:
|
|
132
|
+
a_block = forward[dec[-6:]]
|
|
133
|
+
b_block = xor(aes.encrypt(a_block), dec) # middle block
|
|
134
|
+
break
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Key insight:** When a sponge rate is smaller than the state size, the uncontrolled bytes create a meet-in-the-middle opportunity. Precompute one direction, search the other — reducing 2^48 to 2^24 space + 2^24 time.
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## CBC IV Forgery + Block Truncation for Authentication Bypass (0CTF 2017)
|
|
142
|
+
|
|
143
|
+
**Pattern:** Service encrypts `MD5(padded_name) || padded_name` with AES-CBC. The MD5 serves as an integrity check on login. Two attacks combine: (1) IV manipulation: XOR IV bytes to change the decrypted first block from the source MD5 to the target MD5. (2) Block truncation: register with `pad("admin") + 16_junk_bytes`, then strip trailing ciphertext blocks — AES-CBC has no length field, so shorter ciphertext decrypts validly if PKCS7 padding is correct.
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
# Forge IV to flip MD5 from registered user to "admin"
|
|
147
|
+
source_md5 = md5(pad("admin") + b"A"*16)
|
|
148
|
+
target_md5 = md5(pad("admin"))
|
|
149
|
+
new_iv = bytes(a ^ b ^ c for a, b, c in zip(original_iv, source_md5, target_md5))
|
|
150
|
+
|
|
151
|
+
# Strip last 2 blocks (junk + PKCS padding block)
|
|
152
|
+
forged_token = new_iv + ciphertext[16:-32]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Key insight:** AES-CBC decryption has no built-in length integrity. Truncating ciphertext blocks from the end is valid as long as the new last block decrypts to valid PKCS7 padding. Combined with IV manipulation of block 0, this forges arbitrary first-block content.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Padding Oracle to CBC Bitflip Command Injection (BSidesSF 2017)
|
|
160
|
+
|
|
161
|
+
**Pattern:** Encrypted commands passed via URL parameter. Error messages reveal padding validity (padding oracle). Chain two attacks: (1) Padding oracle recovers the plaintext of the encrypted command. (2) CBC bitflipping modifies a ciphertext block to inject shell metacharacters (`;$(cmd)`) into the decrypted command, achieving RCE through crypto manipulation alone.
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
# Step 1: Padding oracle recovers plaintext
|
|
165
|
+
plaintext = padding_oracle_decrypt(ciphertext, oracle_fn)
|
|
166
|
+
|
|
167
|
+
# Step 2: CBC bitflip — modify block N-1 to change decrypted block N
|
|
168
|
+
target_block = 5
|
|
169
|
+
desired = b';$(cat *.txt) ' # 16 bytes, pad with spaces
|
|
170
|
+
original = plaintext[target_block * 16:(target_block + 1) * 16]
|
|
171
|
+
ct = bytearray(bytes.fromhex(ciphertext))
|
|
172
|
+
for i in range(16):
|
|
173
|
+
ct[(target_block - 1) * 16 + i] ^= original[i] ^ desired[i]
|
|
174
|
+
forged = ct.hex()
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**Key insight:** Padding oracle and CBC bitflipping are usually taught separately. Chaining them converts a pure cryptographic weakness into full command injection: the oracle recovers plaintext needed to compute the XOR mask, and the bitflip injects the payload.
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## SPN Cipher Partial Key Recovery via S-box Intersection (SharifCTF 7 2016)
|
|
182
|
+
|
|
183
|
+
**Pattern:** A 3-round substitution-permutation network with 36-bit blocks and 6-bit S-boxes. Attack using chosen-plaintext pairs: for each pair of 6-bit sub-keys (rounds 2 and 3), partially decrypt through the last two rounds and check if the intermediate S-box input matches. Intersecting candidate key sets across ~200 plaintext-ciphertext pairs uniquely identifies each 6-bit sub-key, reducing a 108-bit brute force to six independent 12-bit searches.
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
def recover_subkeys(pairs, sbox, perm):
|
|
187
|
+
"""Recover 6-bit subkeys via intersection across plaintext-ciphertext pairs."""
|
|
188
|
+
for sbox_pos in range(6): # 6 S-boxes per round
|
|
189
|
+
candidates = None
|
|
190
|
+
for pt, ct in pairs:
|
|
191
|
+
valid = set()
|
|
192
|
+
for k2 in range(64): # 6-bit subkey round 2
|
|
193
|
+
for k3 in range(64): # 6-bit subkey round 3
|
|
194
|
+
# Partial decrypt through rounds 3 and 2
|
|
195
|
+
intermediate = inv_sbox[ct_bits[sbox_pos] ^ k3]
|
|
196
|
+
intermediate = inv_perm(intermediate)
|
|
197
|
+
if inv_sbox[intermediate ^ k2] == expected_from_pt:
|
|
198
|
+
valid.add((k2, k3))
|
|
199
|
+
candidates = valid if candidates is None else candidates & valid
|
|
200
|
+
assert len(candidates) == 1 # unique key pair
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
**Key insight:** SPN structures allow divide-and-conquer key recovery. Each S-box position can be attacked independently, and the intersection of valid key candidates across multiple plaintext-ciphertext pairs converges to a unique solution.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## AES-CFB IV Recovery from Timestamp-Seeded PRNG (SHA2017)
|
|
208
|
+
|
|
209
|
+
**Pattern:** Ransomware encrypts files with AES-CFB using a hardcoded password from bash_history. The IV is derived from `random.choice()` seeded with `int(time())` at encryption time. The file's mtime (preserved by the filesystem) equals the exact seed used, enabling full decryption without the private key.
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
import random, os, string, base64
|
|
213
|
+
from Crypto.Cipher import AES
|
|
214
|
+
|
|
215
|
+
password = b'hardcoded_password_from_bash_history'
|
|
216
|
+
img = 'encrypted_file.enc'
|
|
217
|
+
|
|
218
|
+
# File mtime IS the random seed used at encryption time
|
|
219
|
+
random.seed(int(os.stat(img).st_mtime))
|
|
220
|
+
iv = ''.join(random.choice(string.letters + string.digits) for _ in range(16))
|
|
221
|
+
|
|
222
|
+
aes = AES.new(password, AES.MODE_CFB, iv.encode())
|
|
223
|
+
with open(img, 'rb') as f:
|
|
224
|
+
ciphertext = base64.b64decode(f.read())
|
|
225
|
+
plaintext = aes.decrypt(ciphertext)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**Key insight:** PRNG seeded with `time()` at encryption time leaks the seed via the filesystem mtime. Always check Python version compatibility — Python 2 and Python 3 have different `random` module implementations producing different sequences from the same seed. The `-it` flag on `cp`/`mv` may reset mtime; work from the original unmodified file.
|
|
229
|
+
|
|
230
|
+
**References:** SHA2017
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Three-Round XOR Protocol Key Cancellation (HITB 2017)
|
|
235
|
+
|
|
236
|
+
**Pattern:** A custom protocol performs a three-message XOR key exchange:
|
|
237
|
+
1. Client sends `c1 = msg XOR clientKey`
|
|
238
|
+
2. Server responds `c2 = c1 XOR serverKey`
|
|
239
|
+
3. Client sends `c3 = c2 XOR clientKey`
|
|
240
|
+
|
|
241
|
+
All three ciphertexts are observable in a PCAP or network capture. Computing `c1 XOR c2 XOR c3` directly recovers the original `msg` because all key material cancels:
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
# c1 = msg ^ clientKey
|
|
245
|
+
# c2 = msg ^ clientKey ^ serverKey
|
|
246
|
+
# c3 = msg ^ serverKey
|
|
247
|
+
# c1 ^ c2 ^ c3 = msg ^ clientKey ^ msg ^ clientKey ^ serverKey ^ msg ^ serverKey
|
|
248
|
+
# = msg (all keys cancel via XOR)
|
|
249
|
+
plaintext = bytes(a ^ b ^ c for a, b, c in zip(c1, c2, c3))
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
**Key insight:** Three-message XOR key exchange where the client applies its key twice creates an algebraic weakness: XOR of all three ciphertexts directly recovers the original message without knowledge of either key. Any protocol where the same key is applied an even number of times is trivially broken.
|
|
253
|
+
|
|
254
|
+
**References:** HITB 2017
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## AES-CBC UnicodeDecodeError Side-Channel Oracle (Kaspersky 2017)
|
|
259
|
+
|
|
260
|
+
**Pattern:** Server decrypts AES-CBC ciphertext and attempts to UTF-8 decode the result. Invalid UTF-8 sequences raise a `UnicodeDecodeError` (or equivalent). This error is distinguishable from other errors (e.g., application-level errors), creating a decryption oracle analogous to a padding oracle.
|
|
261
|
+
|
|
262
|
+
**Attack:** Standard CBC bit-flip oracle technique, using UTF-8 validity as the distinguisher:
|
|
263
|
+
1. For each target plaintext byte at position `i` in block `b`, modify byte `i` in block `b-1`
|
|
264
|
+
2. Cycle through all 256 XOR values; when the decrypted byte produces valid UTF-8 in context, the server returns a non-`UnicodeDecodeError` response
|
|
265
|
+
3. From the XOR value that passes and the known modification to `c[b-1][i]`, recover `plaintext[b][i]`
|
|
266
|
+
|
|
267
|
+
```python
|
|
268
|
+
# CBC bit-flip oracle using UTF-8 validity
|
|
269
|
+
for guess in range(256):
|
|
270
|
+
modified = bytearray(prev_block)
|
|
271
|
+
modified[pos] = known_intermediate[pos] ^ guess # produce desired output byte
|
|
272
|
+
if not unicode_error(modified_block + target_block):
|
|
273
|
+
plaintext_byte = guess # valid UTF-8 at this position
|
|
274
|
+
break
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
**Key insight:** Any error that distinguishes valid from invalid plaintext content serves as a decryption oracle — not just PKCS#7 padding errors. UTF-8 validity, base64 decodability, JSON parsability, and ASCII-only constraints are all valid oracle conditions. The only requirement is a server-side distinguishable response.
|
|
278
|
+
|
|
279
|
+
**References:** Kaspersky CTF 2017
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## SHA-256 Basis Attack for XOR-Aggregate Hash Bypass (34C3 CTF 2017)
|
|
284
|
+
|
|
285
|
+
**Pattern:** Find 256 files whose SHA-256 hashes form a basis for Z_2^256. Then for any target hash, compute which subset of basis files XORs to produce the desired hash difference. This breaks systems that verify integrity via `XOR(sha256(file_i)) == expected`.
|
|
286
|
+
|
|
287
|
+
```python
|
|
288
|
+
# 1. Generate ~300 random valid Python files
|
|
289
|
+
# 2. Compute SHA-256 of each -> 256-bit vectors over GF(2)
|
|
290
|
+
# 3. Gaussian elimination to find 256 linearly independent vectors
|
|
291
|
+
# 4. Target: h_new XOR (XOR of sha256(basis_files)) = h_orig
|
|
292
|
+
# 5. Solve the linear system to find which basis files to include
|
|
293
|
+
from sage.all import GF, matrix
|
|
294
|
+
M = matrix(GF(2), [hash_to_bits(sha256(f)) for f in basis_files])
|
|
295
|
+
target = hash_to_bits(sha256(malicious_zip)) ^ hash_to_bits(original_hash)
|
|
296
|
+
solution = M.solve_left(target)
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
**Key insight:** SHA-256 hashes are 256-bit vectors over GF(2). Given ~256 random hashes, they almost certainly span the full space, meaning you can XOR-combine them to produce any target 256-bit value. This breaks XOR-based aggregate hash verification: if the system checks `XOR(sha256(file_i)) == expected`, you can replace files while maintaining the aggregate. The attack does NOT find SHA-256 collisions -- it exploits the linearity of XOR aggregation over non-linear hash outputs.
|
|
300
|
+
|
|
301
|
+
**References:** 34C3 CTF 2017
|
|
302
|
+
|
|
303
|
+
---
|
|
304
|
+
|
|
305
|
+
### Custom MAC Forgery via XOR Block Cancellation with Key Rotation (PlaidCTF 2018)
|
|
306
|
+
|
|
307
|
+
**Pattern:** Custom MAC uses AES-ECB with key stream that repeats every 128 blocks. Craft three queries where 2048-byte filler blocks cancel via XOR between queries, leaving only the target command's MAC. (PlaidCTF 2018)
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
mac1 = fmac("tag " + tag_cmd(cmdline)) # tag AAA...
|
|
311
|
+
mac2 = fmac("tag " + expand_cmd(cmdline)) # tag BBB...(2048) + cmd_padded
|
|
312
|
+
mac3 = fmac("tag " + expand_cmd(tag_cmd(cmdline))) # tag BBB...(2048) + tagAAA_padded
|
|
313
|
+
forged_mac = mac1 ^ mac2 ^ mac3 # XOR cancellation = fmac(cmdline)
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
**Key insight:** When a MAC's internal key stream repeats periodically, arrange message blocks so that identical blocks at the same key-stream positions cancel via XOR across multiple queries. Three queries suffice to forge any target command's MAC.
|
|
317
|
+
|
|
318
|
+
---
|
|
319
|
+
|
|
320
|
+
### Bit-by-Bit HMAC Key Recovery via XOR Plus Addition Arithmetic (Midnight Sun CTF 2018)
|
|
321
|
+
|
|
322
|
+
**Pattern:** Flawed HMAC computes `sha256((key XOR msg) + msg)` where `+` is bitwise addition (not concatenation). Sending `msg=0` gives `sha256(key)`. For bit position `i`, sending `msg=2^i`: if key bit `i` is set, XOR clears it and addition restores it, giving the same hash. (Midnight Sun CTF 2018)
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
key_hash = get_digest(b'\x00') # sha256(key + 0) = sha256(key)
|
|
326
|
+
key = 0
|
|
327
|
+
for i in range(key_bits):
|
|
328
|
+
digest = get_digest(int_to_bytes(2**i))
|
|
329
|
+
if digest == key_hash:
|
|
330
|
+
key |= (1 << i) # bit i is set in key
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
**Key insight:** When XOR and addition interact, setting bit `i` in the message XORs it away from the key but adds it back. If key bit `i` was already set, `XOR(1,1)=0` and `0+1=1`, restoring the original value. If key bit `i` was 0, `XOR(0,1)=1` and `1+1=0` with carry, changing the hash. This creates a per-bit oracle.
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
### CBC IV Recovery from Block-2 Known Plaintext (RITSEC 2018)
|
|
338
|
+
|
|
339
|
+
**Pattern:** AES-CBC given: full ciphertext, known plaintext from block 2 onward, partial key. Recover the missing IV by first brute-forcing missing key bytes via block 2 (which does not depend on the IV), then XOR plaintext[0] with `AES_decrypt(ct[0], K)` to get the IV.
|
|
340
|
+
|
|
341
|
+
```python
|
|
342
|
+
for tail in itertools.product(string.printable, repeat=2):
|
|
343
|
+
K = base_key + ''.join(tail).encode()
|
|
344
|
+
if AES.new(K, AES.MODE_ECB).decrypt(ct)[16:32] == plaintext[16:32]:
|
|
345
|
+
raw = AES.new(K, AES.MODE_ECB).decrypt(ct[:16])
|
|
346
|
+
IV = bytes(a ^ b for a, b in zip(raw, plaintext[:16]))
|
|
347
|
+
break
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
**Key insight:** Block 2 of CBC decrypts with `prev_ct XOR raw_decrypt` where `prev_ct` is from the ciphertext itself — IV-independent. Use it to recover the key first, then XOR back to the IV.
|
|
351
|
+
|
|
352
|
+
**References:** RITSEC CTF 2018 — Who drew on my program, writeup 12269
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
### Iterated SHA-256 Timing Oracle on Character Match (35C3 2018)
|
|
357
|
+
|
|
358
|
+
**Pattern:** Server validates password character-by-character, and each correct character triggers an additional `sha256` iterated 9999 times. Correct characters therefore make the server respond ~0.66 s slower. Brute-force each position by timing responses.
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
for ch in string.printable:
|
|
362
|
+
t = time.time()
|
|
363
|
+
send(prefix + ch)
|
|
364
|
+
dt = time.time() - t
|
|
365
|
+
if dt > baseline + 0.3:
|
|
366
|
+
prefix += ch; break
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
**Key insight:** Any early-exit or variable-work validator using heavy hashing leaks position-by-position through total wall-time. Measure baseline vs. correct-char time, not absolute times.
|
|
370
|
+
|
|
371
|
+
**References:** 35C3 CTF 2018 — ultra secret, writeup 12820
|
|
372
|
+
|
|
373
|
+
---
|
|
374
|
+
|
|
375
|
+
### GF(p) Linear-System AES Key Recovery from PCAP Matrix (35C3 Junior 2018)
|
|
376
|
+
|
|
377
|
+
**Pattern:** Service sends 40 plaintext/ciphertext pairs over the network. Extract from pcap with tshark, build a 40×40 matrix `A` and vector `b` over `GF(p)`, then solve for the unknown AES round-key bytes.
|
|
378
|
+
|
|
379
|
+
```python
|
|
380
|
+
from sage.all import matrix, GF
|
|
381
|
+
A = matrix(GF(p), 40, A_rows)
|
|
382
|
+
key = A.solve_right(vector(GF(p), b))
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Use `tshark -r file.pcap -Y 'data.len>0' -T fields -e data` to dump the packet bytes, parse into rows, feed to Sage.
|
|
386
|
+
|
|
387
|
+
**Key insight:** Any protocol that reveals multiple "key applied to known input" samples collapses to linear algebra when the transformation is linear (or linear in a subfield). Sage's `solve_right` handles the rest.
|
|
388
|
+
|
|
389
|
+
**References:** 35C3 Junior CTF 2018 — pretty-linear, writeups 12788, 12789
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
### SHA-1 Length Extension with UTF-8 High-Byte Bypass (OTW Advent 2018)
|
|
394
|
+
|
|
395
|
+
**Pattern:** Server checks that all appended bytes to a length-extendable SHA-1 MAC are `< 0x80`. Standard `hashpumpy`/`hlextend` output contains `0x80` and padding bytes that fail the check. Rewrite the padding region using valid multi-byte UTF-8 sequences (e.g., `\xc2\x80` → U+0080) that survive the filter but SHA-1 treats identically.
|
|
396
|
+
|
|
397
|
+
```python
|
|
398
|
+
import hlextend
|
|
399
|
+
h = hlextend.new('sha1')
|
|
400
|
+
forged = h.extend(b';cat flag', b'A'*msg_len, key_len, old_mac)
|
|
401
|
+
# Replace any 0x80-0xFF bytes with UTF-8 two-byte equivalents
|
|
402
|
+
safe = forged.replace(b'\x80', b'\xc2\x80')
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
**Key insight:** ASCII-only filters can be bypassed by substituting multi-byte Unicode sequences whose byte values stay below `0x80`. Any length-extension attack behind an ASCII validator is still exploitable with UTF-8 creativity.
|
|
406
|
+
|
|
407
|
+
**References:** OverTheWire Advent Bonanza 2018 — Day 16, writeup 12754
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
### Cross-Session Cube-Root Recovery via CRT (X-MAS 2018)
|
|
412
|
+
|
|
413
|
+
**Pattern:** Service exposes `m^3 mod N_i` across multiple sessions with different moduli but the same small plaintext. Because `m^3 < N_1 * N_2 * N_3` for small `m`, Chinese Remainder Theorem recovers `m^3` as an integer, then `iroot` gives `m`.
|
|
414
|
+
|
|
415
|
+
```python
|
|
416
|
+
from sympy.ntheory.modular import crt
|
|
417
|
+
from gmpy2 import iroot
|
|
418
|
+
m_cubed, _ = crt([N1, N2, N3], [c1, c2, c3])
|
|
419
|
+
m, exact = iroot(int(m_cubed), 3)
|
|
420
|
+
assert exact
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
**Key insight:** Håstad broadcast attack for `e = 3` generalises to any scenario where you see `m^e mod N_i` across enough moduli that `m^e < prod(N_i)`. CRT joins them; integer root extraction finishes.
|
|
424
|
+
|
|
425
|
+
**References:** X-MAS CTF 2018 — Santa's list 2.0, writeup 12659
|
|
426
|
+
|
|
427
|
+
---
|
|
428
|
+
|
|
429
|
+
## CBC Previous-Block Byte Flipping for Cookie Privilege Escalation (picoCTF 2018)
|
|
430
|
+
|
|
431
|
+
**Pattern (Secured Logon):** Server stores `{"username": "...", "admin": 0, ...}` encrypted with AES-CBC + base64, returned as a cookie. No MAC. To escalate from `admin: 0` to `admin: 1`, locate the byte offset of `'0'` inside some plaintext block `P_{n+1}`, then XOR the corresponding byte in the *previous* ciphertext block `C_n` with `ord('0') ^ ord('1')`. The attacker block `C_n` decrypts to garbage (which may break the preceding JSON field), but the targeted byte in `P_{n+1}` flips cleanly because `P_{n+1} = AES_dec(C_{n+1}) XOR C_n`.
|
|
432
|
+
|
|
433
|
+
```python
|
|
434
|
+
from base64 import b64encode, b64decode
|
|
435
|
+
|
|
436
|
+
cookie = b64decode(stolen_cookie) # IV || C1 || C2 || ... (or C0||... )
|
|
437
|
+
buf = bytearray(cookie)
|
|
438
|
+
|
|
439
|
+
# Block layout for plaintext {'username': '', 'admin': 0, 'password': ''}:
|
|
440
|
+
# block 1: "{'username': ''," <- will decrypt to garbage after flip
|
|
441
|
+
# block 2: " 'admin': 0, 'pa" <- target byte 10 (the '0')
|
|
442
|
+
# block 3: "ssword': ''}"
|
|
443
|
+
|
|
444
|
+
# To flip byte 10 of plaintext block 2, XOR byte 10 of ciphertext block 1.
|
|
445
|
+
# With IV-prefixed layout: index = 16 (IV) + 0*16 (C1) + 10 = 26
|
|
446
|
+
# (or 0*16 + 10 = 10 if there is no separate IV prepended)
|
|
447
|
+
offset = 10
|
|
448
|
+
buf[offset] ^= ord('0') ^ ord('1') # 0x30 ^ 0x31 = 0x01
|
|
449
|
+
|
|
450
|
+
forged_cookie = b64encode(bytes(buf)).decode()
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
**Key insight:** In AES-CBC, `P_{n+1} = AES_dec(C_{n+1}) XOR C_n`. Flipping byte `i` of `C_n` flips byte `i` of `P_{n+1}` with zero side effects on `P_{n+1}`, but turns `P_n` (which was `AES_dec(C_n) XOR C_{n-1}`) into pseudo-random garbage. Works whenever the server (a) uses CBC without integrity checks, (b) parses the JSON/cookie leniently enough to tolerate a corrupted earlier block (unknown-key field, ignored garbage, lenient JSON parser), and (c) exposes the block boundary offset of the target byte. Contrast with [AES-CBC IV Bit-Flip (Google CTF 2016)](modern-ciphers-2.md#aes-cbc-iv-bit-flip-authentication-bypass-google-ctf-2016), which targets block 0 by flipping the IV and leaves all later blocks intact.
|