Project

General

Profile

Download (9.79 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 13d193c2 Scott Ullrich
/*	
27
	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 ab6a5cfc Scott Ullrich
$pgtitle = array("Diagnostics", "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
$snaplen = 1500;//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 4e7d1665 Scott Ullrich
	
56
	conf_mount_rw();
57 ab6a5cfc Scott Ullrich
58 b39ca83c Scott Ullrich
	if ($_POST['dnsquery']) {
59
		//if dns lookup is checked
60 ab6a5cfc Scott Ullrich
		$disabledns = "";
61 b39ca83c Scott Ullrich
	} else {
62
		//if dns lookup is unchecked
63 ab6a5cfc Scott Ullrich
		$disabledns = "-n";
64
	}
65
66 b39ca83c Scott Ullrich
	if ($_POST['startbtn'] != "" ) {
67 ab6a5cfc Scott Ullrich
		$action = "Start";
68 232d6540 Scott Dale
		
69
	 	//delete previous packet capture if it exists
70
	 	if (file_exists($fp.$fn))
71
	 		unlink ($fp.$fn);
72 ab6a5cfc Scott Ullrich
73 b39ca83c Scott Ullrich
	} elseif ($_POST['stopbtn']!= "") {
74 ab6a5cfc Scott Ullrich
		$action = "Stop";
75 f426f89e Chris Buechler
		$processes_running = trim(shell_exec('/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep $fn | /usr/bin/grep -v pflog'));
76 d5c2fde5 Scott Ullrich
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 b39ca83c Scott Ullrich
		foreach ($processes_running_array as $process) {
82 d5c2fde5 Scott Ullrich
			$process_id_pos = strpos($process, ' ');
83
			$process_id = substr($process, 0, $process_id_pos);
84
			exec("kill $process_id");
85
		}
86
87 b39ca83c Scott Ullrich
	} else {
88
		//download file
89 93c86d2b Scott Dale
		$fs = filesize($fp.$fn);
90 ab6a5cfc Scott Ullrich
		header("Content-Type: application/octet-stream");
91 9f3259e0 Scott Dale
        header("Content-Disposition: attachment; filename=$fn");
92 ab6a5cfc Scott Ullrich
		header("Content-Length: $fs");
93
		readfile($fp.$fn);
94
	}
95 b39ca83c Scott Ullrich
} else {
96 ab6a5cfc Scott Ullrich
	$do_tcpdump = false;
97
}
98
$pgtitle = "Diagnostics: Packet Capture";
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
include("fbegin.inc"); 
105
?>
106 ab6a5cfc Scott Ullrich
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 0cece4a2 Scott Ullrich
				<tr>
113
					<td colspan="2" valign="top" class="listtopic">Packet capture</td>
114
				</tr>	
115 ab6a5cfc Scott Ullrich
               	<tr>
116
				  <td width="17%" valign="top" class="vncellreq">Interface</td>
117
				  <td width="83%" class="vtable">
118 dddf4c16 Scott Ullrich
				<select name="interface">
119 b39ca83c Scott Ullrich
<?php 
120
					$interfaces = get_configured_interface_with_descr();
121
					foreach ($interfaces as $iface => $ifacename): 
122
?>
123 7ec05d27 Ermal Luçi
                      <option value="<?=$iface;?>" <?php if (!link_interface_to_bridge($iface) && $selectedif == $iface) echo "selected"; ?>>
124 ab6a5cfc Scott Ullrich
                      <?php echo $ifacename;?>
125
                      </option>
126
                      <?php endforeach;?>
127
                    </select>
128 bda839a7 Scott Dale
                    <br/>Select the interface the traffic will be passing through. Typically this will be the WAN interface.
129 ab6a5cfc Scott Ullrich
				  </td>
130
				</tr>
131
			    <tr>
132
				  <td width="17%" valign="top" class="vncellreq">Host Address</td>
133
				  <td width="83%" class="vtable">
134 b5c78501 Seth Mos
                    <input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
135 bda839a7 Scott Dale
					<br/>This value is either the Source or Destination IP address. The packet capture will look for this address in either field.
136
					<br/>This value can be a domain name or IP address.
137
					<br/>If you leave this field blank all packets on the specified interface will be captured 
138 ab6a5cfc Scott Ullrich
					</td>
139
				</tr>
140
				<tr>
141
				  <td width="17%" valign="top" class="vncellreq">Port</td>
142
				  <td width="83%" class="vtable">
143 b5c78501 Seth Mos
                    <input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
144 bda839a7 Scott Dale
					<br/>The port can be either the source or destination port. The packet capture will look for this port in either field.
145
					<br/>Leave blank if you do not want to the capture to filter by port.
146 ab6a5cfc Scott Ullrich
					</td>
147
				</tr>
148
				<tr>
149
				  <td width="17%" valign="top" class="vncellreq">Packet Length</td>
150
				  <td width="83%" class="vtable">
151 b5c78501 Seth Mos
                    <input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
152 bda839a7 Scott Dale
					<br/>The Packet length is the number of bytes the packet will capture for each payload. Default value is 1500.
153
					<br/>This value should be the same as the MTU of the Interface selected above.
154 ab6a5cfc Scott Ullrich
					</td>
155
				</tr>
156
				<tr>
157
				  <td width="17%" valign="top" class="vncellreq">Count</td>
158
				  <td width="83%" class="vtable">
159 b5c78501 Seth Mos
                    <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
160 bda839a7 Scott Dale
					<br/>This is the number of packets the packet capture will grab. Default value is 100. <br/>Enter 0 (zero) for no count limit.
161 ab6a5cfc Scott Ullrich
				</tr>
162
				<tr>
163
				  <td width="17%" valign="top" class="vncellreq">Level of Detail</td>
164
				  <td width="83%" class="vtable">
165 b5c78501 Seth Mos
                    <select name="detail" type="text" class="formselect" id="detail" size="1">
166 ab6a5cfc Scott Ullrich
						<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>>Normal</option>
167
						<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>>Medium</option>
168
						<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>>High</option>
169
						<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>>Full</option>
170
					</select>
171 bda839a7 Scott Dale
					<br/>This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured. <br/><b>Note:</b> This option does not affect the level of detail when downloading the packet capture.
172 ab6a5cfc Scott Ullrich
				</tr>
173
				<tr>
174
				  <td width="17%" valign="top" class="vncellreq">Reverse DNS Lookup</td>
175
				  <td width="83%" class="vtable">
176
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
177 bda839a7 Scott Dale
					<br/>This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.
178
					<br/><b>Note: </b>This option can be CPU intensive for large packet captures.
179 ab6a5cfc Scott Ullrich
					</td>
180
				</tr>
181
				<tr>
182
				  <td width="17%" valign="top">&nbsp;</td>
183
				  <td width="83%">
184 b39ca83c Scott Ullrich
<?php
185 d5c2fde5 Scott Ullrich
186 b39ca83c Scott Ullrich
                    /* check to see if packet capture tcpdump is already running */
187 00c89b35 Chris Buechler
					$processcheck = (trim(shell_exec('/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep $fn | /usr/bin/grep -v pflog')));
188 9b1b06b5 Scott Dale
					
189
					$processisrunning = false;
190 d5c2fde5 Scott Ullrich
191 9b1b06b5 Scott Dale
					if ($processcheck != false)
192
						$processisrunning = true;
193
						
194
					if (($action == "Stop" or $action == "") and $processisrunning != true)
195 ab6a5cfc Scott Ullrich
						echo "<input type=\"submit\" name=\"startbtn\" value=\"Start\">&nbsp;";
196 b39ca83c Scott Ullrich
				  	else {
197 ab6a5cfc Scott Ullrich
					  	echo "<input type=\"submit\" name=\"stopbtn\" value=\"Stop\">&nbsp;";
198 232d6540 Scott Dale
				  	}
199 b39ca83c Scott Ullrich
					if (file_exists($fp.$fn) and $processisrunning != true) {
200 ab6a5cfc Scott Ullrich
						echo "<input type=\"submit\" name=\"downloadbtn\" value=\"Download Capture\">";
201
						echo "&nbsp;&nbsp;(The packet capture file was last updated: " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
202
					}
203 b39ca83c Scott Ullrich
?>
204 ab6a5cfc Scott Ullrich
				  </td>
205
				</tr>
206
				<tr>
207
				<td valign="top" colspan="2">
208 b39ca83c Scott Ullrich
<?php
209 9b1b06b5 Scott Dale
				echo "<font face='terminal' size='2'>";
210
				if ($processisrunning == true)
211
						echo("<strong>Packet Capture is running.</strong><br/>");
212
						
213
				if ($do_tcpdump) {					
214 ab6a5cfc Scott Ullrich
215 b39ca83c Scott Ullrich
					if ($port != "") {
216 232d6540 Scott Dale
                       $searchport = "and port ".$port;
217
                       if($host <> "")                        
218
							$searchport = "and port ".$port;
219
						else
220
							$searchport = "port ".$port;
221 b39ca83c Scott Ullrich
                    } else {
222 232d6540 Scott Dale
                        $searchport = "";
223
                    }
224
225 b39ca83c Scott Ullrich
       				if ($host != "") {
226 bda839a7 Scott Dale
             	       $searchhost = "host " . $host;
227 b39ca83c Scott Ullrich
					} else {
228 232d6540 Scott Dale
                       $searchhost = "";
229 bda839a7 Scott Dale
             		}
230 b39ca83c Scott Ullrich
             		if ($count != "0" ) {
231 bda839a7 Scott Dale
             			 $searchcount = "-c " . $count;
232 b39ca83c Scott Ullrich
             		} else {
233 bda839a7 Scott Dale
             			$searchcount = "";
234
             		}
235 ab6a5cfc Scott Ullrich
236
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
237 b39ca83c Scott Ullrich
	
238
					if ($action == "Start") {
239 bda839a7 Scott Dale
						echo("<strong>Packet Capture is running.</strong><br/>");
240 9b1b06b5 Scott Dale
					 	mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $searchhost $searchport");
241 b39ca83c Scott Ullrich
					} else  {
242
						//action = stop
243 bda839a7 Scott Dale
						echo("<strong>Packet Capture stopped. <br/><br/>Packets Captured:</strong><br/>");
244 b39ca83c Scott Ullrich
?>
245 ab6a5cfc Scott Ullrich
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
246 b39ca83c Scott Ullrich
<?php
247
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");
248
249
						conf_mount_ro();
250
?>
251
						</textarea>
252
<?php
253 ab6a5cfc Scott Ullrich
					}
254 b39ca83c Scott Ullrich
				}
255
?>
256 ab6a5cfc Scott Ullrich
				</td>
257
				</tr>
258
				<tr>
259
260
		</table>
261
</form>
262 b39ca83c Scott Ullrich
</td>
263
</tr>
264
</table>
265 4e7d1665 Scott Ullrich
266 b39ca83c Scott Ullrich
<?php 
267 4e7d1665 Scott Ullrich
include("fend.inc"); 
268
?>