Created
November 20, 2012 09:06
-
-
Save MichaelMayorov/4116862 to your computer and use it in GitHub Desktop.
Script for automatic posting flags on competitions like RuCTF(e)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
# Copyright::MichaelMayorov([email protected]) | |
""" | |
Simple python(tested on 2.7.3) script for automatic posting flags on competitions like RuCTF(e) | |
It stores flags in sqlite3 database which located in same directory with script. | |
Features: | |
- You could paste all flags in one line divided by whitespaces as well as one per line | |
- If connection to jury server failed, script will try to reconnects after timeout*2 secs, | |
where timeout increasing after each try. | |
""" | |
import sqlite3 | |
import sys | |
import os | |
import re | |
import socket | |
import time | |
REGEXP = r"^\w{31}=$" | |
DIR_NAME = os.path.dirname(sys.argv[0]) | |
SRV_ADDR = "localhost" | |
SRV_PORT = 31337 | |
TM_NUMBER_REQUIRED = True | |
TM_NUMBER = 73 | |
try: | |
sock = socket.create_connection( (SRV_ADDR, SRV_PORT), timeout=15 ) | |
except (socket.error, socket.timeout, socket.gaierror) as e: | |
print "[-]", e | |
sys.exit(1) | |
conn = sqlite3.connect(DIR_NAME + "/flags.sqlite3") | |
cur = conn.cursor() | |
conn.execute("create table if not exists FLAGS (id integer primary key, flag varchar unique)") | |
def send_team_number(sock): | |
sock.recv(1024) # receive server's greeting | |
sock.send("%s\n" % TM_NUMBER) | |
sock.recv(1024) | |
def send_flag(sock, flag): | |
sock.send("%s\n" % flag) | |
feedback = sock.recv(1024) | |
if not feedback: # Oops, server has closed connection | |
raise socket.error | |
print feedback | |
def reconnect(): | |
reconnect_timeout = 1 | |
while True: | |
print "[!] Reconnect after %d secs" % reconnect_timeout | |
time.sleep(reconnect_timeout) | |
try: | |
sock = socket.create_connection( (SRV_ADDR, SRV_PORT), timeout=5 ) | |
print "[+] Success!" | |
if TM_NUMBER_REQUIRED: | |
send_team_number(sock) | |
return sock | |
except socket.timeout: | |
print "Timeout..." | |
except socket.error: | |
print "Server goes down..." | |
reconnect_timeout *= 2 | |
print "[+] Connected to %s:%s, now post flags" % (SRV_ADDR, SRV_PORT) | |
# If required, send team number | |
if TM_NUMBER_REQUIRED: | |
send_team_number(sock) | |
while True: | |
data = sys.stdin.readline() | |
chunks = data.split() | |
for chunk in chunks: | |
is_flag_here = re.findall(REGEXP, chunk) | |
if not is_flag_here: | |
print "NOT A FLAG", chunk | |
continue | |
print "FLAG FOUND", is_flag_here | |
flag_t = (is_flag_here[0],) # for securuty reasons ;) | |
cur.execute("SELECT * FROM FLAGS WHERE flag=?", flag_t) | |
is_flag_in_db = cur.fetchall() | |
if not is_flag_in_db: | |
# Post flag | |
print "[!] SENDING NEW FLAG TO JURY" | |
try: | |
send_flag(sock, flag_t[0]) | |
except socket.error: | |
sock = reconnect() | |
send_flag(sock, flag_t[0]) | |
print "[!] SAVING FLAG IN DB", flag_t | |
cur.execute("INSERT INTO FLAGS VALUES (NULL, ?)", flag_t) | |
conn.commit() | |
continue | |
print "FLAG EXISTS", flag_t | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment