网络安全——防火墙详解

发布时间:2022-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了网络安全——防火墙详解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

什么是防火墙?路由策略和策略路由

在计算机中,防火墙是基于预定安全规则来监视和控制传入和传出网络流量的网络安全系统。该计算机流入流出的所有网络通信均要经过此防火墙。防火墙对流经它的网络通信进行扫描,这样能够过滤掉一些攻击,以免其在目标计算机上被执行。防火墙还可以关闭不使用的端口。而且它还能禁止特定端口的流出通信,封锁特洛伊木马。最后,它可以禁止来自特殊站点的访问,从而防止来自不明入侵者的所有通信。

防火墙分为软件防火墙和硬件防火墙,他们的优缺点: **硬件防火墙:**拥有经过特别设计的硬件及芯片,性能高、成本高(当然硬件防火墙也是有软件的,只不过有部分功能由硬件实现,所以硬件防火墙其实是硬件+软件的方式); **软件防火墙:**应用软件处理逻辑运行于通用硬件平台之上的防火墙,性能比硬件防火墙低、成本低、性价比高。

Netfilter与iptables的关系 Netfilter是Linux操作系统核心层内部的一个数据包处理模块,它具有如下功能:

  • 网络地址转换(Network Address Translate)
  • 数据包内容修改
  • 以及数据包过滤的防火墙功能

Netfilter平台中制定了数据包的五个挂载点(Hook Point,我们可以理解为回调函数点,数据包到达这些位置的时候会主动调用我们的函数,使我们有机会能在数据包路由的时候改变它们的方向、内容),这5个挂载点分别是PRE_ROUTINGINPUTOUTPUTFORWARDPOST_ROUTING。 Netfilter所设置的规则是存放在内核空间中的,而iptables是一个应用层的应用程序,它通过Netfilter放出的接口来对存放在内核空间中的 XXtables(Netfilter的配置表)进行修改。这个XXtables由表tables、链chains、规则rules组成,iptables在应用层负责修改这个规则文件,类似的应用程序还有firewalld(CentOS7默认防火墙)。

所以Linux中真正的防火墙是Netfilter,iptables只是作为一个工具。但由于都是通过应用层程序如iptables或firewalld进行操作,所以我们一般把iptables或firewalld叫做Linux的防火墙。

**注意:**以上说的iptables都是针对IPv4的,如果IPv6,则要用ip6tables,至于用法应该是跟iptables是一样的。

链的概念

iptables开启后,数据报文从进入服务器到出来会经过5道关卡,分别为Prerouting(路由前)、Input(输入)、Outpu(输出)、Forward(转发)、Postrouting(路由后):

网络安全——防火墙详解

每一道关卡中有多个规则,数据报文必须按顺序一个一个匹配这些规则,这些规则串起来就像一条链,所以我们把这些关卡都叫**“链”**:

网络安全——防火墙详解

  • **INPUT链:**当接收到防火墙本机地址的数据包(入站)时,应用此链中的规则;
  • **OUTPUT链:**当防火墙本机向外发送数据包(出站)时,应用此链中的规则;
  • **FORWARD链:**当接收到需要通过防火墙发送给其他地址的数据包(转发)时,应用此链中的规则;
  • PREROUTING链:(互联网进入局域网)在对数据包作路由选择之前,应用此链中的规则,如DNAT(只有目标地址的转换 公网访问私网);
  • POSTROUTING链:(局域网出互联网)在对数据包作路由选择之后,应用此链中的规则,如SNAT(只有源地址的转换 私网访问公网)。

其中中INPUT、OUTPUT链更多的应用在“主机防火墙”中,即主要针对服务器本机进出数据的安全控制;而FORWARD、PREROUTING、POSTROUTING链更多的应用在“网络防火墙”中,特别是防火墙服务器作为网关使用时的情况。

表的概念

虽然每一条链上有多条规则,但有些规则的作用(功能)很相似,多条具有相同功能的规则合在一起就组成了一个“表”,iptables提供了四种“表”: – **filter表:**主要用于对数据包进行过滤,根据具体的规则决定是否放行该数据包(如DROP、ACCEPT、REJECT、LOG),所谓的防火墙其实基本上是指这张表上的过滤规则,对应内核模块iptables_filter; – **nat表:**network address translation,网络地址转换功能,主要用于修改数据包的IP地址、端口号等信息(网络地址转换,如SNAT、DNAT、MASQUERADE、REDIRECT)。属于一个流的包(因为包的大小限制导致数据可能会被分成多个数据包)只会经过这个表一次,如果第一个包被允许做NAT或Masqueraded,那么余下的包都会自动地被做相同的操作,也就是说,余下的包不会再通过这个表。对应内核模块iptables_nat; – **mangle表:**拆解报文,做出修改,并重新封装,主要用于修改数据包的TOS(Type Of Service,服务类型)、TTL(Time To Live,生存周期)指以及为数据包设置Mark标记,以实现Qos(Quality Of Service,服务质量)调整以及策略路由等应用,由于需要相应的路由设备支持,因此应用并不广泛。对应内核模块iptables_mangle; – **raw表:**是自1.2.9以后版本的iptables新增的表,主要用于决定数据包是否被状态跟踪机制处理,在匹配数据包时,raw表的规则要优先于其他表,对应内核模块iptables_raw。 我们最终定义的防火墙规则,都会添加到这四张表中的其中一张表中。

