【Linux】iptables(ファイアウォール)設定&使用方法【CentOS6】

公開日時:2018年12月23日 / 最終更新日時:2019年02月11日

今回は Linux(CentOS6系)にデフォルトで実装されているのファイアウォール(パケットフィルタリング)ソフトの「iptables」の設定および使用方法について解説します。

CentOS7 系の場合は firewalld を利用します。

 

 

 

iptables とは

iptables の機能です。

 

 

iptables コマンド

iptables には、以下の3つのポリシーがあります。

 

それぞれの通信の基本ポリシーを以下の3つのターゲットに設定できます。

■ターゲット

 

 

 

iptables の注意点

iptables で一番失敗するパターンとしては「iptables コマンドと iptables ファイルの両方を考慮していない」場合に、実はブロックされてなかったとか、うまく機能しているように見えないなどのトラブルが発生するように思います。

そのため、iptables の設定をする際は、

の両方に注意します。

 

 

iptables 設定

■iptables 自動起動 ON 手順

# chkconfig iptables on

 

 

■iptables 起動手順

# /etc/init.d/iptables start
iptables: ファイアウォールルールを適用中: [ OK ]
#

 

■現状確認手順

自動起動が off の場合

# chkconfig | grep iptables
iptables  0:off  1:off  2:off  3:off  4:off  5:off  6:off
#

 

 

自動起動が on の場合

# chkconfig | grep iptables
iptables  0:off  1:off  2:off  3:off  4:off  5:off  6:off
#

 

 

■iptables が起動していない場合

[root@CentOS6 ~]# iptables -L 
Chain INPUT (policy ACCEPT) 
target     prot opt source               destination 
 
Chain FORWARD (policy ACCEPT) 
target     prot opt source               destination 
 
Chain OUTPUT (policy ACCEPT) 
target     prot opt source               destination 
[root@CentOS6 ~]#

 

 

 

インバウンド拒否(ブロック)の設定

インバウンド(入ってくる方)のパケットをブロックする設定です。

■特定 IP アドレスからのアクセスを拒否する

例えば、192.168.1.13 からのアクセスを拒否する場合です。

その場合は、拒否したい IP アドレスを設定します。

# iptables -I INPUT -s 192.168.1.13 -j DROP

 

 

設定したら「iptables -L」コマンドで設定状況を確認します。

[root@CentOS6 ~]# iptables -L 
Chain INPUT (policy ACCEPT) 
target     prot opt source               destination 
DROP       all  --  ip-192-168-1-13.ap-northeast-1.compute.internal  anywhere    
 
Chain FORWARD (policy ACCEPT) 
target     prot opt source               destination 
 
Chain OUTPUT (policy ACCEPT) 
target     prot opt source               destination 
[root@CentOS6 ~]#

 

ただし、ソース(source)が「ip-192-168-1-13.ap-northeast-1.compute.internal」になっています。

「ip-192-168-1-13.ap-northeast-1.compute.internal」ですが、自動的に名前解決をされてしまうので、「-n」オプションを付けます。

 

■オプション

 

[root@CentOS6 ~]# iptables -L -n 
Chain INPUT (policy ACCEPT) 
target     prot opt source               destination 
DROP       all  --  192.168.1.13         0.0.0.0/0 ← IPアドレスで表示されています。 
 
Chain FORWARD (policy ACCEPT) 
target     prot opt source               destination 
 
Chain OUTPUT (policy ACCEPT) 
target     prot opt source               destination 
[root@CentOS6 ~]#

 

「192.168.1.13」からのアクセスは Ping も ssh もアクセス不可になります。

 

 

 

ルールを削除したい場合

ルールを削除したい場合は、iptables -L の一覧で表示されたライン番号を下記コマンドで指定します。

ちなみにライン番号を表示する場合は「--line-numbers」オプションを追加します。

[root@CentOS6 ~]# iptables -L -n --line-numbers 
Chain INPUT (policy ACCEPT) 
num  target     prot opt source               destination 
1    DROP       all  --  192.168.1.13         0.0.0.0/0 ← 番号「1」で表示されています。 
 
Chain FORWARD (policy ACCEPT) 
num  target     prot opt source               destination 
 
