AWD攻防赛之各类漏洞FIX方案
字数 927 2025-08-18 11:38:56
AWD攻防赛漏洞修复方案详解
0x01 引言
AWD(Attack With Defence)是CTF比赛中的线下赛形式,考验选手的攻击能力、防御能力及团队协作能力。比赛中各队伍需维护多台服务器,通过利用漏洞攻击他队得分,同时修补自身漏洞避免失分。
0x02 SQL注入漏洞修复
漏洞成因
- 不规范SQL语句书写
- 特殊字符过滤不严格
- 通过POST/GET提交恶意代码
修复方案
黑名单过滤
$filter = "regexp|from|count|procedure|and|ascii|substr|substring|left|right|union|if|case|pow|exp|order|sleep|benchmark|into|load|outfile|dumpfile|load_file|join|show|select|update|set|concat|delete|alter|insert|create|union|or|drop|not|for|join|is|between|group_concat|like|where|user|ascii|greatest|mid|substr|left|right|char|hex|ord|case|limit|conv|table|mysql_history|flag|count|rpad|if";
if((preg_match("/".$filter."/is",$username)== 1) || (preg_match("/".$filter."/is",$password)== 1)){
die();
}
转义处理
- PHP配置(5.4以下版本有效):
magic_quotes_gpc=on
- 使用
addslashes()函数:
addslashes(string)
注意:不要对已由magic_quotes_gpc转义的字符串再次使用addslashes()
错误控制
error_reporting(0); // 关闭错误显示
0x03 反序列化漏洞修复
PHP修复方案
PHP7新特性
// 禁止所有类反序列化
$data = unserialize($foo, ["allowed_classes" => false]);
// 只允许特定类反序列化
$data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]]);
Session处理器设置
ini_set('session.serialize_handler', 'php_serialize');
ini_set('session.serialize_handler', 'php');
PHAR协议过滤
$filter = "phar|zip|compress.bzip2|compress.zlib";
if(preg_match("/".$filter."/is",$name)== 1){
die();
}
禁用危险函数
disable_functions=fileatime,filectime,file_exists,file_get_contents,file_put_content,filegroup,fileinode,filemtime,fileowner,fileperms,is_dir,is_executable,is_file,is_link,is_readable,is_writable,is_writeable,fopen,readfile,unlink,parse_ini_file,file,copy,stat,serialize,unserialize,__construct,__destruct,__toString,__sleep,__wakeup,__get,__set,__isset,__unset,__invoke
Java修复方案
白名单校验
对所有传入的反序列化对象进行类型名称检查
禁止执行外部命令
SecurityManager sm = new SecurityManager() {
private void check(Permission perm) {
// 禁止exec
if (perm instanceof java.io.FilePermission) {
String actions = perm.getActions();
if (actions != null && actions.contains("execute")) {
throw new SecurityException("execute denied!");
}
}
// 禁止设置新的SecurityManager
if (perm instanceof java.lang.RuntimePermission) {
String name = perm.getName();
if (name != null && name.contains("setSecurityManager")) {
throw new SecurityException("System.setSecurityManager denied!");
}
}
}
};
System.setSecurityManager(sm);
0x04 文件上传漏洞修复
上传限制
if (($_FILES["Up10defile"]["type"]=="image/gif") &&
(substr($_FILES["Up10defile"]["name"], strrpos($_FILES["Up10defile"]["name"], '.')) == '.gif') &&
($_FILES["file"]["size"] < 1024000)) {
// 允许上传
} else {
die();
}
强制添加后缀
move_uploaded_file($_FILES["Up10defile"]["tmp_name"], "upload_file/" . $_FILES["Up10defile"]["name"].".gif");
0x05 文件包含漏洞修复
本地文件包含(LFI)修复
$filename = $_GET['filename'];
$pattern = "etc|var|php|jpg|jpeg|png|bmp|gif";
if(preg_match("/".$pattern."/is",$filename)== 1){
die();
}
include($filename);
远程文件包含(RFI)修复
allow_url_fopen = off
allow_url_include = off
伪协议过滤
$pattern = "etc|var|php|jpg|jpeg|png|bmp|gif|file|http|ftp|php|zlib|data|glob|phar|ssh2|rar|ogg|expect|zip|compress|filter|input";
if(preg_match("/".$pattern."/is",$filename)== 1){
die();
}
0x06 任意文件读取防御
读取限制
$filename = $_GET['filename'];
$pattern = "etc|var|file|http|ftp|php|zlib|data|glob|phar|ssh2|rar|ogg|expect|zip|compress|filter|input";
if(preg_match("/".$pattern."/is",$filename)== 1){
die();
}
echo file_get_contents($filename);
限定访问范围
open_basedir="/var/www/html"
0x07 代码/命令执行漏洞修复
PHP修复方案
preg_replace()修复
移除/e修饰符,使用preg_replace_callback()替代
调用函数过滤
$pattern = "eval|assert|passthru|pcntl_exec|exec|system|escapeshellcmd|popen|chroot|scandir|chgrp|chown|shell_exec|proc_open|proc_get_status|ob_start";
if(preg_match("/".$pattern."/is",$cc)== 1){
die();
}
禁用危险函数
disable_functions=call_user_func,call_user_func_array,array_map,array_filter,ob_start,phpinfo,eval,assert,passthru,pcntl_exec,exec,system,escapeshellcmd,popen,chroot,scandir,chgrp,chown,shell_exec
0x08 XSS防御方案
HttpOnly属性
防止JavaScript访问带有HttpOnly属性的Cookie
htmlspecialchars()使用
$name = htmlspecialchars($_GET['name'], ENT_QUOTES);
注意flags参数:
- ENT_COMPAT - 默认,仅编码双引号
- ENT_QUOTES - 编码双引号和单引号
- ENT_NOQUOTES - 不编码任何引号
JavaScript编码
使用\xhh方式对非字母数字字符进行编码
0x09 CSRF防御方案
- 验证HTTP Referer字段
- 在请求地址中添加Token并验证
- 在HTTP头中自定义属性并验证
0x10 XXE防御方案
PHP
libxml_disable_entity_loader(true);
Java
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
通用过滤
过滤关键词:<!DOCTYPE、!ENTITY、SYSTEM和PUBLIC