表链关系

5条链(即5个关卡)中,并不是每条链都能应用所有类型的表,事实上除了Ouptput链能同时有四种表,其他链都只有两种或三种表:

网络安全——防火墙详解

实际上由上图我们可以看出,无论在哪条链上,raw表永远在mangle表上边,而mangle表永远在nat表上边,nat表又永远在filter表上边,这表明各表之间是有匹配顺序的。

前面说过,数据报文必须按顺序匹配每条链上的一个一个的规则,但其实同一类(即属于同一种表)的规则是放在一起的,不同类的规则不会交叉着放,按上边的规律,每条链上各个表被匹配的顺序为:raw→mangle→nat→filter

前面说过,我们最终定义的防火墙规则,都会添加到这四张表中的其中一张表中,所以我们实际操作是对“表”进行操作的,所以我们反过来说一下,每种表都能用于哪些链:

表名能应用的链
rawpreroutingoutput
manglepreroutinginputforwardoutputpostrouting
natpreroutinginput(仅centos7)outputpostrouting
filterinputforwardoutput

综上,数据包通过防火墙的流程可总结为下图:

网络安全——防火墙详解

forward功能 :默认是0,打开为1即生效

匹配条件 S_IP:source ip 源ip S_PORT:source port 源端口 D_IP:destination ip 目标ip D_PORT:destination port 目标端口 TCP/UDP:第四层 传输层协议

处理的动作: accept:允许数据包通过 drop:直接丢弃数据包,不回应任何消息,客户端只有当该链接超时后才会有反应 timeout–超时 reject:拒绝数据包,会给客户端发送一个数据包被丢弃的响应的信息 result out–请求丢失

关闭防火墙

systemctl stop firewalld

关闭firewalld防火墙开机启动

systemctl disable firewalld

配置好yum源,完成挂载后 安装iptables防火墙

sudo yum -y install iptables

安装iptables的service启动工具

sudo yum -y install iptables-services

启动iptables

systemctl start iptables

查看iptables状态

systemctl status iptables

停止iptables

systemctl stop iptables

重启iptables

systemctl restart iptables

重载iptables

systemctl reload iptables

使用service命令查看iptables状态

service iptables status

查询规则 1.-L list,列出每条链上的规则,且链名必须全大写

列出input链上的规则

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

若不指定链名的话,查看的是含有filter表的三条链input forward output,默认的策略是接受,可以修改为reject drop等(黑名单)

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

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
iptables -L
//等同于
iptables -t filter -L     // -t filter可以省略

target:目标,该列的值通常是动作:accept reject等 创建一条链

iptables -N July_filter

在INPUT链上添加一条规则,让它跳转到刚刚的新链

iptables -A INPUT -p tcp -j July_filter

查看

iptables -L

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
July_filter  tcp  --  anywhere             anywhere

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain July_filter (1 references)
target     prot opt source               destination

prot:protocol 协议 opt:option 选项 source:源地址、网段、域名、主机名 destination:目标地址、网段、域名、主机名 最后一列:额外的一些信息

2.-t table,指定显示哪张表的规则,不写的话默认为 filter表 -n numeric,意思是指定的源和目标地址等都已数字或者数值的方式显示,一般与-L合用

[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
July_filter  tcp  --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain July_filter (1 references)
target     prot opt source               destination

-v:verbose 冗余的 啰嗦的,显示详情的意思,一般也可以与-L合用

[root@localhost ~]# iptables -vL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                          
  252 18896 ACCEPT     all  --  any    any     anywhere             anywhere                                                                                       state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere                                                                             
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere                                                                             
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere                                                                                       state NEW tcp dpt:ssh
    8  1420 REJECT     all  --  any    any     anywhere             anywhere                                                                                       reject-with icmp-host-prohibited
    0     0 July_filter  tcp  --  any    any     anywhere             anywhere                                                                           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                          
    0     0 REJECT     all  --  any    any     anywhere             anywhere                                                                                       reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 163 packets, 25174 bytes)
 pkts bytes target     prot opt in     out     source               destination                                                                          

Chain July_filter (1 references)
 pkts bytes target     prot opt in     out     source               destination            

多的四列: pkts:packets 包的数量 bytes:流过的数据包的字节数 in:入站网卡 out:出站网卡

-n -v -L三个可以合用,但-L要写在最后

