iptablesでファイヤーウォールの設定 |
※ サーバー機でルータ構築した場合は、ここでのファイヤーウォールの設定は不要です。
実際に運用するシステムでは、何回もiptablesコマンドを実行しなくてはいけません。
かなり面倒で、途中で何処までコマンドを打ったか分からなくなったり、間違い等も起こりやすいので、
ここでは1連のコマンドをシェルスクリプトとして作成する事にします。
# rm -f /etc/ppp/firewall-masq ← デフォルトの設定ファイルを削除 ※ 設定ファイルは当サイトよりダウンロードする事も可能です。 # cd /etc/ppp/ # wget http://vine.1-max.net/rpm/firewall-masq # vi /etc/ppp/firewall-masq ← 新しく設定スクリプトを作成 i キー(入力モード) ※ 下記内容を各自の環境に合わせて編集してください。 #!/bin/bash #------------------------# # インターフェイスの設定 # #------------------------# WAN='eth0' # 外部インタフェース #----------------------------------------# # ローカル・ネットワーク・アドレスの設定 # #----------------------------------------# LAN='192.168.11.0/24' # ローカル・ネットワーク・アドレス ※ 192.168.11.0の部分は下記コマンドで確認できます。 # netstat -rn|grep eth0|grep 255.255.255.0|cut -f1 -d' ' #-----------------------------# # FTP接続モージュールをロード # #-----------------------------# modprobe ip_conntrack_ftp #------------------------# # すべてのルールをクリア # #------------------------# /etc/init.d/iptables stop #--------------------------------------------------------------------------# # デフォルトルール(以降のルールにマッチしなかった場合に適用するルール)設定 # #--------------------------------------------------------------------------# iptables -P INPUT DROP # 受信はすべて破棄 iptables -P FORWARD DROP # 通過はすべて破棄 iptables -P OUTPUT ACCEPT # 送信はすべて許可 #------------------------------------------------# # ループバックアドレスからのアクセスをすべて許可 # #------------------------------------------------# iptables -A INPUT -i lo -j ACCEPT #--------------------------------------------------# # ローカルネットワーク内からのアクセスをすべて許可 # #--------------------------------------------------# iptables -A INPUT -s $LAN -j ACCEPT #------------------------------------------------------------# # 内部から行ったアクセスに対する外部からの応答アクセスを許可 # #------------------------------------------------------------# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #--------------------# # フラグメントを破棄 # #--------------------# iptables -N fragment iptables -A fragment -j LOG --log-prefix '[iptables FRAGMENT] ' iptables -A fragment -j DROP iptables -A INPUT -f -j fragment #-------------------------------# # NetBIOS関連パケットの流出防止 # #-------------------------------# iptables -N net-bios iptables -A net-bios -j LOG --log-prefix '[iptables NETBIOS] ' iptables -A net-bios -j DROP iptables -A INPUT -s ! $LAN -p tcp -m multiport --dports 135,137,138,139,445 -j net-bios iptables -A INPUT -s ! $LAN -p udp -m multiport --dports 135,137,138,139,445 -j net-bios iptables -A OUTPUT -d ! $LAN -p tcp -m multiport --sports 135,137,138,139,445 -j net-bios iptables -A OUTPUT -d ! $LAN -p udp -m multiport --sports 135,137,138,139,445 -j net-bios #-----------------------# # Ping of Death攻撃対策 # #-----------------------# iptables -N ping-death iptables -A ping-death -m limit --limit 1/s --limit-burst 4 -j ACCEPT iptables -A ping-death -j LOG --log-prefix '[iptables PING DEATH] ' iptables -A ping-death -j DROP iptables -A INPUT -p icmp --icmp-type echo-request -j ping-death #---------------------# # SYNフラッド攻撃対策 # #---------------------# echo 1 > /proc/sys/net/ipv4/tcp_syncookies #---------------# # Smurf攻撃対策 # #---------------# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts #------------------------------------# # メールサーバ等のレスポンス低下防止 # #------------------------------------# iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset #---------------------------------------------------# # FTPサーバー(21番ポート)へのアクセスをすべて許可 # #---------------------------------------------------# iptables -A INPUT -p tcp --dport 21 -j ACCEPT #------------------------------------------------# # PASV用ポート(FTP-DATA)へのアクセスをすべて許可 # # FTPサーバー構築でPASV用ポーを指定した場合のみ # #------------------------------------------------# #iptables -A INPUT -p tcp --dport 4000:4029 -j ACCEPT #--------------------------------------------# # LAN内からのみSSH(22番ポート)に接続を許可 # #--------------------------------------------# iptables -A INPUT -s $LAN -p tcp --dport 22 -j ACCEPT #-----------------------------------------# # 外部からの22番ポート(SSH)への接続を許可 # #-----------------------------------------# #iptables -A INPUT -p tcp --dport 22 -j ACCEPT #-------------------------------------------------------# # Webサーバー(80番ポート)HTTPへのアクセスをすべて許可 # #-------------------------------------------------------# iptables -A INPUT -p tcp --dport 80 -j ACCEPT #---------------------------------------------------------# # Webサーバー(433番ポート)HTTPSへのアクセスをすべて許可 # # サーバー通信暗号化(HTTPS)を運営する場合のみ # #---------------------------------------------------------# iptables -A INPUT -p tcp --dport 443 -j ACCEPT #----------------------------------------------------------# # メールサーバー(25番ポート)SMTPへのアクセスをすべて許可 # #----------------------------------------------------------# iptables -A INPUT -p tcp --dport 25 -j ACCEPT #----------------------------------------------------------# # メールサーバー(110番ポート)POPへのアクセスをすべて許可 # #----------------------------------------------------------# iptables -A INPUT -p tcp --dport 110 -j ACCEPT #-----------------------------------------------------------# # メールサーバー(143番ポート)IMAPへのアクセスをすべて許可 # #-----------------------------------------------------------# iptables -A INPUT -p tcp --dport 143 -j ACCEPT #----------------------------------------# # Usermin(20000番ポート)への接続を許可 # # Userminを運営する場合のみ # #----------------------------------------# iptables -A INPUT -p tcp --dport 20000 -j ACCEPT #----------------------------------------------------# # LAN内からのみWebmin(10000番ポート)への接続を許可 # #----------------------------------------------------# iptables -A INPUT -s $LAN -p tcp --dport 10000 -j ACCEPT #--------------------------------------------# # 設定ルール外のアクセスはログを記録して破棄 # #--------------------------------------------# iptables -A INPUT -j LOG --log-prefix '[iptables INPUT DROP] ' iptables -A INPUT -j DROP iptables -A FORWARD -j LOG --log-prefix '[iptables FORWARD DROP] ' iptables -A FORWARD -j DROP #----------------# # 設定内容の保存 # #----------------# /etc/rc.d/init.d/iptables save #------------------------# # 再起動で設定内容を反映 # #------------------------# service iptables restart編集したら Esc キー(コマンドモード):wqで保存 |
設定の反映 |
# sh /etc/ppp/firewall-masq ← スクリプト実行 組込みチェインを標準ACCEPTポリシーに再設定: [ OK ] 現在のルールを/etc/sysconfig/iptablesに保存中: [ OK ] 現在のすべてのルールとユーザ定義チェインを初期化中: [ OK ] 現在のすべてのルールとユーザ定義チェインを破棄中: [ OK ] iptablesファイアウォールルールを適用中: [ OK ] [ OK ] |
これで、ポリシーに基づいたパケットフィルタが可能になりました。
Copyright c Vine Linuxで自宅サーバー. 2004 All Rights Reserved.