Project

General

Profile

Download (13.4 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['dnsquery']) {
121
			//if dns lookup is checked
122
			$disabledns = "";
123
		} else {
124
			//if dns lookup is unchecked
125
			$disabledns = "-n";
126
		}
127

    
128
		if ($_POST['startbtn'] != "" ) {
129
			$action = gettext("Start");
130

    
131
			//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

    
149
		} 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
	}
159
} else {
160
	$do_tcpdump = false;
161
}
162

    
163
include("head.inc"); ?>
164

    
165
<body link="#000000" vlink="#0000CC" alink="#0000CC">
166

    
167
<?php
168
include("fbegin.inc");
169
?>
170

    
171
<?php if ($input_errors) print_input_errors($input_errors); ?>
172

    
173
<table width="100%" border="0" cellpadding="0" cellspacing="0">
174
	<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
		<tr>
178
			<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
			<?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
			</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
		<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
				<option value="carp" <?php if ($proto == "carp") echo "selected"; ?>>CARP (VRRP)</option>
217
				<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
		<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
				<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
			</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
<?php
277

    
278
			/* 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
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\">&nbsp;";
293
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
294
				echo "<br/>" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
295
			}
296
?>
297
			</td>
298
		</tr>
299
	</table>
300
	</form>
301
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
302
		<tr>
303
		<td valign="top" colspan="2">
304
<?php
305
		echo "<font face='terminal' size='2'>";
306
		if ($processisrunning == true)
307
			echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
308

    
309
		if ($do_tcpdump) {
310
			$matches = array();
311

    
312
			if (in_array($fam, $fams))
313
				$matches[] = $fam;
314

    
315
			if (in_array($proto, $protos)) {
316
				if ($proto == "carp") {
317
					$matches[] = 'proto 112';
318
				} else {
319
					$matches[] = $proto;
320
				}
321
			}
322

    
323
			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
				mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $snaplen -w $fp$fn $matchstr");
345
			} else {
346
				//action = stop
347
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
348
?>
349
				<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
350
<?php
351
				$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

    
369
				conf_mount_ro();
370
?>
371
				</textarea>
372
<?php
373
			}
374
		}
375
?>
376
		</td>
377
		</tr>
378
	</table>
379
	</td></tr>
380
</table>
381

    
382
<?php
383
include("fend.inc");
384
?>
(37-37/249)