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


import socket
import json
import re

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

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

# Читаем
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}")

# Перебор x в ТОМ ЖЕ подключении
print("[*] Перебираем x...")
found_x = None
for x in range(1, 10_000_001):
    if pow(g, x, p) == A:
        found_x = x
        print(f"[+] НАЙДЕНО! x = {x}")
        break
    if x % 1_000_000 == 0:
        print(f"[*] Проверено {x}...")

if found_x is None:
    print("[-] x не найден. Пробуем x = A...")
    found_x = A

# Отправляем
payload = json.dumps({"answer": found_x})
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()