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

标题: iptables 脚本工具 [打印本页]

作者: crx349    时间: 2019-9-18 02:44
标题: iptables 脚本工具
分享一个一键设置Centos系统防火墙脚本
  1. #!/bin/bash
  2. #无源IP限制的端口
  3. TCP_DPORT="80 443 22 21 2299 8888 9922 39000:40000"
  4. UDP_DPORT=""

  5. #带有源IP限制的端口 192.168.0.1改成对应内网ip
  6. S_TCP_IP="192.168.0.1 192.168.0.2 192.168.0.3"
  7. S_TCP_MAC=""
  8. S_TCP_DPORT="3306 6379"

  9. S_UDP_IP=""
  10. S_UDP_MAC=""
  11. S_UDP_DPORT=""

  12. #黑名单IP,禁止接入
  13. DROP_IP=""

  14. #系统版本,输入大版本号,6(Centos 6)或者7(Centos 7)
  15. sysver=


  16. if [[ ! -n $sysver ]];then
  17.         while true;
  18.         do
  19.                 read -p "请选择系统版本[1.Centos6 2.Centos7]: " version
  20.                 case $version in
  21.                                 1|6)
  22.                                 sysver=6
  23.                                 break;
  24.                                 ;;
  25.                                 2|7)
  26.                                 sysver=7
  27.                                 break;
  28.                                 ;;
  29.                                 *)
  30.                                 echo "----请输入1或者2----"
  31.                                 ;;
  32.                 esac
  33.         done               
  34. fi

  35. function config_iptables() {
  36. #判断SSH端口
  37. if [ ! -n "$(egrep -wi "Port" /etc/ssh/sshd_config | grep -v \#)" ];then
  38.         sshport=22
  39. else
  40.         if [ "$(egrep -wi "Port" /etc/ssh/sshd_config |grep -v \#|wc -l)" == "1" ];then
  41.                 sshport=$(egrep -wi "Port" /etc/ssh/sshd_config |grep -v \# |awk -F" " '{print $2}')
  42.         else
  43.                 sshport=0
  44.         fi
  45. fi

  46. iptables -F
  47. iptables -X
  48. iptables -Z
  49. iptables -P INPUT DROP
  50. iptables -P FORWARD DROP
  51. iptables -P OUTPUT ACCEPT
  52. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  53. iptables -A INPUT -p icmp -j ACCEPT
  54. iptables -A INPUT -i lo -j ACCEPT

  55. if [ "$sshport" == "0" ];then
  56.         for sshport in $(egrep -wi "Port" /etc/ssh/sshd_config |grep -v \# |awk -F" " '{print $2}')
  57.         do
  58.                 iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport $sshport -j ACCEPT
  59.         done
  60. else
  61.         iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport $sshport -j ACCEPT
  62. fi

  63. if [[ -n ${TCP_DPORT} ]];then
  64.         for t_prot in ${TCP_DPORT};
  65.                 do
  66.                         iptables -A INPUT -p tcp -m tcp --dport ${t_prot} -j ACCEPT
  67.                 done
  68. fi

  69. if [[ -n ${UDP_DPORT} ]];then
  70.         for u_port in ${UDP_DPORT};
  71.                 do
  72.                         iptables -A INPUT -p udp --dport ${u_port} -j ACCEPT
  73.                 done
  74. fi

  75. if [[ -n ${S_TCP_IP} && -n ${S_TCP_DPORT} ]];then
  76.         for ip in ${S_TCP_IP};
  77.                 do
  78.                         for s_tport in ${S_TCP_DPORT};
  79.                                 do
  80.                                         iptables -A INPUT -p tcp -m tcp -s $ip --dport ${s_tport} -j ACCEPT
  81.                                 done
  82.                 done
  83. fi

  84. if [[ -n ${S_TCP_MAC} && -n ${S_TCP_DPORT} ]];then
  85.         for tmac in ${S_TCP_MAC};
  86.                 do
  87.                         for s_tport in ${S_TCP_DPORT};
  88.                                 do
  89.                                         iptables -A INPUT -p tcp -m mac --mac-source $tmac --dport ${s_tport} -j ACCEPT
  90.                                 done
  91.                 done
  92. fi

  93. if [[ -n ${S_UDP_IP} && -n ${S_UDP_DPORT} ]];then
  94.         for ip in ${S_UDP_IP};
  95.                 do
  96.                         for s_uport in ${S_UDP_DPORT};
  97.                                 do
  98.                                         iptables -A INPUT -p udp -s $ip --dport ${s_uport} -j ACCEPT
  99.                                 done
  100.                 done
  101. fi

  102. if [[ -n ${S_UDP_MAC} && -n ${S_UDP_DPORT} ]];then
  103.         for umac in ${S_UDP_MAC};
  104.                 do
  105.                         for s_uport in ${S_UDP_DPORT};
  106.                                 do
  107.                                         iptables -A INPUT -p udp -m mac --mac-source $umac --dport ${s_uport} -j ACCEPT
  108.                                 done
  109.                 done
  110. fi
  111. if [[ -n ${DROP_IP} ]];then
  112.         for d_ip in ${DROP_IP};
  113.                 do
  114.                         iptables -A INPUT -s ${d_ip} -j DROP
  115.                 done
  116. fi
  117. iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
  118. iptables -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  119. }

  120. function main() {
  121. if (( "$sysver" == "6" ));then
  122.         echo "*/3 * * * * /etc/init.d/iptables stop" >> /var/spool/cron/root
  123.         echo -e "[\033[35mINFO\033[0m] [开始配置防火墙策略,并加入每5分钟关闭防火墙服务的定时任务,如稍后无法连接服务器,请静等五分钟再尝试连接]"
  124.         /etc/init.d/iptables restart
  125.         sleep 3
  126.         config_iptables
  127.         /etc/init.d/iptables save
  128.         echo -e "[\033[32mOK\033[0m] [防火墙策略已生效,测试无问题后请在5分钟内删除关闭防火墙服务的定时任务]"
  129. elif (( "$sysver" == "7" ));then
  130.         echo "*/5 * * * * /bin/systemctl stop firewalld" >> /var/spool/cron/root
  131.         echo -e "[\033[35mINFO\033[0m] [开始配置防火墙策略,并加入每5分钟关闭防火墙服务的定时任务,如稍后无法连接服务器,请静等五分钟再尝试连接]"
  132.         systemctl restart firewalld
  133.         sleep 3
  134.         config_iptables
  135.         echo -e "[\033[32mOK\033[0m] [防火墙策略已生效,测试无问题后请在5分钟内删除关闭防火墙服务的定时任务]"
  136. else
  137.         echo "不正确的版本号,请检查脚本"
  138.         exit 0
  139. fi
  140. }
  141. main

复制代码







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