Protostar – net0

Information

This level takes a look at converting strings to little endian integers.

This level is at /opt/protostar/bin/net0

Source code

#include "../common/common.c"

#define NAME "net0"
#define UID 999
#define GID 999
#define PORT 2999

void run()
{
  unsigned int i;
  unsigned int wanted;

  wanted = random();

  printf("Please send '%d' as a little endian 32bit int\n", wanted);

  if(fread(&i, sizeof(i), 1, stdin) == NULL) {
      errx(1, ":(\n");
  }

  if(i == wanted) {
      printf("Thank you sir/madam\n");
  } else {
      printf("I'm sorry, you sent %d instead\n", i);
  }
}

int main(int argc, char **argv, char **envp)
{
  int fd;
  char *username;

  /* Run the process as a daemon */
  background_process(NAME, UID, GID); 
  
  /* Wait for socket activity and return */
  fd = serve_forever(PORT);

  /* Set the client socket to STDIN, STDOUT, and STDERR */
  set_io(fd);

  /* Don't do this :> */
  srandom(time(NULL));

  run();
}

Solution

In this level, a server is waiting for a connection on the port 29999. When connected, it generates an unisgned integer :

wanted = random();

It sends it to the client :

printf(« Please send ‘%d’ as a little endian 32bit int\n », wanted);

Then, it waits for an input from the client. And finally, it compares the input with the unisgned integer generated before.

The integer is sent as string by the server so first I need to convert it to int().
Then, as I send this number through a network socket, I need to send it in little endian format.

For this challenge, I use the following python script :

#!/usr/bin/python

import socket
import struct

host = '192.168.0.46'
port = 2999

sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port))

# store the printf() string in recv
recv = sock.recv(2048)

# start & end point to the beginning and the end of the integer in the string
start = recv.find('\'')+1
end = recv.find('\'',start)

# keep only the integer from the string received
to_send_string = recv[start:end]

print "Integer to send in little endian: %s" % to_send_string

# convert the string integer to integer
to_send_int = int(to_send_string)

# little endian format
to_send_net = struct.pack('I',to_send_int)

sock.send(to_send_net)

recv = ''

recv = sock.recv(2048)

print recv

sock.close()

Let’s fire up :

$ python net0.py
Integer to send in little endian: 1399658911
Thank you sir/madam

That’s it, net0 is done.


Laisser un commentaire