Project

General

Profile

Download (9.82 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
##|+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
$pgtitle = array("Diagnostics", "Packet Capture");
35
require_once("guiconfig.inc");
36
require_once("pfsense-utils.inc");
37

    
38
$fp = "/tmp/";
39
$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
		
65
	 	//delete previous packet capture if it exists
66
	 	if (file_exists($fp.$fn))
67
	 		unlink ($fp.$fn);
68

    
69
	}
70
	elseif ($_POST['stopbtn']!= "")
71
	{
72
		$action = "Stop";
73
		$processes_running = trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog"));
74

    
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
	}
87
	else //download file
88
	{
89
		$fs = filesize($fp.$fn);
90
		header("Content-Type: application/octet-stream");
91
        header("Content-Disposition: attachment; filename=$fn");
92
		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
				<tr>
112
					<td colspan="2" valign="top" class="listtopic">Packet capture</td>
113
				</tr>	
114
               	<tr>
115
				  <td width="17%" valign="top" class="vncellreq">Interface</td>
116
				  <td width="83%" class="vtable">
117
				<select name="interface">
118
                     <?php $interfaces = get_configured_interface_with_descr();
119
					  foreach ($interfaces as $iface => $ifacename): ?>
120
                      <option value="<?=$iface;?>" <?php if (!link_interface_to_bridge($iface) && $selectedif == $iface) echo "selected"; ?>>
121
                      <?php echo $ifacename;?>
122
                      </option>
123
                      <?php endforeach;?>
124
                    </select>
125
                    <br/>Select the interface the traffic will be passing through. Typically this will be the WAN interface.
126
				  </td>
127
				</tr>
128
			    <tr>
129
				  <td width="17%" valign="top" class="vncellreq">Host Address</td>
130
				  <td width="83%" class="vtable">
131
                    <input name="host" type="text" class="formfld host" id="host" size="20" value="<?=htmlspecialchars($host);?>">
132
					<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
					</td>
136
				</tr>
137
				<tr>
138
				  <td width="17%" valign="top" class="vncellreq">Port</td>
139
				  <td width="83%" class="vtable">
140
                    <input name="port" type="text" class="formfld unknown" id="port" size="5" value="<?=$port;?>">
141
					<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
					</td>
144
				</tr>
145
				<tr>
146
				  <td width="17%" valign="top" class="vncellreq">Packet Length</td>
147
				  <td width="83%" class="vtable">
148
                    <input name="snaplen" type="text" class="formfld unknown" id="snaplen" size="5" value="<?=$snaplen;?>">
149
					<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
					</td>
152
				</tr>
153
				<tr>
154
				  <td width="17%" valign="top" class="vncellreq">Count</td>
155
				  <td width="83%" class="vtable">
156
                    <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
157
					<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
				</tr>
159
				<tr>
160
				  <td width="17%" valign="top" class="vncellreq">Level of Detail</td>
161
				  <td width="83%" class="vtable">
162
                    <select name="detail" type="text" class="formselect" id="detail" size="1">
163
						<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
					<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
				</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
					<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
					</td>
177
				</tr>
178
				<tr>
179
				  <td width="17%" valign="top">&nbsp;</td>
180
				  <td width="83%">
181
                    <?php
182

    
183
                    /*check to see if packet capture tcpdump is already running*/
184
					$processcheck = (trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn | grep -v pflog")));
185
					
186
					$processisrunning = false;
187

    
188
					if ($processcheck != false)
189
						$processisrunning = true;
190
						
191
					if (($action == "Stop" or $action == "") and $processisrunning != true)
192
						echo "<input type=\"submit\" name=\"startbtn\" value=\"Start\">&nbsp;";
193
				  	else{
194
					  	echo "<input type=\"submit\" name=\"stopbtn\" value=\"Stop\">&nbsp;";
195
				  	}
196
					if (file_exists($fp.$fn) and $processisrunning != true){
197
						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
				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

    
212
					if ($port != "")
213
                    {
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
             	       $searchhost = "host " . $host;
228
            	   }
229
             	   else
230
                	{
231
                       $searchhost = "";
232
             		}
233
             		if ($count != "0" )
234
             		{
235
             			 $searchcount = "-c " . $count;
236
             		}
237
             		else
238
             		{
239
             			$searchcount = "";
240
             		}
241

    
242
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
243
				
244
					
245
						
246
					if ($action == "Start")
247
					{
248
						echo("<strong>Packet Capture is running.</strong><br/>");
249
					 	mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $searchhost $searchport");
250
						}
251
					else  //action = stop
252
					{
253

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

    
266
		</table>
267
</form>
268
</td></tr></table>
269
<?php include("fend.inc"); ?>
(26-26/216)