iptables/netfilter

一 简介

    防火墙是指设置在不同网络或网络安全域之间的一系列部件的组合,它能增强机构内部网络的安全性。它通过访问控制机制,确定哪些内部服务允许外部访问。它可以根据网络传输的类型决定IP包是否可以传进或传出内部网
    防火墙通过审查经过的每一个数据包,判断它是否有相匹配的过滤规则,根据规则的先后顺序进行一一比较,直到满足其中的一条规则为止,然后依据控制机制做出相应的动作。如果都不满足,则将数据包丢弃,从而保护网络的安全

总结:
    工作在主机边缘处或者网络边缘处根据预定义规则对进出报文进行处理的模块

 

防火墙机制:

a. 一种机制是拦阻传输流通行(白名单)ssh
b. 一种机制是允许传输流通过(黑名单)http

一些防火墙偏重拦阻传输流的通行,而另一些防火墙则偏重允许传输流通过。


防火墙实现功能:

1. 可以保护易受攻击的服务;
2. 控制内外网之间网络系统的访问;
3. 集中管理内网的安全性,降低管理成本;
4. 提高网络的保密性和私有性;
5. 记录网络的使用状态,为安全规划和网络维护提供依据

 

安全产品分类

基础类防火墙:
      包过滤防火墙 IDS 类防火墙:
      入侵检测系统 提供事后日志分析 IPS 类防火墙:
      入侵防御系统 主动匹配特征码拒绝数据报文 主动类防火墙:
      WAF DAF

 

防火墙演替

  netfilter/iptables 分别是内核态模块和用户态工具,netfilter位于Linux内核中的包过滤功能体系,iptables 位于/sbin/iptables,用来管理防火墙规则的工具,
管理员通过iptables给netfilter变更规则实现防火作用 kernel
2.0.x firewall ipfw kernel 2.2.x firewall ipchains kernel 2.4.x netfilter iptables kernel 3.13.x netfilter firewall

 

二 表和链

netfilter/iptables 预设的规则表 
    表作用:容纳各种规则链
    划分依据:根据防火墙对数据的处理方式

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

 

netfilter/iptables 预设的规则链:
    规则的作用:对数据包进行过滤或处理
    链的作用:容纳各种防火墙规则
    链的分类依据:处理数据包的不同时机
规则链: INPUT:处理入站数据包 OUTPUT:处理出站数据包 FORWARD:处理转发数据包 POSTROUTING链:在进行路由选择处理数据包 PREROUTING链:在进行路由选择处理数据包

 

链表结构

     

 

防火墙应用顺序

表之间的顺序
    raw — mangle — nat — filter

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

规则链内的匹配顺序
    按顺序依次检查,匹配即停止(LOG策略例外)
    若找不到相匹配规则,则按该链的默认策略处理

 

数据匹配流程

     

 

三 指定防火墙规则

语法构成

iptables  [-t 表名]  选项  [链名]  [条件]  [-j 控制类型]

 

注意事项

1. 不指定表名时,默认指filter表
2. 不指定链名时,默认指表内的所有链
3. 除非设置链的默认策略,否则必须指定匹配条件
4.选项、链名、控制类型使用大写字母,其余均为小写

 

设置防火墙命令

设置iptables开机启动
        chkconfig iptables on
        chkconfig iptables off

即时启动iptables
        service iptables start

即时关闭iptables
        service iptables stop

 

保存和恢复iptables规则

防火墙规则只在计算机处于开启状态时才有效。如果系统被重新引导,这些规则就会自动被清除并重设。要保存规则以便今,请使用以下命令:后载入
        sbin/service iptables save

保存在 /etc/sysconfig/iptables  文件中的规则会在服务启动或重新启动时(包括机器被重新引导时)被应用

 

数据包的常见控制类型

ACCEPT:
    允许通过 DROP:
    直接丢弃,不给出任何回应 REJECT:
    拒绝通过,必要时会给出提示 LOG:
    记录日志信息,然后传给下一条规则继续匹配 SNAT:
    修改数据包源地址 DNAT:
    修改数据包目的地址 REDIRECT:
    重定向

 

添加新的规则

-A:在链的末尾追加一条规则
-I:在链的开头(或指定序号)插入一条规则
[root@localhost ~]# service iptables start
iptables:应用防火墙规则:                                 [确定]
[root@localhost ~]# service iptables status



