温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iptables从入门到应用的实例分析

发布时间:2021-11-03 16:43:24 来源:亿速云 阅读:173 作者:柒染 栏目:建站服务器

iptables从入门到应用的实例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

iptables从入门到应用

 一、简介

 1.1、是什么?

    iptables是隔离主机以及网络的工具,通过自己设定的规则以及处理动作对数据报文进行检测以及处理。 

 1.2、发展史

    防火墙的发展史就是从墙到链再到表的过程,也即是从简单到复杂的过程。为什么规则越来越多,因为互联网越来越不安全了,所有防火墙的的规则也越来越复杂。防火的工具变化如下:

    ipfirewall(墙)-->ipchains(链条)--iptables(表)

    2.0版内核中,包过滤机制是ipfw,管理工具是ipfwadm;

    2.2 版内核中,包过滤机制ipchain,管理工具是ipchains;

    2.4版及以后的内核中,包过滤机制是netfilter,管理工具iptables。

 二、原理

iptables [-t table] {-A|-C|-D} chain rule-specification

  iptables [-t table] -I chain [rulenum] rule-specification

  iptables [-t table] -R chain rulenum rule-specification

  iptables [-t table] -D chain rulenum

  iptables [-t table] -S [chain [rulenum]]

  iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

  iptables [-t table] -N chain

  iptables [-t table] -X [chain]

  iptables [-t table] -P chain target

  iptables [-t table] -E old-chain-name new-chain-name

    rule-specification = [matches...] [target]

   match = -m matchname [per-match-options]

   target = -j targetname [per-target-options]

 3.4、语法结构解析

  语法结构解析如下。

    -N, --new-chain chain:新建一个自定义的规则链;

    -X, --delete-chain [chain]:删除用户自定义的引用计数为0的空链;

    -F, --flush [chain]:清空指定的规则链上的规则;

    -E, --rename-chain old-chain new-chain:重命名链;

    -Z, --zero [chain [rulenum]]:置零计数器;

      注意:每个规则都有两个计数器

      packets:被本规则所匹配到的所有报文的个数;

      bytes:被本规则所匹配到的所有报文的大小之和;

    -P, --policy chain target 制定链表的策略(ACCEPT|DROP|REJECT)

    -A, --append chain rule-specification:追加新规则于指定链的尾部; 

    -I, --insert chain [rulenum] rule-specification:插入新规则于指定链的指定位置,默认为首部;

    -R, --replace chain rulenum rule-specification:替换指定的规则为新的规则;

    -D, --delete chain rulenum:根据规则编号删除规则;

    -D, --delete chain rule-specification:根据规则本身删除规则;

    规则显示      

1

2

3

4

5

6

7

     -L, --list [chain]:列出规则;

     -v, --verbose:详细信息; 

     -vv 更详细的信息

     -n, --numeric:数字格式显示主机地址和端口号;

     -x, --exact:显示计数器的精确值,而非圆整后的数据;

     --line-numbers:列出规则时,显示其在链上的相应的编号;

     -S, --list-rules [chain]:显示指定链的所有规则;

 3.4.3、链

    五链的功能如图所示。   

iptables从入门到应用的实例分析

 3.4.4、条件匹配

    条件匹配分为基本匹配和扩展匹配,扩展匹配又分为显示匹配和隐式匹配。

    基本匹配的特点是:无需加载扩展模块,匹配规则生效;扩展匹配的特点是:需要加载扩展模块,匹配规则方可生效。

    隐式匹配的特点:使用-p选项指明协议时,无需再同时使用-m选项指明扩展模块以及不需要手动加载扩展模块;  

    显示匹配的特点:必须使用-m选项指明要调用的扩展模块的扩展机制以及需要手动加载扩展模块。

    基本匹配的使用选项及功能

1

2

3

4

5

6

-p 指定规则协议,tcp udp icmp all

-s 指定数据包的源地址,ip hostname

-d 指定目的地址

-i 输入接口

-o 输出接口                                              

! 取反

    隐式匹配的使用选项及功能 

-p tcp

  --sport 匹配报文源端口;可以给出多个端口,但只能是连续的端口范围 

  --dport 匹配报文目标端口;可以给出多个端口,但只能是连续的端口范围

  --tcp-flags mask comp 匹配报文中的tcp协议的标志位

