iptables基本规则演示(二)

2023年 7月 16日 60.1k 0

iptables

iptables概念和参数:http://www.gray-track.com/718.html

用户自定义链是无法自动生效,通过引用生效自定义链

显示当前链规则
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#

查看某些表
查看nat表 -t nat

[root@localhost ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#

详细信息级别, -v -vv -vvv
[root@localhost ~]# iptables -t raw -L -n -v
Chain PREROUTING (policy ACCEPT 71 packets, 6250 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 15 packets, 1152 bytes)
pkts bytes target prot opt in out source destination
[root@localhost ~]# iptables -L -n -v
Chain INPUT (policy ACCEPT 251 packets, 20369 bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 136 packets, 17796 bytes)
pkts bytes target prot opt in out source destination

pkts bytes target prot opt in out source destination
pkts: 被本规则所匹配到的包个数;
bytes:被本规则所匹配到的所包的大小之和;
target: 处理目标 (目标可以为用户自定义的链)
prot: 协议 {tcp, udp, icmp}
opt: 可选项
in: 数据包流入接口
out: 数据包流出接口
source: 源地址
destination: 目标地址;

查看规则号码
iptables -L -n --line-numbers

匹配172.16.100.2 到172.16.100.11所有规则,任意协议全部允许
iptable -t filter -A INPUT -s 172.16.100.2 -d 172.16.100.11 -j ACCEPT

 

一,通用匹配:

[!] -s, --src, --source IP|Network:检查报文中的源IP地址;
d, --dst, --destination:检查报文中的目标IP地址;
-p, --protocol:检查报文中的协议,即ip首部中的protocols所标识的协议;tcp、udp或icmp三者之一;
-i, --in-interface:数据报文的流入接口;通常只用于PREROUTING, INPUT, FORWARD链上的规则;
-o, --out-interface:检查报文的流出接口;通常只用于FORWARD, OUTPUT, POSTROUTING链上的规则;
iptable -t filter -A INPUT -s 172.16.100.2 -d 172.16.100.11 -j ACCEPT
指明协议
修改规则,替换第一规则,在原来的规则上加上tcp协议(放行TCP协议)
iptable -t filter可省 -R 替换input链上第一条,从172.16.100.2到172.16.100.11,-p 添加协议为tcp -j ACCEPT
iptable -t filter -R INPUT 1 -s 172.16.100.2 -d 172.16.100.11 -p tcp -j ACCEPT

放行172.16.100.2到172.16.100.11的icmp协议
iptable -A INPUT -s 172.16.100.2 -d 172.16.100.11 -p icmp -j ACCEPT
修改类型为,全部拒绝(如果你在生成环境中,请务必开始22号端口ssh)
iptables -t filter -P INPUT DROP

限制报文流入和流出
流入:
限制本机eth0网卡,在改之前最好使用iptable -L -n --line-number查看是第几条规则,如果不是经常访问的就放在后面,如果是经常访问的就放在前面,匹配的时候会递归匹配规则

iptable -R修改input链的第二条,从72.16.100.2到172.16.100.11,协议为icmp,从eth0流入的,命令如下:
iptable -R INPUT 2 -s 172.16.100.2 -d 172.16.100.11 -p icmp -i eht0 -j ACCEPT

这些条件都可以取反,!

 

二,扩展匹配,扩展协议本身

 
扩展匹配:使用iptables的模块实现扩展性检查机制
rpm -q iptables查看扩展机制模块
隐式扩展:如果在通用匹配上使用-p选项指明了协议的话,则使用-m选项指明对其协议的扩展就变得可有可无了;
tcp:
--dport PORT[-PORT] 源
--sport 目标
--tcp-flags LIST1 LIST2
LIST1: 要检查的标志位;
LIST2:在LIST1中出现过的,且必须为1标记位;而余下的则必须为0;
例如:--tcp-flags syn,ack,fin,rst syn
--syn:用于匹配tcp会话三次握手的第一次;
udp:
--sport
--dport
icmp:
--icmp-types
8: echo request
0:echo reply

拒绝来自172.16.100.2访问172.16.100.11的web

iptables -i 插入input 第1条,从172.16.100.2 到172.16.100.11的所有tcp协议-m扩展匹配tcp,目标端口80,-j 拒绝
iptable -I INPUT 1 -s 172.16.100.2 -d 172.16.100.11 -p tcp-m tcp --dport 80 -j REJECT

也可以省略-m使用
iptable -I INPUT 1 -s 172.16.100.2 -d 172.16.100.11 -p tcp --dport 80 -j REJECT

放行SSH:prot22端口
放行172.16.100.2 到172.16.100.11的22端口
iptabes -I 插入input表的第二条,从172.16.100.2 到172.16.100.11,协议-p tcp ,--dport目标端口 为22 -j ACCEPT放行
iptable -I INPUT 2 -s 172.16.100.2 -d 172.16.100.11 -p tcp --dport 22 -j ACCEPT

删除
iptables -D INPUT 3
iptables -D删除input表中的第三条策略
放行任意地址ping
iptables -R修改input链中第三条,任意地址到172.16.100.11的icmp协议全部放行
iptables -R INPUT 3 -d 172.16.100.11 -p icmp -j ACCEPT
限制流出响应报文
iptables -A output链中,从本机-s 172.16.100.11,响应给所有主机
iptables -A OUTPUt 172.16.100.11 -p tcp --sport 22 -j ACCEPT
将出战改为DROP
iptables -P OUTPUT DROP

那么现在,除22端口,其他都不能通讯。

放行从本机出去的icmp协议8号报文
iptables -A添加 output表中放行本地-s源地址 172.16.100.11,-p指明协议为tcp的icmp ,协议类型--icmp-type 只放行请求8 -j ACCEPT
iptables -A OUTPUT -s 172.16.100.11 -p icmp --icmp-type 8 -j ACCEPT

放行从本机进来的的icmp协议的0号响应报文
iptables -A INTPUT -d 172.16.100.11 -p icmp --icmp-type 0 -j ACCEPT

以上两条设置完成后,本机172.16.100.11可以ping出去,而外面的主机是无法ping进来的。

下面两条规则匹配后可以全部ping通了。
iptables -A INTPUT -d 172.16.100.11 -p icmp --icmp-type 8 -j ACCEPT
iptables -A OUTPUT -s 172.16.100.11 -p icmp --icmp-type 0 -j ACCEPT

 

三,显式扩展:必须指明使用的扩展机制;

 
-m 模块名称
每个模块会引入新的匹配机制;

想知道有哪些模块可用:
rpm -ql iptables
小写字母,以.so结尾;

1,multiport多端口快照:以离散定义多端口匹配;最多指定15个端口;

专用选项:
--source-ports, --sports PORT[,PORT,...]
--destination-ports, --dports PORT[,PORT,...]
--ports PORT[,PORT,...]

例子:
入站 iptables 插入input第一条 目标地址是本地172.16.100.11 -p 协议tcp -m 指明为 -multiport --dport指明为目标端口,22,80,443端口放行
iptables -I INPUT 1 -d 172.16.100.11 -p tcp -m multiport --dport s 22,80,443 -j ACCEPT

出站 -m -multiport 扩展
iptables -I OUTPUT 1 -s 172.16.100.11 -p tcp -m multiport --sports 22,80,443 -j ACCEPT
IP扩展
示例:
172.16.0.1主机仅开放给172.16.100.0网段开放端口23
iptables -A INPUT -s 172.16.100.0/16 -d 172.16.0.1 -p tcp --dport 23 -j ACCEPT
2,iprange范围扩展:指定连续的ip地址范围;在匹配非整个网络地址时使用;
专用选项:
[!] --src-range IP[-IP]
[!] --dst-range IP[-IP]

示例:
iptables -A新建一条INPUT链,目标地址172.16.100.11 -p协议tcp,目标端口dport 23 ,-m扩展iprange --src-range,从172.16.100.1到172.16.100.100结束,这段地址放行访问23号端口入站

iptables -A INPUT -d 172.16.100.11 -p tcp --dport 23 -m iprange --src-range 172.16.100.1-172.16.100.100 -j ACCEPT
iptables -A新建一条OUTPUT链,源地址172.16.100.11 -p协议tcp,源端口sport 23 ,-m扩展iprange --src-range,从172.16.100.1到172.16.100.100结束,这段地址放行访问23号端口出站

iptables -A OUTPUT -s 172.16.100.11 -p tcp --sport 23 -m iprange --dst-range 172.16.100.1-172.16.100.100 -j ACCEPT
示例:
放行yum安装

新建出站规则,源地址172,16,100,11,目标地址0.0.0.0,协议tcp,访问别人的80端口,所以这里是dport 80 -j ACCEPT
iptables -A OUTPUT -s 172.16.100.11 -p tcp --sport 23 -m iprange --dst-range 172.16.100.1-172.16.100.100 -j ACCEPT

iptables -A INTPUT -d 172.16.100.11 -p tcp --sport 23 -m iprange --dst-range 172.16.100.1-172.16.100.100 -j ACCEPT

3,string扩展:检查报文中出现的字符串,与给定的字符串作匹配;

字符串匹配检查算法:
kmp, bm

专用选项:
--algo {kmp|bm}
--string "STRING"
--hex-string "HEX_STRING":HEX_STRING为编码成16进制格式的字串;
示例:
我们过滤web页面中的sex字符,拒绝
iptables -I OUTPUT 1 -s 172.16.100.11 -p tcp --sport 80 -m string --string "sex" --algo kmp -j REJECT
4,time扩展:基于时间区间做访问控制

专用选项:
--datestart YYYY[-MM][-DD][hh[:mm[:ss]]]
--dattestop

--timestart
--timestop

--weekdays DAY1[,DAY2,...]

示例:
修改intables INPUT链第一条,检查法则为,如果目标地址为172.16.100.11 -p协议为tcp,目标端口似dport 80.做时间检查time --timestart开始时间,08:30,时间结束为18:30 ,在这个时间内是拒绝访问80端口
# iptables -R INPUT 1 -d 172.16.100.11 -p tcp --dport 80 -m time --timestart 08:30 --timestop 18:30 -j REJECT

星期一,星期二,星期四,星期五,早8:30到晚上18:30拒绝访问172.16.100.11的80端口
# iptables -R INPUT 1 -d 172.16.100.11 -p tcp --dport 80 -m time --timestart 08:30 --timestop 18:30 --weekdays Mon,Tue,Thu,Fri -j REJECT

5,connlimit扩展:基于连接数作限制;对每个IP能够发起的并发连接数作限制;

专用选项:
--connlimit-above [n]

限制多线程下载!
iptables I插入INPUT第二条,不管是谁发送,只要到达172.16.100.11的tcp协议的目标22端口,一旦 connlimit --connlimit-above 5上限达到五个就拒绝

# iptables -I INPUT 2 -d 172.16.100.11 -p tcp --dport 22 -m connlimit --connlimit-above 5 -j REJECT

6,limit扩展:基于发包速率作限制;
专用选项:令牌桶算法
--limit n[/second|/minit|/hour|/day]
--limit-burst n

限制本机ping操作没
iptable -I INTPUE 第三条规则,到172.16.100.11的,协议为icmp8--limit 每分钟十个报文 10/minute
iptables -R INPUT 3 -d 172.16.100.11 -p icmp --icmp-type 8 -m limit --limit 10/minute --limit-burst 5 -j ACCEPT

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论