AINT NO HILL HIGH ENOUGH….
Here’s the Python source:
1 | import secrets |
Let’s walk through exactly what’s happening.
Step 1
1 | flag = [alphabet.index(a) for a in flag] |
Here, the flag is converted such that each byte becomes the byte’s index in the string “alphabet”, which is of length 75. Then, the flag is padded so that its length is a multiple of 3. For example, if the flag is “tjctf{f4k3_fl46}”, it might look something like this:
1 | [19, 9, 2, 19, 5, 62, 5, 58, 10, 57, 64, 5, 11, 58, 61, 63, 8663296688968513193, 1557885647606936801] |
Step 2
1 | rows = list(zip(flag[::3], flag[1::3], flag[2::3])) |
This operation (by just using a sample flag and running it) just splits the flag into groups of 3 bytes. Let’s use the same example from before:
1 | [(19, 9, 2), (19, 5, 62), (5, 58, 10), (57, 64, 5), (11, 58, 61), (63, 8663296688968513193, 1557885647606936801)] |
Step 3
1 | def det(matrix): |
This is the key generation function. All it does is generate 9 random nonnegative integers below the modulus for a 3x3 matrix. The resultant matrix’s determinant is ensured to not be 0.
Let’s say our key is this:
1 | [ |
Step 4
1 | def dot(a,b): |
Finally, we come to the encryption step. Note that the sum()
function does not actually serve any purpose other than to flatten the ciphertext array down to a 1-dimensional array.
During the actual encryption, we iterate through all the rows of the flag bytes. For each row, we iterate through all 3 rows of the key matrix, computing the dot product for each one. We then return the dot product results for each row and key combination.
Let’s do it for the first row to see what’s happening:
1 | row = rows[0] = (19, 9, 2) |
This repeats itself for all the rows to return this:
1 | [ |
This gets flattened to become this array:
1 | [12832180388573769750, 1476760410063303054, 217890474307251127, 8533544897980456625, 6137609943828277883, 2615492936662095429, 2490386410249040115, 1432864670580339382, 14679230802994899213, 5295816249959299131, 2608450589917129197, 10284357590072520321, 11378953428569071954, 7075887412816480567, 6517782098267772777, 4220528819464674859, 6276519725400122300, 554722810896478431] |
Which is similar in form to what we were provided in out.txt.
So, we’re essentially just using a randomized key matrix, performing matrix multiplication between the flag byte matrix and key matrix, and returning the result. So… how can we break this?
System of equations…?
Well, take a look at the encryption operation:
1 | dot(key[0], row) = (11129846811433172972*19 + 264795847042417231*9 + 12786127660047114284*2) % modulus |
That’s… kind of like a system of equations, right? Except, there’s a slight problem here. In this, it seems like the variables are 19, 9, and 2, the bytes of the flag, and the key matrix values are the coefficients. If we can get the coefficients, we can pretty easily solve this system of equations, as we have 3 variables and 3 equations! But that means we need a total break of this system by recovering the key. How can we do that?
System of equations!!!
Well, systems of equations actually helps here once again. Consider the encryption of the first 3 rows of the flag bytes with only the first key matrix row. And, just so we don’t confuse ourselves, let’s stop using our example numbers and replace them with variables.
1 | dot(key[0], rows[0]) = (key[0][0]*19 + key[0][1]*9 + key[0][2]*2) % modulus |
Note that we know the flag bytes of the first two rows because we know the flag starts with the prefix “tjctf{“.
Now, notice that the key matrix and the flag bytes have switched positions! Now, the key bytes seem like the variables of the equation, while the flag bytes seem like the coefficients.
However, we have a slight problem. It is easy to solve a 3-variable 3-equation system of equations under a modulus/in an integer field by using some matrix functions provided by SageMath, but we’re missing 3 coefficients. How do we deal with that?
Well, conveniently, alphabet
is only 75 characters long. 75^3 is 421875, which is pretty small! Therefore, we can just brute force those 3 missing flag bytes!
Once we find the correct 3 flag bytes, we can easily use SageMath to solve the system of equations and recover the key matrix. From there, we can then turn back to the other system of equations, where the group of 3 flag bytes are the variables and the key matrix is the coefficients, and use the same exact SageMath function to solve the system of equations and get the flag bytes!
Perfect. Now we can just implement in Sage and solve!
It took me a while to debug, but here’s the implementation:
1 | from tqdm import trange |
wtf where’d the last bytes go D:
Yeah so… turns out the program isn’t perfect. It doesn’t decrypt the last group of 3 bytes. I realized that it’s probably because of the padding, i.e. the last byte or two is some random large number less than the modulus, and for some reason it’s screwing up the program.
So far, though, I had most of the flag:
tjctf{aint_no_hillllll_55e4$S56a356^#@
.
So how can we correct the program and get the full flag?
n a h
We know the end of the flag is ‘}’, and the last part of the 3 byte group is probably padding. That means there’s only one byte of the flag left that we don’t know!
Looking at the end of the flag we have so far, it seems likely that it’s going to be a special symbol. Sooooo I just started guessing.
tjctf{aint_no_hillllll_55e4$S56a356^#@!$}
:))