So what’s this whole thing about? “The wargames offered by the OverTheWire community can help you to learn and practice security concepts in the form of funfilled games.” by OverTheWire.org. So I started with Vortex and coded an example how to read 4 integers from server, sum them up, send result to server and get the login information back from server again. Sounds quite simple. I admit I had to refresh my knowledge about linux socket programming and go through some coding examples. And here is my solution for level0:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 * ========================================================
 *
 *       Filename:  vortex_level0.c  
 *
 *    Description:  Read 4 integers from vortex.labs.pulltheplug.org:5842, sum
 *                  them up, send result to server and get login data
 *
 *        Version:  1.0
 *        Created:  05/20/2009 08:53:53 PM
 *       Compiler:  gcc
 *
 *         Author:  Victor Dorneanu 
 *
 * ========================================================
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define HOSTNAME "vortex.labs.pulltheplug.org"
#define PORT "5842"

void die(char *mess) { perror(mess); exit(1); }

int main(int argc, char *argv[]) {
   int sock, rv;
   struct addrinfo hints, *servinfo, *p;
   unsigned int buffer[4];
   char login_data[200];
   unsigned int received = 0;
   int i, bytes;

   /*  Construct the server addrinfo structure */
   memset(&hints, 0, sizeof hints);         /*  Clear struct */
   hints.ai_family = AF_UNSPEC;             /*  IPv4 or IPv6 */
   hints.ai_socktype = SOCK_STREAM;

   if ((rv = getaddrinfo(HOSTNAME, PORT, &hints, &servinfo)) != 0) {
		fprintf(stderr, "getaddrinfo: %s
", gai_strerror(rv));
		return 1;
	}

	// loop through all the results and make a socket
	for(p = servinfo; p != NULL; p = p->ai_next) {
		if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
			die("Couldn't create socket");
			continue;
		}
		break;
	}

   /*  Establish  connection */
   if (connect(sock, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
      die("Failed to connect to server");
   }

   /* Read 4 integers from server */
   for (i = 0; i < 4; i ++) 
      read(sock, &buffer[i], 4);

   /* Sum up integers */
   received = 0;
   for(i=0; i < 4; i++)  
      received += buffer[i];

   /*  Send sum to the server */
   if (send(sock, &received, sizeof(received), 0) != sizeof received) {
      die("Mismatch in number of sent bytes");
   }

   /* Read login data */
   if (recv(sock, &login_data, 200, 0) < 1) {
      die("Mismatch in number of received bytes");
   }

   fprintf(stdout,"login: %s
", login_data);

   /*  Free addrinfo structure */
   freeaddrinfo(servinfo);

   /* Close connection */
   close(sock);
   exit(0);
}