ineedcodes 1.6.0 → 1.7.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 (218) hide show
  1. package/package.json +2 -1
  2. package/skills/401-403-bypass-techniques/SKILL.md +348 -0
  3. package/skills/active-directory-acl-abuse/BLOODHOUND_PATHS.md +281 -0
  4. package/skills/active-directory-acl-abuse/SKILL.md +295 -0
  5. package/skills/active-directory-certificate-services/ADCS_ESC_MATRIX.md +197 -0
  6. package/skills/active-directory-certificate-services/SKILL.md +303 -0
  7. package/skills/active-directory-kerberos-attacks/KERBEROS_ATTACK_CHAINS.md +226 -0
  8. package/skills/active-directory-kerberos-attacks/SKILL.md +311 -0
  9. package/skills/ai-ml-security/SKILL.md +425 -0
  10. package/skills/android-pentesting-tricks/FRIDA_SCRIPTS.md +394 -0
  11. package/skills/android-pentesting-tricks/SKILL.md +369 -0
  12. package/skills/anti-debugging-techniques/ANTI_DEBUG_MATRIX.md +117 -0
  13. package/skills/anti-debugging-techniques/SKILL.md +407 -0
  14. package/skills/api-auth-and-jwt-abuse/SKILL.md +76 -0
  15. package/skills/api-authorization-and-bola/SKILL.md +48 -0
  16. package/skills/api-recon-and-docs/SKILL.md +61 -0
  17. package/skills/api-sec/SKILL.md +49 -0
  18. package/skills/arbitrary-write-to-rce/SKILL.md +361 -0
  19. package/skills/auth-sec/SKILL.md +41 -0
  20. package/skills/authbypass-authentication-flaws/SKILL.md +441 -0
  21. package/skills/bba-ad-attacker/SKILL.md +420 -0
  22. package/skills/bba-api-security/SKILL.md +95 -0
  23. package/skills/bba-attack-planner/SKILL.md +186 -0
  24. package/skills/bba-binary-exploit/SKILL.md +65 -0
  25. package/skills/bba-bizlogic-hunter/SKILL.md +304 -0
  26. package/skills/bba-bug-bounty/SKILL.md +245 -0
  27. package/skills/bba-cicd-redteam/SKILL.md +515 -0
  28. package/skills/bba-cloud-security/SKILL.md +97 -0
  29. package/skills/bba-container-escape/SKILL.md +161 -0
  30. package/skills/bba-credential-tester/SKILL.md +346 -0
  31. package/skills/bba-crypto-analyst/SKILL.md +54 -0
  32. package/skills/bba-ctf-solver/SKILL.md +166 -0
  33. package/skills/bba-detection-engineer/SKILL.md +93 -0
  34. package/skills/bba-engagement-planner/SKILL.md +68 -0
  35. package/skills/bba-exploit-chainer/SKILL.md +280 -0
  36. package/skills/bba-exploit-guide/SKILL.md +96 -0
  37. package/skills/bba-forensics-analyst/SKILL.md +512 -0
  38. package/skills/bba-graphql-hunter/SKILL.md +145 -0
  39. package/skills/bba-hardware-hacker/SKILL.md +57 -0
  40. package/skills/bba-jwt-cracker/SKILL.md +133 -0
  41. package/skills/bba-llm-redteam/SKILL.md +149 -0
  42. package/skills/bba-malware-analyst/SKILL.md +305 -0
  43. package/skills/bba-mobile-pentester/SKILL.md +350 -0
  44. package/skills/bba-osint-collector/SKILL.md +645 -0
  45. package/skills/bba-payload-crafter/SKILL.md +347 -0
  46. package/skills/bba-phishing-operator/SKILL.md +516 -0
  47. package/skills/bba-poc-validator/SKILL.md +249 -0
  48. package/skills/bba-privesc-advisor/SKILL.md +98 -0
  49. package/skills/bba-purple-team/SKILL.md +56 -0
  50. package/skills/bba-recon-advisor/SKILL.md +215 -0
  51. package/skills/bba-red-team-operator/SKILL.md +61 -0
  52. package/skills/bba-report-generator/SKILL.md +145 -0
  53. package/skills/bba-reverse-engineer/SKILL.md +298 -0
  54. package/skills/bba-social-engineer/SKILL.md +341 -0
  55. package/skills/bba-ssrf-hunter/SKILL.md +120 -0
  56. package/skills/bba-stig-analyst/SKILL.md +108 -0
  57. package/skills/bba-subdomain-takeover/SKILL.md +141 -0
  58. package/skills/bba-swarm-orchestrator/SKILL.md +351 -0
  59. package/skills/bba-threat-modeler/SKILL.md +574 -0
  60. package/skills/bba-vuln-scanner/SKILL.md +305 -0
  61. package/skills/bba-web-hunter/SKILL.md +285 -0
  62. package/skills/bba-wireless-pentester/SKILL.md +539 -0
  63. package/skills/binary-protection-bypass/PROTECTION_BYPASS_MATRIX.md +168 -0
  64. package/skills/binary-protection-bypass/SKILL.md +295 -0
  65. package/skills/browser-exploitation-v8/SKILL.md +334 -0
  66. package/skills/browser-exploitation-v8/V8_EXPLOITATION_PATTERNS.md +288 -0
  67. package/skills/business-logic-vuln/SKILL.md +33 -0
  68. package/skills/business-logic-vulnerabilities/CHECKLIST.md +375 -0
  69. package/skills/business-logic-vulnerabilities/METHODOLOGY.md +337 -0
  70. package/skills/business-logic-vulnerabilities/SCENARIOS.md +1095 -0
  71. package/skills/business-logic-vulnerabilities/SKILL.md +711 -0
  72. package/skills/classical-cipher-analysis/SKILL.md +663 -0
  73. package/skills/clickjacking/SKILL.md +163 -0
  74. package/skills/cmdi-command-injection/SKILL.md +687 -0
  75. package/skills/code-obfuscation-deobfuscation/SKILL.md +391 -0
  76. package/skills/container-escape-techniques/DOCKER_ESCAPE_CHAINS.md +296 -0
  77. package/skills/container-escape-techniques/SKILL.md +339 -0
  78. package/skills/cors-cross-origin-misconfiguration/SCENARIOS.md +152 -0
  79. package/skills/cors-cross-origin-misconfiguration/SKILL.md +270 -0
  80. package/skills/crlf-injection/SKILL.md +179 -0
  81. package/skills/csp-bypass-advanced/SKILL.md +259 -0
  82. package/skills/csrf-cross-site-request-forgery/SKILL.md +526 -0
  83. package/skills/csv-formula-injection/SKILL.md +144 -0
  84. package/skills/dangling-markup-injection/SKILL.md +321 -0
  85. package/skills/defi-attack-patterns/SKILL.md +355 -0
  86. package/skills/dependency-confusion/SKILL.md +178 -0
  87. package/skills/deserialization-insecure/JAVA_GADGET_CHAINS.md +511 -0
  88. package/skills/deserialization-insecure/SKILL.md +725 -0
  89. package/skills/dns-rebinding-attacks/SKILL.md +337 -0
  90. package/skills/email-header-injection/SKILL.md +349 -0
  91. package/skills/expression-language-injection/SKILL.md +243 -0
  92. package/skills/file-access-vuln/SKILL.md +33 -0
  93. package/skills/format-string-exploitation/SKILL.md +312 -0
  94. package/skills/ghost-bits-cast-attack/PAYLOAD_COOKBOOK.md +448 -0
  95. package/skills/ghost-bits-cast-attack/SKILL.md +799 -0
  96. package/skills/graphql-and-hidden-parameters/SKILL.md +50 -0
  97. package/skills/hack/SKILL.md +162 -0
  98. package/skills/hash-attack-techniques/SKILL.md +491 -0
  99. package/skills/heap-exploitation/HOUSE_OF_TECHNIQUES.md +244 -0
  100. package/skills/heap-exploitation/IO_FILE_EXPLOITATION.md +256 -0
  101. package/skills/heap-exploitation/SKILL.md +217 -0
  102. package/skills/http-host-header-attacks/SKILL.md +323 -0
  103. package/skills/http-parameter-pollution/SKILL.md +208 -0
  104. package/skills/http2-specific-attacks/SKILL.md +350 -0
  105. package/skills/idor-broken-object-authorization/SKILL.md +336 -0
  106. package/skills/injection-checking/EXTRA_INJECTION_TYPES.md +450 -0
  107. package/skills/injection-checking/SKILL.md +50 -0
  108. package/skills/insecure-source-code-management/SKILL.md +161 -0
  109. package/skills/ios-pentesting-tricks/IOS_RUNTIME_TRICKS.md +411 -0
  110. package/skills/ios-pentesting-tricks/SKILL.md +418 -0
  111. package/skills/jndi-injection/SKILL.md +265 -0
  112. package/skills/jwt-oauth-token-attacks/SKILL.md +301 -0
  113. package/skills/kernel-exploitation/KERNEL_HEAP_TECHNIQUES.md +293 -0
  114. package/skills/kernel-exploitation/KERNEL_MITIGATION_BYPASS.md +224 -0
  115. package/skills/kernel-exploitation/SKILL.md +307 -0
  116. package/skills/kubernetes-pentesting/SKILL.md +348 -0
  117. package/skills/lattice-crypto-attacks/SKILL.md +497 -0
  118. package/skills/linux-lateral-movement/SKILL.md +348 -0
  119. package/skills/linux-privilege-escalation/KERNEL_EXPLOITS_CHECKLIST.md +260 -0
  120. package/skills/linux-privilege-escalation/SKILL.md +346 -0
  121. package/skills/linux-privilege-escalation/SUID_CAPABILITIES_TRICKS.md +305 -0
  122. package/skills/linux-security-bypass/SKILL.md +345 -0
  123. package/skills/llm-prompt-injection/JAILBREAK_PATTERNS.md +306 -0
  124. package/skills/llm-prompt-injection/SKILL.md +357 -0
  125. package/skills/macos-process-injection/DYLIB_XPC_TECHNIQUES.md +373 -0
  126. package/skills/macos-process-injection/SKILL.md +384 -0
  127. package/skills/macos-security-bypass/SKILL.md +337 -0
  128. package/skills/macos-security-bypass/TCC_BYPASS_MATRIX.md +200 -0
  129. package/skills/memory-forensics-volatility/SKILL.md +323 -0
  130. package/skills/memory-forensics-volatility/VOLATILITY_CHEATSHEET.md +225 -0
  131. package/skills/mobile-ssl-pinning-bypass/SKILL.md +531 -0
  132. package/skills/network-protocol-attacks/NAME_RESOLUTION_POISONING.md +226 -0
  133. package/skills/network-protocol-attacks/SKILL.md +330 -0
  134. package/skills/nosql-injection/SKILL.md +341 -0
  135. package/skills/ntlm-relay-coercion/COERCION_METHODS.md +253 -0
  136. package/skills/ntlm-relay-coercion/SKILL.md +267 -0
  137. package/skills/oauth-oidc-misconfiguration/SKILL.md +46 -0
  138. package/skills/open-redirect/SKILL.md +380 -0
  139. package/skills/path-traversal-lfi/SKILL.md +802 -0
  140. package/skills/prototype-pollution/SKILL.md +190 -0
  141. package/skills/prototype-pollution-advanced/KNOWN_GADGETS.md +253 -0
  142. package/skills/prototype-pollution-advanced/SKILL.md +338 -0
  143. package/skills/race-condition/SKILL.md +526 -0
  144. package/skills/recon-and-methodology/SKILL.md +389 -0
  145. package/skills/recon-for-sec/SKILL.md +29 -0
  146. package/skills/request-smuggling/H2_SMUGGLING_VARIANTS.md +498 -0
  147. package/skills/request-smuggling/SKILL.md +314 -0
  148. package/skills/reverse-shell-techniques/SHELL_CHEATSHEET.md +196 -0
  149. package/skills/reverse-shell-techniques/SKILL.md +290 -0
  150. package/skills/rsa-attack-techniques/RSA_ATTACK_CATALOG.md +432 -0
  151. package/skills/rsa-attack-techniques/SKILL.md +437 -0
  152. package/skills/saml-sso-assertion-attacks/SKILL.md +41 -0
  153. package/skills/sandbox-escape-techniques/PYTHON_SANDBOX_ESCAPE.md +349 -0
  154. package/skills/sandbox-escape-techniques/SECCOMP_BYPASS.md +332 -0
  155. package/skills/sandbox-escape-techniques/SKILL.md +250 -0
  156. package/skills/smart-contract-vulnerabilities/SKILL.md +314 -0
  157. package/skills/smart-contract-vulnerabilities/SOLIDITY_VULN_PATTERNS.md +460 -0
  158. package/skills/sqli-sql-injection/SCENARIOS.md +575 -0
  159. package/skills/sqli-sql-injection/SKILL.md +489 -0
  160. package/skills/sqli-sql-injection/SQLMAP_ADVANCED.md +642 -0
  161. package/skills/ssrf-server-side-request-forgery/SCENARIOS.md +226 -0
  162. package/skills/ssrf-server-side-request-forgery/SKILL.md +323 -0
  163. package/skills/ssrf-server-side-request-forgery/URL_PARSER_TRICKS.md +650 -0
  164. package/skills/ssti-server-side-template-injection/ENGINE_PAYLOADS.md +456 -0
  165. package/skills/ssti-server-side-template-injection/SCENARIOS.md +319 -0
  166. package/skills/ssti-server-side-template-injection/SKILL.md +344 -0
  167. package/skills/stack-overflow-and-rop/ROP_ADVANCED_TECHNIQUES.md +249 -0
  168. package/skills/stack-overflow-and-rop/SKILL.md +304 -0
  169. package/skills/steganography-techniques/SKILL.md +348 -0
  170. package/skills/steganography-techniques/STEGO_TOOLS_GUIDE.md +335 -0
  171. package/skills/subdomain-takeover/SKILL.md +247 -0
  172. package/skills/symbolic-execution-tools/ANGR_COOKBOOK.md +437 -0
  173. package/skills/symbolic-execution-tools/SKILL.md +419 -0
  174. package/skills/symmetric-cipher-attacks/BLOCK_CIPHER_ATTACKS.md +364 -0
  175. package/skills/symmetric-cipher-attacks/SKILL.md +462 -0
  176. package/skills/traffic-analysis-pcap/SKILL.md +341 -0
  177. package/skills/tunneling-and-pivoting/SKILL.md +343 -0
  178. package/skills/type-juggling/SKILL.md +291 -0
  179. package/skills/unauthorized-access-common-services/PORT_SERVICE_MATRIX.md +340 -0
  180. package/skills/unauthorized-access-common-services/SKILL.md +388 -0
  181. package/skills/upload-insecure-files/SCENARIOS.md +158 -0
  182. package/skills/upload-insecure-files/SKILL.md +543 -0
  183. package/skills/vm-and-bytecode-reverse/SKILL.md +450 -0
  184. package/skills/vr-cheatsheet-authentication_bypass/SKILL.md +184 -0
  185. package/skills/vr-cheatsheet-dangerous_functions/SKILL.md +200 -0
  186. package/skills/vr-cheatsheet-linux_commands/SKILL.md +40 -0
  187. package/skills/vr-cheatsheet-rce/SKILL.md +177 -0
  188. package/skills/vr-cheatsheet-reverse_engineering/SKILL.md +144 -0
  189. package/skills/vr-cheatsheet-sqli/SKILL.md +270 -0
  190. package/skills/vr-cheatsheet-ssrf/SKILL.md +173 -0
  191. package/skills/vr-cheatsheet-xss/SKILL.md +177 -0
  192. package/skills/vr-methodology-mindset_and_tips/SKILL.md +51 -0
  193. package/skills/vr-template-vulnerability_report_template/SKILL.md +59 -0
  194. package/skills/vr-writeup-CVE-2026-9136/SKILL.md +171 -0
  195. package/skills/vr-writeup-ZVE-2026-3795/SKILL.md +350 -0
  196. package/skills/vr-writeup-api_key_leak/SKILL.md +103 -0
  197. package/skills/waf-bypass-techniques/SKILL.md +290 -0
  198. package/skills/waf-bypass-techniques/WAF_PRODUCT_MATRIX.md +202 -0
  199. package/skills/web-cache-deception/CACHE_POISONING_TECHNIQUES.md +637 -0
  200. package/skills/web-cache-deception/SKILL.md +222 -0
  201. package/skills/websocket-security/SKILL.md +454 -0
  202. package/skills/windows-av-evasion/AMSI_BYPASS_TECHNIQUES.md +289 -0
  203. package/skills/windows-av-evasion/SKILL.md +342 -0
  204. package/skills/windows-lateral-movement/CREDENTIAL_DUMPING.md +308 -0
  205. package/skills/windows-lateral-movement/SKILL.md +335 -0
  206. package/skills/windows-privilege-escalation/SKILL.md +328 -0
  207. package/skills/windows-privilege-escalation/TOKEN_POTATO_TRICKS.md +216 -0
  208. package/skills/windows-privilege-escalation/UAC_BYPASS_METHODS.md +225 -0
  209. package/skills/xslt-injection/SKILL.md +281 -0
  210. package/skills/xss-cross-site-scripting/ADVANCED_XSS_TRICKS.md +276 -0
  211. package/skills/xss-cross-site-scripting/SCENARIOS.md +278 -0
  212. package/skills/xss-cross-site-scripting/SKILL.md +379 -0
  213. package/skills/xxe-xml-external-entity/SCENARIOS.md +112 -0
  214. package/skills/xxe-xml-external-entity/SKILL.md +554 -0
  215. package/src/agent.js +1 -1
  216. package/src/session.js +47 -11
  217. package/src/skills.js +30 -6
  218. package/src/ui.js +1 -1
