$npx -y skills add Prohao42/aimy-skill --skill hash-attack-techniquesHash attack playbook. Use when exploiting length extension, MD5/SHA1 collisions, HMAC timing leaks, birthday attacks, or hash-based proof of work in CTF and authorized testing scenarios.
| 1 | # SKILL: Hash Attack Techniques — Expert Cryptanalysis Playbook |
| 2 | |
| 3 | > **AI LOAD INSTRUCTION**: Expert hash attack techniques for CTF and security assessments. Covers length extension attacks, MD5/SHA1 collision generation, meet-in-the-middle hash attacks, HMAC timing side channels, birthday attacks, and proof-of-work solving. Base models often incorrectly apply length extension to HMAC or SHA-3, or fail to distinguish between identical-prefix and chosen-prefix collisions. |
| 4 | |
| 5 | ## 0. RELATED ROUTING |
| 6 | |
| 7 | - [rsa-attack-techniques](../rsa-attack-techniques/SKILL.md) when hash weaknesses affect RSA signature schemes |
| 8 | - [symmetric-cipher-attacks](../symmetric-cipher-attacks/SKILL.md) when hash is used in key derivation |
| 9 | - [classical-cipher-analysis](../classical-cipher-analysis/SKILL.md) when analyzing hash-like constructions in classical ciphers |
| 10 | |
| 11 | ### Quick attack selection |
| 12 | |
| 13 | | Scenario | Attack | Tool | |
| 14 | |---|---|---| |
| 15 | | `H(secret \|\| msg)` known, extend message | Length extension | HashPump, hash_extender | |
| 16 | | Need two files with same MD5 | Identical-prefix collision | fastcoll | |
| 17 | | Need specific MD5 prefix match | Chosen-prefix collision | hashclash | |
| 18 | | Byte-by-byte HMAC comparison | Timing attack | Custom script | |
| 19 | | Find any collision | Birthday attack | O(2^(n/2)) | |
| 20 | | Proof of work: find hash with leading zeros | Brute force | hashcat, Python | |
| 21 | |
| 22 | --- |
| 23 | |
| 24 | ## 1. LENGTH EXTENSION ATTACK |
| 25 | |
| 26 | ### 1.1 Vulnerable vs Non-Vulnerable |
| 27 | |
| 28 | | Hash | Vulnerable | Why | |
| 29 | |---|---|---| |
| 30 | | MD5 | Yes | Merkle-Damgard construction | |
| 31 | | SHA-1 | Yes | Merkle-Damgard construction | |
| 32 | | SHA-256 | Yes | Merkle-Damgard construction | |
| 33 | | SHA-512 | Yes | Merkle-Damgard construction | |
| 34 | | SHA-3 / Keccak | No | Sponge construction | |
| 35 | | HMAC-* | No | Double hashing prevents extension | |
| 36 | | SHA-256 truncated | No (if truncated) | Missing internal state bits | |
| 37 | | BLAKE2 | No | Different construction | |
| 38 | |
| 39 | ### 1.2 Attack Mechanism |
| 40 | |
| 41 | ``` |
| 42 | Given: MAC = H(secret || original_message) |
| 43 | Known: original_message, len(secret), MAC value |
| 44 | Compute: H(secret || original_message || padding || extension) |
| 45 | WITHOUT knowing the secret! |
| 46 | |
| 47 | How: The MAC value IS the internal hash state after processing |
| 48 | (secret || original_message || padding). |
| 49 | Initialize hash with this state, continue hashing extension. |
| 50 | ``` |
| 51 | |
| 52 | ### 1.3 Padding Calculation (MD5/SHA) |
| 53 | |
| 54 | ```python |
| 55 | def md5_padding(message_len_bytes): |
| 56 | """Calculate MD5/SHA padding for given message length.""" |
| 57 | bit_len = message_len_bytes * 8 |
| 58 | |
| 59 | # Pad with 0x80 + zeros until length ≡ 56 (mod 64) |
| 60 | padding = b'\x80' |
| 61 | padding += b'\x00' * ((55 - message_len_bytes) % 64) |
| 62 | |
| 63 | # Append original length as 64-bit little-endian (MD5) |
| 64 | # or big-endian (SHA) |
| 65 | padding += bit_len.to_bytes(8, 'little') # MD5 |
| 66 | # padding += bit_len.to_bytes(8, 'big') # SHA |
| 67 | |
| 68 | return padding |
| 69 | ``` |
| 70 | |
| 71 | ### 1.4 Tool Usage |
| 72 | |
| 73 | ```bash |
| 74 | # HashPump |
| 75 | hashpump -s "known_mac_hex" \ |
| 76 | -d "original_data" \ |
| 77 | -k 16 \ # secret length |
| 78 | -a "extension_data" |
| 79 | |
| 80 | # Output: new_mac, new_data (original + padding + extension) |
| 81 | |
| 82 | # hash_extender |
| 83 | hash_extender --data "original" \ |
| 84 | --secret 16 \ |
| 85 | --append "extension" \ |
| 86 | --signature "known_mac_hex" \ |
| 87 | --format md5 |
| 88 | ``` |
| 89 | |
| 90 | ### 1.5 Python Implementation |
| 91 | |
| 92 | ```python |
| 93 | import struct |
| 94 | |
| 95 | def md5_extend(original_mac, original_data_len, secret_len, extension): |
| 96 | """ |
| 97 | Perform MD5 length extension attack. |
| 98 | original_mac: hex string of H(secret || original_data) |
| 99 | """ |
| 100 | # Parse MAC into MD5 internal state (4 × 32-bit words, little-endian) |
| 101 | h = struct.unpack('<4I', bytes.fromhex(original_mac)) |
| 102 | |
| 103 | # Calculate total length after padding |
| 104 | total_original = secret_len + original_data_len |
| 105 | padding = md5_padding(total_original) |
| 106 | forged_len = total_original + len(padding) + len(extension) |
| 107 | |
| 108 | # Continue MD5 from saved state with extension |
| 109 | # (requires MD5 implementation that accepts initial state) |
| 110 | from hashlib import md5 |
| 111 | # Most stdlib md5 doesn't expose state setting |
| 112 | # Use: hlextend library or custom MD5 |
| 113 | |
| 114 | import hlextend |
| 115 | sha = hlextend.new('md5') |
| 116 | new_hash = sha.extend(extension, original_data, secret_len, |
| 117 | original_mac) |
| 118 | new_data = sha.payload # includes original + padding + extension |
| 119 | |
| 120 | return new_hash, new_data |
| 121 | ``` |
| 122 | |
| 123 | --- |
| 124 | |
| 125 | ## 2. MD5 COLLISION ATTACKS |
| 126 | |
| 127 | ### 2.1 Identical-Prefix Collision (fastcoll) |
| 128 | |
| 129 | Two messages with same prefix but different content, producing identical MD5. |
| 130 | |
| 131 | ```bash |
| 132 | # Generate collision pair |
| 133 | fastcoll -p prefix_file -o collision1.bin collision2.bin |
| 134 | |
| 135 | # Result: MD5(collision1.bin) == MD5(collision2.bin) |
| 136 | # Files differ in exactly 128 bytes (two MD5 blocks) |
| 137 | ``` |
| 138 | |
| 139 | ### 2.2 Chosen-Prefix Collision (hashclash) |
| 140 | |
| 141 | Two messages with different chosen pref |