httpdのmod_securityルール作成を考える。

httpdでOWASP(mod_securityによるWAF)を導入するときにいつも困るのが偽陽性の判定チェックと、都度ルールを記載してhttpdリスタート、ということを繰り返すのですが、世の中にはこのルールつくりを簡単にしてくれるスクリプトを出しておられる方もいます。

概要は
(1)
httpdで、mod_securityを導入し、DetectionOnlyとしてログを出力しておきます。

[/etc/httpd/conf.d/mod_security.conf]
SecRuleEngine DetectionOnly

(2)
/var/log/httpd/error.log にmod_securityによるワーニングがたくさん記述されるようになったことを確認します。

(3)
自分で、WordPressなりサイト内のあらゆるsubmitをしたり、操作してログを残します。
このとき、自分のリモートホストを控えておきましょう。

(4)
次のサイトから、スクリプトを拝借します。
[Apache ModSecurity Whitelist Generator Script]
https://samhobbs.co.uk/2014/05/apache-modsecurity-whitelist-generator-script

(5)
スクリプトの編集するポイントは、このあたり。
FRIENDLY_IPのIPを元に、ルールを作成します。(これが正しいアクセス、とみなすわけです。)


[36行目:(3)でアクセスしたリモートホストのIPを記載します。]
FRIENDLY_IP="192.168.1.1"

[49行目:ルールを作成するコンテンツのパスを記載します。WordPressなら、wp-admin/ など]
SPECIAL_LOCATIONS=".....

[136行目付近:使用しているログによっては、読み飛ばされてしまうので、読む拡張子を読ませるように]
echo "Processing error logs..."
for f in $LOG_DIR/*; do
if [[ "$f" =~ \.gz$ ]]; then
echo "> reading $f"
zcat $f >> $TEMPFILE1
elif [[ "$f" =~ \.log ]]; then
echo "> reading $f"
cat $f >> $TEMPFILE1
else

(6)
スクリプトを実行すると、指定したフォルダに下記のようなファイルが出来上がります。
whoitelist がmod_securityのルールを除外するための記述になります。
もし、除外するLocationMatchがたりなさそうな時は、problem_locations_remainingを参照し、
スクリプト内にあらためてこのパスを入れて再度実行すると良いと思います。

-rw------- 1 root root 56307 4月 26 22:17 2018 combined_log
-rw-r--r-- 1 root root 36 4月 26 22:17 2018 grouped_locations
-rw-r--r-- 1 root root 51 4月 26 22:17 2018 id_1
-rw-r--r-- 1 root root 9 4月 26 22:17 2018 id_2
-rw-r--r-- 1 root root 9 4月 26 22:17 2018 id_3
-rw-r--r-- 1 root root 12 4月 26 22:17 2018 location_1
-rw-r--r-- 1 root root 13 4月 26 22:17 2018 location_2
-rw-r--r-- 1 root root 11 4月 26 22:17 2018 location_3
-rw-r--r-- 1 root root 36 4月 26 22:17 2018 problem_locations
-rw-r--r-- 1 root root 0 4月 26 22:17 2018 problem_locations_remaining
-rw-r--r-- 1 root root 3 4月 26 22:17 2018 root_ids
-rw-r--r-- 1 root root 0 4月 26 22:17 2018 root_log
-rw-r--r-- 1 root root 706 4月 26 22:17 2018 whitelist

あとは偽陽性が出ないか運用してみて、mod_securityの「SecRuleEngine On」とする運用をできれば運用上支障の少ないWAFとして動作させることができるでしょうか。