]> git.somenet.org - ctf/pub/submit_bot.git/blob - submitbot.py
adapted to esse-CTF
[ctf/pub/submit_bot.git] / submitbot.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 mechanize
12
13 def submit(flag):
14     submission_success = False
15     print "submitting flag: "+flag
16     browser = mechanize.Browser()
17     browser.open('http://10.10.40.200/SubmitFlagServlet')
18     
19     browser.select_form(nr=1)
20     browser.form["teamInput"] = "16"
21     browser.form["flagInput"] = flag
22
23     response = browser.submit()
24     resp = response.read()
25
26     if 'Status:scored' in resp:
27         return (1, 'Status:scored')
28       
29     if 'Status:resubmission' in resp:
30         return (2, 'Status:resubmission')
31       
32     if 'Status:denied' in resp:
33         return (3, 'Status:denied')
34       
35     if 'Status:expired' in resp:
36         return (2, 'Status:expired')
37       
38     if 'Status:error' in resp:
39         wantnext = False
40         servresponse = ""
41         for line in resp.splitlines():
42             if wantnext == True:
43                 wantnext = False
44                 servresponse = line
45             if 'Status:error' in line:
46                 wantnext = True
47         return (2, 'Status:error::'+servresponse)
48     
49     # RETURN (success?, srvresponse)
50     print resp
51     return (0, '')
52   
53 def main():
54     sleeptime = 5
55     dbconn = None
56     while True:
57         try:
58             print "*** sleeping "+str(sleeptime)+" sec..."
59             time.sleep(sleeptime)
60             dbconn = psycopg2.connect("host=127.0.0.1 dbname=postgres user=postgres password=dba")
61             cur = dbconn.cursor()
62             cur.execute("CREATE TABLE IF NOT EXISTS flags ("
63                 "fid serial NOT NULL PRIMARY KEY,"
64                 "flag character varying(32) NOT NULL UNIQUE,"
65                 "service character varying(32),"
66                 "received timestamp without time zone NOT NULL DEFAULT date_trunc('second', NOW()),"
67                 "submitted timestamp without time zone,"
68                 "status integer NOT NULL DEFAULT 0,"
69                 "srvresponse character varying(128)"
70                 ")")
71             dbconn.commit()
72             cur.close()
73             print "Connected to DB + table created"
74
75             cur = None
76             while True:
77                 print "*** sleeping another "+str(sleeptime)+" sec..."
78                 time.sleep(sleeptime)
79                 try:
80                     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
81                     cur.execute("SELECT * from flags where status = 0 or status = 3")
82                     print "Fetched " + str(cur.rowcount) + " rows"
83     #                rows = cur.fetchall()
84                     for row in cur.fetchall():
85                         (success, resp) = submit(row['flag'])
86                         if success != 0:
87                             cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
88                                 "status = %s, srvresponse = %s WHERE fid = %s",
89                                 (success, resp, row['fid']))
90                             dbconn.commit()
91                 except psycopg2.DatabaseError as e:
92                     print 'Error %s' % e
93                 try:
94                     cur.close()
95                     dbconn.rollback()
96                 except psycopg2.DatabaseError as e:
97                     print 'Error %s' % e
98                 cur = None
99                 dbconn.rollback()
100             
101         except psycopg2.DatabaseError as e:
102             print 'Error %s' % e
103         try:
104             dbconn.close()
105         except psycopg2.DatabaseError as e:
106             print 'Error %s' % e
107         dbconn = None
108     print "should never be reached"
109
110 if __name__ == "__main__":
111     def signal_handler(signal, frame):
112         print 'SIG received. exitting!'
113         sys.exit(0)
114     signal.signal(signal.SIGINT, signal_handler)
115     main()