Advertisement
LightProgrammer000

3.Firewall[tabela_NAT]

Mar 15th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.57 KB | None | 0 0
  1. # !/bin/bash
  2.  
  3. ###################
  4. ##### Funcoes #####
  5. ###################
  6.  
  7. # Comando: Desativando Firewall
  8. Configuracoes()
  9. {
  10.     # Tabela: NAT
  11.     iptables -t nat -P INPUT ACCEPT
  12.     iptables -t nat -P OUTPUT ACCEPT
  13.     iptables -t nat -P FORWARD ACCEPT
  14. }
  15.  
  16. Regra_Lista()
  17. {
  18.     # Apresentacao
  19.     clear
  20.     echo ""
  21.     echo -e "\033[01;37m ---------------------- \033[01;37m"
  22.     echo -e "\033[01;36m * Listagem de regras * \033[01;37m"
  23.     echo -e "\033[01;37m ---------------------- \033[01;37m"
  24.     echo -e ""
  25.  
  26.     # Comando: Listagem de regras detalhadas + sequencia
  27.     iptables -t nat -nvL --line-number
  28.     echo -e "\n\033[01;31m --------------------------------------------------------------------- \033[01;37m\n"
  29.    
  30.     # Comando: Listagem de regras em sequencia
  31.     iptables -t nat -L --line-number
  32.     echo -e "\n\033[01;31m --------------------------------------------------------------------- \033[01;37m\n"
  33. }
  34.  
  35. Regra_Limpeza()
  36. {
  37.     # Apresentacao
  38.     clear
  39.     echo ""
  40.     echo -e "\033[01;37m ---------------------- \033[01;37m"
  41.     echo -e "\033[01;36m * Regras reiniciadas * \033[01;37m"
  42.     echo -e "\033[01;37m ---------------------- \033[01;37m"
  43.     echo ""
  44.  
  45.     # Comando: Limpando memoria [flush]
  46.     iptables -t nat -X
  47.     iptables -t nat -F
  48. }
  49.  
  50. Regra_Deletar()
  51. {
  52.     # Apresentacao
  53.     clear
  54.     echo ""
  55.     echo -e "\033[01;37m -------------------- \033[01;37m"
  56.     echo -e "\033[01;36m * Delecao de regra * \033[01;37m"
  57.     echo -e "\033[01;37m -------------------- \033[01;37m"
  58.     echo ""
  59.  
  60.     # Entrada de dados
  61.     echo -e -n "\033[01;35m - Posicao: \033[01;37m"
  62.     read pos
  63.  
  64.     # Comando: Deletar regra pela posicao
  65.     iptables -t nat -D POSTROUTING $pos
  66.     iptables -t nat -D PREROUTING $pos
  67. }
  68.  
  69. Liberar_Internet()
  70. {
  71.     # Apresentacao
  72.     clear
  73.     echo ""
  74.     echo -e "\033[01;37m -------------------- \033[01;37m"
  75.     echo -e "\033[01;36m * Liberar Internet * \033[01;37m"
  76.     echo -e "\033[01;37m -------------------- \033[01;37m"
  77.     echo ""
  78.  
  79.     # Entrada de dados
  80.     echo -e "\033[01;36m * Interface * \033[01;37m"
  81.     ifconfig
  82.     echo ""
  83.  
  84.     # Entrada de dados
  85.     echo -e -n "\033[01;35m - Interface de rede: \033[01;37m"
  86.     read interface
  87.  
  88.     # Comando: Deletar regra pela posicao
  89.     iptables -t nat -A POSTROUTING -o $interface -j MASQUERADE
  90.     echo "1" > /proc/sys/net/ipv4/ip_forward
  91. }
  92.  
  93. Redirecionamento_Porta()
  94. {
  95.     # Apresentacao
  96.     clear
  97.     echo ""
  98.     echo -e "\033[01;37m ------------------------------ \033[01;37m"
  99.     echo -e "\033[01;36m * Redirecionamento de portas * \033[01;37m"
  100.     echo -e "\033[01;37m ------------------------------ \033[01;37m"
  101.     echo ""
  102.  
  103.     # IP [destino] = Nossa maquina
  104.     echo -e -n "\033[01;36m - IP [destino]: \033[01;37m"
  105.     read ip_destino
  106.     echo ""
  107.  
  108.     # Porta origem = 22
  109.     echo -e -n "\033[01;36m - Porta [servico]: \033[01;37m"
  110.     read porta_servico
  111.     echo ""
  112.    
  113.     # IP [origem] = Maquina alvo
  114.     echo -e -n "\033[01;36m - IP [origem]: \033[01;37m"
  115.     read ip_origem
  116.     echo ""
  117.  
  118.     # Porta Alvo = 2200
  119.     echo -e -n "\033[01;36m - Porta [mascarada]: \033[01;37m"
  120.     read porta_mascarada
  121.     echo ""
  122.  
  123.     # Comando: Redirecionamento de porta
  124.     iptables -t nat -A PREROUTING -d $ip_destino -p TCP --dport $porta_mascarada -j DNAT --to $ip_origem:$porta_servico
  125. }
  126.  
  127. ####################
  128. ##### IPTABLES #####
  129. ####################
  130.  
  131. # Configuracao
  132. Configuracoes
  133.  
  134. # Estrutura em loop
  135. while (( 1 ))
  136. do
  137.     # Menu
  138.     clear
  139.     echo -e "\033[01;36m --------------- DATA --------------- \033[01;37m"
  140.     echo -e "\033[01;32m * Num: $(date +%d)           \033[01;37m"
  141.     echo -e "\033[01;31m * Dia: $(date +%a)           \033[01;37m"
  142.     echo -e "\033[01;33m * Mes: $(date +%b)           \033[01;37m"
  143.     echo -e "\033[01;35m * Ano: $(date +%Y)           \033[01;37m"
  144.     echo -e "\033[01;36m ------------------------------------ \033[01;37m"
  145.     echo ""
  146.     echo -e "\033[01;37m ################################# FIREWALL ################################## \033[01;37m\n"
  147.     echo -e "\033[01;33m [1]  Regras [lista]                                   \033[01;37m"
  148.     echo -e "\033[01;33m [2]  Regras [limpeza]                                     \033[01;37m"
  149.     echo -e "\033[01;33m [3]  Regras [deletar]                                 \033[01;37m"
  150.     echo -e ""
  151.     echo -e "\033[01;34m [4] Liberar [internet]                                \033[01;37m"
  152.     echo -e "\033[01;35m [5] Redirecionamento [porta]                          \033[01;37m"
  153.     echo ""
  154.     echo -e "\033[01;37m ############################################################################# \033[01;37m\n"
  155.     echo -e -n "\n\033[01;34m + Opc: \033[01;37m"
  156.     read opc
  157.     echo ""
  158.  
  159.     # Estrutura em escolha
  160.     case $opc in
  161.     1)
  162.         Regra_Lista;;
  163.  
  164.     2)
  165.         Regra_Limpeza;;
  166.  
  167.     3)
  168.         Regra_Deletar;;
  169.  
  170.     4)
  171.         Liberar_Internet;;
  172.    
  173.     5)
  174.         Redirecionamento_Porta;;
  175.    
  176.     *)
  177.         ;;
  178.     esac
  179.  
  180.     echo -e "\n\033[01;32m =-=-= Press [ENTER] =-=-= \033[01;37m\n"
  181.     read
  182. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement