LoginSignup
12
16

More than 5 years have passed since last update.

GeoIPを使って国別にiptablesで接続制御

Last updated at Posted at 2014-12-22

iptablesを使って国ごとにアクセス制限をする際にGeoIPを用いると簡単に設定ができます。
環境はCentOS6です。

インストールの準備

yum install gcc gcc-c++ make automake unzip zip xz kernel-devel-`uname -r` iptables-devel
rpm -ivh ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/6.1/x86_64/updates/security/kernel-devel-2.6.32-279.14.1.el6.x86_64.rpm
rpm -i http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm

perl-Text-CSV_XSのインストール

vi /etc/yum.repos.d/rpmforge.repo 
-------------
enabled = 0
-------------

yum install --enablerepo=rpmforge perl-Text-CSV_XS

xtables-addonsのインストール

xtables-addonsは、iptablesを扱う際のカーネルの拡張モジュールです。

wget http://downloads.sourceforge.net/project/xtables-addons/Xtables-addons/1.37/xtables-addons-1.37.tar.xz

tar xvf xtables-addons-1.37.tar.xz
cd xtables-addons-1.37/

CentOS6の場合は以下の設定が必要なようです。(その場合のip6tablesでの動作は未検証)

vi /lib/modules/2.6.32-279.14.1.el6.x86_64/build/include/linux/autoconf.h
------------
/* #define CONFIG_IP6_NF_IPTABLES_MODULE 1 */
------------

インストールします。

./configure

make && make install

GeoIPデータの取得&展開

データをダウンロードと配置をします。
常に最新の情報に更新するためにはcronなどで以下の処理を行う必要があります。

cd geoip/
./xt_geoip_dl
./xt_geoip_build GeoIPCountryWhois.csv
mkdir -p /usr/share/xt_geoip/
cp -r {BE,LE} /usr/share/xt_geoip/

使い方

iptables -I INPUT -m geoip --src-cc CN -j DROP

のように-src-ccで国コードを指定します。

参考

12
16
0

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
12
16