-p udp

  --sport 匹配报文源端口;可以给出多个端口,但只能是连续的端口范围

  --dport 匹配报文目标端口;可以给出多个端口,但只能是连续的端口范围

--icmp-type

  /0: echo reply 允许其他主机ping

  8/0echo request 允许ping其他主机

   

  • 1

    2

    3

    例子:

            iptables -I INPUT -d 172.16.100.7 -p tcp -m multiport --dports 22,80 -j ACCEPT

            iptables -I OUTPUT -s 172.16.100.7 -p tcp -m multiport --sports 22,80 -j ACCEPT

  •     2.iprange(ip范围)

         以连续地址块的方式来指明多IP地址匹配条件。

    1. 1

      2

      3

      例子:

          iptables -A INPUT -d 172.16.100.7 -p tcp --dport 23 -m iprange --src-range 172.16.100.1-172.16.100.100 -j ACCEPT

          iptables -A OUTPUT -s 172.16.100.7 -p tcp --sport 23 -m iprange --dst-range 172.16.100.1-172.16.100.100 -j ACCEPT

        3.time(时间范围)

         指定时间范围。

    1. 1

      2

      3

       例子:

              iptables -A INPUT -d 172.16.100.7 -p tcp --dport 901 -m time --weekdays Mon,Tus,Wed,Thu,Fri --timestart 08:00:00 --time-stop 18:00:00 -j ACCEPT

              iptables -A OUTPUT -s 172.16.100.7 -p tcp --sport 901 -j ACCEPT

        4.string(字符串)

         对报文中的应用层数据做字符串模式匹配检测(通过算法实现)。

    1. 1

      2

      3

          --algo {bm|kmp}:字符匹配查找时使用算法

          --string "STRING": 要查找的字符串

          --hex-string “HEX-STRING”: 要查找的字符,先编码成16进制格式

        5.connlimit(连接限制)

         根据每个客户端IP作并发连接数量限制。

    1

    2

        --connlimit-upto n  连接数小于等于n时匹配

        --connlimit-above n 连接数大于n时匹配

        6.limit(速率限制)

         报文速率控制。  

        7.state(状态)

         追踪本机上的请求和响应之间的数据报文的状态。状态有五种:INVALID, ESTABLISHED, NEW, RELATED, UNTRACKED.

    iptables从入门到应用的实例分析

    1.  法则:

    2.         1、对于进入的状态为ESTABLISHED都应该放行;

    3.         2、对于出去的状态为ESTABLISHED都应该放行;

    4.         3、严格检查进入的状态为NEW的连接;

    5.         4、所有状态为INVALIED都应该拒绝;  

       

     3.4.5、处理动作

        处理动作有内置的处理动作和自定义的处理动作。自定义的处理动作用的比较少,因此只介绍内置的处理动作。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    ACCEPT:允许数据包通过

     

    DROP:直接丢弃数据包,不给出任何回应信息

     

    REJECT:拒绝数据包通过,必要时会给数据发送端一个响应信息

     

    LOG:在日志文件中记录日志信息,然后将数据包传递给下一条规则

     

    QUEUE: 防火墙将数据包移交到用户空间

     

    RETURN:防火墙停止执行当前链中的后续Rules,并返回到调用链 

     

    REDIRECT:端口重定向  

     

    MARK:做防火墙标记

     

    DNAT:目标地址转换

     

    SNAT:源地址转换   

                                                     

    MASQUERADE:地址伪装

     3.5、保存和载入规则

        CentOS6和CentOS7保存和载入的规则稍有差异。

     


        保存:iptables-save > /PATH/TO/SOME_RULE_FILE   

       重载:iptabls-restore < /PATH/FROM/SOME_RULE_FILE    

        -n, --noflush:不清除原有规则    

        -t, --test:仅分析生成规则集,但不提交

     

        保存规则:service iptables save   #保存规则于/etc/sysconfig/iptables文件,覆盖保存;   

       重载规则:service iptables restart #默认重载/etc/sysconfig/iptables文件中的规则   

       配置文件:/etc/sysconfig/iptables-config

    四、iptables的实践应用

        iptables十分重要与网络的安全息息相关,我们理所应当掌握。不过我们大可不必死记硬背,一定结合实际项目,多多练习,效果才会更好。

     4.1、iptables常用规则

        1.放行sshd服务

    1

    2

     iptables -t filter -A INPUT -s 192.168.0.0/24 -d 192.168.0.1 -p tcp --dport 22 -j ACCEPT

     iptables -t filter -A OUTPUT -s 192.168.0.1  -p tcp --sport 22 -j ACCEPT


  •  iptables -I OUTPUT -s 192.168.0.1 -p tcp --sport 80 -j ACCEPT

     iptables -I INPUT -d 192.168.0.1 -p tcp --dport 80 -j ACCEPT

    1

    2

    3

      iptables -A  INPUT  -s 127.0.0.1 -d 127.0.0.1 -i lo -j ACCEPT

      iptables -A  OUTPUT  -s 127.0.0.1 -d 127.0.0.1 -o lo -j ACCEPT

      #不放行本机的流入与流出,访问本机的httpd服务,网页会出现Error establishing a database connection。

    1

    iptables -A INPUT -i ens33 -d 192.168.0.1 -p icmp --icmp-type 8 -m limit --limit 2/second --limit-burst 3 -j ACCEPT

     4.2、如何配置iptables

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    a) 1. 删除现有规则

    iptables -F

    b) 2. 配置默认链策略

    iptables -P INPUT DROP

    iptables -P FORWARD DROP

    iptables -P OUTPUT DROP

    c) 3. 允许远程主机进行SSH连接

    iptables -A INPUT -i eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT

    iptables -A OUTPUT -o eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT

    d) 4. 允许本地主机进行SSH连接

    iptables -A OUTPUT -o eth0 -p tcp –dport 22 -m state –state NEW,ESTABLISHED -j ACCEPT

    iptables -A INPUT -i eth0 -p tcp –sport 22 -m state –state ESTABLISHED -j ACCEPT

    e) 5. 允许HTTP请求

    iptables -A INPUT -i eth0 -p tcp –dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT

    iptables -A OUTPUT -o eth0 -p tcp –sport 80 -m state –state ESTABLISHED -j ACCEPT

     4.3、iptables初始化脚本

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    #!/bin/bash

      

    echo &quot;Setting firewall . . . . start&quot;

      

    #--------RULESET INIT----------#

    iptables -F

    iptables -X

    iptables -P INPUT DROP

    iptables -P OUTPUT ACCEPT

    iptables -P FORWARD ACCEPT

    iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

    #------------------------------#

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

    iptables -A INPUT -i eth0 -p tcp ! --syn -j ACCEPT

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

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

    #------------------------------#

    #zabbix

    iptables -A INPUT -p tcp --destination-port 10050 -j ACCEPT

    iptables -A INPUT -p udp --destination-port 10051 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 10050 -j ACCEPT

    iptables -A OUTPUT -p udp --destination-port 10051 -j ACCEPT

    #for web

    iptables -A INPUT -p tcp --destination-port 21 -j ACCEPT

    iptables -A INPUT -p tcp --destination-port 80 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 80 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 21 -j ACCEPT

    #for mysql

    iptables -A INPUT -p tcp --destination-port 3306 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 3306 -j ACCEPT

    #for mail

    iptables -A INPUT -p tcp --destination-port 25 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 25 -j ACCEPT

    iptables -A OUTPUT -p tcp --destination-port 110 -j ACCEPT

    #for ssh

    iptables -A INPUT -p tcp -s any/0 --destination-port 22 -j ACCEPT

    iptables -N icmp_allowed

    iptables -A icmp_allowed -p ICMP --icmp-type 11 -j ACCEPT

    iptables -A icmp_allowed -p ICMP --icmp-type 8 -j ACCEPT

    iptables -A icmp_allowed -p ICMP -j DROP

    iptables -A OUTPUT -p icmp -j ACCEPT

    iptables -A INPUT -p icmp -j ACCEPT

    iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT

    iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

    iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT

    /etc/init.d/iptables save

限制ping 192.168.0.1主机的数据包数,平均2/s个,最多不能超过3个

放行本机端的流入流出

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI