Project

General

Profile

Download (11.1 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
if ($_POST) {
51
	$do_tcpdump = true;
52
	$host = $_POST['host'];
53
	$selectedif = $_POST['interface'];
54
	$count = $_POST['count'];
55
	$packetlength = $_POST['snaplen'];
56
	$port = $_POST['port'];
57
	$detail = $_POST['detail'];
58
	$fam = $_POST['fam'];
59

    
60
	conf_mount_rw();
61

    
62
	if ($_POST['dnsquery']) {
63
		//if dns lookup is checked
64
		$disabledns = "";
65
	} else {
66
		//if dns lookup is unchecked
67
		$disabledns = "-n";
68
	}
69

    
70
	if ($_POST['startbtn'] != "" ) {
71
		$action = gettext("Start");
72

    
73
		//delete previous packet capture if it exists
74
		if (file_exists($fp.$fn))
75
			unlink ($fp.$fn);
76

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

    
81
		//explode processes into an array, (delimiter is new line)
82
		$processes_running_array = explode("\n", $processes_running);
83

    
84
		//kill each of the packetcapture processes
85
		foreach ($processes_running_array as $process) {
86
			$process_id_pos = strpos($process, ' ');
87
			$process_id = substr($process, 0, $process_id_pos);
88
			exec("kill $process_id");
89
		}
90

    
91
	} else {
92
		//download file
93
		$fs = filesize($fp.$fn);
94
		header("Content-Type: application/octet-stream");
95
		header("Content-Disposition: attachment; filename=$fn");
96
		header("Content-Length: $fs");
97
		readfile($fp.$fn);
98
		exit;
99
	}
100
} else {
101
	$do_tcpdump = false;
102
}
103
include("head.inc"); ?>
104

    
105
<body link="#000000" vlink="#0000CC" alink="#0000CC">
106

    
107
<?php
108
include("fbegin.inc");
109
?>
110

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

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

    
215
					if ($processcheck != "")
216
						$processisrunning = true;
217
					else
218
						$processisrunning = false;
219

    
220
					if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
221
						echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\">&nbsp;";
222
					else {
223
						echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\">&nbsp;";
224
					}
225
					if (file_exists($fp.$fn) and $processisrunning != true) {
226
						echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
227
						echo "&nbsp;&nbsp;(" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
228
					}
229
?>
230
					</td>
231
				</tr>
232
				<tr>
233
				<td valign="top" colspan="2">
234
<?php
235
				echo "<font face='terminal' size='2'>";
236
				if ($processisrunning == true)
237
						echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
238

    
239
				if ($do_tcpdump) {
240
					$matches = array();
241

    
242
					if (($fam == "ip6") || ($fam == "ip"))
243
						$matches[] = $fam;
244

    
245
					if ($port != "")
246
						$matches[] = "port ".$port;
247

    
248
					if ($host != "") {
249
						if (is_ipaddr($host))
250
							$matches[] = "host " . $host;
251
						elseif (is_subnet($host))
252
							$matches[] = "net " . $host;
253
					}
254

    
255
					if ($count != "0" ) {
256
						$searchcount = "-c " . $count;
257
					} else {
258
						$searchcount = "";
259
					}
260

    
261
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
262

    
263
					if ($action == gettext("Start")) {
264
						$matchstr = implode($matches, " and ");
265
						echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
266
						mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr");
267
						// echo "/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr";
268
					} else {
269
						//action = stop
270
						echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
271
?>
272
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
273
<?php
274
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");
275

    
276
						conf_mount_ro();
277
?>
278
						</textarea>
279
<?php
280
					}
281
				}
282
?>
283
				</td>
284
				</tr>
285
				<tr>
286

    
287
		</table>
288
</form>
289
</td>
290
</tr>
291
</table>
292

    
293
<?php
294
include("fend.inc");
295
?>
(31-31/226)