IT井戸端会議

IT井戸端会議

インフラ、ネットワーク、アプリケーション開発、IT界隈の話等々を東京都千代田区界隈から発信します。

Apache DDoS 対応

数日〜数週間ごとにWebサーバにアクセスできなくなる事象が発生しており、 OS再起動で暫定対応していたが、 あらためて、真面目に調査してみた。

OOM Killer によって Apacheが停止している。

# grep "Out of memory" messages* | grep "Aug 17 12:" | head
messages:Aug 17 12:31:33 hoge kernel: Out of memory: Kill process 1187 (java) score 89 or sacrifice child
messages:Aug 17 12:31:55 hoge kernel: Out of memory: Kill process 12921 (httpd) score 26 or sacrifice child
messages:Aug 17 12:32:05 hoge kernel: Out of memory: Kill process 29707 (httpd) score 25 or sacrifice child
messages:Aug 17 12:32:16 hoge kernel: Out of memory: Kill process 12924 (httpd) score 24 or sacrifice child
messages:Aug 17 12:32:28 hoge kernel: Out of memory: Kill process 18440 (httpd) score 24 or sacrifice child
messages:Aug 17 12:32:42 hoge kernel: Out of memory: Kill process 12922 (httpd) score 23 or sacrifice child
messages:Aug 17 12:32:54 hoge kernel: Out of memory: Kill process 29706 (httpd) score 23 or sacrifice child
messages:Aug 17 12:33:12 hoge kernel: Out of memory: Kill process 12920 (httpd) score 22 or sacrifice child
messages:Aug 17 12:33:21 hoge kernel: Out of memory: Kill process 12926 (httpd) score 22 or sacrifice child
messages:Aug 17 12:33:41 hoge kernel: Out of memory: Kill process 29705 (httpd) score 22 or sacrifice child

Apacheアクセスログを確認すると、特定IPアドレスからのアクセスが大量にあることが見て取れる。

# cat access_log | grep 202.215.15.8 | awk '{print $4}' | grep "17/Aug/2015:12:1" | wc -l
452

ググるとコレくさい

http://itpro.nikkeibp.co.jp/article/NEWS/20110831/367976/

CentOS のデフォルトの yum リポジトリで取得できる Apache だとバージョンが低いらしい。 ということで、yum リポジトリを追加して対応。

アップデート前の Apache のバージョンを確認

# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Oct 16 2014 14:48:21

centalt リポジトリを追加します。

# rpm -Uvh http://mirror.sysadminguide.net/centalt/repository/centos/6/x86_64/centalt-release-6-1.noarch.rpm

baseurlを書き換えます。

# vi /etc/yum.repos.d/centalt.repo
-baseurl=http://centos.alt.ru/repository/centos/6/$basearch/
+baseurl=http://mirror.sysadminguide.net/centalt/repository/centos/6/$basearch/

yum を一旦初期化

# yum clean all

Apache をアップデートします。

# yum update httpd httpd-devel
読み込んだプラグイン:fastestmirror, security
更新処理の設定をしています
Determining fastest mirrors
epel/metalink                                                                                  | 3.0 kB     00:00     
 * base: ftp.tsukuba.wide.ad.jp
 * epel: ftp.kddilabs.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: ftp.tsukuba.wide.ad.jp
CentALT                                                                                        | 2.9 kB     00:00     
CentALT/primary_db                                                                             | 200 kB     00:00     
base                                                                                           | 3.7 kB     00:00     
base/primary_db                                                                                | 4.6 MB     00:00     
elasticsearch-1.3                                                                              |  951 B     00:00     
elasticsearch-1.3/primary                                                                      | 1.9 kB     00:00     
elasticsearch-1.3                                                                                               10/10
epel                                                                                           | 4.4 kB     00:00     
epel/primary_db                                                                                | 6.7 MB     00:00     
extras                                                                                         | 3.4 kB     00:00     
extras/primary_db                                                                              |  26 kB     00:00     
mondorescue                                                                                    | 1.3 kB     00:00     
mondorescue/primary                                                                            |  41 kB     00:01     
mondorescue                                                                                                   165/165
updates                                                                                        | 3.4 kB     00:00     
updates/primary_db                                                                             | 1.3 MB     00:00     
パッケージ httpd-devel は利用できますが、インストールしませんでした。
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> Package httpd.x86_64 0:2.2.15-39.el6.centos will be 更新
---> Package httpd.x86_64 0:2.2.27-1.el6 will be an update
--> 依存性の処理をしています: httpd-tools = 2.2.27-1.el6 のパッケージ: httpd-2.2.27-1.el6.x86_64
--> トランザクションの確認を実行しています。
---> Package httpd-tools.x86_64 0:2.2.15-39.el6.centos will be 更新
---> Package httpd-tools.x86_64 0:2.2.27-1.el6 will be an update
--> 依存性解決を終了しました。

