iptables规则 防止DoS攻击 20171108 Linux教程 安全防御



防止DoS攻击

iptables -A INPUT -p tcp --dport 80 -m limit --limit 50/minute --limit-burst 150 -j ACCEPT
  • -m limit: 启用limit扩展
  • –limit 25/minute: 允许最多每分钟25个连接
  • –limit-burst 100: 当达到100个连接后,才启用上述25/minute限制

对icmp的burst限制

iptables -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT

iptables -A INPUT -p icmp -j DROP


限制每秒只接受一个 icmp echo-request 封包
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 1 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP


防治 SYN-Flood 碎片攻击
iptables -N syn-flood
iptables -A syn-flood -m limit --limit 100/s --limit-burst 150 -j RETURN
iptables -A syn-flood -j DROP
iptables -I INPUT -j syn-flood


防端口扫描

iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT


安装iptable iptable-service

[plain] view plain copy
  1. #先检查是否安装了iptables  
  2. service iptables status  
  3. #安装iptables  
  4. yum install -y iptables  
  5. #升级iptables  
  6. yum update iptables   
  7. #安装iptables-services  
  8. yum install iptables-services  

禁用/停止自带的firewalld服务

[plain] view plain copy
  1. #停止firewalld服务  
  2. systemctl stop firewalld  
  3. #禁用firewalld服务  
  4. systemctl mask firewalld  

设置现有规则

[plain] view plain copy
  1. #查看iptables现有规则  
  2. iptables -L -n  
  3. #先允许所有,不然有可能会杯具  
  4. iptables -P INPUT ACCEPT  
  5. #清空所有默认规则  
  6. iptables -F  
  7. #清空所有自定义规则  
  8. iptables -X  
  9. #所有计数器归0  
  10. iptables -Z  
  11. #允许来自于lo接口的数据包(本地访问)  
  12. iptables -A INPUT -i lo -j ACCEPT  
  13. #开放22端口  
  14. iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
  15. #开放21端口(FTP)  
  16. iptables -A INPUT -p tcp --dport 21 -j ACCEPT  
  17. #开放80端口(HTTP)  
  18. iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
  19. #开放443端口(HTTPS)  
  20. iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  21. #开放6379端口(redis)  
  22. iptables -A INPUT -p tcp --dport 6379 -j ACCEPT 
  23. #开放11211端口(memcached缓存)  
  24. iptables -A INPUT -p tcp --dport 11211 -j ACCEPT
  25. #开放3306端口(mysql)  
  26. iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
  27. #开放5555端口(云锁)  
  28. iptables -A INPUT -p tcp -m tcp --dport 5555 -j ACCEPT        
  29. iptables -A INPUT -p udp -m udp --dport 5555 -j ACCEPT        
  30. iptables -A OUTPUT -p tcp -m tcp --dport 5555 -j ACCEPT        
  31. iptables -A OUTPUT -p udp -m udp --dport 5555 -j ACCEPT 
  32. #开放55555,888端口(宝塔)  
  33. iptables -A INPUT -p tcp --dport 55555 -j ACCEPT
  34. iptables -A INPUT -p tcp --dport 888 -j ACCEPT
  35.  
  36. #禁止ping  
  37. iptables -A INPUT -p icmp --icmp-type 8 -j DROP   (允许ping为: ACCEPT)  
  38. #允许接受本机请求之后的返回数据 RELATED,是为FTP设置的  
  39. iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT  
  40. #其他入站一律丢弃  
  41. iptables -P INPUT DROP  
  42. #所有出站一律绿灯  
  43. iptables -P OUTPUT ACCEPT  
  44. #所有转发一律丢弃  
  45. iptables -P FORWARD DROP  

其他规则设定

[plain] view plain copy
  1. #如果要添加内网ip信任(接受其所有TCP请求)  
  2. iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT  
  3. #过滤所有非以上规则的请求  
  4. iptables -P INPUT DROP  
  5. #要封停一个IP,使用下面这条命令:  
  6. iptables -I INPUT -s ***.***.***.*** -j DROP  
  7. #要解封一个IP,使用下面这条命令:  
  8. iptables -D INPUT -s ***.***.***.*** -j DROP  

修改防火墙:  vi /etc/sysconfig/iptables


保存规则设定

[plain] view plain copy
  1. #保存上述规则  
  2. service iptables save  

开启iptables服务

[plain] view plain copy
  1. #注册iptables服务  
  2. #相当于以前的chkconfig iptables on  
  3. systemctl enable iptables.service 

  4. systemctl restart iptables.service  
  1. systemctl stop iptables.service  
  1. #开启服务  
  2. systemctl start iptables.service  
  3. #查看状态  
  4. systemctl status iptables.service  

 

签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回