Web Shell攻击是常见的服务器入侵手段,通过在服务器上植入恶意脚本文件获取控制权。以下是针对Nginx环境的全面防护方案:
location ~* \.(php|jsp|asp|aspx|sh|pl|py)$ {
deny all;
return 403;
}
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
SecRuleEngine On
SecRequestBodyAccess On
Include /path/to/owasp-modsecurity-crs/crs-setup.conf
Include /path/to/owasp-modsecurity-crs/rules/*.conf
SecRule REQUEST_URI|REQUEST_BODY "@rx (cmd\.exe|/bin/sh|wget|curl|nc|netcat|php\s*-r|eval\s*\(|base64_decode|system\s*\(|shell_exec\s*\()" \
"id:1001,phase:2,log,deny,status:403,msg:'Web Shell Attack Detected'"
# 使用inotify-tools监控web目录
inotifywait -m -r -e create,modify /var/www/html | while read path action file; do
echo "文件变更: $path$file - $action"
# 触发扫描或报警
done
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /var/log/nginx/access.log combined if=$loggable;
# /etc/fail2ban/jail.d/nginx-webshell.conf
[nginx-webshell]
enabled = true
filter = nginx-webshell
port = http,https
logpath = /var/log/nginx/access.log
maxretry = 3
bantime = 86400
chown -R www-data:www-data /var/www/html
find /var/www/html -type d -exec chmod 750 {} \;
find /var/www/html -type f -exec chmod 640 {} \;
; php.ini 关键设置
disable_functions = exec,passthru,shell_exec,system,proc_open,popen
open_basedir = /var/www/html
expose_php = Off
LMD
或ClamAV
通过以上多层防御措施,可以显著降低Web Shell攻击的成功率,同时提高攻击检测能力。