当前位置: 移动技术网 > 科技>操作系统>Linux > Linux系统iptables与firewalld防火墙

Linux系统iptables与firewalld防火墙

2019年09月02日  | 移动技术网科技  | 我要评论

安娜霍兹,鸣金 site:317.ee,at-at

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 ~]# 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网