HC's CTF site

The simplest ctf service

This is the source code of the simplest ctf service possible.

You might also be interested in the testscript for this service.

 #!/usr/local/bin/python
 # -*- coding: utf-8 -*-
 from socket import *
 from sys import exit
 from time import time
 class Message:
     TIMEOUT = 1800 # validity period
     def __init__(self, msg):
         self.ct = time()
         self.msg = msg
     def isvalid(self): return (time() - self.ct) < self.TIMEOUT
 messages = {}
 def p_add(addr, message, sock):
     h, p, i, m = message.split(" ", 3)
     h = addr[0]
     p = addr[1]
     messages[i] = Message(m)
     print "%s -> %s" % (i, m)
     sock.sendto("OK", (h, int(p)))
 def p_get(addr, message, sock):
     h, p, i = message.split(" ", 2)
     h = addr[0]
     p = addr[1]
     if i in messages: print "sending %s to %s:%s" % (i, h, p)
     else: print "Message with ID %s not found" % i
     try: sock.sendto(messages[i].msg, (h, int(p)))
     except: sock.sendto('invalid ID', (h, int(p)))
     # del messages[i]
 procedures = {
     'add': p_add,
     'get': p_get,
 }
 host = "0.0.0.0"
 port = 1984
 buf = 1024
 addr = (host, port)
 sock = socket(AF_INET, SOCK_DGRAM)
 sock.bind(addr)
 print "Waiting for connections..."
 while True:
 	data, addr = sock.recvfrom(buf)
 	if data:
             try:
                 command, message = data.split(" ", 1)
                 procedure = procedures[command]
                 procedure(addr, message, sock)
             except Exception, e:
                 print e
 UDPSock.close()