Linux_28 iptables

由于这五个规则是内核中的功能,用户不能直接操作内核中的功能,内核中所有的功能都是通过接口向外提供调用的,所以用户想要和内核交互,只能通过系统调用完成,所以需要开发一个用户空间工具,利用这个工具编写一些内容,送到内核的这些个位置上,来帮我们生成规则,这个工具就叫做iptables,内核中的叫netfilter

有个图形界面工具配置iptables规则:
	yum install firewall-config -y 
	firewall-config  自动弹出图形化界面

iptables

iptables:包过滤型的防火墙
Firewall:防火墙,隔离工具;工作于主机或网络边缘,对于进出本主机或本网络的报文根据事先定义的检查规则匹配检测,对于能够被规则匹配到的报文做出相应处理的组件;
	主机防火墙
	网络防火墙
	
	软件防火墙(软件逻辑)
	硬件防火墙(硬件和软件逻辑)

ipfw(firewall framework) linux 2.0之前
ipchains(firewall framework) Linux2.0~2.4之间

iptables(netfilter) Linux2.4之后
	netfilter:kernel
	iptables:rules until

nftables Linux4.0之后

	hook function:
		input
		output
		forward
		prerouting
		postrouting

链(内置):
	PREROUTING
	INPUT
	FORWARD
	OUTPUT
	POSTROUTING
功能:
	filter:过滤,防火墙;
	nat:network address translation;用于修改源IP或目标IP,也可以改端口;
	mangle:拆解报文,做出修改,并重新封装起来;
	raw:关闭nat表上启用的连接追踪机制;
	security:

功能<--链:
raw:PREROUTING,OUTPUT
mangle:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat:PREROUTING,[INPUT,]OUTPUT,POSTROUTING
filter:INPUT,FORWARD,OUTPUT 

报文流向:
	流入本机:PREROUTING-->INPUT
	由本机流出:OUTPUT-->POSTROUTING
	转发:PREROUTING-->FORWARD-->POSTROUTING

路由功能发生的时刻:
	报文进入本机后:
		判断目标主机是?
	报文离开本机之前:
		判断经由哪个接口送往下一站?

