Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Tag: | Revision:
1 ab6a5cfc Scott Ullrich
<?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 f35abee2 jim-p
/*
27 13d193c2 Scott Ullrich
	pfSense_BUILDER_BINARIES:	/bin/ps	/usr/bin/grep	/usr/sbin/tcpdump
28
	pfSense_MODULE:	routing
29
*/
30
31 6b07c15a Matthew Grooms
##|+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 7c9a30c8 jim-p
$allowautocomplete = true;
39
40 f257c139 jim-p
if ($_POST['downloadbtn'] == gettext("Download Capture"))
41
	$nocsrf = true;
42
43 1c550bed Renato Botelho
$pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
44 d7cd7129 Scott Ullrich
require_once("guiconfig.inc");
45
require_once("pfsense-utils.inc");
46 ab6a5cfc Scott Ullrich
47 4e7d1665 Scott Ullrich
$fp = "/root/";
48 ab6a5cfc Scott Ullrich
$fn = "packetcapture.cap";
49 68ed7d9d Chris Buechler
$snaplen = 0;//default packet length
50 ab6a5cfc Scott Ullrich
$count = 100;//default number of packets to capture
51
52 fd8774c0 jim-p
$fams = array('ip', 'ip6');
53 43d735de jim-p
$protos = array('icmp', 'icmp6', 'tcp', 'udp', 'arp', 'carp', 'esp');
54 fd8774c0 jim-p
55 391cd070 Darren Embry
$input_errors = array();
56
57 f7b6c87a Darren Embry
$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 ab6a5cfc Scott Ullrich
if ($_POST) {
71
	$host = $_POST['host'];
72
	$selectedif = $_POST['interface'];
73
	$count = $_POST['count'];
74 391cd070 Darren Embry
	$snaplen = $_POST['snaplen'];
75 ab6a5cfc Scott Ullrich
	$port = $_POST['port'];
76
	$detail = $_POST['detail'];
77 f35abee2 jim-p
	$fam = $_POST['fam'];
78 fd8774c0 jim-p
	$proto = $_POST['proto'];
79 f35abee2 jim-p
80 20e7e444 jim-p
	if (!array_key_exists($selectedif, $interfaces)) {
81 622caf8f Darren Embry
		$input_errors[] = gettext("Invalid interface.");
82
	}
83
	if ($fam !== "" && $fam !== "ip" && $fam !== "ip6") {
84
		$input_errors[] = gettext("Invalid address family.");
85
	}
86 e88ea344 Erik Fonnesbeck
	if ($proto !== "" && !in_array($proto, $protos)) {
87 622caf8f Darren Embry
		$input_errors[] = gettext("Invalid protocol.");
88
	}
89
	
90 391cd070 Darren Embry
	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 b39ca83c Scott Ullrich
	} else {
103 391cd070 Darren Embry
		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 ab6a5cfc Scott Ullrich
	}
114
115 391cd070 Darren Embry
	if (!count($input_errors)) {
116
		$do_tcpdump = true;
117 f35abee2 jim-p
118 391cd070 Darren Embry
		conf_mount_rw();
119 ab6a5cfc Scott Ullrich
120 8d64d6b5 N0YB
		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 391cd070 Darren Embry
		if ($_POST['dnsquery']) {
129
			//if dns lookup is checked
130
			$disabledns = "";
131
		} else {
132
			//if dns lookup is unchecked
133
			$disabledns = "-n";
134
		}
135 d5c2fde5 Scott Ullrich
136 391cd070 Darren Embry
		if ($_POST['startbtn'] != "" ) {
137
			$action = gettext("Start");
138 d5c2fde5 Scott Ullrich
139 391cd070 Darren Embry
			//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 d5c2fde5 Scott Ullrich
157 391cd070 Darren Embry
		} 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 ab6a5cfc Scott Ullrich
	}
167 b39ca83c Scott Ullrich
} else {
168 ab6a5cfc Scott Ullrich
	$do_tcpdump = false;
169
}
170 f7b6c87a Darren Embry
171 ab6a5cfc Scott Ullrich
include("head.inc"); ?>
172 b39ca83c Scott Ullrich
173 ab6a5cfc Scott Ullrich
<body link="#000000" vlink="#0000CC" alink="#0000CC">
174 b39ca83c Scott Ullrich
175
<?php
176 f35abee2 jim-p
include("fbegin.inc");
177 b39ca83c Scott Ullrich
?>
178 ab6a5cfc Scott Ullrich
179 391cd070 Darren Embry
<?php if ($input_errors) print_input_errors($input_errors); ?>
180
181 ab6a5cfc Scott Ullrich
<table width="100%" border="0" cellpadding="0" cellspacing="0">
182 89fae3e7 jim-p
	<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">
