]> git.somenet.org - ctf/pub/submit_bot.git/blob - submitbot_tcp.py
modified: submitbot_tcp.py
[ctf/pub/submit_bot.git] / submitbot_tcp.py
1 #!/usr/bin/env python
2
3 # 2014 by Jan "Someone" Vales <someone@somenet.org>
4 # do not publish!
5
6 import psycopg2
7 import psycopg2.extras
8 import sys
9 import time
10 import signal
11 import socket
12
13 def readlines(sock, recv_buffer=4096, delim='\n'):
14         buffer = ''
15         data = True
16         while data:
17                 data = sock.recv(recv_buffer)
18                 buffer += data
19
20                 while buffer.find(delim) != -1:
21                         line, buffer = buffer.split('\n', 1)
22                         yield line
23         return
24
25 def submit(sock,flag):
26     submission_success = False
27     fs=sock.makefile()
28
29     print "submitting flag: "+flag
30     sock.sendall(flag+"\n")
31
32     resp = fs.readline()+""
33
34     if 'Accepted' in resp:
35         return (1, 'Accepted')
36
37     if 'Denied: no such flag' in resp:
38         return (2, 'Denied: no such flag')
39
40     if 'Denied: flag is too old' in resp:
41         return (2, 'Denied: flag is too old')
42
43     if 'Denied: you already submitted this flag' in resp:
44         return (2, 'Denied: you already submitted this flag')
45
46     if 'Denied: flag is your own' in resp:
47         return (2, 'Denied: flag is your own')
48
49     if 'Denied: your appropriate service' in resp:
50         return (3, 'Denied: your appropriate service')
51
52     print(resp)
53
54
55     if 'Status:error' in resp:
56         wantnext = False
57         servresponse = ""
58         for line in resp.splitlines():
59             if wantnext == True:
60                 wantnext = False
61                 servresponse = line
62             if 'Status:error' in line:
63                 wantnext = True
64         return (2, 'Status:error::'+servresponse)
65
66     # RETURN (success?, srvresponse)
67     print resp
68     return (0, '')
69
70 def main():
71     sleeptime = 5
72     dbconn = None
73     while True:
74         try:
75             print "*** sleeping "+str(sleeptime)+" sec..."
76             time.sleep(sleeptime)
77             dbconn = psycopg2.connect("host=127.0.0.1 port=5432 dbname=flagbot user=flagbot password=flagbotpw")
78             print "Connected to DB"
79
80             cur = None
81             while True:
82                 print "*** sleeping another "+str(sleeptime)+" sec..."
83                 time.sleep(sleeptime)
84                 try:
85                     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
86                     cur.execute("SELECT * from flags where status = 0 or status = 3 limit 500")
87                     print "Fetched " + str(cur.rowcount) + " rows"
88                     if cur.rowcount == 0:
89                         continue
90                     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
91                     sock.connect(("10.10.10.2", 31337))
92                     eating = True
93                     fs=sock.makefile()
94                     while eating:
95                         resp = fs.readline()+""
96                         if resp.startswith('Enter your flags, finished with newline'):
97                             eating = False
98
99                     for row in cur.fetchall():
100                         (success, resp) = submit(sock,row['flag'])
101                         if success != 0:
102                             cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
103                                 "status = %s, srvresponse = %s WHERE fid = %s",
104                                 (success, resp, row['fid']))
105                             dbconn.commit()
106                     sock.shutdown(socket.SHUT_WR)
107                     sock.close()
108                 except psycopg2.DatabaseError as e:
109                     print 'Error %s' % e
110                 try:
111                     cur.close()
112                     dbconn.rollback()
113                 except psycopg2.DatabaseError as e:
114                     print 'Error %s' % e
115                 cur = None
116                 dbconn.rollback()
117
118         except psycopg2.DatabaseError as e:
119             print 'Error %s' % e
120         try:
121             dbconn.close()
122         except psycopg2.DatabaseError as e:
123             print 'Error %s' % e
124         dbconn = None
125     print "should never be reached"
126
127 if __name__ == "__main__":
128     def signal_handler(signal, frame):
129         print 'SIG received. exitting!'
130         sys.exit(0)
131     signal.signal(signal.SIGINT, signal_handler)
132     main()