# -*- coding:utf-8 -*- import os import random import string import hashlib import socketserver from Crypto.Util.number import isPrime, long_to_bytes, getStrongPrime, bytes_to_long
flag = b"D0g3{******************************************}"
class MyServer(socketserver.BaseRequestHandler): def proof(self): random.seed(os.urandom(8)) random_str = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(20)]) str_sha256 = hashlib.sha256(random_str.encode()).hexdigest() self.request.sendall(('SHA256(XXXX + %s):%s\n' % (random_str[4:], str_sha256)).encode()) self.request.sendall('Give Me XXXX:\n'.encode()) XXXX = self.request.recv(2048).strip()
if hashlib.sha256((XXXX + random_str[4:].encode())).hexdigest() != str_sha256: return False
return True
def getPQN(self): while True: p = getStrongPrime(2048) q = getStrongPrime(2048) n = p * q if p.bit_length() == 2048 and q.bit_length() == 2048 and n.bit_length() == 4096: return p, q, n
def encrypt(self): p, q, n = self.getPQN() m = bytes_to_long(flag) e = 0x10001 c = pow(m, e, n) p = bin(p)[2:] p1 = list(p[:1024]) p2 = list(p[1024:]) p1[random.choice([i for i, c in enumerate(p1) if c == '1'])] = '0' p2[random.choice([i for i, c in enumerate(p1) if c == '0'])] = '1' return n, ''.join(p1) + ''.join(p2), c
def handle(self): if not self.proof(): self.request.sendall(b'Error Hash!') return n, p, c = self.encrypt() self.request.sendall('Press 1 to get ciphertext\n'.encode()) number = self.request.recv(512).strip().decode() if number == '1': self.request.sendall((str(n) + '\n').encode()) self.request.sendall((str(p) + '\n').encode()) self.request.sendall((str(c) + '\n').encode()) else: self.request.sendall('Incorrect input!\n'.encode()) return
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): pass
if __name__ == '__main__': sever = socketserver.ThreadingTCPServer(('0.0.0.0', 10001), MyServer) ThreadedTCPServer.allow_reuse_address = True ThreadedTCPServer.allow_reuse_port = True sever.serve_forever()
for char_1 in range(48, 123): # ASCII码中数字、大写字母和小写字母的范围 for char_2 in range(48, 123): for char_3 in range(48, 123): for char_4 in range(48, 123): if len({char_1, char_2, char_3, char_4}) == 4: # 确保四个字符都是不同的 ascii_chars = (chr(char_1), chr(char_2), chr(char_3), chr(char_4)) combination = "".join(ascii_chars) + 'mw21BZK89qImQVZX' # 在每个组合后添加特定字符串
根据源码分析,他把其中的p做了这样一个操作 p1[random.choice([i for i, c in enumerate(p1) if c == ‘1’)]) = ‘0’: 在 p1 的随机位置找到一个为 ‘1’ 的位,并将其改为 ‘0’。 p2[random.choice([i for i, c in enumerate(p1) if c == ‘0’)]) = ‘1’: 在 p2 的随机位置找到一个为 ‘0’ 的位,并将其改为 ‘1’。 所以我们只需要根据这个漏洞遍历前1024位的0和后1024位的1进行异或操作即可 笔者这边直接构造了一个密码本
# 检查每一位,如果为0则改为1 for i in range(len(p2)): if p2[i] == '0': modified_p1 = p2[:i] + '1' + p2[i+1:] modified_binaries.append(modified_p1)
# 将所有修改后的结果写入文件 with open(output_file, "w") as file: for modified_p1 in modified_binaries: file.write(modified_p1 + "\n")
# 打开 listp1.txt 和 listp2.txt 文件以及输出文件 p.txt with open('listp1.txt', 'r') as file1, open('listp2.txt', 'r') as file2, open('p.txt', 'w') as output_file: # 逐行读取文件内容 p1_lines = file1.readlines() p2_lines = file2.readlines()
# 遍历两个文件中的所有组合并将其写入输出文件 for p1 in p1_lines: file2.seek(0) # 重新定位到 listp2.txt 文件的开头 for p2 in p2_lines: # 写入组合到输出文件 output_file.write(f"{p1.strip()}{p2.strip()}\n")
n = 689148428917048752698561230187435330535844474535660975519663024686005813423116261691596518960359444398659487569281405280516756122565301499107276527019563398561775190548326726650095124347721633102683517354241616731272290842366644969493523095260169026118700872122205961125840074575539913181179624158591316008961999519664445180765805855068357280325140200570721885619207531433799082034359675994945498536733447149520815614429142224067402920944323350406000139261949314945008134993667106166371729994211490216695953544210086909916594288871041686796264966615061234867849533737850978285759970378711342002735127852669911135633280727507197770896643162828873860290236954478934069039880842775659386156914426681646990123375726632782848318765676209660955746904921591374247548244510795028854167024612141080434153599742660712283405861398644681971440490492676518075438358400296468401567303163631533553410643660848819592904185023897183284947919518254876711845553448295870785376932257813856605607299735796578232939819120320722961149946000934150374343672467868456190855696298746763169171976016018534242348938143398691129090031066272454600059389810619648519400690544893179323782655271245976991844056669848133657162015017534126606913505865717577945680934201 e = 0x10001
with open('p.txt', 'r') as p_file: for line in p_file: p = line.strip()
q_candidate = n // int(p, 2) if q_candidate * int(p, 2) != n: continue # 如果不能整除,跳过当前行处理
phi = (int(p, 2) - 1) * (q_candidate - 1) try: d = gmpy2.invert(e, phi) c = 582755493501376550312021063293971269432622857973591275097520241824639113243678154601039738777785910875356099861877799721721629337353531048570147711682149769656300149663334265982311991206401905216350979684516071937023923185938510050693691337873407171757275643036119429129455102869684317978357782902189346531413458627379268090802106996865349905645096514117275113848528530242217188285324909887937524122208178818340097019103831473349905133558912714201852986162525855145162259280450040753185483478933398530730654529154485769469682628353274080092629445953899362883988215175193630488889635324515092313723195910983168425805177795699565396108348821928449320190783128803002754706992512092839782219348149442008758998793607436136085108333190190347733106486023581643835108125870863346801123024469552193808660738495330000657495372570089643330001020748391509387487524136411478620736071866518379044131693794690230740826475529806050936866355127348429602091123271970724772553111459156824158976033596665391059836828836907988254364980925568575831029223091042575366899359078304670344881979433579570857621141077250931877184312810916773443135402247438937358801349822845162460003198622710136542402567604311023354451215661022609583925197654492145458148760392
m = pow(c, d, n) decoded_message = long_to_bytes(m).decode('utf-8', 'ignore')
if "D0g3" in decoded_message: print(decoded_message) except ZeroDivisionError: continue #flag:D0g3{sYuWzkFk12A1gcWxG9pymFcjJL7CqN4Cq8PAIACObJ}
from Crypto.Util.number import * from gmpy2 import * from tqdm import *
n= p0='' c= def msg(n, p0, c): p1 = p0[:1024] p2 = p0[1024:] pp1 = [i for i, c in enumerate(p1) if c == '0'] pp2 = [i for i, c in enumerate(p1) if c == '1'] # print(pp1) for i in tqdm(pp1): p1 = list(p0[:1024]) p1[i] = '1' for j in pp2: p2 = list(p0[1024:]) p2[j] = '0' ppp = ''.join(p1) + ''.join(p2) ppp2 = int(ppp, 2) if n % ppp2 == 0: p = ppp2 print(i, j) print(p) q = n // p d = invert(65537, (p-1)*(q-1)) m = long_to_bytes(int(pow(c, d, n))) if b'D0g3' in m: print(m) return p2[j] = "1" p1[i] = "0"