Skip to content

Instantly share code, notes, and snippets.

@CLCL
Last active September 8, 2016 22:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CLCL/5894346 to your computer and use it in GitHub Desktop.
Save CLCL/5894346 to your computer and use it in GitHub Desktop.
CentOS 6で稼働しているdtpwiki.jpのWikiサイトに外国からのスパム書き込みが激しいので、GeoIPというIPアドレスから国を判別するApacheモジュールをインストールして、対応した。なお、mod_security使うと書き込み内容でBANすることができるのですが、あれけっこう設定がパラノイアなので、導入時にしっかりテストをする必要があるかな。

CentOS 6のApacheで外国のspamロボットのアクセスを制限する

すべて root で作業しています。

GeoIPとmod_geoipのインストール

GeoIP関連パッケージはEPELにありますので、あらかじめEPELの設定をしたうえで、

# yum --enablerepo=epel install GeoIP mod_geoip

これで、

  • IP判別データベース /usr/share/GeoIP/GeoIP.dat (←もうすでに古いデータ)
  • ユーティリティ(geoiplookupgeoiplookup6geoipupdate
  • Apache用国別判定モジュールの modules/mod_geoip.so
  • Apache用設定ファイル /etc/httpd/conf.d/geoip.conf

が入ります。

GeoIP.dat取得スクリプトを設置

GeoIPパッケージに含まれる geoipupdate を実行してみるとわかるが、IDを要求します。今回は、ID登録なしで使えるGeoLite Free Downloadable Databases(http://dev.maxmind.com/geoip/legacy/geolite/ )から取得するスクリプトを設置します。今のgeoipupdateは、引数なしでLite版の更新ができます。

# mkdir /root/bin
# vi /root/bin/geoipupdate.sh
#!/bin/sh

# geoipupdate.sh geoipupdateはID登録がいるので、
# 無償で使えるGeoLiteCountryをダウンロードする
# ダウンロードしたら、geoiplookup <IP_ADDRESS> で確認できる。

# GeoLiteCountryのURL
GEOIP_URL=http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz

# CentOS 6のGeoIP.dat の格納場所
GEOIP_PATH=/usr/share/GeoIP/GeoIP.dat
GEOIP_TEMP=/usr/share/GeoIP/GeoIP.dat.temp

curl -f $GEOIP_URL | zcat > $GEOIP_TEMP
if [ $? -ne 0 ]
then
    exit 1
fi
if [ -s $GEOIP_TEMP ]
then
    mv -f $GEOIP_TEMP $GEOIP_PATH
    exit 1
fi
exit 0
# chmod 744 /root/bin/geoipupdate.sh

GeoIP.dat取得スクリプトの月1回自動起動

個人的な好みで /etc/crontab に設定を書きます

# vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name comand to be executed

 00 09 02  *  * root      /root/bin/geoipupdate.sh

Apacheの設定ファイルでGeoIPによる判別を記述

個人的好みで、BlueOnyx風設定ファイルにバーチャルホストの設定を書いています。 この設定で、

  • 日本(JP)のアクセスは通す
  • 日本以外のアクセスの場合、UAが "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" 以外なら通します。

好ましくない書き込みをするrobotのUAが上記のものだったため。今どきのWin IE6でももうちょっとUAが長いので、これにマッチするのはrobotじゃねーかと考えています。

# vi /etc/httpd/conf.d/virtualhosts.conf
# dtpwiki.jp
<VirtualHost *:80>
    ServerAdmin webmaster@dtpwiki.jp
    DocumentRoot /home/www/wiki.dtpwiki.jp/html
    ServerName dtpwiki.jp
    CustomLog logs/access_log combined
    <Directory />
        Order Deny,Allow
        Deny from all
        <IfModule mod_geoip.c>
            GeoIPEnable On
            SetEnvIf GEOIP_COUNTRY_CODE JP PassCountry
        </IfModule>
        <IfModule !mod_geoip.c>
            SetEnvIf PassCountry
        </IfModule>
        BrowserMatch "Mozilla/4.0 \(compatible; MSIE 6.0; Windows NT 5.1; SV1\)" SpamRobotUA
        Allow from env=PassCountry
        Allow from env=!SpamRobotUA
    </Directory>
</VirtualHost>

おわり

あとは、Apacheを再起動します。

# /sbin/service httpd restart

IPを変えたり、UAを変えたりしてテストしましょう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment