Project

General

Profile

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

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

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

    
50
$fams = array('ip', 'ip6');
51
$protos = array('icmp', 'icmp6', 'tcp', 'udp', 'arp', 'vrrp', 'esp');
52

    
53
if ($_POST) {
54
	$do_tcpdump = true;
55
	$host = $_POST['host'];
56
	$selectedif = $_POST['interface'];
57
	$count = $_POST['count'];
58
	$packetlength = $_POST['snaplen'];
59
	$port = $_POST['port'];
60
	$detail = $_POST['detail'];
61
	$fam = $_POST['fam'];
62
	$proto = $_POST['proto'];
63

    
64
	conf_mount_rw();
65

    
66
	if ($_POST['dnsquery']) {
67
		//if dns lookup is checked
68
		$disabledns = "";
69
	} else {
70
		//if dns lookup is unchecked
71
		$disabledns = "-n";
72
	}
73

    
74
	if ($_POST['startbtn'] != "" ) {
75
		$action = gettext("Start");
76

    
77
		//delete previous packet capture if it exists
78
		if (file_exists($fp.$fn))
79
			unlink ($fp.$fn);
80

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

    
85
		//explode processes into an array, (delimiter is new line)
86
		$processes_running_array = explode("\n", $processes_running);
87

    
88
		//kill each of the packetcapture processes
89
		foreach ($processes_running_array as $process) {
90
			$process_id_pos = strpos($process, ' ');
91
			$process_id = substr($process, 0, $process_id_pos);
92
			exec("kill $process_id");
93
		}
94

    
95
	} elseif ($_POST['downloadbtn']!= "") {
96
		//download file
97
		$fs = filesize($fp.$fn);
98
		header("Content-Type: application/octet-stream");
99
		header("Content-Disposition: attachment; filename=$fn");
100
		header("Content-Length: $fs");
101
		readfile($fp.$fn);
102
		exit;
103
	}
104
} else {
105
	$do_tcpdump = false;
106
}
107
include("head.inc"); ?>
108

    
109
<body link="#000000" vlink="#0000CC" alink="#0000CC">
110

    
111
<?php
112
include("fbegin.inc");
113
?>
114

    
115
<table width="100%" border="0" cellpadding="0" cellspacing="0">
116
	<tr><td>
117
	<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
118
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
119
		<tr>
120
			<td colspan="2" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
121
		</tr>
122
		<tr>
123
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
124
			<td width="83%" class="vtable">
125
			<select name="interface">
126
<?php
127
			$interfaces = get_configured_interface_with_descr();
128
			if (isset($config['ipsec']['enable']))
129
				$interfaces['ipsec'] = "IPsec";
130
			foreach (array('server', 'client') as $mode) {
131
				if (is_array($config['openvpn']["openvpn-{$mode}"])) {
132
					foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
133
						if (!isset($setting['disable'])) {
134
							$interfaces['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
135
						}
136
					}
137
				}
138
			}
139
			foreach ($interfaces as $iface => $ifacename): ?>
140
			<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
141
			<?php echo $ifacename;?>
142
			</option>
143
			<?php endforeach;?>
144
			</select>
145
			<br/><?=gettext("Select the interface on which to capture traffic.");?>
146
			</td>
147
		</tr>
148
		<tr>
149
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
150
			<td width="83%" class="vtable">
151
			<select name="fam">
152
				<option value="">Any</option>
153
				<option value="ip" <?php if ($fam == "ip") echo "selected"; ?>>IPv4 Only</option>
154
				<option value="ip6" <?php if ($fam == "ip6") echo "selected"; ?>>IPv6 Only</option>
155
			</select>
156
			<br/><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
157
			</td>
158
		</tr>
159
		<tr>
160
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Protocol");?></td>
161
			<td width="83%" class="vtable">
162
			<select name="proto">
163
				<option value="">Any</option>
164
				<option value="icmp" <?php if ($proto == "icmp") echo "selected"; ?>>ICMP</option>
165
				<option value="icmp6" <?php if ($proto == "icmp6") echo "selected"; ?>>ICMPv6</option>
166
				<option value="tcp" <?php if ($proto == "tcp") echo "selected"; ?>>TCP</option>
167
				<option value="udp" <?php if ($proto == "udp") echo "selected"; ?>>UDP</option>
168
				<option value="arp" <?php if ($proto == "arp") echo "selected"; ?>>ARP</option>
169
				<option value="vrrp" <?php if ($proto == "vrrp") echo "selected"; ?>>VRRP (CARP)</option>
