Recover Plaintext with a Padding Oracle
medium · 100 XP · machine with a lab · free · solves: 0
A test service decrypts AES-CBC messages with a fixed secret key. It tells you only one thing: whether the PKCS#7 padding of the result is valid. You captured one message: a 16-byte IV and a one-block ciphertext. They are in target.txt. The block size is 16 bytes.
Start the lab. The service runs on host oracle, TCP port 9000.
Protocol. The service sends four banner lines when you connect: a help line, the target IV, the target ciphertext and the query count. Send one command per line:
check <iv hex> <ciphertext hex>: the IV is exactly 16 bytes. The ciphertext is 1 to 16 blocks of 16 bytes. The service decrypts with your IV and repliesvalidorinvalid.- Each connection allows 12000 check queries. After the reply to the last one, the service sends
error: query limit reachedand closes the connection. A new connection starts a new count with the same key and target. - A bad command gets one reply line that starts with
error:, for exampleerror: iv must be 16 bytes. It uses no query. A line longer than 1024 characters getserror: line too longand closes the connection.
client.py opens one connection and gives you Oracle.check(iv, ciphertext) for your scripts. It returns True for valid padding.
Your task.
- Keep the target ciphertext block. Change only the IV that you submit.
- Recover the intermediate state D(C) of the block, from the last byte to the first.
- For the last byte, confirm each passing candidate: change the byte before it and check that the padding stays valid.
- XOR the intermediate state with the original IV, then remove the PKCS#7 padding.
- Run
python3 verify.py <plaintext>to check your answer.
The flag is CSEMA{<plaintext>}, with the plaintext after padding removal.
Tags: learn
Try next
- Read the Vault's Timing (medium)
- Recover the Hidden Suffix (medium)
- Intercept a Diffie-Hellman Exchange (medium)