firewalld && iptables(二)

本文涉及的产品
云防火墙,500元 1000GB
简介: firewalld && iptables

iptables工作原理示意图

640.png

安装iptables

# 安装iptables
yum install iptables-services -y
# 加载模块
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
# 停止防火墙firewalld
systemctl stop firewalld
systemctl disable firewalld
# 开启iptables
systemctl start iptables.service
systemctl enable iptables.service

iptables基本操作

    [root@halo ~ ]# iptables -h
    iptables v1.4.21
    Usage: iptables -[ACD] chain rule-specification [options]
           iptables -I chain [rulenum] rule-specification [options]
           iptables -R chain rulenum rule-specification [options]
           iptables -D chain rulenum [options]
           iptables -[LS] [chain [rulenum]] [options]
           iptables -[FZ] [chain] [options]
           iptables -[NX] chain
           iptables -E old-chain-name new-chain-name
           iptables -P chain target [options]
           iptables -h (print this help information)
    Commands:
    Either long or short options are allowed.
      --append  -A chain    Append to chain
      --check   -C chain    Check for the existence of a rule
      --delete  -D chain    Delete matching rule from chain
      --delete  -D chain rulenum
            Delete rule rulenum (1 = first) from chain
      --insert  -I chain [rulenum]
            Insert in chain as rulenum (default 1=first)
      --replace -R chain rulenum
            Replace rule rulenum (1 = first) in chain
      --list    -L [chain [rulenum]]
            List the rules in a chain or all chains
      --list-rules -S [chain [rulenum]]
            Print the rules in a chain or all chains
      --flush   -F [chain]    Delete all rules in  chain or all chains
      --zero    -Z [chain [rulenum]]
            Zero counters in chain or all chains
      --new     -N chain    Create a new user-defined chain
      --delete-chain
                -X [chain]    Delete a user-defined chain
      --policy  -P chain target
            Change policy on chain to target
      --rename-chain
                -E old-chain new-chain
            Change chain name, (moving any references)
    Options:
        --ipv4  -4    Nothing (line is ignored by ip6tables-restore)
        --ipv6  -6    Error (line is ignored by iptables-restore)
    [!] --protocol  -p proto  protocol: by number or name, eg. `tcp'
    [!] --source  -s address[/mask][...]
            source specification
    [!] --destination -d address[/mask][...]
            destination specification
    [!] --in-interface -i input name[+]
            network interface name ([+] for wildcard)
     --jump  -j target
            target for rule (may load target extension)
      --goto      -g chain
                                  jump to chain with no return
      --match  -m match
            extended match (may load extension)
      --numeric  -n    numeric output of addresses and ports
    [!] --out-interface -o output name[+]
            network interface name ([+] for wildcard)
      --table  -t table  table to manipulate (default: `filter')
      --verbose  -v    verbose mode
      --wait  -w [seconds]  maximum wait to acquire xtables lock before give up
      --wait-interval -W [usecs]  wait time to try to acquire xtables lock
            default is 1 second
      --line-numbers    print line numbers when listing
      --exact  -x    expand numbers (display exact values)
    [!] --fragment  -f    match second or further fragments only
      --modprobe=<command>    try to insert modules using this command
      --set-counters PKTS BYTES  set the counter during insert/append
    [!] --version  -V    print package version.

    查看规则

    [root@halo ~ ]# 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
    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
    

    清除规则

    iptables -F <- 清除所有规则,不会处理默认的规则
    iptables -X <- 删除用户自定义的链
    iptables -Z <- 链的计数器清零(数据包计数器与数据包字节计数器)

    添加规则

    iptables -t <-指定表d(efault: `filter')
    iptables -A <-把规则添加到指定的链上,默认添加到最后一行。
    iptables -I <-插入规则,默认插入到第一行(封IP)。iptables -D <-删除链上的规则

    网络状态

    NEW:已经或将启动新的连接
    ESTABLISHED:已建立的连接
    RELATED:正在启动的新连接
    INVALID:非法或无法识别的

    删除某个规则

      iptables -nL --line-numbers 查看规则号码
      iptables -D INPUT 1 删除指定链上的指定序号
      iptables A INPUT -p tcp -m state --dport 22 -j DROP
      iptables -nL
      iptables -nL --line-numbers
      iptables -D INPUT 1

      iptables实战

      禁止某个端口访问

        iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
        规则解释:
        -p       #<==指定过滤的协议-p(tcp,udp,icmp,all)
        --dport  #<==指定目标端口(用户请求的端口)。
        -j       #<==对规则的具体处理方法(ACCEPT,DROP,REJECT,SNAT/DNAT)
        --sport   #<==指定源端口。

        禁止某个IP访问

          iptables -I INPUT -p tcp -s 10.0.0.253 -i eth0 -j DROP
          iptables -A INPUT -p tcp ! -s 10.0.0.1 -i eth0 -j DROP
          iptables -A INPUT -p tcp ! -s 10.0.0.0/24 -i eth0 -j DROP
          -s       #<==指定源地址或网段(192.168.1.0/24)。! 取反。
          -d       #<==指定目的地址(nat表prerouting)。
          -i       #<==进入的网络接口(eth0,eth1)。
          -o       #<==出去的网络接口(eth0,eth1)。

          禁止除跳板机以外的IP访问

            iptables -I INPUT -p tcp ! -s 10.0.0.1 -j DROP

            匹配端口范围

              iptables -I INPUT -p tcp -m multiport --dport 21,22,23,24 -j DROP
              iptables -I INPUT -p tcp --dport 3306:8809 -j ACCEPT
              iptables -I INPUT -p tcp --dport 18:80 -j DROP  #<==最佳

              什么是ICMP?

              常见的协议 http https  tcp udp ftp smtp 等等

              ICMP协议的全称为Internet Control Message Protocol,翻译为互联网控制报文协议,它主要用于探测网络上的主机是否可用,目标是否可达,网络是否通畅,路由是否可用等。

              我们平常使用ping命令ping某主机时,如果主机可达,对应主机会对我们的ping请求做出回应(此处不考虑禁ping等情况),也就是说,我们发出ping请求,对方回应ping请求,虽然ping请求报文与ping回应报文都属于ICMP类型的报文,但是如果在概念上细分的话,它们所属的类型还是不同的,我们发出的ping请求属于类型8的icmp报文,而对方主机的ping回应报文则属于类型0的icmp报文,根据应用场景的不同,icmp报文被细分为如下各种类型。

                The most common ICMP message types are:最常见的icmp的消息类型是:
                Type类型 Name名称
                0 Echo Reply回声答复
                三 Destination Unreachable目的地遥不可及
                四 Source Quench来源解渴
                五 Redirect重定向
                六 Alternate Host Address候补主机地址
                八 Echo回声 
                九日 Router Advertisement路由器广告
                10个 Router Solicitation路由器邀约
                11日 Time Exceeded时间超过
                12日 Parameter Problem参数问题
                13 Timestamp时间戳
                14 Timestamp Reply时间戳答复
                15 Information Request资料请求
                16 Information Reply资讯答复
                17 Address Mask Request地址掩模要求
                18 Address Mask Reply地址面具答复
                30 Traceroute traceroute

                匹配ICMP类型

                  iptables -A INPUT -p icmp --icmp-type 8
                  例:iptables -A INPUT -p icmp --icmp-type 8 -j DROP
                  iptables -A INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
                  iptables -A FORWARD -s 192.168.1.0/24 -p icmp -m icmp --icmp-type any -j ACCEPT
                  

                  练习题

                    # 封掉10.0.0.7iptables -I INPUT -s 10.0.0.7 -j DROP
                    # 让10.0.0.7和SSH客户端(10.0.0.1)服务器可以Ping,其它的不能Pingiptables -I INPUT -p icmp --icmp-type 8 -s 10.0.0.7 -j ACCEPTiptables -I INPUT 2 -p icmp ! -s 10.0.0.1 --icmp-type 8 -j DROP
                    # 封掉3306端口iptables -I INPUT -p tcp --dport 3306 -j DROP

                    iptables企业案例

                    部署一个安全的防火墙

                      # 实战部署:
                      [root@halo ~]# iptables -F
                      [root@halo ~]# iptables -X
                      [root@halo ~]# iptables -Z
                      [root@halo ~]# iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
                      [root@halo ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
                      [root@halo ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT
                      [root@halo ~]# iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT
                      [root@halo ~]# iptables -A INPUT -i lo -j ACCEPT
                      [root@halo ~]# iptables -P INPUT DROP
                      [root@halo ~]# iptables -P FORWARD DROP
                      [root@halo ~]# iptables -P OUTPUT ACCEPT
                      [root@halo ~]# iptables -nL
                      Chain INPUT (policy DROP)
                      target     prot opt source               destination         
                      ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 80,443
                      ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
                      ACCEPT     all  --  10.0.0.0/24          0.0.0.0/0           
                      ACCEPT     all  --  172.16.1.0/24        0.0.0.0/0           
                      ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
                      Chain FORWARD (policy DROP)
                      target     prot opt source               destination         
                      Chain OUTPUT (policy ACCEPT)
                      target     prot opt source               destination

                      局域网共享上网

                      实验环境:halo: 共享上网网关出口web01: 只有内网地址,网关指向halo实验步骤:halo操作

                        iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 10.0.0.61
                        echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
                        sysctl -p
                        iptables -F
                        iptables -X
                        iptables -Z
                        iptables -A INPUT -p tcp -m multiport --dport 80,443 -j ACCEPT
                        iptables -A INPUT -p tcp --dport 22 -j ACCEPT
                        iptables -A INPUT -s 10.0.1.0/24 -j ACCEPT
                        iptables -A INPUT -s 172.16.1.0/24 -j ACCEPT
                        iptables -A INPUT -i lo -j ACCEPT
                        iptables -P INPUT DROP
                        iptables -P FORWARD DROP
                        iptables -P OUTPUT ACCEPT
                        iptables -A FORWARD -i eth1 -s 172.16.1.0/24 -j ACCEPT
                        iptables -A FORWARD -o eth0 -s 172.16.1.0/24 -j ACCEPT
                        iptables -A FORWARD -i eth0 -d 172.16.1.0/24 -j ACCEPT
                        iptables -A FORWARD -o eth1 -d 172.16.1.0/24 -j ACCEPT

                        web01操作:

                          [root@web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
                          TYPE=Ethernet
                          BOOTPROTO=none
                          DEFROUTE=yes
                          NAME=eth0
                          DEVICE=eth0
                          ONBOOT=no
                          IPADDR=10.0.0.7
                          PREFIX=24
                          GATEWAY=10.0.0.2
                          DNS1=10.0.0.2
                          [root@web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
                          TYPE=Ethernet
                          BOOTPROTO=none
                          DEFROUTE=yes
                          NAME=eth1
                          DEVICE=eth1
                          ONBOOT=yes
                          IPADDR=172.16.1.7
                          GATEWAY=172.16.1.61
                          PREFIX=24
                          DNS1=10.0.0.2
                          [root@web01 ~]# systemctl restart network 
                          [root@web01 ~]# ip r
                          default via 172.16.1.61 dev eth1  proto static  metric 100 
                          172.16.1.0/24 dev eth1  proto kernel  scope link  src 172.16.1.7  metric 100 
                          [root@web01 ~]# cat /etc/resolv.conf    
                          nameserver 10.0.1.2

                          端口映射

                            # halo操作:
                            iptables -t nat -A PREROUTING -d 10.0.1.61 -p tcp 
                            表:nat
                            链:PREROUTING
                            目标IP:10.0.1.61
                            目标端口:9000
                            协议:tcp
                            动作:DNAT
                            目标IP:172.16.1.7
                            目标端口:22
                            ssh root@10.0.1.61 -p 9000
                            root@10.0.1.61's password:
                            [root@web01 ~]#

                            IP映射

                              [root@halo ~]# ip a add 10.0.1.62/24 dev eth0 label eth0:0
                              [root@halo ~]# iptables -t nat -A PREROUTING -d 10.0.1.62 -j DNAT --to-destination 172.16.1.7
                              [root@halo ~]# iptables -nL -t nat
                              Chain PREROUTING (policy ACCEPT)
                              target     prot opt source               destination         
                              DNAT       tcp  --  0.0.0.0/0            10.0.1.61            tcp dpt:9000 to:172.16.1.7:22
                              DNAT       all  --  0.0.0.0/0            10.0.1.62            to:172.16.1.7
                              Chain INPUT (policy ACCEPT)
                              target     prot opt source               destination         
                              Chain OUTPUT (policy ACCEPT)
                              target     prot opt source               destination         
                              Chain POSTROUTING (policy ACCEPT)
                              target     prot opt source               destination         
                              SNAT       all  --  172.16.1.0/24        0.0.0.0/0            to:10.0.1.61
                              ssh root@10.0.1.62
                              root@10.0.1.62's password:
                              [root@web01 ~]# hostname
                              web01
                              [root@web01 ~]#

                              iptables规则保存

                              iptables-save > 20190821 #将防火墙规则保存到文件中
                              iptables-save #将防火墙规则保存到配置文件中,防止重启后失效
                              iptables-restore < 20190821 #从配置文件里载入防火墙配置

                              iptables防坑指南

                                1.修改之前先导出备份一份
                                2.修改的时候小心别把自己关在外面
                                3.可以现在定时任务里添加一条定时清空的规则,等测试没问题再取消定时任务
                                相关文章
                                |
                                10月前
                                |
                                网络协议 Shell
                                iptables 详解
                                iptables 详解
                                75 0
                                |
                                网络协议 Linux 网络安全
                                iptables 的四表五链
                                iptables 是 Linux 系统上用于定义防火墙规则的工具,它通过四个表和五个链来进行配置。下面是这些表和链的详细说明: 四个表: 1. filter 表:filter 表是最常用的表,用于过滤数据包。它包含了 INPUT、OUTPUT 和 FORWARD 三个默认的链。 2. nat 表:nat 表用于网络地址转换 (NAT)。它包含了 PREROUTING、POSTROUTING 和 OUTPUT 三个默认的链。nat 表用于修改数据包的 IP 地址和端口。 3. mangle 表:mangle 表用于修改数据包的特定字段,如 TTL(生存时间)、TOS(服务类型)等。它包含了
                                265 1
                                |
                                4月前
                                |
                                安全 网络协议 Linux
                                firewalld服务 具体介绍
                                firewalld服务 具体介绍
                                |
                                4月前
                                |
                                网络协议 Linux 网络安全
                                iptables 与 firewalld 防火墙
                                iptables 与 firewalld 防火墙
                                |
                                网络安全 网络架构
                                |
                                存储 网络协议 网络安全
                                firewalld详解
                                firewalld详解
                                firewalld详解
                                |
                                Kubernetes 网络协议 算法
                                Iptables 介绍与使用
                                Iptables 介绍与使用
                                434 0
                                Iptables 介绍与使用
                                |
                                网络安全 Linux Apache
                                |
                                网络协议 网络安全 开发工具
                                |
                                网络协议 网络安全