@ :~/blog/intrusion-detection-system-setup-arnos-iptables-firewall-port-scan-attack-detector-firewall-snort$
← cd ~/blog

less intrusion-detection-system-setup-arnos-iptables-firewall-port-scan-attack-detector-firewall-snort.md

Intrusion Detection System: Setup Arno’s Iptables Firewall, Port Scan Attack Detector, Firewall Snort

#Bash#Debian & Sysadmin#Server archivio

# archivio storico — scritto anni fa, conservato com'era. Alcune info sono datate.

I sistemi host di rilevamento delle intrusioni (HIDS – Host Intrusion Detection System) e i sistemi di rilevamento delle intrusioni di rete (NIDS – Network Intrusion Detection System) sono, rispettivamente, i metodi di gestione della sicurezza per i computer e le reti. In un HIDS, applicazioni anti-minacce come firewall, software antivirus e programmi spyware di rilevamento sono installati su tutti i computer della rete che ha accesso bidirezionale verso l’ambiente esterno come ad esempio Internet. In un NIDS, il software anti-minaccia è installato solo in punti specifici, come i server che interfacciano l’ambiente esterno e il segmento di rete da proteggere.

Tutti i metodi di rilevamento delle intrusioni (ID) prevedono la raccolta e l’analisi delle informazioni provenienti da varie aree all’interno di un computer o di una rete per individuare eventuali minacce da parte di hacker e/o cracker all’interno o all’esterno dell’organizzazione. i sistema Host-based e i Network-based hanno i loro rispettivi vantaggi e limiti. La protezione più efficace per una rete proprietaria è fornita da una combinazione di entrambe le tecnologie.

Focalizziamo l’attenzione su un sistema di Host Intrusion Detection System per proteggere un nostro server remoto. Utilizzeremo come da titolo alcuni software di analisi delle informazioni; vale a dire:

  • Arno’s Iptables Firewall: è un semplice Firewall. Dato un file di configurazione scriverà per voi le regole da passare a Iptables;
  • psad – Post Scan Attack Detector: analizza i file di log prodotti da Iptables e se dovesse rilevare un tentativo di intrusione bloccherebbe la minaccia al nostro server da parte dell’IP;
  • fwsnort – Firewall Snort: analizza le regole del sistema di rilevamento delle intrusioni SNORT® e costruisce un set di regole equivalenti per Iptables.

psad e fwsnort agiranno più come Intrusion Prevention System (IPS), che come Intrusion Detection System (IDS). La differenza è che un IPS al rilevamento di una minaccia agisce per bloccare e impedire l’accesso, mentre in un IDS ci sarà solo la segnalazione all’amministratore del tentativo di minaccia rilevato. Potete immaginare da soli che nel primo caso si automatizza un processo e che nel secondo caso senza un manuale intervento nulla può accadere.

DisclaimerVi ricordo di fare attenzione alle vostre scelte: stiamo supponendo che voi conosciate il vostro sistema. Se il firewall o gli altri software dovessero essere riavviati con configurazioni scorrette potreste incorrere nel rischio di non riuscire più ad accedere al vostro server remoto. Prima provate a casa!!!

Arno’s Iptables Firewall

Partiamo con il nostro semplice firewall:

apt-get install arno-iptables-firewall

Al termine partirà la fase di configurazione (fate attenzione alle vostre scelte!):

[rl_gallery id=”2016″]

Le regole basi del firewall sono buone: ma consiglio di editare il /etc/arno-iptables-firewall/firewall.conf modificando le seguente due variabili:

...
ECHO_IGNORE=1
...
IP_FORWARDING=0

PSAD

Installazione del software

apt-get install psad

Una volta installato il software modificate il file /etc/psad/psad.conf in base alle vostre esigenze. Vi lascio alcune modifiche che ho apportato al mio file:

--- psad.conf.orig	2012-11-19 22:56:28.000000000 +0100
+++ psad.conf	2015-07-11 12:03:52.863623493 +0200
@@ -16,16 +16,16 @@
 
 ### Supports multiple email addresses (as a comma separated
 ### list).
-EMAIL_ADDRESSES             root@localhost;
+EMAIL_ADDRESSES             [email protected];
 
 ### Machine hostname
-HOSTNAME                    _CHANGEME_;
+HOSTNAME                    server.youdomain.cxm;
 
 ### Specify the home and external networks.  Note that by default the
 ### ENABLE_INTF_LOCAL_NETS is enabled, so psad automatically detects
 ### all of the directly connected subnets and uses this information as
 ### the HOME_NET variable.
-HOME_NET                    any;
+HOME_NET                    NOT_USED;
 EXTERNAL_NET                any;
 
 ### The FW_SEARCH_ALL variable controls how psad will parse iptables
@@ -74,7 +74,7 @@
 ### auto_ips so a danger level is automatically
 ### assigned.
 DANGER_LEVEL1               5;    ### Number of packets.
-DANGER_LEVEL2               15;
+DANGER_LEVEL2               35;
 DANGER_LEVEL3               150;
 DANGER_LEVEL4               1500;
 DANGER_LEVEL5               10000;
@@ -141,7 +141,7 @@
 ### usually recommended.
 ENABLE_SYSLOG_FILE          Y;
 IPT_WRITE_FWDATA            Y;
-IPT_SYSLOG_FILE             /var/log/messages;
+IPT_SYSLOG_FILE             /var/log/arno-iptables-firewall;
 
 ### When enabled, this instructs psad to write the "msg" field
 ### associated with Snort rule matches to syslog.
@@ -175,7 +175,7 @@
 ### corresponding protocol,  For example, to have psad ignore all
 ### tcp in the range 61000-61356 and udp ports 53 and 5000, use:
 ### IGNORE_PORTS        tcp/61000-61356, udp/53, udp/5000;
