Path: blob/master/RSA-encryption/Intro-Challenges/Challenge-14/exploit.py
1402 views
from Crypto.Util.number import *1from pwn import *23def _encrypt(message):4r.recvuntil("choice: ")5r.sendline("1")6r.recvuntil("encrypt (in hex): ")7r.sendline(message.encode("hex"))8ct = r.recvline().strip()[37:].decode("hex")9r.recvline()10r.recvline()11return ct1213def extractmod_eunknown(_encrypt, limit=4):14"""15Reference: https://crypto.stackexchange.com/questions/43583/deduce-modulus-n-from-public-exponent-and-encrypted-data1617Function to extract the value of modulus without the value of public key exponent1819:input parameters:20_encrypt : <type 'function'> : Function interacting with the server for encryption21limit : <type 'int'> : number of values to be sent for encryption22"""23try:24assert limit <= 425except AssertionError:26print "[+] Limit too big!"27return -128try:29m_list = [2, 3, 5, 7]30ct_list = [bytes_to_long(_encrypt(long_to_bytes(m_list[i]**2))) for i in range(limit)]31ct_list2 = [bytes_to_long(_encrypt(long_to_bytes(m_list[i]))) for i in range(limit)]32assert len(ct_list) == len(ct_list2)33mod_list = [(ct_list2[i]**2 - ct_list[i]) for i in range(limit)]34_gcd = mod_list[0]35for i in mod_list:36_gcd = GCD(_gcd, i)37return _gcd38except Exception as es:39print "[+] Exception: ", es40return -14142r = process("./run.sh")43N = extractmod_eunknown(_encrypt, 4)44print "N: ", N45assert N != -146r.sendline("2")47r.recvuntil("modulus: ")48r.sendline(str(N))49print r.recvline().strip()505152