| Total Complexity | 345 |
| Total Lines | 1552 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Protector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Protector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Protector |
||
| 7 | { |
||
| 8 | public $mydirname; |
||
| 9 | |||
| 10 | public $_conn; |
||
| 11 | public $_conf = []; |
||
| 12 | public $_conf_serialized = ''; |
||
| 13 | |||
| 14 | public $_bad_globals = []; |
||
| 15 | |||
| 16 | public $message = ''; |
||
| 17 | public $warning = false; |
||
| 18 | public $error = false; |
||
| 19 | public $_doubtful_requests = []; |
||
| 20 | public $_bigumbrella_doubtfuls = []; |
||
| 21 | |||
| 22 | public $_dblayertrap_doubtfuls = []; |
||
| 23 | public $_dblayertrap_doubtful_needles = [ |
||
| 24 | 'information_schema', |
||
| 25 | 'select', |
||
| 26 | "'", |
||
| 27 | '"', |
||
| 28 | ]; |
||
| 29 | |||
| 30 | public $_logged = false; |
||
| 31 | |||
| 32 | public $_done_badext = false; |
||
| 33 | public $_done_intval = false; |
||
| 34 | public $_done_dotdot = false; |
||
| 35 | public $_done_nullbyte = false; |
||
| 36 | public $_done_contami = false; |
||
| 37 | public $_done_isocom = false; |
||
| 38 | public $_done_union = false; |
||
| 39 | public $_done_dos = false; |
||
| 40 | |||
| 41 | public $_safe_badext = true; |
||
| 42 | public $_safe_contami = true; |
||
| 43 | public $_safe_isocom = true; |
||
| 44 | public $_safe_union = true; |
||
| 45 | |||
| 46 | public $_spamcount_uri = 0; |
||
| 47 | |||
| 48 | public $_should_be_banned_time0 = false; |
||
| 49 | public $_should_be_banned = false; |
||
| 50 | |||
| 51 | public $_dos_stage; |
||
| 52 | |||
| 53 | public $ip_matched_info; |
||
| 54 | |||
| 55 | public $last_error_type = 'UNKNOWN'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructor |
||
| 59 | */ |
||
| 60 | protected function __construct() |
||
| 61 | { |
||
| 62 | $this->mydirname = 'protector'; |
||
| 63 | |||
| 64 | // Preferences from configs/cache |
||
| 65 | $this->_conf_serialized = @file_get_contents($this->get_filepath4confighcache()); |
||
| 66 | $this->_conf = @unserialize($this->_conf_serialized, ['allowed_classes' => false]); |
||
|
|
|||
| 67 | if (empty($this->_conf)) { |
||
| 68 | $this->_conf = []; |
||
| 69 | } |
||
| 70 | |||
| 71 | if (!empty($this->_conf['global_disabled'])) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | // die if PHP_SELF XSS found (disabled in 2.53) |
||
| 76 | // if ( preg_match( '/[<>\'";\n ]/' , @$_SERVER['PHP_SELF'] ) ) { |
||
| 77 | // $this->message .= "Invalid PHP_SELF '{$_SERVER['PHP_SELF']}' found.\n" ; |
||
| 78 | // $this->output_log( 'PHP_SELF XSS' ) ; |
||
| 79 | // die( 'invalid PHP_SELF' ) ; |
||
| 80 | // } |
||
| 81 | |||
| 82 | // sanitize against PHP_SELF/PATH_INFO XSS (disabled in 3.33) |
||
| 83 | // $_SERVER['PHP_SELF'] = strtr( @$_SERVER['PHP_SELF'] , array( '<' => '%3C' , '>' => '%3E' , "'" => '%27' , '"' => '%22' ) ) ; |
||
| 84 | // if( ! empty( $_SERVER['PATH_INFO'] ) ) $_SERVER['PATH_INFO'] = strtr( @$_SERVER['PATH_INFO'] , array( '<' => '%3C' , '>' => '%3E' , "'" => '%27' , '"' => '%22' ) ) ; |
||
| 85 | |||
| 86 | $this->_bad_globals = [ |
||
| 87 | 'GLOBALS', |
||
| 88 | '_SESSION', |
||
| 89 | 'HTTP_SESSION_VARS', |
||
| 90 | '_GET', |
||
| 91 | 'HTTP_GET_VARS', |
||
| 92 | '_POST', |
||
| 93 | 'HTTP_POST_VARS', |
||
| 94 | '_COOKIE', |
||
| 95 | 'HTTP_COOKIE_VARS', |
||
| 96 | '_SERVER', |
||
| 97 | 'HTTP_SERVER_VARS', |
||
| 98 | '_REQUEST', |
||
| 99 | '_ENV', |
||
| 100 | '_FILES', |
||
| 101 | 'xoopsDB', |
||
| 102 | 'xoopsUser', |
||
| 103 | 'xoopsUserId', |
||
| 104 | 'xoopsUserGroups', |
||
| 105 | 'xoopsUserIsAdmin', |
||
| 106 | 'xoopsConfig', |
||
| 107 | 'xoopsOption', |
||
| 108 | 'xoopsModule', |
||
| 109 | 'xoopsModuleConfig', |
||
| 110 | ]; |
||
| 111 | |||
| 112 | $this->_initial_recursive($_GET, 'G'); |
||
| 113 | $this->_initial_recursive($_POST, 'P'); |
||
| 114 | $this->_initial_recursive($_COOKIE, 'C'); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param $val |
||
| 119 | * @param $key |
||
| 120 | */ |
||
| 121 | protected function _initial_recursive($val, $key) |
||
| 122 | { |
||
| 123 | if (is_array($val)) { |
||
| 124 | foreach ($val as $subkey => $subval) { |
||
| 125 | // check bad globals |
||
| 126 | if (in_array($subkey, $this->_bad_globals, true)) { |
||
| 127 | $this->message .= "Attempt to inject '$subkey' was found.\n"; |
||
| 128 | $this->_safe_contami = false; |
||
| 129 | $this->last_error_type = 'CONTAMI'; |
||
| 130 | } |
||
| 131 | $this->_initial_recursive($subval, $key . '_' . base64_encode($subkey)); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | // check nullbyte attack |
||
| 135 | if (isset($this->_conf['san_nullbyte']) && $this->_conf['san_nullbyte'] && false !== strpos($val, chr(0))) { |
||
| 136 | $val = str_replace(chr(0), ' ', $val); |
||
| 137 | $this->replace_doubtful($key, $val); |
||
| 138 | $this->message .= "Injecting Null-byte '$val' found.\n"; |
||
| 139 | $this->output_log('NullByte', 0, false, 32); |
||
| 140 | // $this->purge() ; |
||
| 141 | } |
||
| 142 | |||
| 143 | // register as doubtful requests against SQL Injections |
||
| 144 | if (preg_match('?[\s\'"`/]?', $val)) { |
||
| 145 | $this->_doubtful_requests[(string)$key] = $val; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return Protector |
||
| 152 | */ |
||
| 153 | public static function getInstance() |
||
| 154 | { |
||
| 155 | static $instance; |
||
| 156 | if (!isset($instance)) { |
||
| 157 | $instance = new Protector(); |
||
| 158 | } |
||
| 159 | |||
| 160 | return $instance; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function updateConfFromDb() |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param $conn |
||
| 197 | */ |
||
| 198 | public function setConn($conn) |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public function getConf() |
||
| 207 | { |
||
| 208 | return $this->_conf; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param bool $redirect_to_top |
||
| 213 | */ |
||
| 214 | public function purge($redirect_to_top = false) |
||
| 215 | { |
||
| 216 | $this->purgeNoExit(); |
||
| 217 | |||
| 218 | if ($redirect_to_top) { |
||
| 219 | header('Location: ' . XOOPS_URL . '/'); |
||
| 220 | exit; |
||
| 221 | } else { |
||
| 222 | $ret = $this->call_filter('prepurge_exit'); |
||
| 223 | if (false == $ret) { |
||
| 224 | die('Protector detects attacking actions'); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | public function purgeSession() |
||
| 230 | { |
||
| 231 | // clear all session values |
||
| 232 | if (isset($_SESSION)) { |
||
| 233 | foreach ($_SESSION as $key => $val) { |
||
| 234 | $_SESSION[$key] = ''; |
||
| 235 | if (isset($GLOBALS[$key])) { |
||
| 236 | $GLOBALS[$key] = ''; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | public function purgeCookies() |
||
| 243 | { |
||
| 244 | if (!headers_sent()) { |
||
| 245 | $domain = defined(XOOPS_COOKIE_DOMAIN) ? XOOPS_COOKIE_DOMAIN : ''; |
||
| 246 | $past = time() - 3600; |
||
| 247 | foreach ($_COOKIE as $key => $value) { |
||
| 248 | setcookie($key, '', $past, '', $domain); |
||
| 249 | setcookie($key, '', $past, '/', $domain); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | public function purgeNoExit() |
||
| 255 | { |
||
| 256 | $this->purgeSession(); |
||
| 257 | $this->purgeCookies(); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function deactivateCurrentUser() |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $type |
||
| 278 | * @param int $uid |
||
| 279 | * @param bool $unique_check |
||
| 280 | * @param int $level |
||
| 281 | * |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | public function output_log($type = 'UNKNOWN', $uid = 0, $unique_check = false, $level = 1) |
||
| 285 | { |
||
| 286 | if ($this->_logged) { |
||
| 287 | return true; |
||
| 288 | } |
||
| 289 | |||
| 290 | if (!($this->_conf['log_level'] & $level)) { |
||
| 291 | return true; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (empty($this->_conn)) { |
||
| 295 | mysqli_report(MYSQLI_REPORT_OFF); |
||
| 296 | $this->_conn = new mysqli(XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS); |
||
| 297 | if (0 !== $this->_conn->connect_errno) { |
||
| 298 | die('db connection failed.'); |
||
| 299 | } |
||
| 300 | if (!mysqli_select_db($this->_conn, XOOPS_DB_NAME)) { |
||
| 301 | die('db selection failed.'); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | $ip = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
| 306 | $agent = $_SERVER['HTTP_USER_AGENT'] ?? ''; |
||
| 307 | |||
| 308 | |||
| 309 | if ($unique_check) { |
||
| 310 | $result = mysqli_query($this->_conn, 'SELECT ip,type FROM ' . XOOPS_DB_PREFIX . '_' . $this->mydirname . '_log ORDER BY timestamp DESC LIMIT 1'); |
||
| 311 | [$last_ip, $last_type] = mysqli_fetch_row($result); |
||
| 312 | if ($last_ip == $ip && $last_type == $type) { |
||
| 313 | $this->_logged = true; |
||
| 314 | |||
| 315 | return true; |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | mysqli_query( |
||
| 320 | $this->_conn, |
||
| 321 | 'INSERT INTO ' . XOOPS_DB_PREFIX . '_' . $this->mydirname . "_log SET ip='" |
||
| 322 | . mysqli_real_escape_string($this->_conn, $ip) . "',agent='" |
||
| 323 | . mysqli_real_escape_string($this->_conn, $agent) . "',type='" |
||
| 324 | . mysqli_real_escape_string($this->_conn, $type) . "',description='" |
||
| 325 | . mysqli_real_escape_string($this->_conn, $this->message) . "',uid='" |
||
| 326 | . (int) $uid . "',timestamp=NOW()", |
||
| 327 | ); |
||
| 328 | $this->_logged = true; |
||
| 329 | |||
| 330 | return true; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param $expire |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function write_file_bwlimit($expire) |
||
| 339 | { |
||
| 340 | $expire = min((int) $expire, time() + 300); |
||
| 341 | |||
| 342 | $fp = @fopen(static::get_filepath4bwlimit(), 'w'); |
||
| 343 | if ($fp) { |
||
| 344 | @flock($fp, LOCK_EX); |
||
| 345 | fwrite($fp, $expire . "\n"); |
||
| 346 | @flock($fp, LOCK_UN); |
||
| 347 | fclose($fp); |
||
| 348 | |||
| 349 | return true; |
||
| 350 | } else { |
||
| 351 | return false; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | public function get_bwlimit() |
||
| 359 | { |
||
| 360 | [$expire] = @file(Protector::get_filepath4bwlimit()); |
||
| 361 | $expire = min((int) $expire, time() + 300); |
||
| 362 | |||
| 363 | return $expire; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public static function get_filepath4bwlimit() |
||
| 370 | { |
||
| 371 | return XOOPS_VAR_PATH . '/protector/bwlimit' . substr(md5(XOOPS_ROOT_PATH . XOOPS_DB_USER . XOOPS_DB_PREFIX), 0, 6); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param $bad_ips |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | public function write_file_badips($bad_ips) |
||
| 380 | { |
||
| 381 | asort($bad_ips); |
||
| 382 | |||
| 383 | $fp = @fopen(static::get_filepath4badips(), 'w'); |
||
| 384 | if ($fp) { |
||
| 385 | @flock($fp, LOCK_EX); |
||
| 386 | fwrite($fp, serialize($bad_ips) . "\n"); |
||
| 387 | @flock($fp, LOCK_UN); |
||
| 388 | fclose($fp); |
||
| 389 | |||
| 390 | return true; |
||
| 391 | } else { |
||
| 392 | return false; |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param int $jailed_time |
||
| 398 | * @param null|string|false $ip |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | public function register_bad_ips($jailed_time = 0, $ip = null) |
||
| 403 | { |
||
| 404 | if (empty($ip)) { |
||
| 405 | $ip = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
| 406 | } |
||
| 407 | if (empty($ip)) { |
||
| 408 | return false; |
||
| 409 | } |
||
| 410 | |||
| 411 | $bad_ips = $this->get_bad_ips(true); |
||
| 412 | $bad_ips[$ip] = $jailed_time ?: 0x7fffffff; |
||
| 413 | |||
| 414 | return $this->write_file_badips($bad_ips); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param bool $with_jailed_time |
||
| 419 | * |
||
| 420 | * @return array|mixed |
||
| 421 | */ |
||
| 422 | public function get_bad_ips($with_jailed_time = false) |
||
| 423 | { |
||
| 424 | // list($bad_ips_serialized) = @file(Protector::get_filepath4badips()); |
||
| 425 | $filepath4badips = @file(Protector::get_filepath4badips()); |
||
| 426 | |||
| 427 | if (is_array($filepath4badips) && isset($filepath4badips[0])) { |
||
| 428 | [$bad_ips_serialized] = $filepath4badips; |
||
| 429 | } |
||
| 430 | $bad_ips = empty($bad_ips_serialized) ? [] : @unserialize($bad_ips_serialized, ['allowed_classes' => false]); |
||
| 431 | if (!is_array($bad_ips) || isset($bad_ips[0])) { |
||
| 432 | $bad_ips = []; |
||
| 433 | } |
||
| 434 | |||
| 435 | // expire jailed_time |
||
| 436 | $pos = 0; |
||
| 437 | foreach ($bad_ips as $bad_ip => $jailed_time) { |
||
| 438 | if ($jailed_time >= time()) { |
||
| 439 | break; |
||
| 440 | } |
||
| 441 | ++$pos; |
||
| 442 | } |
||
| 443 | $bad_ips = array_slice($bad_ips, $pos); |
||
| 444 | |||
| 445 | if ($with_jailed_time) { |
||
| 446 | return $bad_ips; |
||
| 447 | } else { |
||
| 448 | return array_keys($bad_ips); |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public static function get_filepath4badips() |
||
| 456 | { |
||
| 457 | return XOOPS_VAR_PATH . '/protector/badips' . substr(md5(XOOPS_ROOT_PATH . XOOPS_DB_USER . XOOPS_DB_PREFIX), 0, 6); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param bool $with_info |
||
| 462 | * |
||
| 463 | * @return array|mixed |
||
| 464 | */ |
||
| 465 | public function get_group1_ips($with_info = false) |
||
| 466 | { |
||
| 467 | // list($group1_ips_serialized) = @file(Protector::get_filepath4group1ips()); |
||
| 468 | $group1_ips = []; |
||
| 469 | // Check if the file exists before attempting to read it |
||
| 470 | $filepath = Protector::get_filepath4group1ips(); |
||
| 471 | if (file_exists($filepath)) { |
||
| 472 | $filepath4group1ips = file($filepath); |
||
| 473 | if (false === $filepath4group1ips) { |
||
| 474 | // Handle the error condition when file reading fails |
||
| 475 | } else { |
||
| 476 | // Proceed with your logic here |
||
| 477 | if (is_array($filepath4group1ips) && isset($filepath4group1ips[0])) { |
||
| 478 | [$group1_ips_serialized] = $filepath4group1ips; |
||
| 479 | } |
||
| 480 | |||
| 481 | $group1_ips = empty($group1_ips_serialized) ? [] : @unserialize($group1_ips_serialized, ['allowed_classes' => false]); |
||
| 482 | if (!is_array($group1_ips)) { |
||
| 483 | $group1_ips = []; |
||
| 484 | } |
||
| 485 | |||
| 486 | if ($with_info) { |
||
| 487 | $group1_ips = array_flip($group1_ips); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } else { |
||
| 491 | // File does not exist; handle this condition |
||
| 492 | } |
||
| 493 | |||
| 494 | return $group1_ips; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public static function get_filepath4group1ips() |
||
| 501 | { |
||
| 502 | return XOOPS_VAR_PATH . '/protector/group1ips' . substr(md5(XOOPS_ROOT_PATH . XOOPS_DB_USER . XOOPS_DB_PREFIX), 0, 6); |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function get_filepath4confighcache() |
||
| 509 | { |
||
| 510 | return XOOPS_VAR_PATH . '/protector/configcache' . substr(md5(XOOPS_ROOT_PATH . XOOPS_DB_USER . XOOPS_DB_PREFIX), 0, 6); |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param $ips |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | public function ip_match($ips) |
||
| 519 | { |
||
| 520 | $requestIp = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
| 521 | if (false === $requestIp) { // nothing to match |
||
| 522 | $this->ip_matched_info = null; |
||
| 523 | return false; |
||
| 524 | } |
||
| 525 | foreach ($ips as $ip => $info) { |
||
| 526 | if ($ip) { |
||
| 527 | switch (strtolower(substr($ip, -1))) { |
||
| 528 | case '.': |
||
| 529 | case ':': |
||
| 530 | // forward match |
||
| 531 | if (substr($requestIp, 0, strlen($ip)) == $ip) { |
||
| 532 | $this->ip_matched_info = $info; |
||
| 533 | return true; |
||
| 534 | } |
||
| 535 | break; |
||
| 536 | case '0': |
||
| 537 | case '1': |
||
| 538 | case '2': |
||
| 539 | case '3': |
||
| 540 | case '4': |
||
| 541 | case '5': |
||
| 542 | case '6': |
||
| 543 | case '7': |
||
| 544 | case '8': |
||
| 545 | case '9': |
||
| 546 | case 'a': |
||
| 547 | case 'b': |
||
| 548 | case 'c': |
||
| 549 | case 'd': |
||
| 550 | case 'e': |
||
| 551 | case 'f': |
||
| 552 | // full match |
||
| 553 | if ($requestIp == $ip) { |
||
| 554 | $this->ip_matched_info = $info; |
||
| 555 | return true; |
||
| 556 | } |
||
| 557 | break; |
||
| 558 | default: |
||
| 559 | // perl regex |
||
| 560 | if (@preg_match($ip, $requestIp)) { |
||
| 561 | $this->ip_matched_info = $info; |
||
| 562 | return true; |
||
| 563 | } |
||
| 564 | break; |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } |
||
| 568 | $this->ip_matched_info = null; |
||
| 569 | return false; |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param null|string|false $ip |
||
| 574 | * |
||
| 575 | * @return bool |
||
| 576 | */ |
||
| 577 | public function deny_by_htaccess($ip = null) |
||
| 578 | { |
||
| 579 | if (empty($ip)) { |
||
| 580 | $ip = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
| 581 | } |
||
| 582 | if (empty($ip)) { |
||
| 583 | return false; |
||
| 584 | } |
||
| 585 | if (!function_exists('file_get_contents')) { |
||
| 586 | return false; |
||
| 587 | } |
||
| 588 | |||
| 589 | $target_htaccess = XOOPS_ROOT_PATH . '/.htaccess'; |
||
| 590 | $backup_htaccess = XOOPS_ROOT_PATH . '/uploads/.htaccess.bak'; |
||
| 591 | |||
| 592 | $ht_body = file_get_contents($target_htaccess); |
||
| 593 | |||
| 594 | // make backup as uploads/.htaccess.bak automatically |
||
| 595 | if ($ht_body && !file_exists($backup_htaccess)) { |
||
| 596 | $fw = fopen($backup_htaccess, 'w'); |
||
| 597 | fwrite($fw, $ht_body); |
||
| 598 | fclose($fw); |
||
| 599 | } |
||
| 600 | |||
| 601 | // if .htaccess is broken, restore from backup |
||
| 602 | if (!$ht_body && file_exists($backup_htaccess)) { |
||
| 603 | $ht_body = file_get_contents($backup_htaccess); |
||
| 604 | } |
||
| 605 | |||
| 606 | // new .htaccess |
||
| 607 | if (false === $ht_body) { |
||
| 608 | $ht_body = ''; |
||
| 609 | } |
||
| 610 | |||
| 611 | if (preg_match("/^(.*)#PROTECTOR#\s+(DENY FROM .*)\n#PROTECTOR#\n(.*)$/si", $ht_body, $regs)) { |
||
| 612 | if (substr($regs[2], -strlen($ip)) == $ip) { |
||
| 613 | return true; |
||
| 614 | } |
||
| 615 | $new_ht_body = $regs[1] . "#PROTECTOR#\n" . $regs[2] . " $ip\n#PROTECTOR#\n" . $regs[3]; |
||
| 616 | } else { |
||
| 617 | $new_ht_body = "#PROTECTOR#\nDENY FROM $ip\n#PROTECTOR#\n" . $ht_body; |
||
| 618 | } |
||
| 619 | |||
| 620 | // error_log( "$new_ht_body\n" , 3 , "/tmp/error_log" ) ; |
||
| 621 | |||
| 622 | $fw = fopen($target_htaccess, 'w'); |
||
| 623 | @flock($fw, LOCK_EX); |
||
| 624 | fwrite($fw, $new_ht_body); |
||
| 625 | @flock($fw, LOCK_UN); |
||
| 626 | fclose($fw); |
||
| 627 | |||
| 628 | return true; |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | public function getDblayertrapDoubtfuls() |
||
| 635 | { |
||
| 636 | return $this->_dblayertrap_doubtfuls; |
||
| 637 | } |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @param $val |
||
| 641 | * @return null |
||
| 642 | */ |
||
| 643 | protected function _dblayertrap_check_recursive($val) |
||
| 644 | { |
||
| 645 | if (is_array($val)) { |
||
| 646 | foreach ($val as $subval) { |
||
| 647 | $this->_dblayertrap_check_recursive($subval); |
||
| 648 | } |
||
| 649 | } else { |
||
| 650 | if (strlen($val) < 6) { |
||
| 651 | return null; |
||
| 652 | } |
||
| 653 | foreach ($this->_dblayertrap_doubtful_needles as $needle) { |
||
| 654 | if (false !== stripos($val, (string) $needle)) { |
||
| 655 | $this->_dblayertrap_doubtfuls[] = $val; |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param bool $force_override |
||
| 663 | * @return null |
||
| 664 | */ |
||
| 665 | public function dblayertrap_init($force_override = false) |
||
| 666 | { |
||
| 667 | if (!empty($GLOBALS['xoopsOption']['nocommon']) || defined('_LEGACY_PREVENT_EXEC_COMMON_') || defined('_LEGACY_PREVENT_LOAD_CORE_')) { |
||
| 668 | return null; |
||
| 669 | } // skip |
||
| 670 | |||
| 671 | $this->_dblayertrap_doubtfuls = []; |
||
| 672 | $this->_dblayertrap_check_recursive($_GET); |
||
| 673 | $this->_dblayertrap_check_recursive($_POST); |
||
| 674 | $this->_dblayertrap_check_recursive($_COOKIE); |
||
| 675 | if (empty($this->_conf['dblayertrap_wo_server'])) { |
||
| 676 | $this->_dblayertrap_check_recursive($_SERVER); |
||
| 677 | } |
||
| 678 | |||
| 679 | if (!empty($this->_dblayertrap_doubtfuls) || $force_override) { |
||
| 680 | @define('XOOPS_DB_ALTERNATIVE', 'ProtectorMysqlDatabase'); |
||
| 681 | require_once dirname(__DIR__) . '/class/ProtectorMysqlDatabase.class.php'; |
||
| 682 | } |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param $val |
||
| 687 | */ |
||
| 688 | protected function _bigumbrella_check_recursive($val) |
||
| 689 | { |
||
| 690 | if (is_array($val)) { |
||
| 691 | foreach ($val as $subval) { |
||
| 692 | $this->_bigumbrella_check_recursive($subval); |
||
| 693 | } |
||
| 694 | } else { |
||
| 695 | if (preg_match('/[<\'"].{15}/s', $val, $regs)) { |
||
| 696 | $this->_bigumbrella_doubtfuls[] = $regs[0]; |
||
| 697 | } |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | public function bigumbrella_init() |
||
| 702 | { |
||
| 703 | $this->_bigumbrella_doubtfuls = []; |
||
| 704 | $this->_bigumbrella_check_recursive($_GET); |
||
| 705 | $this->_bigumbrella_check_recursive($_SERVER['PHP_SELF'] ?? ''); |
||
| 706 | |||
| 707 | if (!empty($this->_bigumbrella_doubtfuls)) { |
||
| 708 | ob_start([$this, 'bigumbrella_outputcheck']); |
||
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @param $s |
||
| 714 | * |
||
| 715 | * @return string |
||
| 716 | */ |
||
| 717 | public function bigumbrella_outputcheck($s) |
||
| 718 | { |
||
| 719 | if (defined('BIGUMBRELLA_DISABLED')) { |
||
| 720 | return $s; |
||
| 721 | } |
||
| 722 | |||
| 723 | if (function_exists('headers_list')) { |
||
| 724 | foreach (headers_list() as $header) { |
||
| 725 | if (false !== stripos($header, 'Content-Type:') && false === stripos($header, 'text/html')) { |
||
| 726 | return $s; |
||
| 727 | } |
||
| 728 | } |
||
| 729 | } |
||
| 730 | |||
| 731 | if (!is_array($this->_bigumbrella_doubtfuls)) { |
||
| 732 | return 'bigumbrella injection found.'; |
||
| 733 | } |
||
| 734 | |||
| 735 | foreach ($this->_bigumbrella_doubtfuls as $doubtful) { |
||
| 736 | if (false !== strpos($s, (string) $doubtful)) { |
||
| 737 | return 'XSS found by Protector.'; |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | return $s; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @return bool |
||
| 746 | */ |
||
| 747 | public function intval_allrequestsendid() |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | public function eliminate_dotdot() |
||
| 792 | { |
||
| 793 | global $_GET, $_POST, $_COOKIE; |
||
| 794 | |||
| 795 | if ($this->_done_dotdot) { |
||
| 796 | return true; |
||
| 797 | } else { |
||
| 798 | $this->_done_dotdot = true; |
||
| 799 | } |
||
| 800 | |||
| 801 | foreach ($_GET as $key => $val) { |
||
| 802 | if (is_array($_GET[$key])) { |
||
| 803 | continue; |
||
| 804 | } |
||
| 805 | if ('../' === substr(trim($val), 0, 3) || false !== strpos($val, '/../')) { |
||
| 806 | $this->last_error_type = 'DirTraversal'; |
||
| 807 | $this->message .= "Directory Traversal '$val' found.\n"; |
||
| 808 | $this->output_log($this->last_error_type, 0, false, 64); |
||
| 809 | $sanitized_val = str_replace(chr(0), '', $val); |
||
| 810 | if (' .' !== substr($sanitized_val, -2)) { |
||
| 811 | $sanitized_val .= ' .'; |
||
| 812 | } |
||
| 813 | $_GET[$key] = $sanitized_val; |
||
| 814 | if ($_REQUEST[$key] == $_GET[$key]) { |
||
| 815 | $_REQUEST[$key] = $sanitized_val; |
||
| 816 | } |
||
| 817 | } |
||
| 818 | } |
||
| 819 | |||
| 820 | /* foreach ($_POST as $key => $val) { |
||
| 821 | if( is_array( $_POST[ $key ] ) ) continue ; |
||
| 822 | if ( substr( trim( $val ) , 0 , 3 ) == '../' || false !== strpos( $val , '../../' ) ) { |
||
| 823 | $this->last_error_type = 'ParentDir' ; |
||
| 824 | $this->message .= "Doubtful file specification '$val' found.\n" ; |
||
| 825 | $this->output_log( $this->last_error_type , 0 , false , 128 ) ; |
||
| 826 | $sanitized_val = str_replace( chr(0) , '' , $val ) ; |
||
| 827 | if( substr( $sanitized_val , -2 ) != ' .' ) $sanitized_val .= ' .' ; |
||
| 828 | $_POST[ $key ] = $HTTP_POST_VARS[ $key ] = $sanitized_val ; |
||
| 829 | if ($_REQUEST[ $key ] == $_POST[ $key ]) { |
||
| 830 | $_REQUEST[ $key ] = $sanitized_val ; |
||
| 831 | } |
||
| 832 | } |
||
| 833 | } |
||
| 834 | foreach ($_COOKIE as $key => $val) { |
||
| 835 | if( is_array( $_COOKIE[ $key ] ) ) continue ; |
||
| 836 | if ( substr( trim( $val ) , 0 , 3 ) == '../' || false !== strpos( $val , '../../' ) ) { |
||
| 837 | $this->last_error_type = 'ParentDir' ; |
||
| 838 | $this->message .= "Doubtful file specification '$val' found.\n" ; |
||
| 839 | $this->output_log( $this->last_error_type , 0 , false , 128 ) ; |
||
| 840 | $sanitized_val = str_replace( chr(0) , '' , $val ) ; |
||
| 841 | if( substr( $sanitized_val , -2 ) != ' .' ) $sanitized_val .= ' .' ; |
||
| 842 | $_COOKIE[ $key ] = $HTTP_COOKIE_VARS[ $key ] = $sanitized_val ; |
||
| 843 | if ($_REQUEST[ $key ] == $_COOKIE[ $key ]) { |
||
| 844 | $_REQUEST[ $key ] = $sanitized_val ; |
||
| 845 | } |
||
| 846 | } |
||
| 847 | }*/ |
||
| 848 | |||
| 849 | return true; |
||
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * @param $current |
||
| 854 | * @param $indexes |
||
| 855 | * |
||
| 856 | * @return bool |
||
| 857 | */ |
||
| 858 | public function get_ref_from_base64index(&$current, $indexes) |
||
| 859 | { |
||
| 860 | foreach ($indexes as $index) { |
||
| 861 | $index = base64_decode($index); |
||
| 862 | if (!is_array($current)) { |
||
| 863 | return false; |
||
| 864 | } |
||
| 865 | $current = & $current[$index]; |
||
| 866 | } |
||
| 867 | |||
| 868 | return $current; |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @param $key |
||
| 873 | * @param $val |
||
| 874 | */ |
||
| 875 | public function replace_doubtful($key, $val) |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @return bool |
||
| 912 | */ |
||
| 913 | public function check_uploaded_files() |
||
| 914 | { |
||
| 915 | if ($this->_done_badext) { |
||
| 916 | return $this->_safe_badext; |
||
| 917 | } else { |
||
| 918 | $this->_done_badext = true; |
||
| 919 | } |
||
| 920 | |||
| 921 | // extensions never uploaded |
||
| 922 | $bad_extensions = ['php', 'phtml', 'phtm', 'php3', 'php4', 'cgi', 'pl', 'asp']; |
||
| 923 | // extensions needed image check (anti-IE Content-Type XSS) |
||
| 924 | $image_extensions = [ |
||
| 925 | 1 => 'gif', |
||
| 926 | 2 => 'jpg', |
||
| 927 | 3 => 'png', |
||
| 928 | 4 => 'swf', |
||
| 929 | 5 => 'psd', |
||
| 930 | 6 => 'bmp', |
||
| 931 | 7 => 'tif', |
||
| 932 | 8 => 'tif', |
||
| 933 | 9 => 'jpc', |
||
| 934 | 10 => 'jp2', |
||
| 935 | 11 => 'jpx', |
||
| 936 | 12 => 'jb2', |
||
| 937 | 13 => 'swc', |
||
| 938 | 14 => 'iff', |
||
| 939 | 15 => 'wbmp', |
||
| 940 | 16 => 'xbm', |
||
| 941 | ]; |
||
| 942 | |||
| 943 | foreach ($_FILES as $_file) { |
||
| 944 | if (!empty($_file['error'])) { |
||
| 945 | continue; |
||
| 946 | } |
||
| 947 | if (!empty($_file['name']) && is_string($_file['name'])) { |
||
| 948 | $ext = strtolower(substr(strrchr($_file['name'], '.'), 1)); |
||
| 949 | if ('jpeg' === $ext) { |
||
| 950 | $ext = 'jpg'; |
||
| 951 | } elseif ('tiff' === $ext) { |
||
| 952 | $ext = 'tif'; |
||
| 953 | } |
||
| 954 | |||
| 955 | // anti multiple dot file (Apache mod_mime.c) |
||
| 956 | if (count(explode('.', str_replace('.tar.gz', '.tgz', $_file['name']))) > 2) { |
||
| 957 | $this->message .= "Attempt to multiple dot file {$_file['name']}.\n"; |
||
| 958 | $this->_safe_badext = false; |
||
| 959 | $this->last_error_type = 'UPLOAD'; |
||
| 960 | } |
||
| 961 | |||
| 962 | // anti dangerous extensions |
||
| 963 | if (in_array($ext, $bad_extensions)) { |
||
| 964 | $this->message .= "Attempt to upload {$_file['name']}.\n"; |
||
| 965 | $this->_safe_badext = false; |
||
| 966 | $this->last_error_type = 'UPLOAD'; |
||
| 967 | } |
||
| 968 | |||
| 969 | // anti camouflaged image file |
||
| 970 | if (in_array($ext, $image_extensions)) { |
||
| 971 | $image_attributes = @getimagesize($_file['tmp_name']); |
||
| 972 | if (false === $image_attributes && is_uploaded_file($_file['tmp_name'])) { |
||
| 973 | // open_basedir restriction |
||
| 974 | $temp_file = XOOPS_ROOT_PATH . '/uploads/protector_upload_temporary' . md5(time()); |
||
| 975 | move_uploaded_file($_file['tmp_name'], $temp_file); |
||
| 976 | $image_attributes = @getimagesize($temp_file); |
||
| 977 | @unlink($temp_file); |
||
| 978 | } |
||
| 979 | |||
| 980 | if (false === $image_attributes || $image_extensions[(int)$image_attributes[2]] != $ext) { |
||
| 981 | $this->message .= "Attempt to upload camouflaged image file {$_file['name']}.\n"; |
||
| 982 | $this->_safe_badext = false; |
||
| 983 | $this->last_error_type = 'UPLOAD'; |
||
| 984 | } |
||
| 985 | } |
||
| 986 | } |
||
| 987 | } |
||
| 988 | |||
| 989 | return $this->_safe_badext; |
||
| 990 | } |
||
| 991 | |||
| 992 | /** |
||
| 993 | * @return bool |
||
| 994 | */ |
||
| 995 | public function check_contami_systemglobals() |
||
| 996 | { |
||
| 997 | /* if( $this->_done_contami ) return $this->_safe_contami ; |
||
| 998 | else $this->_done_contami = true ; */ |
||
| 999 | |||
| 1000 | /* foreach ($this->_bad_globals as $bad_global) { |
||
| 1001 | if ( isset( $_REQUEST[ $bad_global ] ) ) { |
||
| 1002 | $this->message .= "Attempt to inject '$bad_global' was found.\n" ; |
||
| 1003 | $this->_safe_contami = false ; |
||
| 1004 | $this->last_error_type = 'CONTAMI' ; |
||
| 1005 | } |
||
| 1006 | }*/ |
||
| 1007 | |||
| 1008 | return $this->_safe_contami; |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * @param bool $sanitize |
||
| 1013 | * |
||
| 1014 | * @return bool |
||
| 1015 | */ |
||
| 1016 | public function check_sql_isolatedcommentin($sanitize = true) |
||
| 1017 | { |
||
| 1018 | if ($this->_done_isocom) { |
||
| 1019 | return $this->_safe_isocom; |
||
| 1020 | } else { |
||
| 1021 | $this->_done_isocom = true; |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | foreach ($this->_doubtful_requests as $key => $val) { |
||
| 1025 | $str = $val; |
||
| 1026 | while ($str = strstr($str, '/*')) { /* */ |
||
| 1027 | $str = strstr(substr($str, 2), '*/'); |
||
| 1028 | if (false === $str) { |
||
| 1029 | $this->message .= "Isolated comment-in found. ($val)\n"; |
||
| 1030 | if ($sanitize) { |
||
| 1031 | $this->replace_doubtful($key, $val . '*/'); |
||
| 1032 | } |
||
| 1033 | $this->_safe_isocom = false; |
||
| 1034 | $this->last_error_type = 'ISOCOM'; |
||
| 1035 | } |
||
| 1036 | } |
||
| 1037 | } |
||
| 1038 | |||
| 1039 | return $this->_safe_isocom; |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * @param bool $sanitize |
||
| 1044 | * |
||
| 1045 | * @return bool |
||
| 1046 | */ |
||
| 1047 | public function check_sql_union($sanitize = true) |
||
| 1048 | { |
||
| 1049 | if ($this->_done_union) { |
||
| 1050 | return $this->_safe_union; |
||
| 1051 | } else { |
||
| 1052 | $this->_done_union = true; |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | foreach ($this->_doubtful_requests as $key => $val) { |
||
| 1056 | $str = str_replace(['/*', '*/'], '', preg_replace('?/\*.+\*/?sU', '', $val)); |
||
| 1057 | if (preg_match('/\sUNION\s+(ALL|SELECT)/i', $str)) { |
||
| 1058 | $this->message .= "Pattern like SQL injection found. ($val)\n"; |
||
| 1059 | if ($sanitize) { |
||
| 1060 | // $this->replace_doubtful($key, preg_replace('/union/i', 'uni-on', $val)); |
||
| 1061 | $this->replace_doubtful($key, str_ireplace('union', 'uni-on', $val)); |
||
| 1062 | } |
||
| 1063 | $this->_safe_union = false; |
||
| 1064 | $this->last_error_type = 'UNION'; |
||
| 1065 | } |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | return $this->_safe_union; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | /** |
||
| 1072 | * @param $uid |
||
| 1073 | * |
||
| 1074 | * @return bool |
||
| 1075 | */ |
||
| 1076 | public function stopforumspam($uid) |
||
| 1077 | { |
||
| 1078 | if ('POST' !== $_SERVER['REQUEST_METHOD']) { |
||
| 1079 | return false; |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | $result = $this->stopForumSpamLookup( |
||
| 1083 | $_POST['email'] ?? null, |
||
| 1084 | $_SERVER['REMOTE_ADDR'], |
||
| 1085 | $_POST['uname'] ?? null, |
||
| 1086 | ); |
||
| 1087 | |||
| 1088 | if (false === $result || isset($result['http_code'])) { |
||
| 1089 | return false; |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | $spammer = false; |
||
| 1093 | if (isset($result['email']) && isset($result['email']['lastseen'])) { |
||
| 1094 | $spammer = true; |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | if (isset($result['ip']) && isset($result['ip']['lastseen'])) { |
||
| 1098 | $last = strtotime($result['ip']['lastseen']); |
||
| 1099 | $oneMonth = 60 * 60 * 24 * 31; |
||
| 1100 | $oneMonthAgo = time() - $oneMonth; |
||
| 1101 | if ($last > $oneMonthAgo) { |
||
| 1102 | $spammer = true; |
||
| 1103 | } |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | if (!$spammer) { |
||
| 1107 | return false; |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | $this->last_error_type = 'SPAMMER POST'; |
||
| 1111 | |||
| 1112 | switch ($this->_conf['stopforumspam_action']) { |
||
| 1113 | default: |
||
| 1114 | case 'log': |
||
| 1115 | break; |
||
| 1116 | case 'san': |
||
| 1117 | $_POST = []; |
||
| 1118 | $this->message .= 'POST deleted for IP:' . $_SERVER['REMOTE_ADDR']; |
||
| 1119 | break; |
||
| 1120 | case 'biptime0': |
||
| 1121 | $_POST = []; |
||
| 1122 | $this->message .= 'BAN and POST deleted for IP:' . $_SERVER['REMOTE_ADDR']; |
||
| 1123 | $this->_should_be_banned_time0 = true; |
||
| 1124 | break; |
||
| 1125 | case 'bip': |
||
| 1126 | $_POST = []; |
||
| 1127 | $this->message .= 'Ban and POST deleted for IP:' . $_SERVER['REMOTE_ADDR']; |
||
| 1128 | $this->_should_be_banned = true; |
||
| 1129 | break; |
||
| 1130 | } |
||
| 1131 | |||
| 1132 | $this->output_log($this->last_error_type, $uid, false, 16); |
||
| 1133 | |||
| 1134 | return true; |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | public function stopForumSpamLookup($email, $ip, $username) |
||
| 1138 | { |
||
| 1139 | if (!function_exists('curl_init')) { |
||
| 1140 | return false; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | $query = ''; |
||
| 1144 | $query .= (empty($ip)) ? '' : '&ip=' . $ip; |
||
| 1145 | $query .= (empty($email)) ? '' : '&email=' . $email; |
||
| 1146 | $query .= (empty($username)) ? '' : '&username=' . $username; |
||
| 1147 | |||
| 1148 | if (empty($query)) { |
||
| 1149 | return false; |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | $url = 'http://www.stopforumspam.com/api?f=json' . $query; |
||
| 1153 | $ch = curl_init(); |
||
| 1154 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 1155 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
| 1156 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
||
| 1157 | $result = curl_exec($ch); |
||
| 1158 | if (false === $result) { |
||
| 1159 | $result = curl_getinfo($ch); |
||
| 1160 | } else { |
||
| 1161 | $result = json_decode(curl_exec($ch), true); |
||
| 1162 | } |
||
| 1163 | curl_close($ch); |
||
| 1164 | |||
| 1165 | return $result; |
||
| 1166 | } |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * @param int $uid |
||
| 1170 | * @param bool $can_ban |
||
| 1171 | * |
||
| 1172 | * @return bool |
||
| 1173 | */ |
||
| 1174 | public function check_dos_attack($uid = 0, $can_ban = false) |
||
| 1175 | { |
||
| 1176 | global $xoopsDB; |
||
| 1177 | |||
| 1178 | if ($this->_done_dos) { |
||
| 1179 | return true; |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | $ip = \Xmf\IPAddress::fromRequest(); |
||
| 1183 | if (false === $ip->asReadable()) { |
||
| 1184 | return true; |
||
| 1185 | } |
||
| 1186 | $uri = $_SERVER['REQUEST_URI'] ?? ''; |
||
| 1187 | |||
| 1188 | $ip4sql = $xoopsDB->quote($ip->asReadable()); |
||
| 1189 | $uri4sql = $xoopsDB->quote($uri); |
||
| 1190 | |||
| 1191 | // gargage collection |
||
| 1192 | $result = $xoopsDB->exec( |
||
| 1193 | 'DELETE FROM ' . $xoopsDB->prefix($this->mydirname . '_access') |
||
| 1194 | . ' WHERE expire < UNIX_TIMESTAMP()', |
||
| 1195 | ); |
||
| 1196 | |||
| 1197 | // for older versions before updating this module |
||
| 1198 | if (false === $result) { |
||
| 1199 | $this->_done_dos = true; |
||
| 1200 | |||
| 1201 | return true; |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | // sql for recording access log (INSERT should be placed after SELECT) |
||
| 1205 | $sql4insertlog = 'INSERT INTO ' . $xoopsDB->prefix($this->mydirname . '_access') |
||
| 1206 | . " SET ip={$ip4sql}, request_uri={$uri4sql}," |
||
| 1207 | . " expire=UNIX_TIMESTAMP()+'" . (int) $this->_conf['dos_expire'] . "'"; |
||
| 1208 | |||
| 1209 | // bandwidth limitation |
||
| 1210 | if (isset($this->_conf['bwlimit_count']) && $this->_conf['bwlimit_count'] >= 10) { |
||
| 1211 | $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix($this->mydirname . '_access'); |
||
| 1212 | $result = $xoopsDB->query($sql); |
||
| 1213 | if ($xoopsDB->isResultSet($result)) { |
||
| 1214 | [$bw_count] = $xoopsDB->fetchRow($result); |
||
| 1215 | if ($bw_count > $this->_conf['bwlimit_count']) { |
||
| 1216 | $this->write_file_bwlimit(time() + $this->_conf['dos_expire']); |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | } |
||
| 1220 | |||
| 1221 | // F5 attack check (High load & same URI) |
||
| 1222 | |||
| 1223 | $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix($this->mydirname . '_access') . " WHERE ip={$ip4sql} AND request_uri={$uri4sql}"; |
||
| 1224 | $result = $xoopsDB->query($sql); |
||
| 1225 | if (!$xoopsDB->isResultSet($result)) { |
||
| 1226 | throw new \RuntimeException( |
||
| 1227 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), |
||
| 1228 | E_USER_ERROR, |
||
| 1229 | ); |
||
| 1230 | } |
||
| 1231 | [$f5_count] = $xoopsDB->fetchRow($result); |
||
| 1232 | if ($f5_count > $this->_conf['dos_f5count']) { |
||
| 1233 | |||
| 1234 | // delayed insert |
||
| 1235 | $xoopsDB->queryF($sql4insertlog); |
||
| 1236 | |||
| 1237 | // extends the expires of the IP with 5 minutes at least (pending) |
||
| 1238 | // $result = $xoopsDB->queryF( "UPDATE ".$xoopsDB->prefix($this->mydirname.'_access')." SET expire=UNIX_TIMESTAMP()+300 WHERE ip='$ip4sql' AND expire<UNIX_TIMESTAMP()+300" ) ; |
||
| 1239 | |||
| 1240 | // call the filter first |
||
| 1241 | $ret = $this->call_filter('f5attack_overrun'); |
||
| 1242 | |||
| 1243 | // actions for F5 Attack |
||
| 1244 | $this->_done_dos = true; |
||
| 1245 | $this->last_error_type = 'DoS'; |
||
| 1246 | switch ($this->_conf['dos_f5action']) { |
||
| 1247 | default: |
||
| 1248 | case 'exit': |
||
| 1249 | $this->output_log($this->last_error_type, $uid, true, 16); |
||
| 1250 | exit; |
||
| 1251 | case 'none': |
||
| 1252 | $this->output_log($this->last_error_type, $uid, true, 16); |
||
| 1253 | |||
| 1254 | return true; |
||
| 1255 | case 'biptime0': |
||
| 1256 | if ($can_ban) { |
||
| 1257 | $this->register_bad_ips(time() + $this->_conf['banip_time0']); |
||
| 1258 | } |
||
| 1259 | break; |
||
| 1260 | case 'bip': |
||
| 1261 | if ($can_ban) { |
||
| 1262 | $this->register_bad_ips(); |
||
| 1263 | } |
||
| 1264 | break; |
||
| 1265 | case 'hta': |
||
| 1266 | if ($can_ban) { |
||
| 1267 | $this->deny_by_htaccess(); |
||
| 1268 | } |
||
| 1269 | break; |
||
| 1270 | case 'sleep': |
||
| 1271 | sleep(5); |
||
| 1272 | break; |
||
| 1273 | } |
||
| 1274 | |||
| 1275 | return false; |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | // Check its Agent |
||
| 1279 | if ('' != trim($this->_conf['dos_crsafe']) && isset($_SERVER['HTTP_USER_AGENT']) && preg_match($this->_conf['dos_crsafe'], $_SERVER['HTTP_USER_AGENT'])) { |
||
| 1280 | // welcomed crawler |
||
| 1281 | $this->_done_dos = true; |
||
| 1282 | |||
| 1283 | return true; |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | // Crawler check (High load & different URI) |
||
| 1287 | $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix($this->mydirname . '_access') . " WHERE ip={$ip4sql}"; |
||
| 1288 | $result = $xoopsDB->query($sql); |
||
| 1289 | if (!$xoopsDB->isResultSet($result)) { |
||
| 1290 | return false; |
||
| 1291 | } |
||
| 1292 | [$crawler_count] = $xoopsDB->fetchRow($result); |
||
| 1293 | |||
| 1294 | // delayed insert |
||
| 1295 | $xoopsDB->exec($sql4insertlog); |
||
| 1296 | |||
| 1297 | if ($crawler_count > $this->_conf['dos_crcount']) { |
||
| 1298 | |||
| 1299 | // call the filter first |
||
| 1300 | $ret = $this->call_filter('crawler_overrun'); |
||
| 1301 | |||
| 1302 | // actions for bad Crawler |
||
| 1303 | $this->_done_dos = true; |
||
| 1304 | $this->last_error_type = 'CRAWLER'; |
||
| 1305 | switch ($this->_conf['dos_craction']) { |
||
| 1306 | default: |
||
| 1307 | case 'exit': |
||
| 1308 | $this->output_log($this->last_error_type, $uid, true, 16); |
||
| 1309 | exit; |
||
| 1310 | case 'none': |
||
| 1311 | $this->output_log($this->last_error_type, $uid, true, 16); |
||
| 1312 | |||
| 1313 | return true; |
||
| 1314 | case 'biptime0': |
||
| 1315 | if ($can_ban) { |
||
| 1316 | $this->register_bad_ips(time() + $this->_conf['banip_time0']); |
||
| 1317 | } |
||
| 1318 | break; |
||
| 1319 | case 'bip': |
||
| 1320 | if ($can_ban) { |
||
| 1321 | $this->register_bad_ips(); |
||
| 1322 | } |
||
| 1323 | break; |
||
| 1324 | case 'hta': |
||
| 1325 | if ($can_ban) { |
||
| 1326 | $this->deny_by_htaccess(); |
||
| 1327 | } |
||
| 1328 | break; |
||
| 1329 | case 'sleep': |
||
| 1330 | sleep(5); |
||
| 1331 | break; |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | return false; |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | return true; |
||
| 1338 | } |
||
| 1339 | |||
| 1340 | // |
||
| 1341 | /** |
||
| 1342 | * @return bool|null |
||
| 1343 | */ |
||
| 1344 | public function check_brute_force() |
||
| 1345 | { |
||
| 1346 | global $xoopsDB; |
||
| 1347 | |||
| 1348 | $ip = \Xmf\IPAddress::fromRequest(); |
||
| 1349 | if (false === $ip->asReadable()) { |
||
| 1350 | return true; |
||
| 1351 | } |
||
| 1352 | $uri = $_SERVER['REQUEST_URI'] ?? ''; |
||
| 1353 | $ip4sql = $xoopsDB->quote($ip->asReadable()); |
||
| 1354 | $uri4sql = $xoopsDB->quote($uri); |
||
| 1355 | |||
| 1356 | $victim_uname = empty($_COOKIE['autologin_uname']) ? $_POST['uname'] : $_COOKIE['autologin_uname']; |
||
| 1357 | // some UA send 'deleted' as a value of the deleted cookie. |
||
| 1358 | if ('deleted' === $victim_uname) { |
||
| 1359 | return null; |
||
| 1360 | } |
||
| 1361 | $mal4sql = $xoopsDB->quote("BRUTE FORCE: $victim_uname"); |
||
| 1362 | |||
| 1363 | // gargage collection |
||
| 1364 | $result = $xoopsDB->exec( |
||
| 1365 | 'DELETE FROM ' . $xoopsDB->prefix($this->mydirname . '_access') . ' WHERE expire < UNIX_TIMESTAMP()', |
||
| 1366 | ); |
||
| 1367 | |||
| 1368 | // sql for recording access log (INSERT should be placed after SELECT) |
||
| 1369 | $sql4insertlog = 'INSERT INTO ' . $xoopsDB->prefix($this->mydirname . '_access') |
||
| 1370 | . " SET ip={$ip4sql}, request_uri={$uri4sql}, malicious_actions={$mal4sql}, expire=UNIX_TIMESTAMP()+600"; |
||
| 1371 | |||
| 1372 | // count check |
||
| 1373 | $bf_count = 0; |
||
| 1374 | $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix($this->mydirname . '_access') . " WHERE ip={$ip4sql} AND malicious_actions like 'BRUTE FORCE:%'"; |
||
| 1375 | $result = $xoopsDB->query($sql); |
||
| 1376 | if ($xoopsDB->isResultSet($result)) { |
||
| 1377 | [$bf_count] = $xoopsDB->fetchRow($result); |
||
| 1378 | } else { |
||
| 1379 | throw new \RuntimeException( |
||
| 1380 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), |
||
| 1381 | E_USER_ERROR, |
||
| 1382 | ); |
||
| 1383 | } |
||
| 1384 | if ($bf_count > $this->_conf['bf_count']) { |
||
| 1385 | $this->register_bad_ips(time() + $this->_conf['banip_time0']); |
||
| 1386 | $this->last_error_type = 'BruteForce'; |
||
| 1387 | $this->message .= "Trying to login as '" . addslashes($victim_uname) . "' found.\n"; |
||
| 1388 | $this->output_log('BRUTE FORCE', 0, true, 1); |
||
| 1389 | $ret = $this->call_filter('bruteforce_overrun'); |
||
| 1390 | if (false == $ret) { |
||
| 1391 | exit; |
||
| 1392 | } |
||
| 1393 | } |
||
| 1394 | // delayed insert |
||
| 1395 | $xoopsDB->exec($sql4insertlog); |
||
| 1396 | return null; |
||
| 1397 | } |
||
| 1398 | |||
| 1399 | /** |
||
| 1400 | * @param $val |
||
| 1401 | */ |
||
| 1402 | protected function _spam_check_point_recursive($val) |
||
| 1403 | { |
||
| 1404 | if (is_array($val)) { |
||
| 1405 | foreach ($val as $subval) { |
||
| 1406 | $this->_spam_check_point_recursive($subval); |
||
| 1407 | } |
||
| 1408 | } else { |
||
| 1409 | // http_host |
||
| 1410 | $path_array = parse_url(XOOPS_URL); |
||
| 1411 | $http_host = empty($path_array['host']) ? 'www.xoops.org' : $path_array['host']; |
||
| 1412 | |||
| 1413 | // count URI up |
||
| 1414 | $count = -1; |
||
| 1415 | foreach (preg_split('#https?\:\/\/#i', $val) as $fragment) { |
||
| 1416 | if (0 !== strncmp($fragment, $http_host, strlen($http_host))) { |
||
| 1417 | ++$count; |
||
| 1418 | } |
||
| 1419 | } |
||
| 1420 | if ($count > 0) { |
||
| 1421 | $this->_spamcount_uri += $count; |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | // count BBCode likd [url=www....] up (without [url=http://...]) |
||
| 1425 | $this->_spamcount_uri += count(preg_split('/\[url=(?!http|\\"http|\\\'http|' . $http_host . ')/i', $val)) - 1; |
||
| 1426 | } |
||
| 1427 | } |
||
| 1428 | |||
| 1429 | /** |
||
| 1430 | * @param $points4deny |
||
| 1431 | * @param $uid |
||
| 1432 | */ |
||
| 1433 | public function spam_check($points4deny, $uid) |
||
| 1434 | { |
||
| 1435 | $this->_spamcount_uri = 0; |
||
| 1436 | $this->_spam_check_point_recursive($_POST); |
||
| 1437 | |||
| 1438 | if ($this->_spamcount_uri >= $points4deny) { |
||
| 1439 | $this->message .= ($_SERVER['REQUEST_URI'] ?? '') . " SPAM POINT: $this->_spamcount_uri\n"; |
||
| 1440 | $this->output_log('URI SPAM', $uid, false, 128); |
||
| 1441 | $ret = $this->call_filter('spamcheck_overrun'); |
||
| 1442 | if (false == $ret) { |
||
| 1443 | exit; |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | } |
||
| 1447 | |||
| 1448 | public function disable_features() |
||
| 1449 | { |
||
| 1450 | global $_POST, $_GET, $_COOKIE; |
||
| 1451 | |||
| 1452 | // disable "Notice: Undefined index: ..." |
||
| 1453 | $error_reporting_level = error_reporting(0); |
||
| 1454 | |||
| 1455 | // |
||
| 1456 | // bit 1 : disable XMLRPC , criteria bug |
||
| 1457 | // |
||
| 1458 | if ($this->_conf['disable_features'] & 1) { |
||
| 1459 | |||
| 1460 | // zx 2005/1/5 disable xmlrpc.php in root |
||
| 1461 | if (isset($_SERVER['SCRIPT_NAME']) && 'xmlrpc.php' === substr($_SERVER['SCRIPT_NAME'], -10)) { |
||
| 1462 | $this->output_log('xmlrpc', 0, true, 1); |
||
| 1463 | exit; |
||
| 1464 | } |
||
| 1465 | |||
| 1466 | // security bug of class/criteria.php 2005/6/27 |
||
| 1467 | if ((isset($_POST['uname']) && '0' === $_POST['uname']) || (isset($_COOKIE['autologin_pass']) && '0' === $_COOKIE['autologin_pass'])) { |
||
| 1468 | $this->output_log('CRITERIA'); |
||
| 1469 | exit; |
||
| 1470 | } |
||
| 1471 | } |
||
| 1472 | |||
| 1473 | // |
||
| 1474 | // bit 11 : XSS+CSRFs in XOOPS < 2.0.10 |
||
| 1475 | // |
||
| 1476 | if ($this->_conf['disable_features'] & 1024) { |
||
| 1477 | |||
| 1478 | // root controllers |
||
| 1479 | if (isset($_SERVER['SCRIPT_NAME']) && false === stripos($_SERVER['SCRIPT_NAME'], 'modules')) { |
||
| 1480 | // zx 2004/12/13 misc.php debug (file check) |
||
| 1481 | if ('misc.php' === substr($_SERVER['SCRIPT_NAME'], -8) && ((isset($_GET['type']) && 'debug' === $_GET['type']) || (isset($_POST['type']) && 'debug' === $_POST['type'])) && isset($_GET['file']) && !preg_match('/^dummy_\d+\.html$/', $_GET['file'])) { |
||
| 1482 | $this->output_log('misc debug'); |
||
| 1483 | exit; |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | // zx 2004/12/13 misc.php smilies |
||
| 1487 | if ('misc.php' === substr($_SERVER['SCRIPT_NAME'], -8) && ((isset($_GET['type']) && 'smilies' === $_GET['type']) || (isset($_POST['type']) && 'smilies' === $_POST['type'])) && isset($_GET['target']) && !preg_match('/^[0-9a-z_]*$/i', $_GET['target'])) { |
||
| 1488 | $this->output_log('misc smilies'); |
||
| 1489 | exit; |
||
| 1490 | } |
||
| 1491 | |||
| 1492 | // zx 2005/1/5 edituser.php avatarchoose |
||
| 1493 | if ('edituser.php' === substr($_SERVER['SCRIPT_NAME'], -12) && isset($_POST['op']) && 'avatarchoose' === $_POST['op'] && isset($_POST['user_avatar']) && false !== strpos($_POST['user_avatar'], '..')) { |
||
| 1494 | $this->output_log('edituser avatarchoose'); |
||
| 1495 | exit; |
||
| 1496 | } |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | // zx 2005/1/4 findusers |
||
| 1500 | if (isset($_SERVER['SCRIPT_NAME']) && 'modules/system/admin.php' === substr($_SERVER['SCRIPT_NAME'], -24) && ((isset($_GET['fct']) && 'findusers' === $_GET['fct']) || (isset($_POST['fct']) && 'findusers' === $_POST['fct']))) { |
||
| 1501 | foreach ($_POST as $key => $val) { |
||
| 1502 | if (false !== strpos($key, "'") || false !== strpos($val, "'")) { |
||
| 1503 | $this->output_log('findusers'); |
||
| 1504 | exit; |
||
| 1505 | } |
||
| 1506 | } |
||
| 1507 | } |
||
| 1508 | |||
| 1509 | // preview CSRF zx 2004/12/14 |
||
| 1510 | // news submit.php |
||
| 1511 | if (isset($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'], -23) === 'modules/news/submit.php' && isset($_POST['preview']) && isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], XOOPS_URL . '/modules/news/submit.php') !== 0) { |
||
| 1512 | $_POST['nohtml'] = 1; |
||
| 1513 | } |
||
| 1514 | |||
| 1515 | // news admin/index.php |
||
| 1516 | if (isset($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'], -28) === 'modules/news/admin/index.php' && (isset($_POST['op']) && $_POST['op'] === 'preview' || isset($_GET['op']) && $_GET['op'] === 'preview') && isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], XOOPS_URL . '/modules/news/admin/index.php') !== 0) { |
||
| 1517 | $_POST['nohtml'] = 1; |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | // comment comment_post.php |
||
| 1521 | if (isset($_POST['com_dopreview']) && isset($_SERVER['HTTP_REFERER']) && strpos(substr($_SERVER['HTTP_REFERER'], -16), 'comment_post.php') === false) { |
||
| 1522 | $_POST['dohtml'] = 0; |
||
| 1523 | } |
||
| 1524 | |||
| 1525 | // disable preview of system's blocksadmin |
||
| 1526 | if (isset($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'], -24) === 'modules/system/admin.php' && (isset($_GET['fct']) && $_GET['fct'] === 'blocksadmin' || isset($_POST['fct']) && $_POST['fct'] === 'blocksadmin') && isset($_POST['previewblock'])) { |
||
| 1527 | die("Danger! Don't use this preview. Use 'altsys module' instead. (by Protector)"); |
||
| 1528 | } |
||
| 1529 | |||
| 1530 | // tpl preview |
||
| 1531 | if (isset($_SERVER['SCRIPT_NAME']) && substr($_SERVER['SCRIPT_NAME'], -24) === 'modules/system/admin.php' && (isset($_GET['fct']) && $_GET['fct'] === 'tplsets' || isset($_POST['fct']) && $_POST['fct'] === 'tplsets')) { |
||
| 1532 | if (isset($_POST['op']) && $_POST['op'] === 'previewpopup' || isset($_GET['op']) && $_GET['op'] === 'previewpopup' || isset($_POST['previewtpl'])) { |
||
| 1533 | die("Danger! Don't use this preview. (by Protector)"); |
||
| 1534 | } |
||
| 1535 | } |
||
| 1536 | } |
||
| 1537 | |||
| 1538 | // restore reporting level |
||
| 1539 | error_reporting($error_reporting_level); |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | /** |
||
| 1543 | * @param string $type |
||
| 1544 | * @param string $dying_message |
||
| 1545 | * |
||
| 1546 | * @return int|mixed |
||
| 1547 | */ |
||
| 1548 | public function call_filter($type, $dying_message = '') |
||
| 1558 | } |
||
| 1559 | } |
||
| 1560 |