ModSecurity(Web Application Firewall)の設定について(ソース版)



●ModSecurityのソースからのインストール

 参考URL:Apache×Mod SecurityでカンタンWAF構築
 参考URL:CentOS7 apacheにmod_securityの導入と設定
 参考URL:CentOS 7 ModSecurity 2.9.2のソースファイルからのインストール
 参考URL:ModSecurity入門

 CentOS 7のApacheを最新版(2.4.50)に変更するついでに、CentOS標準でインストールされているModSecurityもソース版(2.9.4)に変更します。
 apxs、apr-util、apxsのインストールはAPRのインストールAPR-utilのインストールを参照してください。
 ModSecurityの最新版をダウンロードし、インストールします。
# curl -O wget https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.4/modsecurity-2.9.4.tar.gz
# tar fxz modsecurity-2.9.4.tar.gz
# rm -f modsecurity-2.9.4.tar.gz
# yum install gcc-c++ flex bison yajl yajl-devel curl-devel curl GeoIP-devel doxygen zlib-devel pcre-devel
# cd modsecurity-2.9.4
# ./configure --prefix=/opt/modsecurity --with-apxs=/opt/httpd/bin/apxs --with-apr=/usr/local/src/apr-1.7.0 --with-apr-util=/usr/local/src/apr-util-1.6.1
# make
# make install
 mod_security2.soは/opt/httpd/modules/に作成されています。

 mod_security2.soのパーミッションを変更します。
# chmod 755 /opt/httpd/modules/mod_security2.so
 modsecurity.confをコピーします。
# cp modsecurity.conf-recommended /opt/httpd/conf/extra/modsecurity.conf
 Apacheのconfディレクトリに移動し、ルールセットを導入します。
# cd /opt/httpd/conf
# git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# cp -p owasp-modsecurity-crs/crs-setup.conf.example owasp-modsecurity-crs/crs-setup.conf
# mkdir -p /var/log/mod_security/data
# chown -R apache:apache /var/log/mod_security
 REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example及びRESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.exampleのファイル名を変更します。
# cd owasp-modsecurity-crs
# mv rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
# mv rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
 unicode.mappingをコピーします。
# cp /usr/local/src/modsecurity-2.9.4/unicode.mapping /opt/httpd/conf/unicode.mapping
 apacheディレクトリの所有者、グループを変更します(この作業は不要かもしれません)。
# chown -R apache:apache /opt/httpd
 httpd.confを編集します。
# vi /opt/httpd/conf/httpd.conf
<IfModule security2_module>
<IfModule unique_id_module>
  Include conf/extra/modsecurity.conf
  Include conf/owasp-modsecurity-crs/crs-setup.conf
  Include conf/owasp-modsecurity-crs/rules/*.conf
</IfModule>
</IfModule>
 次に、ModSecurityの設定ファイルであるmodsecurity.confを(ModSecurity(WAF、ソース)のインストール)と同様の設定値に編集します。
# vi /opt/httpd/conf/extra/modsecurity.conf
# systemctl restart httpd2450
(Apacheをソースからインストールし、起動スクリプトをhttpd2450で作成しているため)
 http://<サーバ名>?union+selectにアクセスして、Forbiddenと表示されれば問題なく動作しています。

●監査ログ

 監査ログはSecAuditLogTypeの設定値によって保存先が変わります。

 下記のように設定した場合
  SecAuditLogType concurrent
  SecAuditLog /var/log/mod_security/audit_log
  SecAuditLogStorageDir /var/log/mod_security/data
 concurrentはトランザクション毎に別々のファイルに書き込みます。
 /var/log/mod_security/audit_log及び/var/log/mod_security/data/以下となります。

# less /var/log/mod_security/audit_log (抜粋) www.bigbang.mydns.jp 192.241.198.139 - - [18/Oct/2021:08:47:29.833808 +0900] "GET /manager/html HTTP/1.1" 403 199 "-" "-" YWy2ERYrzMk663n4IF84xwAAAAQ "-" /apache/20211018/20211018-0847/20211018-084729-YWy2ERYrzMk663n4IF84xwAAAAQ 0 4394 md5:c793b3e96f5fd371732135db77ca7b83

# cat /var/log/mod_security/data/apache/20211018/20211018-0847/20211018-084729-YWy2ERYrzMk663n4IF84xwAAAAQ
--0ba24055-A--
[18/Oct/2021:08:47:29.833724 +0900] YWy2ERYrzMk663n4IF84xwAAAAQ 192.241.198.139 33510 ***.***.***.*** 80
--0ba24055-B--
GET /manager/html HTTP/1.1
Host: www.bigbang.mydns.jp
User-Agent: Mozilla/5.0 zgrab/0.x
Accept: */*
Accept-Encoding: gzip

--0ba24055-F--
HTTP/1.1 403 Forbidden
Strict-Transport-Security: max-age=15768000
Content-Length: 199
Content-Type: text/html; charset=iso-8859-1

--0ba24055-E--
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head<body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>

