技术:iptables浅谈使用

398 阅读10分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情

引言

1 概述

防火墙技术是通过有机结合各类用于安全管理与筛选的软件和硬件设备,帮助计算机网络于其内、外网之间构建一道相对隔绝的保护屏障,以保护用户资料与信息安全性的一种技术。

防火墙技术的功能主要在于及时发现并处理计算机网络运行时可能存在的安全风险、数据传输等问题,其中处理措施包括隔离与保护,同时可对计算机网络安全当中的各项操作实施记录与检测,以确保计算机网络运行的安全性,保障用户资料与信息的完整性,为用户提供更好、更安全的计算机网络使用体验。

1.1 策略和规则

防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种。无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘。而我们的任务就是需要去定义到底防火墙如何工作。

「策略」

定义到底防火墙如何工作。

「规则」

对出入网络的IP、数据进行检测。

1.2 七层模型

七层模型,亦称OSI(Open System Interconnection)。参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一般称为OSI参考模型或七层模型。

image.png

1.2.1 应用层

「描述」

网络服务与最终用户的一个接口。

「协议」

HTTP FTP TFTP SMTP SNMP DNS TELNET HTTPS POP3 DHCP

1.2.2 表示层

「描述」

数据的表示、安全、压缩。(在五层模型里面已经合并到了应用层)

「格式」

JPEG、ASCll、EBCDIC、加密格式等

1.2.3 会话层

「描述」

建立、管理、终止会话。(在五层模型里面已经合并到了应用层)

对应主机进程,指本地主机与远程主机正在进行的会话

1.2.4 传输层

「描述」

定义传输数据的协议端口号,以及流控和差错校验。

「协议」

TCP UDP,数据包一旦离开网卡即进入网络传输层

1.2.5 网络层

「描述」

进行逻辑地址寻址,实现不同网络之间的路径选择。

「协议」

ICMP IGMP IP(IPV4 IPV6)

1.2.6 数据链路层

「描述」

建立逻辑连接、进行硬件地址寻址、差错校验 [3] 等功能。(由底层网络定义协议)

将比特组合成字节进而组合成帧,用MAC地址访问介质,错误发现但不能纠正。

1.2.7 物理层

「描述」

建立、维护、断开物理连接。(由底层网络定义协议)

1.3 现状

目前市面上比较常见的有3、4层的防火墙。一般的防火墙只能做到3-4层的保护,也叫做网络层的防火墙,对于5-7层的应用保护很一般,保护正式IDS和IPS的长处。还有7层的防火墙,其实是代理层的网关。

对于TCP/IP的七层模型来讲,我们知道第三层是网络层,三层的防火墙会在这层对源地址和目标地址进行检测。但是对于七层的防火墙,不管你源端口或者目标端口,源地址或者目标地址是什么,都将对你所有的东西进行检查。所以,对于设计原理来讲,七层防火墙更加安全,但是这却带来了效率更低。所以市面上通常的防火墙方案,都是两者结合的。而又由于我们都需要从防火墙所控制的这个口来访问,所以防火墙的工作效率就成了用户能够访问数据多少的一个最重要的控制,配置的不好甚至有可能成为流量的瓶颈。

2 iptables

2.1 关于iptables

iptables是netfilter的一个子项目,iptables本身一个通用的防火墙软件,允许定义规则集。IP 表中的每条规则都由许多分类器(iptables 匹配)和一个连接的操作(iptables 目标)组成。

在与Linux内核集成时,版本对应如下:

• Linux 2.0.X内核对应ipfwadm
• Linux 2.2.X内核对应ipchains
• Linux2.4.X内核对应iptables

iptables的官方地址:www.netfilter.org/projects/ip…

iptables版本更改日志记录:git.netfilter.org/iptables/

iptables各个版本下载地址:www.netfilter.org/pub/iptable…

2.2 iptables工作原理

iptables是采用数据包过滤机制工作的,所以它会对请求的数据包的包头进行分析,并根据我们预先设定的规则进行匹配来决定是否可以进入主机。

  1. 防火墙是一层一层过滤的。实际是按照配置规则的顺序从上到下,从前到后进行过滤的;
  2. 如果匹配上规则,即明确表明阻止还是通过,此时数据包就不再向下匹配新规则了;
  3. 如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,则按照默认策略进行处理;
  4. 防火墙的默认规则是对应的链的所有的规则执行完成后才会执行的;

2.3 iptables的规则链

iptables的规则链是NetFilter规定的五个规则链,任何一个数据包,只要经过本机,必将经过这五个链中的其中一个链。

  • PREROUTING (路由前)
  • INPUT (数据包流入口)
  • FORWARD (转发管卡)    
  • OUTPUT(数据包出口)    
  • POSTROUTING(路由后)

image.png

2.4 iptables策略

防火墙策略一般分为两种,一种叫“通”策略,一种叫“堵”策略。

