import socket
import json
import re
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(15.0)
s.connect(('tasks.duckerz.ru', 30009))
# Читаем всё, пока не увидим "Send to Bob:"
data = b""
try:
while b"Send to Bob:" not in data:
chunk = s.recv(4096)
if not chunk:
break
data += chunk
except socket.timeout:
pass
text = data.decode('utf-8', errors='ignore')
print(f"[*] Получено:\n{text}\n")
match = re.search(r'\{.*\}', text)
if not match:
print("[-] JSON не найден")
exit()
alice = json.loads(match.group())
p = int(alice['p'], 16)
g = int(alice['g'], 16)
A = int(alice['A'], 16)
print(f"[*] p = {p} ({p.bit_length()} бит)")
print(f"[*] g = {g}")
print(f"[*] A = {A}")
# 1. Пробуем найти a перебором (до 1 миллиона)
print("[*] Ищем секретный ключ a...")
found_a = None
for a in range(1, 1_000_001):
if pow(g, a, p) == A:
found_a = a
print(f"[+] НАЙДЕНО! a = {a}")
break
if a % 100000 == 0:
print(f"[*] Проверено {a}...")
if found_a is None:
print("[-] a не найден. p слишком большое.")
# Пробуем отправить A как hex
print("[*] Пробуем отправить A в hex...")
s.sendall((alice['A'] + "\n").encode())
else:
# 2. Если нашли a — отправляем его как ответ
print(f"[*] Отправляем a = {found_a}")
s.sendall((str(found_a) + "\n").encode())
# 3. Читаем ответ (флаг) с большим таймаутом
print("[*] Ждем ответ...")
s.settimeout(10.0)
try:
while True:
resp = s.recv(4096)
if not resp:
print("[-] Соединение закрыто")
break
decoded = resp.decode('utf-8', errors='ignore')
print(f"[+] Ответ: {decoded}")
if "DUCKERZ{" in decoded:
print(f"\n[!!!] ФЛАГ НАЙДЕН: {decoded}\n")
break
except socket.timeout:
print("[-] Таймаут")
s.close()