misc_security.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: misc_security.php 25889 2011-11-24 09:52:20Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. global $_G;
  12. if(is_string($this->config['security']['attackevasive'])) {
  13. $attackevasive_tmp = explode('|', $this->config['security']['attackevasive']);
  14. $attackevasive = 0;
  15. foreach($attackevasive_tmp AS $key => $value) {
  16. $attackevasive += intval($value);
  17. }
  18. unset($attackevasive_tmp);
  19. } else {
  20. $attackevasive = $this->config['security']['attackevasive'];
  21. }
  22. $lastrequest = isset($_G['cookie']['lastrequest']) ? authcode($_G['cookie']['lastrequest'], 'DECODE') : '';
  23. if($attackevasive & 1 || $attackevasive & 4) {
  24. dsetcookie('lastrequest', authcode(TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
  25. }
  26. if($attackevasive & 1) {
  27. if(TIMESTAMP - $lastrequest < 1) {
  28. securitymessage('attackevasive_1_subject', 'attackevasive_1_message');
  29. }
  30. }
  31. if(($attackevasive & 2) && ($_SERVER['HTTP_X_FORWARDED_FOR'] ||
  32. $_SERVER['HTTP_VIA'] || $_SERVER['HTTP_PROXY_CONNECTION'] ||
  33. $_SERVER['HTTP_USER_AGENT_VIA'] || $_SERVER['HTTP_CACHE_INFO'] ||
  34. $_SERVER['HTTP_PROXY_CONNECTION'])) {
  35. securitymessage('attackevasive_2_subject', 'attackevasive_2_message', FALSE);
  36. }
  37. if($attackevasive & 4) {
  38. if(empty($lastrequest) || TIMESTAMP - $lastrequest > 300) {
  39. securitymessage('attackevasive_4_subject', 'attackevasive_4_message');
  40. }
  41. }
  42. if($attackevasive & 8) {
  43. list($visitcode, $visitcheck, $visittime) = explode('|', authcode($_G['cookie']['visitcode'], 'DECODE'));
  44. if(!$visitcode || !$visitcheck || !$visittime || TIMESTAMP - $visittime > 60 * 60 * 4 ) {
  45. if(empty($_POST['secqsubmit']) || ($visitcode != md5($_POST['answer']))) {
  46. $answer = 0;
  47. $question = '';
  48. for ($i = 0; $i< rand(2, 5); $i ++) {
  49. $r = rand(1, 20);
  50. $question .= $question ? ' + '.$r : $r;
  51. $answer += $r;
  52. }
  53. $question .= ' = ?';
  54. dsetcookie('visitcode', authcode(md5($answer).'|0|'.TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
  55. securitymessage($question, '<input type="text" name="answer" size="8" maxlength="150" /><input type="submit" name="secqsubmit" class="button" value=" Submit " />', FALSE, TRUE);
  56. } else {
  57. dsetcookie('visitcode', authcode($visitcode.'|1|'.TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
  58. }
  59. }
  60. }
  61. function securitymessage($subject, $message, $reload = TRUE, $form = FALSE) {
  62. global $_G;
  63. $scuritylang = array(
  64. 'attackevasive_1_subject' => '&#x9891;&#x7e41;&#x5237;&#x65b0;&#x9650;&#x5236;',
  65. 'attackevasive_1_message' => '&#x60a8;&#x8bbf;&#x95ee;&#x672c;&#x7ad9;&#x901f;&#x5ea6;&#x8fc7;&#x5feb;&#x6216;&#x8005;&#x5237;&#x65b0;&#x95f4;&#x9694;&#x65f6;&#x95f4;&#x5c0f;&#x4e8e;&#x4e24;&#x79d2;&#xff01;&#x8bf7;&#x7b49;&#x5f85;&#x9875;&#x9762;&#x81ea;&#x52a8;&#x8df3;&#x8f6c;&#x20;&#x2e;&#x2e;&#x2e;',
  66. 'attackevasive_2_subject' => '&#x4ee3;&#x7406;&#x670d;&#x52a1;&#x5668;&#x8bbf;&#x95ee;&#x9650;&#x5236;',
  67. 'attackevasive_2_message' => '&#x672c;&#x7ad9;&#x73b0;&#x5728;&#x9650;&#x5236;&#x4f7f;&#x7528;&#x4ee3;&#x7406;&#x670d;&#x52a1;&#x5668;&#x8bbf;&#x95ee;&#xff0c;&#x8bf7;&#x53bb;&#x9664;&#x60a8;&#x7684;&#x4ee3;&#x7406;&#x8bbe;&#x7f6e;&#xff0c;&#x76f4;&#x63a5;&#x8bbf;&#x95ee;&#x672c;&#x7ad9;&#x3002;',
  68. 'attackevasive_4_subject' => '&#x9875;&#x9762;&#x91cd;&#x8f7d;&#x5f00;&#x542f;',
  69. 'attackevasive_4_message' => '&#x6b22;&#x8fce;&#x5149;&#x4e34;&#x672c;&#x7ad9;&#xff0c;&#x9875;&#x9762;&#x6b63;&#x5728;&#x91cd;&#x65b0;&#x8f7d;&#x5165;&#xff0c;&#x8bf7;&#x7a0d;&#x5019;&#x20;&#x2e;&#x2e;&#x2e;'
  70. );
  71. $subject = $scuritylang[$subject] ? $scuritylang[$subject] : $subject;
  72. $message = $scuritylang[$message] ? $scuritylang[$message] : $message;
  73. if($_GET['inajax']) {
  74. security_ajaxshowheader();
  75. echo '<div id="attackevasive_1" class="popupmenu_option"><b style="font-size: 16px">'.$subject.'</b><br /><br />'.$message.'</div>';
  76. security_ajaxshowfooter();
  77. } else {
  78. echo '<html>';
  79. echo '<head>';
  80. echo '<title>'.$subject.'</title>';
  81. echo '</head>';
  82. echo '<body bgcolor="#FFFFFF">';
  83. if($reload) {
  84. echo '<script language="JavaScript">';
  85. echo 'function reload() {';
  86. echo ' document.location.reload();';
  87. echo '}';
  88. echo 'setTimeout("reload()", 1001);';
  89. echo '</script>';
  90. }
  91. if($form) {
  92. echo '<form action="'.$G['PHP_SELF'].'" method="post" autocomplete="off">';
  93. }
  94. echo '<table cellpadding="0" cellspacing="0" border="0" width="700" align="center" height="85%">';
  95. echo ' <tr align="center" valign="middle">';
  96. echo ' <td>';
  97. echo ' <table cellpadding="10" cellspacing="0" border="0" width="80%" align="center" style="font-family: Verdana, Tahoma; color: #666666; font-size: 11px">';
  98. echo ' <tr>';
  99. echo ' <td valign="middle" align="center" bgcolor="#EBEBEB">';
  100. echo ' <br /><br /> <b style="font-size: 16px">'.$subject.'</b> <br /><br />';
  101. echo $message;
  102. echo ' <br /><br />';
  103. echo ' </td>';
  104. echo ' </tr>';
  105. echo ' </table>';
  106. echo ' </td>';
  107. echo ' </tr>';
  108. echo '</table>';
  109. if($form) {
  110. echo '</form>';
  111. }
  112. echo '</body>';
  113. echo '</html>';
  114. }
  115. exit();
  116. }
  117. function security_ajaxshowheader() {
  118. $charset = getglobal('config/output/charset');
  119. ob_end_clean();
  120. @header("Expires: -1");
  121. @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  122. @header("Pragma: no-cache");
  123. header("Content-type: application/xml");
  124. echo "<?xml version=\"1.0\" encoding=\"".$charset."\"?>\n<root><![CDATA[";
  125. }
  126. function security_ajaxshowfooter() {
  127. echo ']]></root>';
  128. exit();
  129. }
  130. ?>