[root@localhost ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 

[root@localhost ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 

[root@localhost ~]# iptables -t filter -I INPUT 2 -p tcp --dport 443 -j ACCEPT   #插入到第2个位置
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https     #443
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 


[root@localhost ~]# iptables -t filter -I INPUT -p tcp --dport 8080 -j ACCEPT  #不指定位置插入(默认第一个)
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:webcache    #8080
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 
示例

 

查看规则列表

-L:列出所有的规则条目
-n:以数字形式显示地址、端口等信息
-v:以更详细的方式显示规则信息
--line-numbers:查看规则时,显示规则的序号
#拒绝80
[root@localhost ~]# iptables -A INPUT -p tcp --dport 80 -j REJECT
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere            tcp dpt:http reject-with icmp-port-unreachable 

[root@localhost ~]# curl 192.168.10.10
curl: (7) couldn't connect to host



root@localhost ~]# iptables -A INPUT -p tcp --dport 80 -j DROP
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  anywhere             anywhere            tcp dpt:http 

[root@localhost ~]# curl 192.168.10.10   #在192.168.10.20主机测试
    #等待中




#-n

[root@localhost ~]# iptables -t filter -I INPUT -p tcp --dport 10050 -j ACCEPT

[root@localhost ~]# iptables -L 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:zabbix-agent 

[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:10050 



#-v

[root@localhost ~]# iptables -L -n -v
Chain INPUT (policy ACCEPT 86 packets, 6509 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:10050 
示例

 

删除、清空规则

-D:删除链内指定序号(或内容)的一条规则
-F:清空所有的规则
#删除单个

[root@localhost ~]# iptables -L --line-numbers   #有两列
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:zabbix-agent 
2    REJECT     tcp  --  anywhere             anywhere            tcp dpt:http reject-with icmp-port-unreachable 
 
[root@localhost ~]# iptables -t filter -D INPUT 2   #删除第二列
[root@localhost ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:zabbix-agent 



#删除全部

[root@localhost ~]# iptables -t filter -F
示例

 

设置默认策略

-P:为指定的链设置默认规则
[root@localhost ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -t filter -P INPUT REJECT  #不能使用 REJECT
iptables: Bad policy name. Run `dmesg' for more information.#
[root@localhost ~]# 
[root@localhost ~]# iptables -t filter -P INPUT DROP  #使用DROP
[root@localhost ~]# iptables -L
Chain INPUT (policy DROP)       #!!!
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 



#自己访问自己
    [root@localhost ~]# curl localhost
    hello world

其他访问者
    [root@localhost ~]# curl 192.168.10.10   #在192.168.10.20主机测试
        #等待中
示例

 

总结:

      

 

配置类型

通用匹配
  可直接使用,不依赖于其他条件或扩展
  包括网络协议、IP地址、网络接口等条件

隐含匹配
  要求以特定的协议匹配作为前提
  包括端口、TCP标记、ICMP类型等条件

显式匹配
  要求以“-m 扩展模块”的形式明确指出类型
  包括多端口、MAC地址、IP范围、数据包状态等条件

 

通用匹配

协议匹配:-p 协议名
地址匹配:-s 源地址、-d 目的地址
接口匹配:-i 入站网卡、-o 出站网卡


# iptables -A FORWARD -s 192.168.1.11 -j REJECT
# iptables -I INPUT -s 10.20.30.0/24 -j DROP
# iptables -I INPUT -p icmp -j DROP
# iptables -A FORWARD -p ! icmp -j ACCEPT
# iptables -A INPUT -i eth1 -s 172.16.0.0/12 -j DROP

 

隐含匹配

端口匹配:--sport 源端口、--dport 目的端口
ICMP类型匹配:--icmp-type ICMP类型

# iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCEPT # iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT # iptables -A INPUT -p icmp --icmp-type 8 -j DROP # iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT # iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT # iptables -A INPUT -p icmp -j DROP
192.168.10.10  服务端
192.168.10.20  客户端


效果:10  可ping 20
       20  不可ping 10


192.168.10.10
[root@localhost ~]# service iptables start
iptables:应用防火墙规则:                                 [确定]
[root@localhost ~]# chkconifg iptables on
-bash: chkconifg: command not found
[root@localhost ~]# chkconfig iptables on
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination     




[root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p icmp -j DROP
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            icmp echo-reply 


#此时两者 都ping不通



[root@localhost ~]# iptables -D INPUT 2
[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 0 -j ACCEPT  #此时 -I
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            icmp echo-reply 
DROP       icmp --  anywhere             anywhere  



#此时 10 可以ping 20
[root@localhost ~]# ping 192.168.10.20
PING 192.168.10.20 (192.168.10.20) 56(84) bytes of data.
64 bytes from 192.168.10.20: icmp_seq=1 ttl=64 time=2.56 ms
64 bytes from 192.168.10.20: icmp_seq=2 ttl=64 time=1.31 ms



20 不可ping 10



------------------------------------------------------------------------


[root@localhost ~]# ping 192.168.10.30
   #一直会等待


[root@localhost ~]# iptables -I INPUT -p icmp --icmp-type 3 -j ACCEPT
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            icmp destination-unreachable 
ACCEPT     icmp --  anywhere             anywhere            icmp echo-reply 
DROP       icmp --  anywhere             anywhere     


[root@localhost ~]# ping 192.168.10.30    #此时才是完整的ping。ping不通依然给我信息
PING 192.168.10.30 (192.168.10.30) 56(84) bytes of data.
From 192.168.10.10 icmp_seq=1 Destination Host Unreachable
From 192.168.10.10 icmp_seq=2 Destination Host Unreachable
        
示例

 

显式匹配

多端口匹配:
      -m multiport --sport 源端口列表 -m multiport --dport 目的端口列表 IP范围匹配:
      -m iprange --src-range IP范围 MAC地址匹配:
      -m mac –mac1-source MAC地址 状态匹配:
      -m state --state 连接状态


# iptables -A INPUT -p tcp -m multiport --dport 25,80,110,143 -j ACCEPT
# iptables -A FORWARD -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j ACCEPT
# iptables -A INPUT -m mac --mac-source 00:0c:29:c0:55:3f -j DROP
# iptables -P INPUT DROP
# iptables -I INPUT -p tcp -m multiport --dport 80-82,85 -j  ACCEPT
# iptables -I INPUT -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

 

四 策略

SNAT策略

SNAT策略的典型应用环境
      局域网主机共享单个公网IP地址接入Internet

SNAT策略的原理
      源地址转换,Source Network Address Translation
      修改数据包的源地址

 

局域网共享上网

     

 

未启用SNAT

     

 

 启用SNAT

    

 

前提条件
    局域网各主机正确设置IP地址/子网掩码
    局域网各主机正确设置默认网关地址
    Linux网关支持IP路由转发



192.168.10.20 做路由 [root@localhost ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@localhost ~]# sysctl -p [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# cp -a ifcfg-eth0 ifcfg-eth1 [root@localhost network-scripts]# vim ifcfg-eth1   IPADDR=192.168.88.111   NETMASK=255.255.255.0   GATEWAY=192.168.88.2   DNS1=8.8.8.8   DNS2=114.114.114.114 [root@localhost ~]# service iptables start iptables:应用防火墙规则: [确定] [root@localhost ~]# chkconfig iptables on [root@localhost ~]# iptables -F
#编写SNAT转换规则 [root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth1 -j SNAT --to-source 192.168.88.111 [root@localhost ~]# iptables -t nat -L   Chain POSTROUTING (policy ACCEPT)   target prot opt source destination   SNAT all -- 192.168.10.0/24 anywhere to:192.168.88.111 192.168.10.10 [root@localhost ~]# iptables -F [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0   IPADDR=192.168.10.10   NETMASK=255.255.255.0   GATEWAY=192.168.10.20 #网关   DNS1=114.114.114.114   DNS2=8.8.8.8

[root@localhost ~]# service network restart [root@localhost ~]# ping www.baidu.com   PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.   64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=1 ttl=127 time=6.32 ms   64 bytes from 127.0.0.1 (119.75.213.61): icmp_seq=2 ttl=127 time=7.02 ms

 

 MASQUERADE —— 地址伪装

适用于外网 IP地址 非固定的情况
对于ADSL 拨号连接,接口通常为 ppp0、ppp1
将SNAT规则改为 MASQUERADE 即可

 

示例:

192.168.10.20
[root@localhost network-scripts]# iptables -t nat -F
#编写规则 [root@localhost network-scripts]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o eth1 -j MASQUERADE

[root@localhost network-scripts]# iptables -t nat -L   Chain POSTROUTING (policy ACCEPT)   target prot opt source destination   MASQUERADE all -- 192.168.10.0/24 anywhere 192.168.10.10   [root@localhost ~]# ping www.baidu.com   PING www.a.shifen.com (119.75.216.20) 56(84) bytes of data.   64 bytes from 119.75.216.20: icmp_seq=5 ttl=127 time=5.31 ms   64 bytes from 119.75.216.20: icmp_seq=9 ttl=127 time=4.78 ms   64 bytes from 119.75.216.20: icmp_seq=10 ttl=127 time=6.07 ms

 

 

DNAT策略

DNAT策略的典型应用环境
      在Internet中发布位于企业局域网内的服务器

DNAT策略的原理
      目标地址转换,Destination Network Address Translation
      修改数据包的目标地址

 

示图:

     

 

     

 

示例

前提条件
    局域网的Web服务器能够访问Internet
    网关的外网IP地址有正确的DNS解析记录
    Linux网关支持IP路由转发

192.168.10.20(路由器)
[root@localhost network-scripts]# cp -a ifcfg-eth0 ifcfg-eth1
[root@localhost network-scripts]# vim ifcfg-eth1
[root@localhost network-scripts]# vim ifcfg-eth1

DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=20.20.20.12
NETMASK=255.255.255.0

[root@localhost network-scripts]# service network restart


20.20.20.13(外网) [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0   DEVICE=eth0   HWADDR=00:0C:29:57:F5:FA   TYPE=Ethernet   UUID=5e51f99b-ec82-4324-ab66-6f0a77666a91   ONBOOT=yes   NM_CONTROLLED=yes   BOOTPROTO=static   IPADDR=20.20.20.13   NETMASK=255.255.255.0 192.168.10.10(web服务器)   [root@localhost ~]# echo "hello world" > /var/www/html/index.html   [root@localhost ~]# service httpd start   [root@localhost ~]# curl localhost   hello world   [root@localhost ~]# route add default gw 192.168.10.20   [root@localhost ~]# route -n   Kernel IP routing table   Destination Gateway Genmask Flags Metric Ref Use Iface   192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0   169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0   0.0.0.0 192.168.10.20 0.0.0.0 UG 0 0 0 eth0

测试:没有成功
192.168.10.30
  [root@localhost ~]# curl 20.20.20.12
  curl: (7) couldn't connect to host


设置规则 192.168.10.20
  [root@localhost ~]# vim /etc/sysctl.conf
      net.ipv4.ip_forward = 1

  [root@localhost ~]# sysctl -p
  [root@localhost ~]# service iptables start
  iptables:应用防火墙规则:                                 [确定]

  [root@localhost ~]# iptables -F
  [root@localhost ~]# service iptables save
  iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]

  #设置规则
  [root@localhost ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -i eth1 -j DNAT --to-destination


192.168.10.10:80
[root@localhost ~]# iptables -t nat -L
  Chain PREROUTING (policy ACCEPT)
  target     prot opt source               destination         
  DNAT       tcp  --  anywhere             anywhere            tcp dpt:http to:192.168.10.10:80



再次测试:
192.168.10.30
  [root@localhost ~]# curl 20.20.20.12
  hello world

---------------------------
192.168.10.20
  [root@localhost ~]# hostname www.tomcat.com
  [root@localhost ~]# hostname
  www.tomcat.com

192.168.10.30
  [root@localhost ~]# ssh root@20.20.20.12   #只映射到 80端口,没有到 22端口
  [root@www ~]# hostname
  www.tomcat.com

#都映射到 192.168.10.10服务器上
192.168.10.20
  [root@localhost ~]# iptables -F
  [root@localhost ~]# iptables -t nat -A PREROUTING -i eth1 -j DNAT --to-destination 192.168.10.10


20.20.20.13
  [root@localhost ~]# ssh root@20.20.20.12
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  RSA host key for 20.20.20.12 has changed and you have requested strict checking.
  Host key verification failed.

  [root@localhost ~]# cd .ssh/
  [root@localhost .ssh]# ls
  known_hosts
  [root@localhost .ssh]# mv known_hosts /tmp/  #移除


  [root@localhost ~]# ssh root@20.20.20.12  #此时再连接
  [root@www ~]# hostname
  www.server10.com


 

五 防火墙脚本

导出(备份)规则:iptables-save工具
    可结合重定向输出保存到指定文件

导入(还原)规则:iptables-restore工具
    可结合重定向输入指定规则来源

iptables服务
      脚本位置:/etc/init.d/iptables
      规则文件位置:/etc/sysconfig/iptables

 

脚本示例  --> test.iptables

[root@localhost ~]# bash test.iptables 
============================iptables configure============================================
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:     [确定]
iptables:将链设置为政策 ACCEPT:nat filter                [确定]
iptables:清除防火墙规则:                                 [确定]
iptables:正在卸载模块:                                   [确定]
iptables:应用防火墙规则:                                 [确定]
============================iptables configure completed============================================

 

Centos7 更改Firewall 为 iptables

rpm -e --nodeps firewalld
yum
-y install iptables-services
systemctl start iptables
systemctl enable iptables
service iptables save

 

1、直接关闭防火墙

  systemctl stop firewalld.service #停止firewall

  systemctl disable firewalld.service #禁止firewall开机启动

2、设置 iptables.service   yum -y install iptables-services   如果要修改防火墙配置,如增加防火墙端口3306     vi /etc/sysconfig/iptables          -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #增加规则   systemctl restart iptables.service #重启防火墙使配置生效   systemctl enable iptables.service #设置防火墙开机启动

3.SElinux
永久关闭: 
 1. vim /etc/selinux/config
    SELINUX=disabled  2. 重启服务器

#补充:
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config #需重启

临时关闭:
  setenforce 0 #临时关闭,不用重启生效

 

posted @ 2018-05-28 23:12  shadow3  阅读(241)  评论(0)    收藏  举报