Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

iptablesにおけるFTP通信のステートフルインスペクション設定 on CentOS6.5

こんにちは、鯨井貴博@opensourcetechです。

 

本日は、FTP通信とiptables(FW)の関係について書こうと思います。

 

楽にFTP通信を使用と思えば、

iptables -Fとしてしまい、全てのルールを破棄すればいいですが、

やはりセキュリティを考慮したい、

 

そんな場合にはどう設定すればよいか。

 

その為には、FTP通信がどのような特徴を持っているか、

把握する必要があります。

 

FTP通信では、

アクティブモードと呼ばれるもの(TCP20と21を使用)と、

パッシブモードと呼ばれるもの(TCP20とランダムポートを使用)が存在します。

 

いずれも場合も、

まずTCP20で通信の開始を制御したあと、

別のポートでデータを送受信するという特殊な動きをします。

 

これがFWなどセキュリティを考えるときに大変であり、

そのダイナミックな動きに対応したステートフルインスペクションに

iptablesを対応させる必要があります。

 

ではその対策とは。

 

対策①(iptablesにステートフルインスペクションのルールを追加する)

[root@localhost ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state
RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp dpt:ssh
5    REJECT     all  --  anywhere             anywhere           
reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  anywhere             anywhere           
reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination


[root@localhost ~]# iptables -I INPUT 5 -p tcp --dport 21 -j ACCEPT
[root@localhost testuser]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state
RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp dpt:ssh
5    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ftp
6    REJECT     all  --  anywhere             anywhere           
reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  anywhere             anywhere           
reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

 

 

 

対策②(ip_conntrack_ftpを使用する)

[root@localhost ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state
RELATED,                                      ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp                                       dpt:ssh
5    REJECT     all  --  anywhere             anywhere
reject-with ic                                      mp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  anywhere             anywhere
reject-with ic                                      mp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# iptables -I INPUT 5 -m state --state NEW -m tcp -p tcp
-m multiport --dports 20,21 -j ACCEPT
[root@localhost ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state
RELATED,                                      ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp                                       dpt:ssh
5    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp                                       multiport dports ftp-data,ftp
6    REJECT     all  --  anywhere             anywhere
reject-with ic                                      mp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  anywhere             anywhere
reject-with ic                                      mp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination



[root@localhost ~]# vi /etc/sysconfig/iptables-config
[root@localhost ~]# cat -n /etc/sysconfig/iptables-config
     1  # Load additional iptables modules (nat helpers)
     2  #   Default: -none-
     3  # Space separated list of nat helpers (e.g. 'ip_nat_ftp
ip_nat_irc'), which
     4  # are loaded after the firewall rules are applied. Options for the
helpers are
     5  # stored in /etc/modprobe.conf.
     6  IPTABLES_MODULES="ip_conntrack_ftp" ⇒追記


[root@localhost ~]# /etc/init.d/vsftpd start
vsftpd 用の vsftpd を起動中:                               [  OK  ]
[root@localhost ~]# setenforce 0
[root@localhost ~]# /etc/init.d/iptables restart
iptables: チェインをポリシー ACCEPT へ設定中filter         [  OK  ]
iptables: ファイアウォールルールを消去中:                  [  OK  ]
iptables: モジュールを取り外し中:                          [  OK  ]
iptables: ファイアウォールルールを適用中:                  [  OK  ]
iptables: 追加のモジュールを読み込み中:ip_conntrack_ftp    [  OK  ]
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all  --  anywhere             anywhere            state
RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere
3    ACCEPT     all  --  anywhere             anywhere
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW
tcp dpt:ssh
5    ACCEPT     tcp  --  anywhere             anywhere            tcp
dpt:ftp
6    ACCEPT     all  --  anywhere             anywhere            state
NEW,RELATED
7    REJECT     all  --  anywhere             anywhere
reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all  --  anywhere             anywhere
reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

 

 

いずれの場合にも、

TCP20の制御通信に関連するデータ通信がFWで許可されるようになります。

 

 

Opensourcetech by Takahiro Kujirai