defkeyGen(n, gamma): g = getPrime(round(n * gamma)) whileTrue: a, b = 2, 2 while GCD(a, b) != 1: a = getRandomNBitInteger(round((.5 - gamma) * n - 1)) b = getRandomNBitInteger(round((.5 - gamma) * n - 1)) p, q, h = 2 * g * a + 1, 2 * g * b + 1, 2 * g * a * b + a + b if isPrime(p) and isPrime(q) and isPrime(h): break return p, q, g, a, b
p, q, g, a, b = keyGen(n, gamma) d = getPrime(round(n * beta)) e = inverse(d, 2 * g * a * b) print(f'N = {hex(p * q)}') print(f'e = {hex(e)}') flag = 'DubheCTF{{{:x}}}'.format(d & (2**128 - 1))
""" N = 0xbe9ccc83003bedf45421b58377b946f87dfd85be82124dc5d732070d77ef68e0231c3f34dc803a8984de0573db6d83ccea0bd53a885059a10cfa3764c658c4d42c5fa90ecad8573fff8f2c41e513278c59121e42ad83310fb22b4d20e7ada42c76f08891f38c92a1b1aac712bfa7d717a4c4802ed023f12c768972ca1b e = 0x5dc97ed7250e57ce6fac4f57885c0538b1ea540fbaca79730470b6b990f7e861adc4c5fee3acdcd9ae9a2834b606ddfae01ade33edfa96a47a0ffc0036a4497a84c38b7cdac20c38f """
from Crypto.Util.number import * from tqdm import *
N = 0x4d1b4d1495283a766919e55365fd68ae1f729595401467b06fa67f7386be57ec089951679eaedbac8fddaf943828b5942d879e331d4583018049c7a6bbaed870e5db2a444730da0d550b3eb31c9cfa682cb43b5ea30fb4994ef2fbd6c317b0e8d2ea6fbd74de6ae357bd4c96ca95275b8efd211136c24b8968485b320f e = 0xf2d57267270670ebd61f5f0a62030c7a94bdfde353ddb8af46e9cf1a8ab907479ee46084465ff400c02e547572f4736826fbe43a6e3765ae18dfce95a4854100ab9e1085ae1d17 d = 0x17c8548e5b2330ab435c698c1db1ea9e150f13582cb0dc7202e7b
s = 4 #tau = (2*gamma - 8*beta + 1) / (4 * beta) #actually for the task' params tau<0 ,so emm... t = 0
poly=[] monomials=set() S = [(i1,i2,i3) for i2 inrange(s) for i3 inrange(s) for i1 inrange(2*(s-1)-i2-i3-t+1)] M = [(i1,i2,i3) for i2 inrange(s+1) for i3 inrange(s+1) for i1 inrange(2*s-i2-i3-t+1)]
from Crypto.Util.number import * from itertools import * from tqdm import *
N = 0xbe9ccc83003bedf45421b58377b946f87dfd85be82124dc5d732070d77ef68e0231c3f34dc803a8984de0573db6d83ccea0bd53a885059a10cfa3764c658c4d42c5fa90ecad8573fff8f2c41e513278c59121e42ad83310fb22b4d20e7ada42c76f08891f38c92a1b1aac712bfa7d717a4c4802ed023f12c768972ca1b e = 0x5dc97ed7250e57ce6fac4f57885c0538b1ea540fbaca79730470b6b990f7e861adc4c5fee3acdcd9ae9a2834b606ddfae01ade33edfa96a47a0ffc0036a4497a84c38b7cdac20c38f
gamma = 0.42 beta = 0.25
#fixed params n = 2 r = 1 R = [0,1,2] Gamma = [0,beta,1/2] miu = gamma E = inverse(e,N-1) X = int(N^beta) Y = int(N^(1/2))
#!/usr/bin/env python3 from socketserver import BaseRequestHandler,ThreadingTCPServer import random import os import string from hashlib import sha256 import signal import json from flag import flag
BANNER = br''' CCCCC RRRRRR CCCCC GGGG AAA MM MM EEEEEEE CC C RR RR CC C GG GG AAAAA MMM MMM EE CC RRRRRR CC GG AA AA MM MM MM EEEEE CC C RR RR CC C GG GG AAAAAAA MM MM EE CCCCC RR RR CCCCC GGGGGG AA AA MM MM EEEEEEE '''
def_recv(self, sz): try: r = sz res = b"" while r > 0: res += self.request.recv(r) if res.endswith(b"\n"): r = 0 else: r = sz - len(res) res = res.strip() except: res = b"" return res.strip(b"\n")
defcrc256(self,msg,IN,OUT,POLY): crc = IN for b in msg: crc ^= b for _ inrange(8): crc = (crc >> 1) ^ (POLY & -(crc & 1)) return (crc ^ OUT).to_bytes(32,'big')
defsetup(self): self.send(BANNER)
defhandle(self): signal.alarm(120) ifnot self.proof_of_work(): return # initial IN = random.getrandbits(N) OUT = random.getrandbits(N) POLY = random_poly()
for i inrange(5): self.send(b"what do you want to do?") self.send(b"1.calculate crc") self.send(b"2.getflag") self.send(b"3.exit") try: choice = self.recv(5).decode() if choice == '1': self.send(b"Give me your message") msg = self.recv(100) crc_hex = self.crc256(msg,IN,OUT,POLY).hex() self.send(b"Here is your crc: "+crc_hex.encode()) elif choice == '2': flag_crc = self.crc256(flag,IN,OUT,POLY).hex() self.send(b"Here is your flag: "+flag_crc.encode()) else: self.close() return except: self.send(b"error") pass
if __name__ == '__main__': HOST, PORT = "0.0.0.0", 10000 server = ThreadingTCPServer((HOST, PORT), Task) server.allow_reuse_address = True server.serve_forever()
题目基于CRC256,只是做了如下改动:
1 2 3 4 5 6 7
defcrc256(self,msg,IN,OUT,POLY): crc = IN for b in msg: crc ^= b for _ inrange(8): crc = (crc >> 1) ^ (POLY & -(crc & 1)) return (crc ^ OUT).to_bytes(32,'big')
defcrc256(msg,IN,OUT,POLY): crc = IN for b in msg: crc ^^= b for _ inrange(8): crc = (crc >> 1) ^^ (POLY & -(crc & 1)) returnint(crc ^^ OUT).to_bytes(32,'big').hex()
FF = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF IN = random.getrandbits(N) OUT = random.getrandbits(N) POLY = random_poly()
POLY_rev = int("10100100000011011100100010000011101000101010011101010110011011001110010010100011011111001111011011001010110111001000000100001000011101000100000010100111011000010001100100011000000001100010000110000011111110001111001111100001001110111101001100001000110011001"[::-1],2) G = i2p(POLY_rev) I = i2p(FF ^^ IN) Y = i2p(FF) Z = i2p(OUT ^^ FF) n = 256 b = len(msg1)*8 Y = rev(Y, n) I = rev(I, n) Z = rev(Z, n)
M1 = i2p(int.from_bytes(msg1, 'little')) M1 = rev(M1, b) crc1 = (M1 * x ^ n + (Y + I) * x ^ b + Y + Z) % G t1 = (crc1 - M1 * x ^ n - Y*x^b - Y) % G crc1 = rev(crc1, n) print(t1)
print()
M2 = i2p(int.from_bytes(msg2, 'little')) M2 = rev(M2, b) crc2 = rev(i2p(c2),n) print(crc2) crc2 = (M2 * x ^ n + (Y + I) * x ^ b + Y + Z) % G print(crc2) t2 = (crc2 - M2 * x ^ n - Y*x^b - Y) % G crc2 = rev(crc2, n) print(t2)
print()
b = 10*8 M3 = i2p(int.from_bytes(msg2, 'little')) M3 = rev(M3, b) print(M3 % G) T = GF(2^256, 'x', modulus = G) enc = rev(i2p(c3),n) cc = T(enc - Y*x^b - Y - t1) * (T(x^n)^(-1)) print(cc)
from Crypto.Util.number import * from tqdm import * from hashlib import * import string from pwn import *
#context.log_level = 'debug'
sh = remote("1.95.38.136",10000)
#part1 proof defproof_of_work(): table = string.digits + string.ascii_letters temp = sh.recvuntil(b"sha256(XXXX+") temp = sh.recvline() suffix = temp[:16].decode() hex1 = temp[20:].strip().decode() for i in tqdm(table): for j in table: for k in table: for m in table: temp1 = i+j+k+m if(sha256((temp1+suffix).encode()).hexdigest() == hex1): sh.send(temp1.encode()) return proof_of_work()
#get msg sh.recvuntil(b"3.exit") sh.sendline(b"1") sh.recvuntil(b"Give me your message") sh.sendline(b"1"*42) sh.recvuntil(b"Here is your crc: ") print(sh.recvline())
sh.recvuntil(b"3.exit") sh.sendline(b"1") sh.recvuntil(b"Give me your message") sh.sendline(b"2"*42) sh.recvuntil(b"Here is your crc: ") print(sh.recvline())
sh.recvuntil(b"3.exit") sh.sendline(b"1") sh.recvuntil(b"Give me your message") sh.sendline(b"3"*42) sh.recvuntil(b"Here is your crc: ") print(sh.recvline())
sh.recvuntil(b"3.exit") sh.sendline(b"2") sh.recvuntil(b"Here is your flag: ") print(sh.recvline())
ttt = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF POLY_rev = int("10100100000011011100100010000011101000101010011101010110011011001110010010100011011111001111011011001010110111001000000100001000011101000100000010100111011000010001100100011000000001100010000110000011111110001111001111100001001110111101001100001000110011001"[::-1],2) G = i2p(POLY_rev) Y = i2p(ttt) n = 256 Y = rev(Y, n)
ttt = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF POLY_rev = int("10100100000011011100100010000011101000101010011101010110011011001110010010100011011111001111011011001010110111001000000100001000011101000100000010100111011000010001100100011000000001100010000110000011111110001111001111100001001110111101001100001000110011001"[::-1],2) G = i2p(POLY_rev) Y = i2p(ttt) n = 256 Y = rev(Y, n)
from sage.allimport * from secret import flag from hashlib import sha256 from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.number import getPrime from random import getrandbits
p = getPrime(256) G = Zmod(p**3) M = Matrix(G,[G.random_element() for i inrange(64)],ncols=8) a = getrandbits(560) b = getrandbits(560) S = M ** (a * b)
from hashlib import sha256 from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.number import getPrime from random import getrandbits
#gen data p = 79008119711208495443423312926395331665944721527891616265679009115440018598629 G = Zmod(p**3) M = Matrix(G,[G.random_element() for i inrange(64)],ncols=8) a = getrandbits(590) b = getrandbits(590) Ma = M^a Mb = M^b S = M ** (a * b) print(a) print(b)
#solve print() g = M.det() ya = Ma.det() yb = Mb.det()
G = Zmod(p^3) M = Matrix(G,M) Ma = Matrix(G,Ma) Mb = Matrix(G,Mb)
#get eigenvalue in Zmod(p^3) PR.<x> = PolynomialRing(G) f = M.charpoly() fa = Ma.charpoly() fb = Mb.charpoly() g = hensel_lifting(f,p) ya = hensel_lifting(fa,p) yb = hensel_lifting(fb,p)
#part3 crt a = crt([xa1,xa2],[p^2,order]) b = crt([xb1,xb2],[p^2,order])
for i inrange(10): if(M^a != Ma): a += order * p^2 continue S = Mb^a key = sha256(S.str().encode()).digest() flag = AES.new(key, AES.MODE_ECB).decrypt(long_to_bytes(ct)) print(flag) break
defget_matrix(density): P = identity_matrix(ZZ, dims) for _ inrange(2): Li = identity_matrix(ZZ, dims) Ui = identity_matrix(ZZ, dims) for i inrange(dims): for j inrange(i + 1, dims): Li[i, j] = ternary_with_density(density) Ui[j, i] = ternary_with_density(density) P *= Li * Ui return P
dims = 190 bits = 20 density = 30 p = random_prime(2**bits, lbound=2**(bits-1)) q = random_prime(2**bits, lbound=2**(bits-1)) N = p * q
X = get_matrix(density) B = get_matrix(density) limit = round(2.3 * RR(mean([abs(vi) for vi in (X * B).list()]))) K = random_matrix(ZZ, dims, x=-limit, y=limit+1)
Y = random_matrix(Zmod(N), dims).change_ring(ZZ) C = p * random_matrix(Zmod(q), dims).change_ring(ZZ) A = (X.inverse() * (K - Y * C)).change_ring(ZZ)
secret = get_matrix(density)[0] flag = 'DubheCTF{' + md5(str(secret).encode()).hexdigest() + '}'
r = random_vector(ZZ, dims, x=-N, y=N) t = get_matrix(density)[0] cipher = secret * A + r * C + t data = str(cipher.list()) + '\n' + str(X.list()) + '\n' + str(B.list()) + '\n' + str(C.list()) + '\n' + str(K.list()) open('./cipher.txt', 'w').write(data)