[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  284 21312 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    9  1649 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 July_filter  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 181 packets, 28422 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain July_filter (1 references)
 pkts bytes target     prot opt in     out     source               destination

3.-x:exact 精确的,准确的 –line-numbers:列表有序号

[root@localhost ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      294 22048 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5        9  1649 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
6        0     0 July_filter  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 189 packets, 30518 bytes)
num   pkts bytes target     prot opt in     out     source               destination

Chain July_filter (1 references)
num   pkts bytes target     prot opt in     out     source               destination

4.-I:insert,插入,在某个表的最前面添加 -A:append,追加,在某个表的最后天追加 (与ACL相似,自上而下,逐一匹配) 之所以有向前添加和向后添加,是因为如果前面规则的是丢弃或拒绝,那么后面的规则是不会起作用的;而如果前面的是接受后面的是丢弃或拒绝,则接受之后后面的丢弃或拒绝也是不会生效的。

[root@localhost ~]# iptables -t filter -I INPUT -s 10.37.129.2 -j DROP
[root@localhost ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       10.37.129.2          0.0.0.0/0
  327 24440 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   10  1977 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 July_filter  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 210 packets, 34682 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain July_filter (1 references)
 pkts bytes target     prot opt in     out     source               destination

向iptables中的INPUT链(-I INPUT)中的filter表(-t filter)的最前面添加一条记录(-I),这次记录请求会匹配源地址为10.39.129.2(-s 10.37.129.2)的请求,并将这次请求丢掉(-j DROP)

删除iptables中的记录 1.根据编号删除

[root@localhost ~]# iptables -t filter -D INPUT 2
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  10.37.129.2          anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
July_filter  tcp  --  anywhere             anywhere            

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain July_filter (1 references)
target     prot opt source               destination    

-D表示delete 后边跟链名 删除的规则的编号

2.根据条件删除

[root@localhost ~]# iptables -t filter -D INPUT -s 10.37.129.2 -j DROP
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
July_filter  tcp  --  anywhere             anywhere            

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain July_filter (1 references)
target     prot opt source               destination  

删除INPUT链中的filter表中的源地址为10.37.129.2,并且动作为DROP

3.清空

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

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain July_filter (0 references)
target     prot opt source               destination       

代表清空INPUT链中的filter表中的所有规则 若不指定链表,可以直接使用iptables -F

修改规则

iptables -t filter -R INPUT 1 -s 10.37.129.3 -j ACCEPT

-R就是等同于replace,替换的意思 整句话的意思是将INPUT链中的filter表中替换编号为1的规则,编号1后面的就是要换成的新的规则

修改策略

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

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

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain July_filter (0 references)
target     prot opt source               destination    

-p:policy 策略的意思 这句话的意思是要把FORWARD链的默认规则设置为DROP

保存规则

service iptables save

-d:destination,用于匹配报文的目标地址,可以同时指定多个

iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP  //多个ip
iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT       //网段
iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT     //!  取反

-p:用于匹配报文的协议类型

iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT
# 感叹号表示“非”,即除了匹配这个条件的都ACCEPT,但匹配这个条件不一定就是REJECT或DROP?这要看是否有为它特别写一条规则,如果没有写就会用默认策略:
iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

-i:用于匹配报文是从哪个网卡流入本机的,由于匹配条件只是用于匹配报文流入的网卡,所以在OUTPUT链与POSTROUTING链中不能使用此选项

iptables -t filter -I INPUT -p icmp -i eth0 -j DROP
iptables -t filter -I INPUT -p icmp ! -i eth0 -j DROP

-o:用于匹配报文将要从哪个网卡接口流出本机,于匹配条件只是用于匹配报文流出的网卡,所以在INPUT链与PREROUTING链中不能使用此选项

iptables -t filter -I OUTPUT -p icmp -o eth0 -j DROP
iptables -t filter -I OUTPUT -p icmp ! -o eth0 -j DROP

利用tcp-flags限制nmap扫描

# iptables -t filter -I INPUT -s 192.168.64.128 -p tcp -m tcp --dport 22 --tcp-flags ALL SYN  -j REJECT 
使用nmap进行扫描
┌──(root💀kali)-[~]
└─# nmap -sS 192.168.64.128
Starting Nmap 7.91 ( https://nmap.org ) at 2022-01-16 21:28 CST
Nmap scan report for 192.168.64.128
Host is up (0.00017s latency).
Not shown: 998 closed ports
PORT    STATE SERVICE
22/tcp  open  ssh
111/tcp open  rpcbind
MAC Address: 00:0C:29:13:96:29 (VMware)

Nmap done: 1 IP address (1 host up) scanned in 0.16 seconds

脚本宝典总结

以上是脚本宝典为你收集整理的网络安全——防火墙详解全部内容,希望文章能够帮你解决网络安全——防火墙详解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: