Search code examples
pythonpython-3.xsocketstcpconnection

Sending second TCP message doesn't get recognized/no reaction on serverside, with netcat works fine


This is a task for CTF preparation which can be found on https://rookies.fluxfingers.net/code.php?p=chal&id=142 .

The task is to calculate the sum of all even numbers - the sum of all uneven numbers.

I solved this but when sending the answer message to the server it doesn't respond with the flag.

But with nc rkchals.fluxfingers.net 5002 a second message can be send. My code:

import socket
import re

#connection establishing
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("rkchals.fluxfingers.net",5002))
message = str(s.recv(3000))

#data processing
numbers= re.findall("\d+",message)
uneven_numbers=list()
even_numbers = list()
for i in numbers:
 if(int(i)%2==0):        
     even_numbers.append(int(i))
 else:        
     uneven_numbers.append(int(i))
answer = sum(even_numbers)-sum(uneven_numbers)

#log
print("server says: " + message + "\n")
print("numberarray is: " + str(numbers) + "\n")
print("even numbers are: " + str(even_numbers) + "\n")
print("odd numbers are: " + str(uneven_numbers) + "\n")
print("sum of even - sum of odd: " + str(answer) + "\n")

#sending the answer as string to get the flag
s.sendall(bytes(str(answer),"utf-8"))
print("flag is: " + str(s.recv(1024)))

At the end when I send the answer the server ignores it(?), because the last command should give me the flag as an answer in result of sending the answer previously.

What should I change so that the server can receive my second message?

I tried: timeout(?)

I lack of knowledge and I couldn't find an answer here.


Solution

  • Because the server application expects an Enter, I simply added this to my code:

    s.sendall(bytes(str(answer) + "\n","utf-8"))
    

    Then ther server responds normally with the flag.