Cyber Apocalypse 2023: Inside the matrix
A writeup on Inside the matrix
- Given materials: Get it here!
- Description: As you deciphered the Matrix, you discovered that the astronomy scientist had observed that certain stars were not real. He had created two 5x5 matrices with values based on the time the stars were bright, but after some time, the stars stopped emitting light. Nonetheless, he had managed to capture every matrix until then and created an algorithm that simulated their generation. However, he could not understand what was hidden behind them as he was missing something. He believed that if he could understand the stars, he would be able to locate the secret tombs where the relic was hidden.
- Category: Crypto - Easy
The server script is shown below:
|
|
Problem statement
The code defines a class Book
that is used to generate a key matrix and encrypt a message using matrix multiplication. The matrix is generated randomly each time a message is encrypted, and its size is fixed at $5\times 5$. The program encrypts a flag, stored in FLAG
, using the Book
class and presents a menu to the user to interact with the encrypted flag.
The main function of the code presents a menu to the user with three options:
[L]ook at page
: displays the ciphertext and key matrix for the current page number. Here is an example output when you choose this option:
|
|
-
[T]urn page
: generates a new key matrix and ciphertext for the next page number. -
[C]heat
: displays the ciphertext and key matrix in list type. The Cheat output of above example page is:
|
|
Initial analysis
Primes
Prime $p$ is changed whenever Turn page
option is chosen. Though we don’t know what $p$ is, we know that it would be from 16 to 64. There are 12 primes in this range, which are $17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61$.
The encryption
It’s just a multiplication between two $5 \times 5$ matrixs over the field of integers modulo $p$:
$$C \equiv M\times K (\text{mod } p)$$
$$\Leftrightarrow M \equiv C\times K^{-1} (\text{mod } p)$$
Conclusion
We already have key $K$ and ciphertext $C$ by using Cheat option
. Then if we know $p$, we can easily recover message $M$ in modulus $p$. Because $p$ is changeable, we can gather several pairs $(M_i, p_i)$ where $i \geq 2$.
Solution method
Suppose there are some entries in a key $K_1$ which are larger than 59, then $p_1$ must be 61.
Suppose all entries in a key $K_2$ are smaller than 17, then it’s likely that $p_2$ is 17.
If we have $K_1$ and $K_2$, then we can recover $M_1$, $M_2$. By applying CRT (Chinese Remainder Theorem) for 2 pairs $(M_1, 61)$ and $(M_2, 17)$, we can get $M$ in modulus $61\times 17 = 1037$. Because every entries of the actual message’s matrix are bytes, they would be smaller than 128 (which is much smaller than 1037). This means our $M$ is actually the message itself.
So our mission is just to find $C_1, C_2$ by using Turn page
many times. Here is the script after we gather enough materials (I used $p_1=61$ and $p_2=19$):
|
|
Results
The Flag: HTB{l00k_@t_7h3_st4rs!!!}