依存性を解決しました

======================================================================================================================
 パッケージ                   アーキテクチャ          バージョン                       リポジトリー              容量
======================================================================================================================
更新:
 httpd                        x86_64                  2.2.27-1.el6                     CentALT                  887 k
依存性関連での更新をします。:
 httpd-tools                  x86_64                  2.2.27-1.el6                     CentALT                   62 k

トランザクションの要約
======================================================================================================================
アップグレード       2 パッケージ

総ダウンロード容量: 950 k
これでいいですか? [y/N]y
パッケージをダウンロードしています:
(1/2): httpd-2.2.27-1.el6.x86_64.rpm                                                           | 887 kB     00:01     
(2/2): httpd-tools-2.2.27-1.el6.x86_64.rpm                                                     |  62 kB     00:00     
----------------------------------------------------------------------------------------------------------------------
合計                                                                                  536 kB/s | 950 kB     00:01     
rpm_check_debug を実行しています
トランザクションのテストを実行しています
トランザクションのテストを成功しました
トランザクションを実行しています
警告: RPMDB は yum 以外で変更されました。
  更新                    : httpd-tools-2.2.27-1.el6.x86_64                                                       1/4 
  更新                    : httpd-2.2.27-1.el6.x86_64                                                             2/4 
警告: /etc/httpd/conf/httpd.conf は /etc/httpd/conf/httpd.conf.rpmnew として作成されました。
  整理中                  : httpd-2.2.15-39.el6.centos.x86_64                                                     3/4 
  整理中                  : httpd-tools-2.2.15-39.el6.centos.x86_64                                               4/4 
  Verifying               : httpd-tools-2.2.27-1.el6.x86_64                                                       1/4 
  Verifying               : httpd-2.2.27-1.el6.x86_64                                                             2/4 
  Verifying               : httpd-2.2.15-39.el6.centos.x86_64                                                     3/4 
  Verifying               : httpd-tools-2.2.15-39.el6.centos.x86_64                                               4/4 

更新:
  httpd.x86_64 0:2.2.27-1.el6                                                                                         

依存性を更新しました:
  httpd-tools.x86_64 0:2.2.27-1.el6                                                                                   

完了しました!

httpd プロセスが /etc/init.d/httpdコマンドで落とせない状態となってしまっていたので、 OSごとrebootで再起動

# /etc/init.d/httpd restart
httpd を停止中:                                            [失敗]
httpd を起動中: [Tue Aug 18 01:38:29 2015] [warn] module proxy_ajp_module is already loaded, skipping
(98)Address already in use: make_sock: could not bind to address [::]:80
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs

# reboot

無事に Apache のアップデートが完了しました。

# httpd -v
Server version: Apache/2.2.27 (Unix)
Server built:   Apr  9 2014 22:08:12

今後のことを考えて、スワップも作成しておく。

1GBのスワップ用のファイルを作成

# dd if=/dev/zero of=/var/tmp/swap0 bs=1024 count=1048576

作成したスワップ用ファイルの属性を変更

# mkswap /var/tmp/swap0

スワップ用のファイルをスワップとして登録

# swapon /var/tmp/swap0

設定されたことを確認

# cat /proc/swaps
Filename                Type        Size    Used    Priority
/dev/vda2                               partition   2097148 68440   -1
/var/tmp/swap0                          file        1048572 0   -2

OSを再起動した時に適用されるよう、fstabに追記

# vi /etc/fstab
+/var/tmp/swap0          none                    swap    defaults        0 0

あとは、しばらく様子見ですかね。