2、封ip,屏蔽某个ip
1. [root@Ansible ~]# iptables -I INPUT -s 10.0.0.7 -j DROP 2. [root@Ansible ~]# iptables -I INPUT -s 172.16.1.7 -j DROP 3. [root@Ansible ~]# iptables -nL 4. Chain INPUT (policy ACCEPT) 5. target prot opt source destination 6. DROP all -- 172.16.1.7 0.0.0.0/0 7. DROP all -- 10.0.0.7 0.0.0.0/0 8. 9. Chain FORWARD (policy ACCEPT) 10. target prot opt source destination 11. 12. Chain OUTPUT (policy ACCEPT) 13. target prot opt source destination
屏蔽之后用10.0.0.7ssh连接会卡住
[root@Web01 ~]# ssh 10.0.0.61
3、禁止网段连入(禁止10.0.0.0/24网段访问 8888端口)
1. #指定10.0.0.0网段的屏蔽了22端口 2. [root@Ansible ~]# iptables -F 3. [root@Ansible ~]# iptables -I INPUT -s 10.0.0.0/24 -p tcp --dport 22 -j DROP 4. 5. [root@Web01 ~]# ssh 10.0.0.61 hostname 6. ^C 7. [root@Web01 ~]# ssh 172.16.1.61 hostname 8. root@172.16.1.61's password: 9. Ansible 10. 11. #指定10.0.0.0网段的屏蔽8888端口 12. [root@Ansible ~]# iptables -F 13. [root@Ansible ~]# iptables -I INPUT -s 10.0.0.0/24 -p tcp --dport 8888 -j DROP 14. 15. 这个可以用nc和telnet测试
4、只允许指定网段连入(允许10.0.0.0网段)
类似于阿里云白名单功能,开放指定端口和网段,其他均拒绝
1、利用!排除,只准许10.0.0.0/24访问
1. [root@Ansible ~]# iptables -I INPUT ! -s 10.0.0.0/24 -j DROP 2. 3. 4. [root@Web01 ~]# ping 10.0.0.61 5. PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data. 6. 64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.466 ms 7. 64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.658 ms 8. ^C 9. --- 10.0.0.61 ping statistics --- 10. 2 packets transmitted, 2 received, 0% packet loss, time 1000ms 11. rtt min/avg/max/mdev = 0.466/0.562/0.658/0.096 ms 12. [root@Web01 ~]# ping 172.16.1.61 13. PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data. 14. ^C 15. --- 172.16.1.61 ping statistics --- 16. 4 packets transmitted, 0 received, 100% packet loss, time 2999ms
2、修改链默认规则,修改为拒绝,添加准许
若默认规则修改为拒绝后,ping公网IP可以但是ping公网域名不行,故障类似于DNS解析有问题(我踩过的坑)
1. [root@Ansible ~]# iptables -nL 2. Chain INPUT (policy ACCEPT) 3. target prot opt source destination 4. 5. Chain FORWARD (policy ACCEPT) 6. target prot opt source destination 7. 8. Chain OUTPUT (policy ACCEPT) 9. target prot opt source destination 10. [root@Ansible ~]# iptables -P INPUT DROP 11. [root@Ansible ~]# iptables -nL 12. Chain INPUT (policy DROP) 13. target prot opt source destination 14. 15. Chain FORWARD (policy ACCEPT) 16. target prot opt source destination 17. 18. Chain OUTPUT (policy ACCEPT) 19. target prot opt source destination 20. [root@Ansible ~]# iptables -A INPUT -s 10.0.0.0/24 -j ACCEPT 21. [root@Ansible ~]# iptables -nL 22. Chain INPUT (policy DROP) 23. target prot opt source destination 24. ACCEPT all -- 10.0.0.0/24 0.0.0.0/0 25. 26. Chain FORWARD (policy ACCEPT) 27. target prot opt source destination 28. 29. Chain OUTPUT (policy ACCEPT) 30. target prot opt source destination
5、指定多个端口
1. [root@Ansible ~]# iptables -I INPUT -p tcp --dport 8888 -j DROP 2. [root@Ansible ~]# iptables -I INPUT -p tcp --dport 9999 -j DROP 3. [root@Ansible ~]# iptables -nL 4. Chain INPUT (policy ACCEPT) 5. target prot opt source destination 6. DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9999 7. DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8888 8. 9. Chain FORWARD (policy ACCEPT) 10. target prot opt source destination 11. 12. Chain OUTPUT (policy ACCEPT) 13. target prot opt source destination 14. 15. [root@Ansible ~]# iptables -I INPUT -p tcp -m multiport ! --dport 80,443 -j DROP 16. 17. #如果是禁用连续端口可以不加-m multiport 1:1024 18. [root@Ansible ~]# iptables -I INPUT -p tcp --dport 1024:65535 -j DROP 19. [root@Ansible ~]# iptables -nL 20. Chain INPUT (policy ACCEPT) 21. target prot opt source destination 22. DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:1024:65535 23. 24. Chain FORWARD (policy ACCEPT) 25. target prot opt source destination 26. 27. Chain OUTPUT (policy ACCEPT) 28. target prot opt source destination
6、匹配ICMP类型
ICMP(Internet Control Message Protocol)Internet控制报文协议,ping,是整个网站的核心
通过防火墙规则,控制是否可以ping
1. [root@Ansible ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP 2. [root@Ansible ~]# iptables -nL 3. Chain INPUT (policy ACCEPT) 4. target prot opt source destination 5. DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8 6. 7. Chain FORWARD (policy ACCEPT) 8. target prot opt source destination 9. 10. Chain OUTPUT (policy ACCEPT) 11. target prot opt source destination 12. 13. [root@Web01 ~]# ping 10.0.0.61 14. PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data. 15. ^C 16. --- 10.0.0.61 ping statistics --- 17. 6 packets transmitted, 0 received, 100% packet loss, time 5002ms
通过内核参数,控制禁止被ping
1. [root@Ansible ~]# cat /etc/sysctl.conf 2. net.ipv4.icmp_echo_ignore_all = 0 #0为允许,1为禁止 3. [root@Ansible ~]# sysctl -p 4. net.ipv4.icmp_echo_ignore_all = 0
7、匹配网络状态(TCP/IP连接状态)
-m state --state
NEW:已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动的新连接
INVALID:非法或无法识别的
1. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 2. 3. iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
8、限制并发及速率
-m limit 限制模块
-m limit --limit 10/minute #每分钟只能有10个数据包 每6秒生成
-m limit --limit n/{second/minute/hour}:指定时间内的请求速率”n”为速率,后面为时间分别为:秒 分 时
1. -m limit --limit 10/minute --limit-burst 5 每6秒释放工牌 给别人使用 2. 3. #10个数据包 4. 前5个 1个1个工牌 从第6个开始 每6秒 才能释放1个工牌
--limit-burst [n]:在同一时间内允许通过的请求“n”为数字,不指定默认为5
1. #ping icmp 协议 进行测试 2. [root@Ansible ~]# iptables -F 3. [root@Ansible ~]# iptables -I INPUT -p icmp -m limit --limit 10/minute --limit-burst 5 -j ACCEPT 4. [root@Ansible ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT 5. [root@Ansible ~]# iptables -P INPUT DROP 6. [root@Ansible ~]# iptables -nL 7. Chain INPUT (policy DROP) 8. target prot opt source destination 9. ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 limit: avg 10/min burst 5 10. ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 11. 12. Chain FORWARD (policy ACCEPT) 13. target prot opt source destination 14. 15. Chain OUTPUT (policy ACCEPT) 16. target prot opt source destination
测试
1. [root@Web01 ~]# ping 10.0.0.61 2. PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data. 3. 64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.298 ms 4. 64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=1.33 ms 5. 64 bytes from 10.0.0.61: icmp_seq=3 ttl=64 time=0.435 ms 6. 64 bytes from 10.0.0.61: icmp_seq=4 ttl=64 time=0.460 ms 7. 64 bytes from 10.0.0.61: icmp_seq=5 ttl=64 time=0.758 ms 8. 64 bytes from 10.0.0.61: icmp_seq=7 ttl=64 time=2.28 ms #7和1之间间隔6秒 9. 64 bytes from 10.0.0.61: icmp_seq=13 ttl=64 time=0.793 ms #13和7之间间隔6秒 10. 64 bytes from 10.0.0.61: icmp_seq=19 ttl=64 time=0.734 ms 11. 64 bytes from 10.0.0.61: icmp_seq=25 ttl=64 time=0.803 ms 12. ^C 13. --- 10.0.0.61 ping statistics --- 14. 26 packets transmitted, 9 received, 65% packet loss, time 25013ms 15. rtt min/avg/max/mdev = 0.298/0.877/2.286/0.572 ms