@@ -0,0 +1,462 @@
1
+ ---
2
+ name: symmetric-cipher-attacks
3
+ description: >-
4
+ Symmetric cipher attack playbook. Use when exploiting block cipher mode
5
+ weaknesses (CBC padding oracle, ECB cut-and-paste, bit flipping), stream
6
+ cipher key reuse, or meet-in-the-middle attacks.gated: true
7
+ ---
8
+
9
+ # SKILL: Symmetric Cipher Attacks — Expert Cryptanalysis Playbook
10
+
11
+ > **AI LOAD INSTRUCTION**: Expert techniques for attacking symmetric encryption in CTF and authorized testing. Covers CBC padding oracle, CBC bit flipping, ECB detection and exploitation, stream cipher key reuse, LFSR/LCG state recovery, RC4 biases, and meet-in-the-middle attacks. Base models often confuse ECB and CBC attack strategies or fail to set up byte-at-a-time ECB decryption correctly.
12
+
13
+ ## 0. RELATED ROUTING
14
+
15
+ - [rsa-attack-techniques](../rsa-attack-techniques/SKILL.md) when symmetric key is protected by RSA
16
+ - [hash-attack-techniques](../hash-attack-techniques/SKILL.md) when HMAC or hash-based authentication is involved
17
+ - [lattice-crypto-attacks](../lattice-crypto-attacks/SKILL.md) for LCG/LFSR state recovery via lattice methods
18
+
19
+ ### Advanced Reference
20
+
21
+ Also load [BLOCK_CIPHER_ATTACKS.md](./BLOCK_CIPHER_ATTACKS.md) when you need:
22
+ - Detailed attack scripts with full Python implementations
23
+ - Step-by-step byte-at-a-time ECB walkthrough
24
+ - PadBuster usage and custom padding oracle scripts
25
+ - LCG/LFSR recovery implementation
26
+
27
+ ### Quick attack selection
28
+
29
+ | Observable Behavior | Likely Weakness | Attack |
30
+ |---|---|---|
31
+ | Same plaintext → same ciphertext (block-aligned) | ECB mode | Cut-and-paste / byte-at-a-time |
32
+ | Padding error distinguishable | CBC padding oracle | Decrypt without key |
33
+ | Can modify ciphertext, affects next block | CBC mode, no integrity check | Bit flipping |
34
+ | Key reused with XOR/stream cipher | Two-time pad | XOR ciphertexts together |
35
+ | Predictable PRNG output | LCG or LFSR | State recovery |
36
+ | Double encryption used | 2DES-like | Meet in the middle |
37
+
38
+ ---
39
+
40
+ ## 1. PADDING ORACLE ATTACK (CBC MODE)
41
+
42
+ ### 1.1 Mechanism
43
+
44
+ CBC decryption: `P_i = D_K(C_i) ⊕ C_{i-1}`
45
+
46
+ If the server reveals whether padding is valid (PKCS#7), we can decrypt any block by manipulating the previous ciphertext block.
47
+
48
+ ### 1.2 Attack Steps
49
+
50
+ ```
51
+ Target: decrypt block C_i (with unknown plaintext P_i)
52
+
53
+ For byte position b = 15 down to 0 (last byte first):
54
+ padding_value = 16 - b
55
+
56
+ For guess = 0x00 to 0xFF:
57
+ Construct modified C'_{i-1}:
58
+ - Bytes 0..b-1: original C_{i-1} bytes
59
+ - Byte b: guess
60
+ - Bytes b+1..15: calculated to produce correct padding
61
+
62
+ Send (C'_{i-1} || C_i) to oracle
63
+
64
+ If oracle says "valid padding":
65
+ intermediate_byte[b] = guess ⊕ padding_value
66
+ plaintext_byte[b] = intermediate_byte[b] ⊕ original_C_{i-1}[b]
67
+ ```
68
+
69
+ ### 1.3 Python Implementation
70
+
71
+ ```python
72
+ def padding_oracle_attack(ciphertext, block_size, oracle):
73
+ """
74
+ oracle(ct) returns True if padding is valid, False otherwise.
75
+ ciphertext includes IV as first block.
76
+ """
77
+ blocks = [ciphertext[i:i+block_size] for i in range(0, len(ciphertext), block_size)]
78
+ plaintext = b""
79
+
80
+ for block_idx in range(1, len(blocks)):
81
+ prev_block = bytearray(blocks[block_idx - 1])
82
+ curr_block = blocks[block_idx]
83
+ intermediate = [0] * block_size
84
+ decrypted = [0] * block_size
85
+
86
+ for byte_pos in range(block_size - 1, -1, -1):
87
+ padding_val = block_size - byte_pos
88
+
89
+ for guess in range(256):
90
+ modified = bytearray(block_size)
91
+ modified[byte_pos] = guess
92
+
93
+ for j in range(byte_pos + 1, block_size):
94
+ modified[j] = intermediate[j] ^ padding_val
95
+
96
+ test_ct = bytes(modified) + curr_block
97
+ if oracle(test_ct):
98
+ if byte_pos == block_size - 1:
99
+ # Verify it's not a false positive (padding 0x02 0x02)
100
+ check = bytearray(modified)
101
+ check[byte_pos - 1] ^= 1
102
+ if not oracle(bytes(check) + curr_block):
103
+ continue
104
+
105
+ intermediate[byte_pos] = guess ^ padding_val
106
+ decrypted[byte_pos] = intermediate[byte_pos] ^ prev_block[byte_pos]
107
+ break
108
+
109
+ plaintext += bytes(decrypted)
110
+
111
+ return plaintext
112
+ ```
113
+
114
+ ### 1.4 Tools
115
+
116
+ ```bash
117
+ # PadBuster
118
+ padbuster http://target/decrypt?ct= CIPHERTEXT_HEX 16 -encoding 0
119
+ padbuster http://target/decrypt?ct= CIPHERTEXT_HEX 16 -encoding 0 -plaintext "admin=true"
120
+ ```
121
+
122
+ ---
123
+
124
+ ## 2. CBC BIT FLIPPING
125
+
126
+ ### 2.1 Concept
127
+
128
+ Flipping bit at position j in C_{i-1} flips the same bit at position j in P_i (and corrupts all of P_{i-1}).
129
+
130
+ ```
131
+ Original: P_i[j] = D_K(C_i)[j] ⊕ C_{i-1}[j]
132
+ Modified: P'_i[j] = D_K(C_i)[j] ⊕ C'_{i-1}[j]
133
+ = P_i[j] ⊕ (C_{i-1}[j] ⊕ C'_{i-1}[j])
134
+ ```
135
+
136
+ ### 2.2 Practical Example
137
+
138
+ ```python
139
+ def cbc_bitflip(ciphertext, block_size, target_byte_pos, old_value, new_value):
140
+ """
141
+ Flip byte in plaintext block N+1 by modifying ciphertext block N.
142
+ target_byte_pos: absolute position in plaintext (0-indexed)
143
+ """
144
+ ct = bytearray(ciphertext)
145
+ block_num = target_byte_pos // block_size
146
+ byte_in_block = target_byte_pos % block_size
147
+
148
+ # Modify previous block (block_num - 1) to flip target byte
149
+ modify_pos = (block_num - 1) * block_size + byte_in_block
150
+
151
+ # XOR to cancel old value and set new value
152
+ ct[modify_pos] ^= old_value ^ new_value
153
+ return bytes(ct)
154
+
155
+ # Example: flip "admin=0" to "admin=1"
156
+ # If "admin=0" is at byte position 22 (block 1, byte 6):
157
+ modified_ct = cbc_bitflip(ciphertext, 16, 22, ord('0'), ord('1'))
158
+ ```
159
+
160
+ ---
161
+
162
+ ## 3. ECB MODE ATTACKS
163
+
164
+ ### 3.1 Detection
165
+
166
+ ```python
167
+ def detect_ecb(ciphertext, block_size=16):
168
+ """ECB produces identical blocks for identical plaintext blocks."""
169
+ blocks = [ciphertext[i:i+block_size] for i in range(0, len(ciphertext), block_size)]
170
+ return len(blocks) != len(set(blocks))
171
+
172
+ # Force detection: send repeated plaintext
173
+ test_input = b"A" * 48 # at least 3 blocks of identical data
174
+ # If response has repeated blocks → ECB
175
+ ```
176
+
177
+ ### 3.2 ECB Cut-and-Paste
178
+
179
+ Reorder ciphertext blocks to create new valid plaintexts.
180
+
181
+ ```
182
+ Original blocks:
183
+ Block 0: "email=foo@bar.c"
184
+ Block 1: "om&role=user&uid"
185
+ Block 2: "=10\x0d\x0d\x0d..."
186
+
187
+ Attack: craft input so "admin" + padding lands in its own block,
188
+ then swap it in place of "user" block.
189
+
190
+ Step 1: Send email that aligns "admin" + PKCS7 to a block:
191
+ email = "foo@bar.coadmin\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
192
+ → Block 1 encrypts "admin\x0b\x0b..." (save this block)
193
+
194
+ Step 2: Send email that puts "role=" at end of block:
195
+ email = "foo@bar.co"
196
+ → Block 2 = "=user&uid=10..." (but we replace this)
197
+
198
+ Step 3: Replace last block with saved "admin\x0b..." block
199
+ ```
200
+
201
+ ### 3.3 Byte-at-a-Time ECB Decryption
202
+
203
+ Decrypt unknown appended secret one byte at a time.
204
+
205
+ ```python
206
+ def ecb_byte_at_a_time(encrypt_oracle, block_size=16):
207
+ """
208
+ encrypt_oracle(input_bytes) = AES_ECB(input || unknown_secret)
209
+ Returns the unknown_secret.
210
+ """
211
+ secret = b""
212
+ secret_len = len(encrypt_oracle(b""))
213
+
214
+ for i in range(secret_len):
215
+ block_num = i // block_size
216
+ pad_len = block_size - 1 - (i % block_size)
217
+ padding = b"A" * pad_len
218
+
219
+ # Build lookup table
220
+ target_ct = encrypt_oracle(padding)
221
+ target_block = target_ct[block_num * block_size:(block_num + 1) * block_size]
222
+
223
+ for byte_val in range(256):
224
+ test_input = padding + secret + bytes([byte_val])
225
+ test_ct = encrypt_oracle(test_input)
226
+ test_block = test_ct[block_num * block_size:(block_num + 1) * block_size]
227
+
228
+ if test_block == target_block:
229
+ secret += bytes([byte_val])
230
+ break
231
+
232
+ return secret
233
+ ```
234
+
235
+ ---
236
+
237
+ ## 4. STREAM CIPHER ATTACKS
238
+
239
+ ### 4.1 Known Plaintext / Key Reuse (Two-Time Pad)
240
+
241
+ ```python
242
+ def two_time_pad(c1, c2, known_crib=None):
243
+ """
244
+ c1 = m1 ⊕ K, c2 = m2 ⊕ K (same key K)
245
+ c1 ⊕ c2 = m1 ⊕ m2 (key cancels)
246
+ """
247
+ xored = bytes(a ^ b for a, b in zip(c1, c2))
248
+
249
+ if known_crib:
250
+ results = []
251
+ for offset in range(len(xored) - len(known_crib) + 1):
252
+ candidate = bytes(
253
+ xored[offset + i] ^ known_crib[i] for i in range(len(known_crib))
254
+ )
255
+ if all(0x20 <= b <= 0x7e for b in candidate):
256
+ results.append((offset, candidate))
257
+ return results
258
+ return xored
259
+ ```
260
+
261
+ ### 4.2 Single-Byte XOR Brute Force
262
+
263
+ ```python
264
+ def single_byte_xor_crack(ciphertext):
265
+ """Brute force single-byte XOR key using frequency analysis."""
266
+ english_freq = {
267
+ 'e': 12.7, 't': 9.1, 'a': 8.2, 'o': 7.5, 'i': 7.0,
268
+ 'n': 6.7, 's': 6.3, 'h': 6.1, 'r': 6.0, 'd': 4.3,
269
+ }
270
+ best_score, best_key, best_plaintext = 0, 0, b""
271
+
272
+ for key in range(256):
273
+ plaintext = bytes(b ^ key for b in ciphertext)
274
+ score = sum(
275
+ english_freq.get(chr(b).lower(), 0)
276
+ for b in plaintext if 0x20 <= b <= 0x7e
277
+ )
278
+ if score > best_score:
279
+ best_score = score
280
+ best_key = key
281
+ best_plaintext = plaintext
282
+
283
+ return best_key, best_plaintext
284
+ ```
285
+
286
+ ### 4.3 Repeating-Key XOR (Kasiski-like)
287
+
288
+ ```python
289
+ def repeating_xor_crack(ciphertext, max_keylen=40):
290
+ """Crack repeating-key XOR using Hamming distance for key length."""
291
+ def hamming(a, b):
292
+ return sum(bin(x ^ y).count('1') for x, y in zip(a, b))
293
+
294
+ # Find key length
295
+ scores = []
296
+ for kl in range(2, max_keylen + 1):
297
+ blocks = [ciphertext[i:i+kl] for i in range(0, len(ciphertext) - kl, kl)]
298
+ if len(blocks) < 4:
299
+ continue
300
+ dist = sum(hamming(blocks[i], blocks[i+1]) for i in range(min(3, len(blocks)-1)))
301
+ normalized = dist / (min(3, len(blocks)-1) * kl)
302
+ scores.append((normalized, kl))
303
+
304
+ best_keylen = sorted(scores)[0][1]
305
+
306
+ # Crack each position with single-byte XOR
307
+ key = b""
308
+ for i in range(best_keylen):
309
+ column = bytes(ciphertext[j] for j in range(i, len(ciphertext), best_keylen))
310
+ k, _ = single_byte_xor_crack(column)
311
+ key += bytes([k])
312
+
313
+ return key
314
+ ```
315
+
316
+ ### 4.4 LFSR State Recovery (Berlekamp-Massey)
317
+
318
+ ```python
319
+ def berlekamp_massey_gf2(output_bits):
320
+ """Recover LFSR feedback polynomial from output sequence over GF(2)."""
321
+ n = len(output_bits)
322
+ C = [0] * (n + 1)
323
+ B = [0] * (n + 1)
324
+ C[0] = B[0] = 1
325
+ L = 0
326
+ m = 1
327
+ b = 1
328
+
329
+ for N in range(n):
330
+ d = output_bits[N]
331
+ for i in range(1, L + 1):
332
+ d ^= C[i] & output_bits[N - i]
333
+
334
+ if d == 0:
335
+ m += 1
336
+ elif 2 * L <= N:
337
+ T = C[:]
338
+ for i in range(m, n + 1):
339
+ C[i] ^= B[i - m]
340
+ L = N + 1 - L
341
+ B = T
342
+ b = d
343
+ m = 1
344
+ else:
345
+ for i in range(m, n + 1):
346
+ C[i] ^= B[i - m]
347
+ m += 1
348
+
349
+ return C[:L + 1], L
350
+ ```
351
+
352
+ ### 4.5 RC4 Biases
353
+
354
+ | Bias | Description | Exploitation |
355
+ |---|---|---|
356
+ | Initial byte bias | P(K[0] = 0) ≈ 2/256 (double normal) | Statistical plaintext recovery for first bytes |
357
+ | Fluhrer-Mantin-Shamir | Weak key scheduling with IV | WEP attack (historical) |
358
+ | NOMORE attack | Long-term biases in keystream | TLS/RC4 plaintext recovery (2^24-2^26 ciphertexts) |
359
+ | Invariance weakness | Key-dependent biases throughout stream | Statistical attack on many encryptions |
360
+
361
+ ---
362
+
363
+ ## 5. MEET-IN-THE-MIDDLE
364
+
365
+ ### 5.1 Double Encryption Attack
366
+
367
+ ```
368
+ Double encryption: C = E_K2(E_K1(P))
369
+ Brute force: 2^(2n) expected
370
+ MITM: 2^(n+1) + storage for 2^n entries
371
+
372
+ Attack:
373
+ 1. Encrypt P with all possible K1 → store (E_K1(P), K1) in table
374
+ 2. Decrypt C with all possible K2 → check if D_K2(C) matches any entry
375
+ 3. Match found → (K1, K2) recovered
376
+ ```
377
+
378
+ ```python
379
+ from itertools import product
380
+
381
+ def meet_in_the_middle(encrypt, decrypt, plaintext, ciphertext, keyspace_bits):
382
+ """MITM attack on double encryption."""
383
+ # Phase 1: build encryption table
384
+ enc_table = {}
385
+ for k1 in range(2**keyspace_bits):
386
+ intermediate = encrypt(plaintext, k1)
387
+ enc_table[intermediate] = k1
388
+
389
+ # Phase 2: decrypt and look up
390
+ for k2 in range(2**keyspace_bits):
391
+ intermediate = decrypt(ciphertext, k2)
392
+ if intermediate in enc_table:
393
+ k1 = enc_table[intermediate]
394
+ return k1, k2
395
+
396
+ return None
397
+ ```
398
+
399
+ ---
400
+
401
+ ## 6. DECISION TREE
402
+
403
+ ```
404
+ Symmetric cipher challenge — what can you observe?
405
+
406
+ ├─ Can you detect the mode?
407
+ │ ├─ Repeated input → repeated output blocks?
408
+ │ │ └─ Yes → ECB mode
409
+ │ │ ├─ Can control prefix → byte-at-a-time decryption
410
+ │ │ ├─ Can reorder blocks → cut-and-paste
411
+ │ │ └─ Can detect block boundaries → block alignment oracle
412
+ │ │
413
+ │ ├─ Error message differs for bad padding?
414
+ │ │ └─ Yes → Padding oracle (CBC)
415
+ │ │ └─ PadBuster or custom script
416
+ │ │
417
+ │ └─ Can modify ciphertext and observe effect?
418
+ │ └─ Next-block plaintext changes → CBC bit flipping
419
+
420
+ ├─ Stream cipher or XOR?
421
+ │ ├─ Key reused on different messages?
422
+ │ │ └─ XOR ciphertexts → crib drag
423
+ │ │
424
+ │ ├─ Known plaintext-ciphertext pair?
425
+ │ │ └─ Recover keystream directly
426
+ │ │
427
+ │ ├─ Single-byte XOR key?
428
+ │ │ └─ Brute force 256 keys with frequency analysis
429
+ │ │
430
+ │ ├─ Repeating-key XOR?
431
+ │ │ └─ Hamming distance → key length → per-position crack
432
+ │ │
433
+ │ └─ LFSR-based?
434
+ │ └─ Berlekamp-Massey for state/polynomial recovery
435
+
436
+ ├─ PRNG-based cipher?
437
+ │ ├─ LCG → truncated output lattice attack
438
+ │ ├─ Mersenne Twister → 624 outputs → full state recovery
439
+ │ └─ Custom PRNG → analyze period and state size
440
+
441
+ ├─ Double / triple encryption?
442
+ │ └─ Meet-in-the-middle
443
+
444
+ └─ RC4 specifically?
445
+ ├─ Single encryption → initial byte bias
446
+ ├─ Many encryptions same key → statistical attack
447
+ └─ IV prepended to key → FMS attack (WEP-like)
448
+ ```
449
+
450
+ ---
451
+
452
+ ## 7. TOOLS
453
+
454
+ | Tool | Purpose |
455
+ |---|---|
456
+ | **PadBuster** | Automated padding oracle exploitation |
457
+ | **xortool** | Repeating-key XOR analysis (key length detection + cracking) |
458
+ | **CyberChef** | Quick XOR, encoding, block cipher operations |
459
+ | **SageMath** | LFSR/LCG analysis, lattice-based recovery |
460
+ | **pycryptodome** | AES/DES implementation for testing |
461
+ | **hashcat** | Brute force symmetric keys (GPU-accelerated) |
462
+ | **Custom Python** | All attacks above implementable in pure Python |