iptablesをユーザ定義チェインですっきりさせよう

iptablesとは

iptablesとは、Linuxのカーネル機能を使用したIPパケットフィルタである。L3レベルでのフィルタリングとなる為、プログラムとの関連付けやHTTPの中身でのフィルタといった機能はできない。

通信内容

まず、どのような通信があるか考えてみる。ネットワーク構成は、ルータ配下にサーバとクライアントがある。下の図を見てみればわかるが、通信はインターネットからのサーバとの通信(1、2)とクライアントとの通信(3、4)がある。
iptables通信図

ユーザ定義チェイン

iptablesなんかでググったら、もりもり出てくるので今さらiptbalesの基本的な使い方はここでは書かない。ユーザ定義チェインとは、自作のルールみたいなもんだ。これを使うとiptablesを使用する際に、すっきりさせることができる。すでにiptablesを設定してある人がいるならば、iptables -Lで定義してある内容が確認できるので見てくれ。ユーザ定義を使用していないと、INPUTとOUTPUTしかデフォルトのチェインがなく、どれが、どの用途で使用しているのかわからなくなるのだ。そこでユーザ定義チェインを使用すれば、すっきりさせることができる。

設定方法

見た方が早いので、みてくれ。一応、iptablesのスクリプトをダウンロードもできます。

#! /bin/sh

###########################################################
# Define
###########################################################
LOCALNET='192.168.10.0/24'
BROADCAST='192.168.10.255'
SERVER='192.168.10.2/32'
ANY='0.0.0.0/0'

###########################################################
# Init
###########################################################
#Enable SYN Cookie
echo '1' > /proc/sys/net/ipv4/tcp_syncookies
#Disable Broadcat Ping
echo '1' > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
#Enable Forward
echo '1' > /proc/sys/net/ipv4/ip_forward
#ftp passive
modprobe ip_conntrack_ftp

#Flush & Reset
iptables -F
iptables -X

# User Chain
iptables -N WAN_TO_SERVER
iptables -N SERVER_TO_WAN
iptables -N LAN_TO_SERVER
iptables -N SERVER_TO_LAN
iptables -N LOGGING

###########################################################
#Deafult Rule
###########################################################
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

iptables -A INPUT  -i lo -j ACCEPT
iptables -A INPUT  -s $LOCALNET -d ${SERVER} -j LAN_TO_SERVER
iptables -A INPUT  -s $LOCALNET -d ${BROADCAST} -j LAN_TO_SERVER
iptables -A INPUT  -s $ANY -d $SERVER -j WAN_TO_SERVER
iptables -A INPUT  -j LOGGING
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -s $SERVER -d $LOCALNET -j SERVER_TO_LAN
iptables -A OUTPUT -s $SERVER -d $ANY -j SERVER_TO_WAN
iptables -A OUTPUT -j LOGGING
iptables -A FORWARD -j LOGGING

###########################################################
#LAN_TO_SERVER & SERVER_TO_LAN
###########################################################
iptables -A LAN_TO_SERVER -s $LOCALNET -j ACCEPT
iptables -A SERVER_TO_LAN -d $LOCALNET -j ACCEPT
iptables -A FORWARD -s $LOCALNET -j ACCEPT

###########################################################
#SERVER_TO_WAN
###########################################################
iptables -A SERVER_TO_WAN  -m state --state ESTABLISHED,RELATED -j ACCEPT
#NTP
iptables -A SERVER_TO_WAN -p udp --dport 123 -j ACCEPT
iptables -A SERVER_TO_WAN -p tcp --dport 123 -m state --state NEW -j ACCEPT
#DNS
iptables -A SERVER_TO_WAN -p udp --dport 53 -j ACCEPT
iptables -A SERVER_TO_WAN -p tcp --dport 53 -m state --state NEW -j ACCEPT
#ftp
iptables -A SERVER_TO_WAN -p tcp --dport 21 -m state --state NEW -j ACCEPT
#http
iptables -A SERVER_TO_WAN -p tcp --dport 80 -m state --state NEW -j ACCEPT
#https
iptables -A SERVER_TO_WAN -p tcp --dport 443 -m state --state NEW -j ACCEPT
#smtp
iptables -A SERVER_TO_WAN -p tcp --dport 25 -m state --state NEW -j ACCEPT
#pop3
iptables -A SERVER_TO_WAN -p tcp --dport 110 -m state --state NEW -j ACCEPT
#pop3s
iptables -A SERVER_TO_WAN -p tcp --dport 995 -m state --state NEW -j ACCEPT
#ICMP
iptables -A SERVER_TO_WAN -p icmp -j ACCEPT
#logging
iptables -A SERVER_TO_WAN  -j LOGGING