iptables/netfilter
	规则:
		组成部分:根据规则匹配条件来尝试匹配报文,一旦匹配成功,就由规则定义的处理动作作出处理、
			匹配条件:
				基本匹配条件
					-s -d -p -i -o
				扩展匹配条件
					-m matchname per-match-options
					
			处理动作:
				基本处理动作
					ACCEPT
					DROP
				扩展处理动作
					REJECT RETURN LOG REDIRECT,..
				自定义处理机制
			
			iptables的链:内置链和自定义链
				内置链:对应于hook function
				自定义链接:鱼鱼内置链的扩展和补充,可实现更灵活的规则管理机制;
		
	添加规则时的考量点:
		(1)要实现哪种功能:判断添加到哪个表上;
		(2)报文流经的路径:判断添加到哪个链上;
	
		链:链上的规则次序,即为检查的次序;因此,隐含一定的应用法则:
			(1)同类规则(访问同一应用),匹配范围小的放上面;
			(2)不同类的规则(访问不同应用),匹配到报文频率较大的放在上面;
			(3)将那些可由一条规则描述的多个规则合并起来;
			(4)设置默认策略;
	
	iptables命令:
		高度模块化,由诸多扩展模块实现其检查条件或处理动作的定义;
			rpm -ql iptables
			yum info iptables
			/usr/lib64/xtables/
				IPV6:libip6t_
				IPV4:libipt_,libxt
		iptalbes [-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]
			matches:匹配条件
			target:处理动作
		match = -m matchname [per-match-options]
			-m:指明匹配模块的名称
			per-match-options:模块所定义的选项和值
		target = -j targetname [per-target-options]
			-j:指定一个处理动作的扩展;
			per-target-options:每一个处理动作都有可能带有的选项
			
		规则格式: iptables [-t table] COMMAND chain [-m matchname [per-match-options]] -j targetname [per-target-options]
        	-t table: # 不指定表,默认是filter
        		raw mangle,nat,[filter]
			
			COMMAND:
				链管理:
					-N:new,自定义一条新的规则链;
					-X:delete,删除自定义的规则链;
						注意:仅能删除用户 自定义的 引用计数为0的 空的链;
					-P:Policy,设置默认策略;对filter表中的链而言,其默认策略有:
						ACCEPT:接受
						DROP:丢弃
						REJECT:拒绝
					
					-E:重命名自定义链;引用计数不为0的自定义链不能够被重命名,也不能被删除;
				规则管理:
					-A:append,追加;
					-I:insert,插入,要指明位置,省略时表示第一条;
					-D:delete,删除;
						(1)指明规则序号;
						(2)指明规则本身;
					-R:replace,替换指定链上的指定规则;
					-F:flush,清空指定的规则链;
					-Z:zero,置零;
						iptables的每条规则都有两个计数器;
							(1)匹配到的报文的个数;
							(2)匹配到的所有报文的累计字节数;
				查看:
					-L:list,列出指定链上的所有规则;
						-n:numberic,以数字格式显示地址和端口号;
							iptables -L -n

						-v:verbose,详细信息;
							-vv,-vvv
							iptables -L -n --line-numbers -v
							iptables -L -n --line-numbers -vv
							iptables -L -n --line-numbers -vvv
							或者合并子选项:
								iptables -nvxL --line-numbers 
						
						只显示某个链上的规则:
							iptables -nvxL INPUT


						-x:exactly,显示计数器结果的精确值;
						--line-numbers:显示规则的序号;
							iptables -L -n --line-numbers
			
			chain:
				PEROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
			
			匹配条件:
				基本匹配条件:PARAMETERS,无须加载任何模块,由iptables/netfilter自行提供;
				扩展匹配条件:
					隐式扩展:再使用-p选项指明了特定的协议时,无需再同时使用-m选项指明扩展模块的扩展机制;
					显式扩展:必须使用-m选项指明要调用的扩展模块的扩展机制;
					
						扩展匹配条件:需要加载扩展模块,方可生效;# rpm -ql iptables 中小写结尾的是扩展模块,大写的是处理动作模块


				[!] -s,--source address[/mask][...]:检查报文中的源IP地址是否符合此处指定的地址或范围;
				[!] -d, --destination address[/mask][...]:检查报文中的目标IP地址是否符合此处指定的地址或范围;
					匹配所有地址:0.0.0.0/0
				[!]-p,--protocol | protocol
					protocol:tcp,udp,udplite,icmp,icmpv6,esp,ah,sctp,mh or "all"
						{tcp|udp|icmp}
				[!] -i,--in-interface name:数据报文流入接口;只能应用于数据报文流入的环节,只能应用于PREROUTING,INPUT和FORWARD链;
				[!]-o,--out-interface name:数据报文流出的接口;只能应用于数据报文流出的环节,只能应用于FORWARD、OUTPUT和POSTROUTING链;
			
				

iptables命令使用

# 如图1:
iptables -vnL 查看链(INPUT OUTPUT FORWARD)
iptables -N in_web_rules # 新增一个链
iptables -X in_web_rules # 删除一个链 注意:一个链有规则或其引用计数不为零,则无法直接删除。自定义的、空的、引用计数为零的链才可以被删除。

iptables -X INPUT | OUTPUT | FORWARD # 无法删除


# 图2:
iptables -P FORWARD DROP # 所有报文都将无法转发
iptables -vnL
iptables -P FORWARD ACCEPT

# 图3:
iptables -N in_web
iptables -E in_web_rules  # 重命名自定义链
iptables -vnL

# 图4:
systemclt start firewalld
iptables -vnL
iptables -Z INPUT_ZONES # 将INPUT_ZONES链上的引用计数置零
iptables -Z  # 不指定链,表示所有链全部置零
iptables -Z INPUT_ZONES 1 # 表示将哪条链的哪条规则的引用计数置零,这里是第一条规则置零


