| name | cryptanalyse |
| description | Cryptanalyse — attaques de chiffrement classique et moderne, side-channel, padding oracle, meet-in-the-middle, attaques d'implémentation, et outils de cracking |
| tags | ["cryptanalyse","crypto","AES","RSA","side-channel","padding-oracle","hash","cracking"] |
| version | 1 |
Cryptanalyse
Guide de cryptanalyse offensive — attaques sur chiffrements symétriques, asymétriques, hachages, et implémentations.
1. Cryptanalyse Classique
Fréquence (Substitution simple)
Vigenère (Kasiski / Index of Coincidence)
python3 -c "
from pwn import *
# Vigenere solver with chi-squared scoring
# Utiliser https://github.com/woodfrog/pycipher
"
Enigma (WWII)
2. Cryptanalyse Symétrique Moderne
Padding Oracle Attack (AES-CBC)
from pwn import *
def oracle(ciphertext):
"""Retourne True si padding valide, False sinon"""
r = remote('target.com', 443)
r.send(ciphertext)
response = r.recv()
return b'Invalid padding' not in response
def decrypt_block(block, oracle):
"""Déchiffrer un bloc CBC via padding oracle"""
intermediate = [0] * 16
plaintext = [0] * 16
for byte_pos in range(15, -1, -1):
for guess in range(256):
fake = [0] * 16
for i in range(byte_pos + 1, 16):
fake[i] = intermediate[i] ^ (16 - byte_pos)
fake[byte_pos] = guess
if oracle(bytes(fake)):
intermediate[byte_pos] = guess ^ (16 - byte_pos)
break
return bytes(intermediate)
CBC Bit Flipping
def bit_flip(ciphertext, block_num, byte_pos, original, desired):
ct = bytearray(ciphertext)
ct[block_num * 16 + byte_pos] ^= original ^ desired
return bytes(ct)
ECB Byte-at-a-Time
def detect_block_size(oracle):
"""Détecter taille de bloc"""
base = len(oracle(b''))
for i in range(1, 33):
if len(oracle(b'A' * i)) != base:
return len(oracle(b'A' * i)) - base
return 16
def ecb_oracle_attack(oracle, block_size=16):
"""Déchiffrer byte par byte via oracle ECB"""
unknown = b''
for _ in range(len(oracle(b''))):
prefix = b'A' * (block_size - 1 - len(unknown) % block_size)
target = oracle(prefix)[:len(prefix) + len(unknown) + 1]
for b in range(256):
test = oracle(prefix + unknown + bytes([b]))
if test[:len(target)] == target:
unknown += bytes([b])
break
return unknown
3. Cryptanalyse Asymétrique
RSA Attacks
Wiener Attack (d < N^0.25)
from sympy import continued_fraction, convergents
def wiener_attack(e, n):
cf = continued_fraction(e, n)
for k, d in convergents(cf):
if k != 0:
phi = (e * d - 1) // k
discriminant = (n - phi + 1)**2 - 4*n
if discriminant > 0:
p = (n - phi + 1 - sqrt(discriminant)) // 2
if p * n // p == n:
return d
return None
Common Modulus Attack
def common_modulus(c1, c2, e1, e2, n):
g, a, b = extended_gcd(e1, e2)
if a < 0:
c1 = pow(c1, -1, n)
a = -a
if b < 0:
c2 = pow(c2, -1, n)
b = -b
return (pow(c1, a, n) * pow(c2, b, n)) % n
Low Public Exponent (e=3, small m)
import gmpy2
def cube_root_attack(c):
m, exact = gmpy2.iroot(c, 3)
return int(m) if exact else None
Coppersmith Attack
ECDLP (Elliptic Curve Discrete Log)
4. Hash Attacks
Length Extension Attack
import struct
import hashlib
def md5_length_extension(original_hash, original_msg, append_msg, key_len):
pass
Hash Collision
5. Side-Channel Attacks
Timing Attack
import time
import statistics
def timing_attack(oracle, secret_len):
recovered = ''
for pos in range(secret_len):
times = {}
for c in range(32, 127):
test = recovered + chr(c)
measurements = []
for _ in range(100):
start = time.perf_counter_ns()
oracle(test + 'A' * (secret_len - len(test)))
end = time.perf_counter_ns()
measurements.append(end - start)
times[chr(c)] = statistics.median(measurements)
recovered += max(times, key=times.get)
return recovered
Power Analysis (SPA/DPA)
Cache Timing (Spectre/Meltdown class)
6. Implementation Attacks
Lattice-based Attacks
def lattice_attack_dsa(signatures, hash_values, n, k_bits):
"""
Signatures ECDSA avec nonces biaisés
Réduire avec LLL → trouver clé privée
"""
Bleichenbacher Attack (PKCS#1 v1.5)
7. Tools de Cracking
Hashcat
hashcat -m 0 hash.txt wordlist.txt
hashcat -m 0 hash.txt wordlist.txt -r rules/best64.rule
hashcat -m 0 hash.txt -a 3 ?l?l?l?l?l?l?l?l
John the Ripper
john --wordlist=wordlist.txt hash.txt
john --incremental hash.txt
john --show hash.txt
8. Tools Compendium
| Outil | Usage |
|---|
| CrypTool 2 | Cryptanalyse visuelle |
| SageMath | Mathématiques avancées (LLL, Coppersmith) |
| Hashcat | GPU cracking |
| John the Ripper | CPU cracking |
| RsaCtfTool | RSA multi-attack automatisé |
| xortool | XOR analysis |
| FeatherDuster | Crypto analysis framework |
| CryptoHack | Practice + writeups |
| Z3/SMT | Constraint solving (opaque predicates) |
| Galois | EC crypto analysis |
9. Ressources