Don't receive messages returned from server with python socket

Viewed 426

Host: 45.77.245.244
Port: 5006

It is a host in a CTF challenge. However I needn't solve it. I want to automate it in python.

When connecting to the server. The server will return 2 messages

1: Progamming .....

2: n =? \ NAnwser:

and listen to the user input the answers.

Netcat:

toor@MSI:~$ nc 45.77.245.244 5006

PROGRAMING - WHITEHAT 2019:

--> Coungting the triangles <--

HOW MANY TRIANGLE IS CREATED BY N (1..N) NUMBER. N < 10^6

Example: with N = 5
OUTPUT : 3

(2,3,4),(3,4,5),(2,4,5)
................/\...................|\...................
.............../  \..................| \..................
............../    \.................|  \.................
............./      \................|   \................
............/        \...............|    \...............
.........../          \..............|     \..............
........../____________\.............|______\.............

n = 99
Answer: 4
Wrong answer. I can't trust you anymore :'<

I wrote a piece of code that connects and sends back "n" with the desired result:
Wrong answer. I can't trust you anymore: '<

My Python code:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('45.77.245.244', 5006))

print('Server:', str(s.recv(1024)))
while True:
    data = s.recv(1024).strip()
    if data:
        data = str(data)
        print('Server:', data)
        n = ''.join([i for i in data if i.isdigit()])
        n = bytes(n, 'ascii')
        s.send(n)
        print('Client: ', n)
s.close()

Output:

Server: b'\nPROGRAMING - WHITEHAT 2019:\n\n--> Coungting the triangles <--\n\nHOW MANY TRIANGLE IS CREATED BY N (1..N) NUMBER. N < 10^6\n\nExample: with N = 5\nOUTPUT : 3 \n\n(2,3,4),(3,4,5),(2,4,5)\n................/\\...................|\\...................\n.............../  \\..................| \\..................\n............../    \\.................|  \\.................\n............./      \\................|   \\................\n............/        \\...............|    \\...............\n.........../          \\..............|     \\..............\n........../____________\\.............|______\\.............\n\t\n'
Server: b'n = 25\nAnswer:'
Client:  b'25'

After send(n) and print('send:', n) to make sure the sending was successful, I did not get the output I wanted when using netcat.

Output: Wrong answer. I can't trust you anymore :'<

Where did I go wrong? Is there a way to fix it?

1 Answers

Maybe this is a simple question. The code doesn't seem to have any errors, but the reality is I forgot to send the character "\ n". How I fix it:

n = (n + '\n')
s.send(n)

Here, I need to add a new line character ^^

Related