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
31 sock.sendall(flag+"\n")
33 resp = fs.readline()+""
35 if 'Accepted' in resp:
36 return (1, resp.replace(flag,''))
38 if 'Denied: no such flag' in resp:
39 return (2, 'Denied: no such flag')
41 if 'Denied: invalid flag' in resp:
42 return (2, 'Denied: invalid flag')
44 if 'Denied: flag is too old' in resp:
45 return (2, 'Denied: flag is too old')
47 if 'Denied: you already submitted this flag' in resp:
48 return (3, 'Denied: you already submitted this flag')
50 if 'Denied: flag is your own' in resp:
51 return (2, 'Denied: flag is your own')
53 if 'Denied: your appropriate service' in resp:
54 return (4, 'Denied: your appropriate service')
56 # RETURN (success?, srvresponse)
65 print "*** sleeping "+str(sleeptime)+" sec..."
67 dbconn = psycopg2.connect("host=s.i port=5432 dbname=flagbot user=flagbot password=flagbotpw")
68 print "Connected to DB"
71 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
72 sock.connect(("flags.ructfe.org", 31337))
76 resp = fs.readline()+""
77 if resp.startswith('Enter your flags, finished with newline'):
82 cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
83 cur.execute("SELECT * from flags where status = 0 or status = 4 order by random() limit 1")
84 #print "Fetched " + str(cur.rowcount) + " rows"
86 print "*** sleeping another "+str(sleeptime)+" sec..."
89 for row in cur.fetchall():
90 # print "here" + str(row['fid'])
91 if row['flag'] is None or row['flag'].strip() == '':
93 (success, resp) = submit(sock,row['flag'])
95 cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), status = %s, srvresponse = %s WHERE fid = %s and status = 0 or status = 4",
96 (success, resp, row['fid']))
98 except psycopg2.DatabaseError as e:
103 except psycopg2.DatabaseError as e:
107 sock.shutdown(socket.SHUT_WR)
110 except psycopg2.DatabaseError as e:
114 except psycopg2.DatabaseError as e:
117 print "should never be reached"
119 if __name__ == "__main__":
120 def signal_handler(signal, frame):
121 print 'SIG received. exitting!'
123 signal.signal(signal.SIGINT, signal_handler)