实现主机防火墙
开放被动模式的ftp服务
1、装载ftp连接追踪的专用模块:
跟踪模块路径:/lib/modules/kernelversion/kernel/net/netfilter
vim /etc/sysconfig/iptables-config 配置文件
IPTABLES_MODULES=“nf_conntrack_ftp"
modproble nf_conntrack_ftp
2、 放行请求报文:
命令连接:NEW, ESTABLISHED
数据连接:RELATED, ESTABLISHED
iptables –I INPUT -d LocalIP -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -d LocalIP -p tcp --dport 21 -m state --state NEW -j ACCEPT
3、 放行响应报文:
iptables -I OUTPUT -s LocalIP -p tcp -m state --state ESTABLISHED -j ACCEPT
示例:
1)装载ftp连接跟踪模块:
允许10.0.0.122访问10.0.0.121:21控制端口:
1 [root@CentOS7-121 ~]# modprobe nf_conntrack_ftp 2 [root@CentOS7-121 ~]# lsmod |grep ftp 3 nf_conntrack_ftp 18638 0 4 nf_conntrack 133095 4 xt_connlimit,xt_conntrack,nf_conntrack_ftp,nf_conntrack_ipv4 5 [root@CentOS7-121 ~]# iptables -R INPUT 2 -s 10.0.0.122 -p tcp --dport 21 -j ACCEPT 6 [root@CentOS7-121 ~]#iptables -R INPUT 2 -vnL --line-numbers 7 num pkts bytes target prot opt in out source destination 8 1 10996 753K ACCEPT all -- * * 10.0.0.1 0.0.0.0/0 9 2 0 0 ACCEPT tcp -- * * 10.0.0.122 0.0.0.0/0 tcp dpt:21 10 3 32 1596 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
2)10.0.0.122验证:
验证登录成功,但是查看资源被拒绝:
1 [root@CentOS7-122 ~]# ftp 10.0.0.121 2 Connected to 10.0.0.121 (10.0.0.121). 3 220 (vsFTPd 3.0.2) 4 Name (10.0.0.121:root): ftp 5 331 Please specify the password. 6 Password: 7 230 Login successful. 8 Remote system type is UNIX. 9 Using binary mode to transfer files. 10 ftp> ls 11 227 Entering Passive Mode (10,0,0,121,58,50). 12 ftp: connect: Connection refused
3)开启iptables的state状态跟踪工作
默认ftp数据端口是passive被动模式,服务端数据端口随机,不好定位,防火墙状态跟踪
ftp服务数据端口passive模式依赖21端口协商,21端口已开通允许连接,保持ESTABLISHED状态
1 [root@CentOS7-121 ~]#iptables -I INPUT 2 -s 10.0.0.122 -m state --state ESTABLISHED,RELATED -j ACCEPT 2 [root@CentOS7-121 ~]#iptables -I INPUT 2 -vnL --line-numbers 3 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) 4 num pkts bytes target prot opt in out source destination 5 1 11180 765K ACCEPT all -- * * 10.0.0.1 0.0.0.0/0 6 2 0 0 ACCEPT all -- * * 10.0.0.122 0.0.0.0/0 state RELATED,ESTABLISHED 7 3 11 616 ACCEPT tcp -- * * 10.0.0.122 0.0.0.0/0 tcp dpt:21 8 4 33 1656 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
4)10.0.0.122验证:
可以查看ftp服务数据
1 ftp> ls 2 227 Entering Passive Mode (10,0,0,121,189,99). 3 150 Here comes the directory listing. 4 -rw-r--r-- 1 1000 1000 1986 Dec 18 03:14 initial-setup-ks.cfg 5 226 Directory send OK.
【补充】
为了使访问策略更严谨,在OUTPUT链也做策略
1 [root@firewall-121 ~]# iptables -A OUTPUT -d 10.0.0.1 -j ACCEPT 2 [root@firewall-121 ~]# iptables -A OUTPUT -j REJECT 3 [root@firewall-121 ~]# iptables -I OUTPUT 2 -d 10.0.0.122 -m state --state ESTABLISHED,RELATED -j ACCEPT
浙公网安备 33010602011771号