PageRenderTime 1419ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/local/www/diag_packet_capture.php

https://github.com/vongrippen/pfsense
PHP | 293 lines | 222 code | 29 blank | 42 comment | 60 complexity | e3071ca093efc125415d21f2434bc609 MD5 | raw file
  1. <?php
  2. /*
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  11. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  12. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  13. AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  14. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  15. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  16. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  17. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  18. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  19. POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. /*
  22. pfSense_BUILDER_BINARIES: /bin/ps /usr/bin/grep /usr/sbin/tcpdump
  23. pfSense_MODULE: routing
  24. */
  25. ##|+PRIV
  26. ##|*IDENT=page-diagnostics-packetcapture
  27. ##|*NAME=Diagnostics: Packet Capture page
  28. ##|*DESCR=Allow access to the 'Diagnostics: Packet Capture' page.
  29. ##|*MATCH=diag_packet_capture.php*
  30. ##|-PRIV
  31. if ($_POST['downloadbtn'] == gettext("Download Capture"))
  32. $nocsrf = true;
  33. $pgtitle = array(gettext("Diagnostics"), gettext("Packet Capture"));
  34. require_once("guiconfig.inc");
  35. require_once("pfsense-utils.inc");
  36. $fp = "/root/";
  37. $fn = "packetcapture.cap";
  38. $snaplen = 0;//default packet length
  39. $count = 100;//default number of packets to capture
  40. if ($_POST) {
  41. $do_tcpdump = true;
  42. $host = $_POST['host'];
  43. $selectedif = $_POST['interface'];
  44. $count = $_POST['count'];
  45. $packetlength = $_POST['snaplen'];
  46. $port = $_POST['port'];
  47. $detail = $_POST['detail'];
  48. $fam = $_POST['fam'];
  49. conf_mount_rw();
  50. if ($_POST['dnsquery']) {
  51. //if dns lookup is checked
  52. $disabledns = "";
  53. } else {
  54. //if dns lookup is unchecked
  55. $disabledns = "-n";
  56. }
  57. if ($_POST['startbtn'] != "" ) {
  58. $action = gettext("Start");
  59. //delete previous packet capture if it exists
  60. if (file_exists($fp.$fn))
  61. unlink ($fp.$fn);
  62. } elseif ($_POST['stopbtn']!= "") {
  63. $action = gettext("Stop");
  64. $processes_running = trim(shell_exec("/bin/ps axw -O pid= | /usr/bin/grep tcpdump | /usr/bin/grep {$fn} | /usr/bin/egrep -v '(pflog|grep)'"));
  65. //explode processes into an array, (delimiter is new line)
  66. $processes_running_array = explode("\n", $processes_running);
  67. //kill each of the packetcapture processes
  68. foreach ($processes_running_array as $process) {
  69. $process_id_pos = strpos($process, ' ');
  70. $process_id = substr($process, 0, $process_id_pos);
  71. exec("kill $process_id");
  72. }
  73. } else {
  74. //download file
  75. $fs = filesize($fp.$fn);
  76. header("Content-Type: application/octet-stream");
  77. header("Content-Disposition: attachment; filename=$fn");
  78. header("Content-Length: $fs");
  79. readfile($fp.$fn);
  80. exit;
  81. }
  82. } else {
  83. $do_tcpdump = false;
  84. }
  85. include("head.inc"); ?>
  86. <body link="#000000" vlink="#0000CC" alink="#0000CC">
  87. <?php
  88. include("fbegin.inc");
  89. ?>
  90. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  91. <tr><td>
  92. <form action="diag_packet_capture.php" method="post" name="iform" id="iform">
  93. <table width="100%" border="0" cellpadding="6" cellspacing="0">
  94. <tr>
  95. <td colspan="2" valign="top" class="listtopic"><?=gettext("Packet capture");?></td>
  96. </tr>
  97. <tr>
  98. <td width="17%" valign="top" class="vncellreq"><?=gettext("Interface");?></td>
  99. <td width="83%" class="vtable">
  100. <select name="interface">
  101. <?php
  102. $interfaces = get_configured_interface_with_descr();
  103. if (isset($config['ipsec']['enable']))
  104. $interfaces['ipsec'] = "IPsec";
  105. foreach (array('server', 'client') as $mode) {
  106. if (is_array($config['openvpn']["openvpn-{$mode}"])) {
  107. foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
  108. if (!isset($setting['disable'])) {
  109. $interfaces['ovpn' . substr($mode, 0, 1) . $setting['vpnid']] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
  110. }
  111. }
  112. }
  113. }
  114. foreach ($interfaces as $iface => $ifacename): ?>
  115. <option value="<?=$iface;?>" <?php if ($selectedif == $iface) echo "selected"; ?>>
  116. <?php echo $ifacename;?>
  117. </option>
  118. <?php endforeach;?>
  119. </select>
  120. <br/><?=gettext("Select the interface on which to capture traffic.");?>
  121. </td>
  122. </tr>
  123. <tr>
  124. <td width="17%" valign="top" class="vncellreq"><?=gettext("Address Family");?></td>
  125. <td width="83%" class="vtable">
  126. <select name="fam">
  127. <option value="">Any</option>
  128. <option value="ip" <?php if ($fam == "ip") echo "selected"; ?>>IPv4 Only</option>
  129. <option value="ip6" <?php if ($fam == "ip6") echo "selected"; ?>>IPv6 Only</option>
  130. </select>
  131. <br/><?=gettext("Select the type of traffic to be captured, either Any, IPv4 only or IPv6 only.");?>
  132. </td>
  133. </tr>
  134. <tr>
  135. <td width="17%" valign="top" class="vncellreq"><?=gettext("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/><?=gettext("This value is either the Source or Destination IP address or subnet in CIDR notation. The packet capture will look for this address in either field.");?>
  139. <br/><?=gettext("This value can be a domain name or IP address, or subnet in CIDR notation.");?>
  140. <br/><?=gettext("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"><?=gettext("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/><?=gettext("The port can be either the source or destination port. The packet capture will look for this port in either field.");?>
  148. <br/><?=gettext("Leave blank if you do not want to filter by port.");?>
  149. </td>
  150. </tr>
  151. <tr>
  152. <td width="17%" valign="top" class="vncellreq"><?=gettext("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/><?=gettext("The Packet length is the number of bytes of each packet that will be captured. Default value is 0, which will capture the entire frame regardless of its size.");?>
  156. </td>
  157. </tr>
  158. <tr>
  159. <td width="17%" valign="top" class="vncellreq"><?=gettext("Count");?></td>
  160. <td width="83%" class="vtable">
  161. <input name="count" type="text" class="formfld unknown" id="count" size="5" value="<?=$count;?>">
  162. <br/><?=gettext("This is the number of packets the packet capture will grab. Default value is 100.") . "<br/>" . gettext("Enter 0 (zero) for no count limit.");?>
  163. </tr>
  164. <tr>
  165. <td width="17%" valign="top" class="vncellreq"><?=gettext("Level of Detail");?></td>
  166. <td width="83%" class="vtable">
  167. <select name="detail" type="text" class="formselect" id="detail" size="1">
  168. <option value="-q" <?php if ($detail == "-q") echo "selected"; ?>><?=gettext("Normal");?></option>
  169. <option value="-v" <?php if ($detail == "-v") echo "selected"; ?>><?=gettext("Medium");?></option>
  170. <option value="-vv" <?php if ($detail == "-vv") echo "selected"; ?>><?=gettext("High");?></option>
  171. <option value="-vv -e" <?php if ($detail == "-vv -e") echo "selected"; ?>><?=gettext("Full");?></option>
  172. </select>
  173. <br/><?=gettext("This is the level of detail that will be displayed after hitting 'Stop' when the packets have been captured.") . "<br/><b>" .
  174. gettext("Note:") . "</b> " .
  175. gettext("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"><?=gettext("Reverse DNS Lookup");?></td>
  179. <td width="83%" class="vtable">
  180. <input name="dnsquery" type="checkbox"<?php if($_POST['dnsquery']) echo " CHECKED"; ?>>
  181. <br/><?=gettext("This check box will cause the packet capture to perform a reverse DNS lookup associated with all IP addresses.");?>
  182. <br/><b><?=gettext("Note");?>: </b><?=gettext("This option can cause delays 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. /* 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/egrep -v '(pflog|grep)'")));
  191. if ($processcheck != "")
  192. $processisrunning = true;
  193. else
  194. $processisrunning = false;
  195. if (($action == gettext("Stop") or $action == "") and $processisrunning != true)
  196. echo "<input type=\"submit\" name=\"startbtn\" value=\"" . gettext("Start") . "\">&nbsp;";
  197. else {
  198. echo "<input type=\"submit\" name=\"stopbtn\" value=\"" . gettext("Stop") . "\">&nbsp;";
  199. }
  200. if (file_exists($fp.$fn) and $processisrunning != true) {
  201. echo "<input type=\"submit\" name=\"downloadbtn\" value=\"" . gettext("Download Capture") . "\">";
  202. echo "&nbsp;&nbsp;(" . gettext("The packet capture file was last updated:") . " " . date("F jS, Y g:i:s a.", filemtime($fp.$fn)) . ")";
  203. }
  204. ?>
  205. </td>
  206. </tr>
  207. </table>
  208. </form>
  209. <table width="100%" border="0" cellpadding="6" cellspacing="0">
  210. <tr>
  211. <td valign="top" colspan="2">
  212. <?php
  213. echo "<font face='terminal' size='2'>";
  214. if ($processisrunning == true)
  215. echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
  216. if ($do_tcpdump) {
  217. $matches = array();
  218. if (($fam == "ip6") || ($fam == "ip"))
  219. $matches[] = $fam;
  220. if ($port != "")
  221. $matches[] = "port ".$port;
  222. if ($host != "") {
  223. if (is_ipaddr($host))
  224. $matches[] = "host " . $host;
  225. elseif (is_subnet($host))
  226. $matches[] = "net " . $host;
  227. }
  228. if ($count != "0" ) {
  229. $searchcount = "-c " . $count;
  230. } else {
  231. $searchcount = "";
  232. }
  233. $selectedif = convert_friendly_interface_to_real_interface_name($selectedif);
  234. if ($action == gettext("Start")) {
  235. $matchstr = implode($matches, " and ");
  236. echo("<strong>" . gettext("Packet Capture is running.") . "</strong><br/>");
  237. mwexec_bg ("/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr");
  238. // echo "/usr/sbin/tcpdump -i $selectedif $searchcount -s $packetlength -w $fp$fn $matchstr";
  239. } else {
  240. //action = stop
  241. echo("<strong>" . gettext("Packet Capture stopped.") . "<br/><br/>" . gettext("Packets Captured:") . "</strong><br/>");
  242. ?>
  243. <textarea style="width:98%" name="code" rows="15" cols="66" wrap="off" readonly="readonly">
  244. <?php
  245. system ("/usr/sbin/tcpdump $disabledns $detail -r $fp$fn");
  246. conf_mount_ro();
  247. ?>
  248. </textarea>
  249. <?php
  250. }
  251. }
  252. ?>
  253. </td>
  254. </tr>
  255. </table>
  256. </td></tr>
  257. </table>
  258. <?php
  259. include("fend.inc");
  260. ?>