# 图5:
iptables -F IN_public  # 将某条链上的规则全部清除
iptables -F FWDO_public 1 # 将FWDO_public链上的第一条规则清除
iptables -F # 将所有链的所有规则全部清除

# 图6:
iptables -vnL FORWARD --line-numbers
iptables -D FORWARD 7  # 删除FORWARD链中的第七条规则

图1:

图2:

图4:

清空前:

清空后:

图5:

清空前:

清空后:

清空所有链上的规则:

重启firewalld后恢复规则:

图6:

# 添加这两条规则之后 并设定filter链中的策略为DROP,ping包将无法进入,也就无从谈起 出去   如下图1
iptables -t filter -A INPUT -s 192.168.1.0/24 -d 192.168.1.8 -p tcp -j ACCEPT
iptables -t filter -A OUTPUT -s 192.168.1.8 -d  192.168.1.0/24 -p tcp -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

# 可以再添加一条icmp协议的入栈和出栈的规则  如下图2
iptables -A INPUT -d 192.168.1.8 -p icmp -j ACCEPT  # 添加这条入栈规则之后,ping包可以入栈(无法收到响应),但无法出栈,可以使用tcpdump抓包测试
tcpdump -i ens33 -nn icmp

iptables -A OUTPUT -s 192.168.1.8 -p icmp -j ACCEPT # 添加出栈规则,ping包有响应了。



图1:

图2:

隐式扩展

隐式扩展:不需要手动加载扩展模块;因为他们是对协议的扩展,所以,但凡使用-p指明了协议,就表示已经指明了要扩展的模块;
	tcp:
		[!]--source-port,--sport port[:port]:匹配报文的源端口;可以是端口范围;
         [!]--destination-port,--dport port[:port]:匹配报文的目标端口;可以是端口范围;
         [!]--tcp-flags mask comp
			mask is the flags which we should examine,written as a comma-separated list,例如 SYN,ACK,FIN,RST
             comp is a comma-separated list of flags which must be set,例如SYN
             例如:“--tcp-flags SYN ACK FIN RST SYN”表示,要检查的标志位为SYN,ACK,FIN,RST四个,其中SYN必须为1,余下的必须为0.
		[!] --syn :用于匹配第一次握手,相当于“--tcp-flags SYN,ACK,FIN,RST,SYN”;
                                   
	udp:
		[!] --source-port,--sport port[:port:]匹配报文的源端口;可以是端口范围;
		[!]--destination-port,--dport port[:port:]:匹配报文的目标端口;可以是端口范围;
								
	icmp:
		[!] --icmp-type {type[/code]|typename}
			echo-reqeust:8
			echo-reply:0
							
	显示扩展:必须要手动加载扩展模块,[-m,matchname[per-match-options]]
                                
隐式扩展:
	-p tcp:可直接使用tcp扩展模块的专用选项
		[!] --source-port,--sport port[:port] 匹配报文源端口;可以给出多个端口,但只能式连续的端口范围;
		[!] --destination-port,--dport port[:port] 匹配报文目标端口;可以给出多个端口,但只能是连续的端口范围;
		[!] --tcp-flags mask comp 匹配报文中的tcp协议的标志位;Flags are:SYN ACK FIN RST URG PSH ALL NONE;
			mask:要检查的FLAGS list,以逗号分隔;
			comp:在mask给定的诸多的FLAGS中,其值必须为1的FLAGS列表,余下的其值必须为0;
				--tcp-flags SYN,ACK,FIN,RST SYN
				--tcp-flags ALL ALL
				--tcp-flags ALL NONE
		[!] --syn: --tcp-flags SYN,ACK,FIN,RST,SYN
							
	-p udp:可直接使用udp协议扩展模块的专用选项
		[!] --source-prot,--sport port[:port]
		[!] --destination-port,--dport port[:port]
		
	icmp:
		[!] --icmp-type {type[/code]|typename}
			0/0:echo reply
			8/0:echo request
