2009-10-07 16:54:47 -06:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2010-01-19 15:52:08 -07:00
|
|
|
#test
|
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
import os
|
|
|
|
import re
|
2009-10-09 08:55:51 -06:00
|
|
|
import io
|
2009-10-07 16:54:47 -06:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import socket
|
2009-10-08 08:49:50 -06:00
|
|
|
import traceback
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-08 11:11:41 -06:00
|
|
|
from ctf import config
|
|
|
|
from ctf import pointscli
|
2009-10-07 19:02:57 -06:00
|
|
|
|
2009-10-09 10:15:24 -06:00
|
|
|
DEBUG = False
|
2009-10-08 16:42:26 -06:00
|
|
|
POLL_INTERVAL = config.get('pollster', 'poll_interval')
|
2009-10-09 10:15:24 -06:00
|
|
|
IP_DIR = config.get('pollster', 'heartbeat_dir')
|
2009-10-08 16:42:26 -06:00
|
|
|
REPORT_PATH = config.get('pollster', 'results')
|
|
|
|
SOCK_TIMEOUT = config.get('pollster', 'poll_timeout')
|
2009-10-08 11:11:41 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
|
|
|
|
def socket_poll(ip, port, msg, prot, max_recv=1):
|
|
|
|
''' Connect via socket to the specified <ip>:<port> using the
|
2009-10-08 16:42:26 -06:00
|
|
|
specified <prot>, send the specified <msg> and return the
|
2009-10-07 19:02:57 -06:00
|
|
|
response or None if something went wrong. <max_recvs> specifies
|
2009-10-08 08:49:50 -06:00
|
|
|
how many times to read from the socket (default to once). '''
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
# create a socket
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_INET, prot)
|
|
|
|
except Exception as e:
|
2009-10-08 08:49:50 -06:00
|
|
|
print('pollster: create socket failed (%s)' % e)
|
|
|
|
traceback.print_exc()
|
2009-10-07 19:02:57 -06:00
|
|
|
return None
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
sock.settimeout(SOCK_TIMEOUT)
|
|
|
|
|
|
|
|
# connect
|
2009-10-07 16:54:47 -06:00
|
|
|
try:
|
|
|
|
sock.connect((ip, port))
|
2009-10-07 19:02:57 -06:00
|
|
|
except socket.timeout as e:
|
2009-10-08 08:49:50 -06:00
|
|
|
print('pollster: attempt to connect to %s:%d timed out (%s)' % (ip, port, e))
|
|
|
|
traceback.print_exc()
|
2009-10-07 19:02:57 -06:00
|
|
|
return None
|
2009-10-07 16:54:47 -06:00
|
|
|
except Exception as e:
|
2009-10-08 08:49:50 -06:00
|
|
|
print('pollster: attempt to connect to %s:%d failed (%s)' % (ip, port, e))
|
|
|
|
traceback.print_exc()
|
2009-10-07 16:54:47 -06:00
|
|
|
return None
|
2009-10-07 19:02:57 -06:00
|
|
|
|
|
|
|
# send something
|
2009-10-07 16:54:47 -06:00
|
|
|
sock.send(msg)
|
2009-10-07 19:02:57 -06:00
|
|
|
|
|
|
|
# get a response
|
2009-10-08 16:40:14 -06:00
|
|
|
resp = []
|
2009-10-07 19:02:57 -06:00
|
|
|
try:
|
2009-10-08 16:40:14 -06:00
|
|
|
# read from the socket until <max_recv> responses or read,
|
2009-10-08 16:44:42 -06:00
|
|
|
# a timeout occurs, the socket closes, or some other exception
|
2009-10-08 16:40:14 -06:00
|
|
|
# is raised
|
|
|
|
for i in range(max_recv):
|
2009-10-07 19:02:57 -06:00
|
|
|
data = sock.recv(1024)
|
2009-10-08 16:40:14 -06:00
|
|
|
if len(data) == 0:
|
|
|
|
break
|
|
|
|
resp.append(data)
|
2009-10-08 08:49:50 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
except socket.timeout as e:
|
2009-10-08 08:49:50 -06:00
|
|
|
print('pollster: timed out waiting for a response from %s:%d (%s)' % (ip, port, e))
|
|
|
|
traceback.print_exc()
|
2009-10-07 19:02:57 -06:00
|
|
|
except Exception as e:
|
2009-10-08 08:49:50 -06:00
|
|
|
print('pollster: receive from %s:%d failed (%s)' % (ip, port, e))
|
|
|
|
traceback.print_exc()
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-08 16:40:14 -06:00
|
|
|
sock.close()
|
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
if len(resp) == 0:
|
|
|
|
return None
|
|
|
|
|
2009-10-08 16:40:14 -06:00
|
|
|
return b''.join(resp)
|
2009-10-07 16:54:47 -06:00
|
|
|
|
|
|
|
# PUT POLLS FUNCTIONS HERE
|
|
|
|
# Each function should take an IP address and return a team name or None
|
|
|
|
# if (a) the service is not up, (b) it doesn't return a valid team name.
|
|
|
|
|
|
|
|
def poll_fingerd(ip):
|
2009-10-08 08:49:50 -06:00
|
|
|
''' Poll the fingerd service. Returns None or a team name. '''
|
2009-10-07 19:02:57 -06:00
|
|
|
resp = socket_poll(ip, 79, b'flag\n', socket.SOCK_STREAM)
|
2009-10-07 16:54:47 -06:00
|
|
|
if resp is None:
|
|
|
|
return None
|
2009-10-08 16:40:14 -06:00
|
|
|
return resp.strip(b'\r\n')
|
2009-10-07 16:54:47 -06:00
|
|
|
|
|
|
|
def poll_noted(ip):
|
2009-10-08 08:49:50 -06:00
|
|
|
''' Poll the noted service. Returns None or a team name. '''
|
2009-10-07 19:02:57 -06:00
|
|
|
resp = socket_poll(ip, 4000, b'rflag\n', socket.SOCK_STREAM)
|
2009-10-07 16:54:47 -06:00
|
|
|
if resp is None:
|
|
|
|
return None
|
2009-10-08 16:40:14 -06:00
|
|
|
return resp.strip(b'\r\n')
|
2009-10-07 16:54:47 -06:00
|
|
|
|
|
|
|
def poll_catcgi(ip):
|
2009-10-08 08:49:50 -06:00
|
|
|
''' Poll the cat.cgi web service. Returns None or a team name. '''
|
2009-10-07 19:02:57 -06:00
|
|
|
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)
|
|
|
|
if resp is None:
|
2009-10-07 16:54:47 -06:00
|
|
|
return None
|
|
|
|
|
2009-10-08 16:40:14 -06:00
|
|
|
content = resp.split(b'\r\n\r\n')
|
2009-10-07 19:02:57 -06:00
|
|
|
if len(content) < 3:
|
|
|
|
return None
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-08 16:40:14 -06:00
|
|
|
content = content[1].split(b'\r\n')
|
2009-10-07 19:02:57 -06:00
|
|
|
|
|
|
|
try:
|
|
|
|
content_len = int(content[0])
|
|
|
|
except Exception as e:
|
2009-10-07 16:54:47 -06:00
|
|
|
return None
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
if content_len <= 0:
|
|
|
|
return None
|
2009-10-08 16:40:14 -06:00
|
|
|
return content[1].strip(b'\r\n')
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
def poll_tftpd(ip):
|
2009-10-08 08:49:50 -06:00
|
|
|
''' Poll the tftp service. Returns None or a team name. '''
|
2009-10-07 19:02:57 -06:00
|
|
|
resp = socket_poll(ip, 69, b'\x00\x01' + b'flag' + b'\x00' + b'octet' + b'\x00', socket.SOCK_DGRAM)
|
|
|
|
if resp is None:
|
|
|
|
return None
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
if len(resp) <= 5:
|
|
|
|
return None
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-08 16:40:14 -06:00
|
|
|
resp = resp.split(b'\n')[0]
|
|
|
|
|
|
|
|
# ack
|
2009-10-08 17:07:15 -06:00
|
|
|
_ = socket_poll(ip, 69, b'\x00\x04' + resp[2:4], socket.SOCK_DGRAM, 0)
|
2009-10-08 16:40:14 -06:00
|
|
|
|
|
|
|
return resp[4:].strip(b'\r\n')
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
# PUT POLL FUNCTIONS IN HERE OR THEY WONT BE POLLED
|
|
|
|
POLLS = {
|
|
|
|
'fingerd' : poll_fingerd,
|
2009-10-08 17:07:15 -06:00
|
|
|
'noted' : poll_noted,
|
2009-10-07 16:54:47 -06:00
|
|
|
'catcgi' : poll_catcgi,
|
2009-10-08 17:07:15 -06:00
|
|
|
'tftpd' : poll_tftpd,
|
2009-10-07 16:54:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}')
|
2010-01-19 15:11:59 -07:00
|
|
|
poll_no = 0
|
2009-10-07 16:54:47 -06:00
|
|
|
# loop forever
|
2009-10-08 08:49:50 -06:00
|
|
|
while True:
|
|
|
|
|
|
|
|
t_start = time.time()
|
2009-10-07 16:54:47 -06:00
|
|
|
|
|
|
|
# gather the list of IPs to poll
|
2009-10-09 10:15:24 -06:00
|
|
|
ips = os.listdir(IP_DIR)
|
2009-10-07 19:02:57 -06:00
|
|
|
|
2009-10-09 10:15:24 -06:00
|
|
|
out = io.StringIO()
|
2009-10-08 17:07:15 -06:00
|
|
|
out.write(config.start_html('Team Service Availability'))
|
2009-10-07 16:54:47 -06:00
|
|
|
for ip in ips:
|
|
|
|
# check file name format is ip
|
|
|
|
if ip_re.match(ip) is None:
|
|
|
|
continue
|
|
|
|
|
2009-10-07 19:02:57 -06:00
|
|
|
# remove the file
|
2010-01-19 15:11:59 -07:00
|
|
|
try:
|
|
|
|
os.remove(os.path.join(IP_DIR, ip))
|
|
|
|
except Exception as e:
|
|
|
|
print('pollster: could not remove %s' % os.path.join(IP_DIR, ip))
|
|
|
|
traceback.print_exc()
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-08 08:49:50 -06:00
|
|
|
results = {}
|
2009-10-07 16:54:47 -06:00
|
|
|
|
|
|
|
if DEBUG is True:
|
|
|
|
print('ip: %s' % ip)
|
|
|
|
|
2009-10-08 11:11:41 -06:00
|
|
|
if out is not None:
|
|
|
|
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')
|
2009-10-08 08:49:50 -06:00
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
# perform polls
|
|
|
|
for service,func in POLLS.items():
|
2010-01-19 15:11:59 -07:00
|
|
|
try:
|
|
|
|
team = func(ip).decode('utf-8')
|
|
|
|
except:
|
2009-10-07 16:54:47 -06:00
|
|
|
team = 'dirtbags'
|
|
|
|
|
2009-10-08 08:49:50 -06:00
|
|
|
if DEBUG is True:
|
|
|
|
print('\t%s - %s' % (service, team))
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-08 11:11:41 -06:00
|
|
|
if out is not None:
|
|
|
|
out.write('<tr><td>%s</td><td>%s</td>\n' % (service, team))
|
|
|
|
|
2009-10-08 17:24:05 -06:00
|
|
|
pointscli.submit('svc.' + service, team, 1)
|
2009-10-08 11:11:41 -06:00
|
|
|
|
|
|
|
if out is not None:
|
|
|
|
out.write('</table>\n')
|
2009-10-08 16:42:26 -06:00
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
if DEBUG is True:
|
|
|
|
print('+-----------------------------------------+')
|
|
|
|
|
2010-01-19 15:11:59 -07:00
|
|
|
out.write('<p>Poll number: %d</p>' % poll_no)
|
|
|
|
poll_no += 1
|
|
|
|
|
2009-10-08 08:49:50 -06:00
|
|
|
t_end = time.time()
|
|
|
|
exec_time = int(t_end - t_start)
|
|
|
|
sleep_time = POLL_INTERVAL - exec_time
|
2009-10-07 16:54:47 -06:00
|
|
|
|
2009-10-08 11:11:41 -06:00
|
|
|
if out is not None:
|
2009-10-08 17:07:15 -06:00
|
|
|
out.write(config.end_html())
|
2009-10-08 08:49:50 -06:00
|
|
|
|
2009-10-09 10:15:24 -06:00
|
|
|
open(REPORT_PATH, 'w').write(out.getvalue())
|
2009-10-09 08:55:51 -06:00
|
|
|
|
2009-10-07 16:54:47 -06:00
|
|
|
# sleep until its time to poll again
|
2009-10-08 08:49:50 -06:00
|
|
|
time.sleep(sleep_time)
|
2009-10-07 16:54:47 -06:00
|
|
|
|