Advertisement
LightProgrammer000

Firewall [Debian]

Jan 15th, 2019
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Funcao 1
  4.  
  5. # Firewall
  6. firewall()
  7. {
  8.         if [[ -f /usr/local/bin/firewall.sh ]]
  9.         then
  10.                 echo "Arquivo ja existente"
  11.         else
  12.                 echo "#!/bin/bash" > /usr/local/bin/firewall.sh
  13.         echo "# Limpar todas as regras pré existentes" >> /usr/local/bin/firewall.sh
  14.         echo "iptables -F" >> /usr/local/bin/firewall.sh
  15.         echo "iptables -t nat -F" >> /usr/local/bin/firewall.sh
  16.         echo "iptables -t mangle -F" >> /usr/local/bin/firewall.sh
  17.  
  18.         echo "# A linha abaixo ativa o módulo do netfilter que evita ataques DoS" >> /usr/local/bin/firewall.sh
  19.         echo "echo 1 > /proc/sys/net/ipv4/tcp_syncookies" >> /usr/local/bin/firewall.sh
  20.  
  21.         echo "# Liberar portas dos serviços necessários" >> /usr/local/bin/firewall.sh
  22.         echo "iptables -A INPUT -p tcp --dport 22 -j ACCEPT" >> /usr/local/bin/firewall.sh
  23.         echo "iptables -A INPUT -p tcp --dport 80 -j ACCEPT" >> /usr/local/bin/firewall.sh
  24.         echo "iptables -A INPUT -p tcp --dport 443 -j ACCEPT" >> /usr/local/bin/firewall.sh
  25.         echo "iptables -A INPUT -p tcp --dport 3306 -j ACCEPT" >> /usr/local/bin/firewall.sh
  26.  
  27.         echo "# Bloqueio de PING" >> /usr/local/bin/firewall.sh
  28.         echo "iptables -I INPUT -p ICMP -j DROP" >> /usr/local/bin/firewall.sh
  29.  
  30.         echo "# A linha abaixo faz o bloqueio de conexões nas demais portas" >> /usr/local/bin/firewall.sh
  31.         echo "iptables -A INPUT -p tcp --syn -j DROP" >> /usr/local/bin/firewall.sh
  32.  
  33.                 chmod +x /usr/local/bin/firewall.sh
  34.  
  35.                 echo "Script 'firewall.sh' criado"
  36.         fi
  37. }
  38.  
  39. # Servico do Firewall
  40. firewall_service()
  41. {
  42.         if [[ -f /etc/systemd/system/firewall.service ]]
  43.         then
  44.                 echo "Arquivo ja existente"
  45.  
  46.         else
  47.                 echo "[Unit]" >> /etc/systemd/system/firewall.service
  48.                 echo "Description=Firewall" >> /etc/systemd/system/firewall.service
  49.  
  50.         echo "[Service]" >> /etc/systemd/system/firewall.service
  51.                 echo "ExecStart=/usr/local/bin/firewall.sh start" >> /etc/systemd/system/firewall.service
  52.                 echo "ExecStop=/usr/local/bin/firewall.sh stop" >> /etc/systemd/system/firewall.service
  53.                 echo "ExecReload=/usr/local/bin/firewall.sh restart" >> /etc/systemd/system/firewall.service
  54.  
  55.                 echo "[Install]" >> /etc/systemd/system/firewall.service
  56.                 echo "WantebBy=multi-user.target" >> /etc/systemd/system/firewall.service
  57.  
  58.                 echo "Servico 'firewall.service' criado"
  59.         fi
  60. }
  61.  
  62. # Exclusao de Arquivos
  63. deletar_arquivo()
  64. {
  65.         if [[ -f /etc/systemd/system/firewall.service || -f /usr/local/bin/firewall.sh ]]
  66.         then
  67.                 rm -rf /usr/local/bin/firewall.sh
  68.                 rm -rf /etc/systemd/system/firewall.service
  69.  
  70.                 echo "Arquivos excluidos"
  71.  
  72.         else
  73.                 echo "Arquivos nao existem"
  74.         fi
  75. }
  76.  
  77. # Execucao de Comandos
  78. executar()
  79. {
  80.         /usr/local/bin/firewall.sh
  81.         systemctl daemon-reload
  82.         systemctl enable firewall
  83.         systemctl start firewall
  84.         systemctl status firewall
  85. }
  86.  
  87. ############
  88. # Programa #
  89. ############
  90.  
  91. i=1
  92. while(( $i == 1 ))
  93. do
  94.     clear
  95.     echo "Programa Firewall"
  96.         echo ""
  97.         echo "Tarefas Agendadas"
  98.         echo "[1] Criar arquivos de regras de Firewall"
  99.         echo "[2] Executar junto ao sistema"
  100.         echo "[3] Deletar Arquivo de Regras de Firewall [ICMP]"
  101.         echo "[4] Sair"
  102.         echo ""
  103.         echo -n "Resp: "
  104.         read resp
  105.  
  106.         if (( $resp == 1 ))
  107.         then
  108.                 firewall
  109.                 firewall_service
  110.         echo ""
  111.         echo "--- Pressione qualquer tecla ---"
  112.         read
  113.  
  114.         elif (( $resp == 2))
  115.         then
  116.                 executar
  117.         echo ""
  118.         echo "Pressione qualquer tecla"
  119.         read
  120.  
  121.         elif (( $resp == 3 ))
  122.         then
  123.                 deletar_arquivo
  124.         echo ""
  125.         echo "--- Pressione qualquer tecla ---"
  126.         read
  127.  
  128.         else
  129.                 i=2
  130.                 clear
  131.         fi
  132. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement