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


import socket
import json
import re

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

def try_value(val, label):
    """Пробует отправить значение в JSON с ключом answer."""
    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())
        
        payload = json.dumps({"answer": 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[:300]}")
            return resp
    except Exception as e:
        pass
    finally:
        s.close()
    return None

# Перебираем форматы A
formats = [
    (lambda a: a['A'], "A (raw hex string)"),
    (lambda a: a['A'][2:], "A (hex без 0x)"),
    (lambda a: int(a['A'], 16), "A (int)"),
    (lambda a: str(int(a['A'], 16)), "A (str int)"),
    (lambda a: a['p'], "p (raw hex)"),
    (lambda a: a['p'][2:], "p (hex без 0x)"),
    (lambda a: int(a['p'], 16), "p (int)"),
    (lambda a: a['g'], "g (raw hex)"),
    (lambda a: int(a['g'], 16), "g (int)"),
]

print("[*] Перебираем форматы значения...")
for fmt, label in formats:
    print(f"[*] Пробуем: {label}")
    # Получаем свежие данные
    s_temp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s_temp.settimeout(5.0)
    try:
        s_temp.connect((HOST, PORT))
        data = b""
        while b"Send to Bob:" not in data:
            chunk = s_temp.recv(4096)
            if not chunk:
                break
            data += chunk
        text = data.decode('utf-8', errors='ignore')
        match = re.search(r'\{.*\}', text)
        alice = json.loads(match.group())
        s_temp.close()
        
        resp = try_value(alice, label)
        if resp:
            print(f"\n[!] НАЙДЕН РАБОЧИЙ ФОРМАТ: {label}")
            break
    except:
        s_temp.close()
        continue
else:
    print("\n[-] Ни один формат не сработал.")