<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Arquivos portas - Remontti</title>
	<atom:link href="https://blog.remontti.com.br/tag/portas/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.remontti.com.br/tag/portas</link>
	<description>rudimar@remontti</description>
	<lastBuildDate>Wed, 17 Jun 2020 19:50:03 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.remontti.com.br/wp-content/uploads/2024/09/icone-rr-80x80.png</url>
	<title>Arquivos portas - Remontti</title>
	<link>https://blog.remontti.com.br/tag/portas</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Modelo simples e funcional de Firewall com iptables</title>
		<link>https://blog.remontti.com.br/2435</link>
					<comments>https://blog.remontti.com.br/2435#comments</comments>
		
		<dc:creator><![CDATA[Rudimar Remontti]]></dc:creator>
		<pubDate>Fri, 04 May 2018 21:10:53 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Tutoriais]]></category>
		<category><![CDATA[bloqueio]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[portas]]></category>
		<category><![CDATA[Segurança]]></category>
		<guid isPermaLink="false">https://blog.remontti.com.br/?p=2435</guid>

					<description><![CDATA[<p>Neste tutorial vou deixar um scrip bem simples para criar seu firewall, a ideia é que ele seja funcional sem precisar ter que está alterando diversas linhas. Primeiramente precisamos saber quais os serviços (portas)&#46;&#46;&#46;</p>
<p>O post <a href="https://blog.remontti.com.br/2435">Modelo simples e funcional de Firewall com iptables</a> apareceu primeiro em <a href="https://blog.remontti.com.br">Remontti</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" src="https://blog.remontti.com.br/wp-content/uploads/2018/05/iptables-remontti-300x177.png" alt="" width="300" height="177" class="aligncenter size-medium wp-image-2451" srcset="https://blog.remontti.com.br/wp-content/uploads/2018/05/iptables-remontti-300x177.png 300w, https://blog.remontti.com.br/wp-content/uploads/2018/05/iptables-remontti-768x454.png 768w, https://blog.remontti.com.br/wp-content/uploads/2018/05/iptables-remontti-1024x605.png 1024w, https://blog.remontti.com.br/wp-content/uploads/2018/05/iptables-remontti.png 1115w" sizes="(max-width: 300px) 100vw, 300px" /><br />
Neste tutorial vou deixar um scrip bem simples para criar seu firewall, a ideia é que ele seja funcional sem precisar ter que está alterando diversas linhas.</p>
<p>Primeiramente precisamos saber quais os serviços (portas) estão rodando em nosso servidor, para que possamos saber o que queremos proteger. Você pode usar o comando nmpa para fazer scanner de portas, instale ele com apt, e utilize <em>ex: nmap localhost</em>. </p>
<p>No meu exemplo vamos suporte que nosso servidor esteja com SSH e o APACHE instalado, logo temos a porta 22, e a porta 80. Agora preciso saber quem irá ter acesso a este servidor, quais IPs qual eu chamo normalmente de <em>&#8220;IPs de gerencia&#8221;</em> </p>
<p>Agora que sei que vou fechar as portas 22 e 80 e que meus IPs de gerencia são 192.168.254.0/24, 250.250.250.0/28 vamos as alterações do nosso script:</p>
<p>Defina as portas que deseja proteger separada por &#8220;<strong><font color="red">;</font></strong>&#8221;</p>
<pre class="remontti-code">PORTAS=&quot;22;80&quot;</pre>
<p>Defina os IPv4s que terão acesso separado por &#8220;<strong><font color="red">;</font></strong>&#8221;<br />
<em>É válido lembrar do IP de localhost <strong>127.0.0.1</strong>, caso contrário alguma aplicações local não ira funcionar ex.: mysql seria uma.</em></p>
<pre class="remontti-code">IP4GERENCIA=&quot;127.0.0.1;192.168.254.0/24;250.250.250.0/28&quot;</pre>
<p>Defina os IPv4s que terão acesso. (localhost <strong>::1</strong>)</p>
<pre class="remontti-code">IP6GERENCIA=&quot;::1;2001:db8:bebe:c0ca::/64&quot;</pre>
<p>Crie o arquivo do nosso script /etc/init.d/rr-firewall, irei utilizar o editor vim, mas você pode usar o seu favorito.</p>
<pre class="remontti-code"># vim /etc/init.d/rr-firewall</pre>
<pre class="remontti-code">#!/bin/bash
### BEGIN INIT INFO
# Provides:          rr-firewall
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
### END INIT INFO

#Defina as portas que deseja proteger
PORTAS=&quot;22;80&quot;
#Defina os IPv4s que terão acesso a estas portas
IP4GERENCIA=&quot;127.0.0.1;192.168.254.0/24;250.250.250.0/28&quot;
#Defina os IPv4s que terão acesso a estas portas
IP6GERENCIA=&quot;::1;2001:db8:bebe:c0ca::/64&quot;

# Não altere as linhas abaixo
VERMELHO=&#039;\033[1;31m&#039;
VERDE=&#039;\033[1;32m&#039;
AZUL=&#039;\033[1;36m&#039;
AMARELO=&#039;\033[1;33m&#039;
ROSA=&#039;\033[1;35m&#039;
NC=&#039;\033[0m&#039;

function startFirewall(){
    /sbin/iptables -F
    /sbin/iptables -X
    /sbin/iptables -t nat -F
    /sbin/iptables -X -t nat
    /sbin/iptables -F -t mangle
    /sbin/iptables -X -t mangle
    /sbin/ip6tables -F
    /sbin/ip6tables -X
    /sbin/ip6tables -F -t mangle
    /sbin/ip6tables -X -t mangle
    /sbin/modprobe ip_conntrack_ftp
    /sbin/modprobe ip_nat_ftp
    /sbin/modprobe ipt_state
    /sbin/modprobe ipt_limit
    /sbin/modprobe ipt_MASQUERADE
    /sbin/modprobe ipt_LOG
    /sbin/modprobe iptable_nat
    /sbin/modprobe iptable_filter
    /sbin/modprobe ip_gre
    #Protege portas IPv4
    echo; echo -e &quot;[${ROSA} Regras IPv4 ${NC}]&quot;; echo
    portas=$(echo $PORTAS | tr &quot;;&quot; &quot;\n&quot;)
    for porta in $portas
    do
        ip4s=$(echo $IP4GERENCIA | tr &quot;;&quot; &quot;\n&quot;)
        for ip4 in $ip4s
        do
            /sbin/iptables -A INPUT -s $ip4 -p tcp --dport $porta -j ACCEPT
            /sbin/iptables -A INPUT -s $ip4 -p udp --dport $porta -j ACCEPT
            echo -e &quot;[${VERDE} ok ${NC}] Porta ${AMARELO}[$porta]${NC} aberta para ${AZUL}$ip4${NC}&quot;
            sleep 0.1
        done
    done
    portas=$(echo $PORTAS | tr &quot;;&quot; &quot;\n&quot;)
    for porta in $portas
    do
        /sbin/iptables -A INPUT -p tcp --dport $porta -j DROP
        echo -e &quot;[${VERDE} ok ${NC}] Porta ${VERMELHO}[$porta]${NC} fechada&quot;
        sleep 0.1
    done
    #Protege portas IPv6
    echo; echo -e &quot;[${ROSA} Regras IPv6 ${NC}]&quot;; echo

    portas=$(echo $PORTAS | tr &quot;;&quot; &quot;\n&quot;)
    for porta in $portas
    do
        ip6s=$(echo $IP6GERENCIA | tr &quot;;&quot; &quot;\n&quot;)
        for ip6 in $ip6s
        do
            /sbin/ip6tables -A INPUT -s $ip6 -p tcp --dport $porta -j ACCEPT
            /sbin/ip6tables -A INPUT -s $ip6 -p udp --dport $porta -j ACCEPT
            echo -e &quot;[${VERDE} ok ${NC}] Porta ${AMARELO}[$porta]${NC} aberta para ${AZUL}$ip6${NC}&quot;
        done
    done
    portas=$(echo $PORTAS | tr &quot;;&quot; &quot;\n&quot;)
    for porta in $portas
    do
        /sbin/ip6tables -A INPUT -p tcp --dport $porta -j DROP
        echo -e &quot;[${VERDE} ok ${NC}] Porta ${VERMELHO}[$porta]${NC} fechada&quot;
        sleep 0.1
    done
}

function stopFirewall(){
    /sbin/iptables -F
    /sbin/iptables -X
    /sbin/iptables -t nat -F
    /sbin/iptables -X -t nat
    /sbin/iptables -F -t mangle
    /sbin/iptables -X -t mangle

    /sbin/ip6tables -F
    /sbin/ip6tables -X
    /sbin/ip6tables -F -t mangle
    /sbin/ip6tables -X -t mangle

    /sbin/modprobe ip_conntrack_ftp
    /sbin/modprobe ip_nat_ftp
    /sbin/modprobe ipt_state
    /sbin/modprobe ipt_limit
    /sbin/modprobe ipt_MASQUERADE
    /sbin/modprobe ipt_LOG
    /sbin/modprobe iptable_nat
    /sbin/modprobe iptable_filter
    /sbin/modprobe ip_gre
}

case &quot;$1&quot; in
    start )
        startFirewall
        echo; echo -e &quot;[${VERDE} Firewall carregado ${NC}]&quot;; 
        echo &quot;Use: /etc/init.d/rr-firewall status&quot;
        echo &quot;para verificar as regras&quot;
        ;;

    stop )
        stopFirewall
        echo; echo -e &quot;[${VERDE} Regras de firewall removidas ${NC}]&quot;; echo
        ;;

    restart )
        stopFirewall
        sleep 1
        startFirewall
        ;;

    status )
        echo; echo -e &quot;[${VERDE} Regras IPv4 ${NC}]&quot;; echo
        /sbin/iptables -nL
        echo; echo -e &quot;[${VERDE} Regras IPv6 ${NC}]&quot;; echo
        /sbin/ip6tables -nL
        ;;

    * )
        echo &quot;Opção inválida, use rr-firewall start | stop | restart | status&quot;
        ;;
