无限星辰工作室-客户无限互联网动力之源

标题: iptables 端口安全脚本 [打印本页]

作者: crx349    时间: 2020-1-17 16:25
标题: iptables 端口安全脚本
iptables 端口安全脚本,适合小白使用,注意端口访问配置哦 自己改
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. function support_distro(){
  5. if [ -z "`egrep -i "centos" /etc/issue`" ];then
  6. echo "Sorry,iptables script only support centos system now."
  7. exit 1
  8. fi
  9. }
  10. support_distro
  11. echo "============================iptables configure============================================"
  12. # Only support CentOS system
  13. # 获取SSH端口
  14. if grep "^Port" /etc/ssh/sshd_config>/dev/null;then
  15. sshdport=`grep "^Port" /etc/ssh/sshd_config | sed "s/Port\s//g" `
  16. else
  17. sshdport=22
  18. fi
  19. # 获取DNS服务器IP
  20. if [ -s /etc/resolv.conf ];then
  21. nameserver1=`cat /etc/resolv.conf |grep nameserver |awk 'NR==1{print $2 }'`
  22. nameserver2=`cat /etc/resolv.conf |grep nameserver |awk 'NR==2{print $2 }'`
  23. fi
  24. IPT="/sbin/iptables"
  25. # 删除已有规则
  26. $IPT --delete-chain
  27. $IPT --flush
  28. # 禁止进,允许出,允许回环网卡
  29. $IPT -P INPUT DROP   
  30. $IPT -P FORWARD DROP
  31. $IPT -P OUTPUT ACCEPT
  32. $IPT -A INPUT -i lo -j ACCEPT
  33. # 允许已建立的或相关连接的通行
  34. $IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  35. $IPT -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  36. # 限制80端口单个IP的最大连接数为10
  37. $IPT -I INPUT -p tcp --dport 80 -m connlimit --connlimit-above 10 -j DROP
  38. # 允许80(HTTP)/873(RSYNC)/443(HTTPS)/20,21(FTP)/25(SMTP)端口的连接
  39. $IPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
  40. $IPT -A INPUT -p tcp -m tcp --dport 873 -j ACCEPT
  41. $IPT -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
  42. $IPT -A INPUT -p tcp -m tcp --dport 139 -j ACCEPT
  43. $IPT -A INPUT -p tcp -m tcp --dport 445 -j ACCEPT
  44. $IPT -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT
  45. # 允许SSH端口的连接,脚本自动侦测目前的SSH端口,否则默认为22端口
  46. $IPT -A INPUT -p tcp -m tcp --dport $sshdport -j ACCEPT
  47. # 允许ping
  48. $IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  49. $IPT -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
  50. # 允许DNS
  51. [ ! -z "$nameserver1" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver1 --dport 53 -j ACCEPT
  52. [ ! -z "$nameserver2" ] && $IPT -A OUTPUT -p udp -m udp -d $nameserver2 --dport 53 -j ACCEPT
  53. # 保存规则并重启IPTABLES
  54. service iptables save
  55. service iptables restart
  56. echo "============================iptables configure completed============================================"
复制代码







欢迎光临 无限星辰工作室-客户无限互联网动力之源 (https://xmspace.net/) Powered by Discuz! X3.4