約 3 分で読めます。
AWSのLightsailにWAF(Web Application Firewall)であるmod-securityを入れました。
予想に反してつまづいてしまったので、やり方を残しておきます。
セキュリティ対策はウィルス以外にもあるよね。
ブログのWordPressはWebアプリだけど、これらの対策はないかな?
こんな疑問を解決します。
WAFって何のことだよ、やらないと何か問題あるのかよ教えろよ、
という方はこちらの記事が参考になりますので、ぜひ。
Raspberry PiにWAFを入れた記事ですが、WAFの一般的な概念を解説しています。
mod-securityのインストール
ではさっそく本題です。
※LightsailインスタンスにWordPress Multisiteを選んだものとして解説します。
インスタンス起動時点でbitnamiによりapache2/WordPressが入っていますね。
mod-securityも入っています。
ただ、mod-security向けルールファイルはなぜか入っていないので、入れます。
その後にmod-securityを使用するようにhttpd.confを編集する、という具合です。
まずルールファイルのインストール。
※余談ですが、crsはCore Rule Setの略で、基本的な検出ルール集です。
sudo apt install modsecurity-crs
お次はbitnamiに入っているmod-securityの設定ファイル編集。
ログファイルの場所はご自身のお好きな場所でいいと思います。
特に保持しなくていいなら /tmp の下でも。
sudo vim /opt/bitnami/apache2/conf/modsecurity.conf # modsecurity.conf の末尾に以下を追記 SecAuditLog /opt/bitnami/apache2/logs/modsec_audit.log IncludeOptional /usr/share/modsecurity-crs/*.load # mod-security用のログファイルを作っておく sudo touch /opt/bitnami/apache2/logs/modsec_audit.log sudo chown bitnami:bitnami /opt/bitnami/apache2/logs/modsec_audit.log
初期状態ではSecRuleEngineがDetectionOnlyになっていると思います。
いきなりOnにすると、mod-securityの誤検出(フォールスポジティブ)により稼働中のサービスが影響を受ける可能性があります。
しばらくはDetectionOnlyにして様子を見ましょう。
設定 | 攻撃検出時の挙動 | 攻撃をapache error_logに記録するか |
SecRuleEngine On | HTTP 403 Forbidden | する |
SecRuleEngine DetectionOnly | (変更なし) | する |
検出した攻撃パターンはapache2のerror_logに記録されます。
Lightsailのbitnami環境だと以下にあります。
/opt/bitnami/apache2/logs/error_log
apache2からmod-securityを使うように設定します。
mod-securityはmod_unique_idを使うので、合わせて設定。
sudo vim /opt/bitnami/apache2/conf/httpd.conf # httpd.confの末尾に以下を追記 LoadModule unique_id_module modules/mod_unique_id.so LoadModule security2_module modules/mod_security2.so
最後にapacheを再起動させればmod-securityが組み込まれているはずです。
sudo /opt/bitnami/ctlscript.sh restart apache apachectl -M (略) security2_module (shared)
動作しているか確認してみましょう。
http://<ドメイン名 or IPアドレス>/?union+select にアクセスします。
error_logには以下の通り記録されていました。
Detects MSSQL code executionなので、SQLインジェクションを検出しています。
[Wed Mar 03 00:41:00.861258 2021] [:error] [pid 20781:tid 140209710642944] [client 124.219.163.155:44334] [client 124.219.163.155] ModSecurity: Warning. Pattern match "(?i:(?:[\"'](?:;?\\\\s* ?(?:having|select|union)\\\\b\\\\s*?[^\\\\s]|\\\\s*?!\\\\s*?[\\"'
\\w])|(?:c(?:onnection_id|urrent_user)|database)\\s?\\([^\\)]?|u(?:nion(?:[\\w(\\s]?select| select @)|ser\\s?
\\([^\\)]?)|s(?:chema\\s?\\([^\\)]?|elect.?\\w?user\\()|in …" at ARGS_NAMES:union select. [file "/usr/share/modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf"]
[line "193"] [id "942190"] [msg "Detects MSSQL code execution and information gathering attempts"] [data "Matched Data: union select found within ARGS_NAMES:union select: union select"] [severi
ty "CRITICAL"] [ver "OWASP_CRS/3.1.0"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-sqli"] [tag "OWASP_CRS/WEB_ATTACK/SQL_INJECTION"] [tag "WASCTC/WASC-1
9"] [tag "OWASP_TOP_10/A1"] [tag "OWASP_AppSensor/CIE1"] [tag "PCI/6.5.2"] [hostname "linuxfun.org"] [uri "/"] [unique_id "YD7bHLlvQ8mBaJ1EzMQ7RwAAAJo"]
[Wed Mar 03 00:41:00.863318 2021] [:error] [pid 20781:tid 140209710642944] [client 124.219.163.155:44334] [client 124.219.163.155] ModSecurity: Warning. Operator GE matched 5 at TX:anomaly_scor
e. [file "/usr/share/modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf"] [line "93"] [id "949110"] [msg "Inbound Anomaly Score Exceeded (Total Score: 5)"] [severity "CRITICAL"] [tag "a
pplication-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-generic"] [hostname "linuxfun.org"] [uri "/"] [unique_id "YD7bHLlvQ8mBaJ1EzMQ7RwAAAJo"]
終わりに
いかがでしたか。
このサイトでもmod-securityを導入済みですので、頃合いを見てerror_logを見て除外ルールの検討をやっていきます!
Comments