Project

General

Profile

Download (10.1 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
$pgtitle = array("Diagnostics", "Packet Capture");
27
require_once("guiconfig.inc");
28
require_once("pfsense-utils.inc");
29

    
30
$fp = "/tmp/";
31
$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
	global $config;
37

    
38
	$ifdescr = convert_friendly_interface_to_friendly_descr($if);
39

    
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
	if ($_POST['dnsquery'])//if dns lookup is checked
60
	{
61
		$disabledns = "";
62
	}
63
	else //if dns lookup is unchecked
64
	{
65
		$disabledns = "-n";
66
	}
67

    
68
	if ($_POST['startbtn'] != "" )
69
	{
70
		$action = "Start";
71
		
72
	 	//delete previous packet capture if it exists
73
	 	if (file_exists($fp.$fn))
74
	 		unlink ($fp.$fn);
75

    
76
	}
77
	elseif ($_POST['stopbtn']!= "")
78
	{
79
		$action = "Stop";
80
		$processes_running = trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog"));
81

    
82
		//explode processes into an array, (delimiter is new line)
83
		$processes_running_array = explode("\n", $processes_running);
84

    
85
		//kill each of the packetcapture processes
86
		foreach ($processes_running_array as $process)
87
		{
88
			$process_id_pos = strpos($process, ' ');
89
			$process_id = substr($process, 0, $process_id_pos);
90
			exec("kill $process_id");
91
		}
92

    
93
	}
94
	else //download file
95
	{
96
		$fs = filesize($fp.$fn);
97
		header("Content-Type: application/octet-stream");
98
        header("Content-Disposition: attachment; filename=$fn");
99
		header("Content-Length: $fs");
100
		readfile($fp.$fn);
101
	}
102
}
103
else
104
{
105
	$do_tcpdump = false;
106

    
107
}
108
$pgtitle = "Diagnostics: Packet Capture";
109
include("head.inc"); ?>
110
<body link="#000000" vlink="#0000CC" alink="#0000CC">
111
<? include("fbegin.inc"); ?>
112

    
113

    
114

    
115
<p class="pgtitle"><?=$pgtitle?></p>
116
<table width="100%" border="0" cellpadding="0" cellspacing="0">
117
        <tr>
118
          <td>
119
			<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
120
			  <table width="100%" border="0" cellpadding="6" cellspacing="0">
121
               	<tr>
122
				  <td width="17%" valign="top" class="vncellreq">Interface</td>
123
				  <td width="83%" class="vtable">
124
				<select name="interface" class="formfld">
125
                     <?php $interfaces = array('wan' => 'WAN', 'lan' => 'LAN');
126
					  for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
127
					    if (isset($config['interfaces']['opt' . $i]['enable']) &&
128
							!$config['interfaces']['opt' . $i]['bridge'])
129
					  		$interfaces['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
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/>Select the interface the traffic will be passing through. Typically this will be the WAN interface.
138
				  </td>
139
				</tr>
140
			    <tr>
141
				  <td width="17%" valign="top" class="vncellreq">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/>This value is either the Source or Destination IP address. The packet capture will look for this address in either field.
145
					<br/>This value can be a domain name or IP address.
146
					<br/>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">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/>The port can be either the source or destination port. The packet capture will look for this port in either field.
154
					<br/>Leave blank if you do not want to the capture to filter by port.
155
					</td>
156
				</tr>
157
				<tr>
158
				  <td width="17%" valign="top" class="vncellreq">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/>The Packet length is the number of bytes the packet will capture for each payload. Default value is 1500.
162
					<br/>This value should be the same as the MTU of the Interface selected above.
163
					</td>
164
				</tr>
165
				<tr>
166
				  <td width="17%" valign="top" class="vncellreq">Count</td>
167
				  <td width="83%" class="vtable">
168
                    <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
169
					<br/>This is the number of packets the packet capture will grab. Default value is 100. <br/>Enter 0 (zero) for no count limit.
170
				</tr>
171
				<tr>
172
				  <td width="17%" valign="top" class="vncellreq">Level of Detail</td>
173
				  <td width="83%" class="vtable">
174
                    <select name="detail" type="text" class="formselect" id="detail" size="1">
175
						<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>>Normal</option>
176
						<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>>Medium</option>
177
						<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>>High</option>
178
						<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>>Full</option>
179
					</select>
180
					<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.
181
				</tr>
182
				<tr>
183
				  <td width="17%" valign="top" class="vncellreq">Reverse DNS Lookup</td>
184
				  <td width="83%" class="vtable">
185
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
186
					<br/>This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.
187
					<br/><b>Note: </b>This option can be CPU intensive for large packet captures.
188
					</td>
189
				</tr>
190
				<tr>
191
				  <td width="17%" valign="top">&nbsp;</td>
192
				  <td width="83%">
193
                    <?php
194

    
195
                    /*check to see if packet capture tcpdump is already running*/
196
					$processcheck = (trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog")));
197
					
198
					$processisrunning = false;
199

    
200
					if ($processcheck != false)
201
						$processisrunning = true;
202
						
203
					if (($action == "Stop" or $action == "") and $processisrunning != true)
204
						echo "<input type=\"submit\" name=\"startbtn\" value=\"Start\">&nbsp;";
205
				  	else{
206
					  	echo "<input type=\"submit\" name=\"stopbtn\" value=\"Stop\">&nbsp;";
207
				  	}
208
					if (file_exists($fp.$fn) and $processisrunning != true){
209
						echo "<input type=\"submit\" name=\"downloadbtn\" value=\"Download Capture\">";
210
						echo "&nbsp;&nbsp;(The packet capture file was last updated: " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
211
					}
212
					?>
213
				  </td>
214
				</tr>
215
				<tr>
216
				<td valign="top" colspan="2">
217
				<?php
218
				echo "<font face='terminal' size='2'>";
219
				if ($processisrunning == true)
220
						echo("<strong>Packet Capture is running.</strong><br/>");
221
						
222
				if ($do_tcpdump) {					
223

    
224
					if ($port != "")
225
                    {
226
                       $searchport = "and port ".$port;
227
                       if($host <> "")                        
228
							$searchport = "and port ".$port;
229
						else
230
							$searchport = "port ".$port;
231
                    }
232
                    else
233
                    {
234
                        $searchport = "";
235
                    }
236

    
237
       				if ($host != "")
238
         	       {
239
             	       $searchhost = "host " . $host;
240
            	   }
241
             	   else
242
                	{
243
                       $searchhost = "";
244
             		}
245
             		if ($count != "0" )
246
             		{
247
             			 $searchcount = "-c " . $count;
248
             		}
249
             		else
250
             		{
251
             			$searchcount = "";
252
             		}
253

    
254
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
255
				
256
					
257
						
258
					if ($action == "Start")
259
					{
260
						echo("<strong>Packet Capture is running.</strong><br/>");
261
					 	mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $searchhost $searchport");
262
						}
263
					else  //action = stop
264
					{
265

    
266
						echo("<strong>Packet Capture stopped. <br/><br/>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
						</textarea><?php
272
					}
273
				}?>
274
				</td>
275
				</tr>
276
				<tr>
277

    
278
		</table>
279
</form>
280
</td></tr></table>
281
<?php include("fend.inc"); ?>
(24-24/186)