# icmp:
iptables -I OUTPUT 2 -s 192.168.1.8 -p icmp --icmp-type 8 -j ACCEPT
	ping 192.168.1.9 # 添加上面的规则,ping的报文能出去,但进不来
	在192.168.1.9主机上,抓取icmp包:
		tcpdump -i ens33 -nn icmp

iptables -I INPUT 2 -d 192.168.1.8 -p icmp --icmp-type 0/0 -j ACCEPT
	添加这条规则之后ping的报文能被收到

# udp(samba端口 udp 137 138 tcp 139 445): 目前iptables不支持离散端口的设定,必须一个个设定
iptables -I INPUT -d 192.168.1.8 -p udp --dport 137:138 -j ACCEPT  # 入站
iptables -I OUTPUT -s 192.168.1.8 -p udp --dport 137:138 -j ACCEPT

显示扩展

显示扩展:必须使用-m选项指明要调用的扩展模块的扩展机制;
	1.multiport:
		以离散或连续的方式定义多端口匹配条件,最多15个;
								
		[!] --source-ports,--sports port[,port|,port:port]...:指定多个源端口;离散端口用逗号分隔,连续端口用冒号分隔。
		[!] --destination-ports,--dports port[,port|,port:port]...:指定多个目标端口;
								
		# iptables -I INPUT -d 192.168.1.8 -p tcp -m multiport -dports 22,80,139,445,3306 -j ACCEPT
							
	2.iprange
		以连续地址块的方式来指明多IP地址匹配条件;
		[!] --src-range from [-to]
		[!] --dst-range from [-to]
		
		# iptables -I INPUT -d 192.168.1.8 -p tcp -m multiport 22,80,139,445,3306 -m iprange --src-range 192.168.1.5-192.168.1.20 -j REJECT

	3.time
		This matches if the packet arrival time/date is within a given range.
		
		--timestart hh:mm[:ss]
		--timestop hh:mm[:ss]
		
		[!]--weekdays day[,day...]
		[!]--monthdays day[,day...]
		
		--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
		--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
		
		--kerneltz:使用内核配置的时区而非默认的UTC;CentOS6不需加此参数,默认就是选择内核时区。
systemctl start httpd.service nmb.service smb.service
1.multiport:
	iptables -R INPUT 2 -d 192.168.1.8 -p tcp -m multiport --dports 22,80,139,445 -j ACCEPT # 入站
	iptables -R OUTPUT 2 -s 192.168.1.8 -p tcp -m multiport --sports 22,80,139,445 -j ACCEPT # 出站
		smbclinet -L 192.168.1.8

2.iprange:
	iptables -I INPUT 3 -d 192.168.1.8 -p tcp --dport 23 -m iprange --src-range 192.168.1.5-192.168.1.20 -j ACCEPT # 限制入站请求23号端口的源地址范围为5~20
	iptables -I OUTPUT 3 -s 192.168.1.8 -p tcp --sport 23 -m iprange --dst-range 192.168.1.5-192.168.1.20 -j ACCEPT # 限制入站请求23号端口的进程响应时,响应的地址范围为5~20
	
3.time
	iptables -R OUTPUT 4 -s 192.168.1.8 -p udp -m multiport --dports 123,323 -j ACCEPT #开启时间服务端口123和323
	iptables -R INPUT 4 -d 192.168.1.8 -p udp -m multiport --sports 123,323 -j ACCEPT
	
	iptables -R INPUT 3 -d 192.168.1.8 -p tcp --dport 23 -m iprange --src-range 192.168.1.5-192.168.1.20 -m time --timestart 10:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 --kerneltz -j ACCEPT  # --kerneltz 指的是同步的是内核时钟
	
	iptables -R OUTPUT 3 -s 192.168.1.8 -p tcp --sport 23 -m iprange --dst-range 192.168.1.5-192.168.1.20 -m time --timestart 10:00:00 --timestop 16:00:00 --weekdays 1,2,3,4,5 --kerneltz -j ACCEPT
