Protostar – net1

Information

This level tests the ability to convert binary integers into ascii representation.

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

Source code

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

#define NAME "net1"
#define UID 998
#define GID 998
#define PORT 2998

void run()
{
  char buf[12];
  char fub[12];
  char *q;

  unsigned int wanted;

  wanted = random();

  sprintf(fub, "%d", wanted);

  if(write(0, &wanted, sizeof(wanted)) != sizeof(wanted)) {
      errx(1, ":(\n");
  }

  if(fgets(buf, sizeof(buf)-1, stdin) == NULL) {
      errx(1, ":(\n");
  }

  q = strchr(buf, '\r'); if(q) *q = 0;
  q = strchr(buf, '\n'); if(q) *q = 0;

  if(strcmp(fub, buf) == 0) {
      printf("you correctly sent the data\n");
  } else {
      printf("you didn't send the data properly\n");
  }
}

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

This level is nearly the same as the previous one, excepted that the server sends the integer in little endian format, and it waits its’ ASCII value.

I use the following python script :

#!/usr/bin/python

import socket
import struct

host = '192.168.0.46'
port = 2998

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

recv = sock.recv(2048)

u_int_recv = struct.unpack("<I",recv)[0]

sock.send(str(u_int_recv))

recv = ''

recv = sock.recv(2048)

print recv

sock.close()

Let’s fire up :

$ python net1.py
you correctly sent the data

That’s it, net1 is done.


Laisser un commentaire