]> git.somenet.org - ctf/pub/submit_bot.git/blob - bots/submit_ictf_batch.py
GITOLITE.txt
[ctf/pub/submit_bot.git] / bots / submit_ictf_batch.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 submitFlags():
30     print("submitFlag(): starting...")
31     dbconn = None
32
33     try:
34         login()
35         global team
36
37         dbconn = psycopg2.connect(dbconnstring)
38         cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
39         cur.execute("SELECT * from flags where status = 0 or status = 4 order by random() limit 400")
40         print("submitFlag(): submitting " + str(cur.rowcount) + " flags...")
41         flist = [x['flag'] for x in cur.fetchall()]
42         result = team.submit_flag(flist)
43         resultlist = list(zip(flist,result))
44
45         for res in resultlist:
46             try:
47                 result = res[1]
48
49                 if "notactive" in result:
50                     cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
51                                 "status = %s, srvresponse = %s WHERE flag = %s",
52                                 (2, result, res[0]))
53
54                 elif "incorrect" in result:
55                     cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
56                                 "status = %s, srvresponse = %s WHERE flag = %s",
57                                 (2, result, res[0]))
58
59                 elif "correct" in result:
60                     cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
61                                 "status = %s, srvresponse = %s WHERE flag = %s",
62                                 (1, result, res[0]))
63
64                 elif "already" in result:
65                     cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
66                                 "status = %s, srvresponse = %s WHERE flag = %s",
67                                 (3, result, res[0]))
68
69
70                 else:
71                     import pprint
72                     pprint.pprint(result)
73                     cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
74                                 "status = %s, srvresponse = %s WHERE flag = %s",
75                                 (4, result, res[0]))
76
77                 dbconn.commit()
78             except psycopg2.DatabaseError as e:
79                 print('Error %s' % e)
80                 traceback.print_exc(file=sys.stdout)
81
82     except Exception as e:
83         print('Error %s' % e)
84         traceback.print_exc(file=sys.stdout)
85
86     try:
87         dbconn.close()
88     except AttributeError as e:
89         pass
90 #        print('Error %s' % e)
91 #        traceback.print_exc(file=sys.stdout)
92
93     except psycopg2.DatabaseError as e:
94         print('Error %s' % e)
95         traceback.print_exc(file=sys.stdout)
96
97     print("submitFlag(): ended.")
98     time.sleep(5.0)
99
100
101 def main():
102     print("*** starting ...")
103     while True:
104         submitFlags()
105
106 if __name__ == "__main__":
107     def signal_handler(signal, frame):
108         print('SIG received. exitting!')
109         sys.exit(0)
110     signal.signal(signal.SIGINT, signal_handler)
111     main()
112