170
				<option value="esp" <?php if ($proto == "esp") echo "selected"; ?>>ESP</option>
171
			</select>
172
			<br/><?=gettext("Select the protocol to capture, or Any.");?>
173
			</td>
174
		</tr>
175
		<tr>
176
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Host Address");?></td>
177
			<td width="83%" class="vtable">
178
			<input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
179
			<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.");?>
180
			<br/><?=gettext("This value can be a domain name or IP address, or subnet in CIDR notation.");?>
181
			<br/><?=gettext("If you leave this field blank, all packets on the specified interface will be captured.");?>
182
			</td>
183
		</tr>
184
		<tr>
185
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Port");?></td>
186
			<td width="83%" class="vtable">
187
			<input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
188
			<br/><?=gettext("The port can be either the source or destination port. The packet capture will look for this port in either field.");?>
189
			<br/><?=gettext("Leave blank if you do not want to filter by port.");?>
190
			</td>
191
		</tr>
192
		<tr>
193
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Packet Length");?></td>
194
			<td width="83%" class="vtable">
195
			<input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
196
			<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.");?>
197
			</td>
198
		</tr>
199
		<tr>
200
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
201
			<td width="83%" class="vtable">
202
			<input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
203
			<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.");?>
204
		</tr>
205
		<tr>
206
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
207
			<td width="83%" class="vtable">
208
			<select name="detail" type="text" class="formselect" id="detail" size="1">
209
				<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>><?=gettext("Normal");?></option>
210
				<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>><?=gettext("Medium");?></option>
211
				<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>><?=gettext("High");?></option>
212
				<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>><?=gettext("Full");?></option>
213
			</select>
214
			<br/><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br/><b>" .
215
					gettext("Note:") . "</b> " .
216
					gettext("This option does not affect the level of detail when downloading the packet capture.");?>
217
		</tr>
218
		<tr>
219
			<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
220
			<td width="83%" class="vtable">
221
			<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
222
			<br/><?=gettext("This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.");?>
223
			<br/><b><?=gettext("Note");?>: </b><?=gettext("This option can cause delays for large packet captures.");?>
224
			</td>
225
		</tr>
226
		<tr>
227
			<td width="17%" valign="top">&nbsp;</td>
228
			<td width="83%">
229
<?php
230

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

    
234
			if ($processcheck != "")
235
				$processisrunning = true;
236
			else
237
				$processisrunning = false;
238

    
239
			if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
240
				echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\">&nbsp;";
241
			else {
242
				echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\">&nbsp;";
243
			}
244
			if (file_exists($fp.$fn) and $processisrunning != true) {
245
				echo "<input type=\"submit\" name=\"viewbtn\" value=\"" . gettext("View Capture") . "\">&nbsp;";
246
				echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
247
				echo "<br/>" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn));
248
			}
249
?>
250
			</td>
251
		</tr>
252
	</table>
253
	</form>
254
	<table width="100%" border="0" cellpadding="6" cellspacing="0">
255
		<tr>
256
		<td valign="top" colspan="2">
257
<?php
258
		echo "<font face='terminal' size='2'>";
259
		if ($processisrunning == true)
260
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
261

    
262
		if ($do_tcpdump) {
263
			$matches = array();
264

    
265
			if (in_array($fam, $fams))
266
				$matches[] = $fam;
267

    
268
			if (in_array($proto, $protos))
269
				$matches[] = $proto;
270

    
271
			if ($port != "")
272
				$matches[] = "port ".$port;
273

    
274
			if ($host != "") {
275
				if (is_ipaddr($host))
276
					$matches[] = "host " . $host;
277
				elseif (is_subnet($host))
278
					$matches[] = "net " . $host;
279
			}
280

    
281
			if ($count != "0" ) {
282
				$searchcount = "-c " . $count;
283
			} else {
284
				$searchcount = "";
285
			}
286

    
287
			$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
288

    
289
			if ($action == gettext("Start")) {
290
				$matchstr = implode($matches, " and ");
291
				echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
292
				mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr");
293
				// echo "/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr";
294
			} else {
295
				//action = stop
296
				echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
297
?>
298
				<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
299
<?php
300
				system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");
301

    
302
				conf_mount_ro();
303
?>
304
				</textarea>
305
<?php
306
			}
307
		}
308
?>
309
		</td>
310
		</tr>
311
	</table>
312
	</td></tr>
313
</table>
314

    
315
<?php
316
include("fend.inc");
317
?>
(36-36/242)