👾
Elijah's CTF Blog
  • 👋Home
  • 🇲🇾Wargames.MY CTF 2024
    • Credentials (crypto)
    • Stones (rev)
    • Rick'S Algorithm (crypto)
    • Rick'S Algorithm 2 (crypto)
    • Hohoho 3 continue (crypto)
  • 🎄Advent of CTF 2024
    • Jingle Bell ROP (pwn)
    • help (pwn)
  • Backdoor CTF 24
    • [rev] Ratatouille
  • 🇭🇰HKCERT CTF 24
    • Shellcode Runner 3 + Revenge (pwn)
    • ISH (1) (pwn)
    • Cyp.ress (rev)
    • Void (rev)
  • 🇮🇹ECSC 2024
    • ➕OffTopic (crypto)
  • 🎩Greyhats WelcomeCTF 24
    • EE2026 (misc)
  • 🚆UIUCTF 24
    • Syscalls (pwn)
    • Summarize (rev)
    • X Marked the Spot (crypto)
    • Without a Trace (crypto)
    • Determined (crypto)
    • Naptime (crypto)
    • Snore Signatures (crypto)
  • 🪼Jelly CTF 24
    • Cherry (crypto)
    • the_brewing_secrets (crypto)
  • 👨‍🦯vsCTF 24
    • Dream (crypto)
    • Cosmic Ray V3 (pwn)
  • 😎AKASEC CTF 24
    • Warmup (pwn)
    • Good_trip (pwn)
    • Sperm Rev (rev)
    • Paranoia (rev)
    • Grip (rev)
    • Risks (rev)
    • Lost (crypto)
  • 😁L3AK CTF 24
    • oorrww (pwn)
    • angry (rev)
    • Related (crypto)
    • BatBot (web-misc)
    • Matrix Magic (crypto)
  • 🥹CDDC Qualifiers 2024
    • WASM (rev)
    • crashMe (pwn)
Powered by GitBook
On this page
  1. UIUCTF 24

Determined (crypto)

"It is my experience that proofs involving matrices can be shortened by 50% if one throws the matrices out." Emil Artin

Last updated 11 months ago

We are given the main code running on the server in server.py, and gen.py which generates an encrypted message using RSA. We are given the RSA modulus n, exponent e and ciphertext c.

# server.py
from Crypto.Util.number import bytes_to_long, long_to_bytes
from itertools import permutations
from SECRET import FLAG, p, q, r

def inputs():
    print("[DET] First things first, gimme some numbers:")
    M = [
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
    ]
    try:
        M[0][0] = p
        M[0][2] = int(input("[DET] M[0][2] = "))
        M[0][4] = int(input("[DET] M[0][4] = "))

        M[1][1] = int(input("[DET] M[1][1] = "))
        M[1][3] = int(input("[DET] M[1][3] = "))

        M[2][0] = int(input("[DET] M[2][0] = "))
        M[2][2] = int(input("[DET] M[2][2] = "))
        M[2][4] = int(input("[DET] M[2][4] = "))

        M[3][1] = q
        M[3][3] = r

        M[4][0] = int(input("[DET] M[4][0] = "))
        M[4][2] = int(input("[DET] M[4][2] = "))
        """
    M = [
        [p, 0, x, 0, x],
        [0, x, 0, x, 0],
        [x, 0, x, 0, x],
        [0, q, 0, r, 0],
        [x, 0, x, 0, 0],
    ]
        """
    except:
        return None
    return M

def handler(signum, frame):
    raise Exception("[DET] You're trying too hard, try something simpler")

def fun(M):
    def sign(sigma):
        l = 0
        for i in range(5):
            for j in range(i + 1, 5):
                if sigma[i] > sigma[j]:
                    l += 1
        return (-1)**l

    res = 0
    for sigma in permutations([0,1,2,3,4]):
        curr = 1
        for i in range(5):
            curr *= M[sigma[i]][i]
        res += sign(sigma) * curr
    return res

def main():
    print("[DET] Welcome")
    M = inputs()
    if M is None:
        print("[DET] You tried something weird...")
        return

    res = fun(M)
    print(f"[DET] Have fun: {res}")

if __name__ == "__main__":
    main()

# gen.py
from SECRET import FLAG, p, q, r
from Crypto.Util.number import bytes_to_long
n = p * q
e = 65535
m = bytes_to_long(FLAG)
c = pow(m, e, n)
# printed to gen.txt
print(f"{n = }")
print(f"{e = }")
print(f"{c = }")

server.py imports the RSA primes p and q , along with r (but r is useless). main() first calls inputs(), which prompts us for entries to a matrix M. After inputting our values, M looks like this:

