Iptables从简单到深入

1.  Iptables防火墙简介

   unix/linux自带的一款优秀且开放源代码的完全自由的基于包过滤的防火墙工具。

   主要工作在OSI七层的二,三,四层。如果重新编译内核,也可以支持7层控制。

2.  Iptables的名词和术语

   1.  什么是容器?

      在Iptables里面用来描述这种包含或者属于的关系。

   2.  什么是Netfilter?

      Netfilter是表(tables)的容器。如果把Netfilter看成是某小区的一栋楼,那么表就是楼里的其中一套房子。

   3.  什么是表(tables)?

      表是链的容器。

   4.  什么是链(chains)?

      链是规则的容器。如果表是房子,那么链就是家具。

   5.  什么是规则(Policy)?

      iptables过滤信息的规范和具体方法。

      

3.  Iptables工作流程

   

   1.  按照规则的顺序从上到下,从前到后进行过滤的。

   2.  最后都有一个默认规则。最后执行默认规则。

   3.  如果数据报匹配上规则,就不会再向下匹配。

4.  Iptables的表和链

   

   

5.  表中链的作用

   

   

   

 6.  Iptables表和链的工作流程图

   

   

 7.  Iptables安装

   centos5.8或者centos6.4都是自带iptables工具的。

   centos7以上版本,防火墙使用firewalled,我们要把这个服务禁用,然后通过yum install iptables-services -y,安装iptables工具。

8.  Iptables的命令行配置

   iptables -V   查看版本

   iptables -h 查看帮助

   iptables -L -n -t filter  查看filter表的所有链信息

      iptables -L -n -t nat  查看nat表的所有链信息

   iptables-save  列出防火墙所有规则

   iptables -F -t filter|nat  清空所有的规则,默认规则不会被清除

   iptables -X  清除用户自定义的链

   iptables -Z  清除所有的链的计数

9.  Iptables的添加filter表的规则

   1.  添加所有的主机到本地主机的22端口,都被丢弃。

      iptables -t filter -A INPUT -p tcp --dport 22 -j DROP

      查看一下我添加的规则:iptables -L -n -t filter

      

   2.  处理行为的区别:

      ACCEPT:接受

      DROP:丢弃  生产环境常用DROP

      REJECT:拒绝  会返回信息的

   3.  在此策略上添加一条,只允许192.168.0.100,需要使用-I。

      -I与-A的区别:-I默认会添加在规则第一条,-A默认会添加在规则最后一条。

      例子: 

      

      使用-A添加的规则会保存在INPUT的最后一条,因为iptables规则执行顺序,是从上到下,所以此规则不会生效。  

      使用-I添加的规则会保存在INPUT的第一条,所以会生效。如下:

       还可以-D删除规则。

       

       可以使用--line-number查看规则的行号。

       -D可以使用行号删除指定的规则。

       -I也可以使用行号插入指定的规则到这个行号。

     4.  -i可以决定哪个接口的进出。

     5.  !表示非。

        iptables -I INPUT -p tcp ! -s 192.168.0.12 --dport 22 -j DROP

     6.  -p icmp禁止ping

       iptables -A INPUT -p icmp --icmp-type 8 -s 192.168.0.100 -j DROP

       -p [ tcp | udp |  all |  icmp ]

    7.  -s  某个Ip

       -s  某个网段/子网掩码

    8.  --sport 源端口号

       --sport   20:80   端口范围

       --sport   22,23,24,25  这种方式不支持

       要想使用上面的方式,需要前面加一个-m multiport。后面跟--sport   22,23,24,25,这种方式就支持。

       iptables -I INPUT -p tcp -s 192.168.0.100 -m multiport --dport 22,23,24 -j DROP

    9.  --dport  目标端口号

    10.   -d  目标网络

    11.  禁止连续的IP地址

        iptables -I INPUT --src-range 192.168.1.17-192.168.1.19 -j DROP        

10.  企业级防火墙案例

    1.  企业级配置防火墙的两种模式

       1.  配置网关。默认随便进出,对非法的分子进行拒绝。

       2.  配置主机防火墙。默认不能进,经过允许才能进入。

    2.  配置主机防火墙

       iptables -F

       iptables -X

       iptables -Z

       iptables -A INPUT -s 192.168.0.0/24 -j ACCEPT

       iptables -P INPUT DROP

       iptables -P FORWARD DROP

    3.  开启信任的IP源

       允许办公网出口IP访问主机  iptables  -A INPUT -s 222.222.222.222 -j ACCEPT

       允许任何地址访问80端口    iptables -A INPUT -p tcp --dport 80 -j ACCEPT

       允许任何地址访问443端口  iptables -A INPUT -p tcp --dport 443 -j ACCEPT

    4.  允许关联的状态包通过

       iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

       iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

    5.  保存配置的防火墙规则

       /etc/init.d/iptalbes save

       iptables-save >/etc/sysconfig/iptables

11.  生产环境如何维护防火墙

    1.  直接修改/etc/sysconfig/iptables文件,添加对应的规则语句。

       重启防火墙服务。/etc/init.d/iptables restart

    2.  手工封IP地址。

       iptables -I INPUT -s 10.0.0.1 -j DROP

    3.  自动封IP地址。

       分析web日志或者网络连接状态封掉垃圾IP

       netstat -an | grep ESTABLISHED

       分析web日志

12.  生产环境使用脚本配置防火墙

    1.  主机防火墙

[root@private ~]# cat iptables_script.sh 
#!/bin/bash
modprobe ip_tables
modprobe iptable_nat
modprobe pf_conntrack
iptables -F
iptables -X
iptables -Z
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -X -t mangle
iptables -P INPUT DROP
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -t nat -P PERROUTING ACCEPT
iptables -t nat -P POSRROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

    2.  使用NAT上网

        1.  需要条件

            1.  网关要有双网卡

            2.  网关要可以上网

            3.  网关要开启路由转发功能

                net.ipv4.ip_forward=1

                sysctl -p  配置生效

            4.  网关要配置FORWARD链允许

                iptables -P FORWARD ACCEPT

            5.  加载iptables内核模块,执行并放入rc.local

                lsmod | egrep ^ip

                modprobe ip_tables

                modprobe iptable_filter

                modprobe ip_conntrack

                modprobe ip_conntrack_ftp

                modprobe ip_nat_ftp

                modprobe ipt_state

        2.  iptables配置192.168.1.0网段使用nat上网,--to-source为外网IP地址为10.0.0.19, -o eth0 为外网的网卡

            iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.19

        3.  如果网关IP地址不是固定IP地址

            iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

    3.  iptables的DNAT配置

        iptables -t nat -A PREROUTING -d 10.0.0.19 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.17:9000

    4.  实现外部IP地址一对一映射内部IP地址

        iptables -t nat -A PREROUTING -d 124.42.34.112 -j DNAT --to-destination 10.0.0.8

        iptables -t nat -A POSTROUTING -s 10.0.0.8 -o eth0 -j SNAT --to-source 124.42.34.112

        iptables -t nat -A POSTROUTING -s 10.0.0.0/255.255.240.0 -d 124.42.34.112 -j SNAT --to-source 10.0.0.254

13.  centos7安装iptables

    1.  检查是否安装iptables

        systemctl status iptables

    2.  安装iptables

        yum install -y iptables iptables-services

    3.  启动

        systemctl start iptables

    4.  开机自动启动

        systemctl enable iptables

 

 

 

       

    

       

       

           

       

          

    

   

 

   

  

posted @ 2018-03-27 22:21  奋斗史  阅读(251)  评论(0)    收藏  举报