SQL注入是Web应用程序最常见的安全威胁之一。以下是在CentOS系统上保护Web应用程序免受SQL注入攻击的综合配置方案:
# 在httpd.conf或虚拟主机配置中添加
<IfModule mod_security2.c>
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess On
SecRule REQUEST_HEADERS:Content-Type "text/xml" "id:'200000',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=XML"
SecRule REQUEST_URI|REQUEST_HEADERS|ARGS|ARGS_NAMES "@detectSQLi" "id:'950001',phase:2,log,deny,status:403,msg:'SQL Injection Attack'"
</IfModule>
# 在nginx.conf或站点配置中添加
location / {
# 启用ModSecurity (需先安装)
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
# 基本过滤规则
if ($args ~* "union.*select.*\(") {
return 403;
}
if ($args ~* "concat.*\(") {
return 403;
}
}
-- 创建最小权限用户
CREATE USER 'webapp'@'localhost' IDENTIFIED BY '强密码123!@#';
GRANT SELECT, INSERT, UPDATE, DELETE ON database.* TO 'webapp'@'localhost';
REVOKE ALL PRIVILEGES ON mysql.* FROM 'webapp'@'localhost';
-- 启用查询日志监控可疑活动
SET GLOBAL general_log = 'ON';
SET GLOBAL log_output = 'FILE';
-- 在pg_hba.conf中限制连接
host all all 127.0.0.1/32 md5
-- 创建角色限制
CREATE ROLE web_user WITH LOGIN PASSWORD '强密码456$%^';
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO web_user;
; 在/etc/php.ini中修改
allow_url_fopen = Off
allow_url_include = Off
display_errors = Off
log_errors = On
expose_php = Off
magic_quotes_gpc = Off ; 已弃用,使用预处理语句替代
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'webapp', '强密码123!@#');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // 禁用模拟预处理
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $_POST['username']]);
$user = $stmt->fetch();
?>
# 安装Fail2Ban
yum install -y fail2ban
# 创建自定义jail配置
cat > /etc/fail2ban/jail.d/sqli.conf <<EOF
[apache-sqli]
enabled = true
port = http,https
filter = apache-sqli
logpath = /var/log/httpd/error_log
maxretry = 3
bantime = 86400
EOF
# 确保SELinux处于强制模式
setenforce 1
# 为Web目录设置合适的安全上下文
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -Rv /var/www/html
yum install -y logwatch
echo "/usr/sbin/logwatch --output mail --mailto admin@example.com --detail high" >> /etc/cron.daily/0logwatch
# 配置yum自动安全更新
yum install -y yum-cron
sed -i 's/apply_updates = no/apply_updates = yes/' /etc/yum/yum-cron.conf
systemctl enable yum-cron && systemctl start yum-cron
# 对于Apache
yum install -y mod_security mod_security_crs
cp /etc/httpd/modsecurity.d/modsecurity.conf-recommended /etc/httpd/modsecurity.d/modsecurity.conf
wget https://github.com/coreruleset/coreruleset/archive/v3.3.0.tar.gz
tar -xvzf v3.3.0.tar.gz -C /etc/httpd/modsecurity.d/ --strip-components=1
# 对于Nginx
yum install -y nginx-mod-modsecurity
mkdir /etc/nginx/modsec
wget https://github.com/SpiderLabs/ModSecurity/releases/download/v3.0.4/modsecurity-v3.0.4.tar.gz
# 在modsecurity.conf中添加
SecRule REQUEST_FILENAME|REQUEST_URI|ARGS_NAMES|ARGS "@detectSQLi" \
"id:942100,\
phase:2,\
block,\
t:none,t:urlDecodeUni,\
msg:'SQL Injection Attack Detected',\
logdata:'Matched Data: %{TX.0} found within %{MATCHED_VAR_NAME}: %{MATCHED_VAR}',\
tag:'application-multi',\
tag:'language-multi',\
tag:'platform-multi',\
tag:'attack-sqli',\
severity:'CRITICAL'"
通过实施以上多层次防护措施,可以显著降低CentOS系统上Web应用程序遭受SQL注入攻击的风险。