PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/icwp-optionshandler-firewall.php

https://github.com/stackgrinder/wp-simple-firewall
PHP | 304 lines | 263 code | 21 blank | 20 comment | 10 complexity | 2997d42adc50329f47f177685e55f85a MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2014 iControlWP <support@icontrolwp.com>
  4. * All rights reserved.
  5. *
  6. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  7. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  8. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  9. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  10. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  11. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  12. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  13. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  15. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. */
  17. require_once( dirname(__FILE__).'/icwp-optionshandler-base.php' );
  18. if ( !class_exists('ICWP_OptionsHandler_Firewall') ):
  19. class ICWP_OptionsHandler_Firewall extends ICWP_OptionsHandler_Base_Wpsf {
  20. const StoreName = 'firewall_options';
  21. public function __construct( $oPluginVo ) {
  22. parent::__construct( $oPluginVo, self::StoreName );
  23. $this->sFeatureName = _wpsf__('Firewall');
  24. $this->sFeatureSlug = 'firewall';
  25. }
  26. /**
  27. */
  28. public function doPrePluginOptionsSave() {
  29. $aIpWhitelist = $this->getOpt( 'ips_whitelist' );
  30. if ( $aIpWhitelist === false ) {
  31. $aIpWhitelist = '';
  32. $this->setOpt( 'ips_whitelist', $aIpWhitelist );
  33. }
  34. $this->processIpFilter( 'ips_whitelist', 'icwp_simple_firewall_whitelist_ips' );
  35. $aIpBlacklist = $this->getOpt( 'ips_blacklist' );
  36. if ( $aIpBlacklist === false ) {
  37. $aIpBlacklist = '';
  38. $this->setOpt( 'ips_blacklist', $aIpBlacklist );
  39. }
  40. $this->processIpFilter( 'ips_blacklist', 'icwp_simple_firewall_blacklist_ips' );
  41. $aPageWhitelist = $this->getOpt( 'page_params_whitelist' );
  42. if ( $aPageWhitelist === false ) {
  43. $aPageWhitelist = '';
  44. $this->setOpt( 'page_params_whitelist', $aPageWhitelist );
  45. }
  46. $sBlockResponse = $this->getOpt( 'block_response' );
  47. if ( empty( $sBlockResponse ) ) {
  48. $sBlockResponse = 'redirect_die_message';
  49. $aIpWhitelist = $this->setOpt( 'block_response', $sBlockResponse );
  50. }
  51. }
  52. /**
  53. * @return bool|void
  54. */
  55. public function defineOptions() {
  56. $aFirewallBase = array(
  57. 'section_title' => sprintf( _wpsf__( 'Enable Plugin Feature: %s' ), _wpsf__('WordPress Firewall') ),
  58. 'section_options' => array(
  59. array(
  60. 'enable_firewall',
  61. '',
  62. 'N',
  63. 'checkbox',
  64. _wpsf__( 'Enable Firewall' ),
  65. _wpsf__( 'Enable (or Disable) The WordPress Firewall Feature' ),
  66. sprintf( _wpsf__( 'Checking/Un-Checking this option will completely turn on/off the whole %s feature.' ), _wpsf__('WordPress Firewall') ),
  67. '<a href="http://icwp.io/43" target="_blank">'._wpsf__( 'more info' ).'</a>'
  68. .' | <a href="http://icwp.io/wpsf01" target="_blank">'._wpsf__( 'blog' ).'</a>'
  69. )
  70. )
  71. );
  72. $aBlockTypesSection = array(
  73. 'section_title' => _wpsf__( 'Firewall Blocking Options' ),
  74. 'section_options' => array(
  75. array(
  76. 'include_cookie_checks',
  77. '',
  78. 'N',
  79. 'checkbox',
  80. _wpsf__( 'Include Cookies' ),
  81. _wpsf__( 'Also Test Cookie Values In Firewall Tests' ),
  82. _wpsf__( 'The firewall tests GET and POST, but with this option checked it will also COOKIE values.' )
  83. ),
  84. array(
  85. 'block_dir_traversal',
  86. '',
  87. 'Y',
  88. 'checkbox',
  89. _wpsf__( 'Directory Traversals' ),
  90. _wpsf__( 'Block Directory Traversals' ),
  91. _wpsf__( 'This will block directory traversal paths in in application parameters (e.g. ../, ../../etc/passwd, etc.).' )
  92. ),
  93. array(
  94. 'block_sql_queries',
  95. '',
  96. 'Y',
  97. 'checkbox',
  98. _wpsf__( 'SQL Queries' ),
  99. _wpsf__( 'Block SQL Queries' ),
  100. _wpsf__( 'This will block sql in application parameters (e.g. union select, concat(, /**/, etc.).' )
  101. ),
  102. array(
  103. 'block_wordpress_terms',
  104. '',
  105. 'N',
  106. 'checkbox',
  107. _wpsf__( 'WordPress Terms' ),
  108. _wpsf__( 'Block WordPress Specific Terms' ),
  109. _wpsf__( 'This will block WordPress specific terms in application parameters (wp_, user_login, etc.).' )
  110. ),
  111. array(
  112. 'block_field_truncation',
  113. '',
  114. 'Y',
  115. 'checkbox',
  116. _wpsf__( 'Field Truncation' ),
  117. _wpsf__( 'Block Field Truncation Attacks' ),
  118. _wpsf__( 'This will block field truncation attacks in application parameters.' )
  119. ),
  120. array(
  121. 'block_php_code',
  122. '',
  123. 'N',
  124. 'checkbox',
  125. _wpsf__( 'PHP Code' ),
  126. sprintf( _wpsf__( 'Block %s' ), _wpsf__( 'PHP Code Includes' ) ),
  127. _wpsf__( 'This will block any data that appears to try and include PHP files.' )
  128. .'<br />'. _wpsf__( 'Will probably block saving within the Plugin/Theme file editors.' )
  129. ),
  130. array(
  131. 'block_exe_file_uploads',
  132. '',
  133. 'N',
  134. 'checkbox',
  135. _wpsf__( 'Exe File Uploads' ),
  136. _wpsf__( 'Block Executable File Uploads' ),
  137. _wpsf__( 'This will block executable file uploads (.php, .exe, etc.).' )
  138. ),
  139. array(
  140. 'block_leading_schema',
  141. '',
  142. 'N',
  143. 'checkbox',
  144. _wpsf__( 'Leading Schemas' ),
  145. _wpsf__( 'Block Leading Schemas (HTTPS / HTTP)' ),
  146. _wpsf__( 'This will block leading schemas http:// and https:// in application parameters (off by default; may cause problems with other plugins).' )
  147. )
  148. ),
  149. );
  150. $aRedirectOptions = array( 'select',
  151. array( 'redirect_die_message', _wpsf__( 'Die With Message' ) ),
  152. array( 'redirect_die', _wpsf__( 'Die' ) ),
  153. array( 'redirect_home', _wpsf__( 'Redirect To Home Page' ) ),
  154. array( 'redirect_404', _wpsf__( 'Return 404' ) ),
  155. );
  156. $aBlockSection = array(
  157. 'section_title' => _wpsf__( 'Choose Firewall Block Response' ),
  158. 'section_options' => array(
  159. array(
  160. 'block_response',
  161. '',
  162. 'none',
  163. $aRedirectOptions,
  164. _wpsf__( 'Block Response' ),
  165. _wpsf__( 'Choose how the firewall responds when it blocks a request' ),
  166. _wpsf__( 'We recommend dying with a message so you know what might have occurred when the firewall blocks you' )
  167. ),
  168. array(
  169. 'block_send_email',
  170. '',
  171. 'N',
  172. 'checkbox',
  173. _wpsf__( 'Send Email Report' ),
  174. _wpsf__( 'When a visitor is blocked the firewall will send an email to the configured email address' ),
  175. _wpsf__( 'Use with caution - if you get hit by automated bots you may send out too many emails and you could get blocked by your host' )
  176. )
  177. )
  178. );
  179. $aWhitelistSection = array(
  180. 'section_title' => _wpsf__( 'Whitelists - IPs, Pages, Parameters, and Users that by-pass the Firewall' ),
  181. 'section_options' => array(
  182. array(
  183. 'ips_whitelist',
  184. '',
  185. '',
  186. 'ip_addresses',
  187. _wpsf__( 'Whitelist IP Addresses' ),
  188. _wpsf__( 'Choose IP Addresses that are never subjected to Firewall Rules' ),
  189. sprintf( _wpsf__( 'Take a new line per address. Your IP address is: %s' ), '<span class="code">'.$this->getVisitorIpAddress( false ).'</span>' )
  190. ),
  191. array(
  192. 'page_params_whitelist',
  193. '',
  194. '',
  195. 'comma_separated_lists',
  196. _wpsf__( 'Whitelist Parameters' ),
  197. _wpsf__( 'Detail pages and parameters that are whitelisted (ignored by the firewall)' ),
  198. _wpsf__( 'This should be used with caution and you should only provide parameter names that you must have excluded' )
  199. .' '.sprintf( _wpsf__( '%sHelp%s' ), '[<a href="http://icwp.io/2a" target="_blank">', '</a>]' )
  200. ),
  201. array(
  202. 'whitelist_admins',
  203. '',
  204. 'Y',
  205. 'checkbox',
  206. sprintf( _wpsf__( 'Ignore %s' ), _wpsf__( 'Administrators' ) ),
  207. _wpsf__( 'Ignore users logged in as Administrator' ),
  208. _wpsf__( 'Authenticated administrator users will not be processed by the firewall' )
  209. ),
  210. array(
  211. 'ignore_search_engines',
  212. '',
  213. 'N',
  214. 'checkbox',
  215. sprintf( _wpsf__( 'Ignore %s' ), _wpsf__( 'Search Engines' ) ),
  216. _wpsf__( 'Ignore Search Engine Bots' ),
  217. _wpsf__( 'When selected, the firewall will try to recognise search engine spiders/bots and not apply firewall rules to them' )
  218. )
  219. )
  220. );
  221. $aBlacklistSection = array(
  222. 'section_title' => _wpsf__( 'Choose IP Addresses To Blacklist' ),
  223. 'section_options' => array(
  224. array(
  225. 'ips_blacklist',
  226. '',
  227. '',
  228. 'ip_addresses',
  229. _wpsf__( 'Blacklist IP Addresses' ),
  230. _wpsf__( 'Choose IP Addresses that are always blocked from accessing the site' ),
  231. _wpsf__( 'Take a new line per address. Each IP Address must be valid and will be checked' )
  232. )
  233. )
  234. );
  235. $aMisc = array(
  236. 'section_title' => _wpsf__( 'Miscellaneous Plugin Options' ),
  237. 'section_options' => array(
  238. array(
  239. 'enable_firewall_log',
  240. '',
  241. 'N',
  242. 'checkbox',
  243. _wpsf__( 'Firewall Logging' ),
  244. _wpsf__( 'Turn on a detailed Firewall Log' ),
  245. _wpsf__( 'Will log every visit to the site and how the firewall processes it. Not recommended to leave on unless you want to debug something and check the firewall is working as you expect' )
  246. )
  247. )
  248. );
  249. $this->m_aOptions = array(
  250. $aFirewallBase,
  251. $aBlockSection,
  252. $aWhitelistSection,
  253. $aBlacklistSection,
  254. $aBlockTypesSection,
  255. $aMisc
  256. );
  257. }
  258. public function addRawIpsToFirewallList( $insListName, $inaNewIps ) {
  259. if ( empty( $inaNewIps ) ) {
  260. return;
  261. }
  262. $aIplist = $this->getOpt( $insListName );
  263. if ( empty( $aIplist ) ) {
  264. $aIplist = array();
  265. }
  266. $aNewList = array();
  267. foreach( $inaNewIps as $sAddress ) {
  268. $aNewList[ $sAddress ] = '';
  269. }
  270. $this->setOpt( $insListName, ICWP_WPSF_DataProcessor::Add_New_Raw_Ips( $aIplist, $aNewList ) );
  271. }
  272. public function removeRawIpsFromFirewallList( $insListName, $inaRemoveIps ) {
  273. if ( empty( $inaRemoveIps ) ) {
  274. return;
  275. }
  276. $aIplist = $this->getOpt( $insListName );
  277. if ( empty( $aIplist ) || empty( $inaRemoveIps ) ) {
  278. return;
  279. }
  280. $this->setOpt( $insListName, ICWP_WPSF_DataProcessor::Remove_Raw_Ips( $aIplist, $inaRemoveIps ) );
  281. }
  282. }
  283. endif;