The Challenge
The server sends an arithmetic expression labelled with an operation name in Italian and expects the correct answer. There are 2049 rounds. Doing them by hand is not an option.
Approach
The response always contains the operation keyword and the operands as integers in the string. re.findall(r'-?\d+\.?\d*', ...) extracts all numbers including negatives. The operation name determines how to combine them. After 2049 correct answers the server returns the flag.
The five operations covered:
SOMMA→ sum of all extracted numbersDIFFERENZA→ first number minus secondPRODOTTO→ first times secondPOTENZA→ first to the power of the secondDIVISIONE_INTERA→ integer division of first by second
context.timeout = 1 keeps recv from hanging on a slow round.
Solution
|
|
Each iteration reads a message, picks the branch for the current operation, extracts all integers from the string with a single regex pass, computes the answer, and sends it. After all 2049 iterations the final r.recv() contains the flag.
What I Learned
Arithmetic servers are trivially automated once you pick the right operation dispatch pattern. The real risk is slow receives or a partial buffer — using context.timeout and re-entering r.recv() at the top of the loop handles both without complicated state management.