Project

General

Profile

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

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

    
296
			if ($processcheck != "")
297
				$processisrunning = true;
298
			else
299
				$processisrunning = false;
300

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

    
324
		if ($do_tcpdump) {
325
			$matches = array();
326

    
327
			if (in_array($fam, $fams))
328
				$matches[] = $fam;
329

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

    
338
			if ($port != "")
339
				$matches[] = "port ".$port;
340

    
341
			if ($host != "") {
342
				if (is_ipaddr($host))
343
					$matches[] = "host " . $host;
344
				elseif (is_subnet($host))
345
					$matches[] = "net " . $host;
346
			}
347

    
348
			if ($count != "0" ) {
349
				$searchcount = "-c " . $count;
350
			} else {
351
				$searchcount = "";
352
			}
353

    
354
			$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
355

    
356
			if ($action == gettext("Start")) {
357
				$matchstr = implode($matches, " and ");
358
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
359
				mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $disablepromiscuous $searchcount -s $snaplen -w $fp$fn $matchstr");
360
			} else {
361
				//action = stop
362
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
363
?>
364
				<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
365
<?php
366
				$detail_args = "";
367
				switch ($detail) {
368
				case "full":
369
					$detail_args = "-vv -e";
370
					break;
371
				case "high":
372
					$detail_args = "-vv";
373
					break;
374
				case "medium":
375
					$detail_args = "-v";
376
					break;
377
				case "normal":
378
				default:
379
					$detail_args = "-q";
380
					break;
381
				}
382
				system("/usr/sbin/tcpdump $disabledns $detail_args -r $fp$fn");
383

    
384
				conf_mount_ro();
385
?>
386
				</textarea>
387
<?php
388
			}
389
		}
390
?>
391
		</td>
392
		</tr>
393
	</table>
394
	</td></tr>
395
</table>
396

    
397
<?php
398
include("fend.inc");
399
?>
(37-37/246)