185 f35abee2 jim-p
		<tr>
186 8d64d6b5 N0YB
			<td colspan="3" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
187 89fae3e7 jim-p
		</tr>
188
		<tr>
189
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
190 8d64d6b5 N0YB
			<td width="32%" class="vtable">
191 89fae3e7 jim-p
			<select name="interface">
192 391cd070 Darren Embry
			<?php
193
			?>
194
			<?php foreach ($interfaces as $iface => $ifacename): ?>
195
				<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
196
				<?php echo $ifacename;?>
197
				</option>
198
			<?php endforeach; ?>
199 89fae3e7 jim-p
			</select>
200 8cd558b6 ayvis
			<br /><?=gettext("Select the interface on which to capture traffic.");?>
201 89fae3e7 jim-p
			</td>
202 b2f73235 jim-p
		</tr>
203
		<tr>
204
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Promiscuous");?></td>
205 8d64d6b5 N0YB
			<td width="51%" class="vtable">
206
			<input name="promiscuous" type="checkbox"<?php if($_POST['promiscuous']) echo " CHECKED"; ?>>
207 8cd558b6 ayvis
			<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 8d64d6b5 N0YB
			</td>
210 89fae3e7 jim-p
		</tr>
211
		<tr>
212
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
213 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
214 89fae3e7 jim-p
			<select name="fam">
215
				<option value="">Any</option>
216
				<option value="ip" <?php if ($fam == "ip") echo "selected"; ?>>IPv4 Only</option>
217
				<option value="ip6" <?php if ($fam == "ip6") echo "selected"; ?>>IPv6 Only</option>
218
			</select>
219 8cd558b6 ayvis
			<br /><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
220 89fae3e7 jim-p
			</td>
221
		</tr>
222 fd8774c0 jim-p
		<tr>
223
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Protocol");?></td>
224 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
225 fd8774c0 jim-p
			<select name="proto">
226
				<option value="">Any</option>
227
				<option value="icmp" <?php if ($proto == "icmp") echo "selected"; ?>>ICMP</option>
228
				<option value="icmp6" <?php if ($proto == "icmp6") echo "selected"; ?>>ICMPv6</option>
229
				<option value="tcp" <?php if ($proto == "tcp") echo "selected"; ?>>TCP</option>
230
				<option value="udp" <?php if ($proto == "udp") echo "selected"; ?>>UDP</option>
231
				<option value="arp" <?php if ($proto == "arp") echo "selected"; ?>>ARP</option>
232 43d735de jim-p
				<option value="carp" <?php if ($proto == "carp") echo "selected"; ?>>CARP (VRRP)</option>
233 fd8774c0 jim-p
				<option value="esp" <?php if ($proto == "esp") echo "selected"; ?>>ESP</option>
234
			</select>
235 8cd558b6 ayvis
			<br /><?=gettext("Select the protocol to capture, or Any.");?>
236 fd8774c0 jim-p
			</td>
237
		</tr>
238 89fae3e7 jim-p
		<tr>
239
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Host Address");?></td>
240 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
241 89fae3e7 jim-p
			<input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
242 8cd558b6 ayvis
			<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 89fae3e7 jim-p
			</td>
246
		</tr>
247
		<tr>
248
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Port");?></td>
249 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
250 89fae3e7 jim-p
			<input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
251 8cd558b6 ayvis
			<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 89fae3e7 jim-p
			</td>
254
		</tr>
255
		<tr>
256
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Packet Length");?></td>
257 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
258 89fae3e7 jim-p
			<input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
259 8cd558b6 ayvis
			<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 89fae3e7 jim-p
			</td>
261
		</tr>
262
		<tr>
263
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
264 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
265 89fae3e7 jim-p
			<input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
266 8cd558b6 ayvis
			<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 8d64d6b5 N0YB
			</td>
268 89fae3e7 jim-p
		</tr>
269
		<tr>
270
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
271 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
272 89fae3e7 jim-p
			<select name="detail" type="text" class="formselect" id="detail" size="1">