通策略

默认门是关着的,必须要定义谁能进。

堵策略

大门是洞开的,但是你必须有身份认证,否则不能进。

我们现在用的比较多个功能有3个,

  • filter
    • 定义允许或者不允许的
    • INPUT ,FORWARD ,OUTPUT
  • nat
    • 定义地址转换的 
    • PREROUTING ,OUTPUT ,POSTROUTING
  • mangle
    • 功能管理,修改报文原数据
    • PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

3 iptables使用规则

先使用命令iptables -V在机器上面查看是否安装,以及安装的版本。从下面的命令来看,我机器已经安装了iptables,而且安装的版本为v1.4.21。在官网,截止到目前为止,我们看到的最新版本为v1.8.8 。

[root@nto2 ~]# iptables -V
iptables v1.4.21

关于使用,可以使用iptables --help来查看具体的使用规则。

[root@nto2 ~]# iptables --help
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.

4 安装与使用

4.1 安装iptables环境

在Linux操作系统中,可以使用yum管理来安装,也可以从官网下载后上传到服务器手动安装。

「yum安装」

$ yum -y install iptables-services

「下载安装」

image.png

4.2 启动iptables

按照我们平时启动软件方式即可。

$ systemctl stop firewalld && systemctl disable firewalld
$ systemctl start iptables && systemctl enable iptables

5 基本命令

这个有点像firewalld防火墙,都是有基本的操作命令。

5.1 查看iptables已设置的规则

查看已设置规则列表:iptables -L

