-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMultiPortScan.php
executable file
·367 lines (346 loc) · 8.86 KB
/
MultiPortScan.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
class MultiPortScan
{
private $connect_number = 0;
private $connect_number_limit = 500;
private $connect_timeout = 2000000;
private $connect_usleep = 400000;
private $socket_ip = "0.0.0.0";
private $socket_retry = 0;
private $connected_socket_array = array();
private $done_socket_array = array();
private $supported_protocols = [ 'icmp' , 'tcp' , 'udp' ];
private $verbose = false;
public function __construct()
{
return;
}
public function set_verbose($bool)
{
$this->verbose = $bool;
}
public function print_verbose($msg)
{
if( $this->verbose )
{
echo $msg;
}
}
public function scan_udp($ip,$port_start,$port_end,$limit)
{
$start = intval($port_start);
$end = intval($port_end);
if( $end < $start )
{
echo "Invalid port range: $start -> $end\n";
return -1;
}
//Create a UDP socket
if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
print("Couldn't create socket: [$errorcode] $errormsg \n");
return -2;
}
$this->print_verbose("Socket created \n");
// Set timeout
$this->print_verbose("Using usleep recv timeout: $limit\n");
if( ! socket_set_option($sock,SOL_SOCKET,SO_RCVTIMEO,array('sec'=>$limit,'usec'=>0)) )
{
echo "Unable to set SO_RCVTIMEO: " . socket_strerror(socket_last_error()) . "\n";
return -3;
}
$this->print_verbose( "Port range: $start -> $end\n" );
for($i=$start; $i < $end; $i++)
{
//Send back the data to the client
socket_sendto($sock, "pingpong" , 100 , 0 , $ip , $i);
$this->print_verbose( "Waiting for data ... \n" );
//Receive some data
if( ($r = socket_recvfrom($sock, $buf, 512, 0, $ip, $i)) !== FALSE )
{
$this->connected_socket_array[$ip] = array(
"socket" => $sock
,"wait_count" => 0
,"state" => 0
,"retry" => 0
);
echo "[port:" . intval($i) . "] response\n";
}
}
$this->print_verbose("Closing socket\n");
socket_close($sock);
return 0;
}
public function scan_port($ip, $port_start, $port_end, $limit=1000)
{
$this->connect_number_limit = $limit;
$this->socket_ip = $ip;
$port = $port_start;
while($port != $port_end+1)
{
if($this->connect_number >= $this->connect_number_limit)
{
usleep($this->connect_usleep);
$this->refresh_connect();
continue;
}
$this->connected_socket_array[$port] = array(
"socket" => $this->create_tcp_connect($ip, $port)
,"wait_count" => 0
,"state" => 0
,"retry" => 0
);
if($port % 1000 == 0)
{
echo "scanning to port: ~ $port\n";
if(count($this->done_socket_array) != 0)
print_r($this->done_socket_array);
}
//usleep(1000);
$this->connect_number++;
$port++;
}
$this->wait_connected();
}
private function wait_connected()
{
while($this->connect_number != 0)
{
usleep($this->connect_usleep);
$this->refresh_connect();
}
print_r($this->done_socket_array);
}
public function create_tcp_connect($ip, $port)
{
return $this->create_connection("tcp",$ip,$port);
}
public function test_tcp_connect($socket, $ip, $port)
{
@socket_connect($socket, $ip, $port);
$err = socket_last_error($socket);
if ($err == 36 || $err == 37 || $err == 114 || $err == 115)
{
return 1;//waiting
}
elseif($err == 56 || $err == 106)
{
return 2;//connect success
}
elseif($err == 61 || $err == 111)
{
return 3;//failure
}
else
{
echo $err ." ".socket_strerror($err) . "\n";
return 0;
}
}
public function scan_ping($ip_prefix, $ip_start, $ip_end, $limit=1000)
{
$this->connect_number_limit = $limit;
$this->socket_ip = $ip_prefix;
$ip = $ip_start;
while($ip != $ip_end+1)
{
if($this->connect_number >= $this->connect_number_limit)
{
usleep($this->connect_usleep);
$this->refresh_icmp_connect();
continue;
}
$this->connected_socket_array[$ip] = array(
"socket" => $this->create_icmp_connect($ip)
,"wait_count" => 0
,"state" => 0
,"retry" => 0
);
if($ip % 20 == 0)
{
echo "scanning to port: ~ $ip\n";
//if(count($this->done_socket_array) != 0)
// print_r($this->done_socket_array);
}
//usleep(1000);
$this->connect_number++;
$ip++;
}
$this->wait_icmp_connected();
}
public function create_icmp_connect($ip)
{
return $this->create_connection( "icmp", $ip, null, "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost" );
}
public function create_connection( $proto, $ip_prefix, $port = null, $send_payload_now=null )
{
if( ! $this->proto_is_supported($proto) )
{
echo "Protocol $proto not supported [create_connection]\n";
return -1;
}
$payload=$sock_type="";
switch( $proto )
{
case "tcp":
$sock_type = SOCK_STREAM;
$payload="";
break;
case "icmp":
$sock_type = SOCK_RAW;
break;
default:
echo "Unknown protocol passed: $proto [create_connection]\n";
return -2;
}
//echo $sock_type . " " . getprotobyname($proto) . " " . $proto . "\n";
$socket = socket_create(AF_INET, $sock_type , getprotobyname($proto)) or die("Unable to create socket\n");
socket_set_nonblock($socket);
// Not sure why this is appending an ip to an ip // fix ip to ip_prefix
socket_connect($socket, "{$this->socket_ip}.$ip_prefix", $port);
if( $send_payload_now )
{
socket_send($socket, $send_payload_now, strlen($send_payload_now), 0);
}
return $socket;
}
private function refresh_icmp_connect()
{
return $this->refresh("icmp");
}
private function refresh_connect()
{
return $this->refresh("tcp");
}
public function proto_is_supported($proto)
{
return in_array($proto,$this->supported_protocols);
}
public function refresh($proto)
{
if( ! $this->proto_is_supported($proto) )
{
echo "Protocol $proto not supported [refresh]\n";
return -1;
}
if( $proto == 'udp' )
{
$suffix = "";
}
else
{
$suffix = "_connect";
}
$testFunc = "test_${proto}${suffix}";
$createFunc = "create_${proto}${suffix}";
$success_port = array();
foreach($this->connected_socket_array as $ip => &$socket)
{
$state = $this->$testFunc( $socket["socket"], $this->socket_ip, $ip );
//echo "port". $ip . " " . $state." ".$socket["wait_count"]."\n";
if($state == 1 || $state == 3)
{
$socket["wait_count"] ++;
if($state == 3 || $socket["wait_count"]*$this->connect_usleep >= $this->connect_timeout)
{
if($state == 3 && $socket["retry"] < $this->socket_retry)
{
//retry
$socket["retry"] = 1;
$socket["wait_count"] = 0;
socket_close($socket["socket"]);
$socket["socket"] = $this->$createFunc($ip);
}
else
{
$socket["state"] = 3; // timeout
$success_port[$ip] = false;
}
}
else
{
$socket["state"] = 1; // still wait
}
}
elseif($state == 2)
{
$socket["state"] = 2;//success
$success_port[$ip] = true;
}
}
foreach($success_port as $ip => $value)
{
$this->done_socket_array[$ip] = (bool)$value;
socket_set_block($this->connected_socket_array[$ip]["socket"]);
socket_close($this->connected_socket_array[$ip]["socket"]);
$this->connect_number--;
unset($this->connected_socket_array[$ip]);
}
return 0;
}
public function test_icmp_connect($socket,$ignored=null,$ignored2=null)
{
if (socket_read($socket, 255))
return 2;
else
return 1;
}
private function wait_icmp_connected()
{
while($this->connect_number != 0)
{
usleep($this->connect_usleep);
$this->refresh_icmp_connect();
}
ksort($this->done_socket_array);
foreach($this->done_socket_array as $ip => $v)
{
if($v)
echo "{$this->socket_ip}.$ip ===PING:SUCCESS===\n";
else
echo "{$this->socket_ip}.$ip ---PING:Failure\n";
}
}
}
function usage()
{
$a = <<<EOF
MultiPortScan Version 3.0
=========================
Original Author: Andy - [email protected] - https://github.com/poi5305
Other Author: William Merfalen - [email protected] - https://github.com/iteratedlateralus
usage: ./thisfile ping|port ip range_start range_end socket_number
EXAMPLES:
# Perform an ICMP network scan for hosts 192.168.0.1-25
./thisfile ping 192.168.0 1 25 0 #Note: 192.168.0 is not a typo
# Perform a TCP scan of ports 1 through 500 on localhost
./thisfile port 127.0.0.1 1 500 8
# Perform a UDP scan of ports 53 through 137 on localhost
./thisfile udp 127.0.0.1 53 137 0
NOTE: In order to use ICMP scan, you must run the script as r00t since we are opening a raw socket
EOF;
return $a;
}
/* If being called from the commandline */
if( isset($argc) )
{
echo "Commandline interface\n";
echo "=====================\n";
$a = new MultiPortScan();
if($argc < 6)
{
die(usage());
}
//if( $argv[$i] == '-v' )
//{
// $a->set_verbose(true);
//}
$type = "scan_".$argv[1];
if( $argv[3] > $argv[4] )
{
die("Invalid port range: $argv[3] $argv[4]\n");
}
$a->$type ($argv[2], $argv[3], $argv[4], $argv[5]);
}