security.inc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. // TODO: move it to bx_process_input
  9. list ($iImpactLog, $iImpactBlock) = bx_sys_security_get_impact_threshold ();
  10. if ((-1 != $iImpactLog || -1 != $iImpactBlock) && !defined('BX_DOL_CRON_EXECUTE')) {
  11. if (version_compare(phpversion(), '5.1.6', '>=')) {
  12. set_include_path (
  13. get_include_path()
  14. . PATH_SEPARATOR
  15. . BX_DIRECTORY_PATH_PLUGINS . 'phpids/'
  16. );
  17. require_once 'IDS/Init.php';
  18. $request = array(
  19. 'GET' => $_GET,
  20. 'POST' => $_POST,
  21. 'COOKIE' => $_COOKIE,
  22. 'PHP_SELF' => $_SERVER['PHP_SELF'],
  23. );
  24. $init = IDS_Init::init(BX_DIRECTORY_PATH_PLUGINS . 'phpids/IDS/Config/Config.ini');
  25. $init->config['General']['base_path'] = BX_DIRECTORY_PATH_PLUGINS . 'phpids/IDS/';
  26. $init->config['General']['use_base_path'] = true;
  27. $init->config['General']['tmp_path'] = '../../../tmp/';
  28. $init->config['Caching']['path'] = '../../../tmp/default_filter.cache';
  29. if (defined('BX_SECURITY_JSON') && is_array($aBxSecurityJSON)) {
  30. $init->config['General']['json'] = array_merge ($init->config['General']['json'], $aBxSecurityJSON);
  31. }
  32. $init->config['General']['json'] = array_merge($init->config['General']['json'], bx_sys_security_get_fields ('json'));
  33. if (defined('BX_SECURITY_HTML') && is_array($aBxSecurityHTML)) {
  34. $init->config['General']['html'] = array_merge ($init->config['General']['html'], $aBxSecurityHTML);
  35. }
  36. $init->config['General']['html'] = array_merge($init->config['General']['html'], bx_sys_security_get_fields ('html'));
  37. if (defined('BX_SECURITY_EXCEPTIONS') && is_array($aBxSecurityExceptions)) {
  38. $init->config['General']['exceptions'] = array_merge ($init->config['General']['exceptions'], $aBxSecurityExceptions);
  39. }
  40. $init->config['General']['exceptions'] = array_merge($init->config['General']['exceptions'], bx_sys_security_get_fields ('exceptions'));
  41. $init->config['General']['HTML_Purifier_Path'] = BX_DIRECTORY_PATH_PLUGINS . 'htmlpurifier/HTMLPurifier.standalone.php';
  42. $init->config['General']['HTML_Purifier_Cache'] = rtrim(BX_DIRECTORY_PATH_CACHE, '/');
  43. $ids = new IDS_Monitor($request, $init);
  44. $result = $ids->run();
  45. if (!$result->isEmpty() && $result->getImpact() >= $iImpactLog) {
  46. require_once(BX_DIRECTORY_PATH_INC . 'utils.inc.php');
  47. require_once(BX_DIRECTORY_PATH_INC . 'db.inc.php');
  48. bx_import('BxDolService');
  49. $s = (string)$result;
  50. $s .= "\nREMOTE_ADDR: " . $_SERVER['REMOTE_ADDR'];
  51. $s .= "\nHTTP_X_FORWARDED_FOR: " . $_SERVER['HTTP_X_FORWARDED_FOR'];
  52. $s .= "\nHTTP_CLIENT_IP: " . $_SERVER['HTTP_CLIENT_IP'];
  53. $s .= "\nSCRIPT_FILENAME: " . $_SERVER['SCRIPT_FILENAME'];
  54. $s .= "\nQUERY_STRING: " . $_SERVER['QUERY_STRING'];
  55. $s .= "\nREQUEST_URI: " . $_SERVER['REQUEST_URI'];
  56. $s .= "\nQUERY_STRING: " . $_SERVER['QUERY_STRING'];
  57. $s .= "\nSCRIPT_NAME: " . $_SERVER['SCRIPT_NAME'];
  58. $s .= "\nPHP_SELF: " . $_SERVER['PHP_SELF'];
  59. if ($result->getImpact() >= $iImpactBlock) {
  60. sendMail(getParam('site_email_bug_report'), BX_DOL_URL_ROOT . ' - security attack was stopped!', $s, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template
  61. echo 'Possible security attack!!! All data has been collected and sent to the site owner for analysis.';
  62. exit;
  63. } else {
  64. sendMail(getParam('site_email_bug_report'), BX_DOL_URL_ROOT . ' - possible security attack!', $s, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template
  65. }
  66. }
  67. } else {
  68. echo 'Site security module is disabled, please upgrade to php 5.1.6 or higher to make your site secure.';
  69. }
  70. }
  71. function bx_sys_security_get_fields ($sType) {
  72. switch ($sType) {
  73. case 'html':
  74. case 'json':
  75. case 'exceptions':
  76. break;
  77. default:
  78. return array();
  79. }
  80. $sCacheFile = BX_DIRECTORY_PATH_CACHE . 'sys_options_' . bx_site_hash() . '.php';
  81. if (!file_exists($sCacheFile)) {
  82. require_once( BX_DIRECTORY_PATH_INC . 'utils.inc.php' );
  83. require_once( BX_DIRECTORY_PATH_INC . 'db.inc.php' );
  84. $mixedVar = getParam("sys_{$sType}_fields");
  85. } else {
  86. include $sCacheFile;
  87. $mixedVar = $mixedData["sys_{$sType}_fields"];
  88. $mixedData = null;
  89. }
  90. $mixedVar = unserialize ($mixedVar);
  91. if (!$mixedVar || !is_array($mixedVar))
  92. return array ();
  93. $a = array ();
  94. foreach ($mixedVar as $r)
  95. $a = array_merge ($a, $r);
  96. return $a;
  97. }
  98. function bx_sys_security_get_impact_threshold () {
  99. // TODO: remake security to check in bx_process_input function only
  100. $sCacheFile = BX_DIRECTORY_PATH_CACHE . 'sys_options_' . bx_site_hash('', true) . '.php';
  101. if (!file_exists($sCacheFile)) {
  102. require_once( BX_DIRECTORY_PATH_INC . 'utils.inc.php' );
  103. require_once( BX_DIRECTORY_PATH_INC . 'db.inc.php' );
  104. return array (getParam('sys_security_impact_threshold_log'), getParam('sys_security_impact_threshold_block'));
  105. } else {
  106. include $sCacheFile;
  107. $iThresholdLog = $mixedData['sys_security_impact_threshold_log'];
  108. $iThresholdBlock = $mixedData['sys_security_impact_threshold_block'];
  109. $mixedData = null;
  110. return array ($iThresholdLog, $iThresholdBlock);
  111. }
  112. }