WAF開源解決方案-恆逸資深講師 陳勇勳
在一般企業網路安全防護中最常使用的就是防火牆,但如果僅是Layer 3的防火牆、那是無法防護應用程式本身設計瑕疵的問題,例如網站常見的SQL Injection、Cross-Site Scripting、Local File Inclusion、Remote File Inclusion等安全問題,為了解決這方面的問題就必須使用Layer 7等級的應用程式防火牆, ModSecurity是一種開源的Web應用防火牆WAF(Web Application Firewall),用來防護Web應用程式免受常見的網路攻擊,ModSecurity透過檢查HTTP請求和回應來過濾並阻擋潛在的惡意流量,來達成防護網站程式設計瑕疵造成的安全問題,ModSecurity包含 ”程式” 與 ”規則” 兩個部份。
- 程式
ModSecurity被設計成Apache、Nginx 及IIS網站伺服器的模組,並與Web Server協同工作,也可以將之部署在Reverse Proxy上,本文將以Nginx Reverse Proxy搭配 ModSecurity為例。
- 規則
ModSecurity是執行者,至於要如何進行安全防護則需要 “規則”,如果您對網站的攻擊手法很了解,那麼 ”規則” 您可以自己動作撰寫,有興趣的朋友們可以參考 https://coreruleset.org/docs/rules/creating/ ,或者我們可以下載別人寫好的規則來使用,例如:
- 開源規則集 OWASP Core Rule Set (CRS)
可以https://coreruleset.org/ 網站下載,其規則涵蓋常見攻擊,如 SQL Injection、XSS、Local File Inclusion 等,這些規則由社群維護並且定期更新。
- 商業公司規則
由Trustwave、F5等公司提供,這些規則能更快速反應最新的攻擊手法,至於取得方式就請洽該公司網站
- 開源規則集 OWASP Core Rule Set (CRS)
- 使用 Nginx + ModSecurity 及 OWASP CRS 建構 Reverse Proxy
接續 “使用 nginx提供 http 反向代理”
(https://www.uuu.com.tw/Public/content/article/23/20231225.htm) - 連接 Extra Packages for Enterprise Linux (EPEL) - Fedora Docs
- 安裝 modsecurity

dnf install nginx-mod-modsecurity
- 安裝OWASP Core Rule Set (CRS)
[root@Reverse-Proxy tmp]# cd /etc/nginx/ [root@Reverse-Proxy nginx]# git clone https://github.com/coreruleset/coreruleset.git Cloning into 'coreruleset'... remote: Enumerating objects: 34159, done. remote: Counting objects: 100% (47/47), done. remote: Compressing objects: 100% (30/30), done. remote: Total 34159 (delta 33), reused 18 (delta 17), pack-reused 34112 (from 3) Receiving objects: 100% (34159/34159), 9.62 MiB | 15.82 MiB/s, done. Resolving deltas: 100% (26980/26980), done. [root@Reverse-Proxy nginx]# [root@Reverse-Proxy nginx]# cp coreruleset/crs-setup.conf.example coreruleset/crs.conf [root@Reverse-Proxy nginx]# [root@Reverse-Proxy nginx]# vi /etc/nginx/modsecurity.conf 在檔案的最後加上如下兩行 Include /etc/nginx/coreruleset/crs.conf Include /etc/nginx/coreruleset/rules/*.conf [root@Reverse-Proxy nginx]#
- 修改 Web Site 設定檔 /etc/nginx/conf.d/abc.conf
server { listen 80; listen [::]:80; server_name www.abc.io; root /usr/share/nginx/html; # 加入以下兩行 modsecurity on; modsecurity_rules_file /etc/nginx/modsecurity.conf; location/{ proxy_pass http://www.abc.io:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto; } }
- 修改 Web Site 設定檔 /etc/nginx/conf.d/xyz.conf
server { listen 80; listen [::]:80; server_name www.xyz.io; root /usr/share/nginx/html; # 加入以下兩行 modsecurity on; modsecurity_rules_file /etc/nginx/modsecurity.conf; location/{ proxy_pass http://www.xyz.io:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto; } }
- 重啟 nginx web server
systemctl restart nginx
重啟後如果一切順利,且可在 /var/log/nginx/ 目錄下看到 modsec_audit.log的記錄檔,這個檔案即是用戶端存取 Web Server 的違規記錄。
0 意見:
張貼留言