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")
79 cur.execute("CREATE TABLE IF NOT EXISTS flags ("
80 "fid serial NOT NULL PRIMARY KEY,"
81 "flag character varying(32) NOT NULL UNIQUE,"
82 "service character varying(32),"
83 "received timestamp without time zone NOT NULL DEFAULT date_trunc('second', NOW()),"
84 "submitted timestamp without time zone,"
85 "status integer NOT NULL DEFAULT 0,"
86 "srvresponse character varying(128)"
90 print "Connected to DB + table created"
94 print "*** sleeping another "+str(sleeptime)+" sec..."
97 cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
98 cur.execute("SELECT * from flags where status = 0 or status = 3 limit 500")
99 print "Fetched " + str(cur.rowcount) + " rows"
100 if cur.rowcount == 0:
102 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
103 sock.connect(("10.10.10.2", 31337))
107 resp = fs.readline()+""
108 if resp.startswith('Enter your flags, finished with newline'):
111 for row in cur.fetchall():
112 (success, resp) = submit(sock,row['flag'])
114 cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), "
115 "status = %s, srvresponse = %s WHERE fid = %s",
116 (success, resp, row['fid']))
118 sock.shutdown(socket.SHUT_WR)
120 except psycopg2.DatabaseError as e:
125 except psycopg2.DatabaseError as e:
130 except psycopg2.DatabaseError as e:
134 except psycopg2.DatabaseError as e:
137 print "should never be reached"
139 if __name__ == "__main__":
140 def signal_handler(signal, frame):
141 print 'SIG received. exitting!'
143 signal.signal(signal.SIGINT, signal_handler)