-IGNORE_PORTS                NONE;
+IGNORE_PORTS                udp/53;
 
 ### allow entire protocols to be ignored.  This keyword can accept
 ### a comma separated list of protocols.  Each protocol must match
@@ -198,10 +198,10 @@
 ### IP directory is created within /var/log/psad/.  Hence
 ### MIN_DANGER_LEVEL should be set less than or equal to the value
 ### assigned to the EMAIL_ALERT_DANGER_LEVEL variable.
-MIN_DANGER_LEVEL            1;
+MIN_DANGER_LEVEL            2;
 
 ### Only send email alert if danger level >= to this value.
-EMAIL_ALERT_DANGER_LEVEL    1;
+EMAIL_ALERT_DANGER_LEVEL    3;
 
 ### Enable detection of malicious activity that is delivered via IPv6.  If
 ### ip6tables is not logging any traffic, then psad won't know anything
@@ -233,11 +233,11 @@
 
 ### If "Y", send a status email message when an IP has reached the
 ### EMAIL_LIMIT threshold.
-EMAIL_LIMIT_STATUS_MSG      Y;
+EMAIL_LIMIT_STATUS_MSG      N;
 
 ### If "Y", send email for all newly logged packets from the same
 ### source ip instead of just when a danger level increases.
-ALERT_ALL                   Y;
+ALERT_ALL                   N;
 
 ### If "Y", then psad will import old scan source ip directories
 ### as current scans instead of moving the directories into the
@@ -322,11 +322,11 @@
 
 ### If "Y", enable automated IDS response (auto manages
 ### firewall rulesets).
-ENABLE_AUTO_IDS             N;
+ENABLE_AUTO_IDS             Y;
 
 ### Block all traffic from offending IP if danger
 ### level >= to this value
-AUTO_IDS_DANGER_LEVEL       5;
+AUTO_IDS_DANGER_LEVEL       2;
 
 ### Set the auto-blocked timeout in seconds (the default
 ### is one hour).
@@ -345,7 +345,7 @@
 
 ### By setting this variable to N, all auto-blocking emails can be
 ### suppressed.
-ENABLE_AUTO_IDS_EMAILS      Y;
+ENABLE_AUTO_IDS_EMAILS      N;
 
 ### Enable iptables blocking (only gets enabled if
 ### ENABLE_AUTO_IDS is also set)

Una volta fatto non riavviate ancora nulla… aspettate lo faremo in seguito!

FWSNORT

Procediamo all’installazione. La configurazione base è già buona e non occorreranno modifiche particolari.

apt-get install fwsnort

Ora ci servirà un piccolo script necessario ad aggiornare FWSNORT & le PSAD signatures una volta a settimana. Create il file /usr/local/bin/update-fwsnort

#!/bin/bash
# update fwsnort & psad signatures
/usr/sbin/fwsnort --update-rules
/usr/sbin/fwsnort
/var/lib/fwsnort/fwsnort.sh
echo "fwsnort signatures updated"
/usr/sbin/psad --sig-update
/usr/sbin/psad -H
echo "psad signatures updated"

Rendiamolo eseguibile con un chmod a+x /usr/local/bin/update-fwsnort

Ora potete inserire un copia del file nella cartella /etc/cron.weekly/ oppure creare una regola attraverso crontab -e

0 15 * * 5 /usr/local/bin/update-fwsnort 2>&1 >> /var/log/fwsnort_update.log

Perché le regole di FWSNORT vengano inserite in iptables ad ogni riavvio create il seguente file /etc/init.d/fwsnort

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          fwsnort
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Load/unload snort signatures for psad
# Description:       update-rc.d -f fwsnort defaults 
### END INIT INFO
/var/lib/fwsnort/fwsnort.sh
exit 0

eseguite questi due comandi

chmod a+x /etc/init.d/fwsnort
update-rc.d -f fwsnort defaults

Manca ancora qualcosina: facciamo in modo che il nostro firewall (armo’s iptable firewall) crei i Chain necessari a psad per funzionare correttamente:

editiamo il file /etc/arno-iptables-firewall/custom-rules e inserite le seguenti regole:

# Put any custom (iptables) rules here down below:
##################################################

#create PSAD chains
iptables -N PSAD_BLOCK_INPUT
iptables -N PSAD_BLOCK_OUTPUT
iptables -N PSAD_BLOCK_FORWARD
iptables -A INPUT -j PSAD_BLOCK_INPUT
iptables -A OUTPUT -j PSAD_BLOCK_OUTPUT
iptables -A FORWARD -j PSAD_BLOCK_FORWARD

ip6tables -A INPUT -j LOG
ip6tables -A FORWARD -j LOG
#create PSAD chains IPV6
ip6tables -N PSAD_BLOCK_INPUT
ip6tables -N PSAD_BLOCK_OUTPUT
ip6tables -N PSAD_BLOCK_FORWARD
ip6tables -A INPUT -j PSAD_BLOCK_INPUT
ip6tables -A OUTPUT -j PSAD_BLOCK_OUTPUT
ip6tables -A FORWARD -j PSAD_BLOCK_FORWARD

Ora è arrivato il momento di riavviare i nostri servizi!

service psad stop
service arno-iptables-firewall stop

verificate che iptables sia vuoto:

iptables -n -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Avviamo tutto!

service arno-iptables-firewall start
service psad start
service fwsnort start

Ora provate a controllare nuovamente iptables ( iptables -n -L ) vi sorprenderete di quante regole troverete! Se vi occorre ulteriore documentazione seguite i link dei relativi software. Se vi occorre qualche spiegazione non avete che da chiedere.

A presto!!!