3 # 2014 by Jan "Someone" Vales <someone@somenet.org>
13 def readlines(sock, recv_buffer=4096, delim='\n'):
17 data = sock.recv(recv_buffer)
20 while buffer.find(delim) != -1:
21 line, buffer = buffer.split('\n', 1)
25 def submit(sock,flag):
26 submission_success = False
29 print "submitting flag: "+flag
30 sock.sendall(flag+"\n")
32 resp = fs.readline()+""
34 if 'Accepted' in resp:
35 return (1, 'Accepted')
37 if 'Denied: no such flag' in resp:
38 return (2, 'Denied: no such flag')
40 if 'Denied: flag is too old' in resp:
41 return (2, 'Denied: flag is too old')
43 if 'Denied: you already submitted this flag' in resp:
44 return (2, 'Denied: you already submitted this flag')
46 if 'Denied: flag is your own' in resp:
47 return (2, 'Denied: flag is your own')
49 if 'Denied: your appropriate service' in resp:
50 return (3, 'Denied: your appropriate service')
55 if 'Status:error' in resp:
58 for line in resp.splitlines():
62 if 'Status:error' in line:
64 return (2, 'Status:error::'+servresponse)
66 # RETURN (success?, srvresponse)
75 print "*** sleeping "+str(sleeptime)+" sec..."
77 dbconn = psycopg2.connect("host=127.0.0.1 port=5432 dbname=flagbot user=flagbot password=flagbotpw")
78 print "Connected to DB"
82 print "*** sleeping another "+str(sleeptime)+" sec..."
85 cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
86 cur.execute("SELECT * from flags where status = 0 or status = 3 limit 500")
87 print "Fetched " + str(cur.rowcount) + " rows"
90 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
91 sock.connect(("10.10.10.2", 31337))
95 resp = fs.readline()+""
96 if resp.startswith('Enter your flags, finished with newline'):
99 for row in cur.fetchall():
100 (success, resp) = submit(sock,row['flag'])
102 cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
103 "status = %s, srvresponse = %s WHERE fid = %s",
104 (success, resp, row['fid']))
106 sock.shutdown(socket.SHUT_WR)
108 except psycopg2.DatabaseError as e:
113 except psycopg2.DatabaseError as e:
118 except psycopg2.DatabaseError as e:
122 except psycopg2.DatabaseError as e:
125 print "should never be reached"
127 if __name__ == "__main__":
128 def signal_handler(signal, frame):
129 print 'SIG received. exitting!'
131 signal.signal(signal.SIGINT, signal_handler)