| 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 | |
|---|
| 9 | // TODO: move it to bx_process_input |
|---|
| 10 | |
|---|
| 11 | list ($iImpactLog, $iImpactBlock) = bx_sys_security_get_impact_threshold (); |
|---|
| 12 | |
|---|
| 13 | if ((-1 != $iImpactLog || -1 != $iImpactBlock) && !defined('BX_DOL_CRON_EXECUTE')) { |
|---|
| 14 | |
|---|
| 15 | if (version_compare(phpversion(), '5.1.6', '>=')) { |
|---|
| 16 | |
|---|
| 17 | set_include_path ( |
|---|
| 18 | get_include_path() |
|---|
| 19 | . PATH_SEPARATOR |
|---|
| 20 | . BX_DIRECTORY_PATH_PLUGINS . 'phpids/' |
|---|
| 21 | ); |
|---|
| 22 | |
|---|
| 23 | require_once 'IDS/Init.php'; |
|---|
| 24 | $request = array( |
|---|
| 25 | 'GET' => $_GET, |
|---|
| 26 | 'POST' => $_POST, |
|---|
| 27 | 'COOKIE' => $_COOKIE, |
|---|
| 28 | 'PHP_SELF' => $_SERVER['PHP_SELF'], |
|---|
| 29 | ); |
|---|
| 30 | $init = IDS_Init::init(BX_DIRECTORY_PATH_PLUGINS . 'phpids/IDS/Config/Config.ini'); |
|---|
| 31 | $init->config['General']['base_path'] = BX_DIRECTORY_PATH_PLUGINS . 'phpids/IDS/'; |
|---|
| 32 | $init->config['General']['use_base_path'] = true; |
|---|
| 33 | $init->config['General']['tmp_path'] = '../../../tmp/'; |
|---|
| 34 | $init->config['Caching']['path'] = '../../../tmp/default_filter.cache'; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | if (defined('BX_SECURITY_JSON') && is_array($aBxSecurityJSON)) { |
|---|
| 38 | $init->config['General']['json'] = array_merge ($init->config['General']['json'], $aBxSecurityJSON); |
|---|
| 39 | } |
|---|
| 40 | $init->config['General']['json'] = array_merge($init->config['General']['json'], bx_sys_security_get_fields ('json')); |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | if (defined('BX_SECURITY_HTML') && is_array($aBxSecurityHTML)) { |
|---|
| 44 | $init->config['General']['html'] = array_merge ($init->config['General']['html'], $aBxSecurityHTML); |
|---|
| 45 | } |
|---|
| 46 | $init->config['General']['html'] = array_merge($init->config['General']['html'], bx_sys_security_get_fields ('html')); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | if (defined('BX_SECURITY_EXCEPTIONS') && is_array($aBxSecurityExceptions)) { |
|---|
| 50 | $init->config['General']['exceptions'] = array_merge ($init->config['General']['exceptions'], $aBxSecurityExceptions); |
|---|
| 51 | } |
|---|
| 52 | $init->config['General']['exceptions'] = array_merge($init->config['General']['exceptions'], bx_sys_security_get_fields ('exceptions')); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | $init->config['General']['HTML_Purifier_Path'] = BX_DIRECTORY_PATH_PLUGINS . 'htmlpurifier/HTMLPurifier.standalone.php'; |
|---|
| 56 | $init->config['General']['HTML_Purifier_Cache'] = '../../htmlpurifier/standalone/HTMLPurifier/DefinitionCache/Serializer/'; |
|---|
| 57 | |
|---|
| 58 | $ids = new IDS_Monitor($request, $init); |
|---|
| 59 | $result = $ids->run(); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | if (!$result->isEmpty() && $result->getImpact() >= $iImpactLog) { |
|---|
| 63 | |
|---|
| 64 | require_once(BX_DIRECTORY_PATH_INC . 'utils.inc.php'); |
|---|
| 65 | require_once(BX_DIRECTORY_PATH_INC . 'db.inc.php'); |
|---|
| 66 | bx_import('BxDolService'); |
|---|
| 67 | |
|---|
| 68 | $s = (string)$result; |
|---|
| 69 | $s .= "\nREMOTE_ADDR: " . $_SERVER['REMOTE_ADDR']; |
|---|
| 70 | $s .= "\nHTTP_X_FORWARDED_FOR: " . $_SERVER['HTTP_X_FORWARDED_FOR']; |
|---|
| 71 | $s .= "\nHTTP_CLIENT_IP: " . $_SERVER['HTTP_CLIENT_IP']; |
|---|
| 72 | $s .= "\nSCRIPT_FILENAME: " . $_SERVER['SCRIPT_FILENAME']; |
|---|
| 73 | $s .= "\nQUERY_STRING: " . $_SERVER['QUERY_STRING']; |
|---|
| 74 | $s .= "\nREQUEST_URI: " . $_SERVER['REQUEST_URI']; |
|---|
| 75 | $s .= "\nQUERY_STRING: " . $_SERVER['QUERY_STRING']; |
|---|
| 76 | $s .= "\nSCRIPT_NAME: " . $_SERVER['SCRIPT_NAME']; |
|---|
| 77 | $s .= "\nPHP_SELF: " . $_SERVER['PHP_SELF']; |
|---|
| 78 | if ($result->getImpact() >= $iImpactBlock) { |
|---|
| 79 | sendMail(getParam('site_email_bug_report'), BX_DOL_URL_ROOT . ' - security attack was stopped!', $s, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template |
|---|
| 80 | echo 'Possible security attack!!! All data has been collected and sent to the site owner for analysis.'; |
|---|
| 81 | exit; |
|---|
| 82 | } else { |
|---|
| 83 | sendMail(getParam('site_email_bug_report'), BX_DOL_URL_ROOT . ' - possible security attack!', $s, 0, array(), BX_EMAIL_NOTIFY, 'text'); // TODO: email template |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | } else { |
|---|
| 87 | echo 'Site security module is disabled, please upgrade to php 5.1.6 or higher to make your site secure.'; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | function bx_sys_security_get_fields ($sType) { |
|---|
| 92 | |
|---|
| 93 | switch ($sType) { |
|---|
| 94 | case 'html': |
|---|
| 95 | case 'json': |
|---|
| 96 | case 'exceptions': |
|---|
| 97 | break; |
|---|
| 98 | default: |
|---|
| 99 | return array(); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | $sCacheFile = BX_DIRECTORY_PATH_CACHE . 'sys_options_' . md5(BX_DOL_VERSION . BX_DOL_BUILD . BX_DOL_URL_ROOT) . '.php'; |
|---|
| 103 | if (!file_exists($sCacheFile)) { |
|---|
| 104 | require_once( BX_DIRECTORY_PATH_INC . 'utils.inc.php' ); |
|---|
| 105 | require_once( BX_DIRECTORY_PATH_INC . 'db.inc.php' ); |
|---|
| 106 | $mixedVar = getParam("sys_{$sType}_fields"); |
|---|
| 107 | } else { |
|---|
| 108 | include $sCacheFile; |
|---|
| 109 | $mixedVar = $mixedData["sys_{$sType}_fields"]; |
|---|
| 110 | $mixedData = null; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | $mixedVar = unserialize ($mixedVar); |
|---|
| 114 | if (!$mixedVar || !is_array($mixedVar)) |
|---|
| 115 | return array (); |
|---|
| 116 | $a = array (); |
|---|
| 117 | foreach ($mixedVar as $r) |
|---|
| 118 | $a = array_merge ($a, $r); |
|---|
| 119 | |
|---|
| 120 | return $a; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | function bx_sys_security_get_impact_threshold () { |
|---|
| 124 | $sCacheFile = BX_DIRECTORY_PATH_CACHE . 'sys_options_' . md5(BX_DOL_VERSION . BX_DOL_BUILD . BX_DOL_URL_ROOT) . '.php'; |
|---|
| 125 | if (!file_exists($sCacheFile)) { |
|---|
| 126 | require_once( BX_DIRECTORY_PATH_INC . 'utils.inc.php' ); |
|---|
| 127 | require_once( BX_DIRECTORY_PATH_INC . 'db.inc.php' ); |
|---|
| 128 | return array (getParam('sys_security_impact_threshold_log'), getParam('sys_security_impact_threshold_block')); |
|---|
| 129 | } else { |
|---|
| 130 | include $sCacheFile; |
|---|
| 131 | $iThresholdLog = $mixedData['sys_security_impact_threshold_log']; |
|---|
| 132 | $iThresholdBlock = $mixedData['sys_security_impact_threshold_block']; |
|---|
| 133 | $mixedData = null; |
|---|
| 134 | return array ($iThresholdLog, $iThresholdBlock); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|