程序员

Iptables AND Firewalld

2018-12-06  本文已影响0人  枕梦_a280

iptables与firewalld都是防火墙规则生成工具。iptables设置好策略后交由内核层面的netfilter网络过滤来处理,而firewalld设置好策略后交由内核层面的nftables包过滤框架来管理。

1. iptables防火墙

四种连接状态:
NEW:新的连接
ESTABLISHED:已经建立的连接
INBALID:非法连接/无效连接
RELATED:相关联的连接。如ftp命令连接状态。
service iptables save
没有添加规则之前:
[root@ftp-server ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:22 

添加规则:
[root@ftp-server ~]# iptables -t filter -I INPUT 2 -p tcp --dport 21 -j ACCEPT
[root@ftp-server ~]# iptables -t filter -I OUTPUT 2 -p tcp --sport 21 -j ACCEPT
[root@ftp-server ~]# iptables -t filter -I OUTPUT 3 -p tcp --sport 20 -j ACCEPT
[root@ftp-server ~]# iptables -t filter -I INPUT 3 -p tcp --dport 20 -j ACCEPT
在客户端访问测试:
[root@ftp-client ~]# ftp 10.1.1.20
Connected to 10.1.1.20 (10.1.1.20).
220 (vsFTPd 2.2.2)
Name (10.1.1.20:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> passive              #关闭了被动模式
Passive mode off.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x    2 0        0            4096 Mar 01  2013 pub
226 Directory send OK.

查看规则:
[root@ftp-server ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:21 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:20 

被动模式:客户端开启一个大于1024的随机命令端口去连接ftp服务器的21号命令端口,客户端再开启一个大于1024的随机数据端口去连接服务器上的一个大于1024的随机数据端口。故此,在ftp服务器首先要先规定好一个随机数据端口的范围(3000~3005),然后添加防火墙规则。

命令行连接方式与主动模式相同,故省略。
[root@ftp-server ~]# vim /etc/vsftpd/vsftpd.conf
pasv_min_port=3000
pasv_max_port=3005

重启ftp服务:
[root@ftp-server ~]# service vsftpd restart

添加规则:
[root@ftp-server ~]# iptables -I INPUT 4 -p tcp --dport 3000:3005 -j ACCEPT
[root@ftp-server ~]# iptables -I OUTPUT 4 -p tcp --sport 3000:3005 -j ACCEPT

客户端访问测试:
ftp> passive              #开启被动模式
Passive mode on.
ftp> ls
227 Entering Passive Mode (10,1,1,20,11,186).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0            4096 Mar 01  2013 pub
226 Directory send OK.
[root@ftp-server ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpts:3000:3005 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:22 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:21 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spt:20 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp spts:3000:3005 

规则优化

[root@ftp-server ~]# iptables -A INPUT -p tcp -m multiport --dports 22,20,21,3000:3005 -j ACCEPT 
[root@ftp-server ~]# iptables -A OUTPUT -p tcp -m multiport --sports 22,20,21,3000:3005 -j ACCEPT 
[root@ftp-server ~]# iptables -L -n --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           multiport dports 22,20,21,3000:3005 

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

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           multiport sports 22,20,21,3000:3005 

除上述方法外,还可以调用iptables的conntrack模块来解决ftp服务被动模式规则。不过首先要将命令端口添加到防火墙规则。

打开iptables配置文件添加模块
[root@ftp-server ~]# vim /etc/sysconfig/iptables-config
IPTABLES_MODULES="nf_conntrack_ftp"
重启iptables服务
[root@ftp-server ~]# service iptables restart
[root@ftp-server ~]# iptables -I INPUT -p tcp -m multiport --dports 21,22 -j ACCEPT
[root@ftp-server ~]# iptables -I OUTPUT -p tcp -m multiport --sports 21,22 -j ACCEPT
[root@ftp-server ~]# iptables -I INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@ftp-server ~]# iptables -I OUTPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@ftp-server ~]# service iptables save

客户端访问测试:
[root@ftp-client ~]# ftp 10.1.1.20
Connected to 10.1.1.20 (10.1.1.20).
220 (vsFTPd 2.2.2)
Name (10.1.1.20:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (10,1,1,20,11,187).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0            4096 Mar 01  2013 pub
226 Directory send OK.
ftp> passive
Passive mode off.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
drwxr-xr-x    2 0        0            4096 Mar 01  2013 pub
226 Directory send OK.

查看规则:
[root@ftp-server ~]# iptables -L -n --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           multiport dports 21,22 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 

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

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           multiport sports 21,22 
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
1:在server02 开启路由转发功能
[root@server02 ipv4]# echo 1 > /proc/sys/net/ipv4/ip_forward
2:在server02 添加防火墙策略
[root@server02 ipv4]# iptables -t nat -I POSTROUTING -s 10.1.1.20 -j SNAT --to 10.2.2.99
3:将server01 的网关指向server02
[root@server01 ~]# route add default gw 10.1.1.21
4:在server01上ping 8.8.8.8测试
[root@server01 ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=127 time=203 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=127 time=205 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=127 time=208 ms
^C
--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2376ms
rtt min/avg/max/mdev = 203.127/205.624/208.274/2.104 ms

另外,在开启路由转发的前提下,也可以直接在server02添加地址伪装策略实现server01访问外网。

[root@server02 ipv4]# iptables -t nat -I POSTROUTING -s 10.1.1.20 -j MASQUERADE

目标地址转换:
server01:内网web服务主机 10.1.1.20
server02:作为路由转发的主机 10.1.1.21 11.11.11.11
server03:模拟外网主机 11.11.11.12

1:在server01 搭建web服务
[root@server01 ~]# yum -y install httpd httpd-devel
2:创建首页测试文件并启动web服务,并在本机测试访问。
[root@server01 ~]# echo "server01 for test" > /var/www/html/index.html
[root@server01 ~]# service httpd start
[root@server01 ~]# curl 10.1.1.20
server01 for test
3:在server03访问测试:
[root@server03 ~]# curl 10.1.1.20
curl: (7) Failed to connect to 10.1.1.20: 网络不可达
4:在server02开启目标地址转发,开启路由转发功能:
[root@server02 ~]# iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 10.1.1.20
[root@server02 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
5:在server01指定网关为10.1.1.21:
[root@server01 ~]# route del default gw 10.1.1.21
6:在server03访问测试:
[root@server03 ~]# curl 11.11.11.11
server01 for test

2. Firewall防火墙

[root@server03 ~]# firewall-cmd --get-zones
block dmz drop external home internal public trusted work
[root@server03 ~]# firewall-cmd --list-all-zones
block
  target: %%REJECT%%
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    

dmz
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  ...

查看当前默认zone:

[root@server03 ~]# firewall-cmd --get-default-zone
public

更改默认zone:

[root@server03 ~]# firewall-cmd --set-default-zone=home
success
[root@server03 ~]# firewall-cmd --get-default-zone
home
1:查看当前规则集下的策略:
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
2:当前规则集允许tcp80端口通过(删除该端口只需将 --add 改为 --remove 即可)
[root@server03 ~]# firewall-cmd --add-port=80/tcp
success
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
3:添加多个连续的端口:
[root@server03 ~]# firewall-cmd --add-port=3333-4444/tcp
success
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client
  ports: 80/tcp 3333-4444/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@server03 ~]# firewall-cmd --add-service=http
success
[root@server03 ~]# firewall-cmd --add-service=ftp
success
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client http ftp
  ports: 80/tcp 3333-4444/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
1:不允许10.1.1.22访问本机http服务
[root@server03 ~]# firewall-cmd --add-rich-rule='rule family="ipv4" source address=10.1.1.22 service name="http" drop'
success
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client http ftp samba
  ports: 80/tcp 3333-4444/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    rule family="ipv4" source address="10.1.1.22" service name="http" drop
2: 允许11.11.11.11访问本机http服务,但每分钟只允许有两次连接。
[root@server03 ~]# firewall-cmd --add-rich-rule='rule family="ipv4" source address=11.11.11.11 service name="http" limit value=2/m accept'
success
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client http ftp samba
  ports: 80/tcp 3333-4444/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    rule family="ipv4" source address="10.1.1.22" service name="http" drop
    rule family="ipv4" source address="11.11.11.11" service name="http" accept limit value="2/m"
[root@server03 ~]# firewall-cmd --add-service=nfs
success
[root@server03 ~]# firewall-cmd --permanent --add-service=ntp
success
[root@server03 ~]# 
[root@server03 ~]# systemctl restart firewalld
[root@server03 ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33 ens37
  sources: 
  services: ssh dhcpv6-client ntp
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

一般在写规则的时候不用指定模式,在写完所有规则后执行下面命令即可保存

[root@server03 ~]# firewall-cmd --runtime-to-permanent
success
[root@server03 ~]# firewall-cmd --reload
success
1:查看是否开启了panic模式
[root@server03 ~]# firewall-cmd --query-panic
no
2:开启panic模式
[root@server03 ~]# firewall-cmd --panic-on
3:关闭panic模式
[root@server03 ~]# firewall-cmd --panic-off
[root@server03 ~]# firewall-config

开启源地址转换命令(如前文中iptables情景中)

[root@server03 ~]# firewall-cmd --add-masquerade 
success

开启目标地址转换命令(如前文iptables情景中)

[root@server03 ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=10.1.1.20
success
上一篇 下一篇

猜你喜欢

热点阅读