4.string  # 只能对明文编码的协议生效,https/ssh等检测不到
	This modules matches a given string by using some pattern matching strategy.
	
	--algo {bm|kmp}
	[!] --string pattern
	[!] --hex-string pattern
	
	--from offset
	--to offset
	
	# iptables -I OUTPUT -s -m string --algo bm --string "gay" -j REJECT
	
	iptables -I OUTPUT -s 192.168.1.8 -m string --algo bm --string "gay" -j REJECT
		vim /var/www/html
			添加 文本“gay”词句,浏览器访问192.168.1.8 ,页面将被拒绝响应
5.connlimit (限制单个用户的最大连接数:并发连接数) # 如下图1
	Allows you to restrict the number of parallel connetctions to a server per client Ip address (or client address block).
	--connlimit-upto n
	--connlimit-above n
	
	# iptables -I INPUT -d 192.168.1.8 -p tcp --syn --dport 22 -m connlimit --connlimit-above 2 -j REJECT 
	
	# 在192.168.1.8主机上:
	mysql
	create user 'test'@'192.168.1.%' IDENTIFIED by 'test123'
	iptables -R OUTPUT 2 -d 192.168.1.0/24 -s 192.168.1.8 -p tcp --sport 3306 -j ACCEPT
	
	iptables -R INPUT 2 -d 192.168.1.8 -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT
	
	iptables -R INPUT 2 -d 192.168.1.8 -s 192.168.1.0/24 -p tcp --dport 3306 -m connlimit --connlimit-upto 2 -j ACCEPT
	# 在192.168.1.9主机上:
	mysql -utest -h 192.168.1.8 -ptest123 # 能

6.limit 
	This modile matches at a limited rate using a token bucket filter.
	--limit rate[/second|/minute|hour|/day] # 设置发送(接收)包的速率
	--limit-brust number # 设置令牌桶手记收集令牌的数量
	# iptables -I OUTPUT -s 192.168.1.8 -p icmp --icmp-type 0 -j ACCEPT
	
	yum install -y hping3 # 按自定义时间间隔发送hping3包
	
	iptables -I INPUT 5 -d 192.168.1.8 -p icmp --icmp-type 8 -m limit --limit-burst 5 --limit 20/minute -j ACCEPT # 入站
	iptables -I OUTPUT 5 -s 192.168.1.8 -p icmp --icmp-type 0 -j ACCEPT # 出站

	限制本机某tcp服务接收新请求的速率:--syn,-m limit
	
7.state
	The "state" extension is a subset of the "conntrack" module "state" allows access to the connection tracking state for this packet.
	
	[!] --state state
		INVALID,ESTABLISHED,NEW,RELATED or UNTRACKED.
		
		NEW:新连接请求; # 第一次连接请求都是NEW
		ESTABLISHED:已建立的连接;
		INVALID:无法识别的连接;
		RELATED:相关联的连接,当前连接是一个新请求,但附属于某个已存在的连接;
		UNTRACKED:未追踪的连接;
		
		state扩展:
			内核模块装载:
				nf_conntrack
				nf_conntrack_ipv4
				
				手动装载:
					nf_conntrack_ftp
		
		追踪到的连接:
			/proc/net/nf_conntrack
		调整可记录的连接数量最大值:
			/proc/sys/net/nf_conntrack_max
		超时时长:
			/proc/sys/net/netfilter/*timeout*
		
		# 在192.168.0.10机器上创建以下规则并创建一个mysql用户:
		iptables -A INPUT -d 192.168.0.10 -p tcp -m multiport --dports 22:23,80,139,445,3306 -m state --state NEW -j ACCEPT
		iptables -A INPUT -d 192.168.0.10 -p tcp -m state --state ESTABLISHED -j ACCEPT
		iptables -A OUTPUT -s 192.168.0.10 -p tcp -m state --state ESTABLISHED -j ACCEPT
		
		iptables -A INPUT -d 192.168.0.10 -j REJECT
		iptables -A OUTPUT -s 192.168.0.10 -j REJECT
		iptables -vnL
		create user 'test'@'192.168.0.%' IDENTIFIED by 'test123';

		# 在192.168.0.11 主机上登录192.168.0.10机器上的mysql服务:
			mysql -utest -h192.168.0.10 -ptest123  # 正常连接,以上创建的规则生效;
			smbclient -L 192.168.0.10 # 正常连接
			
		iptables -I INPUT 2 -d 192.168.0.10 -p udp --dport 137:138 -m state --state NEW -j ACCEPT
		iptables -I OUTPUT 2 -s 192.168.0.10 -p udp -m multiport --dports 123,323 -j ACCEPT

图1:

处理动作(跳转目标):
	-j targetname [per-target-options]
		简单target:
			ACCEPT,DROP
		扩展target:
			REJECT
				This is used to send back an error packet in packet in respose to the matched packet:otherwise it is equlvalent to DROP so it is a terminating TARGET,ending rule traversal.
				
				--reject-with type
					The type can be icmp-net-unreachable,icmp-host-unreachable,icmp-proto-unreach-able,icmp-net-prohibited,icmp-host-prohibited,or icmp-admin-prohibited(*),which return the appropriate ICMP error message(icmp-port-unreachable is the default).
			
			LOG
				Turn on hernel logging of matching packets.
				--log-level
				--log-prefix
				
				默认日志保存于/var/log/messages
				
            iptables -I INPUT 3 -d 192.168.0.10 -p tcp --dport 23 -m state --state NEW -j LOG # 下图1
        在192.168.0.11机器上使用telnet登录,查看192.168.0.10机器上/var/log/messages的日志信息:
            telnet 192.168.0.10  
            登录的日志被记录 # 如下图2:

        iptables -R INPUT 3 -d 192.168.0.10 -p tcp --dport 23 -m state --state NEW -j LOG --log-prefix "access telnet service"

		RETURN :
			返回调用者;
		
		自定义链作为target:# 如下图3和4:
			
			iptables -N in_ping_rules # 自定义一个链
			iptables -A in_ping_rules -d 192.168.0.10 -p icmp --icmp-type 8 -j ACCEPT # 允许ping请求来ping这台主机
			iptables -I in_ping_rules -d 192.168.0.10 -s 192.168.0.120 -p icmp -j REJECT # 不允许这台主机ping
			iptables -vnL
			添加完以上规则后,这个自定义链(in_ping_rules)还没生效,必须要在INPUT OUTPUT FORWORD上调用才能使用
			iptables -I INPUT 5 -d 192.168.0.10 -p icmp -j in_ping_rules # 自定义链被INPUT链引用,如图4
			测试:
				在192.168.0.120主机上ping 192.168.0.10 没有响应,如他主机ping能正常响应 # 如图5
			
			自定义链一旦被引用了,是无法删除链的,必须先清除被引用链中对应的那一条规则,然后清空自定义链规则,然后才能删除自定义链 # 图6
				iptables -X in_ping_rules  # 无法直接删除已被引用的链,应该先删除在被引用链中的对应的那一条规则
				iptables -D INPUT 5  # 将在被引用链中的对应的那一条规则删除
				iptables -X in_ping_rules  # 还是不能删除,还要删除自定义链中定义的规则
				iptables -F in_ping_rules  # 清空自定义链(in_ping_rules)中的规则
				iptables -vnL
				iptables -X in_ping_rules 

图1:

图2:

图3:

图4:

image-20210705163753363

图5:

图6:

image-20210705170417668

以上定义的规则都是保存在内存中的,被送往内核立即生效,如果机器重启,所有规则将被清空,如果想保存规则,就要将规则保存在文件当中。。。。。

保存和载入规则:
	保存:iptables-save > /PATH/TO/SOME_RULE_FILE
		iptables-save > /etc/sysconfig/iptables-v1 # 这时再次清空规则,并不影响已经定义的规则的使用,可以执行下面的重载命令
			iptables -F
	重载:iptables-restore < /PATH/FROM/SOME_RULE_FILE
		-n,--noflush:不清楚原有规则
		-t,--test:仅分析生成规则集,但不提交
			iptables-restore < /etc/sysconfig/iptables-v1
	
	CentOS 6:
		保存规则:
            service iptables save
            保存规则于/etc/sysconfig/iptables文件,覆盖保存;
		重载规则:
			service  iptables restart
			默认重载/etc/sysconfig/iptables文件中的规则
	
		配置文件:/etc/sysconfig/iptables-config
		
	CentOS 7:
		(1)自定义Unit File,进行iptables-restore;
		(2)firewalld服务;
		(3)自定义脚本;
	
规则优化的思路:
	使用自定义链管理特定应用的相关规则,模块化管理规则;
	
	(1)优先放行双方向状态为ESTABLISHED的报文;
	(2)服务于不同类别的功能的规则,匹配到报文可能性更大的放前面;
	(3)服务于同一类别的功能的规则,匹配条件较严格的放在前面;
	(4)设置默认策略:白名单机制
		(a)iptables -P,不建议;
		(b)建议在规则的最后定义规则作为默认策略;

iptables/netfilter网络防火墙

(1)网关;# 打开核心转发功能(/etc/sys/net/ipv4/ip_forward) sysctl -w net.ipv4.ip_forward=1
(2)filter表得FORWARD链;

需要注意得问题:
	(1)请求-响应报文均会经由FORWARD链,要注意规则得方向性;
	(2)如果要启用conntrack机制,建议将双方向得状态为ESTABLISHED得报文直接放行;

NAT:Network Address Translation
	请求报文:由管理员定义;
	响应报文:由NAT得conntrack机制自动实现;
	
	请求报文:
		改源地址:SNAT(Source Network Address Translation:源地址转换),MASQUERADE
		改目标地址:DNAT(Destination Network Address Translation:目标地址转换)
	
iptables/netfilter:
	NAT定义在nat表:
		PREROUTING,INPUT,OUTPUT,POSTROUTING
		
		SNAT:源地址转换定义在POSTROUTING链上
		DNAT:目标地址转换定义在PREROUTING链上
		PAT(Port Address Translation:端口地址转换):
	
target:
	man iptables-extensions
	
	SNAT:
		This  target  is only valid in the nat table, in the POSTROUTING and INPUT chains, and user-defined chains which are only called from those chains.
		
		--to-source [ipaddr[-ipaddr]]
		
		iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 172.16.0.6
		iptables -t nat -vnL
	
	DNAT:
		This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
		--to-destination [ipaddr[-ipaddr]][:port[-port]]
		
		iptables -t nat -F
		iptables -t nat -vnL
		iptables -t nat -A PREROUTING -d 172.16.0.6 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.2
		
	MASQUERADE
       This target is only valid in the nat table, in the POSTROUTING chain.  It should only be used with dynamically assigned IP (dialup) connections: if you have a static IP address, you should use the SNAT target.
       
       SNAT场景中应用于POSTROUTING链上的规则实现源地址转换,但外网地址不固定时,使用此target ;
	
	REDIRECT # 端口映射,在目标主机上添加规则后,其它主机访问该主机的某个端口,通过规则,将其映射到另外一个已监听的端口上。
		This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains.
		--to-ports port[-port]
		iptables -A PREROUTING -t nat -d 192.168.10.2 -p tcp --dport 80 -j REJECT --t0-ports 8080
		
posted on 2021-07-06 15:58  jueyuanfengsheng  阅读(98)  评论(0编辑  收藏  举报