Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Tip] Uno script di init per iptables
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

Goto page 1, 2, 3, 4  Next  
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Risorse italiane (documentazione e tools)
View previous topic :: View next topic  
Author Message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Sun May 22, 2005 6:04 pm    Post subject: [Tip] Uno script di init per iptables Reply with quote

A giro per la rete (e per le guide gentoo) ho raccattato un po' di script di init per iptables e ne ho fatto un copia&incolla ragionato...
Siccome tanta gente spesso posta topic in cerca di una configurazione per il loro firewall casalingo, questo penso possa fare al caso loro...

[AGGIORNATO 01/05/2007]
Changelog:
- Lo script di init non contiene più regole preimpostate ma si appoggia esclusivamente ad uno script esterno per le regole da caricare
- Lo script di esempio per le regole è ora un po' più interattivo e contiene alcuni parametri da riga di comando con cui chiamare più set di regole. Date un'occhiata allo script ma è abbastanza intuitivo.
- La directory di default per lo script è ora /etc/firewall e non più /etc/conf.d in quanto ritengo sia più appropriata


Va copiato il /etc/init.d/ con il nome che preferite (io l'ho chiamato "muro_di_fuoco") e aggiunto al runlevel che volete
poi dategli i permessi giusti con
Code:
chmod 755 /etc/init.d/muro_di_fuoco
chown root:root /etc/init.d/muro_di_fuoco

Ha delle opzioni che sono molto intuitive e che sono descritte in fondo allo script :wink:
In ogni caso basta lanciare
Code:
/etc/init.d/muro_di_fuoco showoptions
per vedere le opzioni
Create anche una cartella /etc/firewall
Code:
mkdir /etc/firewall
chmod 700 /etc/firewall

per lo script contenente le regole da caricare. Se preferite potete specificare una directory diversa nello script di init.

in /etc/firewall/muro_di_fuoco potete scrivere le vostre regole personalizzate (sotto forma di script eseguibile... ) che verranno caricate all'avvio dello script
In questo stesso post trovate un esempio di file di configurazione.
In pratica in questo modo avete un modo veloce e "user friendly" per caricare le vostre regole del firewall (con la possibilità di fare start, stop, restart, block_all, status etc... )

Code:

#!/sbin/runscript

#Defininco la posizione degli eseguibili di iptables
IPTABLES=/sbin/iptables
IPTABLESSAVE=/sbin/iptables-save
IPTABLESRESTORE=/sbin/iptables-restore

#file di configurazione del firewall
#deve essere uno script eseguibile con una sintassi bash
FIREWALL=/etc/firewall/muro_di_fuoco
FIREWALL_SAVE=/etc/firewall/firewall.rules

#Interfacce
IFACE=eth0 #Interfaccia esterna (internet)
LFACE=eth1 #Interfaccia interna (lan)

opts="${opts} showstatus block_all save restore showoptions"

depend() {
  need net
}

start() {
  ebegin "Caricamento regole del firewall"
  if [ -e "${FIREWALL}" ]; then
    $FIREWALL
    eend $?
  else
    eend 1 "${FIREWALL} non esiste"
    return 1
  fi
}

stop() {
  ebegin "Arresto del firewall (tutte le regole su ACCEPT)"
 
  #Clear all chains in all tables
  $IPTABLES -F
  $IPTABLES -F -t mangle
  $IPTABLES -F -t nat
  $IPTABLES -X
  $IPTABLES -X -t mangle
  $IPTABLES -X -t nat
 
  #Set Defaults to ACCEPT
  $IPTABLES -P INPUT ACCEPT
  $IPTABLES -P OUTPUT ACCEPT
  $IPTABLES -P FORWARD ACCEPT

  eend $?
}

showstatus() {
  ebegin "Status"
  $IPTABLES -L -n -v --line-numbers
  einfo "NAT status"
  $IPTABLES -L -n -v --line-numbers -t nat
  eend $?
}

block_all() {
  ebegin "Impostazione delle regole di BLOCCO TOTALE in entrata e in uscita"
 
  #Clear all chains and accept packets only from localhost
  $IPTABLES -F
  $IPTABLES -F -t mangle
  $IPTABLES -F -t nat
  $IPTABLES -X
  $IPTABLES -X -t mangle
  $IPTABLES -X -t nat
  $IPTABLES -P FORWARD DROP
  $IPTABLES -P INPUT   DROP
  $IPTABLES -P OUTPUT  DROP
  $IPTABLES -A INPUT -i lo -j ACCEPT
  $IPTABLES -A OUTPUT -o lo -j ACCEPT
  eend $?
}

save() {
  ebegin "Salvataggio delle regole del firewall"
  $IPTABLESSAVE > $FIREWALL_SAVE
  eend $?
}

restore() {
  ebegin "Ripristino delle regole del firewall"
  $IPTABLESRESTORE < $FIREWALL_SAVE
  eend $?
}

restart() {
  svc_stop; svc_start
}

showoptions() {
  echo "Usage: $0 {start|save|restore|block_all|stop|restart|showstatus}"
  echo "start)      Avvia le regole impostate in ${FIREWALL}"
  echo "stop)       Cancella le regole e imposta tutto su ACCEPT"
  echo "restart)    Arresta e riavia il firewall"
  echo "save)       Salva le regole in ${FIREWALL_SAVE}"
  echo "restore)    Ripristina le regole da ${FIREWALL_SAVE}"
  echo "showstatus) Mostra lo status del firewall"
  echo "block_all)  Blocca tutti i pacchetti (accetta solo pacchetti da localhost)"
}


Siccome è casalingo e copiato-incollato potrebbe avere dentro degli errori di cui non mi sono accorto... a occhio mi sembra di no, ma suggerimenti sono bene accetti :wink:

Questo che segue è un esempio di regole personalizzate da aggiungere in /etc/firewall/muro_di_fuoco (ricordatevi di rendelo eseguibile con un chmod 700)
Se volete potete anche aggiungerlo al $PATH e chiamarlo da terminale come un qualsiasi programma. In questo caso la cosa migliore è aggiungere questo link
Code:
ln -s /etc/firewall/muro_di_fuoco /usr/local/sbin/muro_di_fuoco
in modo da non dover inserire il path completo ogni volta che viene lanciato.
Vengono definite diverse catene sia in entrata che in uscita (potete applicarle o meno a vostro piacimento... di default ne sono applicate ben poche) e vengono lasciati uscire anche i pacchetti INVALID (in modo da consentirvi di effettuare dei portscan :wink: ).
Vi consiglio di cambiare la porta di ssh e di spostarla da quella di default (22) verso una più alta (che so.... > 1024 e < 65535 sicuramente) che è molto più sicuro...
Per aggiungere le catene al firewall modificate o aggiungete le funzioni nella sezione #FUNZIONI. In alternativa potete direttamente modificare la parte finale dello script ma non ve lo consiglio
Se avete bisogno di aiuto per definire le regole contattatemi pure via pm (non ne abusate però! ;-) )

