VPS(CentOS6.5 64bit)のhttpdのmod_securityを導入してみたメモ。
インストール
ソースをDLしてapxsでコンパイルとインストールすればいいのだが、ここはあえてyumでインストール。
epelを利用できるようにしておく必要がある。
# yum install mod_security mod_security_crs
インストール後、httpdを再起動する。
# /etc/init.d/httpd restart
mod_security及びCRS(Core Rule Set)の動作確認
IP直打ちでのアクセスはデフォルトで拒否する設定なので、
$ curl -v http://127.0.0.1/
として、
* About to connect() to 127.0.0.1 port 80 (#0) * Trying 127.0.0.1... connected * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2 > Host: 127.0.0.1 > Accept: */* > < HTTP/1.1 403 Forbidden < Date: Fri, 27 Dec 2013 10:10:53 GMT < Server: Apache < Content-Length: 202 < Connection: close < Content-Type: text/html; charset=iso-8859-1 < … * Closing connection #0
と拒否応答が返り、/var/log/httpd/modsec_audit.logに
--e6314230-A-- … --e6314230-H-- Message: Access denied with code 403 (phase 2). Pattern match "^[\\d.:]+$" at REQUEST_HEADERS:Host. [file "/etc/httpd/modsecurity.d/activated_rules/modsecurity_crs_21_protocol_anomalies.conf"] [line "98"] [id "960017"] [rev "2"] [msg "Host header is a numeric IP address"] [data "127.0.0.1"] [severity "WARNING"] [ver "OWASP_CRS/2.2.6"] [maturity "9"] [accuracy "9"] [tag "OWASP_CRS/PROTOCOL_VIOLATION/IP_HOST"] [tag "WASCTC/WASC-21"] [tag "OWASP_TOP_10/A7"] [tag "PCI/6.5.10"] [tag "http://technet.microsoft.com/en-us/magazine/2005.01.hackerbasher.aspx"] Action: Intercepted (phase 2) Stopwatch: 1388139053365342 1528 (- - -) Stopwatch2: 1388139053365342 1528; combined=370, p1=245, p2=89, p3=0, p4=0, p5=35, sr=71, sw=1, l=0, gc=0 Response-Body-Transformed: Dechunked Producer: ModSecurity for Apache/2.7.3 (http://www.modsecurity.org/); OWASP_CRS/2.2.6. Server: Apache Engine-Mode: "ENABLED" --e6314230-Z--
みたいなのが記録されれば正常。
注意
自分が使っているDokuWikiは、mod_security(CRSインストール済)との相性が悪いので、SQLインジェクションなどのルールを無効化しておく必要がある。
httpd.confのDirectoryディレクティブ内などで除外設定(SecRuleRemoveById,SecRuleRemoveByTag)すれば編集時にmod_securityに引っかからなくなる。除外設定するタグ/IDは誤作動した時のログ(/var/log/httpd/modsec_audit.log)などを参考に。
他のスクリプトでも誤作動引き起こすことが結構ある(ID 960024など)のでmod_securityの設定なども調整しておいたほうがいいかもしれない。
<VirtualHost 10.20.30.40:80> ServerName example.com … <Directory /> Options All AllowOverride All SecRuleRemoveByTag OWASP_CRS/WEB_ATTACK/SQLI SecRuleRemoveByTag OWASP_CRS/WEB_ATTACK/SQL_INJECTION SecRuleRemoveByTag OWASP_CRS/WEB_ATTACK/XSS SecRuleRemoveById 960024 SecRuleRemoveById 981173 SecRuleRemoveById 973300 </Directory> … </VirtualHost>