--0ba24055-H--
Message: Warning. Matched phrase "zgrab" at REQUEST_HEADERS:User-Agent. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf"] [line "56"] [id "913100"] [msg "Found User-Agent associated with security scanner"] [data "Matched Data: zgrab found within REQUEST_HEADERS:User-Agent: mozilla/5.0 zgrab/0.x"] [severity "CRITICAL"] [ver "OWASP_CRS/3.2.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-reputation-scanner"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "OWASP_CRS/AUTOMATION/SECURITY_SCANNER"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A7"] [tag "PCI/6.5.10"]
Message: Access denied with code 403 (phase 2). Operator GE matched 5 at TX:anomaly_score. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "93"] [id "949110"] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [severity "CRITICAL"] [ver "OWASP_CRS/3.2.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"]
Message: Warning. Operator GE matched 5 at TX:inbound_anomaly_score. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf"] [line "91"] [id "980130"] [msg "Inbound Anomaly Score Exceeded (Total Inbound Score: 5 - SQLI=0,XSS=0,RFI=0,LFI=0,RCE=0,PHPI=0,HTTP=0,SESS=0): individual paranoia level scores: 5, 0, 0, 0"] [ver "OWASP_CRS/3.2.0"] [tag "event-correlation"]
Apache-Error: [file "apache2_util.c"] [line 271] [level 3] [client 192.241.198.139] ModSecurity: Warning.
Matched phrase "zgrab" at REQUEST_HEADERS:User-Agent. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf"] [line "56"] [id "913100"] [msg "Found User-Agent associated with security scanner"] [data "Matched Data: zgrab found within REQUEST_HEADERS:User-Agent: mozilla/5.0 zgrab/0.x"] [severity "CRITICAL"] [ver "OWASP_CRS/3.2.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-reputation-scanner"] [tag "paranoia-level/1"] [tag "OWASP_CRS"] [tag "OWASP_CRS/AUTOMATION/SECURITY_SCANNER"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A7"] [tag "PCI/6.5.10"] [hostname "www.bigbang.mydns.jp"] [uri "/manager/html"] [unique_id "YWy2ERYrzMk663n4IF84xwAAAAQ"]
Apache-Error: [file "apache2_util.c"] [line 271] [level 3] [client 192.241.198.139] ModSecurity: Access denied with code 403 (phase 2). Operator GE matched 5 at TX:anomaly_score. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "93"] [id "949110"] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [severity "CRITICAL"] [ver "OWASP_CRS/3.2.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "www.bigbang.mydns.jp"] [uri "/manager/html"] [unique_id "YWy2ERYrzMk663n4IF84xwAAAAQ"]
Apache-Error: [file "apache2_util.c"] [line 271] [level 3] [client 192.241.198.139] ModSecurity: Warning. Operator GE matched 5 at TX:inbound_anomaly_score. [file "/opt/httpd/conf/owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf"] [line "91"] [id "980130"] [msg "Inbound Anomaly Score Exceeded (Total Inbound Score: 5 - SQLI=0,XSS=0,RFI=0,LFI=0,RCE=0,PHPI=0,HTTP=0,SESS=0): individual paranoia level scores: 5, 0, 0, 0"] [ver "OWASP_CRS/3.2.0"] [tag "event-correlation"] [hostname "www.bigbang.mydns.jp"] [uri "/manager/html"] [unique_id "YWy2ERYrzMk663n4IF84xwAAAAQ"]
Action: Intercepted (phase 2)
Stopwatch: 1634514449830791 2960 (- - -)
Stopwatch2: 1634514449830791 2960; combined=2131, p1=1269, p2=577, p3=0, p4=0, p5=193, sr=988, sw=92, l=0, gc=0
Response-Body-Transformed: Dechunked
Producer: ModSecurity for Apache/2.9.4 (http://www.modsecurity.org/); OWASP_CRS/3.2.0.
Server: Apache
Engine-Mode: "ENABLED"

--0ba24055-Z--

 下記のように設定した場合
  SecAuditLogType Sirial
  SecAuditLog /var/log/mod_security/audit_log
  SecAuditLogStorageDir /var/log/mod_security/data
 /var/log/mod_security/audit_logに全て書き込まれます。

●設定ファイル

 ModSecurityに関する設定ファイルは下記にあります。
・ModSecurityの設定ファイル
/opt/httpd/conf/extra/modsecurity.conf

・CRSの設定ファイル
/opt/httpd/conf/owasp-modsecurity-crs/crs-setup.conf

・CRS定義(アクティブなルール)
/opt/httpd/conf/owasp-modsecurity-crs/rules/*.conf
 CRS(Core Rule Set)とは、ModSecurityで使用するルール集です。
 例えば、クロスサイト・スクリプティングとSQLインジェクションの設定のみを有効化するには下記のようにします。
# vi /opt/httpd/conf/httpd.conf
<IfModule security2_module>
<IfModule unique_id_module>
  Include conf/extra/modsecurity.conf
  Include conf/owasp-modsecurity-crs/crs-setup.conf
  #Include conf/owasp-modsecurity-crs/rules/*.conf ← コメントアウト
  Include conf/owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf ← 新規(に直接指定して)適用
  Include conf/owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf ← 新規(に直接指定して)適用
</IfModule>
</IfModule>

# systemctl restart httpd2450
(Apacheをソースからインストールし、起動スクリプトをhttpd2450で作成しているため)


●Core Rule Set(CRS)

 ModSecurityには、攻撃を検出するためCore Rule Set(CRS)というルールセットが準備されています。このルールセットがシグネチャに該当します。
 CRSは、要求と応答の両方で脅威カテゴリごとにルールを有効にすることができます。
 また、URI・ヘッダー・Cookie・リクエストボディ・応答本文などの対象を指定することが可能です。
 ルールセット内には、偽陽性が発生した場合にそれぞれのルールを無効にするオプションがあります。
 偽陽性とは、「正常なHTTP通信」であるにもかかわらず「不正なHTTP通信」と判定されるエラーです。偽陽性が生じると、利用者の正常なHTTP通信がWAFで遮断されることになります。防御力はもちろんのこと、偽陽性が少ないということは、WAFの導入・運用において重要なポイントとなります。

 要求ルールセット

 要求ルールセットは、Webサーバーに対するHTTP要求を検査して不正アクセスがないかを判定するために用いられます。
  • Scanner detection(スキャナ検出)
  • Protocol enforcement(プロトコル強制)
  • Protocol attack(プロトコル攻撃)
  • Local file inclusion(ローカルファイルインクルージョン)
  • Remote file inclusion(リモートファイルインクルージョン)
  • Remote code execution(リモートコード実行)
  • PHP injectionPHP(インジェクション)
  • Cross-site scripting(クロスサイトスクリプティング)
  • SQL injection(SQL インジェクション)
  • Session fixation(セッション固定)
  • Session Java(Java攻撃)
 CRSを利用することで、SQLインジェクションやクロスサイトスクリプティング(XSS)、OSコマンドインジェクション等のWebアプリケーションにおける脆弱性を悪用する攻撃からWebサイトを保護することができます。

 応答ルールセット

 応答ルールセットには、下記のようなものがあります。
  • Data leakages(データ漏洩)
  • SQL data leakages(SQL データ漏洩)
  • Java data leakages(Java データ漏洩)
  • PHP data leakages(PHP データ漏洩)
  • IIS data leakages(IIS データ漏洩)
 応答ルールセットは、Webサーバーからの応答をチェックして、応答に存在すべきでない情報(SQLエラーやphpinfoの情報など)がないかを検査します。
 検出時は、遮断(ブロック)するだけでなく、攻撃を検出しても記録(ロギング)のみといったアクションを指定することができます。

●独自ルールの設定

 参考URL:Apache×Mod SecurityでカンタンWAF構築

 ローカルネットワーク等をWAFの監視から除外します。
# vi /opt/httpd/conf/owasp-modsecurity-crs/rules/rules-01.conf ← 新規作成
SecRule REMOTE_ADDR "@ipMatch 127.0.0.1" "phase:1,id:1,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
SecRule REMOTE_ADDR "@ipMatch 172.27.0.0/24" "phase:1,id:2,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"
SecRule REMOTE_ADDR "@ipMatch 192.168.0.0/24" "phase:1,id:3,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"

or

SecRule REMOTE_ADDR "@ipMatch 127.0.0.1,172.27.0.0/24,192.168.0.0/24" "phase:1,id:1,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"

# systemctl restart httpd2450
(Apacheをソースからインストールし、起動スクリプトをhttpd2450で作成しているため)


 ホワイトリスト

 IPアドレスのホワイトリストを利用する場合は下記のように記載します。
# vi /opt/httpd/conf/owasp-modsecurity-crs/rules/rules-01.conf
(下記行を追記)
SecRule REMOTE_ADDR "@pmFromFile whitelist_ip.txt" "phase:1,id:1,nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off"

# vi /opt/httpd/conf/owasp-modsecurity-crs/rules/whitelist_ip.txt ← 新規作成
127.0.0.1
172.27.0.8
192.168.0.17
※ただし、記載できるのはIPアドレスのみです。
 ネットワークアドレスは認識しません。

# systemctl restart httpd2450
(Apacheをソースからインストールし、起動スクリプトをhttpd2450で作成しているため)
  • nolog:ログを記録しない
  • allow:アクセスを許可
  • ctl:ruleEngine=Off:ルール検知しない
  • ctl:auditEngine=Off:監査ログ記録しない
 以上となります。

 ブラックリスト

 IPアドレスのホワイトリストを利用する場合は下記のように記載します。
# vi /opt/httpd/conf/owasp-modsecurity-crs/rules/rules-01.conf
(下記行を追記)
SecRule REMOTE_ADDR "@pmFromFile blacklist_ip.txt" "phase:1,id:2,deny,msg:'Blacklisted IP address'"

# vi /opt/httpd/conf/owasp-modsecurity-crs/rules/blacklist_ip.txt ← 新規作成
192.168.0.27
※ただし、記載できるのはIPアドレスのみです。
 ネットワークアドレスは認識しません。

# systemctl restart httpd2450
(Apacheをソースからインストールし、起動スクリプトをhttpd2450で作成しているため)
 以上となります。