assert flag.startswith(b'DDC{') and flag.endswith(b'}')
assert len(flag) == 18
with open("output.txt", "w") as f:
for i in range(2):
p = getPrime(64)
f.write(f'p_{i} = {p}\n')
f.write(f'f_{i} = {binarify(flag) % p}\n')
Assistant
The chal.py code:
Reads a flag of length 18 bytes with format DDC{...}.
Defines a function binarify(m) which converts the bytes m to an int by first interpreting bytes as an integer, then converting that integer to a binary string, encoding that string as bytes, and finally interpreting that as a big-endian integer.
Generates 2 primes (p_0, p_1) each 64 bits.
Writes p_i and f_i = binarify(flag) % p_i to output.txt.
Problem:
We want to recover the flag from 2 modular residues f_0, f_1 and primes p_0, p_1.
But note the binarify function: takes the integer from bytes of flag, converts it to binary string, encodes that string to bytes, then int from those bytes. This is not a simple int(flag_bytes) but one extra step.
Goal:
Parse output.txt to get p_0, p_1, f_0, f_1.
Using CRT, recover binarify(flag).
Reverse binarify to get flag bytes.
User
continue
Memory:
Available files: ['chal.py', 'output.txt']
[Content of 'chal.py']:
from Crypto.Util.number import getPrime
import os
Validate flag starts with DDC{ and ends with } and length 18.
We have two residues mod two primes, so CRT will allow to get binarify(flag) mod p0 * p1.
Let's proceed to write the solve script.
TASK: write solve.py that:
Reads output.txt
Extracts p0, p1, f0, f1
Applies CRT to get binarify_flag
Reverses binarify by:
Converts binarify_flag to bytes representing the binary string of the integer representation of flag
Then converts that binary string back to int
Then converts to bytes (flag)
Make sure flag length 18 bytes and format DDC{...}
TASK COMPLETE
from sympy.ntheory.modular import crt
def parse_output(filename="output.txt"):
p = []
f = []
with open(filename) as file:
for line in file:
line = line.strip()
if line.startswith("p_"):
p.append(int(line.split(" = ")[1]))
elif line.startswith("f_"):
f.append(int(line.split(" = ")[1]))
return p, f
def invert_binarify(x):
# x = int.from_bytes(bin(int.from_bytes(m, "big")).encode(), "big")
# We want to invert this process.
# Step 1: Convert x to bytes
bin_bytes = x.to_bytes((x.bit_length() + 7) // 8, "big")
# Step 2: Decode bytes to get the binary string like b'101010...'
bin_str = bin_bytes.decode()
# Step 3: Parse binary string to integer
flag_int = int(bin_str, 2)
# Step 4: Convert int to bytes of flag (18 bytes)
flag_bytes = flag_int.to_bytes(18, "big")
return flag_bytes
def main():
p, f = parse_output()
# Use CRT to recover binarify(flag)
val, _ = crt(p, f) # val is integer representing binarify(flag)
flag = invert_binarify(val)
assert flag.startswith(b'DDC{') and flag.endswith(b'}') and len(flag) == 18
print("Recovered flag:", flag.decode())
if __name__ == "__main__":
main()
This script:
Reads primes and residues from output.txt.
Uses sympy's CRT to recover the binarified flag integer.
Inverts the binarify process.
Prints the recovered flag.
TASK COMPLETE
from sympy.ntheory.modular import crt
def parse_output(filename="output.txt"):
p = []
f = []
with open(filename) as file:
for line in file:
line = line.strip()
if line.startswith("p_"):
p.append(int(line.split(" = ")[1]))
elif line.startswith("f_"):
f.append(int(line.split(" = ")[1]))
return p, f
def invert_binarify(x):
# x = int.from_bytes(bin(int.from_bytes(m, "big")).encode(), "big")
# Reverse steps:
# 1. to bytes to get the encoded bin string
bin_bytes = x.to_bytes((x.bit_length() + 7) // 8, "big")
# 2. decode to string of '0' and '1'
bin_str = bin_bytes.decode()
# 3. convert binary string to int
flag_int = int(bin_str, 2)
# 4. to bytes (flag length is 18 bytes)
flag_bytes = flag_int.to_bytes(18, "big")
return flag_bytes
def main():
p, f = parse_output()
val, _ = crt(p, f)
flag = invert_binarify(val)
assert flag.startswith(b'DDC{') and flag.endswith(b'}') and len(flag) == 18
print("Recovered flag:", flag.decode())
if __name__ == "__main__":
main()