/
LookingGlass.php
361 lines (338 loc) · 10.5 KB
/
LookingGlass.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
<?php
/**
* LookingGlass - User friendly PHP Looking Glass
*
* @package LookingGlass
* @author Nick Adams <nick@iamtelephone.com>
* @copyright 2015 Nick Adams.
* @link http://iamtelephone.com
* @license http://opensource.org/licenses/MIT MIT License
* @version 1.3.0
*/
namespace Telephone;
/**
* Create a Looking Glass with output buffering
*/
class LookingGlass
{
/**
* Execute a 'host' command against given host:
* Host is a simple utility for performing DNS lookups
*
* @param string $host
* IP/URL to perform command against
* @return boolean
* True on success
*/
public function host($host)
{
if ($host = $this->validate($host)) {
return $this->procExecute('host', $host);
}
return false;
}
/**
* Execute a 'mtr' command against given host:
* Mtr combines the functionality of the traceroute and ping programs in a
* single network diagnostic tool.
*
* @param string $host
* IP/URL to perform command against
* @return boolean
* True on success
*/
public function mtr($host)
{
if ($host = $this->validate($host)) {
return $this->procExecute('mtr -4 --report --report-wide', $host);
}
return false;
}
/**
* Execute a 'mtr6' command against given host:
* Mtr combines the functionality of the traceroute and ping programs in a
* single network diagnostic tool.
*
* @param string $host
* IP/URL to perform command against
* @return boolean
* True on success
*/
public function mtr6($host)
{
if ($host = $this->validate($host, 6)) {
return $this->procExecute('mtr -6 --report --report-wide', $host);
}
return false;
}
/**
* Execute a 'ping' command against given host:
* Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit
* an ICMP ECHO_RESPONSE from a host or gateway.
*
* @param string $host
* IP/URL to perform command against
* @param intger $count
* Number of ping requests
* @return boolean
* True on success
*/
public function ping($host, $count = 4)
{
if ($host = $this->validate($host)) {
return $this->procExecute('ping -c' . $count . ' -w15', $host);
}
return false;
}
/**
* Execute a 'ping6' command against given host:
* Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit
* an ICMP ECHO_RESPONSE from a host or gateway.
*
* @param string $host
* IP/URL to perform command against
* @param intger $count
* Number of ping requests
* @return boolean
* True on success
*/
public function ping6($host, $count = 4)
{
if ($host = $this->validate($host, 6)) {
return $this->procExecute('ping6 -c' . $count . ' -w15', $host);
}
return false;
}
/**
* Execute a 'traceroute' command against given host:
* Traceroute tracks the route packets taken from an IP network on their
* way to a given host.
*
* @param string $host
* IP/URL to perform command against
* @param intger $fail
* Number of failed hops before exiting command
* @return boolean
* True on success
*/
public function traceroute($host, $fail = 2)
{
if ($host = $this->validate($host)) {
return $this->procExecute('traceroute -4 -w2', $host, $fail);
}
return false;
}
/**
* Execute a 'traceroute6' command against given host:
* Traceroute tracks the route packets taken from an IP network on their
* way to a given host.
*
* @param string $host
* IP/URL to perform command against
* @param intger $fail
* Number of failed hops before exiting command
* @return boolean
* True on success
*/
public function traceroute6($host, $fail = 2)
{
if ($host = $this->validate($host, 6)) {
return $this->procExecute('traceroute -6 -w2', $host, $fail);
}
return false;
}
// ==================================================================
//
// Internal functions
//
// ------------------------------------------------------------------
/**
* Execute command, and open pipe for input/output
* This is a work around to terminate the command if X consecutive
* timeouts occur (traceroute)
*
* @param string $cmd
* Command to perform
* @param string $host
* IP/URL to issue command against
* @param integer $failCount
* Number of consecutive failed hops (traceroute)
* @return boolean
* True on success
*/
private function procExecute($cmd, $host, $failCount = 2)
{
// define output pipes
$spec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
// sanitize + remove single quotes
$host = str_replace('\'', '', filter_var($host, FILTER_SANITIZE_URL));
// execute command
$process = proc_open("{$cmd} '{$host}'", $spec, $pipes, null);
// check pipe exists
if (!is_resource($process)) {
return false;
}
// check for mtr/traceroute
if (strpos($cmd, 'mtr') !== false) {
$type = 'mtr';
} elseif (strpos($cmd, 'traceroute') !== false) {
$type = 'traceroute';
} else {
$type = '';
}
$fail = 0;
$match = 0;
$traceCount = 0;
$lastFail = 'start';
// iterate stdout
while (($str = fgets($pipes[1], 1024)) != null) {
// check for output buffer
if (ob_get_level() == 0) {
ob_start();
}
// fix RDNS XSS (outputs non-breakble space correctly)
$str = htmlspecialchars(trim($str));
// correct output for mtr
if ($type === 'mtr') {
if ($match < 10 && preg_match('/^[0-9]\. /', $str, $string)) {
$str = preg_replace('/^[0-9]\. /', ' ' . $string[0], $str);
$match++;
} else {
$str = preg_replace('/^[0-9]{2}\. /', ' ' . substr($str, 0, 4), $str);
}
}
// correct output for traceroute
elseif ($type === 'traceroute') {
if ($match < 10 && preg_match('/^[0-9] /', $str, $string)) {
$str = preg_replace('/^[0-9] /', ' ' . $string[0], $str);
$match++;
}
// check for consecutive failed hops
if (strpos($str, '* * *') !== false) {
$fail++;
if ($lastFail !== 'start'
&& ($traceCount - 1) === $lastFail
&& $fail >= $failCount
) {
echo str_pad($str . '<br />-- Traceroute timed out --<br />', 1024, ' ', STR_PAD_RIGHT);
break;
}
$lastFail = $traceCount;
}
$traceCount++;
}
// pad string for live output
echo str_pad($str . '<br />', 1024, ' ', STR_PAD_RIGHT);
// flush output buffering
@ob_flush();
flush();
}
// iterate stderr
while (($err = fgets($pipes[2], 1024)) != null) {
// check for IPv6 hostname passed to IPv4 command, and vice versa
if (strpos($err, 'Name or service not known') !== false || strpos($err, 'unknown host') !== false) {
echo 'Unauthorized request';
break;
}
}
$status = proc_get_status($process);
if ($status['running'] == true) {
// close pipes that are still open
foreach ($pipes as $pipe) {
fclose($pipe);
}
// retrieve parent pid
$ppid = $status['pid'];
// use ps to get all the children of this process
$pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
// kill remaining processes
foreach($pids as $pid) {
if (is_numeric($pid)) {
posix_kill($pid, 9);
}
}
proc_close($process);
}
return true;
}
/**
* Validate IP and URL
*
* @param string $host
* IP/URL to validate
* @param integer $type
* Define 4 or 6 for IPv4 or IPv6 validation
* @return boolean
* True on success
*/
private function validate($host, $type = 4)
{
// validate ip
if ($this->validIP($host, $type)) {
return $host;
}
// validate that IPv4 doesn't pass as a valid URL for IPv6
elseif ($type === 6 && $this->validIP($host, 4)) {
return false;
}
// validate url
elseif ($host = $this->validUrl($host)) {
return $host;
}
return false;
}
/**
* Validate IP: IPv4 or IPv6
*
* @param string $host
* IP to validate
* @param integer $type
* Define 4 or 6 for IPv4 or IPv6 validation
* @return boolean
* True on success
*/
private function validIP($host, $type = 4)
{
// validate IPv4
if ($type === 4) {
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) {
return true;
}
}
// validate IPv6
else {
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return true;
}
}
return false;
}
/**
* Validate hostname/URL
*
* @param string $url
* Hostname or URL to validate
* @return boolean
* True on success
*/
private function validUrl($url)
{
// check for http
if (stripos($url, 'http') === false) {
$url = 'http://' . $url;
}
// validate url
if (filter_var($url, FILTER_VALIDATE_URL)) {
// parse url for host
if ($host = parse_url($url, PHP_URL_HOST)) {
return $host;
}
return $url;
}
return false;
}
}