M=[p0x00x10x20x30x40x50x60q0r0x70x800]M=\begin{bmatrix} p & 0 & x_0 & 0 & x_1\\ 0 & x_2 & 0 & x_3 & 0\\ x_4 & 0 & x_5 & 0 & x_6 \\ 0 & q & 0 & r & 0 \\ x_7 & 0 & x_8 & 0 & 0 \end{bmatrix}M=​p0x4​0x7​​0x2​0q0​x0​0x5​0x8​​0x3​0r0​x1​0x6​00​​

where all the xxx values are our controlled inputs.

fun(M) is then printed. fun(M) is essentially using a permutation method to compute the determinant of a matrix. Let's compute the determinant of M using cofactor expansion:

Along the fourth row:

∣p0x00x10x20x30x40x50x60q0r0x70x800∣=q∣px00x100x30x4x50x6x7x800∣+r∣p0x0x10x200x40x5x6x70x80∣\begin{vmatrix} p & 0 & x_0 & 0 & x_1\\ 0 & x_2 & 0 & x_3 & 0\\ x_4 & 0 & x_5 & 0 & x_6 \\ 0 & q & 0 & r & 0 \\ x_7 & 0 & x_8 & 0 & 0 \end{vmatrix}=q\begin{vmatrix} p & x_0 & 0 & x_1\\ 0 & 0 & x_3 & 0 \\ x_4 & x_5 & 0 & x_6\\ x_7 & x_8 & 0 & 0 \end{vmatrix} + r\begin{vmatrix} p & 0 & x_0 & x_1\\ 0 & x_2 & 0 & 0 \\ x_4 & 0 & x_5 & x_6 \\ x_7 & 0 & x_8 & 0 \end{vmatrix}​p0x4​0x7​​0x2​0q0​x0​0x5​0x8​​0x3​0r0​x1​0x6​00​​=q​p0x4​x7​​x0​0x5​x8​​0x3​00​x1​0x6​0​​+r​p0x4​x7​​0x2​00​x0​0x5​x8​​x1​0x6​0​​

Along second row for both:

=−qx3∣px0x1x4x5x6x7x80∣+rx2∣px0x1x4x5x6x7x80∣=-qx_3\begin{vmatrix} p & x_0 & x_1\\ x_4 & x_5 & x_6\\ x_7 & x_8 & 0 \end{vmatrix} + rx_2\begin{vmatrix} p & x_0 & x_1\\ x_4 & x_5 & x_6\\ x_7 & x_8 & 0 \end{vmatrix}=−qx3​​px4​x7​​x0​x5​x8​​x1​x6​0​​+rx2​​px4​x7​​x0​x5​x8​​x1​x6​0​​
=(rx2−qx3)(x0x6x7+x1x4x8−x1x5x7−px6x8)=(rx_2-qx_3)(x_0x_6x_7+x_1x_4x_8-x_1x_5x_7-px_6x_8)=(rx2​−qx3​)(x0​x6​x7​+x1​x4​x8​−x1​x5​x7​−px6​x8​)

Observing this expression closely, by controlling the different xxx values, we can obtain qqq alone. We can do this by setting x0=x2=x4=x6=x8=0x_0=x_2=x_4=x_6=x_8=0x0​=x2​=x4​=x6​=x8​=0 and x1=x3=x5=x7=1x_1=x_3=x_5=x_7=1x1​=x3​=x5​=x7​=1.

Providing these values to the server, we get that q = 12538849920148192679761652092105069246209474199128389797086660026385076472391166777813291228233723977872612370392782047693930516418386543325438897742835129. With q, we know n hence we can find p. We can then find phi and hence d. We can then find the plaintext m.

from Crypto.Util.number import long_to_bytes

n = 158794636700752922781275926476194117856757725604680390949164778150869764326023702391967976086363365534718230514141547968577753309521188288428236024251993839560087229636799779157903650823700424848036276986652311165197569877428810358366358203174595667453056843209344115949077094799081260298678936223331932826351
"""
    M = [
        [p, 0, x, 0, x],
        [0, x, 0, x, 0],
        [x, 0, x, 0, x],
        [0, q, 0, r, 0],
        [x, 0, x, 0, 0],
    ]
"""
q = 12538849920148192679761652092105069246209474199128389797086660026385076472391166777813291228233723977872612370392782047693930516418386543325438897742835129

assert n % q == 0
p = n // q
tot = (p - 1) * (q - 1)
e = 65535
c = 72186625991702159773441286864850566837138114624570350089877959520356759693054091827950124758916323653021925443200239303328819702117245200182521971965172749321771266746783797202515535351816124885833031875091162736190721470393029924557370228547165074694258453101355875242872797209141366404264775972151904835111
d = pow(e, -1, tot)
m = pow(c, d, n)
print(long_to_bytes(m)) # uiuctf{h4rd_w0rk_&&_d3t3rm1n4t10n}
🚆
2KB
server.py
211B
gen.py