[EDIT]AGGIORNAMENTO 01/05/2007:
Ho Modificato leggermente questo script in modo da renderlo un po' più interattivo. Adesso accetta paramentri da riga di comando (se non specificati carica un set di regole di default). E' possibile creare, nell'apposita sezione, delle funzioni contenenti le chiamate alle catene opportune da abilitare; in questo modo si può ignorare quanto riportato nella parte finale dello script e leggere solamente le prime cento righe.
E' solo un aggiornamento estetico; le funzionalità rimangono inalterate.
[/EDIT]

Code:
#!/bin/sh

#Defininco la posizione degli eseguibili di iptables
IPTABLES=/sbin/iptables

#Interfacce
IFACE=eth1 #Interfaccia esterna
LFACE=eth0 #Interfaccia interna

#Nameservers
NAMESERVER_1=208.67.222.222
NAMESERVER_2=208.67.220.220

#Default gateway
GATEWAY=192.168.0.1

#Definizioni delle porte
SSH_PORT=22
P_PORTS=0:1023
UP_PORTS=1024:65535
ML_BITTORRENT=6882
BITTORRENT_PORTS=6881:6889
BITTORRENT_TRACKER_PORT=6969
MLDONKEY_PORT_tcp=4662
MLDONKEY_PORT_udp=4666
KAD_PORT=20299
OVERNET_PORT=14006
GNUTELLA_PORT=6346
GNUTELLA2_PORT=6347
FASTTRACK_PORTS=1214

AMULE_PORT=4672
ICQ_PORT=5190
JABBER_PORT=5222
GNUNET_PORT=2086
FREENET_PORT=19577
SKYPE_PORT=2424
EKIGA_PORTS=5000:5100

##########################################################################
#FUNZIONI
#Definisco i set di regole da caricare
##########################################################################
#Queste regole sono definite come funzioni da chiamare al momento di
#applicare le catene. Ogni funzione definisce le funzioni input, output e
#forward da applicare rispettivamente a INPUT, OUTPUT e FORWARD.
#A seconda dei parametri passati da riga di comando posso chiamare diversi
#set di regole. Se non specifico niente chiama il set di default.
#Ricordarsi sempre di aggiornare la lista dei parametri accettati.
#Le funzioni devono avere lo stesso nome dei parametri corrispondenti.
LISTA_PARAMETRI="default block server forward"

#Lista catene (solo per comodita'):
# bittorrent_in connessioni_avviate dns_query fasttrack_in ftp_in gnutella2_in
# http_out icmp_in icmp_out ml_bittorrent_in mldonkey_gui mldonkey_in portscan
# rsync_in samba_in smtp_in ssh_in ssh_out syn_flood telnet_in

