diamond-half-strokeThe 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:

  1. 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.

  2. Leak Function:

    • The function leak(i) takes an index i and returns the XOR of the i-th byte of the flag with a randomly chosen byte from the string MUSTCTF.

  3. User Interaction:

    • The program accepts user input i to specify the index of the flag byte to leak.

    • Leaks are restricted to the first half of the flag (len(flag) // 2).

  4. Termination:

    • An unexpected input or exception breaks the loop, terminating the process.

  5. 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:

  1. Setup:

    • Establishes a remote connection to the provided address.

    • Initializes an empty flag array to store deduced bytes.

    • Defines allowed_chars as the byte string b"MUSTCTF".

  2. Deduction Logic:

    • A helper function deduce_flag_byte iterates 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.

  3. Leaking Flag Bytes:

    • For each index in the last half of the flag (-32 to 0):

      • Sends the index to the remote service multiple times to gather leak values.

      • Uses deduce_flag_byte to find the actual flag byte.

      • Maps the deduced byte to the correct position in the flag array.

  4. Error Handling:

    • Handles EOF errors gracefully, ensuring the connection is closed cleanly.

  5. Output:

    • Prints the partially or fully reconstructed flag, replacing undeduced bytes with a ?.

Last updated