forked from rahulkadavil/pentest-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhttp.php
executable file
·168 lines (146 loc) · 3.58 KB
/
testhttp.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
#!/usr/bin/php
<?php
// usage function
function usage( $error='' )
{
echo "Usage: php testhttp.php <host|ip> <port list>\n";
if( $error ) {
echo "Error: ".$error."!\n";
}
exit();
}
// test if a string is an IP address
function isIp( $str )
{
return preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', trim($str) );
}
// global vars
define( 'RESULT_SEP', ':' );
define( 'PORT_SEP', ',' );
define( 'HTTP_KO', 0 );
define( 'HTTP_OK', 1 );
define( 'HTTP_REDIR', 2 );
define( 'MAX_REDIR', 20 );
$t_result = [ 0=>'KO', 1=>'OK', 2=>'REDIR' ];
if( $_SERVER['argc']<2 || $_SERVER['argc']>3 ) {
usage();
}
$host = $_SERVER['argv'][1];
if( $_SERVER['argc'] == 3 ) {
$port = $_SERVER['argv'][2];
} else {
// default port
$port = '80,443';
}
$t_port = explode( PORT_SEP, $port );
$t_headers = [];
$t_headers[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$t_headers[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$t_headers[] = "Cache-Control: max-age=0";
$t_headers[] = "Connection: keep-alive";
$t_headers[] = "Keep-Alive: 300";
$t_headers[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$t_headers[] = "Accept-Language: en-us,en;q=0.5";
$t_headers[] = "Pragma: "; // browsers keep this blank.
// main loop
foreach( $t_port as $port )
{
$t_details = [];
$port_is_http = HTTP_OK;
$scheme = 'http';
if( $port == 443 ) {
$scheme .= 's';
}
$u = $scheme.'://'.$host.':'.$port;
$n_loop = 0;
//echo 'Testing '.$u."\n";
do
{
$n_loop++;
$loop = false;
$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $u );
curl_setopt( $c, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)' );
//curl_setopt( $c, CURLOPT_NOBODY, true );
curl_setopt( $c, CURLOPT_HTTPHEADER, $t_headers );
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 2 );
curl_setopt( $c, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
curl_exec( $c );
$t_info = curl_getinfo( $c );
//var_dump( $t_info );
curl_close( $c );
if( $t_info['http_code'] == 0 )
{
// http service NOT found
$port_is_http = HTTP_KO;
}
else
{
// http service found
$r = $t_info['redirect_url'];
//var_dump( $r );
if( trim($r) != '' )
{
$tmp = parse_url( $r );
//var_dump($tmp);
$u = $r;
$port_is_http = HTTP_REDIR;
$t_details[] = $tmp['host'];
if( $n_loop <= MAX_REDIR ) {
$loop = true;
}
/*
// but it's a redirection!
if( isIp($r) ) {
$h = $r;
} else {
// extract scheme, host and port of the redirection
$tmp = parse_url( $r );
//var_dump( $tmp );
$s = $tmp['scheme'];
$h = $tmp['host'];
if( !isset($tmp['port']) ) {
$p = ($s=='https') ? 443 : 80;
}
}
if( $s == $scheme && $h == $host && $p == $port ) {
// the redirection point to the exact same scheme, host and port
// so we keep looping
$u = $r;
//$port_is_http = HTTP_REDIR;
//$t_details[] = $tmp['host'];
$loop = true;
} else {
// the redirection DO NOT point to the exact same scheme, host and port
// so we leave
$u = $r;
$port_is_http = HTTP_REDIR;
$t_details[] = $tmp['host'];
//$loop = true;
}
*/
}
}
}
while( $loop );
$cnt = count($t_details);
/* if( $port_is_http == HTTP_REDIR && $t_details[$cnt-1]==$host ) {
$port_is_http = HTTP_OK;
$t_details = [];
$cnt = 0;
}
*/
echo $port.RESULT_SEP.$t_result[$port_is_http];
if( $cnt ) {
for( $i=0,$p=null ; $i<$cnt ; $p=$t_details[$i],$i++ ) {
if( $t_details[$i] != $p ) {
echo RESULT_SEP.$t_details[$i];
}
}
}
echo "\n";
}
// the end
exit();
?>