def handle(self): try: signal.signal(signal.SIGALRM, self.timeout_handler) signal.alarm(60) self.send(BANNER) if not self.proof_of_work(): self.send(b'\nWrong!') self.request.close() return self.send( b"It's just a decryption system. And I heard that only the Bytedancer can get secret." ) aes = AES_CFB() signal.alarm(300) for i in range(52): cipher_hex = self.recv( prompt=b'Please enter your cipher in hex > ') if len(cipher_hex) > 2048: self.send(b"It's too long!") continue try: cipher = codecs.decode(cipher_hex, 'hex') except: self.send(b'Not hex data!') continue if len(cipher) == 0 or len(cipher) % BLOCK_SIZE != 0: self.send( f'Cipher length must be a multiple of {BLOCK_SIZE}!'. encode()) continue plaintext = aes.decrypt(cipher) plaintext_hex = codecs.encode(plaintext, 'hex') self.send(b'Your plaintext in hex: \n%s\n' % plaintext_hex) if plaintext == b"Hello, I'm a Bytedancer. Please give me the flag!": self.send(b'OK! Here is your flag: ') self.send(FLAG.encode()) break self.send(b'Bye!\n') except TimeoutError: self.send(b'\nTimeout!') except Exception as err: self.send(b'Something Wrong!') finally: self.request.close()
def get_flag(req): global key req.sendall(b"Send ciphertext as number:\n") try: ciphertext = req.recv(1024).decode().strip() ciphertext = int(ciphertext) plaintext = pow(ciphertext, key.d, key.n) plaintext = long_to_bytes(plaintext) plaintext = plaintext.decode() if plaintext == "I want the flag :)": req.sendall(FLAG.encode() + b"\n") return except: pass req.sendall(b"Wrong :S\n")
def rsaquine(): total = 30 print '0CTF RSA Quine' print '%d Rounds in total to capture the flag.' % total bits = 16 for i in xrange(total): print 'Round %d' % (i+1) N,e,qn = gen_param(bits) bits += 7 print 'N = %d' % N print 'e = %d' % e print 'Total %d quine numbers' % qn if (qn <= 200): print 'Can you find them?' else: qn = 200 print 'Just Give me %d of them.' % qn quine = set() for j in xrange(qn): m = input_int('%d: ' % (j+1)) if m in quine: print 'Do not cheat!' exit() c = pow(m, e, N) if m == c: print 'Good Quine!' quine.add(m) else: print 'Bad Number!' exit() N,e,qn = gen_param(256) print 'You are a quine expert!' print 'N = %d' % N print 'e = %d' % e f = pow(int(FLAG.encode('hex'),16), e, N) print 'Flag = %d' % f print 'Good Luck & Have Fun!'
def rsaquine(): total = 30 print '0CTF RSA Quine' print '%d Rounds in total to capture the flag.' % total bits = 16 for i in xrange(total): print 'Round %d' % (i + 1) N, e, qn = gen_param(bits) bits += 7 print 'N = %d' % N print 'e = %d' % e print 'Total %d quine numbers' % qn if (qn <= 200): print 'Can you find them?' else: qn = 200 print 'Just Give me %d of them.' % qn quine = set() for j in xrange(qn): m = input_int('%d: ' % (j + 1)) if m in quine: print 'Do not cheat!' exit() c = pow(m, e, N) if m == c: print 'Good Quine!' quine.add(m) else: print 'Bad Number!' exit() N, e, qn = gen_param(256) print 'You are a quine expert!' print 'N = %d' % N print 'e = %d' % e f = pow(int(FLAG.encode('hex'), 16), e, N) print 'Flag = %d' % f print 'Good Luck & Have Fun!'
from flag import FLAG FLAG = int(FLAG.encode('hex'),16) P = 0xd3ceec4c84af8fa5f3e9af91e00cabacaaaecec3da619400e29a25abececfdc9bd678e2708a58acb1bd15370acc39c596807dab6229dca11fd3a217510258d1b A = 0x95fc77eb3119991a0022168c83eee7178e6c3eeaf75e0fdf1853b8ef4cb97a9058c271ee193b8b27938a07052f918c35eccb027b0b168b4e2566b247b91dc07 Gx = 0xcf634030986cf41c1add87e71d638b9cc723c764059cf4c9b8ed2a0aaf5d51dc770372503ebfaad746ab9220e992c09822916978226465ad31d354a3efee51da Gy = 0x65eaad8848b2787103fce02358b45d8a61420031989eb6b4b70d82fe20d85583ae542eb8f76749dc640b0f13f682228819b8b2f04bd7a5a17a4c675540fe1c90 assert FLAG < P def egcd(a, b): x,y, u,v = 0,1, 1,0 while a != 0: q, r = b//a, b%a m, n = x-u*q, y-v*q b,a, x,y, u,v = a,r, u,v, m,n gcd = b return gcd, x, y def inv(a): gcd, x, y = egcd(a % P, P) if gcd != 1: return -1 else: return x % P def add(a, b): if a == 0: return b if b == 0: return a
from flag import FLAG FLAG = int(FLAG.encode('hex'), 16) P = 0xd3ceec4c84af8fa5f3e9af91e00cabacaaaecec3da619400e29a25abececfdc9bd678e2708a58acb1bd15370acc39c596807dab6229dca11fd3a217510258d1b A = 0x95fc77eb3119991a0022168c83eee7178e6c3eeaf75e0fdf1853b8ef4cb97a9058c271ee193b8b27938a07052f918c35eccb027b0b168b4e2566b247b91dc07 Gx = 0xcf634030986cf41c1add87e71d638b9cc723c764059cf4c9b8ed2a0aaf5d51dc770372503ebfaad746ab9220e992c09822916978226465ad31d354a3efee51da Gy = 0x65eaad8848b2787103fce02358b45d8a61420031989eb6b4b70d82fe20d85583ae542eb8f76749dc640b0f13f682228819b8b2f04bd7a5a17a4c675540fe1c90 assert FLAG < P def egcd(a, b): x, y, u, v = 0, 1, 1, 0 while a != 0: q, r = b // a, b % a m, n = x - u * q, y - v * q b, a, x, y, u, v = a, r, u, v, m, n gcd = b return gcd, x, y def inv(a): gcd, x, y = egcd(a % P, P) if gcd != 1: return -1 else: return x % P def add(a, b): if a == 0:
L=2048-N-1 while True: q = getPrime(N) if q-1>x: break assert(q-1>x) while True: t = random.getrandbits(L) p = (t * 2*q + 1) if isPrime(p): break e = (p-1) // q g = pow(2, e, p) y = pow(g, x, p) print("genkey_finish!") return {'y':y, 'g':g, 'p':p, 'q':q, 'x':x} def sign(m, key): g, p, q, x = key['g'], key['p'], key['q'], key['x'] k = random.randint(1, q-1) Hm = int.from_bytes(hashlib.md5(str(m).encode('utf-8')).hexdigest().encode(), 'big') r = pow(g, k, p) % q s = (inverse(k, q) * (Hm + x*r)) % q return (r, s, k, q) if __name__ == "__main__": msg=int.from_bytes(FLAG.encode(),'big') rsaEncode(msg)
def __init__(self): self.gen = 0x44120dc98545c6d3d81bfc7898983e7b7f6ac8e08d3943af0be7f5d52264abb3775a905e003151ed0631376165b65c8ef72d0b6880da7e4b5e7b833377bb50fde65846426a5bfdc182673b6b2504ebfe0d6bca36338b3a3be334689c1afb17869baeb2b0380351b61555df31f0cda3445bba4023be72a494588d640a9da7bd16L self.q = 0x926c99d24bd4d5b47adb75bd9933de8be5932f4bL self.p = 0x80000000000001cda6f403d8a752a4e7976173ebfcd2acf69a29f4bada1ca3178b56131c2c1f00cf7875a2e7c497b10fea66b26436e40b7b73952081319e26603810a558f871d6d256fddbec5933b77fa7d1d0d75267dcae1f24ea7cc57b3a30f8ea09310772440f016c13e08b56b1196a687d6a5e5de864068f3fd936a361c5L self.key = int(FLAG.encode("hex"), 16)