Impostare un firewall con uno script iptables: differenze tra le versioni

Vai alla navigazione Vai alla ricerca
(Aggiunto template autori)
 
(27 versioni intermedie di 2 utenti non mostrate)
Riga 78: Riga 78:
$IPT -A INPUT -p icmp --icmp-type timestamp-request -m state --state NEW -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type timestamp-request -m state --state NEW -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type timestamp-reply -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type timestamp-reply -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
$IPT -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
###############################################
# Mi difendo dallo spoofing
###############################################
$IPT -A INPUT -s 10.0.0.0/8 -j DROP
$IPT -A INPUT -s 169.254.0.0/16 -j DROP
$IPT -A INPUT -s 172.16.0.0/12 -j DROP
$IPT -A INPUT -s 127.0.0.0/8 -j DROP
$IPT -A INPUT -s 192.168.0.0/24 -j DROP
$IPT -A INPUT -s 192.168.1.0/24 -j DROP
$IPT -A INPUT -s 192.168.10.0/24 -j DROP
$IPT -A INPUT -s 224.0.0.0/4 -j DROP
$IPT -A INPUT -d 224.0.0.0/4 -j DROP
$IPT -A INPUT -s 240.0.0.0/5 -j DROP
$IPT -A INPUT -d 240.0.0.0/5 -j DROP
$IPT -A INPUT -s 0.0.0.0/8 -j DROP
$IPT -A INPUT -d 0.0.0.0/8 -j DROP
$IPT -A INPUT -d 239.255.255.0/24 -j DROP
$IPT -A INPUT -d 255.255.255.255 -j DROP
########################################
# Mi proteggo da attacchi SMURF
########################################
$IPT -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j DROP
$IPT -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j DROP
$IPT -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT
$IPT -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT
#####################################
# Elimino pacchetti non validi
#####################################
$IPT -A INPUT -m state --state INVALID -j DROP
$IPT -A FORWARD -m state --state INVALID -j DROP
$IPT -A OUTPUT -m state --state INVALID -j DROP
##############################################
# Impedisco i port scan e loggo i tentativi
# Gli IP sono bloccati per 24 ore
##############################################
$IPT -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP
$IPT -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP
$IPT -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"
$IPT -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP




Riga 164: Riga 217:
$IPT -A INPUT -p tcp --dport 2605 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 2605 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 2605 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 2605 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 2812 - Monit - Monitor - LAN
$IPT -A INPUT -p tcp --dport 2812 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 3000 - ntop - Monitor - LAN
$IPT -A INPUT -p tcp --dport 3000 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT


# 3306 - MySQL - localhost
# 3306 - MySQL - localhost
Riga 174: Riga 233:
# 10024/10025 - Amavis - localhost
# 10024/10025 - Amavis - localhost
# Non ha bisogno di configurazione
# Non ha bisogno di configurazione
# 8980 - OpenNMS - Server Monitor - LAN
$IPT -A INPUT -p tcp --dport 8980 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 8980 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 9000 - 9200 - 9300 - 8514 - GrayLOG - LOG Monitor - LAN
$IPT -A INPUT -p tcp --dport 9000 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 9200 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 9300 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 8514 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 9000 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 9200 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 9300 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 8514 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 5353 mDNS - LAN
$IPT -A INPUT -p udp -m udp --dport 5353 --sport 5353 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 67-68 DHCP - LAN
$IPT -A INPUT -p udp -m udp --dport 67 --sport 68 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 4200 - Shell in a Box - LAN
$IPT -A INPUT -p tcp --dport 4200 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 4200 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 173 - 138 - 139 - 445 - 389 - 901 - Samba - LAN
$IPT -A INPUT -p tcp --dport 137 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 137 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 138 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 138 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 139 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 139 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 445 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 445 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 389 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 389 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 901 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 901 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT




Riga 264: Riga 361:
             pre-up /etc/iptables-firewall.sh
             pre-up /etc/iptables-firewall.sh
