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