###########################################################
#WAN_TO_SERVER
###########################################################
iptables -A WAN_TO_SERVER  -m state --state ESTABLISHED,RELATED -j ACCEPT
#web
iptables -A WAN_TO_SERVER -p tcp --dport 80 -m state --state NEW -j ACCEPT
#smtp
iptables -A WAN_TO_SERVER -p tcp --dport 25 -m state --state NEW -j ACCEPT
#ssh
iptables -A WAN_TO_SERVER -p tcp --dport 22 -m state --state NEW -j ACCEPT
#pop3s
iptables -A WAN_TO_SERVER -p tcp --dport 995 -m state --state NEW -j ACCEPT
#OpenVPN
iptables -A WAN_TO_SERVER -p udp --dport 1194 -j ACCEPT
#Ident Reject
iptables -A WAN_TO_SERVER -p tcp --dport 113 -m state --state NEW -j REJECT --reject-with tcp-reset
#ICMP(ping)
iptables -A WAN_TO_SERVER -p icmp --icmp-type 0 -j ACCEPT
#loggin
iptables -A WAN_TO_SERVER  -j LOGGING


###########################################################
#LOGGING
###########################################################
#iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:" -m limit
iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:"
iptables -A LOGGING -j DROP

みそとなるのは、この行。

iptables -A INPUT  -i lo -j ACCEPT
iptables -A INPUT  -s $LOCALNET -d ${SERVER} -j LAN_TO_SERVER
iptables -A INPUT  -s $LOCALNET -d ${BROADCAST} -j LAN_TO_SERVER
iptables -A INPUT  -s $ANY -d $SERVER -j WAN_TO_SERVER
iptables -A INPUT  -j LOGGING
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -s $SERVER -d $LOCALNET -j SERVER_TO_LAN
iptables -A OUTPUT -s $SERVER -d $ANY -j SERVER_TO_WAN
iptables -A OUTPUT -j LOGGING
iptables -A FORWARD -j LOGGING

アドレスでそれぞれのユーザ定義チェインに当てはめている。今回はやってないが、ユーザ定義チェイン内でアドレスもしていできるので、さらに細かい設定もできるようになる。

すっきり感をみてくれ

さぁ、iptables -L -nなどで定義結果をユーザチェインの定義前と比べてくれ。かなりすっきりしているだろう

$ sudo iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
LAN_TO_SERVER  all  --  192.168.10.0/24       192.168.10.2
LAN_TO_SERVER  all  --  192.168.10.0/24       192.168.10.255
WAN_TO_SERVER  all  --  0.0.0.0/0            192.168.10.2
LOGGING    all  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy DROP)
target     prot opt source               destination
LOGGING    all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  192.168.10.0/24       0.0.0.0/0

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
SERVER_TO_LAN  all  --  192.168.10.2          192.168.10.0/24
SERVER_TO_WAN  all  --  192.168.10.2          0.0.0.0/0
LOGGING    all  --  0.0.0.0/0            0.0.0.0/0

Chain LOGGING (5 references)
target     prot opt source               destination
LOG        all  --  0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4 prefix `DROP:'
DROP       all  --  0.0.0.0/0            0.0.0.0/0

Chain LAN_TO_SERVER (2 references)
target     prot opt source               destination
ACCEPT     all  --  192.168.10.0/24       0.0.0.0/0

Chain SERVER_TO_LAN (1 references)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            192.168.10.0/24

Chain SERVER_TO_WAN (1 references)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:123
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:123 state NEW
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:53 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:443 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:25 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:110 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:995 state NEW
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
LOGGING    all  --  0.0.0.0/0            0.0.0.0/0

Chain WAN_TO_SERVER (1 references)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:80 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:25 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 state NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:995 state NEW
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:1194
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:113 state NEW reject-with tcp-reset
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 0
LOGGING    all  --  0.0.0.0/0            0.0.0.0/0

debian Valid HTML 4.01 Strict [VALID RSS!]