# ============================================================
# 世界OnLine · 全站安全加固（阶段4）
# 说明：Apache 配置。若使用 Nginx，请参照注释在 nginx.conf 中
#       配置等价规则（禁目录列举 / 禁敏感文件 / 禁上传目录 PHP 执行）。
# ============================================================

# ---------- 基础：禁止目录列举 / 隐藏服务器版本 ----------
Options -Indexes
ServerSignature Off

# ---------- 安全响应头 ----------
<IfModule mod_headers.c>
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# ---------- 禁止访问隐藏文件（.git / .env / .htaccess 等） ----------
<IfModule mod_authz_core.c>
    <FilesMatch "^\.">
        Require all denied
    </FilesMatch>
</IfModule>
<IfModule !mod_authz_core.c>
    <FilesMatch "^\.">
        Order allow,deny
        Deny from all
    </FilesMatch>
</IfModule>

# ---------- 禁止直接下载敏感文件（源码配置 / 备份脚本 / 数据库备份 / 文档） ----------
# 注意：config.php 由 PHP 内部 include，禁止 HTTP 直连不影响站点功能
<IfModule mod_authz_core.c>
    <FilesMatch "^(config\.php|backup_db\.php)$">
        Require all denied
    </FilesMatch>
    <FilesMatch "\.(sql|md|log|bak|sh|bat|ini)$">
        Require all denied
    </FilesMatch>
</IfModule>
<IfModule !mod_authz_core.c>
    <FilesMatch "^(config\.php|backup_db\.php)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
    <FilesMatch "\.(sql|md|log|bak|sh|bat|ini)$">
        Order allow,deny
        Deny from all
    </FilesMatch>
</IfModule>

# ---------- 屏蔽常见恶意请求特征（注入 / 扫描 / 路径穿越） ----------
<IfModule mod_rewrite.c>
    RewriteEngine On
    # SQL 注入特征
    RewriteCond %{QUERY_STRING} (union\s+select|information_schema|concat\s*\(|sleep\s*\(|benchmark\s*\(|updatexml|extractvalue) [NC,OR]
    # XSS 特征
    RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
    # 危险函数
    RewriteCond %{QUERY_STRING} (base64_(en|de)code|eval\s*\(|gzinflate|assert\s*\() [NC,OR]
    # 路径穿越
    RewriteCond %{QUERY_STRING} (\.\./|\.\.%2[fF]|%00) [NC,OR]
    # 常见扫描器
    RewriteCond %{HTTP_USER_AGENT} (sqlmap|nikto|acunetix|nessus|scanbot) [NC]
    RewriteRule .* - [F,L]
</IfModule>

# ---------- Nginx 迁移速查（供部署参考） ----------
# autoindex off;
# location ~ /\. { deny all; }
# location ~ \.(sql|md|log|bak)$ { deny all; }
# location ~* ^/uploads/.*\.(php|phtml|phar)$ { deny all; }
