Project

General

Profile

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