273 622caf8f Darren Embry
				<option value="normal" <?php if ($detail == "normal") echo "selected"; ?>><?=gettext("Normal");?></option>
274
				<option value="medium" <?php if ($detail == "medium") echo "selected"; ?>><?=gettext("Medium");?></option>
275
				<option value="high"   <?php if ($detail == "high")   echo "selected"; ?>><?=gettext("High");?></option>
276
				<option value="full"   <?php if ($detail == "full")   echo "selected"; ?>><?=gettext("Full");?></option>
277 89fae3e7 jim-p
			</select>
278 8cd558b6 ayvis
			<br /><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br /><b>" .
279 89fae3e7 jim-p
					gettext("Note:") . "</b> " .
280
					gettext("This option does not affect the level of detail when downloading the packet capture.");?>
281 8d64d6b5 N0YB
			</td>
282 89fae3e7 jim-p
		</tr>
283
		<tr>
284
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
285 8d64d6b5 N0YB
			<td colspan="2" width="83%" class="vtable">
286 89fae3e7 jim-p
			<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
287 8cd558b6 ayvis
			<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 89fae3e7 jim-p
			</td>
290
		</tr>
291
		<tr>
292
			<td width="17%" valign="top">&nbsp;</td>
293 8d64d6b5 N0YB
			<td colspan="2" width="83%">
294 b39ca83c Scott Ullrich
<?php
295 d5c2fde5 Scott Ullrich
296 89fae3e7 jim-p
			/* 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 b397879f jim-p
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\">&nbsp;";
311 89fae3e7 jim-p
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
312 8cd558b6 ayvis
				echo "<br />" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
313 89fae3e7 jim-p
			}
314 b39ca83c Scott Ullrich
?>
315 89fae3e7 jim-p
			</td>
316
		</tr>
317 77877238 jim-p
	</table>
318
	</form>
319
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
320 89fae3e7 jim-p
		<tr>
321
		<td valign="top" colspan="2">
322 b39ca83c Scott Ullrich
<?php
323 89fae3e7 jim-p
		echo "<font face='terminal' size='2'>";
324
		if ($processisrunning == true)
325 8cd558b6 ayvis
			echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
326 89fae3e7 jim-p
327
		if ($do_tcpdump) {
328
			$matches = array();
329
330 fd8774c0 jim-p
			if (in_array($fam, $fams))
331 89fae3e7 jim-p
				$matches[] = $fam;
332
333 43d735de jim-p
			if (in_array($proto, $protos)) {
334
				if ($proto == "carp") {
335
					$matches[] = 'proto 112';
336
				} else {
337
					$matches[] = $proto;
338
				}
339
			}
340 fd8774c0 jim-p
341 89fae3e7 jim-p
			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 8cd558b6 ayvis
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br />");
362 8d64d6b5 N0YB
				mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $disablepromiscuous $searchcount -s $snaplen -w $fp$fn $matchstr");
363 89fae3e7 jim-p
			} else {
364
				//action = stop
365 8cd558b6 ayvis
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br /><br />" . gettext("Packets Captured:") . "</strong><br />");
366 b39ca83c Scott Ullrich
?>
367 89fae3e7 jim-p
				<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
368 b39ca83c Scott Ullrich
<?php
369 622caf8f Darren Embry
				$detail_args = "";
370
				switch ($detail) {
371
				case "full":
372
					$detail_args = "-vv -e";
373
					break;
374
				case "high":
375
					$detail_args = "-vv";
376
					break;
377
				case "medium":
378
					$detail_args = "-v";
379
					break;
380
				case "normal":
381
				default:
382
					$detail_args = "-q";
383
					break;
384
				}
385
				system("/usr/sbin/tcpdump $disabledns $detail_args -r $fp$fn");
386 b39ca83c Scott Ullrich
387 89fae3e7 jim-p
				conf_mount_ro();
388 b39ca83c Scott Ullrich
?>
389 89fae3e7 jim-p
				</textarea>
390 b39ca83c Scott Ullrich
<?php
391 89fae3e7 jim-p
			}
392
		}
393 b39ca83c Scott Ullrich
?>
394 89fae3e7 jim-p
		</td>
395
		</tr>
396
	</table>
397
	</td></tr>
398 b39ca83c Scott Ullrich
</table>
399 4e7d1665 Scott Ullrich
400 f35abee2 jim-p
<?php
401
include("fend.inc");
402 4e7d1665 Scott Ullrich
?>