Files
ctf-notes/programming/tcp-back-to-school/main.py
2026-03-21 12:48:36 +01:00

38 lines
829 B
Python

import math
import re
import socket
HOST = "challenge01.root-me.org"
PORT = 52002
def solve_prompt(prompt: str) -> str:
match = re.search(
r"square root of\s*(\d+)\s*and multiply by\s*(\d+)",
prompt,
re.IGNORECASE,
)
if not match:
raise ValueError("Unsupported challenge prompt")
left = int(match.group(1))
right = int(match.group(2))
return f"{math.sqrt(left) * right:.2f}"
def main() -> None:
with socket.create_connection((HOST, PORT), timeout=2) as conn:
prompt = conn.recv(4096).decode(errors="replace")
print(prompt, end="")
answer = solve_prompt(prompt)
conn.sendall((answer + "\n").encode())
result = conn.recv(4096).decode(errors="replace")
print(result, end="")
if __name__ == "__main__":
main()