tabimoba.net

とあるエンジニアの雑記帳

Apache+mod_geoip2で国単位でアクセスを遮断する

最近、某国からのアタック(不正アクセスの試み)やBlogへのコメントやトラックバックスパムが増えているように感じられます。

WAFやIPSなどを導入してアタックに対する影響を軽減する方法もありますが、それらの効果があったとしても、もし公開しているサイトが対象とするユーザーが日本国内や特定の国に限定される場合は、その国に限定して公開するのがよりセキュアであると考えます。

Apacheの場合、mod_geoip2モジュールを導入することで、国を限定してサイトの一部あるいは全体に対するアクセスの許可あるいは制限を行うことが可能です。

手順

以下の手順はCentOS6(x86_64版)をもとにしていますが、その他のディストリビューションにおいても、設定ファイルの場所や設定ファイル名が異なること以外ほぼ同じです。

1.libGeoIPをインストールする

ダウンロード先

GeoIP-latest.tar.gz

インストール手順
$ tar zxf GeoIP-latest.tar.gz
$ cd GeoIP-x.x.x
$ ./configure
$ make
$ su
# make install 

2.mod_geoip2モジュールをインストールする

ダウンロード先

mod_geoip2-latest.tar.gz

インストール手順
$ tar zxvf mod_geoip2-latest.tar.gz
$ cd mod_geoip2_x.x.x
$ su
# apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c

インストール後、httpd.conf(/etc/httpd/conf/httpd.conf)に以下の記述が追加されます。

LoadModule geoip_module       /usr/lib64/httpd/modules/mod_geoip.so

3.GeoIP Country database(GeoIP.dat)をダウンロードする

ダウンロード先

GeoIP.dat.gz

インストール手順

※以下の手順をシェルスクリプト化して、cronで1ヶ月に1回走らせると良いでしょう。

# gzip -d GeoIP.dat.gz
# mv GeoIP.dat /usr/share/GeoIP/

4.Apacheの設定ファイルを編集する

Apachehttpd.conf(/etc/httpd/conf/httpd.conf)または/etc/httpd/conf.dディレクトリ以下に設定ファイル(例:geoip.confなど)を作成し、以下の記述を追加します。

共通
<IfModule mod_geoip.c>
  GeoIPEnable On
  GeoIPOutput Env
  GeoIPDBFile /usr/share/GeoIP/GeoIP.dat MemoryCache
</IfModule>
条件別設定手順

SetEnvIf GEOIP_COUNTRY_CODE 国コード BlockCountryでブロックしたいアクセス元(国)を指定します。
国コードは、ISO 3166-1で定義された国コード(アルファベット2文字)が入ります。詳しくは、mod_geoip2の「GeoIP Region Edition Output Variables」をご参照ください。

▽Locationで指定

<Location "/hoge">
  Order Deny,Allow
  SetEnvIf GEOIP_COUNTRY_CODE ?? BlockCountry
  Deny from env=BlockCountry
</Location>

▽Directoryで指定

<Directory "/hoge">
  Order Deny,Allow
  SetEnvIf GEOIP_COUNTRY_CODE ?? BlockCountry
  Deny from env=BlockCountry
</Directory>

5.Apacheを再起動する

# /etc/init.d/httpd restart

6.ログを確認する

指定された国から該当するページへのアクセスが遮断することを確認します。
Apacheのerror_logには以下のように記録されます。

[Wed Nov dd hh:mm:ss yyyy] [error] [client xxx.xxx.xxx.xxx] client denied by server configuration:

日本以外からのアクセスを遮断する場合の設定

以下のようにGEOIP_COUNTRY_CODEを定義すると、該当するページ・ディレクトリに対する日本以外からのアクセスが遮断されます。

<Location "/hoge/">
Order allow,deny
SetEnvIf GEOIP_COUNTRY_CODE O1 BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AP BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AQ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AX BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE AZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BB BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BJ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE BZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CX BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE CZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DJ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE DZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE EC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE EE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE EG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE EH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ER BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ES BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ET BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE EU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FJ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE FR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GB BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GP BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GQ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE GY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE HU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ID BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IQ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE IT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE JE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE JM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE JO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KP BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE KZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LB BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE LY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ME BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ML BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MP BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MQ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MX BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE MZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NP BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE NZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE OM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE PY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE QA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SB BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SJ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ST BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE SZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TD BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TH BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TJ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TK BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TL BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TO BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TR BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TV BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TW BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE TZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE UA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE UG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE UM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE UY BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE UZ BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VC BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VG BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VI BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE VU BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE WF BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE WS BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE YE BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE YT BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ZA BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ZM BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE ZW BlockCountry
Deny from env=BlockCountry
Allow from all
</Location>

参考

ORBIT SPACE