Skip to content

Instantly share code, notes, and snippets.

@grahamc

grahamc/loris.py Secret

Last active June 21, 2019 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamc/99cbfb4e828f24d97c7abb1bd53a35b8 to your computer and use it in GitHub Desktop.
Save grahamc/99cbfb4e828f24d97c7abb1bd53a35b8 to your computer and use it in GitHub Desktop.
import socketserver
import time
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The request handler 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 handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
self.request.send("HTTP/1.1 302 Found\n".encode())
self.request.send("Server: Slow loris\n".encode())
# Packet verifies the URL is legit before iPXE sees it, if this runs
# inside packet, this part is not needed. Just to make their check happy.
if "User-Agent: Ruby" not in self.data.decode():
print("Engaging slow loris mode")
# here we use a counter to simulate a long delay, waiting for a customer, but
# what you would want to actually do is something like:
# url = packetAPI.get_next_api_url()
# while url is None:
# self.request.send(f"X-SLOW-LORIS-IS-FASTER-THAN-BOOT: {counter}\n".encode())
# self.request.send(f"Location: {url}")
for counter in range(1, 100):
print(f"Engaging slow loris mode {counter}")
# just send back the same data, but upper-cased
self.request.send(f"X-SLOW-LORIS-IS-FASTER-THAN-BOOT: {counter}\n".encode())
time.sleep(1)
print("Sending real location now")
self.request.send("Location: http://gsc.io/<example>/netboot.ipxe\n\n".encode())
if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 9999
socketserver.TCPServer.allow_reuse_address = True
# Create the server, binding to localhost on port 9999
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
server.allow_reuse_address = True
# 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