Skip to content

Instantly share code, notes, and snippets.

@chitoge
Created April 2, 2015 04:46
Show Gist options
  • Save chitoge/10a6d7f1e1217fe252ac to your computer and use it in GitHub Desktop.
Save chitoge/10a6d7f1e1217fe252ac to your computer and use it in GitHub Desktop.
backdoorctf rapidfire
import socket, hashlib, time, requests
from geopy import GoogleV3
import re
import shelve
import omdb
host = '128.199.107.60'
port = 8008
rep_countrycode = False
def fib(n):
i = h = 1
j = k = 0
while (n > 0) :
if (n%2 == 1) : # when n is odd
t = j*h
j = i*h + j*k + t
i = i*k + t
t = h*h
h = 2*k*h + t
k = k*k + t
n = int(n/2)
return j
def get_country(place_name):
gapi = shelve.open('googly_cache', writeback=True)
try:
wat = place_name.encode('base64')
except UnicodeEncodeError:
wat = u' '.join(place_name).encode('utf-8').strip().encode('base64')
if (wat in gapi):
print('[*] Found in shelf')
loc = gapi[wat]
else:
print('[*] Request from GGAPI')
loc = geolocator.geocode(place_name).raw
gapi[wat] = loc
gapi.sync()
gapi.close()
for comp in loc['address_components']:
if 'country' in comp['types']:
if rep_countrycode:
return comp['short_name'] # TODO: not short_name but something else
else:
return comp['long_name']
def get_release(movie_name):
gapi = shelve.open('moviee_cache', writeback=True)
try:
wat = movie_name.encode('base64')
except UnicodeEncodeError:
wat = u' '.join(movie_name).encode('utf-8').strip().encode('base64')
if (wat in gapi):
print('[*] Found in shelf')
loc = gapi[wat]
else:
print('[*] Request from OMDB')
s = omdb.title(movie_name)
loc = s['year']
gapi[wat] = loc
gapi.sync()
gapi.close()
return loc
def read_until(wat):
buf = ''
while not (wat in buf):
buf += sock.recv(1)
return buf
def read_for_fun(sz):
d = ''
while (sz > 0):
tmp = sock.recv(sz)
sz -= len(tmp)
d += tmp
return d
# init connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
geolocator = GoogleV3()
pii = requests.get('http://www.angio.net/pi/digits/pi1000000.txt').text
# read & answer
while True:
s = sock.recv(8192)
if ('code is in CAPS' in s): rep_countrycode = True
if (s == ''): sleep(10)
print(s)
n = 'wat'
res = n
if ('sum' in s):
n = int(re.findall(r'first\ (\d+)\ ', s)[0])
if ('odd' in s):
res = n * n
elif ('fibonacci' in s):
res = fib(n+2) - 1
elif ('natural number' in s):
res = (n * (n + 1) // 2)
res = str(res)
elif ('prime' in s):
n = int(re.findall(r'the\ (\d+)(st|nd|rd|th)', s)[0][0]) + 1
n = str(n)
page = requests.get('http://numbersofprime.com/prime/' + n)
res = re.findall(r'\[([\d,]+)\]', page.text)[1]
res = res.replace(',', '')
res = res.strip()
elif ('md5' in s):
n = re.findall(r'of\ (.*)\n', s)[0]
res = hashlib.md5(n).hexdigest()
elif ('pi' in s):
n = int(re.findall(r'the\ (\d+)(st|nd|rd|th)', s)[0][0])
res = pii[n+1]
elif ('fibonacci' in s):
n = int(re.findall(r'the\ (\d+)(st|nd|rd|th)', s)[0][0])
res = str(fib(n))
elif ('binary' in s):
n = int(re.findall(r'of\ (\d+)\ in', s)[0])
res = bin(n)[2:]
elif ('country' in s):
n = re.findall(r'of\ (.*)\n', s)[0]
res = get_country(n)
elif ('release year' in s):
n = re.findall(r'of\ (.*)\n', s)[0]
res = get_release(n)
print '[*] n = ', n
print '[*] res = ', res
sock.sendall(res+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment