]> git.somenet.org - ctf/pub/submit_bot.git/blob - submitbot_tcp.py
modified: index.php
[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
31     sock.sendall(flag+"\n")
32
33     resp = fs.readline()+""
34     print resp
35     if 'Accepted' in resp:
36         return (1, resp.replace(flag,''))
37
38     if 'Denied: no such flag' in resp:
39         return (2, 'Denied: no such flag')
40
41     if 'Denied: invalid flag' in resp:
42         return (2, 'Denied: invalid flag')
43
44     if 'Denied: flag is too old' in resp:
45         return (2, 'Denied: flag is too old')
46
47     if 'Denied: you already submitted this flag' in resp:
48         return (3, 'Denied: you already submitted this flag')
49
50     if 'Denied: flag is your own' in resp:
51         return (2, 'Denied: flag is your own')
52
53     if 'Denied: your appropriate service' in resp:
54         return (4, 'Denied: your appropriate service')
55
56     # RETURN (success?, srvresponse)
57     print resp
58     return (4, 'WTF?!')
59
60 def main():
61     sleeptime = 1
62     dbconn = None
63     while True:
64         try:
65             print "*** sleeping "+str(sleeptime)+" sec..."
66             time.sleep(sleeptime)
67             dbconn = psycopg2.connect("host=127.0.0.1 port=5432 dbname=flagbot user=flagbot password=flagbotpw")
68             print "Connected to DB"
69
70             cur = None
71             while True:
72                 try:
73                     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
74                     cur.execute("SELECT * from flags where status = 0 or status = 4 limit 2")
75                     print "Fetched " + str(cur.rowcount) + " rows"
76                     if cur.rowcount == 0:
77                         print "*** sleeping another "+str(sleeptime)+" sec..."
78                         time.sleep(sleeptime)
79                         continue
80                     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
81                     sock.connect(("flags.ructfe.org", 31337))
82                     eating = True
83                     fs=sock.makefile()
84                     while eating:
85                         resp = fs.readline()+""
86                         if resp.startswith('Enter your flags, finished with newline'):
87                             eating = False
88
89                     for row in cur.fetchall():
90                         if row['flag'] is None or row['flag'].strip() == '':
91                             continue                                            
92                         (success, resp) = submit(sock,row['flag'])
93                         if success != 0:
94                             cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
95                                 "status = %s, srvresponse = %s WHERE fid = %s",
96                                 (success, resp, row['fid']))
97                             dbconn.commit()
98                     sock.shutdown(socket.SHUT_WR)
99                     sock.close()
100                 except psycopg2.DatabaseError as e:
101                     print 'Error %s' % e
102                 try:
103                     cur.close()
104                     dbconn.rollback()
105                 except psycopg2.DatabaseError as e:
106                     print 'Error %s' % e
107                 cur = None
108                 dbconn.rollback()
109
110         except psycopg2.DatabaseError as e:
111             print 'Error %s' % e
112         try:
113             dbconn.close()
114         except psycopg2.DatabaseError as e:
115             print 'Error %s' % e
116         dbconn = None
117     print "should never be reached"
118
119 if __name__ == "__main__":
120     def signal_handler(signal, frame):
121         print 'SIG received. exitting!'
122         sys.exit(0)
123     signal.signal(signal.SIGINT, signal_handler)
124     main()