Merge branch 'master' of ssh://cfl.lanl.gov/var/projects/gctf

This commit is contained in:
Paul S. Ferrell 2009-10-08 09:04:28 -06:00
commit 8e8bad1e3c
2 changed files with 72 additions and 50 deletions

12
ctf.css
View File

@ -81,3 +81,15 @@ p {
.solved { .solved {
text-decoration: line-through; text-decoration: line-through;
} }
table.pollster {
margin-left: 5em;
}
table.pollster td {
padding: 2px 1em 2px 5px;
}
table.pollster thead {
font-weight: bold;
}

View File

@ -5,19 +5,14 @@ import re
import sys import sys
import time import time
import socket import socket
import urllib.request import traceback
# TODO: # TODO:
# special stops for http and tftp?
# get start time and end time of poll, sleep(60-exectime)
# what to do about exceptions
# no nested dicts
# scoring interface # scoring interface
# config interface # config interface
# html that uses the proper css
DEBUG = True DEBUG = False
POLL_INTERVAL = 2 POLL_INTERVAL = 60
IP_DIR = 'iptest/' IP_DIR = 'iptest/'
REPORT_PATH = 'iptest/pollster.html' REPORT_PATH = 'iptest/pollster.html'
SOCK_TIMEOUT = 0.5 SOCK_TIMEOUT = 0.5
@ -26,13 +21,14 @@ def socket_poll(ip, port, msg, prot, max_recv=1):
''' Connect via socket to the specified <ip>:<port> using the ''' Connect via socket to the specified <ip>:<port> using the
specified <prot>, send the specified <msg> and return the specified <prot>, send the specified <msg> and return the
response or None if something went wrong. <max_recvs> specifies response or None if something went wrong. <max_recvs> specifies
how many times to read from the socket. ''' how many times to read from the socket (default to once). '''
# create a socket # create a socket
try: try:
sock = socket.socket(socket.AF_INET, prot) sock = socket.socket(socket.AF_INET, prot)
except Exception as e: except Exception as e:
print('pollster: create socket failed') print('pollster: create socket failed (%s)' % e)
traceback.print_exc()
return None return None
sock.settimeout(SOCK_TIMEOUT) sock.settimeout(SOCK_TIMEOUT)
@ -41,10 +37,12 @@ def socket_poll(ip, port, msg, prot, max_recv=1):
try: try:
sock.connect((ip, port)) sock.connect((ip, port))
except socket.timeout as e: except socket.timeout as e:
print('pollster: attempt to connect to %s:%d timed out' % (ip, port)) print('pollster: attempt to connect to %s:%d timed out (%s)' % (ip, port, e))
traceback.print_exc()
return None return None
except Exception as e: except Exception as e:
print('pollster: attempt to connect to %s:%d failed' % (ip, port)) print('pollster: attempt to connect to %s:%d failed (%s)' % (ip, port, e))
traceback.print_exc()
return None return None
# send something # send something
@ -64,10 +62,13 @@ def socket_poll(ip, port, msg, prot, max_recv=1):
resp += data.decode('utf-8') resp += data.decode('utf-8')
max_recv -= 1 max_recv -= 1
sock.close() sock.close()
except socket.timeout as e: except socket.timeout as e:
print('pollster: timed out waiting for a response from %s:%d' % (ip, port)) print('pollster: timed out waiting for a response from %s:%d (%s)' % (ip, port, e))
traceback.print_exc()
except Exception as e: except Exception as e:
print('pollster: receive from %s:%d failed' % (ip, port)) print('pollster: receive from %s:%d failed (%s)' % (ip, port, e))
traceback.print_exc()
if len(resp) == 0: if len(resp) == 0:
return None return None
@ -79,21 +80,21 @@ def socket_poll(ip, port, msg, prot, max_recv=1):
# if (a) the service is not up, (b) it doesn't return a valid team name. # if (a) the service is not up, (b) it doesn't return a valid team name.
def poll_fingerd(ip): def poll_fingerd(ip):
''' Poll the fingerd service. ''' ''' Poll the fingerd service. Returns None or a team name. '''
resp = socket_poll(ip, 79, b'flag\n', socket.SOCK_STREAM) resp = socket_poll(ip, 79, b'flag\n', socket.SOCK_STREAM)
if resp is None: if resp is None:
return None return None
return resp.strip('\r\n') return resp.strip('\r\n')
def poll_noted(ip): def poll_noted(ip):
''' Poll the noted service. ''' ''' Poll the noted service. Returns None or a team name. '''
resp = socket_poll(ip, 4000, b'rflag\n', socket.SOCK_STREAM) resp = socket_poll(ip, 4000, b'rflag\n', socket.SOCK_STREAM)
if resp is None: if resp is None:
return None return None
return resp.strip('\r\n') return resp.strip('\r\n')
def poll_catcgi(ip): def poll_catcgi(ip):
''' Poll the cat.cgi web service. ''' ''' Poll the cat.cgi web service. Returns None or a team name. '''
request = bytes('GET /cat.cgi/flag HTTP/1.1\r\nHost: %s\r\n\r\n' % ip, 'ascii') request = bytes('GET /cat.cgi/flag HTTP/1.1\r\nHost: %s\r\n\r\n' % ip, 'ascii')
resp = socket_poll(ip, 80, request, socket.SOCK_STREAM, 3) resp = socket_poll(ip, 80, request, socket.SOCK_STREAM, 3)
if resp is None: if resp is None:
@ -115,7 +116,7 @@ def poll_catcgi(ip):
return content[1].strip('\r\n') return content[1].strip('\r\n')
def poll_tftpd(ip): def poll_tftpd(ip):
''' Poll the tftp service. ''' ''' Poll the tftp service. Returns None or a team name. '''
resp = socket_poll(ip, 69, b'\x00\x01' + b'flag' + b'\x00' + b'octet' + b'\x00', socket.SOCK_DGRAM) resp = socket_poll(ip, 69, b'\x00\x01' + b'flag' + b'\x00' + b'octet' + b'\x00', socket.SOCK_DGRAM)
if resp is None: if resp is None:
return None return None
@ -137,15 +138,28 @@ POLLS = {
ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}') ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}')
# loop forever # loop forever
while(True): while True:
t_start = time.time()
# gather the list of IPs to poll # gather the list of IPs to poll
try: try:
ips = os.listdir(IP_DIR) ips = os.listdir(IP_DIR)
except Exception as e: except Exception as e:
print('pollster: could not list dir %s' % IP_DIR) print('pollster: could not list dir %s (%s)' % (IP_DIR, e))
traceback.print_exc()
try:
os.remove(REPORT_PATH)
except Exception as e:
pass
out = open(REPORT_PATH, 'w')
out.write('<html>\n<head>\n')
out.write('<title>Pollster Results</title>\n')
out.write('<link rel="stylesheet" href="ctf.css" type="text/css" media="all" />\n')
out.write('</head><body>\n<h1>Polling Results</h1>\n')
results = {}
for ip in ips: for ip in ips:
# check file name format is ip # check file name format is ip
@ -153,49 +167,45 @@ while(True):
continue continue
# remove the file # remove the file
#try: try:
# os.remove(os.path.join(IP_DIR, ip)) os.remove(os.path.join(IP_DIR, ip))
#except Exception as e: except Exception as e:
# print('pollster: could not remove %s' % os.path.join(IP_DIR, ip)) print('pollster: could not remove %s' % os.path.join(IP_DIR, ip))
traceback.print_exc()
results[ip] = {} results = {}
if DEBUG is True: if DEBUG is True:
print('ip: %s' % ip) print('ip: %s' % ip)
out.write('<h2>%s</h2>\n' % ip)
out.write('<table class="pollster">\n<thead><tr><td>Service Name</td></td>')
out.write('<td>Flag Holder</td></tr></thead>\n')
# perform polls # perform polls
for service,func in POLLS.items(): for service,func in POLLS.items():
team = func(ip) team = func(ip)
if team is None: if team is None:
team = 'dirtbags' team = 'dirtbags'
results[ip][service] = team if DEBUG is True:
print('\t%s - %s' % (service, team))
if DEBUG is True: out.write('<tr><td>%s</td><td>%s</td>\n' % (service, team))
for k,v in results[ip].items():
print('\t%s - %s' % (k,v)) out.write('</table>\n')
if DEBUG is True: if DEBUG is True:
print('+-----------------------------------------+') print('+-----------------------------------------+')
# allocate points t_end = time.time()
exec_time = int(t_end - t_start)
# generate html report sleep_time = POLL_INTERVAL - exec_time
out = open(REPORT_PATH, 'w')
out.write('<html>\n<title><head>Polling Results</head></title>\n')
out.write('<body>\n<h1>Polling Results</h1>\n')
for ip in results.keys():
out.write('<h2>%s</h2>\n' % ip)
out.write('<table>\n<thead><tr><td>Service Name</td></td>')
out.write('<td>Flag Holder</td></tr></thead>\n')
for service,flag_holder in results[ip].items():
out.write('<tr><td>%s</td><td>%s</td>\n' % (service, flag_holder))
out.write('</table>\n')
out.write('<p><b>Next poll in: %ds</b></p>\n' % sleep_time)
out.write('</body>\n</html>\n') out.write('</body>\n</html>\n')
out.close() out.close()
# sleep until its time to poll again # sleep until its time to poll again
time.sleep(POLL_INTERVAL) time.sleep(sleep_time)