diff --git a/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/__init__.py b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/answer_pallier.py b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/answer_pallier.py new file mode 100644 index 0000000..7188160 --- /dev/null +++ b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/answer_pallier.py @@ -0,0 +1,93 @@ +#!/usr/bin/python + +__author__ = "bt3gl" +__email__ = "bt3gl@gmail.com" + +import decimal +import socket +from constants import mod + +def print_hex(secret): + + # cutting L in the end + a = hex(secret)[:-1] + + # cutting the \x symbol + b = a[2:].decode('hex') + + return b + + + +def convolution(e1, e2, m2, mod): + + return (e1 * e2 )%(mod*mod) + + + +def nc_paillier(mod): + + # create socket + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((HOST, PORT)) + + + # answer the initial question + s.recv(4096) + s.send(b'paillier') + s.recv(4096) + m = s.recv(4096) + m = (m.split(": ")[1]).split('\n')[0] + mdec = decimal.Decimal(m) + + + # encrypt 1 + e = '1' + m2 = decimal.Decimal(e) + s.send(b'E') + s.recv(4096) + s.send(e) + e2 = s.recv(4096) + e2 = e2.split(": ")[1] + e2dec = decimal.Decimal(e2) + + + + # convolute the enc messages + answer = convolution(mdec, e2dec, m2, mod) + + + # get the description from the answer + s.send(b'D') + s.recv(4096) + s.recv(4096) + s.send(str(answer)) + md = s.recv(4096) + md = md.split(": ")[1].strip() + + + + # get the flag, remember to add d(e(1)) = 1 + secret = long(md) + 1 + flag = print_hex(secret) + print("The flag is: ") + print flag + + + + + +if __name__ == "__main__": + + # really long numbers + decimal.getcontext().prec = 1240 + + PORT = 12445 + HOST = 'asis-ctf.ir' + + nc_paillier(mod) + + + + + diff --git a/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/binary_search.py b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/binary_search.py new file mode 100644 index 0000000..201c7c9 --- /dev/null +++ b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/binary_search.py @@ -0,0 +1,62 @@ +#!/usr/bin/python + +__author__ = "bt3gl" +__email__ = "bt3gl@gmail.com" + + +import decimal +import socket + + + + +def bs_paillier(lo, hi, s): + if hi < lo: return None + mid = (hi + lo) // 2 + print("We are at: ") + print(mid) + + s.send(b'E') + s.recv(4096) + s.send(str(mid)[:-1]) + ans = s.recv(4096) + + if 'None' in ans: + print "Found it!" + return mid + 1 + elif 'Your secret' in ans: + return bs_paillier(lo, mid-1, s) + else: + return bs_paillier(mid+1, hi, s) + + + + +def get_mod_paillier(): + + # create socket, answer first question + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((HOST, PORT)) + s.recv(4096) + s.send(b'paillier') + s.recv(4096) + + # start binary search + hi = pow(11,307) + lo = pow(10,307) + mod = bs_paillier(lo, hi, s) + print mod + + + +if __name__ == "__main__": + + PORT = 12445 + HOST = 'asis-ctf.ir' + + get_mod_paillier() + + + + + diff --git a/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/constants.py b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/constants.py new file mode 100644 index 0000000..c552b2f --- /dev/null +++ b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/constants.py @@ -0,0 +1,4 @@ +import decimal + +#mod = decimal.Decimal('28414252178421170251910042147689368446511995764529924640302525001150082023847807850723344018358163485930344707394699713532305322202216635189806057491940857063203918400016948903778913790645080815377317956510275921513980400357522675326929234601338948461475089468134681894547897715946279673410647104276477333937') +mod = decimal.Decimal('17671943390317527594740575037779239788090749028363849573873871285525785364877468659238291287413782918855995881353189626069716161186805808731291508724925847487655603905895106750055611619881911787280882269077856999823769344599404478814635216943095238063240285592085964648122007040660676934950342692770738186633') diff --git a/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/nc_paillier.py b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/nc_paillier.py new file mode 100644 index 0000000..34eee99 --- /dev/null +++ b/CTFs_and_WarGames/2014-ASIS-CTF/crypto_paillier/nc_paillier.py @@ -0,0 +1,85 @@ +#!/usr/bin/python + +__author__ = "bt3gl" +__email__ = "bt3gl@gmail.com" + + +import decimal +import socket + + +def nc_paillier(): + + # create socket + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((HOST, PORT)) + + + # answer the initial question + print s.recv(4096) + s.send(b'paillier') + + + # get the secret + print s.recv(4096) + m = s.recv(4096) + + # cleaning it + m = (m.split(": ")[1]).split('\n')[0] + + # it's good to print (because it changes periodically) + print("The secret is: ") + print(m) + + # change from str to long decimal + mdec = decimal.Decimal(m) + + + ''' + From here you can do whatever you want. + ''' + # If you want to encrypt messages + + msg_to_e = '1' + + s.send(b'E') + print s.recv(4096) + s.send(msg_to_e) + me = s.recv(4096) + me = me.split(": ")[1] + + print("Secret for %s is:" %(msg_to_e)) + print(me) + + medec = decimal.Decimal(me) + + + # If you want to decrypt messages + + msg_to_d = me + + s.send(b'D') + s.recv(4096) + s.recv(4096) + s.send(msg_to_d) + md = s.recv(4096) + md = md.split(": ")[1].strip() + + print("Decryption is: ") + print(md) + + mddec = decimal.Decimal(md) + + + + + +if __name__ == "__main__": + # really long numbers + decimal.getcontext().prec = 1240 + + PORT = 12445 + HOST = 'asis-ctf.ir' + + nc_paillier() +