Project

General

Profile

Download (11 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 1c550bed Renato Botelho
$pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
39 d7cd7129 Scott Ullrich
require_once("guiconfig.inc");
40
require_once("pfsense-utils.inc");
41 ab6a5cfc Scott Ullrich
42 4e7d1665 Scott Ullrich
$fp = "/root/";
43 ab6a5cfc Scott Ullrich
$fn = "packetcapture.cap";
44 68ed7d9d Chris Buechler
$snaplen = 0;//default packet length
45 ab6a5cfc Scott Ullrich
$count = 100;//default number of packets to capture
46
47
if ($_POST) {
48
	$do_tcpdump = true;
49
	$host = $_POST['host'];
50
	$selectedif = $_POST['interface'];
51
	$count = $_POST['count'];
52
	$packetlength = $_POST['snaplen'];
53
	$port = $_POST['port'];
54
	$detail = $_POST['detail'];
55 f35abee2 jim-p
	$fam = $_POST['fam'];
56
57 4e7d1665 Scott Ullrich
	conf_mount_rw();
58 ab6a5cfc Scott Ullrich
59 b39ca83c Scott Ullrich
	if ($_POST['dnsquery']) {
60
		//if dns lookup is checked
61 ab6a5cfc Scott Ullrich
		$disabledns = "";
62 b39ca83c Scott Ullrich
	} else {
63
		//if dns lookup is unchecked
64 ab6a5cfc Scott Ullrich
		$disabledns = "-n";
65
	}
66
67 b39ca83c Scott Ullrich
	if ($_POST['startbtn'] != "" ) {
68 1c550bed Renato Botelho
		$action = gettext("Start");
69 f35abee2 jim-p
70
		//delete previous packet capture if it exists
71
		if (file_exists($fp.$fn))
72
			unlink ($fp.$fn);
73 ab6a5cfc Scott Ullrich
74 b39ca83c Scott Ullrich
	} elseif ($_POST['stopbtn']!= "") {
75 1c550bed Renato Botelho
		$action = gettext("Stop");
76 d67b6b17 jim-p
		$processes_running = trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'"));
77 d5c2fde5 Scott Ullrich
78
		//explode processes into an array, (delimiter is new line)
79
		$processes_running_array = explode("\n", $processes_running);
80
81
		//kill each of the packetcapture processes
82 b39ca83c Scott Ullrich
		foreach ($processes_running_array as $process) {
83 d5c2fde5 Scott Ullrich
			$process_id_pos = strpos($process, ' ');
84
			$process_id = substr($process, 0, $process_id_pos);
85
			exec("kill $process_id");
86
		}
87
88 b39ca83c Scott Ullrich
	} else {
89
		//download file
90 93c86d2b Scott Dale
		$fs = filesize($fp.$fn);
91 ab6a5cfc Scott Ullrich
		header("Content-Type: application/octet-stream");
92 f35abee2 jim-p
		header("Content-Disposition: attachment; filename=$fn");
93 ab6a5cfc Scott Ullrich
		header("Content-Length: $fs");
94
		readfile($fp.$fn);
95
	}
96 b39ca83c Scott Ullrich
} else {
97 ab6a5cfc Scott Ullrich
	$do_tcpdump = false;
98
}
99
include("head.inc"); ?>
100 b39ca83c Scott Ullrich
101 ab6a5cfc Scott Ullrich
<body link="#000000" vlink="#0000CC" alink="#0000CC">
102 b39ca83c Scott Ullrich
103
<?php
104 f35abee2 jim-p
include("fbegin.inc");
105 b39ca83c Scott Ullrich
?>
106 ab6a5cfc Scott Ullrich
107
<table width="100%" border="0" cellpadding="0" cellspacing="0">
108 f35abee2 jim-p
		<tr>
109
		<td>
110 ab6a5cfc Scott Ullrich
			<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
111 f35abee2 jim-p
			<table width="100%" border="0" cellpadding="6" cellspacing="0">
112 0cece4a2 Scott Ullrich
				<tr>
113 1c550bed Renato Botelho
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
114 f35abee2 jim-p
				</tr>
115
				<tr>
116
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
117
					<td width="83%" class="vtable">
118 dddf4c16 Scott Ullrich
				<select name="interface">
119 f35abee2 jim-p
<?php
120 b39ca83c Scott Ullrich
					$interfaces = get_configured_interface_with_descr();
121 0aba3822 jim-p
					if (isset($config['ipsec']['enable']))
122
						$interfaces['ipsec'] = "IPsec";
123
					foreach (array('server', 'client') as $mode) {
124
						if (is_array($config['openvpn']["openvpn-{$mode}"])) {
125
							foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
126
								if (!isset($setting['disable'])) {
127
									$interfaces['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
128
								}
129
							}
130
						}
131
					}
132
					foreach ($interfaces as $iface => $ifacename): ?>
133 f35abee2 jim-p
					<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
134
					<?php echo $ifacename;?>
135
					</option>
136
					<?php endforeach;?>
137
					</select>
138 4a3c31d7 Carlos Eduardo Ramos
					<br/><?=gettext("Select the interface on which to capture traffic.");?>
139 f35abee2 jim-p
					</td>
140
				</tr>
141
				<tr>
142
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
143
					<td width="83%" class="vtable">
144
					<select name="fam">
145
						<option value="">Any</option>
146
						<option value="ip" <?php if ($fam == "ip") echo "selected"; ?>>IPv4 Only</option>
147
						<option value="ip6" <?php if ($fam == "ip6") echo "selected"; ?>>IPv6 Only</option>
148
					</select>
149
					<br/><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
150
					</td>
151 ab6a5cfc Scott Ullrich
				</tr>
152 f35abee2 jim-p
				<tr>
153
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Host Address");?></td>
154
					<td width="83%" class="vtable">
155
					<input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
156
					<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.");?>
157
					<br/><?=gettext("This value can be a domain name or IP address, or subnet in CIDR notation.");?>
158 4a3c31d7 Carlos Eduardo Ramos
					<br/><?=gettext("If you leave this field blank, all packets on the specified interface will be captured.");?>
159 ab6a5cfc Scott Ullrich
					</td>
160
				</tr>
161
				<tr>
162 f35abee2 jim-p
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Port");?></td>
163
					<td width="83%" class="vtable">
164
					<input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
165 1c550bed Renato Botelho
					<br/><?=gettext("The port can be either the source or destination port. The packet capture will look for this port in either field.");?>
166
					<br/><?=gettext("Leave blank if you do not want to filter by port.");?>
167 ab6a5cfc Scott Ullrich
					</td>
168
				</tr>
169
				<tr>
170 f35abee2 jim-p
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Packet Length");?></td>
171
					<td width="83%" class="vtable">
172
					<input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
173 68ed7d9d Chris Buechler
					<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.");?>
174 ab6a5cfc Scott Ullrich
					</td>
175
				</tr>
176
				<tr>
177 f35abee2 jim-p
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
178
					<td width="83%" class="vtable">
179
					<input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
180 1c550bed Renato Botelho
					<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.");?>
181 ab6a5cfc Scott Ullrich
				</tr>
182
				<tr>
183 f35abee2 jim-p
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
184
					<td width="83%" class="vtable">
185
					<select name="detail" type="text" class="formselect" id="detail" size="1">
186 1c550bed Renato Botelho
						<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>><?=gettext("Normal");?></option>
187
						<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>><?=gettext("Medium");?></option>
188
						<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>><?=gettext("High");?></option>
189
						<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>><?=gettext("Full");?></option>
190 ab6a5cfc Scott Ullrich
					</select>
191 1c550bed Renato Botelho
					<br/><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br/><b>" .
192 4a3c31d7 Carlos Eduardo Ramos
							gettext("Note:") . "</b> " .
193 1c550bed Renato Botelho
							gettext("This option does not affect the level of detail when downloading the packet capture.");?>
194 ab6a5cfc Scott Ullrich
				</tr>
195
				<tr>
196 f35abee2 jim-p
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
197
					<td width="83%" class="vtable">
198 ab6a5cfc Scott Ullrich
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
199 1c550bed Renato Botelho
					<br/><?=gettext("This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.");?>
200
					<br/><b><?=gettext("Note");?>: </b><?=gettext("This option can cause delays for large packet captures.");?>
201 ab6a5cfc Scott Ullrich
					</td>
202
				</tr>
203
				<tr>
204 f35abee2 jim-p
					<td width="17%" valign="top">&nbsp;</td>
205
					<td width="83%">
206 b39ca83c Scott Ullrich
<?php
207 d5c2fde5 Scott Ullrich
208 f35abee2 jim-p
					/* check to see if packet capture tcpdump is already running */
209 d67b6b17 jim-p
					$processcheck = (trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'")));
210 f35abee2 jim-p
211 0c951d9b Scott Ullrich
					if ($processcheck != "")
212 9b1b06b5 Scott Dale
						$processisrunning = true;
213 f35abee2 jim-p
					else
214 0c951d9b Scott Ullrich
						$processisrunning = false;
215
216 0cd8ee5c Erik Fonnesbeck
					if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
217 1c550bed Renato Botelho
						echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\">&nbsp;";
218 f35abee2 jim-p
					else {
219
						echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\">&nbsp;";
220
					}
221 b39ca83c Scott Ullrich
					if (file_exists($fp.$fn) and $processisrunning != true) {
222 1c550bed Renato Botelho
						echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
223 4a3c31d7 Carlos Eduardo Ramos
						echo "&nbsp;&nbsp;(" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
224 ab6a5cfc Scott Ullrich
					}
225 b39ca83c Scott Ullrich
?>
226 f35abee2 jim-p
					</td>
227 ab6a5cfc Scott Ullrich
				</tr>
228
				<tr>
229
				<td valign="top" colspan="2">
230 b39ca83c Scott Ullrich
<?php
231 9b1b06b5 Scott Dale
				echo "<font face='terminal' size='2'>";
232
				if ($processisrunning == true)
233 1c550bed Renato Botelho
						echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
234 f35abee2 jim-p
235
				if ($do_tcpdump) {
236
					$matches = array();
237
238
					if (($fam == "ip6") || ($fam == "ip"))
239
						$matches[] = $fam;
240
241
					if ($port != "")
242
						$matches[] = "port ".$port;
243
244
					if ($host != "") {
245
						if (is_ipaddr($host))
246
							$matches[] = "host " . $host;
247
						elseif (is_subnet($host))
248
							$matches[] = "net " . $host;
249
					}
250
251
					if ($count != "0" ) {
252
						$searchcount = "-c " . $count;
253 b39ca83c Scott Ullrich
					} else {
254 f35abee2 jim-p
						$searchcount = "";
255
					}
256 ab6a5cfc Scott Ullrich
257
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
258 f35abee2 jim-p
259 1c550bed Renato Botelho
					if ($action == gettext("Start")) {
260 f35abee2 jim-p
						$matchstr = implode($matches, " and ");
261 1c550bed Renato Botelho
						echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
262 f35abee2 jim-p
						mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr");
263 b0c2087e jim-p
						// echo "/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr";
264 f35abee2 jim-p
					} else {
265 b39ca83c Scott Ullrich
						//action = stop
266 4a3c31d7 Carlos Eduardo Ramos
						echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
267 b39ca83c Scott Ullrich
?>
268 ab6a5cfc Scott Ullrich
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
269 b39ca83c Scott Ullrich
<?php
270
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");
271
272
						conf_mount_ro();
273
?>
274
						</textarea>
275
<?php
276 ab6a5cfc Scott Ullrich
					}
277 b39ca83c Scott Ullrich
				}
278
?>
279 ab6a5cfc Scott Ullrich
				</td>
280
				</tr>
281
				<tr>
282
283
		</table>
284
</form>
285 b39ca83c Scott Ullrich
</td>
286
</tr>
287
</table>
288 4e7d1665 Scott Ullrich
289 f35abee2 jim-p
<?php
290
include("fend.inc");
291 4e7d1665 Scott Ullrich
?>