Linux系统iptables与firewalld防火墙

iptables

  iptables服务用于处理或过滤流量的策略条目(规则),多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类。

      在进行路由选择前处理数据包(PREROUTING);

      处理流入的数据包(INPUT);

      处理流出的数据包(OUTPUT);

      处理转发的数据包(FORWORD);

      在进行路由选择后处理数据包(POSTROUTING)。

  一般来说,从内网向外网发送的流量一般都是可控且良性的,因此使用最多的就是INPUT规则链,该规则链可以增大黑客人员从外网入侵内网的难度。

iptables中的基本参数

iptables中常用的参数以及作用

参数 作用
-P 设置默认策略
-F 清空规则链
-L 查看规则链
-A 在规则链的末尾加入新规则
-I num 在规则链的头部加入新规则
-D num 删除某一条规则
-s 匹配来源地址IP/MASK,加叹号“!”表示这个IP除外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议,如TCP、UDP、ICMP
--dport num 匹配目标端口号
--sport num 匹配来源端口号

  在iptables命令后添加-L参数查看已有的防火墙规则链

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

 target prot opt source destination
 ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
 ACCEPT all -- anywhere anywhere
 INPUT_direct all -- anywhere anywhere
 INPUT_ZONES_SOURCE all -- anywhere anywhere
 INPUT_ZONES all -- anywhere anywhere
 ACCEPT icmp -- anywhere anywhere
 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

 

  在iptables命令后添加-F参数清空已有的防火墙规则链

[root@localhost ~]# iptables -F
[root@localhost ~]# 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

  把INPUT规则链的默认策略设置为拒绝:

[root@localhost ~]# iptables -P INPUT DROP

   将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有的主机流量:

[root@localhost ~]# iptables -I INPUT -s 10.6.12.0/24 -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
[root@localhost ~]# iptables -L

Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 10.6.12.0/24 anywhere tcp dpt:ssh
ACCEPT icmp -- anywhere anywhere
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable

  防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放在拒绝动作前面。否则所有的流量就将被拒绝掉

使用CRT

一个是来自192.168.72.0/24的主机访问,会被拒绝

Connection timed out

来自10.6.72.0/24的主机访问

Last login: Sat Aug 31 11:43:09 2019 from 10.6.12.47
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 

 

posted @ 2019-09-01 21:52  DevonL  阅读(420)  评论(0编辑  收藏  举报