强烈推荐读下这篇文章
《一篇搞懂》系列之一 —— iptables_iptables优先级_祁祁不正经的博客-CSDN博客
1. iptables -t filter -A与iptables -t filter -I的区别。
-A会追加到当前表的最后,-I会添加到当前表的第一行。
2. iptables典型命令
如果没有指定表,默认表为filter. table to manipulate (default: `filter')
iptables -t filter -A INPUT -i eth0 -s 10.0.0.2 -p tcp --dport 8080 -j ACCEPT
iptables -nvL // n表示不对ip进行反向解析,v-verbose, L list
//下面这条规则的执行效果怎么样,得看在规则链执行过程中,在表中该规则在哪个位置,有时候该规则甚至都没有机会执行。
iptables -t filter -A INPUT -j drop
- 查看Filter表中所有链以及规则
iptables -nL --line-numbers
-n:将主机信息(IP地址,端口等)以数字的形式打印出来。默认会以hostname等方式打印出来;
-L:显示规则链中已有的条目;
–line-numbers:显示条目序号。
3. iptables使用的一些场景
3.1 修改规则链的默认策略
--policy -P chain target
Change policy on chain to target
iptables -P INPUT DROP
3.2 全部允许,只阻止某些ip访问。可以用来测试程序访问不通某些程序时,软件的运行是否正常。
3.3 清除规则
只能清除添加的规则,默认规则是不受影响的。
iptables -F
3.4 当在云主机上操作iptables,注意不要因为修改iptables导致与云主机的连接断开。
3.5 iptables -D 删除规则。
3.6 自定义规则链。
3.7 一般情况下,filter表中,一般只限制INPUT链中对一些来源IP或端口进行限制。
在INPUT链中,指定目标IP是无影响的。
3.8 Linux IPTABLES中默认是阻止所有端口访问的,如果要开放服务的网络访问,一般要开通相关IP或端口访问。
4. 防火墙的分类
- 软件防火墙,可分为包过滤防火墙和应用层防火墙,相对而言,包过滤防火墙效率更高。
- 硬件防火墙
5. iptables、Firewalld、NetFilter这三者的关系?
由于iptables配置网络比较复杂,且参数较多,而且实际控制效果依赖规则链中规则的顺序。
iptables
实际上只是位于用户空间的一个面向系统管理员的Linux防火墙的管理工具而已,而真正实现防火墙功能的是netfilter
,它是Linux内核中实现包过滤的内核模块,iptables
对应在内核中的模块应该是ip_tables
,我们查看系统内核中ip_tables
的信息的时候可以看到ip_tables.ko
这个模块是在netfilter
这个目录下的。
实际上除了iptables
还有如nftables
、firewalld
等防火墙工具都是在用户空间(用户层)对相应的内核空间中对应的netfilter
相关的模块进行操作的工具。
6. 规则表和规则链
规则表中最常用的就filter和nat,也是iptables早期支持的两个表。
之所以叫做链就是因为在访问该链的时候会按照每个链对应的表依次进行查询匹配执行的操作,如PREROUTING
链对应的就是(raw
->mangle
->nat
),每个表按照优先级顺序进行连接,每个表中还可能有多个规则,因此最后看起来就像链一样,因此称为链。而iptables
的表中存储的就是对应的规则和需要执行的操作。
注意每一个链对应的表都是不完全一样的,表和链之间是多对多的对应关系。但是不管一个链对应多少个表,它的表都是按照下面的优先顺序来进行查找匹配的。
表的处理优先级:raw
>mangle
>nat
>filter
。
四表
iptables
的四个表iptable_filter
,iptable_mangle
,iptable_nat
,iptable_raw
,默认表是filter
(没有指定表的时候就是filter表)。
filter 表
:用来对数据包进行过滤,具体的规则要求决定如何处理一个数据包。
对应的内核模块为:iptable_filter
,其表内包括三个链:input
、forward
、output
;nat 表
:nat 全称:network address translation 网络地址转换,主要用来修改数据包的 IP 地址、端口号信息。
对应的内核模块为:iptable_nat
,其表内包括三个链:prerouting
、postrouting
、output
;mangle 表
:主要用来修改数据包的服务类型,生存周期,为数据包设置标记,实现流量整形、策略路由等。
对应的内核模块为:iptable_mangle
,其表内包括五个链:prerouting
、postrouting
、input
、output
、forward
;raw 表
:主要用来决定是否对数据包进行状态跟踪。
对应的内核模块为:iptable_raw
,其表内包括两个链:output
、prerouting
;
raw表只使用在PREROUTING
链和OUTPUT
链上,因为优先级最高,从而可以对收到的数据包在系统进行ip_conntrack(连接跟踪)前进行处理。一但用户使用了raw表,在某个链上,raw表处理完后,将跳过NAT表和ip_conntrack处理,即不再做地址转换和数据包的链接跟踪处理了。RAW表可以应用在那些不需要做nat的情况下,以提高性能。
- 链(chains):每一张表包含若干
链
,其规定了相关规则在什么时候执行。内核中内置有5条链,分别对应netfilter提供的5个Hook点。链
能够让管理员在数据包传输过程中的某一个点通过相关规则控制数据包的走向。
7. 在iptables中设置的对数据包的处理方式会对请示响应报错的现象产生影响 。特别是drop和reject两者的效果是不一样的。
ACCEPT | 允许数据包通过 |
DROP | 直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求没有响应,过了超时时间才会有反应。 |
REJECT | 拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息 |
8. iptables实战
实战篇
禁止源地址192.168.1.1访问服务器的22,53,80端口:
iptables -I INPUT -s 192.168.1.1/32 -p tcp --dport 22,53,80 -j DROP
-s:source address,匹配源地址
-p:protocol,匹配协议
–dport:destination port,匹配目的端口。逗号分隔多个端口。
对于访问量比较大的服务器,例如192.168.1.1,可以通过raw表配置绕过连接跟踪:
iptables -t raw -A PREROUTING -d 192.168.1.1/32 -p tcp --dport 80 -j NOTRACK
将访问192.168.1.1:80的请求转到192.168.1.2:9000上
iptables -t nat -A PREROUTING -d 192.168.1.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.1.2:9000
实现所有192.168.1.0/24的地址通过123.123.123.123公网地址访问公网
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 123.123.123.123
拒绝一分钟内新建超过4次SSH连接的IP再次连接
iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource
iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 --name SSH --rsource -j DROP
===========
Linux内核中数据包的传输过程_linux内核网络回复报文转发路径_白-胖-子的博客-CSDN博客
《一篇搞懂》系列之一 —— iptables_iptables优先级_祁祁不正经的博客-CSDN博客
Iptables Tutorial: Ultimate Guide to Linux Firewall
关于在iptables里面使用-m参数,内容特别多,有一些常用的参数可以了解一下,全部记住不太现实,但可以通过下面的方式查看帮助文档 。比如下面查看iprange模块的详细使用方法。
# iptables -I input -m iprange -h
iptables v1.6.1
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.
iprange match options:
[!] --src-range ip[-ip] Match source IP in the specified range
[!] --dst-range ip[-ip] Match destination IP in the specified range
#