iptables 設定スクリプト(メモ書き)

メモ書きです。

ルール生成に大量のiptablesコマンドを実行すると、
処理に時間がかかるようなので、ベタ書きに変更。
サーバがグローバルIP持ってるので端折ってたりするけどもたぶん大丈夫。。。
v6は無効にしてるので、処理を保留。

[text]
#!/bin/bash

#
# iptables 設定用スクリプト
#

CIDRTXT=/workdir/cidr.txt

#
#
ACCEPT_COUNTRY_MAKE(){
for addr in `cat $CIDRTXT | grep ^$1 | awk ‘{print $2}’`
do
printf ‘%s%s%s\n’ ‘-A ACCEPT_COUNTRY -s ‘ $addr ‘ -j ACCEPT ‘ >> iptables_setup.list
done
}

#
#
DROP_COUNTRY_MAKE(){
for addr in `cat $CIDRTXT | grep ^$1 | awk ‘{print $2}’`
do
printf ‘%s%s%s\n’ ‘-A DROP_COUNTRY -s ‘ $addr ‘ -j DROP ‘ >> iptables_setup.list
done
}

rm -f iptables_setup.list

#
# 設定リスト生成開始
#
printf ‘%s\n’ ‘*filter’ >> iptables_setup.list
printf ‘%s\n’ ‘:INPUT DROP [0:0]’ >> iptables_setup.list
printf ‘%s\n’ ‘:FORWARD DROP [0:0]’ >> iptables_setup.list
printf ‘%s\n’ ‘:OUTPUT ACCEPT [0:0]’ >> iptables_setup.list
printf ‘%s\n’ ‘:ACCEPT_COUNTRY – [0:0]’ >> iptables_setup.list
printf ‘%s\n’ ‘:DROP_COUNTRY – [0:0]’ >> iptables_setup.list
printf ‘%s\n’ ‘:LOG_PINGDEATH – [0:0]’ >> iptables_setup.list

#
# 自ホストからのアクセスをすべて許可
printf ‘%s\n’ ‘-A INPUT -i lo -j ACCEPT ‘ >> iptables_setup.list

#
# 内部から行ったアクセスに対する外部からの返答アクセスを許可
printf ‘%s\n’ ‘-A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT ‘ >> iptables_setup.list

#
# フラグメント化されたパケットはログを記録して破棄
printf ‘%s\n’ ‘-A INPUT -f -j LOG –log-prefix "[IPTABLES FRAGMENT] : " ‘ >> iptables_setup.list
printf ‘%s\n’ ‘-A INPUT -f -j DROP ‘ >> iptables_setup.list

#
# NetBIOS関連のアクセスはログを記録せずに破棄
printf ‘%s\n’ ‘-A INPUT ! -s 127.0.0.0/255.0.0.0 -p tcp -m multiport –dports 135,137,138,139,445 -j DROP ‘ >> iptables_setup.list
printf ‘%s\n’ ‘-A INPUT ! -s 127.0.0.0/255.0.0.0 -p udp -m multiport –dports 135,137,138,139,445 -j DROP ‘ >> iptables_setup.list

#
# Ping of Death攻撃対策、ルールは下のほうに記述
printf ‘%s\n’ ‘-A INPUT -p icmp -m icmp –icmp-type 8 -j LOG_PINGDEATH ‘ >> iptables_setup.list

#
# 全ホスト(ブロードキャストアドレス、マルチキャストアドレス)宛パケットはログを記録せずに破棄
printf ‘%s\n’ ‘-A INPUT -d 255.255.255.255 -j DROP ‘ >> iptables_setup.list
printf ‘%s\n’ ‘-A INPUT -d 224.0.0.1 -j DROP ‘ >> iptables_setup.list

# 113番ポート(IDENT)へのアクセスには拒否応答
# ※メールサーバ等のレスポンス低下防止
printf ‘%s\n’ ‘-A INPUT -p tcp -m tcp –dport 113 -j REJECT –reject-with tcp-reset ‘ >> iptables_setup.list

#
# 拒否したい国からのアクセスを破棄
printf ‘%s\n’ ‘-A INPUT -j DROP_COUNTRY ‘ >> iptables_setup.list

#
# SSHサーバへのアクセスを許可したい国からのみ許可
printf ‘%s\n’ ‘-A INPUT -p tcp -m tcp –dport 22 -j ACCEPT_COUNTRY ‘ >> iptables_setup.list

#
# Webサーバへのアクセスを許可
printf ‘%s\n’ ‘-A INPUT -p tcp -m tcp –dport 80 -j ACCEPT ‘ >> iptables_setup.list
printf ‘%s\n’ ‘-A INPUT -p tcp -m tcp –dport 443 -j ACCEPT ‘ >> iptables_setup.list

#
# 以外拒否
printf ‘%s\n’ ‘-A INPUT -j DROP ‘ >> iptables_setup.list

#
# 転送も拒否
printf ‘%s\n’ ‘-A FORWARD -j DROP ‘ >> iptables_setup.list

#
# ACCEPT_COUNTRY ルール作成
ACCEPT_COUNTRY_MAKE JP

#
# DROP_COUNTRY ルール作成
DROP_COUNTRY_MAKE CN
DROP_COUNTRY_MAKE KR
DROP_COUNTRY_MAKE TW
DROP_COUNTRY_MAKE RU
DROP_COUNTRY_MAKE BR

#
# Ping of Death攻撃対策
# 1秒間に4回を超えるpingは破棄
printf ‘%s\n’ ‘-A LOG_PINGDEATH -m limit –limit 1/sec –limit-burst 4 -j ACCEPT ‘ >> iptables_setup.list
printf ‘%s\n’ ‘-A LOG_PINGDEATH -j DROP ‘ >> iptables_setup.list

#
# 反映
printf ‘%s\n’ ‘COMMIT’ >> iptables_setup.list

#
# ——–
#

service fail2ban stop
iptables-restore < iptables_setup.list
service iptables save
service fail2ban start

[/text]

参考文献:
ファイアウォール構築(iptables) – CentOSで自宅サーバー構築

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.