]> git.somenet.org - ctf/pub/submit_bot.git/blob - bots/submit_ictf.py
GITOLITE.txt
[ctf/pub/submit_bot.git] / bots / submit_ictf.py
1 #!/usr/bin/env python2
2 #
3 # Copyright 2015-2017 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
4 # send me your changes. credit author(s). do not publish. share alike.
5 # to be done: find a suitable licence text.
6 #
7
8 import psycopg2
9 import psycopg2.extras
10 import sys
11 import time
12 import signal
13 import socket
14 import threading
15 import traceback
16
17 import ictf
18
19 team = None
20 dbconnstring = "host=127.0.0.1 port=5432 dbname=flagbot user=flagbot password=flagbotpw"
21
22 def login(force = False):
23     global team
24     if force == True or team is None:
25         team = ictf.iCTF().login('ctf@w0y.at','ZmtphWHFUDwRWk6m')
26         print("login(): logged in.")
27
28
29 def submit(flag):
30     global team
31
32     try:
33         result = team.submit_flag([flag])
34         result = result[0]
35
36         if "notactive" in result:
37             return (2, result)
38         elif "incorrect" in result:
39             return (3, result)
40         elif "correct" in result:
41             return (1, result)
42         else:
43             return (4, result)
44
45     except Exception as e:
46         print('Error %s' % e)
47         traceback.print_exc(file=sys.stdout)
48
49
50 def main():
51     sleeptime = 1
52     dbconn = None
53     while True:
54         try:
55             print("*** sleeping "+str(sleeptime)+" sec...")
56             time.sleep(sleeptime)
57             dbconn = psycopg2.connect("host=s.i port=5432 dbname=flagbot user=flagbot password=flagbotpw")
58             print("Connected to DB")
59
60             cur = None
61             login(True)
62             global team
63
64             while True:
65                 try:
66                     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
67                     cur.execute("SELECT * from flags where status = 0 or status = 4 order by random() limit 1")
68                     if cur.rowcount == 0:
69                         print("*** sleeping another "+str(sleeptime)+" sec...")
70                         time.sleep(sleeptime)
71                         continue
72                     for row in cur.fetchall():
73                         if row['flag'] is None or row['flag'].strip() == '':
74                             continue
75                         (status, resp) = submit(row['flag'])
76                         print("submitted: "+row['flag']+" - response: ("+str(status)+") "+resp)
77                         if status != 0:
78                             cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), status = %s, srvresponse = %s WHERE fid = %s and status = 0 or status = 4",
79                                 (status, resp, row['fid']))
80                             dbconn.commit()
81                 except psycopg2.DatabaseError as e:
82                     print('Error %s' % e)
83                 try:
84                     cur.close()
85                     dbconn.rollback()
86                 except psycopg2.DatabaseError as e:
87                     print('Error %s' % e)
88                 cur = None
89                 dbconn.rollback()
90             sock.shutdown(socket.SHUT_WR)
91             sock.close()
92
93         except psycopg2.DatabaseError as e:
94             print('Error %s' % e)
95         try:
96             dbconn.close()
97         except psycopg2.DatabaseError as e:
98             print('Error %s' % e)
99         dbconn = None
100
101     print("should never be reached")
102
103
104 if __name__ == "__main__":
105     def signal_handler(signal, frame):
106         print('SIG received. exitting!')
107         sys.exit(0)
108     signal.signal(signal.SIGINT, signal_handler)
109     main()
110