import socket
import json
import re
HOST = 'tasks.duckerz.ru'
PORT = 30009
def test_format(fmt_func, label):
"""Пробует отправить ключ в заданном формате."""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5.0)
try:
s.connect((HOST, PORT))
data = b""
while b"Send to Bob:" not in data:
chunk = s.recv(4096)
if not chunk:
break
data += chunk
text = data.decode('utf-8', errors='ignore')
match = re.search(r'\{.*\}', text)
if not match:
return None
alice = json.loads(match.group())
p = int(alice['p'], 16)
g = int(alice['g'], 16)
c = 1337
C = pow(g, c, p)
# Формируем значение
val = fmt_func(C)
payload = json.dumps({"C": val})
s.sendall((payload + "\n").encode())
resp = s.recv(4096).decode('utf-8', errors='ignore').strip()
if resp and "Connection terminated" not in resp:
print(f"[+] УСПЕХ с форматом '{label}'!")
print(f" Ответ: {resp[:200]}")
return resp
except Exception as e:
pass
finally:
s.close()
return None
# Перебираем форматы
formats = [
(lambda x: x, "int"),
(lambda x: str(x), "str(int)"),
(lambda x: hex(x), "hex (0x...)"),
(lambda x: hex(x)[2:], "hex (без 0x)"),
(lambda x: str(hex(x)), "str(hex)"),
(lambda x: format(x, 'x'), "hex (format)"),
(lambda x: format(x, 'X'), "HEX (format)"),
(lambda x: str(x).encode().hex(), "int as hex"),
]
print("[*] Перебираем форматы значения C...")
for fmt, label in formats:
print(f"[*] Пробуем: {label}")
resp = test_format(fmt, label)
if resp:
print(f"\n[!] НАЙДЕН ФОРМАТ: {label}")
break
else:
print("\n[-] Ни один формат не сработал.")