Project

General

Profile

Download (9.82 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 6b07c15a Matthew Grooms
##|+PRIV
27
##|*IDENT=page-diagnostics-packetcapture
28
##|*NAME=Diagnostics: Packet Capture page
29
##|*DESCR=Allow access to the 'Diagnostics: Packet Capture' page.
30
##|*MATCH=diag_packet_capture.php*
31
##|-PRIV
32
33
34 ab6a5cfc Scott Ullrich
$pgtitle = array("Diagnostics", "Packet Capture");
35 d7cd7129 Scott Ullrich
require_once("guiconfig.inc");
36
require_once("pfsense-utils.inc");
37 ab6a5cfc Scott Ullrich
38 9b1b06b5 Scott Dale
$fp = "/tmp/";
39 ab6a5cfc Scott Ullrich
$fn = "packetcapture.cap";
40
$snaplen = 1500;//default packet length
41
$count = 100;//default number of packets to capture
42
43
if ($_POST) {
44
	$do_tcpdump = true;
45
	$host = $_POST['host'];
46
	$selectedif = $_POST['interface'];
47
	$count = $_POST['count'];
48
	$packetlength = $_POST['snaplen'];
49
	$port = $_POST['port'];
50
	$detail = $_POST['detail'];
51
52
	if ($_POST['dnsquery'])//if dns lookup is checked
53
	{
54
		$disabledns = "";
55
	}
56
	else //if dns lookup is unchecked
57
	{
58
		$disabledns = "-n";
59
	}
60
61
	if ($_POST['startbtn'] != "" )
62
	{
63
		$action = "Start";
64 232d6540 Scott Dale
		
65
	 	//delete previous packet capture if it exists
66
	 	if (file_exists($fp.$fn))
67
	 		unlink ($fp.$fn);
68 ab6a5cfc Scott Ullrich
69
	}
70
	elseif ($_POST['stopbtn']!= "")
71
	{
72
		$action = "Stop";
73 232d6540 Scott Dale
		$processes_running = trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog"));
74 d5c2fde5 Scott Ullrich
75
		//explode processes into an array, (delimiter is new line)
76
		$processes_running_array = explode("\n", $processes_running);
77
78
		//kill each of the packetcapture processes
79
		foreach ($processes_running_array as $process)
80
		{
81
			$process_id_pos = strpos($process, ' ');
82
			$process_id = substr($process, 0, $process_id_pos);
83
			exec("kill $process_id");
84
		}
85
86 ab6a5cfc Scott Ullrich
	}
87
	else //download file
88
	{
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
}
96
else
97
{
98
	$do_tcpdump = false;
99
100
}
101
$pgtitle = "Diagnostics: Packet Capture";
102
include("head.inc"); ?>
103
<body link="#000000" vlink="#0000CC" alink="#0000CC">
104
<? include("fbegin.inc"); ?>
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 0cece4a2 Scott Ullrich
				<tr>
112
					<td colspan="2" valign="top" class="listtopic">Packet capture</td>
113
				</tr>	
114 ab6a5cfc Scott Ullrich
               	<tr>
115
				  <td width="17%" valign="top" class="vncellreq">Interface</td>
116
				  <td width="83%" class="vtable">
117 dddf4c16 Scott Ullrich
				<select name="interface">
118 cbe3ea96 Ermal Luçi
                     <?php $interfaces = get_configured_interface_with_descr();
119 ab6a5cfc Scott Ullrich
					  foreach ($interfaces as $iface => $ifacename): ?>
120 7ec05d27 Ermal Luçi
                      <option value="<?=$iface;?>" <?php if (!link_interface_to_bridge($iface) && $selectedif == $iface) echo "selected"; ?>>
121 ab6a5cfc Scott Ullrich
                      <?php echo $ifacename;?>
122
                      </option>
123
                      <?php endforeach;?>
124
                    </select>
125 bda839a7 Scott Dale
                    <br/>Select the interface the traffic will be passing through. Typically this will be the WAN interface.
126 ab6a5cfc Scott Ullrich
				  </td>
127
				</tr>
128
			    <tr>
129
				  <td width="17%" valign="top" class="vncellreq">Host Address</td>
130
				  <td width="83%" class="vtable">
131 b5c78501 Seth Mos
                    <input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
132 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.
133
					<br/>This value can be a domain name or IP address.
134
					<br/>If you leave this field blank all packets on the specified interface will be captured 
135 ab6a5cfc Scott Ullrich
					</td>
136
				</tr>
137
				<tr>
138
				  <td width="17%" valign="top" class="vncellreq">Port</td>
139
				  <td width="83%" class="vtable">
140 b5c78501 Seth Mos
                    <input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
141 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.
142
					<br/>Leave blank if you do not want to the capture to filter by port.
143 ab6a5cfc Scott Ullrich
					</td>
144
				</tr>
145
				<tr>
146
				  <td width="17%" valign="top" class="vncellreq">Packet Length</td>
147
				  <td width="83%" class="vtable">
148 b5c78501 Seth Mos
                    <input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
149 bda839a7 Scott Dale
					<br/>The Packet length is the number of bytes the packet will capture for each payload. Default value is 1500.
150
					<br/>This value should be the same as the MTU of the Interface selected above.
151 ab6a5cfc Scott Ullrich
					</td>
152
				</tr>
153
				<tr>
154
				  <td width="17%" valign="top" class="vncellreq">Count</td>
155
				  <td width="83%" class="vtable">
156 b5c78501 Seth Mos
                    <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
157 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.
158 ab6a5cfc Scott Ullrich
				</tr>
159
				<tr>
160
				  <td width="17%" valign="top" class="vncellreq">Level of Detail</td>
161
				  <td width="83%" class="vtable">
162 b5c78501 Seth Mos
                    <select name="detail" type="text" class="formselect" id="detail" size="1">
163 ab6a5cfc Scott Ullrich
						<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>>Normal</option>
164
						<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>>Medium</option>
165
						<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>>High</option>
166
						<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>>Full</option>
167
					</select>
168 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.
169 ab6a5cfc Scott Ullrich
				</tr>
170
				<tr>
171
				  <td width="17%" valign="top" class="vncellreq">Reverse DNS Lookup</td>
172
				  <td width="83%" class="vtable">
173
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
174 bda839a7 Scott Dale
					<br/>This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.
175
					<br/><b>Note: </b>This option can be CPU intensive for large packet captures.
176 ab6a5cfc Scott Ullrich
					</td>
177
				</tr>
178
				<tr>
179
				  <td width="17%" valign="top">&nbsp;</td>
180
				  <td width="83%">
181
                    <?php
182 d5c2fde5 Scott Ullrich
183
                    /*check to see if packet capture tcpdump is already running*/
184 232d6540 Scott Dale
					$processcheck = (trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog")));
185 9b1b06b5 Scott Dale
					
186
					$processisrunning = false;
187 d5c2fde5 Scott Ullrich
188 9b1b06b5 Scott Dale
					if ($processcheck != false)
189
						$processisrunning = true;
190
						
191
					if (($action == "Stop" or $action == "") and $processisrunning != true)
192 ab6a5cfc Scott Ullrich
						echo "<input type=\"submit\" name=\"startbtn\" value=\"Start\">&nbsp;";
193 232d6540 Scott Dale
				  	else{
194 ab6a5cfc Scott Ullrich
					  	echo "<input type=\"submit\" name=\"stopbtn\" value=\"Stop\">&nbsp;";
195 232d6540 Scott Dale
				  	}
196 9b1b06b5 Scott Dale
					if (file_exists($fp.$fn) and $processisrunning != true){
197 ab6a5cfc Scott Ullrich
						echo "<input type=\"submit\" name=\"downloadbtn\" value=\"Download Capture\">";
198
						echo "&nbsp;&nbsp;(The packet capture file was last updated: " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
199
					}
200
					?>
201
				  </td>
202
				</tr>
203
				<tr>
204
				<td valign="top" colspan="2">
205
				<?php
206 9b1b06b5 Scott Dale
				echo "<font face='terminal' size='2'>";
207
				if ($processisrunning == true)
208
						echo("<strong>Packet Capture is running.</strong><br/>");
209
						
210
				if ($do_tcpdump) {					
211 ab6a5cfc Scott Ullrich
212
					if ($port != "")
213 232d6540 Scott Dale
                    {
214
                       $searchport = "and port ".$port;
215
                       if($host <> "")                        
216
							$searchport = "and port ".$port;
217
						else
218
							$searchport = "port ".$port;
219
                    }
220
                    else
221
                    {
222
                        $searchport = "";
223
                    }
224
225
       				if ($host != "")
226
         	       {
227 bda839a7 Scott Dale
             	       $searchhost = "host " . $host;
228 232d6540 Scott Dale
            	   }
229
             	   else
230
                	{
231
                       $searchhost = "";
232 bda839a7 Scott Dale
             		}
233
             		if ($count != "0" )
234
             		{
235
             			 $searchcount = "-c " . $count;
236
             		}
237
             		else
238
             		{
239
             			$searchcount = "";
240
             		}
241 ab6a5cfc Scott Ullrich
242
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
243 232d6540 Scott Dale
				
244 9b1b06b5 Scott Dale
					
245 232d6540 Scott Dale
						
246 ab6a5cfc Scott Ullrich
					if ($action == "Start")
247
					{
248 bda839a7 Scott Dale
						echo("<strong>Packet Capture is running.</strong><br/>");
249 9b1b06b5 Scott Dale
					 	mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $searchhost $searchport");
250 ab6a5cfc Scott Ullrich
						}
251 232d6540 Scott Dale
					else  //action = stop
252 ab6a5cfc Scott Ullrich
					{
253 d5c2fde5 Scott Ullrich
254 bda839a7 Scott Dale
						echo("<strong>Packet Capture stopped. <br/><br/>Packets Captured:</strong><br/>");
255 ab6a5cfc Scott Ullrich
						?>
256
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
257
						<?php
258 9b1b06b5 Scott Dale
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");?>
259 ab6a5cfc Scott Ullrich
						</textarea><?php
260
					}
261 232d6540 Scott Dale
				}?>
262 ab6a5cfc Scott Ullrich
				</td>
263
				</tr>
264
				<tr>
265
266
		</table>
267
</form>
268
</td></tr></table>
269
<?php include("fend.inc"); ?>