import socket
import json
import re
HOST = 'tasks.duckerz.ru'
PORT = 30009
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(120.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')
match = re.search(r'\{.*\}', text)
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+b, a-b, a*b, a//b
# где a, b от 1 до 1000
print("[*] Перебираем выражения...")
found_expr = None
found_result = None
ops = ['+', '-', '*', '//']
for a in range(1, 1001):
if a % 100 == 0:
print(f"[*] a = {a}...")
for b in range(1, 1001):
for op in ops:
expr_str = f"{a}{op}{b}"
# Переводим строку в число (как байты)
x = int.from_bytes(expr_str.encode(), 'big')
if pow(g, x, p) == A:
found_expr = expr_str
found_result = eval(expr_str)
print(f"\n[!!!] НАЙДЕНО! {expr_str} = {found_result}")
break
if found_expr:
break
if found_expr:
break
if found_expr:
# Отправляем результат
payload = json.dumps({"answer": found_result})
print(f"[*] Отправляем: {payload}")
s.sendall((payload + "\n").encode())
s.settimeout(15.0)
try:
while True:
resp = s.recv(4096)
if not resp:
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("[-] Таймаут")
else:
print("\n[-] Выражение не найдено.")
s.close()