mod_security導入手順(簡易版)

Mod_Security

Apacheのmod_securityについてだいぶ前に検証しました。
ご参考まで。

環境:RedHat Enterprise Linux ES3.0
Apacheバージョン:httpd-2.0.46-44.ent

1. mod_security の最新版を下記よりダウンロードします。
http://www.modsecurity.org/download/index.html

2. tarを展開します。

[root@www zzz]# tar -xvzf modsecurity-1.8.7.tar.gz

3. 展開してできた apache もしくは apache2 ディレクトリに移動します。
(Apacheのバージョンが1.x系か2.x系かによって使用するモジュールが異なる点にご注意下さい。)

4. Apache の apxs を使用して mod_security.so を作成します。
Apacheが既にインストールされていてDSO(Dynamic Shared Object)が有効になっている場合、apxsコマンドを使ってモジュールを作成します。ほとんどのディストリビューションではApacheのDSOは有効になっていますので、この方法でインストールすることになるはずです。

apxs の場所は find コマンドなどで探してください。

[root@www modsecurity-1.8.7]# find / -name apxs
/usr/local/apache2/bin/apxs
[root@www modsecurity-1.8.7]# /usr/local/apache2/bin/apxs -cia mod_security.c

DSOが有効になっていない場合、下記のようなエラーが出力されます。

apxs:Error: Sorry, no DSO support for Apache available
apxs:Error: under your platform. Make sure the Apache
apxs:Error: module mod_so is compiled into your server
apxs:Error: binary `/usr/local/apache/bin/httpd’.

また、組み込まれているモジュールはapacheがインストールされている状態では下記コマンドにて確認が行えます。mod_securityを組み込むためにはDSOを有効にするため、mod_so.c が組み込まれている必要があります。(下記例では組み込まれておりませんので、Apacheのソースからの再コンパイルが必要となります。ご注意下さい。下記コマンドで再コンパイルを実施)

# ./configure –enable-module=so –enable-rule=SHARED_CORE

[root@www modsecurity-1.8.7]# /usr/local/apache2/bin/httpd -l
Compiled-in modules:
http_core.c
mod_env.c
mod_log_config.c
mod_mime.c
mod_negotiation.c
mod_status.c
mod_include.c
mod_autoindex.c
mod_dir.c
mod_cgi.c
mod_asis.c
mod_imap.c
mod_actions.c
mod_userdir.c
mod_alias.c
mod_access.c
mod_auth.c
mod_setenvif.c
suexec: disabled; invalid wrapper /usr/local/apache/bin/suexec

mod_security.so の作成が終われば下記のようなメッセージが表示されます。

Libraries have been installed in:
/usr/local/apache2/modules

また、httpd.conf に下記の一行が追記されます。

LoadModule security_module modules/mod_security.so

5. mod_security を有効化するため、httpd(apache)を再起動します。

[root@www httpd-2.0.54]# /usr/local/apache2/bin/apachectl stop
[root@www httpd-2.0.54]# /usr/local/apache2/bin/apachectl start

これで mod_security の組み込みは完了です。

6. mod_security によるフィルタ
フィルタルールはhttpd.confに記載します。下記はフィルタの例です。

フィルタ内容の意味については下記URLを参考にしました。
http://www.softek.co.jp/Sec/mod_security2.html
http://www.softek.co.jp/Sec/mod_security2.html
SecFilterEngine On
SecFilterScanPOST On
SecFilterDefaultAction “deny,log,status:406”
SecFilter /etc/passwd
SecFilterSelective REQUEST_METHOD “!(GET|POST|HEAD)”
SecAuditEngine RelevantOnly
SecAuditLog /var/log/apache2/audit.log

httpd.conf の変更を行った場合は必ずhttpdを再起動してください。

[補足]テスト

実はテストが一番難しいような気がしますが、mod_securityが有効かどうかのテストを実施する場合、一番簡単な方法は、
SecFilterEngine On
SecFilterScanOutput On
SecfilterSelective OUTPUT “Bad” deny,status:406
SecAuditEngine RelevantOnly
SecAuditLog /var/log/apache2/audit.log
SecFilter /etc/passwd

とした場合に、対象サーバの index.html?/etc/passwd などにアクセスしてみることです。

SecFilter /etc/passwd

上記のルールにて拒否していますので、audit.logに下記のようなレポートが出力されます。

[root@www conf]# more /var/log/apache2/audit.log
========================================
Request: xxx.xxx.xxx.xx – – [02/Aug/2005:16:53:42 +0900] “GET /test.html?/etc/passwd HTTP/1.1” 403 2
92
Handler: (null)
—————————————-
GET /test.html?/etc/passwd HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/v
nd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
Accept-Language: ja
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.
4322)
Host: www.xxxxxxx.com:8888
Connection: Keep-Alive
mod_security-message: Access denied with code 403. Pattern match “/etc/passwd” at THE_REQUEST
mod_security-action: 403

HTTP/1.1 403 Forbidden
Content-Length: 292
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

[amazonjs asin=”B00V7B1IZC” locale=”JP” title=”Hadoop: The Definitive Guide”]