CentOS 5.1 を始めてみた 8

CentOS 5.1 インストール初期時の iptables の設定。

# /sbin/iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
RH-Firewall-1-INPUT  all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            icmp any 
ACCEPT     esp  --  anywhere             anywhere            
ACCEPT     ah   --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp 
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

「模倣は最大の賛辞」ということで参考にさせてもらう。
第1回 ステートフルパケットフィルタを使ったサービスの公開

#!/bin/sh

trusthost='192.168.0.0/24'
myhost='192.168.0.31'
any='0.0.0.0/0'
sshport='22'

# Flush & reset the rule
iptables -F # 指定したチェインの内容を全て消去する
iptables -X # 指定したユーザ定義チェインを削除する


# Deafult the rule
# -P デフォルト(ルールが最後までマッチしなかった場合)に適用されるターゲットを指定する
iptables -P INPUT DROP   # INPUT   チェインで定義されたパケットをすべて破棄する
iptables -P OUTPUT DROP  # OUTPUT  チェインで定義されたパケットをすべて破棄する
iptables -P FORWARD DROP # FORWARD チェインで定義されたパケットをすべて破棄する


# Loopback setting
# -A 指定したチェインにルールを追加する
# -i インターフェイス名
# -j ターゲットを指定する。ターゲットには ACCEPT、DROP、RETURN、LOG、REJECT、ユーザ定義チェインを指定
iptables -A INPUT -i lo -j ACCEPT  # ループバックの INPUT チェインはすべて許可する
iptables -A OUTPUT -o lo -j ACCEPT # ループバックの OUTPUT チェインはすべて許可する

# ICMP trusthost->myhost
# -p          プロトコル tcp、udp、icmp、all のどれかを指定
# --icmp-type ICMPのタイプ番号またはタイプ名を指定
# -s          送信元を指定
# -d          送信先を指定
iptables -A INPUT -p icmp --icmp-type echo-request -s $trusthost -d $myhost -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply  -s $myhost -d $trusthost -j ACCEPT

# ICMP myhost->trusthost
iptables -A OUTPUT -p icmp --icmp-type echo-request -s $myhost -d $trusthost -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -s $trusthost -j ACCEPT
#iptables -A INPUT -p icmp --icmp-type echo-reply -s $trusthost -d $myhost -j ACCEPT



# ssh connection trusthost-> myhost
# --dport 送信先ポートを指定する
# --sport 送信元ポートを指定する
iptables -A INPUT -p tcp -s $trusthost -d $myhost --dport $sshport -j ACCEPT
iptables -A OUTPUT -p tcp -s $myhost --sport $sshport -d $trusthost -j ACCEPT


# logging
# -N           指定した名前でユーザ定義チェインを作成
# --log-level  ログを記録するレベルを指定
# --log-prefix 記録するログの先頭に文字列を追加
# -m limit     単位時間あたりにマッチする最大値。second、minute、hour、dayが指定できる
iptables -N LOGGING
iptables -A LOGGING -j LOG --log-level warning --log-prefix "DROP:" -m limit
iptables -A LOGGING -j DROP
iptables -A INPUT -j LOGGING
iptables -A OUTPUT -j LOGGING