{{ message }}

Instantly share code, notes, and snippets.

# saurav3199/titanic.py

Created Jul 5, 2020
titanic solver script for challenge in asis-ctf-2020
 #!/usr/bin/python3 # challenge titanic # source asis-ctf-2020 import hashlib import uuid from pwn import * from Crypto.Util.number import * import string charset = [ord(i) for i in string.printable] charset.sort() def front(lis): ans = [] lis = lis[::-1] carry = 0 for i in range(len(lis)): if carry == 1: for each in range(len(ans)): ans[each] = 9 lis[i]+=carry carry=0 if lis[i] in charset: ans.append(lis[i]) else: if lis[i]>126: ans.append(9) carry+=1 else: if lis[i] not in charset: for each in range(len(ans)): ans[each] = 9 while(lis[i] not in charset): lis[i]+=1 ans.append(lis[i]) if carry == 1: ans.append(9) return ans[::-1] def prev(lis): if len(lis) == 0: return [] elif len(lis) == 1 and lis[0] <9: return [-9] f=True ans = [] lis = lis[::-1] carry = 0 for i in range(len(lis)): lis[i]-=carry lis[i]%=256 carry=0 if lis[i] in charset: ans.append(lis[i]) else: if lis[i]<9: ans.append(126) carry+=1 else: if lis[i] not in charset: for each in range(len(ans)): ans[each] = 126 while(lis[i] not in charset): lis[i]-=1 ans.append(lis[i]) if(carry): ans.append(carry) return ans[::-1] def check_nearest(num): print(long_to_bytes(num)) x = num lis = [] while(x): lis.append(x%256) x = x//256 lis = lis[::-1] next_list = front(lis) string1 = "".join([chr(i) for i in next_list]) next_number = bytes_to_long(string1.encode()) prev_list = prev(lis) string2 = "".join([chr(i) for i in prev_list]) print(string1) print(string2) prev_number = bytes_to_long(string2.encode()) print("next_number " , next_number) print("prev_number " , prev_number) print("question " , lis) print("next_list " , next_list) print("prev_list " , prev_list) print(abs(prev_number -num)) print(abs(next_number-num)) return prev_number if abs(prev_number-num) < abs(next_number-num) else next_number r = remote("76.74.178.201", 8002) info = r.recvuntil("\n").decode("UTF-8").split(" ") print(info) algo = info[8].split("(")[0] target = info[10] length = int(info[-1]) print(f"[*] algo needed: {algo}") print(f"[*] target needed: {target}") print(f"[*] length needed: {length}") def random_ascii_generator(l): string = str(uuid.uuid4()).replace("-", "")[0:l].encode("ascii") return string[0:l] algo_matching = { "sha1": hashlib.sha1, "md5": hashlib.md5, "sha224": hashlib.sha224, "sha256": hashlib.sha256, "sha384": hashlib.sha384, "sha512": hashlib.sha512, } candidate_hash = "" while not candidate_hash[-6:] == target: candidate_string = random_ascii_generator(length) candidate_hash = algo_matching[algo](candidate_string).hexdigest() print("[*] PoW found") print(candidate_string) r.sendline(candidate_string) print(r.recvline()) print(r.recvline()) print(r.recvline()) print(r.recvline()) print(r.recvline()) print(r.recvline()) level = 1 while True: print("-------------------------------trying level {level} -----------------------------------------------") level+=1 try: response = r.recvline() print(response) temp = response.decode("UTF-8").split(" ")[6] temp =int(temp) except: pass print(temp,type(temp)) nearest = check_nearest(temp) print(nearest , "nearest",type(nearest)) r.sendline(str(nearest)) print(r.recvline())