TCTF/0CTF 2022 Crypto real_magic_dlog
Problem description
Based on HITCON 2021 "magic dlog". According to its author @lyc, the intended solution is "Finding Smooth Integers in Short Intervals Using CRT Decoding".
proof of work
1 | def proof_of_work(self): |
dlog
1 | def handle(self): |
Solution
proof of work
The proof of work part requires us to break the first 4 chars of a random 8 bytes string. We are given the condition where the SHA-256 of the string is shown.
For example, we are shown something like:
sha256(XXXX + n56#&U856iv#8vI3) == fb3d8b097f094662b84c90fd6faca83a385a075dc2e25f477b379da7408dac19 Give me XXXX:
We can just iterate all the possible combinations since the complexity isn't too large. It's just a PoW anyway.
The interactive code is below.
1 | from hashlib import sha256 |
dlog
The expected solution should be from this paper: Finding Smooth Integers in Short Intervals Using CRT Decoding Although, in the description, the challenge mentions that this problem is developed from HITCON 2021 "magic dlog". I searched on the internet and found a solution for HITCON 2021 version. By slightly adjusting the code, the solution can be applied to this challenge.
1 | from Crypto.Util.number import * |
We still want to look at how this works.
The challenge first generates a random 17 bytes (136 bits) and
converts it to long integer as a magic_num \(m\). We are given the hex of this number
and expected to find a proper set of \(\{P,\;
E,\; {\rm data}\}\) such that: \[
\begin{aligned}
\lfloor {P \over {2^{248}}} \rfloor = m\; ;\\
P \; {\rm is \; prime.}
\end{aligned}
\] It also means that the first 136 bits of \(P\) should be exactly equal to bits in
\(m\). Since \(P\) is prime, \(P-1\) can be \(m
\cdot 2^{248} + i\), where \(i \in
[2^{247}, 2^{248})\) is an even number.
Here, I learned a trick from the solution. We can actually only iterate the highest 16 bits after 136 bits and let all the rest be 0's. It will make the factorization of \(P-1\) way easier and faster.
Also, there's another condition on this set. Let data2
be hexdigest of sha384 of the input data. num1
and num2 are integer converted from data and
data2. The other constraints on \(\{P,\; E,\; {\rm data}\}\) are: \[
\begin{aligned}
n_{1}^{E} &\equiv n_{2} \mod P \\
n_{1} &= {\rm int(data, 16)}\\
n_{2} &= {\rm int(sha384(data), 16)}
\end{aligned}
\] It can be clearly seen that this is a Discrete Logarithm (DL)
problem. The core method for this kind of problem is to find a enough
smooth \(P\) so that \(E\) can be easily/quickly obtained by
calling discrete_log from SageMath (I'll write a sum-up
later for these maths/cryptanalysis tools). The smoothness can be
restricted by conditioning all factors of rank of \(P\) (which is \(P-1\) since \(P\) is prime) to be no greater than 40 (or
50 if it doens't work).
The detailed explaination can be found here (in Chinese). This kind of algorithm is called Pohlig-hellman. The conditions of applying Pohlig-hellman to find DLP solution in limited time is:
\[g^{x} \equiv a \mod P,\; {\rm g\;is\;the\;primitive\;root\;of\;P.}\] Thus, we can set \(n_{1}\) to be the primitive root of \(P\).
Reference
Hitcon2021 Crypto Writeup 离散对数 Finding Smooth Integers in Short Intervals Using CRT Decoding