greyctf23-ot

GreyCTF 2023 Challenge 'OT'
git clone https://git.sinitax.com/sinitax/greyctf23-ot
Log | Files | Refs | sfeed.txt

solve.py (3059B)


      1from pwn import *
      2from math import lcm
      3from Crypto.Util.number import isPrime, long_to_bytes
      4
      5def checkN(N):
      6    if (N < 0):
      7        return "what?"
      8    if (N.bit_length() != 4096):
      9        return "N should be 4096 bits"
     10    if (isPrime(N) or isPrime(N + 23)):
     11        return "Hey no cheating"
     12    return None
     13
     14def xor(a, b):
     15    return bytes([i ^ j for i,j in zip(a,b)])
     16
     17def decrypt(key, msg):
     18    key = hashlib.shake_256(long_to_bytes(key)).digest(len(msg))
     19    return xor(key, msg)
     20
     21# generated with: openssl dhparam 4091
     22# might take multiple tries to hit 4096-bit N
     23p = """
     2407:79:28:c9:e4:cf:f8:bb:ee:5e:88:73:32:40:88:
     2508:2f:85:d8:e5:94:d0:c9:9b:ea:29:73:3b:6a:a7:
     261d:ff:f7:c6:a2:96:cf:b9:a4:2f:a8:b1:26:ab:ec:
     2715:ae:14:06:89:ee:67:1e:55:de:89:db:b6:61:41:
     285d:ff:0c:09:3f:04:09:85:98:e5:5c:3e:95:a2:5b:
     29a5:e9:0e:8b:4e:0a:7b:ea:eb:4b:d3:5f:10:fd:2e:
     30a6:8b:2a:91:21:a2:b5:c0:95:74:67:69:9c:76:95:
     315d:8c:a5:db:a7:1a:b1:9b:84:35:95:98:fd:77:41:
     3205:68:da:84:0b:13:d4:bb:69:05:6e:92:57:f1:6a:
     33da:ed:7b:57:ae:dc:d0:96:9e:78:e5:35:cf:7c:1e:
     343b:26:db:10:0d:aa:85:56:ae:27:ca:ea:f6:ef:b7:
     35d7:9f:58:02:b3:83:73:18:7d:f2:af:7b:a2:ba:63:
     36de:79:d5:cf:f1:66:37:2c:cf:8b:39:dc:af:1b:9d:
     37c2:b5:60:78:f8:a6:45:e9:7d:4c:34:a3:ec:3a:4e:
     3864:13:a9:de:f0:94:c9:d9:c8:d1:ee:e3:41:09:f1:
     3994:1c:e0:8e:00:9d:d3:80:51:84:37:2d:08:07:e6:
     40cb:83:d4:43:de:1c:84:ae:81:1a:04:9c:4b:3b:27:
     417e:2a:aa:ac:e4:62:4f:ce:ee:5b:38:d0:cc:48:d3:
     425e:17:fe:b7:83:98:4b:9f:8e:55:aa:c6:98:c3:66:
     43e9:10:eb:e9:28:9d:b8:a2:90:64:4a:24:bc:ea:d7:
     440c:19:7f:6b:ae:5f:ea:03:25:0d:1e:ac:e1:7f:98:
     4518:2f:19:99:81:ae:79:29:67:5d:08:22:f7:59:54:
     46d0:07:07:30:3b:52:6a:3b:de:11:75:a7:f4:28:fc:
     4720:f4:be:f3:a0:6e:b4:2a:d6:20:26:21:61:54:02:
     4861:8c:c6:6b:73:43:46:23:57:5a:39:67:c2:95:2b:
     49dd:1b:4a:f1:94:50:c4:77:40:a7:30:31:ee:6e:3e:
     506b:5a:51:fe:0a:e7:d1:80:98:c6:12:28:00:8a:94:
     5143:bb:9e:38:bb:24:6f:3c:2f:04:e3:71:4b:48:85:
     52b8:08:4d:f3:12:73:47:a1:38:2d:85:22:d3:dd:81:
     531a:ca:eb:42:df:cf:32:5a:e7:23:a1:98:04:08:a7:
     54ae:87:7b:4e:ff:c2:35:a1:04:23:c1:9d:ae:46:1e:
     5543:9f:e8:5d:a7:96:b0:72:09:db:a2:ad:3d:c8:fe:
     56e4:76:71:15:60:d9:ea:0e:d8:d0:c5:b7:0a:76:3b:
     5729:c3:94:34:8d:bd:75:cd:9a:d5:ac:f2:38:13:02:
     5835:bf
     59"""
     60p = int.from_bytes(bytes([int(v,16) for v in p.replace("\n", "").split(":")]), "big")
     61import math
     62print(math.log2(p))
     63assert(isPrime(p) and isPrime(p//2))
     64pp = p // 2 # 4090.? - 1 = 4089.?
     65
     66e = 0x10001
     67n = pp * 2 * 23 # 4089.? + 1 + 4.523 = 4094.? or 4095.?
     68n2 = p * 23
     69print(math.log2(n))
     70assert(checkN(n) is None)
     71
     72while True:
     73    io = process("python3 ../chall/main.py".split())
     74    io.sendline(str(n).encode())
     75    io.readuntil(b" = ");
     76    r1 = int(io.readline())
     77    io.readuntil(b" = ");
     78    r2 = int(io.readline())
     79    io.readuntil(b" = ");
     80    flag = bytes.fromhex(io.readline().strip().decode())
     81
     82    carmichael = lambda *a: lcm(*[v-1 for v in a])
     83
     84    d1 = pow(e, -1, carmichael(pp, 2, 23))
     85    k1 = pow(r1, d1, n)
     86
     87    d2 = pow(e, -1, carmichael(p, 23))
     88    k2 = pow(r2, d2, n2)
     89
     90    out = decrypt(k1 ^ k2, flag)
     91    if b"grey" in out:
     92        print(out)
     93        break
     94
     95    io.close()
     96