Discuz! x2的防CC攻击功能分析
在config_global.php文件中有如下代码$_config['security']['attackevasive'] = 0;可以设置的值有:0表示关闭此功能1表示cookie刷新限制
2表示限制代理访问
4表示二次请求
8表示回答问题(第一次访问时需要回答问题)在source/class/class_core.php文件中可以找到如下代码if($this->config['security']['attackevasive'] && (!defined('CURSCRIPT') || !in_array($this->var['mod'], array('seccode', 'secqaa', 'swfupload')))) {
require_once libfile('misc/security', 'include');
}$this->config['security']['attackevasive']为config_global.php文件里设置的$_config['security']['attackevasive']的值。
找到source/misc/misc_security.php文件if(is_string($this->config['security']['attackevasive'])) {
//如果$this->config['security']['attackevasive']是字符串
$attackevasive_tmp = explode('|', $this->config['security']['attackevasive']);
根据分隔符|分割$this->config['security']['attackevasive']得到数组$attackevasive_tmp
$attackevasive = 0;
foreach($attackevasive_tmp AS $key => $value) {
$attackevasive += intval($value);
//将数组$attackevasive中每项的值加起来得到$attackevasive
}
unset($attackevasive_tmp);
} else {
$attackevasive = $this->config['security']['attackevasive'];
}$lastrequest = isset($_G['cookie']['lastrequest']) ? authcode($_G['cookie']['lastrequest'], 'DECODE') : '';获取上一次请求的时间。
$_G['cookie']['lastrequest']为记录上一次请求时间的cookie。if($attackevasive & 1 || $attackevasive & 4) {
dsetcookie('lastrequest', authcode(TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
}如果设置的是cookie刷新限制($attackevasive & 1)或者二次请求($attackevasive & 4)的方式,那么创建以当前时间为值的上一次请求的cookie。if($attackevasive & 1) {
if(TIMESTAMP - $lastrequest < 1) {
securitymessage('attackevasive_1_subject', 'attackevasive_1_message');
}
}cookie刷新限制的方式:当前时间-上一次请求的时间<1的时候会有页面重载的提示。if(($attackevasive & 2) && ($_SERVER['HTTP_X_FORWARDED_FOR'] ||
$_SERVER['HTTP_VIA'] || $_SERVER['HTTP_PROXY_CONNECTION'] ||
$_SERVER['HTTP_USER_AGENT_VIA'] || $_SERVER['HTTP_CACHE_INFO'] ||
$_SERVER['HTTP_PROXY_CONNECTION'])) {
securitymessage('attackevasive_2_subject', 'attackevasive_2_message', FALSE);
}限制代理访问的方式。if($attackevasive & 4) {
if(empty($lastrequest) || TIMESTAMP - $lastrequest > 300) {
securitymessage('attackevasive_4_subject', 'attackevasive_4_message');
}
}二次请求的方式:当前时间-上一次请求时间>300秒的时候会有页面重载的提示。if($attackevasive & 8) {
list($visitcode, $visitcheck, $visittime) = explode('|', authcode($_G['cookie']['visitcode'], 'DECODE'));
if(!$visitcode || !$visitcheck || !$visittime || TIMESTAMP - $visittime > 60 * 60 * 4 ) {
if(empty($_POST['secqsubmit']) || ($visitcode != md5($_POST['answer']))) {
$answer = 0;
$question = '';
for ($i = 0; $i< rand(2, 5); $i ++) {
$r = rand(1, 20);
$question .= $question ? ' + '.$r : $r;
$answer += $r;
}
$question .= ' = ?';
dsetcookie('visitcode', authcode(md5($answer).'|0|'.TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
securitymessage($question, '<input type="text" name="answer" size="8" maxlength="150" /><input type="submit" name="secqsubmit" class="button" value=" Submit " />', FALSE, TRUE);
} else {
dsetcookie('visitcode', authcode($visitcode.'|1|'.TIMESTAMP, 'ENCODE'), TIMESTAMP + 816400, 1, true);
}
}
}回答问题的方式:第一次访问时需要回答问题。
页:
[1]