esac</pre>
<p>Por fim aplicamos as permissões necessárias.</p>
<pre class="remontti-code"># chmod a+x  /etc/init.d/rr-firewall</pre>
<p>Para executa-lo use:<br />
<strong>/etc/init.d/rr-firewall start </strong><br />
<a href="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400.png" data-rel="lightbox-gallery-CxQcmqP6" data-rl_title="" data-rl_caption="" title=""><img decoding="async" src="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400-300x188.png" alt="" width="300" height="188" class="alignnone size-medium wp-image-2441" srcset="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400-300x188.png 300w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400-768x482.png 768w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400-1024x643.png 1024w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175400.png 1354w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><strong>/etc/init.d/rr-firewall status</strong><br />
<a href="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413.png" data-rel="lightbox-gallery-CxQcmqP6" data-rl_title="" data-rl_caption="" title=""><img decoding="async" src="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413-300x188.png" alt="" width="300" height="188" class="alignnone size-medium wp-image-2442" srcset="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413-300x188.png 300w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413-768x482.png 768w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413-1024x643.png 1024w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175413.png 1354w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p><strong>/etc/init.d/rr-firewall stop</strong><br />
<a href="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430.png" data-rel="lightbox-gallery-CxQcmqP6" data-rl_title="" data-rl_caption="" title=""><img loading="lazy" decoding="async" src="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430-300x188.png" alt="" width="300" height="188" class="alignnone size-medium wp-image-2443" srcset="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430-300x188.png 300w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430-768x482.png 768w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430-1024x643.png 1024w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175430.png 1354w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a></p>
<p><strong>/etc/init.d/rr-firewall restart   </strong><br />
<a href="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449.png" data-rel="lightbox-gallery-CxQcmqP6" data-rl_title="" data-rl_caption="" title=""><img loading="lazy" decoding="async" src="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449-300x188.png" alt="" width="300" height="188" class="alignnone size-medium wp-image-2444" srcset="https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449-300x188.png 300w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449-768x482.png 768w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449-1024x643.png 1024w, https://blog.remontti.com.br/wp-content/uploads/2018/05/Screenshot_20180504_175449.png 1354w" sizes="auto, (max-width: 300px) 100vw, 300px" /></a><br />
<em>Nos prints estou fechando a porta 3306 e 80</em></p>
<p>Pronto! Seu servidor já vai estar protegido <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>Como carregar ele na inicialização? Acesse: <a href="https://blog.remontti.com.br/2478" rel="noopener noreferrer" target="_blank"> Como usar o systemd para iniciar seu script/Aplicação no Debian</a></p>
<p>Dúvidas sugestões deixe seu comentário.</p>
<p>O post <a href="https://blog.remontti.com.br/2435">Modelo simples e funcional de Firewall com iptables</a> apareceu primeiro em <a href="https://blog.remontti.com.br">Remontti</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.remontti.com.br/2435/feed</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