[root@otn2 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

查看已设置规则详细列表:iptables -L -vn

[root@otn2 ~]# iptables -L -vn
Chain INPUT (policy ACCEPT 268M packets, 83G bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:53
    0     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:53
    0     0 ACCEPT     udp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            udp dpt:67
    0     0 ACCEPT     tcp  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:67

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      virbr0  0.0.0.0/0            192.168.11.0/24     ctstate RELATED,ESTABLISHED
    0     0 ACCEPT     all  --  virbr0 *       192.168.11.0/24     0.0.0.0/0
    0     0 ACCEPT     all  --  virbr0 virbr0  0.0.0.0/0            0.0.0.0/0
    0     0 REJECT     all  --  *      virbr0  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
    0     0 REJECT     all  --  virbr0 *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT 249M packets, 74G bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     udp  --  *      virbr0  0.0.0.0/0            0.0.0.0/0            udp dpt:68

5.2 清除iptables已设置规则

清除所有规则:iptables -F

删除用户自定义的链:iptables -X

链的计数器清零:iptables -Z

「命令」

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

5.3 添加iptables规则

指定表:iptables -t

把规则添加到指定的链上(默认添加到最后一行):iptables -A

插入规则(默认插入到第一行,封IP):iptables -I

删除链上的规则:iptables -D

「命令」

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

5.4 清除iptables指定规则

查看规则号码:iptables -nL --line-numbers

删除指定链上的指定序号:iptables -D INPUT 1 

$ iptables -nL --line-numbers   # 查看规则号码
$ iptables -D INPUT 1           # 删除指定链上的指定序号

6 实战

6.1 配置允许ssh端口连接

配置ssh连接:iptables -A INPUT


$ iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

22:是ssh默认端口,

-s 192.168.1.0/24:表示允许这个网段的机器来连接,其它网段的ip地址是登陆不了你的机器的。

-j ACCEPT:表示接受这样的请求

6.2 允许本地圆环地址使用


$ iptables -A INPUT -i lo -j ACCEPT
$ iptables -A OUTPUT -o lo -j ACCEPT

本地圆环地址就是那个127.0.0.1,是本机上使用的,它进与出都设置为允许。

6.3 设置默认规则

默认情况,配置不允许进入:iptables -P INPUT DROP

默认情况,配置不允许转发:iptables -P FORWARD DROP

默认情况,配制可以转发出去:iptables -P OUTPUT ACCEPT

$ iptables -P INPUT DROP # 配置默认的不让进
$ iptables -P FORWARD DROP # 默认的不允许转发
$ iptables -P OUTPUT ACCEPT # 默认的可以出去

6.4 配置白名单

允许机房内网机器可以访问:iptables -A INPUT -p all

$ iptables -A INPUT -p all -s 192.168.1.0/24 -j ACCEPT  # 允许机房内网机器可以访问
$ iptables -A INPUT -p all -s 192.168.140.0/24 -j ACCEPT  # 允许机房内网机器可以访问
$ iptables -A INPUT -p tcp -s 183.121.3.7 --dport 3380 -j ACCEPT # 允许183.121.3.7访问本机的3380端口

6.5 开启相应的服务端口

$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 开启80端口,因为web对外都是这个端口
$ iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT # 允许被ping
$ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 已经建立的连接得让它进来

6.6 保存iptables规则

「方法一」

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

「方法二」

$ cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak # 任何改动之前先备份,请保持这一优秀的习惯
$ iptables-save > /etc/sysconfig/iptables
$ cat /etc/sysconfig/iptables

6.7 禁止某个IP访问

$ iptables -I INPUT -p tcp -s 192.168.1.253 -i ens33 -j DROP
$ iptables -A INPUT -p tcp ! -s 192.168.1.1 -i ens33 -j DROP
$ iptables -A INPUT -p tcp ! -s 192.168.1.0/24 -i ens33 -j DROP

6.8 禁止初跳板机以外的IP访问

$ iptables -I INPUT -p tcp ! -s 192.168.1.1 -j DROP

6.9 匹配端口范围

$ 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
$ 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 OUTPUT -j ACCEPT         
# 允许所有本机向外的访问
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT    
# 允许访问22端口
$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT    
# 允许访问80端口
$ iptables -A INPUT -p tcp --dport 21 -j ACCEPT    
# 允许ftp服务的21端口
$ iptables -A INPUT -p tcp --dport 20 -j ACCEPT    
# 允许FTP服务的20端口
$ iptables -A INPUT -j reject      
# 禁止其他未允许的规则访问
$ iptables -A FORWARD -j REJECT     
# 禁止其他未允许的规则访问

6.10 匹配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

「实验」

$ iptables -I INPUT -s 192.168.1.200 -j DROP
# 封掉190.168.1.200
$ iptables -I INPUT -p icmp --icmp-type 8 -s 192.168.1.10 -j ACCEPT
$ iptables -I INPUT 2 -p icmp ! -s 192.168.1.1 --icmp-type 8 -j DROP
# 只允许192.168.1.1 192.168.1.10可以ping
$ iptables -I INPUT -p tcp --dport 3306 -j DROP
# 将3306端口封掉
$ iptables -A INPUT -p tcp -m tcp -s 192.168.0.8 -j DROP 
# 屏蔽恶意主机(比如,192.168.0.8
$ iptables -I INPUT -s 123.45.6.7 -j DROP       
# 屏蔽单个IP的命令
$ iptables -I INPUT -s 123.0.0.0/8 -j DROP     
# 封整个段即从123.0.0.1123.255.255.254的命令
$ iptables -I INPUT -s 124.45.0.0/16 -j DROP   
# 封IP段即从123.45.0.1123.45.255.254的命令
$ iptables -I INPUT -s 123.45.6.0/24 -j DROP    
# 封IP段即从123.45.6.1123.45.6.254

6.11 端口映射

$ iptables -t nat -A PREROUTING -d 10.0.1.61 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.7:22
命令拆解:
表:nat
链:PREROUTING
源IP:10.0.1.61
源端口:9000
协议:tcp
动作:DNAT
目标IP:172.16.1.7
目标端口:22
 
$ iptables -t nat -A PREROUTING -d 210.14.67.127 -p tcp --dport 2222  -j DNAT --to-dest 192.168.188.115:22
# 本机的 2222 端口映射到内网 虚拟机的22 端口

6.12 IP映射

$ iptables -t nat -A PREROUTING -d 10.0.1.62 -j DNAT --to-destination 172.16.1.7
# 将10.0.1.62的访问请求转发到172.16.1.7

6.13 启动网络转发规则

$ iptables -A FORWARD -o eth0
# 只对 OUTPUT,FORWARD,POSTROUTING 三个链起作用

6.14 字符串匹配

$ iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT
iptables -A INPUT -p tcp -m string --algo kmp --string "test" -j REJECT --reject-with tcp-reset
iptables -L
 
# Chain INPUT (policy ACCEPT)
# target     prot opt source               destination
# REJECT     tcp  --  anywhere             anywhere            STRING match "test" ALGO name kmp TO 65535 reject-with tcp-reset
#
# Chain FORWARD (policy ACCEPT)
# target     prot opt source               destination
#
# Chain OUTPUT (policy ACCEPT)
# target     prot opt source               destination

6.15 阻止Windows蠕虫

$ iptables -I INPUT -j DROP -p tcp -s 0.0.0.0/0 -m string --algo kmp --string "cmd.exe"

6.16 防止SYN洪水

$ iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT

6.17 列出已设置的规则

这个在基础规则已经列出来。

「格式」

iptables -L [-t 表名] [链名]

「描述」

• 四个表名 raw,nat,filter,mangle

• 五个规则链名 INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING

• filter表包含INPUT、OUTPUT、FORWARD三个规则链

$ iptables -L -t nat                  # 列出 nat 上面的所有规则
#            ^ -t 参数指定,必须是 raw, nat,filter,mangle 中的一个
$ iptables -L -t nat  --line-numbers  # 规则带编号
$ iptables -L INPUT
 
$ iptables -L -nv  # 查看,这个列表看起来更详细