10月 04

mod_security2 による slow DoS 攻撃対策 (1) CentOS 5.8 に mod_security2 をインストール だけ する

先日はslowhttptestの威力により、当ブログサーバが陥落しサービス停止に陥りました。
このままではまずいです。そういうわけで…

 

本シリーズでは slowhttptest で試験することのできる4種類の slow DoS 攻撃から
mod_security2 を導入することで防衛することを目標にします。

 

しかし実際セットアップを試してみて、動くことは動きました。
でも、ぶっちゃけ mod_security2 の書き方は難解でした!
真面目に一から書く方法は僕もぜんぜんわかりません。

 

そこで、
本稿ではまず mod_security2 を CentOS 5.8 (x64) の標準的なモジュール構成による
Webサイトに導入 だけ して動作はさせない、というところまでを紹介します。
本稿では いったんすべての mod_security2 の設定は OFF にして、設定の仕方は
次回に回します。

今回試験した環境は
・CentOS 5.8
・Apache 2.2.3
・PHP 5.3.3
です。
前提条件として、epelレポジトリを有効にする必要がありますので、
続きを読む前にまず有効にしてください

 

まず、yum コマンドで mod_security2 のバージョンを確認します

[code.zobe.jp]# yum --enablerepo=epel info mod_security
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * rpmforge: ftp.riken.jp
 * updates: ftp.iij.ad.jp
epel                                                                                            | 3.7 kB     00:00
epel/primary_db                                                                                 | 3.8 MB     00:00
1752 packages excluded due to repository priority protections
Available Packages
Name       : mod_security
Arch       : x86_64
Version    : 2.6.7
Release    : 1.el5
Size       : 158 k
Repo       : epel
Summary    : Security module for the Apache HTTP Server
URL        : http://www.modsecurity.org/
License    : ASL 2.0
Description: ModSecurity is an open source intrusion detection and prevention engine
           : for web applications. It operates embedded into the web server, acting
           : as a powerful umbrella - shielding web applications from attacks.

 

本試験では 2.6.7 を使用します。最新版は http://www.modsecurity.org/ で確認することができます。

 

さっそくインストール…の前に、epel の mod_security2 は
同じく epel に入っている lua に依存しているので、
先にそちらをインストールします。
(手持ちの環境では、yumが依存関係を自動解決してくれませんでした)

 

[code.zobe.jp]# yum --enablerepo=epel install lua
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package lua.i386 0:5.1.4-4.el5 set to be updated
---> Package lua.x86_64 0:5.1.4-4.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                  Arch                        Version                          Repository                 Size
=======================================================================================================================
Installing:
 lua                      i386                        5.1.4-4.el5                      epel                      228 k
 lua                      x86_64                      5.1.4-4.el5                      epel                      229 k

Transaction Summary
=======================================================================================================================
Install       2 Package(s)
Upgrade       0 Package(s)

Total download size: 457 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): lua-5.1.4-4.el5.i386.rpm                                                                 | 228 kB     00:00
(2/2): lua-5.1.4-4.el5.x86_64.rpm                                                               | 229 kB     00:00
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  1.8 MB/s | 457 kB     00:00
警告: rpmts_HdrFromFdno: ヘッダ V3 DSA signature: NOKEY, key ID 217521f6
epel/gpgkey                                                                                     | 1.7 kB     00:00
Importing GPG key 0x217521F6 "Fedora EPEL <epel@fedoraproject.org>" from /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
Is this ok [y/N]: y
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : lua                                                                                             1/2
  Installing     : lua                                                                                             2/2

Installed:
  lua.i386 0:5.1.4-4.el5                                    lua.x86_64 0:5.1.4-4.el5

Complete!

 

ちなみに当サーバでは、何故か rpmforge レポジトリの lua が既にインストールされていて
競合してしまっていたため、それをいったんアンインストールしました。

また環境によっては、他にも必要になるものがあると思います。
http://www.yomaigoto.jp/archives/188
さんの情報だと、httpd-devel や pcre-devel が必要になるようです。

 

…さて、必要なモジュールのインストールが終わったら、
いよいよ mod_security2 のインストールです。

 

ここから先は休憩なしで実行してください。
なんでかというと、途中で休憩をしてしまうと、
デフォルトでインストールされる mod_security.conf が機能してしまうからです。
本稿の目的は mod_security2 モジュールのインストールまでであり、
システムに悪影響を及ぼすことのない安全な設定ファイルを作成するのは次稿からだからです。

 

ちなみに、インストールして、デフォルトの設定ファイルを有効にして httpd を起動すると、
いろいろ動かないものが出たりして困る かも しれないです(試してないけど)。
そうなっても僕は知りませんよ。

 

mod_security2 はインストールする。
でも、なんの機能も有効にしない。
という状態まで、本稿ではもっていきます。

 

心の準備ができたなら、ここから先の作業に踏み込みます。

 

[code.zobe.jp]# yum --enablerepo=epel install mod_security
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mod_security.x86_64 0:2.6.7-1.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================
 Package                        Arch                     Version                          Repository              Size
=======================================================================================================================
Installing:
 mod_security                   x86_64                   2.6.7-1.el5                      epel                   158 k

Transaction Summary
=======================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 158 k
Is this ok [y/N]: y
Downloading Packages:
mod_security-2.6.7-1.el5.x86_64.rpm                                                             | 158 kB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : mod_security                                                                                    1/1

Installed:
  mod_security.x86_64 0:2.6.7-1.el5

Complete!

 

インストールは無事に済みました。

あとは、自動的につくられる設定ファイルを無効化します。
(CentOS 5.X のファイル配置とポリシーに従った、独自の方法です。他のディストリビューションの場合はまた変わるでしょう)

まず、/etc/httpd/conf.d/mod_security.conf に書かれている2行の LoadModule 宣言を
/etc/httpd/conf/httpd.conf に移動します。

httpd.conf の
「The following modules are not loaded by default:」
と書いてある行のうしろに、これを移します。

LoadModule unique_id_module modules/mod_unique_id.so
LoadModule security2_module modules/mod_security2.so

 

次に、

mv /etc/httpd/conf.d/mod_security.conf /etc/httpd/conf.d/mod_security.conf.original

のようにして、デフォルトで設置されちゃう設定ファイルを無効化します。

代わりに、次のようなからっぽの設定をつくります。

[code.zobe.jp]# vi /etc/httpd/conf.d/mod_security.conf

↓設定内容はこれだけ

<IfModule mod_security2.c>
</IfModule>

 

また、mod_security2 で使用することになる
mod_reqtimeout をついでに有効にしておきます。

CentOS 5.8 の Apache ならば、既に mod_reqtimeout.so があるはずですので、
/etc/httpd/conf/httpd.conf で次の行を有効にしておきましょう。
なければ追記してください。

LoadModule reqtimeout_module modules/mod_reqtimeout.so

 

ここまでできたら、httpd を再起動して、
mod_security2 が組み込まれていることを確認してみましょう。

service httpd restart

 

あとは

httpd -M

としたときに

reqtimeout_module (shared)
unique_id_module (shared)
security2_module (shared)

のように出てくれば成功です。

 

次回は mod_security2の設定ファイルのセット 「ModSecurity Core Rule Set」の導入方法について紹介します。

コメントを残す

Your email address will not be published.