Project

General

Profile

Download (10 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
/*	
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("Diagnostics", "Packet Capture");
39
require_once("guiconfig.inc");
40
require_once("pfsense-utils.inc");
41

    
42
$fp = "/root/";
43
$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
	
56
	conf_mount_rw();
57

    
58
	if ($_POST['dnsquery'])//if dns lookup is checked
59
	{
60
		$disabledns = "";
61
	}
62
	else //if dns lookup is unchecked
63
	{
64
		$disabledns = "-n";
65
	}
66

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

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

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

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

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

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

    
112
<table width="100%" border="0" cellpadding="0" cellspacing="0">
113
        <tr>
114
          <td>
115
			<form action="diag_packet_capture.php" method="post" name="iform" id="iform">
116
			  <table width="100%" border="0" cellpadding="6" cellspacing="0">
117
				<tr>
118
					<td colspan="2" valign="top" class="listtopic">Packet capture</td>
119
				</tr>	
120
               	<tr>
121
				  <td width="17%" valign="top" class="vncellreq">Interface</td>
122
				  <td width="83%" class="vtable">
123
				<select name="interface">
124
                     <?php $interfaces = get_configured_interface_with_descr();
125
					  foreach ($interfaces as $iface => $ifacename): ?>
126
                      <option value="<?=$iface;?>" <?php if (!link_interface_to_bridge($iface) && $selectedif == $iface) echo "selected"; ?>>
127
                      <?php echo $ifacename;?>
128
                      </option>
129
                      <?php endforeach;?>
130
                    </select>
131
                    <br/>Select the interface the traffic will be passing through. Typically this will be the WAN interface.
132
				  </td>
133
				</tr>
134
			    <tr>
135
				  <td width="17%" valign="top" class="vncellreq">Host Address</td>
136
				  <td width="83%" class="vtable">
137
                    <input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
138
					<br/>This value is either the Source or Destination IP address. The packet capture will look for this address in either field.
139
					<br/>This value can be a domain name or IP address.
140
					<br/>If you leave this field blank all packets on the specified interface will be captured 
141
					</td>
142
				</tr>
143
				<tr>
144
				  <td width="17%" valign="top" class="vncellreq">Port</td>
145
				  <td width="83%" class="vtable">
146
                    <input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
147
					<br/>The port can be either the source or destination port. The packet capture will look for this port in either field.
148
					<br/>Leave blank if you do not want to the capture to filter by port.
149
					</td>
150
				</tr>
151
				<tr>
152
				  <td width="17%" valign="top" class="vncellreq">Packet Length</td>
153
				  <td width="83%" class="vtable">
154
                    <input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
155
					<br/>The Packet length is the number of bytes the packet will capture for each payload. Default value is 1500.
156
					<br/>This value should be the same as the MTU of the Interface selected above.
157
					</td>
158
				</tr>
159
				<tr>
160
				  <td width="17%" valign="top" class="vncellreq">Count</td>
161
				  <td width="83%" class="vtable">
162
                    <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
163
					<br/>This is the number of packets the packet capture will grab. Default value is 100. <br/>Enter 0 (zero) for no count limit.
164
				</tr>
165
				<tr>
166
				  <td width="17%" valign="top" class="vncellreq">Level of Detail</td>
167
				  <td width="83%" class="vtable">
168
                    <select name="detail" type="text" class="formselect" id="detail" size="1">
169
						<option value="-q" <?php if ($detail == "-q") echo "selected"; ?>>Normal</option>
170
						<option value="-v" <?php if ($detail == "-v") echo "selected"; ?>>Medium</option>
171
						<option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>>High</option>
172
						<option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>>Full</option>
173
					</select>
174
					<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.
175
				</tr>
176
				<tr>
177
				  <td width="17%" valign="top" class="vncellreq">Reverse DNS Lookup</td>
178
				  <td width="83%" class="vtable">
179
					<input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
180
					<br/>This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.
181
					<br/><b>Note: </b>This option can be CPU intensive for large packet captures.
182
					</td>
183
				</tr>
184
				<tr>
185
				  <td width="17%" valign="top">&nbsp;</td>
186
				  <td width="83%">
187
                    <?php
188

    
189
                    /*check to see if packet capture tcpdump is already running*/
190
					$processcheck = (trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep $fn | /usr/bin/grep -v pflog")));
191
					
192
					$processisrunning = false;
193

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

    
218
					if ($port != "")
219
                    {
220
                       $searchport = "and port ".$port;
221
                       if($host <> "")                        
222
							$searchport = "and port ".$port;
223
						else
224
							$searchport = "port ".$port;
225
                    }
226
                    else
227
                    {
228
                        $searchport = "";
229
                    }
230

    
231
       				if ($host != "")
232
         	       {
233
             	       $searchhost = "host " . $host;
234
            	   }
235
             	   else
236
                	{
237
                       $searchhost = "";
238
             		}
239
             		if ($count != "0" )
240
             		{
241
             			 $searchcount = "-c " . $count;
242
             		}
243
             		else
244
             		{
245
             			$searchcount = "";
246
             		}
247

    
248
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
249
				
250
					
251
						
252
					if ($action == "Start")
253
					{
254
						echo("<strong>Packet Capture is running.</strong><br/>");
255
					 	mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $searchhost $searchport");
256
						}
257
					else  //action = stop
258
					{
259

    
260
						echo("<strong>Packet Capture stopped. <br/><br/>Packets Captured:</strong><br/>");
261
						?>
262
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
263
						<?php
264
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");?>
265
						</textarea><?php
266
					}
267
				}?>
268
				</td>
269
				</tr>
270
				<tr>
271

    
272
		</table>
273
</form>
274
</td></tr></table>
275
<?php 
276

    
277
conf_mount_ro();
278

    
279
include("fend.inc"); 
280

    
281
?>
(28-28/217)