LoginSignup
5

More than 5 years have passed since last update.

[AWS][WordPress] WordPress のコアソースが改竄されていないかを CloudWatch のカスタムメトリクスで検知しよう

Last updated at Posted at 2016-08-03

wp-clicore verify-checksums コマンドを使うと、コアソースに改竄があるか確認できます。
それを元に CloudWatch のカスタムメトリクスに改竄されたファイル数を送って監視してしまおうというお話。

こんな感じのシェルスクリプトを作って、cron で5分ごとに回すといいんでないかなと思います。

wp-verify-checksums.sh
#!/bin/bash

# WordPress の Web ドキュメントルートディレクトリへのパス
wp_path='/path/to/wordpress'

# EC2 のインスタンス ID を取得
instanceid=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

# region を取得
az=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
_length=$(echo $((${#az} - 1)))
region=$(echo ${az} | cut -c 1-${_length})

# WP verify checksums
tmpfile=$(mktemp)
wp --path=${wp_path} core verify-checksums 2>&1 > /dev/null | grep -v "Success" > ${tmpfile}
err_count=$(cat ${tmpfile} | grep "Error" | wc -l)
warn_count=$(cat ${tmpfile} | grep "Warning" | wc -l)
rm -f ${tmpfile}

# put metric data
aws cloudwatch put-metric-data \
 --region ${region} \
 --namespace "WordPress" \
 --dimensions InstanceId=${instanceid} \
 --unit Count \
 --metric-name "WordPress verify checksums(Error)" \
 --value "${err_count}"
aws cloudwatch put-metric-data \
 --region ${region} \
 --namespace "WordPress" \
 --dimensions InstanceId=${instanceid} \
 --unit Count \
 --metric-name "WordPress verify checksums(Warning)" \
 --value "${warn_count}"

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5