Project

General

Profile

Download (13.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 391cd070 Darren Embry
		if ($_POST['dnsquery']) {
121
			//if dns lookup is checked
122
			$disabledns = "";
123
		} else {
124
			//if dns lookup is unchecked
125
			$disabledns = "-n";
126
		}
127 d5c2fde5 Scott Ullrich
128 391cd070 Darren Embry
		if ($_POST['startbtn'] != "" ) {
129
			$action = gettext("Start");
130 d5c2fde5 Scott Ullrich
131 391cd070 Darren Embry
			//delete previous packet capture if it exists
132
			if (file_exists($fp.$fn))
133
				unlink ($fp.$fn);
134
135
		} elseif ($_POST['stopbtn']!= "") {
136
			$action = gettext("Stop");
137
			$processes_running = trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'"));
138
139
			//explode processes into an array, (delimiter is new line)
140
			$processes_running_array = explode("\n", $processes_running);
141
142
			//kill each of the packetcapture processes
143
			foreach ($processes_running_array as $process) {
144
				$process_id_pos = strpos($process, ' ');
145
				$process_id = substr($process, 0, $process_id_pos);
146
				exec("kill $process_id");
147
			}
148 d5c2fde5 Scott Ullrich
149 391cd070 Darren Embry
		} elseif ($_POST['downloadbtn']!= "") {
150
			//download file
151
			$fs = filesize($fp.$fn);
152
			header("Content-Type: application/octet-stream");
153
			header("Content-Disposition: attachment; filename=$fn");
154
			header("Content-Length: $fs");
155
			readfile($fp.$fn);
156
			exit;
157
		}
158 ab6a5cfc Scott Ullrich
	}
159 b39ca83c Scott Ullrich
} else {
160 ab6a5cfc Scott Ullrich
	$do_tcpdump = false;
161
}
162 f7b6c87a Darren Embry
163 ab6a5cfc Scott Ullrich
include("head.inc"); ?>
164 b39ca83c Scott Ullrich
165 ab6a5cfc Scott Ullrich
<body link="#000000" vlink="#0000CC" alink="#0000CC">
166 b39ca83c Scott Ullrich
167
<?php
168 f35abee2 jim-p
include("fbegin.inc");
169 b39ca83c Scott Ullrich
?>
170 ab6a5cfc Scott Ullrich
171 391cd070 Darren Embry
<?php if ($input_errors) print_input_errors($input_errors); ?>
172
173 ab6a5cfc Scott Ullrich
<table width="100%" border="0" cellpadding="0" cellspacing="0">
174 89fae3e7 jim-p
	<tr><td>
175
	<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
176
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
177 f35abee2 jim-p
		<tr>
178 89fae3e7 jim-p
			<td colspan="2" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
179
		</tr>
180
		<tr>
181
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
182
			<td width="83%" class="vtable">
183
			<select name="interface">
184 391cd070 Darren Embry
			<?php
185
			?>
186
			<?php foreach ($interfaces as $iface => $ifacename): ?>
187
				<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
188
				<?php echo $ifacename;?>
189
				</option>
190
			<?php endforeach; ?>
191 89fae3e7 jim-p
			</select>
192
			<br/><?=gettext("Select the interface on which to capture traffic.");?>
193
			</td>
194
		</tr>
195
		<tr>
196
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
197
			<td width="83%" class="vtable">
198
			<select name="fam">
199
				<option value="">Any</option>
200
				<option value="ip" <?php if ($fam == "ip") echo "selected"; ?>>IPv4 Only</option>
201
				<option value="ip6" <?php if ($fam == "ip6") echo "selected"; ?>>IPv6 Only</option>
202
			</select>
203
			<br/><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
204
			</td>
205
		</tr>
206 fd8774c0 jim-p
		<tr>
207
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Protocol");?></td>
208
			<td width="83%" class="vtable">
209
			<select name="proto">
210
				<option value="">Any</option>
211
				<option value="icmp" <?php if ($proto == "icmp") echo "selected"; ?>>ICMP</option>
212
				<option value="icmp6" <?php if ($proto == "icmp6") echo "selected"; ?>>ICMPv6</option>
213
				<option value="tcp" <?php if ($proto == "tcp") echo "selected"; ?>>TCP</option>
214
				<option value="udp" <?php if ($proto == "udp") echo "selected"; ?>>UDP</option>
215
				<option value="arp" <?php if ($proto == "arp") echo "selected"; ?>>ARP</option>
216 43d735de jim-p
				<option value="carp" <?php if ($proto == "carp") echo "selected"; ?>>CARP (VRRP)</option>
217 fd8774c0 jim-p
				<option value="esp" <?php if ($proto == "esp") echo "selected"; ?>>ESP</option>
218
			</select>
219
			<br/><?=gettext("Select the protocol to capture, or Any.");?>
220
			</td>
221
		</tr>
222 89fae3e7 jim-p
		<tr>
223
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Host Address");?></td>
224
			<td width="83%" class="vtable">
225
			<input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
226
			<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.");?>
227
			<br/><?=gettext("This value can be a domain name or IP address, or subnet in CIDR notation.");?>
228
			<br/><?=gettext("If you leave this field blank, all packets on the specified interface will be captured.");?>
229
			</td>
230
		</tr>
231
		<tr>
232
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Port");?></td>
233
			<td width="83%" class="vtable">
234
			<input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
235
			<br/><?=gettext("The port can be either the source or destination port. The packet capture will look for this port in either field.");?>
236
			<br/><?=gettext("Leave blank if you do not want to filter by port.");?>
