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

(Creata pagina con '== Introduzione == Questa guida non vuole essere il solito howto dove si spiega che in <code>iptables</code> ci sono le catene, le policy di default, quali sono e come funzionano...')
 
Riga 58: Riga 58:
</pre>
</pre>
e diamogli questo contenuto, mettendo in pratica quanto visto nel paragrafo precedente:
e diamogli questo contenuto, mettendo in pratica quanto visto nel paragrafo precedente:
<pre>
#!/bin/sh
###########################
# Imposto alcune variabili
###########################
# Il path di iptables
IPT="/sbin/iptables"
# Interfaccia di rete esterna
IFACE=bond0
########################
# Un messaggio di avvio
########################
echo -n " Loading iptables rules..."
#####################################
# Pulisco la configurazione corrente
#####################################
# Cancellazione delle regole presenti nelle chains
$IPT -F
$IPT -F -t nat
# Eliminazione delle chains non standard vuote
$IPT -X
# Inizializzazione dei contatori (utile per il debugging)
$IPT -Z
###################################################
# Blocco tutto il traffico tranne quello in uscita.
# NOTA: per ragioni di sicurezza sarebbe opportuno
# bloccare anche il traffico in uscita e stabilire
# poi delle regole selettive
###################################################
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
##############################
# Abilito il traffico locale
##############################
$IPT -A INPUT -i lo -j ACCEPT
$IPT -A OUTPUT -o lo -j ACCEPT
#####################################################
# Imposto alcune regole per i pacchetti ICMP di ping
#####################################################
$IPT -A INPUT -p icmp --icmp-type echo-reply -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/s -m state --state NEW -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type destination-unreachable -m state --state NEW -j ACCEPT
$IPT -A INPUT -p icmp --icmp-type time-exceeded -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
###############################################
# Blocco le nuove connessioni senza SYN e
# mi proteggo dagli attacchi Denial of Service
###############################################
$IPT -N syn-flood
$IPT -A INPUT -i $IFACE -p tcp syn -j syn-flood
$IPT -A syn-flood -m limit limit 1/s limit-burst 4 -j RETURN
$IPT -A syn-flood -j DROP
$IPT -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
#########################################################
# Consento il traffico delle connessioni gia' stabilite
#########################################################
$IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#########################################################
# Regole sulle porte. Da modificare secondo le esigenze
# Per ogni regola nel commento viene indicato:
# 1) il numero della porta
# 2) il nome del servizio
# 3) il protocollo
# 4) il livello di accesso
#    - pubblico = accesso permesso a tutti
#    - LAN = accesso permesso solo ai client della LAN
#########################################################
# 21 - ProFTPD - FTP - pubblico
$IPT -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT
# 25 - Postfix - SMTP - pubblico
$IPT -A INPUT -p tcp --dport 25 -m state --state NEW -j ACCEPT
# 80/443 - Apache - HTTP - pubblico
$IPT -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
$IPT -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
# 110 - Dovecot - POP3 - pubblico
$IPT -A INPUT -p tcp --dport 110 -m state --state NEW -j ACCEPT
# 111 - Ulogd - Syslog Server - VPN
$IPT -A INPUT -p tcp --dport 111 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 111 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 111 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p udp --dport 111 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 143 - Dovecot - IMAP - pubblico
$IPT -A INPUT -p tcp --dport 143 -m state --state NEW -j ACCEPT
# 667 - Darkstat - Statistiche - VPN
$IPT -A INPUT -p tcp --dport 667 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 667 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 993 - Dovecot - IMAPs - pubblico
$IPT -A INPUT -p tcp --dport 993 -m state --state NEW -j ACCEPT
# 995 - Dovecot - POP3s - pubblico
$IPT -A INPUT -p tcp --dport 995 -m state --state NEW -j ACCEPT
# 1050/1051 - Zabbix - Monitor - VPN
$IPT -A INPUT -p tcp --dport 1050 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 1050 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 1051 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 1051 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 1194 - OpenVPN - pubblico
$IPT -A INPUT -p tcp --dport 1194 -m state --state NEW -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
# 2000 - Sieve - Spam filter - localhost
# Non ha bisogno di configurazione
# 2293 - OpenSSH - SSH - pubblico
$IPT -A INPUT -p tcp --dport 2293 -m state --state NEW -j ACCEPT
# 2605 - BitMeter - Monitor - VPN
$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
# 3306 - MySQL - localhost
# Non ha bisogno di configurazione
# 10000  Webmin - Monitor - VPN
$IPT -A INPUT -p tcp --dport 10000 -m state --state NEW -s 192.168.90.0/24 -j ACCEPT
$IPT -A INPUT -p tcp --dport 10000 -m state --state NEW -s 10.0.0.0/24 -j ACCEPT
# 10024/10025 - Amavis - localhost
# Non ha bisogno di configurazione
###############################################################
# Regole di sicurezza
# Block fragments and Xmas tree as well as SYN,FIN and SYN,RST
###############################################################
$IPT -A INPUT -p ip -f -j DROP
$IPT -A INPUT -p tcp --tcp-flags ALL ACK,RST,SYN,FIN -j DROP
$IPT -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
$IPT -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
###############################
# Concludo lo script firewall
###############################
# echo -n "Iptables successfully configured."
</pre>