Projet

Général

Profil

Télécharger (15,7 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / diag_packet_capture.php @ 715d2895

1
<?php
2
/*
3

    
4
	Redistribution and use in source and binary forms, with or without
5
	modification, are permitted provided that the following conditions are met:
6

    
7
	1. Redistributions of source code must retain the above copyright notice,
8
	this list of conditions and the following disclaimer.
9

    
10
	2. Redistributions in binary form must reproduce the above copyright
11
	notice, this list of conditions and the following disclaimer in the
12
	documentation and/or other materials provided with the distribution.
13

    
14
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
18
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23
	POSSIBILITY OF SUCH DAMAGE.
24
*/
25

    
26
/*
27
	pfSense_BUILDER_BINARIES:	/bin/ps	/usr/bin/grep	/usr/sbin/tcpdump
28
	pfSense_MODULE:	routing
29
*/
30

    
31
##|+PRIV
32
##|*IDENT=page-diagnostics-packetcapture
33
##|*NAME=Diagnostics: Packet Capture page
34
##|*DESCR=Allow access to the 'Diagnostics: Packet Capture' page.
35
##|*MATCH=diag_packet_capture.php*
36
##|-PRIV
37

    
38
$allowautocomplete = true;
39

    
40
function fixup_not($value) {
41
	return str_replace("!", "not ", $value);
42
}
43
function strip_not($value) {
44
	return ltrim($value, '!');
45
}
46

    
47
if ($_POST['downloadbtn'] == gettext("Download Capture"))
48
	$nocsrf = true;
49

    
50
$pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
51
require_once("guiconfig.inc");
52
require_once("pfsense-utils.inc");
53

    
54
$fp = "/root/";
55
$fn = "packetcapture.cap";
56
$snaplen = 0;//default packet length
57
$count = 100;//default number of packets to capture
58

    
59
$fams = array('ip', 'ip6');
60
$protos = array('icmp', 'icmp6', 'tcp', 'udp', 'arp', 'carp', 'esp',
61
		'!icmp', '!icmp6', '!tcp', '!udp', '!arp', '!carp', '!esp');
62

    
63
$input_errors = array();
64

    
65
$interfaces = get_configured_interface_with_descr();
66
if (isset($config['ipsec']['enable']))
67
	$interfaces['ipsec'] = "IPsec";
68
foreach (array('server', 'client') as $mode) {
69
	if (is_array($config['openvpn']["openvpn-{$mode}"])) {
70
		foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
71
			if (!isset($setting['disable'])) {
72
				$interfaces['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
73
			}
74
		}
75
	}
76
}
77

    
78
if ($_POST) {
79
	$host = $_POST['host'];
80
	$selectedif = $_POST['interface'];
81
	$count = $_POST['count'];
82
	$snaplen = $_POST['snaplen'];
83
	$port = $_POST['port'];
84
	$detail = $_POST['detail'];
85
	$fam = $_POST['fam'];
86
	$proto = $_POST['proto'];
87

    
88
	if (!array_key_exists($selectedif, $interfaces)) {
89
		$input_errors[] = gettext("Invalid interface.");
90
	}
91
	if ($fam !== "" && $fam !== "ip" && $fam !== "ip6") {
92
		$input_errors[] = gettext("Invalid address family.");
93
	}
94
	if ($proto !== "" && !in_array(strip_not($proto), $protos)) {
95
		$input_errors[] = gettext("Invalid protocol.");
96
	}
97
	
98
	if ($host != "") {
99
		if (!is_subnet(strip_not($host)) && !is_ipaddr(strip_not($host))) {
100
			$input_errors[] = sprintf(gettext("A valid IP address or CIDR block must be specified. [%s]"), $host);
101
		}
102
	}
103
	if ($port != "") {
104
		if (!is_port(strip_not($port))) {
105
			$input_errors[] = gettext("Invalid value specified for port.");
106
		}
107
	}
108
	if ($snaplen == "") {
109
		$snaplen = 0;
110
	} else {
111
		if (!is_numeric($snaplen) || $snaplen < 0) {
112
			$input_errors[] = gettext("Invalid value specified for packet length.");
113
		}
114
	}
115
	if ($count == "") {
116
		$count = 0;
117
	} else {
118
		if (!is_numeric($count) || $count < 0) {
119
			$input_errors[] = gettext("Invalid value specified for packet count.");
120
		}
121
	}
122

    
123
	if (!count($input_errors)) {
124
		$do_tcpdump = true;
125

    
126
		conf_mount_rw();
127

    
128
		if ($_POST['promiscuous']) {
129
			//if promiscuous mode is checked
130
			$disablepromiscuous = "";
131
		} else {
132
			//if promiscuous mode is unchecked
133
			$disablepromiscuous = "-p";
134
		}
135

    
136
		if ($_POST['dnsquery']) {
137
			//if dns lookup is checked
138
			$disabledns = "";
139
		} else {
140
			//if dns lookup is unchecked
141
			$disabledns = "-n";
142
		}
143

    
144
		if ($_POST['startbtn'] != "" ) {
145
			$action = gettext("Start");
146

    
147
			//delete previous packet capture if it exists
148
			if (file_exists($fp.$fn))
149
				unlink ($fp.$fn);
150

    
151
		} elseif ($_POST['stopbtn']!= "") {
152
			$action = gettext("Stop");
153
			$processes_running = trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'"));
154

    
155
			//explode processes into an array, (delimiter is new line)
156
			$processes_running_array = explode("\n", $processes_running);
157

    
158
			//kill each of the packetcapture processes
159
			foreach ($processes_running_array as $process) {
160
				$process_id_pos = strpos($process, ' ');
161
				$process_id = substr($process, 0, $process_id_pos);
162
				exec("kill $process_id");
163
			}
164

    
165
		} elseif ($_POST['downloadbtn']!= "") {
166
			//download file
167
			$fs = filesize($fp.$fn);
168
			header("Content-Type: application/octet-stream");
169
			header("Content-Disposition: attachment; filename=$fn");
170
			header("Content-Length: $fs");
171
			readfile($fp.$fn);
172
			exit;
173
		}
174
	}
175
} else {
176
	$do_tcpdump = false;
177
}
178

    
179
include("head.inc"); ?>
180

    
181
<body link="#000000" vlink="#0000CC" alink="#0000CC">
182

    
183
<?php
184
include("fbegin.inc");
185
?>
186

    
187
<?php if ($input_errors) print_input_errors($input_errors); ?>
188

    
189
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="diag packet capture">
190
	<tr><td>
191
	<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
192
	<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="table">
193
		<tr>
194
			<td colspan="3" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
195
		</tr>
196
		<tr>
197
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
198
			<td width="32%" class="vtable">
199
			<select name="interface">
200
			<?php
201
			?>
202
			<?php foreach ($interfaces as $iface => $ifacename): ?>
203
				<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected=\"selected\""; ?>>
204
				<?php echo $ifacename;?>
205
				</option>
206
			<?php endforeach; ?>
207
			</select>
208
			<br /><?=gettext("Select the interface on which to capture traffic.");?>
209
			</td>
210
		</tr>
211
		<tr>
212
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Promiscuous");?></td>
213
			<td width="51%" class="vtable">
214
			<input name="promiscuous" type="checkbox"<?php if($_POST['promiscuous']) echo " checked=\"checked\""; ?> />
215
			<br /><?=gettext("If checked, the");?> <a target="_blank" href="http://www.freebsd.org/cgi/man.cgi?query=tcpdump&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+8.3-stable&amp;arch=default&amp;format=html"><?= gettext("packet capture")?></a> <?= gettext("will be performed using promiscuous mode.");?>
216
			<br /><b><?=gettext("Note");?>: </b><?=gettext("Some network adapters do not support or work well in promiscuous mode.");?>
217
			</td>
218
		</tr>
219
		<tr>
220
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
221
			<td colspan="2" width="83%" class="vtable">
222
			<select name="fam">
223
				<option value="">Any</option>
224
				<option value="ip" <?php if ($fam == "ip") echo "selected=\"selected\""; ?>>IPv4 Only</option>
225
				<option value="ip6" <?php if ($fam == "ip6") echo "selected=\"selected\""; ?>>IPv6 Only</option>
226
			</select>
227
			<br /><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
228
			</td>
229
		</tr>
230
		<tr>
231
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Protocol");?></td>
232
			<td colspan="2" width="83%" class="vtable">
233
			<select name="proto">
234
				<option value="">Any</option>
235
				<option value="icmp" <?php if ($proto == "icmp") echo "selected=\"selected\""; ?>>ICMP</option>
236
				<option value="!icmp" <?php if ($proto == "!icmp") echo "selected=\"selected\""; ?>>Exclude ICMP</option>
237
				<option value="icmp6" <?php if ($proto == "icmp6") echo "selected=\"selected\""; ?>>ICMPv6</option>
238
				<option value="!icmp6" <?php if ($proto == "!icmp6") echo "selected=\"selected\""; ?>>Exclude ICMPv6</option>
239
				<option value="tcp" <?php if ($proto == "tcp") echo "selected=\"selected\""; ?>>TCP</option>
240
				<option value="!tcp" <?php if ($proto == "!tcp") echo "selected=\"selected\""; ?>>Exclude TCP</option>
241
				<option value="udp" <?php if ($proto == "udp") echo "selected=\"selected\""; ?>>UDP</option>
242
				<option value="!udp" <?php if ($proto == "!udp") echo "selected=\"selected\""; ?>>Exclude UDP</option>
243
				<option value="arp" <?php if ($proto == "arp") echo "selected=\"selected\""; ?>>ARP</option>
244
				<option value="!arp" <?php if ($proto == "!arp") echo "selected=\"selected\""; ?>>Exclude ARP</option>
245
				<option value="carp" <?php if ($proto == "carp") echo "selected=\"selected\""; ?>>CARP (VRRP)</option>
246
				<option value="!carp" <?php if ($proto == "!carp") echo "selected=\"selected\""; ?>>Exclude CARP (VRRP)</option>
247
				<option value="esp" <?php if ($proto == "esp") echo "selected=\"selected\""; ?>>ESP</option>
248
			</select>
249
			<br /><?=gettext("Select the protocol to capture, or Any.");?>
250
			</td>
251
		</tr>
252
		<tr>
253
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Host Address");?></td>
254
			<td colspan="2" width="83%" class="vtable">
255
			<input name="host" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>" />
256
			<br /><?=gettext("This value is either the Source or Destination IP address or subnet in CIDR notation. The packet capture will look for this address in either field.");?>
257
			<br /><?=gettext("This value can be a domain name or IP address, or subnet in CIDR notation.");?>
258
			<br /><?=gettext("If you leave this field blank, all packets on the specified interface will be captured.");?>
259
			</td>
260
		</tr>
261
		<tr>
262
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Port");?></td>
263
			<td colspan="2" width="83%" class="vtable">
264
			<input name="port" class="formfld unknown" id="port" size="5" value="<?=$port;?>" />
265
			<br /><?=gettext("The port can be either the source or destination port. The packet capture will look for this port in either field.");?>
266
			<br /><?=gettext("Leave blank if you do not want to filter by port.");?>
267
			</td>
268
		</tr>
269
		<tr>
270
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Packet Length");?></td>
271
			<td colspan="2" width="83%" class="vtable">
272
			<input name="snaplen" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>" />
273
			<br /><?=gettext("The Packet length is the number of bytes of each packet that will be captured. Default value is 0, which will capture the entire frame regardless of its size.");?>
274
			</td>
275
		</tr>
276
		<tr>
277
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
278
			<td colspan="2" width="83%" class="vtable">
279
			<input name="count" class="formfld unknown" id="count" size="5" value="<?=$count;?>" />
280
			<br /><?=gettext("This is the number of packets the packet capture will grab. Default value is 100.") . "<br />" . gettext("Enter 0 (zero) for no count limit.");?>
281
			</td>
282
		</tr>
283
		<tr>
284
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
285
			<td colspan="2" width="83%" class="vtable">
286
			<select name="detail" class="formselect" id="detail" size="1">
287
				<option value="normal" <?php if ($detail == "normal") echo "selected=\"selected\""; ?>><?=gettext("Normal");?></option>
288
				<option value="medium" <?php if ($detail == "medium") echo "selected=\"selected\""; ?>><?=gettext("Medium");?></option>
289
				<option value="high"   <?php if ($detail == "high")   echo "selected=\"selected\""; ?>><?=gettext("High");?></option>
290
				<option value="full"   <?php if ($detail == "full")   echo "selected=\"selected\""; ?>><?=gettext("Full");?></option>
291
			</select>
292
			<br /><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br /><b>" .
293
					gettext("Note:") . "</b> " .
294
					gettext("This option does not affect the level of detail when downloading the packet capture.");?>
295
			</td>
296
		</tr>
297
		<tr>
298
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
299
			<td colspan="2" width="83%" class="vtable">
300
			<input name="dnsquery" type="checkbox" <?php if($_POST['dnsquery']) echo " checked=\"checked\""; ?> />
301
			<br /><?=gettext("This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.");?>
302
			<br /><b><?=gettext("Note");?>: </b><?=gettext("This option can cause delays for large packet captures.");?>
303
			</td>
304
		</tr>
305
		<tr>
306
			<td width="17%" valign="top">&nbsp;</td>
307
			<td colspan="2" width="83%">
308
<?php
309

    
310
			/* check to see if packet capture tcpdump is already running */
311
			$processcheck = (trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'")));
312

    
313
			if ($processcheck != "")
314
				$processisrunning = true;
315
			else
316
				$processisrunning = false;
317

    
318
			if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
319
				echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\" />&nbsp;";
320
			else {
321
				echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\" />&nbsp;";
322
			}
323
			if (file_exists($fp.$fn) and $processisrunning != true) {
324
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\" />&nbsp;";
325
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\" />";
326
				echo "<br />" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
327
			}
328
?>
329
			</td>
330
		</tr>
331
	</table>
332
	</form>
333
	<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="results">
334
		<tr>
335
		<td valign="top" colspan="2">
336
<?php
337
		echo "<font face=\"terminal\" size=\"2\">";
338
		if ($processisrunning == true)
339
			echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
340

    
341
		if ($do_tcpdump) {
342
			$matches = array();
343

    
344
			if (in_array($fam, $fams))
345
				$matches[] = $fam;
346

    
347
			if (in_array($proto, $protos)) {
348
				$matches[] = fixup_not($proto);
349
			}
350

    
351
			if ($port != "")
352
				$matches[] = "port ".fixup_not($port);
353

    
354
			if ($host != "") {
355
				if (is_ipaddr($host))
356
					$matches[] = "host " . fixup_not($host);
357
				elseif (is_subnet($host))
358
					$matches[] = "net " . fixup_not($host);
359
			}
360

    
361
			if ($count != "0" ) {
362
				$searchcount = "-c " . $count;
363
			} else {
364
				$searchcount = "";
365
			}
366

    
367
			$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
368

    
369
			if ($action == gettext("Start")) {
370
				$matchstr = implode($matches, " and ");
371
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
372
				mwexec_bg ("/usr/sbin/tcpdump -i {$selectedif} {$disablepromiscuous} {$searchcount} -s {$snaplen} -w {$fp}{$fn} {$matchstr}");
373
			} else {
374
				//action = stop
375
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br /><br />" . gettext("Packets Captured:") . "</strong><br />");
376
?>
377
				<script type="text/javascript">
378
				//<![CDATA[
379
				window.onload=function(){
380
					document.getElementById("packetsCaptured").wrap='off';
381
				}
382
				//]]>
383
				</script>
384
				<textarea id="packetsCaptured" style="width:98%" name="code" rows="15" cols="66" readonly="readonly">
385
<?php
386
				$detail_args = "";
387
				switch ($detail) {
388
				case "full":
389
					$detail_args = "-vv -e";
390
					break;
391
				case "high":
392
					$detail_args = "-vv";
393
					break;
394
				case "medium":
395
					$detail_args = "-v";
396
					break;
397
				case "normal":
398
				default:
399
					$detail_args = "-q";
400
					break;
401
				}
402
				system("/usr/sbin/tcpdump {$disabledns} {$detail_args} -r {$fp}{$fn}");
403

    
404
				conf_mount_ro();
405
?>
406
				</textarea>
407
<?php
408
			}
409
		}
410
?>
411
		&nbsp;</font>
412
		</td>
413
		</tr>
414
	</table>
415
	</td></tr>
416
</table>
417

    
418
<?php
419
include("fend.inc");
420
?>
421
</body>
422
</html>
(37-37/254)