]> git.somenet.org - ctf/pub/submit_bot.git/blob - bots/get_targets_ictf.py
GITOLITE.txt
[ctf/pub/submit_bot.git] / bots / get_targets_ictf.py
1 #!/usr/bin/env python3
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 pprint
16 import traceback
17
18 import ictf
19
20 team = None
21 dbconnstring = "host=127.0.0.1 port=5432 dbname=flagbot user=flagbot password=flagbotpw"
22
23 def login(force = False):
24     global team
25     if force == True or team is None:
26         team = ictf.iCTF().login('ctf@w0y.at','ZmtphWHFUDwRWk6m')
27         print("login(): logged in.")
28
29 def getTargets():
30     print("getTargets() starting...")
31     try:
32         login(True)
33         global team
34
35         service_list = team.get_service_list()
36     #dbconn = psycopg2.connect(dbconnstring)
37     #cur = dbconn.cursor()
38     #cur.execute("DELETE FROM flag_ids")
39     #dbconn.commit()
40
41         for service in service_list:
42             try:
43                 print("getTargets() getting for:"+str(service['service_name']))
44                 targets = team.get_targets(service['service_id'])
45                 print("getTargets() got "+str(len(targets['targets']))+" targets.")
46                 for target in targets['targets']:
47                     try:
48                         dbconn = psycopg2.connect(dbconnstring)
49                         cur = dbconn.cursor()
50                         cur.execute("INSERT INTO flag_ids (service, team, host, port, flag_id) VALUES (%s, %s, %s, %s, %s)",
51                                     (service['service_name'], target['team_name'], target['hostname'], target['port'], target['flag_id']))
52                         dbconn.commit()
53                     except psycopg2.IntegrityError as e:
54                         pass
55
56                     except psycopg2.DatabaseError as e:
57                         print('getTargets() Error %s' % e)
58                         traceback.print_exc(file=sys.stdout)
59
60             except Exception as e:
61                 print('getTargets() Error %s' % e)
62                 traceback.print_exc(file=sys.stdout)
63
64     except Exception as e:
65         print('getTargets() Error %s' % e)
66         traceback.print_exc(file=sys.stdout)
67
68
69     time.sleep(10.0)
70     gettargetth = threading.Timer(0.5, getTargets)
71     gettargetth.start()
72     print("getTargets(): ended")
73
74 def main():
75     print("*** starting ...")
76     getTargets()
77
78 if __name__ == "__main__":
79     def signal_handler(signal, frame):
80         print('SIG received. exitting!')
81         sys.exit(0)
82     signal.signal(signal.SIGINT, signal_handler)
83     main()
84