Linux----iptables防火墙!

Linux防火墙基础

Linux的防火墙主要工作在网络层,对数据包进行过滤和限制,体现在对数据包内IP地址、端口信息的处理上

  • netfilter:指的是Linux内核中实现包过滤防火墙的内部结构,不以程序或文件的形式存在,属于“内核态”的防火墙功能体系
  • iptables:指的是用来管理Linux防火墙的命令程序,属于“用户态”的防火墙管理体系

iptables的表、链结构

规则表

规则表容纳各种规则链

  • raw表 :确定是否对该数据包进行状态跟踪
  • mangle表 :为数据包设置标记
  • nat表 :修改数据包中的源、目标IP地址或端口
  • filter表 :确定是否放行该数据包(过滤)

规则表之间的顺序
raw➡mangle➡nat➡filter

规则链

规则链对数据包进行过滤或处理,容纳各种防火墙规则,处理数据包的不同时机,按顺序一次检测,匹配即停止,若找不到相匹配的规则,则按该链的默认策略处理

  • INPUT :处理入站数据包
  • OUTPUT :处理出站数据包
  • PORWARD :处理转发数据包
  • POSTROUTING链 :在进行路由选择后处理数据包
  • PREROUTING链 :在进行路由选择前处理数据包

规则链之间的顺序
入站 :PREROUTING➡INPUT
出战 :OUTPUT➡POSTROUTING
转发 :PREROUTING➡FORWARD➡POSTROUTING

设置iptables防火墙

[root@localhost ~]# systemctl stop firewalld.service    //CentOS 7要想使用iptables防火墙必须先关闭firewalld防火墙
[root@localhost ~]# yum install -y iptables iptable-services        //安装iptables防火墙
[root@handsomeboy1 ~]# systemctl start iptables         //开启iptables防火墙
iptable [-t 表名] 选项 [链名] [匹配条件] [-j 控制类型]
控制类型 :
	ACCEPT :允许数据包通过
	DROP :直接丢弃数据包,不给出任何回应消息,
	REJECT :拒绝数据包通过,必要时会给发送端一个响应消息
	LOG :记录日志信息,然后传给下一条规则继续匹配
注意事项 :
 - 不知道表名时,默认指filter表
 - 不指定链名时,默认指表内的所有链
 - 除非设置链的默认策略,否则必须指定匹配条件
 - 选项、链名、控制类型使用大写字母

iptables命令常用管理选项

-A :在指定链的末尾添加新规则
-D :删除指定链中的某一条规则,可指定规则序号或具体内容
-I :在指定链中插入一条新规则,为指定序号时默认作为第一条规则
-R :修改、替换指定链中的某一条规则,可指定规则号或具体内容
-L :列出指定链中的所有规则,若为指定链名,则列出表中的所有链
-F :清空指定链中的所有规则,若未指定链名,则清空表中的所有连链
-P :设置指定链的默认策略
-n :使用数字形式显示输出结果,如显示IP 地址而不是主机名
-v :查看规则列表时显示详细信息
--line-numbers :查看规则列表时,同时显示规则在链中的顺序号
  • 阻止所有ping测试
[root@handsomeboy1 ~]# iptables -t filter -I -INPUT -p icmp -j REJECT
  • 查看规则列表
[root@handsomeboy1 ~]# iptables -t filter -L           //查看filter中的所有规则
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  
[root@handsomeboy1 ~]# iptables -tfilter -nvL         //以数字形式查看filter所有规则的更详细信息
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  211 14564 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
    5   573 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

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 138 packets, 14832 bytes)
 pkts bytes target     prot opt in     out     source               destination 
 [root@handsomeboy1 ~]# iptables -L INPUT --line-numbers        //显示filter表中INPUT链的规则号
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     all  --  anywhere             anywhere            
4    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
5    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[root@handsomeboy1 ~]# iptables -t filter -S           //以另外一种形式查看表中规则
-P INPUT ACCEPT       //-P后面表示的是默认规则
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
[root@handsomeboy1 ~]# iptables -S INPUT 3                //查看INPUT链第三条规则
-A INPUT -i lo -j ACCEPT
  • 删除、清空规则
[root@handsomeboy1 ~]# iptables -D INPUT 3              //删除INPUT链中第三条规则
[root@handsomeboy1 ~]# iptables -F                      //清空filter表所有内容
[root@handsomeboy1 ~]# iptables -X                      //清除自定义的链
[root@handsomeboy1 ~]# iptables -Z                      //清除表内计数器
  • 设置规则匹配条件

1)设置默认规则

[root@handsomeboy1 ~]# iptables -P FORWARD DROP            //设置FORWARD链的默认规则为丢弃数据包,设置默认规则不用加-j    
[root@handsomeboy1 ~]# iptables -P OUTPUT ACCETP          //设置出战的默认规则为放通所有数据包

2)设置通用匹配条件

  • 设置协议配置
[root@handsomeboy1 ~]# iptables -I INPUT -p icmp -j DROP       //拒绝其他人ping本机
[root@handsomeboy1 ~]# iptables -A FORWARD ! -p icmp -j ACCEPT   //加上感叹号表示取反的意思
  • 地址匹配
[root@handsomeboy1 ~]# iptables -A FORWARD -s 192.168.43.55 -j REJECT
[root@handsomeboy1 ~]# iptables -A FORWARD -s 192.168.43.0/24 -j ACCEPT
  • 网卡匹配
[root@handsomeboy1 ~]# iptables -A INPUT -i ens33 -s 192.168.43.0/24 -j REJECT
[root@handsomeboy1 ~]# iptables -A OUTPUT -o ens33 -s 192.168.43.0/24 -j REJECT
#### “INPUT -i 接口名”和“OUTPUT -o 接口名”分别用于检查防火墙的网卡接口进入或发出

3)设置隐含匹配

  • 端口匹配
[root@localhost ~]# iptables -A FORWARD -s 192.168.43.0/24 -udp --dport 53 -j ACCEPT
[root@localhost ~]# iptables -A FORWARD -s 192.168.43.0/24 -udp --sport 53 -j ACCEPT
#### "--sport"表示源端口,“--dport”表示目标端口
[root@localhost ~]# iptables  -A INPUT -p tcp --dport 20:23 -j ACCEPT   //设置范围端口匹配
  • ICMP类型匹配
echo-request :代码为8
echo-reply :代码为0
destination-unreachable :代码为3
[root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 -s 192.168.43.55 -j DROP
#### 拒绝来自该网址的reques数据包

4)显式匹配

  • 多端口匹配
[root@localhost ~]# iptables -A INPUT -p tcp -m multiport --dport 25,80,110 -j ACCEPT
#### “-m multiport” 端口列表
  • IP范围匹配
[root@localhost ~]# iptables -A INPUT -p tcp -m iprange --src-range 192.168.43.10-192.168.43.60 -j ACCEPT
#### “-m iprange --src-range IP范围”允许别人ping自己
  • MAC地址匹配
[root@localhost ~]# iptables -A INPUT -m mac --mac-source MAC地址 -j DROP
#### 禁止该MAC地址主机的数据包
  • 状态匹配
NEW :想要新建立联机的封包状态
ESTABLISHED :已经联机成功的联机状态
RELATED ;与已有连接有相关性的
[root@localhost ~]# iptables -A INPUT -m state --state NEW -p tcp ! --syn -j DROP
#### 禁止接受与正常TCP连接无关的非--syn请求数据包
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值