Загрузка данных


import socket
import json
import re

HOST = 'tasks.duckerz.ru'
PORT = 30009

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(15.0)
s.connect((HOST, PORT))

# Читаем всё, пока не увидим "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}")

# === ГЛАВНЫЙ ВОПРОС: Что отправить? ===
# Попробуем самый логичный вариант: A как ответ
answer_to_send = A

# Формируем JSON
payload = json.dumps({"answer": answer_to_send})
print(f"[*] Отправляем: {payload[:100]}...")
s.sendall((payload + "\n").encode())

# Читаем ответ (долго, без спама)
print("[*] Ждем ответ...")
s.settimeout(15.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()