Lab 7: Symmetric Encryption
XOR and OTP
We’ve been talking in lecture about symmetric encryption techniques, in particular xor and the One-Time Pad (OTP).
In this week’s lab, we’ll apply some of of this a bit more directly with some challenges from the Cryptography module of Intro to Cybersecurity.
XORing ASCII Strings
In this challenge, you will be using xor to decrypt ascii strings given a key string.
Concretely, each byte of the decrypted string is the result of computing the xor between the corresponding bytes in the encrypted string and the key string.
The challenge script /challenge/run looks as follows:
#!/usr/bin/exec-suid -- /usr/bin/python3 -I
import random
import string
import sys
from Crypto.Util.strxor import strxor
valid_keys = "!#$%&()"
valid_chars = ''.join(
c for c in string.ascii_letters
if all(chr(ord(k)^ord(c)) in string.ascii_letters for k in valid_keys)
)
print(valid_keys, valid_chars)
for n in range(1, 10):
print(f"Challenge number {n}...")
key_str = ''.join(random.sample(valid_keys*10, 10))
pt_str = ''.join(random.sample(valid_chars*10, 10))
ct_str = strxor(pt_str.encode(), key_str.encode()).decode()
print(f"- Encrypted String: {ct_str}")
print(f"- XOR Key String: {key_str}")
answer = input("- Decrypted String? ").strip()
if answer != pt_str:
print("Incorrect!")
sys.exit(1)
print("Correct! Moving on.")
print("You have mastered XORing ASCII! Your flag:")
print(open("/flag").read())
This script will give you an encrypted string and a key string and ask you for the decrypted result.
You will need to answer 10 of these questions to get the flag.
Note that you do not need to compute the xor of each character yourself, as the strxor function is provded and can be imported as shown in line 7.
One-Time Pad
As we’ve discussed, a One-Time Pad is a symmetric cryptosystem where each key is used exactly once.
In this challenge, the run script (shown below) will encipher the flag using a randomly generated key, then provide you with the key and the corresponding ciphertext.
#!/usr/bin/exec-suid -- /usr/bin/python3 -I
from Crypto.Random import get_random_bytes
from Crypto.Util.strxor import strxor
flag = open("/flag", "rb").read()
key = get_random_bytes(len(flag))
ciphertext = strxor(flag, key)
print(f"One-Time Pad Key (hex): {key.hex()}")
print(f"Flag Ciphertext (hex): {ciphertext.hex()}")
You just need to decrypt the ciphertext for the flag. Pretty straightforward stuff.
Many-Time Pad
The perfect secrecy of OTP is violated if you use the key more than once.
In this challenge, you’ll be conducting a chosen plaintext attack on a “many-time pad”, where the same key is used to encrypt every message. The run script will encipher the flag and print the resulting hex string, then allow you to encipher any message you want.
The run script is as follows:
#!/usr/bin/exec-suid -- /usr/bin/python3 -I
from Crypto.Random import get_random_bytes
from Crypto.Util.strxor import strxor
flag = open("/flag", "rb").read()
key = get_random_bytes(256)
ciphertext = strxor(flag, key[:len(flag)])
print(f"Flag Ciphertext (hex): {ciphertext.hex()}")
while True:
plaintext = bytes.fromhex(input("Plaintext (hex): "))
ciphertext = strxor(plaintext, key[:len(plaintext)])
print(f"Ciphertext (hex): {ciphertext.hex()}")
One-Time Pad Tampering (Extra)
As the challenge instructions state, OTP does not protect a message’s integrity.
In this challenge, there are two scripts: /challenge/dispatcher and /challenge/worker.
dispatcher reads the key, xor’s it with the string “sleep” and prints the enciphered string.
#!/usr/bin/exec-suid -- /usr/bin/python3 -I
from Crypto.Util.strxor import strxor
key = open("/challenge/.key", "rb").read()
ciphertext = strxor(b"sleep", key[:5])
print(f"TASK: {ciphertext.hex()}")
$ /challenge/dispatcher
TASK: d180a04bda
The output is meant to be piped into the worker, wchich listens on stdin for a line that starts with “TASK: “ and does an action in response.
#!/usr/bin/exec-suid -- /usr/bin/python3 -I
from Crypto.Util.strxor import strxor
import time
import sys
key = open("/challenge/.key", "rb").read()
while line := sys.stdin.readline():
if not line.startswith("TASK: "):
continue
data = bytes.fromhex(line.split()[1])
cipher_len = min(len(data), len(key))
plaintext = strxor(data[:cipher_len], key[:cipher_len])
print(f"Hex of plaintext: {plaintext.hex()}")
print(f"Received command: {plaintext}")
if plaintext == b"sleep":
print("Sleeping!")
time.sleep(1)
elif plaintext == b"flag!":
print("Victory! Your flag:")
print(open("/flag").read())
else:
print("Unknown command!")
$ /challenge/dispatcher | /challenge/worker
Hex of plaintext: 736c656570
Received command: b'sleep'
Sleeping!
We are conducting a known plaintext attack, since we know the plaintext “sleep” and its corresponding ciphertext for an unknown key.