Project

General

Profile

Download (9.13 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 = "/usr/local/www/";
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
	}
73
	elseif ($_POST['stopbtn']!= "")
74
	{
75
		$action = "Stop";
76
		$processes_running = trim(shell_exec("ps axw -O pid= | grep tcpdump | grep $fn"));
77

    
78
		//explode processes into an array, (delimiter is new line)
79
		$processes_running_array = explode("\n", $processes_running);
80

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

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

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

    
109

    
110

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

    
190
                    /*check to see if packet capture tcpdump is already running*/
191
					$processcheck = (trim(shell_exec("ps axwu | grep tcpdump | grep -v 'grep' | grep -i $fn")));
192

    
193
					$processisrunning = False;
194

    
195
					if ($processcheck != False)
196
						$processisrunning = True;
197

    
198

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

    
216
					if ($port != "")
217
					{
218
						$searchport = "and port ".$port;
219
					}
220
					else
221
					{
222
						$searchport = "";
223
					}
224

    
225
					$selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
226

    
227
					if ($action == "Start")
228
					{
229
					 	//delete previous packet capture if it exists
230
					 	if (file_exists($fp.$fn))
231
					 		unlink ($fp.$fn);
232

    
233
				 		echo("<strong>Packet Capture is running.</strong><br>");
234
					 	exec ("/usr/sbin/tcpdump -i $selectedif -v -c $count -s $packetlength -w $fn host $host $searchport");
235
						}
236
					else //action = stop
237
					{
238

    
239
						echo("<strong>Packet Capture stopped. <br><br>Packets Captured:</strong><br>");
240
						?>
241
						<textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
242
						<?php
243
						system ("/usr/sbin/tcpdump $disabledns $detail -r $fn");?>
244
						</textarea><?php
245
					}
246
				}
247
				if ($processisrunning)
248
					echo("<strong>Packet Capture is running.</strong><br>");?>
249
				</td>
250
				</tr>
251
				<tr>
252

    
253
		</table>
254
</form>
255
</td></tr></table>
256
<?php include("fend.inc"); ?>
(23-23/169)