iptables的使用
1、安装
[root@m01 ~]# systemctl status iptables
2、启动
[root@m01 ~]# systemctl restart iptables
3、关闭firewalld
[root@m01 ~]# systemctl disable --now firewalld
4、查看iptables是否开启
[root@m01 ~]# systemctl status iptables
5、格式
iptables -t 表名 选项 链名称 条件 动作
6、参数(增删改查)
-t: 指定操作的表(默认查看的是filter)
-L, --list 列出当前的规则
-v 显示数据包和数据包大小
-n 不反解地址
-A, --append 追加一条规则到链中
-I, --insert 插入一条规则,插入到顶部
注:链的匹配规则是从上到下的,一旦匹配上就无法再往下进行匹配
-F, --flush 清空
-Z, --zero 清空计数器( 包数量 、包大小)
-D, --delete 删除链中的规则
-R, --replace 修改
-S, --list-rules 列出所有的规则
-N, --new-chain 创建一个自定义 链
-X, --delete-chain 删除一个自定义链
-P, --policy 指定链的默认策略
7、iptables的动作
ACCEPT 将数据包放行,进行完此处理动作后,将不再比对其它规则,直接跳往下一个规则链。
REJECT 拦阻该数据包,并传送数据包通知对方。
DROP 丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。
REDIRECT 将包重新导向到另一个端口,进行完此处理动作后,将会继续比对其它规则。
8、iptables基本的条件匹配
TCP(http)
UDP(用户数据包协议)
ICMP(ping)
ALL(所有协议)
9、-s和-d
-s是源地址即为发送请求的地址
-d是目标地址即为访问的地址
10、--sport和--dport
--sport为源端口:发送请求的端口
--dport为目标端口:访问的端口
11、动作
-i : 进来的网卡
-o : 出去的网卡
-m : 指定模块
-j : 转发动作
-p :指定协议
12、查看本机端口
netstat -nutlp
13、案例
案例1:只允许22端口可以访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
案例2:只允许22,80,443端口可以访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 443 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
案例3:要求使用192.168.15.81能够通过22端口链接,但是其他的不行
iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
案例4:只允许192.168.15.71能够通过22端口链接,其他的不行。
iptables -t filter -A INPUT -p TCP -s 192.168.15.71 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
案例5:要求192.168.15.71对外部不可见
iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP
案例6:要求使用eth0网卡的所有请求全部拒绝
iptables -t filter -A INPUT -p TCP -i etho -j DROP
使用172.16.1.71登录进来的窗口,不允许访问百度。
iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP
案例7:要求访问服务器的8080端口转发至80端口
iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
案例8:要求只允许windows通过ssh连接192.168.15.81,其他的拒绝
iptables -t filter -A INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 22 -j DROP
iptables模块
1、作用
扩展iptables的功能
2、参数
-m 指定模块
3、连续匹配多个端口(multiport)
--dports :指定多个端口(不同端口之间用逗号分割,连续的端口使用冒号分割)
4、指定一段连续的ip地址范围(iprange)
--src-range from[-to]: 源地址范围
--dst-range from[-to] 目标地址范围
5、匹配指定字符(string)
--string pattern # 指定要匹配的字符串
--algo {bm|kmp} # 匹配的查询算法
6、根据时间段匹配报文(time)(UTC时间)
--timestart hh:mm[:ss] # 开始时间
--timestop hh:mm[:ss] # 结束时间
--monthdays day[,day...] # 指定一个月的某一天
--weekdays day[,day...] # 指定周 还是 周天
7、禁ping, 默认本机无法ping别人 、别人无法ping自己
--icmp-type {type[/code]|typename}
echo-request (8) 请求
echo-reply (0) 回应
8、限制链接数,并发连接数(connlimit)
--connlimit-upto n # 如果现有连接数小于或等于 n 则 匹配
--connlimit-above n # 如果现有连接数大于n 则匹配
9、针对 报文速率 进行限制。 秒、分钟、小时、天。
--limit rate[/second|/minute|/hour|/day] # 报文数量
--limit-burst number # 报文数量(默认:5)
10、案例
1、要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
2、要求访问数据包中包含HelloWorld的数据不允许通过。
iptables -t filter -A INPUT -p TCP -m string --string "HelloWorld" --algo kmp -j DROP
3、要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝
iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
4、要求每天的12到13之间,不允许访问
iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DROP
必须使用UTC时间
5、要求别人不能ping本机,但是本机可以ping别人
iptables -t filter -A INPUT -p TCP -m icmp --icmp-type "echo-request" -j DROP
6、要求主机连接最多有2个
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP
7、要求限制速率在500k/s左右
iptables -t filter -A INPUT -p TCP -m limit 333/s -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP