首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

隱藏 Apache 與 PHP 版本資訊:完整安全強化設定指南

     編輯:PHP 相關知識

為什麼要隱藏版本資訊?

HTTP 回應標頭預設會洩露:`Server: Apache/2.4.41 (Ubuntu) PHP/7.4.3`。攻擊者可據此鎖定已知漏洞 (CVE) 進行針對性攻擊。

Apache 設定 (httpd.conf / apache2.conf)

# 只顯示 "Apache",隱藏版本號、OS、模組
ServerTokens Prod
ServerSignature Off

# 進階:使用 mod_headers 移除/修改標頭 (需啟用 mod_headers)
Header unset Server
Header always set Server "WebServer"
# 或完全移除
Header always unset Server
Header always unset X-Powered-By

ServerTokens 選項比較:

值回應標頭範例
Prod (推薦)Server: Apache
MajorServer: Apache/2
MinorServer: Apache/2.4
MinimalServer: Apache/2.4.41
OSServer: Apache/2.4.41 (Ubuntu)
Full (預設)Server: Apache/2.4.41 (Ubuntu) PHP/7.4.3 mod_ssl/2.4.41 OpenSSL/1.1.1

PHP 設定 (php.ini)

; 隱藏 X-Powered-By: PHP/7.4.3 標頭
expose_php = Off

修改後必須重啟 Apache:`systemctl reload apache2` 或 `apachectl graceful`

驗證設定生效

$ curl -I https://example.com
HTTP/1.1 200 OK
Date: Mon, 01 Jan 2024 00:00:00 GMT
Server: Apache
Content-Type: text/html; charset=UTF-8
# 不再出現 PHP 版本、Apache 詳細版本、OpenSSL 版本等

進階防護:偽裝 Server 標頭

<IfModule mod_headers.c>
    # 偽裝為 Nginx
    Header always set Server "nginx"
    # 或自訂
    Header always set Server "MyCustomServer/1.0"
</IfModule>

注意:偽裝可能干擾某些相容性檢查,建議僅隱藏細節而非偽裝。

其他 HTTP 安全標頭建議

<IfModule mod_headers.c>
    # 防止點擊劫持
    Header always set X-Frame-Options "SAMEORIGIN"
    # XSS 保護
    Header always set X-XSS-Protection "1; mode=block"
    # 禁止 MIME 嗅探
    Header always set X-Content-Type-Options "nosniff"
    # HTTPS 強制 (HSTS)
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    # 內容安全政策 (CSP) - 需根據網站調整
    Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"
</IfModule>

Nginx + PHP-FPM 環境設定

# nginx.conf
server {
    # 隱藏 Nginx 版本
    server_tokens off;
    
    # 隱藏 PHP 版本 (需 PHP 設定 expose_php=Off)
    fastcgi_hide_header X-Powered-By;
    
    # 自訂 Server 標頭 (需 ngx_headers_more 模組)
    # more_set_headers 'Server: MyServer';
}

總結檢查清單

  • [ ] Apache: `ServerTokens Prod`
  • [ ] Apache: `ServerSignature Off`
  • [ ] PHP: `expose_php = Off`
  • [ ] (選擇性) `Header unset Server` / `Header unset X-Powered-By`
  • [ ] 重啟 Web 伺服器
  • [ ] `curl -I` 驗證回應標頭
  • [ ] 部署額外安全標頭 (HSTS, CSP, X-Frame-Options 等)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved