[ [[CentOS/セキュリティ]] ] iptables でファイアウォールを構築します。 * インストール [#wbc689f1] ** インストール確認 [#ie45bff4] [root@linux ~]# yum list | grep iptables iptables.i386 1.3.5-1.2.1 installed ←■ インストール済 iptables-ipv6.i386 1.3.5-1.2.1 installed ←■ インストール済(ipv6用) iptables-devel.i386 1.3.5-1.2.1 base [root@linux ~]# rpm -qa | grep iptables iptables-1.3.5-1.2.1 ←■ インストール済 iptables-ipv6-1.3.5-1.2.1 ←■ インストール済(ipv6用) [root@linux ~]# #shjs(sh){{ [root@linux ~]# yum list | grep iptables iptables.i386 1.3.5-1.2.1 installed ←■ インストール済 iptables-ipv6.i386 1.3.5-1.2.1 installed ←■ インストール済(ipv6用) iptables-devel.i386 1.3.5-1.2.1 base [root@linux ~]# rpm -qa | grep iptables iptables-1.3.5-1.2.1 ←■ インストール済 iptables-ipv6-1.3.5-1.2.1 ←■ インストール済(ipv6用) [root@linux ~]# }} ** インストール [#e6b622f7] インストールされていなかった場合、インストールします。 [root@linux ~]# yum -y install iptables ←■ インストール [root@linux ~]# [root@linux ~]# chkconfig iptables on ←■ 自動起動をONにする [root@linux ~]# [root@linux ~]# chkconfig --list iptables ←■ 自動起動の確認 iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@linux ~]# #shjs(sh){{ [root@linux ~]# yum -y install iptables ←■ インストール [root@linux ~]# [root@linux ~]# chkconfig iptables on ←■ 自動起動をONにする [root@linux ~]# [root@linux ~]# chkconfig --list iptables ←■ 自動起動の確認 iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@linux ~]# }} * iptables 設定確認 [#g8c16657] インストール時にファイアフォールを有効にした場合は以下のコマンドで設定内容が表示されます。 [root@linux ~]# 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 ipv6-crypt-- anywhere anywhere ACCEPT ipv6-auth-- anywhere anywhere ACCEPT udp -- anywhere 224.0.0.251 udp dpt:5353 ACCEPT udp -- anywhere anywhere udp dpt:ipp ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:smtp REJECT all -- anywhere anywhere reject-with icmp-host-prohibited [root@linux ~]# #shjs(sh){{ [root@linux ~]# 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 ipv6-crypt-- anywhere anywhere ACCEPT ipv6-auth-- anywhere anywhere ACCEPT udp -- anywhere 224.0.0.251 udp dpt:5353 ACCEPT udp -- anywhere anywhere udp dpt:ipp ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:smtp REJECT all -- anywhere anywhere reject-with icmp-host-prohibited [root@linux ~]# }} * iptables 設定 [#g5ead960] *** 編集前にファイルバックアップ [#n93ae8ea] [root@linux ~]# cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.org #shjs(sh){{ [root@linux ~]# cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.org }} *** /etc/sysconfig/iptables の編集 [#d79d5b9f] [root@linux ~]# vi /etc/sysconfig/iptables ← viエディタで編集する # Firewall configuration written by system-config-securitylevel # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :RH-Firewall-1-INPUT - [0:0] -A INPUT -j RH-Firewall-1-INPUT -A FORWARD -j RH-Firewall-1-INPUT -A RH-Firewall-1-INPUT -i lo -j ACCEPT -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT -A RH-Firewall-1-INPUT -p 50 -j ACCEPT -A RH-Firewall-1-INPUT -p 51 -j ACCEPT -A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT -A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #■↓ SSH -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 2222 -j ACCEPT #■↓ HTTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #■↓ FTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT #■↓ FTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT #■↓ SMTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT #■↓ POP3 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT #■↓ HTTPS -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT #■↓ OP25B対策(サブミッションポート) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 587 -j ACCEPT #■↓ MySQL(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #■↓ PostgreSQL(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT #■↓ Webmin(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited COMMIT #shjs(sh){{ [root@linux ~]# vi /etc/sysconfig/iptables ← viエディタで編集する # Firewall configuration written by system-config-securitylevel # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] :RH-Firewall-1-INPUT - [0:0] -A INPUT -j RH-Firewall-1-INPUT -A FORWARD -j RH-Firewall-1-INPUT -A RH-Firewall-1-INPUT -i lo -j ACCEPT -A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT -A RH-Firewall-1-INPUT -p 50 -j ACCEPT -A RH-Firewall-1-INPUT -p 51 -j ACCEPT -A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT -A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT -A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #■↓ SSH -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 2222 -j ACCEPT #■↓ HTTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #■↓ FTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT #■↓ FTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT #■↓ SMTP -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT #■↓ POP3 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT #■↓ HTTPS -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT #■↓ OP25B対策(サブミッションポート) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 587 -j ACCEPT #■↓ MySQL(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #■↓ PostgreSQL(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT #■↓ Webmin(使用する場合) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACCEPT -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited COMMIT }} ポート番号の範囲指定する場合~ 例) 50000番~50005番を設定する -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 50000:50005 -j ACCEPT #shjs(sh){{ -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 50000:50005 -j ACCEPT }} * iptables 起動・停止 [#hd078775] *** 起動 [#n62886d4] [root@linux ~]# /etc/rc.d/init.d/iptables start ← iptables 起動 iptables ファイアウォールルールを適用中: [ OK ] [root@linux ~]# #shjs(sh){{ [root@linux ~]# /etc/rc.d/init.d/iptables start ← iptables 起動 iptables ファイアウォールルールを適用中: [ OK ] [root@linux ~]# }} *** 停止 [#c403e4d0] [root@linux ~]# /etc/rc.d/init.d/iptables stop ← iptables 停止 ファイアウォールルールを適用中: [ OK ] チェインポリシーを ACCEPT に設定中filter [ OK ] iptables モジュールを取り外し中 [ OK ] [root@linux ~]# #shjs(sh){{ [root@linux ~]# /etc/rc.d/init.d/iptables stop ← iptables 停止 ファイアウォールルールを適用中: [ OK ] チェインポリシーを ACCEPT に設定中filter [ OK ] iptables モジュールを取り外し中 [ OK ] [root@linux ~]# }} *** 再起動 [#v57a561f] [root@linux ~]# /etc/rc.d/init.d/iptables restart ← iptables 再起動 ファイアウォールルールを適用中: [ OK ] チェインポリシーを ACCEPT に設定中filter [ OK ] iptables モジュールを取り外し中 [ OK ] iptables ファイアウォールルールを適用中: [ OK ] [root@linux ~]# #shjs(sh){{ [root@linux ~]# /etc/rc.d/init.d/iptables restart ← iptables 再起動 ファイアウォールルールを適用中: [ OK ] チェインポリシーを ACCEPT に設定中filter [ OK ] iptables モジュールを取り外し中 [ OK ] iptables ファイアウォールルールを適用中: [ OK ] [root@linux ~]# }} * iptables 有効・無効 [#ecaea107] iptables を有効にするとは、OSの起動と同時にサービスを起動すること *** iptables を有効にする [#ya0b2e9f] [root@linux ~]# chkconfig iptables on ← iptables サービスを有効 [root@linux ~]# #shjs(sh){{ [root@linux ~]# chkconfig iptables on ← iptables サービスを有効 [root@linux ~]# }} *** iptables を無効にする [#oa8c8531] [root@linux ~]# chkconfig iptables off ← iptables サービスを無効 [root@linux ~]# #shjs(sh){{ [root@linux ~]# chkconfig iptables off ← iptables サービスを無効 [root@linux ~]# }} *** 設定確認 [#tf6cfda3] [root@linux ~]# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@linux ~]# #shjs(sh){{ [root@linux ~]# chkconfig --list iptables iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@linux ~]# }} * 良く使う使用ポート一覧 [#z956f220] |CENTER: ポート番号 |CENTER: サービス名 |CENTER: 説明 | | 22 | SSH | SSH デフォルト ポート番号を変更する場合は不要 | | 2222 | SSH | SSH カスタム | | 20 | FTP(データ) | FTP デフォルト | | 21 | FTP(制御コマンド) | FTP デフォルト | | 80 | HTTP | HTTP デフォルト | | 443 | HTTPS | HTTPS デフォルト | | 25 | SMTP | メール送信 | | 110 | POP3 | メール受信 | | 587 | SMTP | OP25B対策(サブミッションポート) | | 465 | SMTPS | メール送信 SSL対応 | | 995 | POP3S | メール受信 SSL対応 | | 143 | IMAP | メールIMAP受信 | | 993 | IMAPS | メールIMAPS受信 | | 3306 | MySQL | MySQL デフォルト | | 5432 | PostgreSQL | PostgreSQL デフォルト | | 10000 | Webmin | Webmin デフォルト |