The First Half: Reloaded
Cryptography · MUST CTF: 2024 · Superior
Problem
This code represents a challenge where the goal is to deduce a secret "flag" value using a partially leaking encryption process.
The Encryption Process:
Flag Setup:
A flag is fetched from the environment variable
flag, defaulting to a placeholder value if not set.The flag is stored as a byte-encoded string.
Leak Function:
The function
leak(i)takes an indexiand returns the XOR of thei-th byte of the flag with a randomly chosen byte from the stringMUSTCTF.
User Interaction:
The program accepts user input
ito specify the index of the flag byte to leak.Leaks are restricted to the first half of the flag (
len(flag) // 2).
Termination:
An unexpected input or exception breaks the loop, terminating the process.
Remote Access:
Players interact with the challenge through a remote service using the command:
nc 139.162.5.230 10169
Solution
This Python script uses the Pwntools library to communicate with the remote server and deduce the flag:
Setup:
Establishes a remote connection to the provided address.
Initializes an empty
flagarray to store deduced bytes.Defines
allowed_charsas the byte stringb"MUSTCTF".
Deduction Logic:
A helper function
deduce_flag_byteiterates over possible byte values (0–255) and determines which matches the leaked XORed values.A byte is deemed correct if XORing it with each leaked value produces a valid character from
allowed_chars.
Leaking Flag Bytes:
For each index in the last half of the flag (
-32to0):Sends the index to the remote service multiple times to gather leak values.
Uses
deduce_flag_byteto find the actual flag byte.Maps the deduced byte to the correct position in the
flagarray.
Error Handling:
Handles EOF errors gracefully, ensuring the connection is closed cleanly.
Output:
Prints the partially or fully reconstructed flag, replacing undeduced bytes with a
?.
Last updated