Chain OUTPUT (policy ACCEPT) 
num  target     prot opt source               destination 
[root@CentOS6 ~]#

 

 

ライン番号を利用して設定を削除します。

[root@CentOS6 ~]# iptables -D INPUT 1

 

 

結果を確認します。

[root@CentOS6 ~]# iptables -L -n 
Chain INPUT (policy ACCEPT) 
target     prot opt source               destination 
 
Chain FORWARD (policy ACCEPT) 
target     prot opt source               destination 
 
Chain OUTPUT (policy ACCEPT) 
target     prot opt source               destination 
[root@CentOS6 ~]#

 

 

 

iptables コマンド実行例

以下、iptables コマンド実行例を記載します。

 

■ネットワーク「192.168.1.0/24」からの SSH(22/TCP)アクセスを許可する設定

 

[root@CentOS6 ~]# iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

 

 

ネットワーク「192.168.1.0/24」への 3306/TCP へのアクセスを拒否する設定【アウトバウンドの拒否】

 

# iptables -A OUTPUT -p tcp -d 192.168.1.0/24 --dport 3306 -j REJECT

 

DROP と REJECT の違い

 

■DROP と REJECT どちらを選ぶべきなのか

REJECT の場合は、すぐにレスポンスを返すので、サーバーの存在を相手に知らせます。そのため、外部に公開している場合は、攻撃者に対して存在を知られてしまうリスクがあります。(ただし、今はサーバーを直接インターネットにさらすようなネットワーク構成を選択する企業はないと思いますが)

DROP の場合は、レスポンスが返ってこないため、サーバーの存在を相手から隠蔽することができます。

その代わり、ping を DROP するとサーバーが存在しているのか存在していないのか分からなくなるため、IP アドレスの重複など事故が起こりやすくなります。

 

 

REJECT の場合は、ping コマンドを実行した瞬間、以下のようなレスポンスが返ってきます。

From 192.168.1.23 icmp_seq=167 Destination Port Unreachable

 

 

 

ネットワーク「192.168.1.0/24」へのすべてのパケットを拒否する設定【アウトバウンドの拒否】

 

# iptables -A OUTPUT -p all -d 192.168.1.0/24 -j REJECT

 

 

 

iptables コマンドを実行後に設定を反映させる

iptables コマンドを実行しただけでは、OS 再起動後に設定が消えてしまいます。

そのため OS 再起動後にも残るように、以下のように設定を反映させます。

 

iptables コマンドで 192.168.1.83/32 へのアクセスを拒否する設定をします。

iptables -A OUTPUT -p all -d 192.168.1.83/32 -j REJECT 

 

save コマンドを実行します。

/etc/init.d/iptables save 

ファイアウォールのルールを /etc/sysconfig/iptables に保存中: [ OK ]
#

 

save コマンドを実行することで iptables ファイルに追加されます。 

# cat /etc/sysconfig/iptables 
# Generated by iptables-save v1.4.7 on Thu Dec 20 16:47:54 2018 
*filter 
:INPUT ACCEPT [0:0] 
:FORWARD ACCEPT [0:0] 
:OUTPUT ACCEPT [74:16336] 
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A INPUT -p icmp -j ACCEPT 
-A INPUT -i lo -j ACCEPT 
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A INPUT -j REJECT --reject-with icmp-host-prohibited 
-A FORWARD -j REJECT --reject-with icmp-host-prohibited 
-A OUTPUT -d 192.168.1.83/32 -j REJECT --reject-with icmp-port-unreachable ← 設定が追加されています。
COMMIT 
# Completed on Thu Dec 20 16:47:54 2018 

 

 

 

iptables -L コマンドでも設定が入っていることが確認できます。

iptables -L 
Chain INPUT (policy ACCEPT) 
target prot opt source destination 
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED 
ACCEPT icmp -- anywhere anywhere 
ACCEPT all -- anywhere anywhere 
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh 
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 
 
Chain FORWARD (policy ACCEPT) 
target prot opt source destination 
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 
 
Chain OUTPUT (policy ACCEPT) 
target prot opt source destination 
REJECT all -- anywhere 192.168.1.83 reject-with icmp-port-unreachable 

 

 

 

iptables コマンドオプション

iptables のオプションです。

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange