iptablesでファイアウォールを作ろう!
ここではiptablesの設定について掲載しています。
iptablesとは…?
簡単に説明すると、ファイアウォールです。Windowsにもファイアウォールソフトってありますよね?アレです。
ただ、Windowsのとは違い、グラフィックを用いて操作しないのでちょっととっつきにくいですが、適切に設定することでセキュリティを高めることが出来ます。
iptablesの設定
いちいちコマンドで打ち込んでいるとめんどくさいので、スクリプトで一気に設定したいと思います。
iptables設定スクリプト
スクリプトファイルを作り、以下を丸ごとコピーします。Tera Termの場合、テキストモードにしておいてマウスの右クリック、または"Alt + v"で貼り付け可能です。
スクリプトはCentOSで自宅サーバー構築様のものを参考にさせて頂きます。
[root@co ~]# vi iptables.sh
#!/bin/bash
# インターフェイスの設定
WAN='eth0'
# ローカルネットワークアドレスの設定(各自変更してください)
LAN='192.168.11.0/24'
# iptablesを停止
/etc/rc.d/init.d/iptables stop
# ポリシーを決定(一時的に送受信を許可、転送を破棄)
iptables -P INPUT ACCEPT
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# ルールをクリア
iptables -F
# icmp(ping)と自端末からの入力を許可
iptables -A INPUT -p icmp -j 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
# TCP SYN Flood攻撃対策
sysctl -w net.ipv4.tcp_syncookies=1 > /dev/null
sed -i '/net.ipv4.tcp_syncookies/d' /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies=1" >> /etc/sysctl.conf
# Smurf攻撃対策
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1 > /dev/null
sed -i '/net.ipv4.icmp_echo_ignore_broadcasts/d' /etc/sysctl.conf
echo "net.ipv4.icmp_echo_ignore_broadcasts=1" >> /etc/sysctl.conf
# フラグメント化されたパケットはログを記録して破棄
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 -A INPUT -s ! $LAN -p tcp -m multiport --dports 135,137,138,139,445 -j DROP
iptables -A INPUT -s ! $LAN -p udp -m multiport --dports 135,137,138,139,445 -j DROP
iptables -A OUTPUT -d ! $LAN -p tcp -m multiport --sports 135,137,138,139,445 -j DROP
iptables -A OUTPUT -d ! $LAN -p udp -m multiport --sports 135,137,138,139,445 -j DROP
# 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
# ブロードキャスト、マルチキャスト宛パケットはログを記録せずに破棄
iptables -A INPUT -d 255.255.255.255 -j DROP
iptables -A INPUT -d 224.0.0.1 -j DROP
# メールサーバ等のレスポンス低下防止
iptables -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset
#----------------------------------------------------------#
# 各種サービスを公開する場合の設定(ここから) #
#----------------------------------------------------------#
# SSH(Port22)への接続を許可
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
# DNS(Port53)への接続を許可
#iptables -A INPUT -p udp --dport 53 -j ACCEPT
#iptables -A INPUT -p udp --sport 53 -j ACCEPT
#iptables -A INPUT -p tcp --dport 53 -j ACCEPT
#iptables -A INPUT -p tcp --sport 53 -j ACCEPT
# HTTP(Port80)への接続を許可
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
# HTTPS(Port443)への接続を許可
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p tcp --sport 443 -j ACCEPT
# FTP-Control(Port20)への接続を許可
#iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#iptables -A INPUT -p tcp --sport 20 -j ACCEPT
# FTP-Data(Port21)への接続を許可
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -j ACCEPT
# FTP-Pasv(Port64000-64029)への接続を許可
iptables -A INPUT -p tcp --dport 64000:64029 -j ACCEPT
iptables -A INPUT -p tcp --sport 64000:64029 -j ACCEPT
# SMTP(Port25)への接続を許可
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
# SMTPS(Port465)への接続を許可
iptables -A INPUT -p tcp --dport 465 -j ACCEPT
iptables -A INPUT -p tcp --sport 465 -j ACCEPT
# POP(Port110)への接続を許可
#iptables -A INPUT -p tcp --dport 110 -j ACCEPT
#iptables -A INPUT -p tcp --sport 110 -j ACCEPT
# POPS(Port995)への接続を許可
#iptables -A INPUT -p tcp --dport 993 -j ACCEPT
#iptables -A INPUT -p tcp --sport 993 -j ACCEPT
# IMAP(Port143)への接続を許可
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
iptables -A INPUT -p tcp --sport 143 -j ACCEPT
# IMAPS(Port993)への接続を許可
iptables -A INPUT -p tcp --dport 993 -j ACCEPT
iptables -A INPUT -p tcp --sport 993 -j ACCEPT
# NTP(Port123)への接続を許可
iptables -A INPUT -p udp --dport 123 -j ACCEPT
iptables -A INPUT -p udp --sport 123 -j ACCEPT
# OpenVPN(Port1194)への接続を許可
#iptables -A INPUT -p udp --dport 1194 -j ACCEPT
#iptables -A INPUT -p udp --sport 1194 -j ACCEPT
# VPNインタフェース用ファイアウォール設定
# ※OpenVPNサーバーを公開する場合のみ
#[ -f /etc/openvpn/openvpn-startup ] && /etc/openvpn/openvpn-startup
# FORWARD設定
iptables -A FORWARD -s $LAN -j ACCEPT
iptables -A FORWARD -d $LAN -j ACCEPT
#----------------------------------------------------------#
# 各種サービスを公開する場合の設定(ここまで) #
#----------------------------------------------------------#
# 上記以外からの接続は破棄
iptables -P INPUT DROP
# ルールを保存
/etc/rc.d/init.d/iptables save
# iptablesを起動
/etc/rc.d/init.d/iptables start
スクリプトを実行
[root@co ~]# chmod +x iptables.sh
[root@co ~]# ./iptables.sh
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
ファイアウォールのルールを /etc/sysconfig/iptables に保存中[ OK ]
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]
iptables モジュールを読み込み中ip_conntrack_netbios_ns [ OK ]
ルールの一覧を確認
スクリプトを走らせたら、きちんと設定されているかを確認します。
[root@co ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT all -- 192.168.11.0/24 anywhere
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
fragment all -f anywhere anywhere
DROP tcp -- !192.168.11.0/24 anywhere multiport dports epmap,netbios-ns,netbios-dgm,netbios-ssn,microsoft-ds
DROP udp -- !192.168.11.0/24 anywhere multiport dports epmap,netbios-ns,netbios-dgm,netbios-ssn,microsoft-ds
ping-death icmp -- anywhere anywhere icmp echo-request
DROP all -- anywhere 255.255.255.255
DROP all -- anywhere ALL-SYSTEMS.MCAST.NET
REJECT tcp -- anywhere anywhere tcp dpt:auth reject-with tcp-reset
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp spt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:http
ACCEPT tcp -- anywhere anywhere tcp spt:http
ACCEPT tcp -- anywhere anywhere tcp dpt:https
ACCEPT tcp -- anywhere anywhere tcp spt:https
ACCEPT tcp -- anywhere anywhere tcp dpt:ftp
ACCEPT tcp -- anywhere anywhere tcp spt:ftp
ACCEPT tcp -- anywhere anywhere tcp dpts:terabase:ip-qsig
ACCEPT tcp -- anywhere anywhere tcp spts:terabase:ip-qsig
ACCEPT tcp -- anywhere anywhere tcp dpt:smtp
ACCEPT tcp -- anywhere anywhere tcp spt:smtp
ACCEPT tcp -- anywhere anywhere tcp dpt:smtps
ACCEPT tcp -- anywhere anywhere tcp spt:smtps
ACCEPT tcp -- anywhere anywhere tcp dpt:imap
ACCEPT tcp -- anywhere anywhere tcp spt:imap
ACCEPT tcp -- anywhere anywhere tcp dpt:imaps
ACCEPT tcp -- anywhere anywhere tcp spt:imaps
ACCEPT udp -- anywhere anywhere udp dpt:ntp
ACCEPT udp -- anywhere anywhere udp spt:ntp
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- anywhere !192.168.11.0/24 multiport sports epmap,netbios-ns,netbios-dgm,netbios-ssn,microsoft-ds
DROP udp -- anywhere !192.168.11.0/24 multiport sports epmap,netbios-ns,netbios-dgm,netbios-ssn,microsoft-ds
Chain fragment (1 references)
target prot opt source destination
LOG all -- anywhere anywhere LOG level warning prefix `[iptables FRAGMENT] '
DROP all -- anywhere anywhere
Chain ping-death (1 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere limit: avg 1/sec burst 4
LOG all -- anywhere anywhere LOG level warning prefix `[iptables PING DEATH] '
DROP all -- anywhere anywhere
ごちゃごちゃと書いてありますが、簡単に説明すると、ここでのルールは以下の通りです。
1.ローカルネットワーク内からのアクセスは無条件で許可
2.SSH、HTTP(S)、FTP(Pasv)、SMTP(S)、IMAP(S)、NTPでの接続を許可
3.あとは基本的に破棄(拒否)
#の付いた行はコメントとして扱われます。例えばDNSへの接続を許可したい場合は、行頭の#を消してください。
その他、追加したいルールがある場合は改造して使ってください。
この辺の話は、ただ真似ただけでは意味がありません。スクリプトの1行1行が何を意味しているのかを知るためにも、自分で色々と調べてみた方が良いでしょう。
そのうち、自分できちんとしたルールを作れるようになると思います。
今回新しく登場したコマンド
- vi
- iptables
- sysctl
- chmod
[広告]

トップページ
Rocky Linux 8
CentOS 7
Scientific Linux 6
CentOS 5
○準備
○VMware Player
○Hyper-V(Win Proのみ)
○導入
○セキュリティ対策
○Dynamic DNS
○NTPサーバ
○データベース
○WEBサーバ
○FTPサーバ
○メールサーバ
○DNSサーバ
○ファイルサーバ
○ブログシステム
○その他
ブログ
[広告]