default() {
  input() {
    $IPTABLES -A INPUT -j ssh_in
  }
  output() {
  sleep 0
  }
  forward() {
  sleep 0
  }
}

server() {
  input() {
    echo "Carico set di regole 'server'"
    $IPTABLES -A INPUT -j ssh_in
    $IPTABLES -A INPUT -j rsync_in
    $IPTABLES -A INPUT -j samba_in
    $IPTABLES -A INPUT -j mldonkey_in
    $IPTABLES -A INPUT -j mldonkey_gui
  }
  output() {
  sleep 0
  }
  forward() {
  sleep 0
  }
}

forward() {
  input() {
    $IPTABLES -A INPUT -j ssh_in
  }
  output() {
  sleep 0
  }
  forward() {
    echo "Forwarding dalla porta $LFACE a $GATEWAY"
    echo 1 > /proc/sys/net/ipv4/ip_forward
    iptables -t nat -A POSTROUTING -o $LFACE -j SNAT --to $GATEWAY
  }
}

#Definisco quanto specificato da parametro.
if [ ${#@} -gt 0 ]; then
  for i in $@; do
    if [ `echo $LISTA_PARAMETRI|grep -e $i|wc -l` -gt 0 ]; then
     [ $i != block ] && $i
    else
      echo "parametro '$i' sconosciuto"
      echo "Lista dei parametri accettati:"
      echo $LISTA_PARAMETRI
      exit 0
    fi
  done
else
  default
fi

##########################################################################
#Pulisco tutte le regole e ripristino quelle di default (ACCEPT)
##########################################################################
$IPTABLES -F
$IPTABLES -F -t mangle
$IPTABLES -F -t nat
$IPTABLES -X
$IPTABLES -X -t mangle
$IPTABLES -X -t nat
#Se ho specificato il parametro "block" blocca tutto il traffico in e out
BLOCK=`echo $@|grep block |wc -l`
if [ $BLOCK -gt 0 ]; then
  echo "blocking all network trafic 'in' and 'out'"
  $IPTABLES -P FORWARD DROP
  $IPTABLES -P INPUT   DROP
  $IPTABLES -P OUTPUT  DROP
  $IPTABLES -A INPUT -i lo -j ACCEPT
  $IPTABLES -A OUTPUT -o lo -j ACCEPT
  exit 0
fi
#Set Defaults to ACCEPT
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT

##########################################################################
#Definisco la policy di default
##########################################################################
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -P OUTPUT ACCEPT

##########################################################################
#Definisco le catene per i vari tipi di paccehtti
##########################################################################
#Catena per le connessioni già avviate (da mettere in fondo alle regole)
 $IPTABLES -N connessioni_avviate
 $IPTABLES -F connessioni_avviate
 $IPTABLES -A connessioni_avviate -m state --state ESTABLISHED,RELATED -j ACCEPT
 $IPTABLES -A connessioni_avviate -i $IFACE -m limit -j LOG --log-prefix "FW:Bad packet from ${IFACE}:"
 $IPTABLES -A connessioni_avviate -j DROP

#Catena per il traffico ICMP in entrata
#ICMP (in entrata) Solo se fanno parte di connessioni preesistenti, cioè si tratta di una risposta ad un
#pacchetto inviato dalla nostra rete
 $IPTABLES -N icmp_in
 $IPTABLES -F icmp_in
 $IPTABLES -A icmp_in -p icmp --icmp-type time-exceeded -j ACCEPT
 $IPTABLES -A icmp_in -p icmp --icmp-type destination-unreachable -j ACCEPT
#protezione dai PING-FLOOD
 $IPTABLES -A icmp_in -p icmp --icmp-type ping -m limit --limit 1/s -j ACCEPT
 $IPTABLES -A icmp_in -p icmp -j LOG --log-prefix "FW:Bad ICMP traffic:"

#Protezione dai SYN-FLOOD
# $IPTABLES -N syn_flood
# $IPTABLES -F syn_flood
# $IPTABLES -A syn_flood -p tcp --syn -m limit --limit 1/s --limit-burst 4 -j ACCEPT
# $IPTABLES -A syn_flood -p tcp --syn -j DROP

#Catena per il traffico ICMP in uscita
#ICMP (in uscita) Tutti gli icmp
 $IPTABLES -N icmp_out
 $IPTABLES -F icmp_out
 $IPTABLES -A icmp_out -o $IFACE -p icmp -j ACCEPT

#Catena per il DNS query
#DNS (client -> server) Vengono abilitate le query in uscita ai DNS servers
 $IPTABLES -N dns_query
 $IPTABLES -F dns_query
 $IPTABLES -A dns_query -o $IFACE -p udp -s $NAMESERVER_1 --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A dns_query -o $IFACE -p udp -s $NAMESERVER_2 --dport 53 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per SSH in entrata
#SSH (in entrata) In entrata abilitato il traffico per la porta $SSH_PORT
#Limtati i pacchetti RST FIN e SYN a 1/second
 $IPTABLES -N ssh_in
 $IPTABLES -F ssh_in
 $IPTABLES -A ssh_in -i $IFACE -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport $SSH_PORT -j ACCEPT
 $IPTABLES -A ssh_in -i $IFACE -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport $SSH_PORT -j ACCEPT
 $IPTABLES -A ssh_in -i $IFACE -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport $SSH_PORT -j ACCEPT
 $IPTABLES -A ssh_in -i $IFACE -p tcp --dport $SSH_PORT -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per SSH in uscita abilita le connessioni ssh alla porta $SSH_PORT e a quella di default (22)
 $IPTABLES -N ssh_out
 $IPTABLES -F ssh_out
 $IPTABLES -A ssh_out -o $IFACE -p tcp --dport $SSH_PORT -m state --state NEW,ESTABLISHED -j ACCEPT 
 $IPTABLES -A ssh_out -o $IFACE -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per il traffico HTTP e HTTPS in uscita
 $IPTABLES -N http_out
 $IPTABLES -F http_out
 $IPTABLES -A http_out -o $IFACE -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A http_out -o $IFACE -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per SAMBA in entrata
 $IPTABLES -N samba_in
 $IPTABLES -F samba_in
 $IPTABLES -A samba_in -i $IFACE -p udp --dport 137 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A samba_in -i $IFACE -p udp --dport 138 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A samba_in -i $IFACE -p tcp --dport 139 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A samba_in -i $IFACE -p tcp --dport 445 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per FTP in entrata
#FTP (in uscita)
 $IPTABLES -N ftp_in
 $IPTABLES -F ftp_in
 $IPTABLES -A ftp_in -i $IFACE -p tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per RSYNC in entrata
 $IPTABLES -N rsync_in
 $IPTABLES -F rsync_in
 $IPTABLES -A rsync_in -i $IFACE -p tcp --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT
 
#Catena per TELNET in entrata
 $IPTABLES -N telnet_in
 $IPTABLES -F telnet_in
 $IPTABLES -A telnet_in -i $IFACE -p tcp --dport 23 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per SMTP in entrata
 $IPTABLES -N smtp_in
 $IPTABLES -F smtp_in
 $IPTABLES -A smtp_in -i $IFACE -p tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per BITTORRENT in entrata
 $IPTABLES -N bittorrent_in
 $IPTABLES -F bittorrent_in
 $IPTABLES -A bittorrent_in -i $IFACE -p tcp --dport $BITTORRENT_PORTS -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per il bittorrent integrato in mldonkey in entrata:
 $IPTABLES -N ml_bittorrent_in
 $IPTABLES -F ml_bittorrent_in
 $IPTABLES -A ml_bittorrent_in -i $IFACE -p tcp --dport $ML_BITTORRENT -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per MLDONKEY in entrata
 $IPTABLES -N mldonkey_in
 $IPTABLES -F mldonkey_in
 $IPTABLES -A mldonkey_in -i $IFACE -p tcp --dport $MLDONKEY_PORT_tcp -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_in -i $IFACE -p udp --dport $MLDONKEY_PORT_udp -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_in -i $IFACE -p tcp --dport $KAD_PORT -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_in -i $IFACE -p udp --dport $KAD_PORT -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_in -i $IFACE -p tcp --dport $OVERNET_PORT -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_in -i $IFACE -p udp --dport $OVERNET_PORT -m state --state NEW,ESTABLISHED -j ACCEPT
 
#Catena per la GUI di MLDONKEY in entrata
 $IPTABLES -N mldonkey_gui
 $IPTABLES -F mldonkey_gui
 $IPTABLES -A mldonkey_gui -i $IFACE -p tcp --dport 4080 -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A mldonkey_gui -i $IFACE -p tcp --dport 4001 -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per GNUTELLA2 in entrata
 $IPTABLES -N gnutella2_in
 $IPTABLES -F gnutella2_in
 $IPTABLES -A gnutella2_in -i $IFACE -p tcp --dport $GNUTELLA2_PORT -m state --state NEW,ESTABLISHED -j ACCEPT
 $IPTABLES -A gnutella2_in -i $IFACE -p udp --dport $GNUTELLA2_PORT -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per FASTTRACK in entrata
 $IPTABLES -N fasttrack_in
 $IPTABLES -F fasttrack_in
 $IPTABLES -A fasttrack_in -i $IFACE -p tcp --dport $FASTTRACK_PORTS -m state --state NEW,ESTABLISHED -j ACCEPT

#Catena per bloccare ACROREAD in uscita
# $IPTABLES -N no_acroread
# $IPTABLES -F no_acroread
# $IPTABLES -A no_acroread -m owner --cmd-owner acroread -j DROP

##########################################################################
#Definisco la catene per intercettare i portscan
##########################################################################
#Catena per loggare i portscan
 $IPTABLES -N portscan
 $IPTABLES -F portscan
 
 #NMAP-XMAS
 $IPTABLES -A portscan -p tcp --tcp-flags ALL FIN,URG,PSH -m limit --limit 5/minute -j LOG --log-level alert --log-prefix "FW:SCAN:NMAP-XMAS:"
 $IPTABLES -A portscan -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
 
 #XMAS
 $IPTABLES -A portscan -p tcp --tcp-flags ALL ALL -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "FW:SCAN:XMAS:"
 $IPTABLES -A portscan -p tcp --tcp-flags ALL ALL -j DROP

 #XMAS-PSH
 $IPTABLES -A portscan -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "FW:SCAN:XMAS-PSH:"
 $IPTABLES -A portscan -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP

 #NULL_SCAN
 $IPTABLES -A portscan -p tcp --tcp-flags ALL NONE -m limit --limit 5/minute -j LOG --log-level 1 --log-prefix "FW:SCAN:NULL_SCAN:"
 $IPTABLES -A portscan -p tcp --tcp-flags ALL NONE -j DROP

 #SYN/RST
 $IPTABLES -A portscan -p tcp --tcp-flags SYN,RST SYN,RST -m limit --limit 5/minute -j LOG --log-level 5 --log-prefix "FW:SCAN:SYN/RST:"
 $IPTABLES -A portscan -p tcp --tcp-flags SYN,RST SYN,RST -j DROP

 #SYN/FIN
 $IPTABLES -A portscan -p tcp --tcp-flags SYN,FIN SYN,FIN -m limit --limit 5/minute -j LOG --log-level 5 --log-prefix "FW:SCAN:SYN/FIN:"
 $IPTABLES -A portscan -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP

##########################################################################
#Variabili SYS-CTL
##########################################################################
#Abilita o disabilita l'IP FORWARDING
#echo "1" > /proc/sys/net/ipv4/ip_forward
/bin/echo "0" > /proc/sys/net/ipv4/ip_forward

#DYNAMIC ADDRESSING (utile per il forwarding)
#/bin/echo "1" > /proc/sys/net/ipv4/ip_dynaddr

#Disabilita l'IP Spoofing
/bin/echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter

#Non rispondere ai PING
#/bin/echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all

#Non rispondere agli ICMP BROADCAST (attacchi smurf)
/bin/echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

#Disabilita l'accettazione dei REDIRECT
/bin/echo "0" > /proc/sys/net/ipv4/conf/all/accept_redirects
/bin/echo "0" > /proc/sys/net/ipv4/conf/all/secure_redirects

#Protezione verso i messaggi di errore ICMP malformati
/bin/echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses

#Disabilita i pacchetti source routed (previene dal guardare attraverso il NAT)
/bin/echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route

#Abilita LOG_MARTIANS (effettua il log dei pacchetti strani)
/bin/echo "1" > /proc/sys/net/ipv4/conf/all/log_martians

#Abilita il  REVERSE PATH FILTERING
for i in /proc/sys/net/ipv4/conf/*; do
  /bin/echo "1" > $i/rp_filter
done

##########################################################################
#Applico le catene a INPUT, OUTPUT e FORWARD
#Scarto i pacchetti INVALID
##########################################################################
#Applico le catene a INPUT
  $IPTABLES -A INPUT -j portscan
  $IPTABLES -A INPUT -m state --state INVALID -j DROP
  $IPTABLES -A INPUT -j icmp_in
  $IPTABLES -A INPUT -i lo -j ACCEPT
  input
  $IPTABLES -A INPUT -j connessioni_avviate

#Applico le catene a OUTPUT
#  $IPTABLES -A OUTPUT -m state --state INVALID -j DROP
#  $IPTABLES -A OUTPUT -j no_acroread
  output

#Applico le catene a FORWARD
  $IPTABLES -A FORWARD -m state --state INVALID -j DROP
  $IPTABLES -A FORWARD -o lo -j ACCEPT
  forward
  $IPTABLES -A FORWARD -j connessioni_avviate


Per poter loggare meglio gli eventi del vostro firewall (specialmente i portscan) potete impostare dei filtri in modo da catturare gli eventi del kernel relativi a queste stringhe:
Code:
FW:
FW:SCAN:

Se usate syslog-ng e avete in /etc/syslog-ng/syslog-ng.conf questa riga:
Code:
source kernsrc { file("/proc/kmsg"); };

allora aggiungete anche queste:
Code:
destination firewall { file("/var/log/firewall.log"); };
filter f_firewall { match ("FW:"); };
log { source(kernsrc); filter(f_firewall); destination(firewall); };

in modo da destinare tutti gli eventi del firewall nel file /var/log/firewall.log
Per trovare i portscan basta cercare con grep la stringa "SCAN"
Se avete definito diversamente la sorgente degli eventi del kernel (la prima riga) allora aggiorate le righe che vi ho posto per puntare alla giusta source (in realtà dovete cambiare solo l'ultima riga)
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne


Last edited by Cazzantonio on Tue May 01, 2007 5:40 pm; edited 34 times in total
Back to top
View user's profile Send private message
neryo
Veteran
Veteran


Joined: 09 Oct 2004
Posts: 1292
Location: Ferrara, Italy, Europe

PostPosted: Sun May 22, 2005 6:07 pm    Post subject: Reply with quote

ottimo.. :wink:
_________________
cache: a safe place for hiding or storing things..

D-link DWL-G650 AirPlus
Apache Php Mysql
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Sun May 22, 2005 6:09 pm    Post subject: Reply with quote

Aggiunto ai post utilissimi sezione Tips.
_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Sun May 22, 2005 6:15 pm    Post subject: Reply with quote

gutter wrote:
Aggiunto ai post utilissimi sezione Tips.

Esagerato... guarda che è davvero poco più di un copia&incolla :D
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Sun May 22, 2005 6:17 pm    Post subject: Reply with quote

Cazzantonio wrote:

Esagerato... guarda che è davvero poco più di un copia&incolla :D


Ma fatto con criterio ;)

Almeno così vediamo se riusciamo a diminuire i post della serie: "Consigli sul mio firewall" ;)
_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
X-Drum
Advocate
Advocate


Joined: 24 Aug 2003
Posts: 2517
Location: ('Modica','Trieste','Ferrara') Italy

PostPosted: Sun May 22, 2005 7:16 pm    Post subject: Reply with quote

gutter wrote:
Cazzantonio wrote:

Esagerato... guarda che è davvero poco più di un copia&incolla :D


Ma fatto con criterio ;)

Almeno così vediamo se riusciamo a diminuire i post della serie: "Consigli sul mio firewall" ;)


seeeeeeeeeei morirai di speranza asd :wink:
_________________
"...There are two sort of lies, lies and benchmarks..."
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 30822
Location: here

PostPosted: Sun May 22, 2005 7:26 pm    Post subject: Reply with quote

Grazie Cazzantonio per l'ottimo tip
_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Sun May 22, 2005 8:56 pm    Post subject: Reply with quote

giusto per completezza, cambierei muro_di_fuoco con porta_tagliafuoco... che sarebbe la traduzione piu' corretta per firewall*.

P.S.: si', lo so... sono un gran rompiballe ;-)
* le porte tagliafuoco, ovvero firewall, sono costruzioni utilizzate nei dispositivi antincendio, quando si costruiscono gl'edifici

Coda
Back to top
View user's profile Send private message
Xet
Apprentice
Apprentice


Joined: 02 May 2004
Posts: 170

PostPosted: Sun May 22, 2005 9:06 pm    Post subject: Reply with quote

:O spettacolo!!!

piccolo? ... come diceva Yoda il valore di qualunque cosa non deve essere valutata secondo le dimensioni...

io di solito risolvo mettendo in local la chiamata allo script bash...ma questa soluzione è molto più elegante...
e sopratutto comforme agli "standard" di configrazione e gestione dei serviz...AEHM...DEMONI di Gentoo

veramente GJ e grazie
_________________
sono colui che sostiene il reale
Back to top
View user's profile Send private message
Xet
Apprentice
Apprentice


Joined: 02 May 2004
Posts: 170

PostPosted: Sun May 22, 2005 9:11 pm    Post subject: Reply with quote

posso essere puntiglioso anche io?
Code:

...
IPTABLESRESTORE=/sbin/iptables-restore
ECHO=/bin/echo
...


non penso che tanti abbiano echo in directory diverse da /bin
è solo per puntigliosità ed eleganza di programmazione...
_________________
sono colui che sostiene il reale
Back to top
View user's profile Send private message
gutter
Bodhisattva
Bodhisattva


Joined: 13 Mar 2004
Posts: 7162
Location: Aarau, Aargau, Switzerland

PostPosted: Sun May 22, 2005 10:40 pm    Post subject: Reply with quote

Dal momento che si tratta di uno script bash credo che prima si faccia riferimento ai built-in ;) e quindi è poco probabile che la tua costante sia utile ;)
_________________
Registered as User #281564 and Machines #163761
Back to top
View user's profile Send private message
gionag
n00b
n00b


Joined: 01 Mar 2004
Posts: 37

PostPosted: Sun May 22, 2005 10:50 pm    Post subject: Reply with quote

Code:

 * OUTPUT su ACCEPT, INPUT e FORWARD su DROP
iptables v1.2.11: invalid TCP port/service `-m' specified
Try `iptables -h' or 'iptables --help' for more information.
iptables v1.2.11: invalid TCP port/service `-m' specified
Try `iptables -h' or 'iptables --help' for more information.
 * Impostate regole per intercettare i portscan


come mai ? questo quando lancio il demone.

ottimo lavoro ragazzo :D
_________________
etc-update, da fare sobri, non oltre le 22 e senza pressioni esterne. .:deadhead:.
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Mon May 23, 2005 9:17 am    Post subject: Reply with quote

gionag wrote:
come mai ? questo quando lancio il demone.

Potrebbe essere un errore da qualche parte visto che nel postarlo ho modificato un paio di voci (per adattarlo ad una configurazione più generica...)
Tuttavia lo script sopra postato a me non da errore...
Code:
star_platinum root # /etc/init.d/pippo start
 * Re-caching dependency info (mtimes differ)...
 * Caricamento regole del firewall...                                             [ ok ]
star_platinum root # /etc/init.d/pippo stop
 * Arresto del firewall (tutte le regole su ACCEPT)...                            [ ok ]

Potrebbe essere che ti manca qualche modulo di iptables nella conf dl kernel?
Quote:
ottimo lavoro ragazzo :D

Grazie :D
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Mon May 23, 2005 9:29 am    Post subject: Reply with quote

Visto che stamattina ho più tempo (e che mi pare vi sia piaciuto lo script) vi posto un esempio di regole personalizzate (in realtà sono le mie regole :wink: ) da mettere in /etc/conf.d/muro_di_fuoco (liberissimi di chiamarlo porta_tagliafuoco :lol: .... basta modificare le voci all'inizio del primo script :wink: )
Cambiatele a vostro piacimento :)

Code:
EDIT
Aggiunto in coda al primo post

_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne


Last edited by Cazzantonio on Mon Dec 26, 2005 12:27 am; edited 7 times in total
Back to top
View user's profile Send private message
codadilupo
Advocate
Advocate


Joined: 05 Aug 2003
Posts: 3135

PostPosted: Mon May 23, 2005 9:58 am    Post subject: Reply with quote

Cazzantonio wrote:
Code:
#######protezione dai PING-FLOOD... da non confondersi con i pink-floyd
 $IPTABLES -A icmp_in -p icmp --icmp-type ping -m limit --limit 1/s -j ACCEPT


ma LOL :lol: :lol: :lol:

Coda
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Mon May 23, 2005 10:00 am    Post subject: Reply with quote

codadilupo wrote:

ma LOL :lol: :lol: :lol:

Si :wink: ma non è lì evidenziata per fare la battuta idiota... è perché ancora non so se tenerla o eliminarla :wink:
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
neryo
Veteran
Veteran


Joined: 09 Oct 2004
Posts: 1292
Location: Ferrara, Italy, Europe

PostPosted: Tue May 24, 2005 9:49 pm    Post subject: Reply with quote

Io aggiungerei anche la possibilita' di loggarsi da ssh sulla interfaccia interna alla rete..

Code:
$IPTABLES -A ssh_in -i $EFACE -p tcp --dport $SSH_PORT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT

_________________
cache: a safe place for hiding or storing things..

D-link DWL-G650 AirPlus
Apache Php Mysql
Back to top
View user's profile Send private message
GhePeU
Guru
Guru


Joined: 12 Aug 2003
Posts: 549
Location: Mestre, Italy

PostPosted: Tue May 24, 2005 10:00 pm    Post subject: Reply with quote

perchè uno script a parte?

bastava lanciare uno script bash con le sole impostazioni di iptables, quindi /etc/init.d/iptables save e rc-update add iptables boot
_________________
That is not dead which can eternal lie,
and with strange aeons even death may die.
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Wed May 25, 2005 9:25 am    Post subject: Reply with quote

neryo wrote:
Io aggiungerei anche la possibilita' di loggarsi da ssh sulla interfaccia interna alla rete..

Più che giusto :wink:
Non ci avevo pensato perché sono dietro uno switch quindi in realtà la $EFACE non la uso :)
Thanks :D

@GhePeU
Uno script di init ti permette anche di dargli dei comandi, tipo star, stop, restart, block_all, save, restore, showstatus .... è più interattivo... :) Inoltre stampa quei simpatici asterischi verdi e quegli "[OK]" che ci piacciono tanto :D
In ogni caso è questione di gusti :wink:
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
neryo
Veteran
Veteran


Joined: 09 Oct 2004
Posts: 1292
Location: Ferrara, Italy, Europe

PostPosted: Wed May 25, 2005 9:46 am    Post subject: Reply with quote

Cazzantonio wrote:

Più che giusto :wink:
Non ci avevo pensato perché sono dietro uno switch quindi in realtà la $EFACE non la uso :)


Ora che ci penso forse e' un settaggio troppo personale che solitamente non fa parte della necessita' della maggior parte degli utenti.. io ho un serverino di front-end e quindi mi serve!! :wink:
Fai tu se tenerla o meno.. magari mettigli qualche commento in modo che per i meno esperti capiscano che non serve se sono collegati a uno switch sulla etho e non hanno una eth1.. :wink:

Ciao
_________________
cache: a safe place for hiding or storing things..

D-link DWL-G650 AirPlus
Apache Php Mysql
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Wed May 25, 2005 10:33 am    Post subject: Reply with quote

neryo wrote:
Fai tu se tenerla o meno.. magari mettigli qualche commento in modo che per i meno esperti capiscano che non serve se sono collegati a uno switch sulla etho e non hanno una eth1.. :wink:


In generale lo script deve fornire solo delle regole generiche... per chi vuole regole ad-hoc c'è /etc/conf.d/muro_di_fuoco :wink:
In teroria quelle regole ci sono per le persone che non conoscono iptables ma che comunque vorrebbero mettere un firewall minimo sul loro pc
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
hellraiser
Guru
Guru


Joined: 14 Jun 2003
Posts: 431
Location: Pescara [Italy]

PostPosted: Wed May 25, 2005 1:05 pm    Post subject: Reply with quote

Penso c sia un piccolo errore...

Code:
#Disabilita l'IP Spoofing
/bin/echo "2" > /proc/sys/net/ipv4/conf/all/rp_filter


da sostituire con

Code:
#Disabilita l'IP Spoofing
/bin/echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter


Comunque ottimo lavoro...
_________________
Io non sono nessuno...ma nessuno è come me!

"Open Source is a good idea..."
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Wed May 25, 2005 1:09 pm    Post subject: Reply with quote

@hellraiser
Corretto! Del resto lo dicevo che c'erano sicuramente degli errori :roll: :D
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
iridium103
Tux's lil' helper
Tux's lil' helper


Joined: 08 Mar 2004
Posts: 104
Location: Treviso

PostPosted: Mon Jun 27, 2005 3:31 pm    Post subject: Reply with quote

ragazzi, scusate se faccio ri-emergere questo topic... (aprirne un'altro mi sembrava troppo brutto :P )
ma avrei un paio di dubbi.. riguardo alla mia configurazione di rete..

visto che ho due intefacce virtuali .. come posso impostare il firewall per filtrare anche loro?

vi posto la mia configurazione di rete

Code:

eth0      Link encap:Ethernet  HWaddr 00:0C:76:A0:9F:9B 
          inet addr:193.109.45.xxx  Bcast:193.109.45.223  Mask:255.255.255.0
          inet6 addr: fe80::20c:76ff:fea0:9f9b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6176395 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5315145 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1158118753 (1104.4 Mb)  TX bytes:1652664772 (1576.1 Mb)
          Interrupt:21

eth0:1    Link encap:Ethernet  HWaddr 00:0C:76:A0:9F:9B 
          inet addr:193.109.45.xxx  Bcast:193.109.45.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:21

eth0:2    Link encap:Ethernet  HWaddr 00:0C:76:A0:9F:9B 
          inet addr:193.109.45.xxx  Bcast:193.109.45.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
          Interrupt:21

eth1      Link encap:Ethernet  HWaddr 00:E0:4C:39:0B:46 
          inet addr:192.168.0.250  Bcast:192.169.0.255  Mask:255.255.255.0
          inet6 addr: fe80::2e0:4cff:fe39:b46/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1531528 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1101109 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:670031802 (638.9 Mb)  TX bytes:1031268363 (983.4 Mb)
          Interrupt:18 Base address:0x6f00

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8021647 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8021647 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2250184343 (2145.9 Mb)  TX bytes:2250184343 (2145.9 Mb)




qualcuno saprebbe mica dove/come indirizzarmi? ho proprio bisogno di una manina :)

grazie in anticipo!! :)
_________________
Imagination is more important than knowledge.
Albert Einstein
Back to top
View user's profile Send private message
Cazzantonio
Bodhisattva
Bodhisattva


Joined: 20 Mar 2004
Posts: 4514
Location: Somewere around the world

PostPosted: Mon Jun 27, 2005 4:26 pm    Post subject: Reply with quote

che cosa ci passa da quelle interfacce?
Il modo corretto è scrivere regole per ogni interfaccia... (penso... non ho mai messo un firewall su più di un interfaccia esterna....), altrimenti se non specifichi l'interfaccia da filtrare iptables te le filtra tutte (nel caso dello script sopra devi settare IFACE="")
_________________
Any mans death diminishes me, because I am involved in Mankinde; and therefore never send to know for whom the bell tolls; It tolls for thee.
-John Donne
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Risorse italiane (documentazione e tools) All times are GMT
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum