Length Extension Attack exploits the iterative structure of Merkle-Damgård hash functions (MD5, SHA-1, SHA-256). Given H(secret || message), an attacker can compute H(secret || message || padding || extension) without knowing the secret.
// BAD: Using hash for authentication
signature = SHA256(secret + data)
// Attacker knows:
// - The data
// - The signature (hash output)
// - Length of secret (or can guess)
// Attacker can compute:
new_signature = SHA256(secret + data + padding + evil_data)
The hash output represents the internal state after processing. The attacker:
// Original signed request
data = "amount=100&to=bob"
sig = SHA256(secret + data) // Known to attacker
// Attacker extends to:
evil_data = data + padding + "&amount=10000"
evil_sig = length_extend(sig, secret_length, "&amount=10000")
// Server validates:
SHA256(secret + evil_data) == evil_sig // PASSES!
HMAC's nested construction (H(K XOR opad || H(K XOR ipad || message))) prevents length extension by design. The outer hash operation on the inner hash output makes it impossible to continue the hash state. Always use HMAC - not ad-hoc constructions.