</pre>
</pre>
== Impostare il ban di IP ostili ==
Per impostare il blocco di un IP è sempre possibile agire manualmente, in questo modo:
<pre>
# iptables -I INPUT -s 192.168.1.100 -j DROP
</pre>
ma l'amministrazione e la gestione manuale dei ban agli indirizzi IP ostili non è praticabile.
<br/>
A questo scopo ci può venire in aiuto uno strumento disponibile nei repository ufficiali di Debian:
<pre>
# apt-get install ipset
</pre>
Una volta installato, occorre creare la lista, che chiameremo ''blacklist'':
<pre>
# ipset create blacklist hash:net maxelem 1000000
</pre>
Con il comando:
<pre>
# ipset list
</pre>
possiamo vedere l'elenco delle liste già create.
<br/>
A questo punto è possibile aggiungere a mano il primo IP da bannare:
<pre>
# ipset add blacklist 1.1.2.0/24
# ipset add blacklist 1.1.3.0/24
</pre>
Controlliamo se i due IP sono stati aggiunti alla lista di ban:
<pre>
# ipset list
Name: blacklist
Type: hash:net
Revision: 6
Header: family inet hashsize 1024 maxelem 1000000
Size in memory: 472
References: 0
Number of entries: 2
Members:
1.1.3.0/24
1.1.2.0/24
</pre>
Aggiungere a mano tutti gli IP da bannare è un'impresa titanica; meglio affidarsi a liste già costruite, come quelle utilizzate qui di seguito, disponibili sotto licenza GPL.
<br/>
Per prima cosa installiamo un tool per convertire le liste di iBlockList nel formato accettato da Ipset.
<pre>
# apt-get install python-pip curl
</pre>
Quindi recuperiamo uno script che ci permetterà di mantenere aggiornata la nostra lista di IP ostili:
<pre>
# wget -O /usr/local/sbin/update-blacklist.sh https://raw.githubusercontent.com/trick77/ipset-blacklist/master/update-blacklist.sh
# chmod +x /usr/local/sbin/update-blacklist.sh
# mkdir -p /etc/ipset-blacklist
# wget -O /etc/ipset-blacklist/ipset-blacklist.conf https://raw.githubusercontent.com/trick77/ipset-blacklist/master/ipset-blacklist.conf
</pre>
Quindi salviamo l'attuale configurazione di ipset:
<pre>
# ipset save > /etc/ipset-blacklist/ip-blacklist.restore
</pre>
Modifichiamo il file di configurazione adattandolo alle nostre necessità:
<pre>
# nano /etc/ipset-blacklist/ipset-blacklist.conf
</pre>
Impostiamo un cronjob che ogni domenica a mezzanotte aggiornerà le nostre liste:
<pre>
crontab -e
0 0 * * 7 /usr/local/sbin/update-blacklist.sh /etc/ipset-blacklist/ipset-blacklist.conf
</pre>
Aggiungiamo la nuova regola al nostro script iptables:
<pre>
# nano /etc/iptables-firewall.sh
# Imposto le regole ipset
iptables -I INPUT -m set --match-set blacklist src -j DROP
iptables -I FORWARD -m set --match-set blacklist src -j DROP
</pre>
e facciamo in modo che ipset sia impostato correttamente dopo un riavvio, aggiungendo la direttiva:
<pre>
pre-up  ipset restore -! < /etc/ipset-blacklist/ip-blacklist.restore
</pre>
alla sezione della scheda di rete principale nel file <tt>/etc/network/interfaces</tt>, stando attenti a inserirla '''''prima''''' della direttiva <tt>pre up</tt> impostata in precedenza per l'avvio dello script di iptables.
<br/>
<br/>


== Sitografia ==
== Sitografia ==
http://www.insicurezza.org/firewall/fire_iptablesconf.htm
http://www.sitepoint.com/secure-server-iptables/
<br/>
http://blogs.sitepoint.com/secure-server-iptables/
<br/>
<br/>
http://blog.dediserve.com/configuring-iptables-on-your-vps/
http://news.dediserve.com/2009/09/23/configuring-iptables-on-your-vps/
<br/>
<br/>
http://tuxblog.it/2008/03/03/configurare-un-firewall-con-iptables/
http://www.temporini.net/configurare-un-firewall-con-iptables
<br/>
<br/>
http://www.mod-xslt2.com/people/ccontavalli/docs-it/iptables/iptables4dummies/iptables4dummies-28.html
http://www.commedia.it/ccontavalli/docs-it/iptables/iptables4dummies/
<br/><br/>
<br/><br/>


Menu di navigazione