Protostar 0x09 - Net1

Prev: 0x08 - Net0
Next: 0x0A - Net2

According to this level's instructions, it's similar to the previous level but this time asks for the opposite: to convert binary integers into ASCII characters.


The "main" function hasn't changed, so let's examine "run:" first, a random integer is generated and put into the "wanted" variable. Then, it is loaded into the "fub" character buffer via "sprintf." Next, "fgets" takes our input and places it into the "buf" character buffer. Finally, "fub" and "buf" are compared, and "printf" provides feedback as appropriate.

If we use "netstat -l" as we did in the previous level, we see that net1's 2998 port is already listening. Let's build our socket and confirm that we receive input:


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 2998))

request = s.recv(1024)
print request 


If we run this, we see:


$ python net1.py
~


Where the orange text is some random ASCII representation of binary characters that will vary. Let's use "struct.unpack" to convert this and send it back in the expected format. Note that, unlike last level, the string that we receive from the net1 server contains no additional text, so we can convert it directly within the Python program without any string splicing.


import socket, struct

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 2998))

request = s.recv(1024)
print request

response = str(struct.unpack("<I", request)[0])
print "Sending back: " + response
s.send(response)

feedback = s.recv(1024)
print feedback 


Note the changes from the previous level which are highlighted orange: first, the port, but also "struct.unpack." This function results in a tuple, even if it is only one item, so we must use "[0]" to pull it out, and then "str()" to convert from integer to string before printing and sending.

Running the program, here's what we see:


$ python net1.py
VQ
Sending back: 1375571798
you correctly sent the data 


We've done it!

Prev: 0x08 - Net0
Next: 0x0A - Net2