WEBアプリケーションのユーザーが限定されていればiptablesやAWSのsecurity groupとかでIPフィルタリングしてしまうのですが、そうでない場合にhttpポートを全開放してしまうと、全世界からアタックし放題な状態になってしまいます。
海外からのアクセスブロックするとか、手段は他にも色々とありますが、今回はその一つであるWeb Application Firewallをインストールしてみます。
CentOS6.5で既にApacheが動いている状態でWeb Application Firewallであるmod_securityを入れてみますね。
インストール
こちらを参考にしました。yumでインストールしていきます。
http://oki2a24.com/2013/09/27/yum-install-epel-modsecurity/
どうやらepelリポジトリを使用すればより新しいバージョンのmod_securityがインストール出来るみたいですね。
epelリポジトリの追加方法については忘れてしまったので省略しますね。このあたりに載ってるかな。
http://qiita.com/muniere/items/6c4923a070cbbd824f39
mod_security をインストールします。
現時点で2.7.3がインストールされました。
1 |
$ sudo yum --enablerepo=epel install mod_security |
この時点ではまだ有効になっていません。
有効にするには mod_security_crs(Core Rule Set) のインストールが必要です。
1 2 |
$ sudo yum --enablerepo=epel install mod_security_crs $ sudo service httpd restart |
これでmod_securityが有効になりました。下記のようなパラメータを付与してアクセスしてみます。
http://your-own.appliation.com/?union+select
するとapacheのテストページに飛ばされました。ちゃんとフィルタリングされているみたいですね。
ログを確認すると、modsecurity_crs_41_sql_injection_attacks.conf の内容でフィルタリングされていることがわかるのでSQLインジェクションのルールでブロックされたということですね。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
$ sudo tail -f /var/log/httpd/modsec_audit.log --d807881a-A-- [03/Jun/2014:14:37:16 +0900] U41fDH8AAAEAAGWWbvQAAAAJ 183.180.128.104 52067 49.212.163.243 80 --d807881a-B-- GET /?union+select HTTP/1.1 Host: harada-atsushi.info User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:29.0) Gecko/20100101 Firefox/29.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: ja,en-us;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate DNT: 1 Cookie: _ga=GA1.2.936886617.1400933798 Connection: keep-alive Cache-Control: max-age=0 --d807881a-F-- HTTP/1.1 403 Forbidden Accept-Ranges: bytes Content-Length: 5039 Connection: close Content-Type: text/html; charset=UTF-8 --d807881a-H-- Message: Access denied with code 403 (phase 2). Pattern match "(?i:(?:(?:s(?:t(?:d(?:dev(_pop|_samp)?)?|r(?:_to_date|cmp))|u(?:b(?:str(?:ing(_index)?)?|(?:dat|tim)e)|m)|e(?:c(?:_to_time|ond)|ssion_user)|ys(?:tem_user|date)|ha(1|2)?|oundex|chema|ig?n|pace|qrt)|i(?:s(null|_(free_lock|ipv4_compat|ipv4_mapped|ipv4|ipv ..." at ARGS_NAMES:union select. [file "/etc/httpd/modsecurity.d/activated_rules/modsecurity_crs_41_sql_injection_attacks.conf"] [line "125"] [id "950001"] [rev "2"] [msg "SQL Injection Attack"] [data "Matched Data: union select found within ARGS_NAMES:union select: union select"] [severity "CRITICAL"] [ver "OWASP_CRS/2.2.6"] [maturity "9"] [accuracy "8"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"] [tag "WASCTC/WASC-19"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"] Action: Intercepted (phase 2) Stopwatch: 1401773836738804 31294 (- - -) Stopwatch2: 1401773836738804 31294; combined=1319, p1=281, p2=993, p3=0, p4=0, p5=44, sr=80, sw=1, l=0, gc=0 Producer: ModSecurity for Apache/2.7.3 (http://www.modsecurity.org/); OWASP_CRS/2.2.6. Server: Apache Engine-Mode: "ENABLED" --d807881a-Z-- |
SQLインジェクション以外にもデフォルトで多数のルールセットが有効になっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$ ls -letc/httpd/modsecurity.d/activated_rules 合計 88 lrwxrwxrwx 1 root root 64 6月 3 12:26 2014 modsecurity_35_bad_robots.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_35_bad_robots.data lrwxrwxrwx 1 root root 62 6月 3 12:26 2014 modsecurity_35_scanners.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_35_scanners.data lrwxrwxrwx 1 root root 69 6月 3 12:26 2014 modsecurity_40_generic_attacks.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_40_generic_attacks.data lrwxrwxrwx 1 root root 75 6月 3 12:26 2014 modsecurity_41_sql_injection_attacks.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_41_sql_injection_attacks.data lrwxrwxrwx 1 root root 62 6月 3 12:26 2014 modsecurity_50_outbound.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_50_outbound.data lrwxrwxrwx 1 root root 70 6月 3 12:26 2014 modsecurity_50_outbound_malware.data -> /usr/lib/modsecurity.d/base_rules/modsecurity_50_outbound_malware.data lrwxrwxrwx 1 root root 77 6月 3 12:26 2014 modsecurity_crs_20_protocol_violations.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_20_protocol_violations.conf lrwxrwxrwx 1 root root 76 6月 3 12:26 2014 modsecurity_crs_21_protocol_anomalies.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_21_protocol_anomalies.conf lrwxrwxrwx 1 root root 72 6月 3 12:26 2014 modsecurity_crs_23_request_limits.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_23_request_limits.conf lrwxrwxrwx 1 root root 69 6月 3 12:26 2014 modsecurity_crs_30_http_policy.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_30_http_policy.conf lrwxrwxrwx 1 root root 68 6月 3 12:26 2014 modsecurity_crs_35_bad_robots.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_35_bad_robots.conf lrwxrwxrwx 1 root root 73 6月 3 12:26 2014 modsecurity_crs_40_generic_attacks.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_40_generic_attacks.conf lrwxrwxrwx 1 root root 79 6月 3 12:26 2014 modsecurity_crs_41_sql_injection_attacks.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_41_sql_injection_attacks.conf lrwxrwxrwx 1 root root 69 6月 3 12:26 2014 modsecurity_crs_41_xss_attacks.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_41_xss_attacks.conf lrwxrwxrwx 1 root root 72 6月 3 12:26 2014 modsecurity_crs_42_tight_security.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_42_tight_security.conf lrwxrwxrwx 1 root root 65 6月 3 12:26 2014 modsecurity_crs_45_trojans.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_45_trojans.conf lrwxrwxrwx 1 root root 75 6月 3 12:26 2014 modsecurity_crs_47_common_exceptions.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_47_common_exceptions.conf lrwxrwxrwx 1 root root 82 6月 3 12:26 2014 modsecurity_crs_48_local_exceptions.conf.example -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_48_local_exceptions.conf.example lrwxrwxrwx 1 root root 74 6月 3 12:26 2014 modsecurity_crs_49_inbound_blocking.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_49_inbound_blocking.conf lrwxrwxrwx 1 root root 66 6月 3 12:26 2014 modsecurity_crs_50_outbound.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_50_outbound.conf lrwxrwxrwx 1 root root 75 6月 3 12:26 2014 modsecurity_crs_59_outbound_blocking.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_59_outbound_blocking.conf lrwxrwxrwx 1 root root 69 6月 3 12:26 2014 modsecurity_crs_60_correlation.conf -> /usr/lib/modsecurity.d/base_rules/modsecurity_crs_60_correlation.conf |
ちなみに追加のルールセットもインストールすることも出来ます。どのようなルールセットが追加されるかは今回は割愛しますね。
1 2 |
$ sudo yum --enablerepo=epel install mod_security_crs-extras $ sudo service httpd restart |
こんな感じに簡単に mod_securityが導入出来ました!
ただWAFの設定は非常に勘所が難しく、強化し過ぎるとアプリケーションに害を及ぼすし、緩くし過ぎると脆弱性のリスクが高まるので、設定は随時見なおしていくのがよいかと思います。
おまけ:ベンチマークとってみた
ついでですが、mod_securityはインストールした時点から、apacheのレスポンスが落ちるという情報があったのでベンチマークとってみました。ぼくの個人サイトで試してます。
before
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
$ ab -n 100 -c 10 http://harada-atsushi.info/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking harada-atsushi.info (be patient).....done Server Software: Apache Server Hostname: harada-atsushi.info Server Port: 80 Document Path: / Document Length: 27284 bytes Concurrency Level: 10 Time taken for tests: 21.931 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 2749100 bytes HTML transferred: 2728400 bytes Requests per second: 4.56 [#/sec] (mean) Time per request: 2193.108 [ms] (mean) Time per request: 219.311 [ms] (mean, across all concurrent requests) Transfer rate: 122.41 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.2 0 1 Processing: 1892 2182 108.4 2175 2433 Waiting: 781 885 67.8 878 1062 Total: 1892 2182 108.5 2175 2433 Percentage of the requests served within a certain time (ms) 50% 2175 66% 2235 75% 2280 80% 2306 90% 2321 95% 2336 98% 2411 99% 2433 100% 2433 (longest request) |
after
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
$ ab -n 100 -c 10 http://harada-atsushi.info/ This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking harada-atsushi.info (be patient).....done Server Software: Apache Server Hostname: harada-atsushi.info Server Port: 80 Document Path: / Document Length: 27284 bytes Concurrency Level: 10 Time taken for tests: 22.301 seconds Complete requests: 100 Failed requests: 0 Write errors: 0 Total transferred: 2749100 bytes HTML transferred: 2728400 bytes Requests per second: 4.48 [#/sec] (mean) Time per request: 2230.131 [ms] (mean) Time per request: 223.013 [ms] (mean, across all concurrent requests) Transfer rate: 120.38 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.3 0 1 Processing: 2009 2220 177.1 2168 2714 Waiting: 779 928 139.1 868 1247 Total: 2009 2220 177.2 2168 2714 Percentage of the requests served within a certain time (ms) 50% 2168 66% 2238 75% 2315 80% 2382 90% 2470 95% 2649 98% 2713 99% 2714 100% 2714 (longest request) |
before => Time taken for tests: 21.931 seconds
after => Time taken for tests: 22.301 seconds
すべてのリクエストが完了するまでの時間で0.4秒程の差が計測されました。カジュアルな測定ですが事実っぽいですね。

原田 敦

最新記事 by 原田 敦 (全て見る)
- Rails Engineでブログ機能追加するgemを作る - 2015年3月15日
- WEBエンジニア一人だけでサービスを作りきる方法-夫婦のための自動ごはん予定お知らせサービス「GoHaaan」制作でやったこと - 2015年3月7日
- CentOS6でMariaDBのDynamic Columnsを試してみた - 2015年2月28日