end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

install modsecurity ver.2.9.3 (FOSS WAF) from source to apache 2.4 + centos8

ファイアウォール系の仕組みは、実運用に向けたルール選定や設定が非常に難しい。

特にWAFは単純導入だけでは、検知できないケースがあるだけでなく、過検知によりサービス不能に陥る。

2010年のIPAによる「オープンソースWAF「ModSecurity」導入事例 ~ IPA はこう考えた ~」公開も こういった背景があるのかと思います。

ただ、全く触ったこともないと、今後の議論や検討を継続できない為、以下、手習いメモ。

参考URL

install mod security

$ wget https://www.modsecurity.org/tarball/2.9.3/modsecurity-2.9.3.tar.gz
$ tar -xvf modsecurity-2.9.3.tar.gz
$ cd modsecurity-2.9.3
$ ./configure --prefix=/home/end0tknr/local/modsecurity \
              --with-apxs=/home/end0tknr/local/apache/bin/apxs \
              --with-apr=/home/end0tknr/local/apache/bin/apr-1-config \
              --with-apu=/home/end0tknr/local/apache/bin/apu-1-config
$ make
$ make check
$ make install 

$ cp modsecurity.conf-recommended \
     /home/end0tknr/local/apache/conf/extra/modsecurity.conf
$ cp unicode.mapping /home/end0tknr/local/apache/conf/extra/ ※1

※1 以下のようなerrorとなる為

$ ./bin/apachectl start
AH00526: Syntax error on line 219 of apache/conf/extra/modsecurity.conf:
Could not open unicode map file "apache/conf/extra/unicode.mapping"

install OWASP ModSecurity CRS(Core Rule Set)

こちらは、ダウンロード&解凍のみ

$ cd /home/end0tknr/local/apache/conf
$ wget https://github.com/SpiderLabs/owasp-modsecurity-crs/archive/v3.2.0.tar.gz
$ tar -xvf v3.2.0.tar.gz
$ ln -s owasp-modsecurity-crs-3.2.0 modsecurity
$ cp modsecurity/crs-setup.conf.example modsecurity/crs-setup.conf

config apache and mod_security

$ vi /home/end0tknr/local/apache/conf/httpd.conf
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so

<IfModule security2_module>
Include conf/extra/modsecurity.conf
</IfModule>
$ vi /home/end0tknr/local/apache/conf/extra/modsecurity.conf
old) /var/log/modsec_audit.log
new) logs/modsec_audit.log

old) #SecDebugLog /opt/modsecurity/var/log/debug.log
old) #SecDebugLogLevel 3
new) SecDebugLog logs/modsec_debug.log
new) SecDebugLogLevel 3

old) SecPcreMatchLimit 1000
old) SecPcreMatchLimitRecursion 1000
new) SecPcreMatchLimit 5000000
new) SecPcreMatchLimitRecursion 5000000 ※2

add) Include conf/modsecurity/crs-setup.conf 
add) Include conf/modsecurity/rules/*.conf ※3

※2 default値が小さく、以下のように error_log に「Execution error - PCRE limits exceeded (-8)」と表示された為

[:error] [pid 10784:tid 139973680060160] [client 192.168.63.1:42265] [client 192.168.63.1]
  ModSecurity: Rule 17ba660 [id "-"]
  [file "/home/end0tknr/local/apache/conf/modsecurity/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf"]
  [line "342"] - Execution error - PCRE limits exceeded (-8): (null). [hostname "cent80.a5.jp"]
  [uri "/Test/test.php"] [unique_id "Xsl37TWWTWnMNiW9xIdZDAAAAIQ"]```

※3 採用するルールは厳選が必要です


# 動作確認

modsecurity.conf で「SecRuleEngine DetectionOnly→On」と設定し、
「http://cent80.a5.jp:8080/test.php?union+select」のようにアクセスし、
「403 Forbidden」と表示されれば、modsecurityは動作しています。