Project

General

Profile

Download (11 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
$pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
39
require_once("guiconfig.inc");
40
require_once("pfsense-utils.inc");
41

    
42
$fp = "/root/";
43
$fn = "packetcapture.cap";
44
$snaplen = 0;//default packet length
45
$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
	$fam = $_POST['fam'];
56

    
57
	conf_mount_rw();
58

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

    
67
	if ($_POST['startbtn'] != "" ) {
68
		$action = gettext("Start");
69

    
70
		//delete previous packet capture if it exists
71
		if (file_exists($fp.$fn))
72
			unlink ($fp.$fn);
73

    
74
	} elseif ($_POST['stopbtn']!= "") {
75
		$action = gettext("Stop");
76
		$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

    
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
		foreach ($processes_running_array as $process) {
83
			$process_id_pos = strpos($process, ' ');
84
			$process_id = substr($process, 0, $process_id_pos);
85
			exec("kill $process_id");
86
		}
87

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

    
101
<body link="#000000" vlink="#0000CC" alink="#0000CC">
102

    
103
<?php
104
include("fbegin.inc");
105
?>
106

    
107
<table width="100%" border="0" cellpadding="0" cellspacing="0">
108
		<tr>
109
		<td>
110
			<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
111
			<table width="100%" border="0" cellpadding="6" cellspacing="0">
112
				<tr>
113
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
114
				</tr>
115
				<tr>
116
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
117
					<td width="83%" class="vtable">
118
				<select name="interface">
119
<?php
120
					$interfaces = get_configured_interface_with_descr();
121
					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
					<option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
134
					<?php echo $ifacename;?>
135
					</option>
136
					<?php endforeach;?>
137
					</select>
138
					<br/><?=gettext("Select the interface on which to capture traffic.");?>
139
					</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
				</tr>
152
				<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
					<br/><?=gettext("If you leave this field blank, all packets on the specified interface will be captured.");?>
159
					</td>
160
				</tr>
161
				<tr>
162
					<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
					<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
					</td>
168
				</tr>
169
				<tr>
170
					<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
					<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
					</td>
175
				</tr>
176
				<tr>
177
					<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
					<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
				</tr>
182
				<tr>
183
					<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
						<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
					</select>
191
					<br/><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") .  "<br/><b>" .
192
							gettext("Note:") . "</b> " .
193
							gettext("This option does not affect the level of detail when downloading the packet capture.");?>
194
				</tr>
195
				<tr>
196
					<td width="17%" valign="top" class="vncellreq"><?=gettext("Reverse DNS Lookup");?></td>
197
					<td width="83%" class="vtable">
198
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
199
					<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
					</td>
202
				</tr>
203
				<tr>
204
					<td width="17%" valign="top">&nbsp;</td>
205
					<td width="83%">
206
<?php
207

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

    
211
					if ($processcheck != "")
212
						$processisrunning = true;
213
					else
214
						$processisrunning = false;
215

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

    
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
					} else {
254
						$searchcount = "";
255
					}
256

    
257
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
258

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

    
272
						conf_mount_ro();
273
?>
274
						</textarea>
275
<?php
276
					}
277
				}
278
?>
279
				</td>
280
				</tr>
281
				<tr>
282

    
283
		</table>
284
</form>
285
</td>
286
</tr>
287
</table>
288

    
289
<?php
290
include("fend.inc");
291
?>
(31-31/225)