Projet

Général

Profil

Télécharger (14,9 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / diag_packet_capture.php @ e19669c5

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
if ($_POST['downloadbtn'] == gettext("Download Capture"))
41
	$nocsrf = true;
42

    
43
$pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
44
require_once("guiconfig.inc");
45
require_once("pfsense-utils.inc");
46

    
47
$fp = "/root/";
48
$fn = "packetcapture.cap";
49
$snaplen = 0;//default packet length
50
$count = 100;//default number of packets to capture
51

    
52
$fams = array('ip', 'ip6');
53
$protos = array('icmp', 'icmp6', 'tcp', 'udp', 'arp', 'carp', 'esp');
54

    
55
$input_errors = array();
56

    
57
$interfaces = get_configured_interface_with_descr();
58
if (isset($config['ipsec']['enable']))
59
	$interfaces['ipsec'] = "IPsec";
60
foreach (array('server', 'client') as $mode) {
61
	if (is_array($config['openvpn']["openvpn-{$mode}"])) {
62
		foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
63
			if (!isset($setting['disable'])) {
64
				$interfaces['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
65
			}
66
		}
67
	}
68
}
69

    
70
if ($_POST) {
71
	$host = $_POST['host'];
72
	$selectedif = $_POST['interface'];
73
	$count = $_POST['count'];
74
	$snaplen = $_POST['snaplen'];
75
	$port = $_POST['port'];
76
	$detail = $_POST['detail'];
77
	$fam = $_POST['fam'];
78
	$proto = $_POST['proto'];
79

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

    
115
	if (!count($input_errors)) {
116
		$do_tcpdump = true;
117

    
118
		conf_mount_rw();
119

    
120
		if ($_POST['promiscuous']) {
121
			//if promiscuous mode is checked
122
			$disablepromiscuous = "";
123
		} else {
124
			//if promiscuous mode is unchecked
125
			$disablepromiscuous = "-p";
126
		}
127

    
128
		if ($_POST['dnsquery']) {
129
			//if dns lookup is checked
130
			$disabledns = "";
131
		} else {
132
			//if dns lookup is unchecked
133
			$disabledns = "-n";
134
		}
135

    
136
		if ($_POST['startbtn'] != "" ) {
137
			$action = gettext("Start");
138

    
139
			//delete previous packet capture if it exists
140
			if (file_exists($fp.$fn))
141
				unlink ($fp.$fn);
142

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

    
147
			//explode processes into an array, (delimiter is new line)
148
			$processes_running_array = explode("\n", $processes_running);
149

    
150
			//kill each of the packetcapture processes
151
			foreach ($processes_running_array as $process) {
152
				$process_id_pos = strpos($process, ' ');
153
				$process_id = substr($process, 0, $process_id_pos);
154
				exec("kill $process_id");
155
			}
156

    
157
		} elseif ($_POST['downloadbtn']!= "") {
158
			//download file
159
			$fs = filesize($fp.$fn);
160
			header("Content-Type: application/octet-stream");
161
			header("Content-Disposition: attachment; filename=$fn");
162
			header("Content-Length: $fs");
163
			readfile($fp.$fn);
164
			exit;
165
		}
166
	}
167
} else {
168
	$do_tcpdump = false;
169
}
170

    
171
include("head.inc"); ?>
172

    
173
<body link="#000000" vlink="#0000CC" alink="#0000CC">
174

    
175
<?php
176
include("fbegin.inc");
177
?>
178

    
179
<?php if ($input_errors) print_input_errors($input_errors); ?>
180

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

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

    
299
			if ($processcheck != "")
300
				$processisrunning = true;
301
			else
302
				$processisrunning = false;
303

    
304
			if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
305
				echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\" />&nbsp;";
306
			else {
307
				echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\" />&nbsp;";
308
			}
309
			if (file_exists($fp.$fn) and $processisrunning != true) {
310
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\" />&nbsp;";
311
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\" />";
312
				echo "<br />" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
313
			}
314
?>
315
			</td>
316
		</tr>
317
	</table>
318
	</form>
319
	<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="results">
320
		<tr>
321
		<td valign="top" colspan="2">
322
<?php
323
		echo "<font face=\"terminal\" size=\"2\">";
324
		if ($processisrunning == true)
325
			echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
326

    
327
		if ($do_tcpdump) {
328
			$matches = array();
329

    
330
			if (in_array($fam, $fams))
331
				$matches[] = $fam;
332

    
333
			if (in_array($proto, $protos)) {
334
				if ($proto == "carp") {
335
					$matches[] = 'proto 112';
336
				} else {
337
					$matches[] = $proto;
338
				}
339
			}
340

    
341
			if ($port != "")
342
				$matches[] = "port ".$port;
343

    
344
			if ($host != "") {
345
				if (is_ipaddr($host))
346
					$matches[] = "host " . $host;
347
				elseif (is_subnet($host))
348
					$matches[] = "net " . $host;
349
			}
350

    
351
			if ($count != "0" ) {
352
				$searchcount = "-c " . $count;
353
			} else {
354
				$searchcount = "";
355
			}
356

    
357
			$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
358

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

    
394
				conf_mount_ro();
395
?>
396
				</textarea>
397
<?php
398
			}
399
		}
400
?>
401
		&nbsp;</font>
402
		</td>
403
		</tr>
404
	</table>
405
	</td></tr>
406
</table>
407

    
408
<?php
409
include("fend.inc");
410
?>
411
</body>
412
</html>
(37-37/254)