WAF(mod_security)を導入する
WAF(Web Application Firewall)について調べていたら、Apacheのモジュールに mod_security というよさげなものがあることを知った。早速公式サイトに行ってみたら、トップページでオライリー本の「Apache Security」に解説があると謳っており、日本語訳も出版されていたので買ってきた。インストール方法と設定方法が丁寧に解説されていたのでその通りにやってみると、インストール一発目のコマンドからきれいにすっこけた。よくよく確認してみると、オライリー本は v1.x 準拠、mod_security の最新版は v2.5.12だった。インストール方法からして、既に根本から変更されているようだ。ぶわっかやろぉ。ま、本代は会社からせびったからいいんだけど。
ということで、試行錯誤した記録。
ソースコードは上記公式サイトから入手。ソースコードからインストールするには、 httpd-devel パッケージと pcre-devel パッケージが必要。
# rpm -qa | grep httpd httpd-2.2.3-43.el5.centos httpd-manual-2.2.3-43.el5.centos # rpm -qa | grep pcre pcre-6.6-2.el5_1.7 # yum -y install httpd-devel pcre-devel Dependencies Resolved ========================================================================================== Package Arch Version Repository Size ========================================================================================== Installing: httpd-devel i386 2.2.3-43.el5.centos base 148 k httpd-devel x86_64 2.2.3-43.el5.centos base 148 k pcre-devel i386 6.6-2.el5_1.7 base 176 k pcre-devel x86_64 6.6-2.el5_1.7 base 179 k Installing for dependencies: apr-devel x86_64 1.2.7-11.el5_3.1 base 237 k apr-util-devel x86_64 1.2.7-11.el5 base 53 k cyrus-sasl-devel x86_64 2.1.22-5.el5_4.3 base 1.4 M openldap-devel x86_64 2.3.43-12.el5 base 1.6 M pcre i386 6.6-2.el5_1.7 base 112 k Transaction Summary ========================================================================================== Install 9 Package(s) Upgrade 0 Package(s)
ソースを展開してインストールする。v2.x 系は apxs コマンド使わない。
# tar xvfz modsecurity-apache_2.5.12.tar.gz # cd modsecurity-apache_2.5.12/apache2 # ./configure # make # make test All tests passed (576). # make install
FireWallというからにはポリシールールが必要。頑張ってゼロから自分で書く、という根性はない。公式サイトからリンクされている Modsecurity Core Rules をありがたく使わせてもらうことにする。
# tar xvfz modsecurity-crs_2.0.7.tar.gz -C /etc/httpd/conf/ # ln -s /etc/httpd/conf/modsecurity-crs_2.0.7 /etc/httpd/conf/modsecurity-crs
mod_security を組み込むには、同時に mod_unique_id を有効にする必要がある。デフォルトでは無効なので追加しておく。ログの設定は見よう見まねで適当に(をい)。
# vi /etc/httpd/conf.d/security.conf
[/etc/httpd/conf.d/security.conf]
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so
SecDataDir /var/log/httpd/data
SecAuditEngine RelevantOnly
SecAuditLog /var/log/httpd/audit/audit.log
SecAuditLogParts ABCFHZ
SecAuditLogType concurrent
SecAuditLogStorageDir logs/audit
SecAuditLogRelevantStatus ^(?:5|4\d[^4])
<IfModule security2_module>
Include conf/modsecurity-crs/*.conf
Include conf/modsecurity-crs/base_rules/*.conf
</IfModule>
ログ用のディレクトリを掘る。ローテーションの設定もしておく。
# mkdir -p /var/log/httpd/audit
# mkdir -p /var/log/httpd/data
# chown apache:apache /var/log/httpd/audit
# chown apache:apache /var/log/httpd/data
# vi /etc/logrotate.d/httpd
[/etc/logrotate.d/httpd]
1c1
< /var/log/httpd/*log {
---
> /var/log/httpd/*log /home/httpd/audit/audit.log {
■apache再起動
# service httpd restart [error_log] [Sat Jul 17 20:45:12 2010] [notice] caught SIGTERM, shutting down [Sat Jul 17 20:45:13 2010] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec) [Sat Jul 17 20:45:14 2010] [notice] ModSecurity for Apache/2.5.12 (http://www.modsecurity.org/) configured. [Sat Jul 17 20:45:14 2010] [notice] Digest: generating secret for digest authentication ... [Sat Jul 17 20:45:14 2010] [notice] Digest: done [Sat Jul 17 20:45:14 2010] [notice] mod_python: Creating 4 session mutexes based on 256 max processes and 0 max threads. [Sat Jul 17 20:45:15 2010] [notice] Apache/2.2.3 (CentOS) configured -- resuming normal operations
たぶん動いてるのではなかろうかと。