fake_bot を修正
以前、gogglebot や bingbot を詐称するホストを、.htacces で拒否するようにした SHELL スクリプトを作りました
一部を修正しました。 修正したのは、以下の色を付けた部分です。
(2022.02.27 追記) 以下のスクリプトに不具合があることがわかりました。使用しないでください。
#!/bin/bash trap 'rm -f ${tmpfile}; exit 1' 1 2 3 15 tmpfile=/tmp/fake_bot$$ if [ -f "${tmpfile}" ]; then rm -f "${tmpfile}" fi edit_htaccess () { newip=$1 htaccess_list=".htaccess blog/wordpress/.htaccess" for htaccess in ${htaccess_list} do target="/var/www/sirius10/${htaccess}" if ! grep "${newip}" "${target}" > /dev/null then echo "Add new deny ${newip} to ${htaccess}." sed -e "s/^\(<\/RequireAll>\)$/ Require not ip ${newip}\n\1/" -i "${target}" fi done } apache_log="ログファイル名" safe_domain="googlebot.com" ua="Googlebot" if [ $# -eq 2 ]; then ua=$1 safe_domain=$2 elif [ $# -ne 0 ]; then echo "Usage: $0 [<ua> <safe domain>]" exit fi for log in ${apache_log} do grep "${ua}" /var/log/apache2/${log} | sed -e 's/\(^.*\) - - \[.*/\1/' | grep -v "${safe_domain}$" >> ${tmpfile} done for ip in $(sort -u ${tmpfile}) do if ! expr "${ip}" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*' > /dev/null then domain=${ip} ina=$(dig "${domain}" | grep '^[^;].* IN A ') ip=${ina##*IN A} ip=$(echo ${ip}) else ptr=$(dig -x ${ip} | grep "^[^;].*PTR") domain=${ptr##*PTR} domain=$(echo ${domain}) fi if [ "${domain}" != "" ]; then if ! echo ${domain} | grep "${safe_domain}.$" >/dev/null then echo "Fake ${ua} ${ip} ${domain}" edit_htaccess ${ip} fi else echo "Fake $ua ${ip}" edit_htaccess ${ip} fi done if [ -f "${tmpfile}" ]; then rm -f "${tmpfile}" fi
正規のドメイン以外でも Apache で名前解決できたものが有ったので、それに対処しました。Fake bot はそれほど多くないのか、拒否している IP は今の所 7 つだけです。
ディスカッション
コメント一覧
まだ、コメントがありません