From 6cdf6d501ca6541df5afee9e883fda4670637246 Mon Sep 17 00:00:00 2001 From: Someone Date: Sat, 17 Jan 2015 18:02:42 +0100 Subject: [PATCH] SubmitBot as of RuCTFe 2014. --- submitbot.py | 89 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 30 deletions(-) diff --git a/submitbot.py b/submitbot.py index 879e57a..b7895c3 100755 --- a/submitbot.py +++ b/submitbot.py @@ -8,33 +8,50 @@ import psycopg2.extras import sys import time import signal -import mechanize +import socket -def submit(flag): +def readlines(sock, recv_buffer=4096, delim='\n'): + buffer = '' + data = True + while data: + data = sock.recv(recv_buffer) + buffer += data + + while buffer.find(delim) != -1: + line, buffer = buffer.split('\n', 1) + yield line + return + +def submit(sock,flag): submission_success = False + fs=sock.makefile() + print "submitting flag: "+flag - browser = mechanize.Browser() - browser.open('http://10.10.40.200/SubmitFlagServlet') - - browser.select_form(nr=1) - browser.form["teamInput"] = "16" - browser.form["flagInput"] = flag - - response = browser.submit() - resp = response.read() - - if 'Status:scored' in resp: - return (1, 'Status:scored') - - if 'Status:resubmission' in resp: - return (2, 'Status:resubmission') - - if 'Status:denied' in resp: - return (3, 'Status:denied') - - if 'Status:expired' in resp: - return (2, 'Status:expired') - + sock.sendall(flag+"\n") + + resp = fs.readline()+"" + + if 'Accepted' in resp: + return (1, 'Accepted') + + if 'Denied: no such flag' in resp: + return (2, 'Denied: no such flag') + + if 'Denied: flag is too old' in resp: + return (2, 'Denied: flag is too old') + + if 'Denied: you already submitted this flag' in resp: + return (2, 'Denied: you already submitted this flag') + + if 'Denied: flag is your own' in resp: + return (2, 'Denied: flag is your own') + + if 'Denied: your appropriate service' in resp: + return (3, 'Denied: your appropriate service') + + print(resp) + + if 'Status:error' in resp: wantnext = False servresponse = "" @@ -45,11 +62,11 @@ def submit(flag): if 'Status:error' in line: wantnext = True return (2, 'Status:error::'+servresponse) - + # RETURN (success?, srvresponse) print resp return (0, '') - + def main(): sleeptime = 5 dbconn = None @@ -57,7 +74,7 @@ def main(): try: print "*** sleeping "+str(sleeptime)+" sec..." time.sleep(sleeptime) - dbconn = psycopg2.connect("host=127.0.0.1 dbname=postgres user=postgres password=dba") + dbconn = psycopg2.connect("host=127.0.0.1 port=5433 dbname=postgres user=postgres password=dba") cur = dbconn.cursor() cur.execute("CREATE TABLE IF NOT EXISTS flags (" "fid serial NOT NULL PRIMARY KEY," @@ -78,16 +95,28 @@ def main(): time.sleep(sleeptime) try: cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) - cur.execute("SELECT * from flags where status = 0 or status = 3") + cur.execute("SELECT * from flags where status = 0 or status = 3 limit 500") print "Fetched " + str(cur.rowcount) + " rows" - # rows = cur.fetchall() + if cur.rowcount == 0: + continue + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(("10.10.10.2", 31337)) + eating = True + fs=sock.makefile() + while eating: + resp = fs.readline()+"" + if resp.startswith('Enter your flags, finished with newline'): + eating = False + for row in cur.fetchall(): - (success, resp) = submit(row['flag']) + (success, resp) = submit(sock,row['flag']) if success != 0: cur.execute("UPDATE flags SET submitted = date_trunc('second', NOW()), " "status = %s, srvresponse = %s WHERE fid = %s", (success, resp, row['fid'])) dbconn.commit() + sock.shutdown(socket.SHUT_WR) + sock.close() except psycopg2.DatabaseError as e: print 'Error %s' % e try: -- 2.43.0