ECB Mode Attacks
Block Cipher
ECB (Electronic Codebook) mode encrypts each block independently, making it vulnerable to pattern analysis, block swapping, and chosen-plaintext attacks.
ECB Weaknesses
Problems
- • Identical blocks → identical ciphertext
- • Patterns visible (famous penguin image)
- • Blocks can be reordered
- • Cut-and-paste attacks
Detection
- • Repeated 16-byte blocks
- • No IV in ciphertext
- • Same plaintext = same ciphertext
- • Block size analysis
ECB Byte-at-a-Time Attack
python
# Attack scenario:
# AES-ECB(your_input || secret || padding)
# Goal: Reveal the secret one byte at a time
def detect_block_size(oracle):
"""Find the cipher block size"""
prev_len = len(oracle(b''))
for i in range(1, 33):
curr_len = len(oracle(b'A' * i))
if curr_len > prev_len:
return curr_len - prev_len
return 16
def ecb_byte_at_a_time(oracle, block_size=16):
"""Recover secret byte-by-byte"""
known = b''
while True:
# Padding to align target byte at block boundary
pad_len = block_size - (len(known) % block_size) - 1
padding = b'A' * pad_len
# Get target block
target = oracle(padding)
target_block = target[:len(padding) + len(known) + 1]
# Brute force the byte
for byte in range(256):
test = padding + known + bytes([byte])
result = oracle(test)
if result[:len(test)] == target_block:
known += bytes([byte])
break
else:
break # No match, probably hit padding
return known