237
			</td>
238
		</tr>
239
		<tr>
240
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Packet Length");?></td>
241
			<td width="83%" class="vtable">
242
			<input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
243
			<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.");?>
244
			</td>
245
		</tr>
246
		<tr>
247
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
248
			<td width="83%" class="vtable">
249
			<input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
250
			<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.");?>
251
		</tr>
252
		<tr>
253
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
254
			<td width="83%" class="vtable">
255
			<select name="detail" type="text" class="formselect" id="detail" size="1">
256 622caf8f Darren Embry
				<option value="normal" <?php if ($detail == "normal") echo "selected"; ?>><?=gettext("Normal");?></option>
257
				<option value="medium" <?php if ($detail == "medium") echo "selected"; ?>><?=gettext("Medium");?></option>
258
				<option value="high"   <?php if ($detail == "high")   echo "selected"; ?>><?=gettext("High");?></option>
259
				<option value="full"   <?php if ($detail == "full")   echo "selected"; ?>><?=gettext("Full");?></option>
260 89fae3e7 jim-p
			</select>
261
			<br/><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br/><b>" .
262
					gettext("Note:") . "</b> " .
263
					gettext("This option does not affect the level of detail when downloading the packet capture.");?>
264
		</tr>
265
		<tr>
266
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
267
			<td width="83%" class="vtable">
268
			<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
269
			<br/><?=gettext("This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.");?>
270
			<br/><b><?=gettext("Note");?>: </b><?=gettext("This option can cause delays for large packet captures.");?>
271
			</td>
272
		</tr>
273
		<tr>
274
			<td width="17%" valign="top">&nbsp;</td>
275
			<td width="83%">
276 b39ca83c Scott Ullrich
<?php
277 d5c2fde5 Scott Ullrich
278 89fae3e7 jim-p
			/* check to see if packet capture tcpdump is already running */
279
			$processcheck = (trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'")));
280
281
			if ($processcheck != "")
282
				$processisrunning = true;
283
			else
284
				$processisrunning = false;
285
286
			if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
287
				echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\">&nbsp;";
288
			else {
289
				echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\">&nbsp;";
290
			}
291
			if (file_exists($fp.$fn) and $processisrunning != true) {
292 b397879f jim-p
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\">&nbsp;";
293 89fae3e7 jim-p
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
294 b397879f jim-p
				echo "<br/>" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
295 89fae3e7 jim-p
			}
296 b39ca83c Scott Ullrich
?>
297 89fae3e7 jim-p
			</td>
298
		</tr>
299 77877238 jim-p
	</table>
300
	</form>
301
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
302 89fae3e7 jim-p
		<tr>
303
		<td valign="top" colspan="2">
304 b39ca83c Scott Ullrich
<?php
305 89fae3e7 jim-p
		echo "<font face='terminal' size='2'>";
306
		if ($processisrunning == true)
307 622caf8f Darren Embry
			echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
308 89fae3e7 jim-p
309
		if ($do_tcpdump) {
310
			$matches = array();
311
312 fd8774c0 jim-p
			if (in_array($fam, $fams))
313 89fae3e7 jim-p
				$matches[] = $fam;
314
315 43d735de jim-p
			if (in_array($proto, $protos)) {
316
				if ($proto == "carp") {
317
					$matches[] = 'proto 112';
318
				} else {
319
					$matches[] = $proto;
320
				}
321
			}
322 fd8774c0 jim-p
323 89fae3e7 jim-p
			if ($port != "")
324
				$matches[] = "port ".$port;
325
326
			if ($host != "") {
327
				if (is_ipaddr($host))
328
					$matches[] = "host " . $host;
329
				elseif (is_subnet($host))
330
					$matches[] = "net " . $host;
331
			}
332
333
			if ($count != "0" ) {
334
				$searchcount = "-c " . $count;
335
			} else {
336
				$searchcount = "";
337
			}
338
339
			$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
340
341
			if ($action == gettext("Start")) {
342
				$matchstr = implode($matches, " and ");
343
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
344 391cd070 Darren Embry
				mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $snaplen -w $fp$fn $matchstr");
345 89fae3e7 jim-p
			} else {
346
				//action = stop
347
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
348 b39ca83c Scott Ullrich
?>
349 89fae3e7 jim-p
				<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
350 b39ca83c Scott Ullrich
<?php
351 622caf8f Darren Embry
				$detail_args = "";
352
				switch ($detail) {
353
				case "full":
354
					$detail_args = "-vv -e";
355
					break;
356
				case "high":
357
					$detail_args = "-vv";
358
					break;
359
				case "medium":
360
					$detail_args = "-v";
361
					break;
362
				case "normal":
363
				default:
364
					$detail_args = "-q";
365
					break;
366
				}
367
				system("/usr/sbin/tcpdump $disabledns $detail_args -r $fp$fn");
368 b39ca83c Scott Ullrich
369 89fae3e7 jim-p
				conf_mount_ro();
370 b39ca83c Scott Ullrich
?>
371 89fae3e7 jim-p
				</textarea>
372 b39ca83c Scott Ullrich
<?php
373 89fae3e7 jim-p
			}
374
		}
375 b39ca83c Scott Ullrich
?>
376 89fae3e7 jim-p
		</td>
377
		</tr>
378
	</table>
379
	</td></tr>
380 b39ca83c Scott Ullrich
</table>
381 4e7d1665 Scott Ullrich
382 f35abee2 jim-p
<?php
383
include("fend.inc");
384 4e7d1665 Scott Ullrich
?>