Archivi tag: auditing

CentOS 6: monitorare le regole di auditing in modo proattivo mediante Nagios, NRDP e swatch

In questo post ed in quest’altro ho illustrato, rispettivamente, come configurare Nagios, NRDP e swatch per la ricezione dei check passivi e dei security alert.

Adesso vedremo come fare a monitorare in modo proattivo le regole di auditing definite in precedenza mediante il tool auditd (vedi qui per ulteriori dettagli).

Nagios_logo_blackIn soldoni, la configurazione si avvale di due passaggi:

1) creazione di un file da dare in pasto a swatch, nel quale sono definite le espressioni regolari in grado di identificare in modo univoco ciascun evento di auditing;

2) definizione dei servizi di Nagios per la ricezione dei check passivi.

Ecco uno stralcio della configurazione di swatch:

#time_changes auditing rule
watchfor /time_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='time_changes auditing rule' --output\='$_ | time_changes\=1'"
#system_locale_changes auditing rule
watchfor /system_locale_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='system_locale_changes auditing rule' --output\='$_ | system_locale_changes\=1'"
#shadow_changes auditing rule
watchfor /shadow_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='shadow-file auditing rule' --output\='$_ | shadow_changes\=1'"
#passwd_changes auditing rule
watchfor /passwd_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='passwd_changes auditing rules' --output\='$_ | passwd_changes\=1'"
#group_changes auditing rule
watchfor /group_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='group_changes auditing rule' --output\='$_ | group_changes\=1'"
#sudoers_changes auditing rule
watchfor /sudoers_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='sudoers_changes auditing rule' --output\='$_ | sudoers_changes\=1'"
#selinux_changes auditing rule
watchfor /selinux_changes/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='selinux_changes auditing rule' --output\='$_ | selinux_changes\=1'"
#module_insertion auditing rule
watchfor /module_insertion/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='module_insertion auditing rule' --output\='$_ | module_insertion\=1'"
#webserver_watch_tmp auditing rule
watchfor /webserver_watch_tmp/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='webserver_watch_tmp auditing rules' --output\='$_ | webserver_watch_tmp\=1'"
#sshd_config auditing rule
watchfor /sshd_config/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='sshd_config auditing rules' --output\='$_ | sshd_config\=1'"
#httpd_config auditing rule
watchfor /httpd_config/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='httpd_config auditing rules' --output\='$_ | httpd_config\=1'"
#ntp_config auditing rule
watchfor /ntp_config/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='ntp_config auditing rules' --output\='$_ | ntp_config\=1'"
#iptables_config auditing rule
watchfor /iptables_config/
     echo
     exec "/usr/bin/php /usr/lib64/nagios/plugins/send_nrdp.php --url\=http://192.168.1.1/nrdp --token\=s3cr3t --host\=localhost --state\=1 --service\='iptables_config auditing rules' --output\='$_ | iptables_config\=1'"

Il comando da lanciare per rendere operativo il suddetto applicativo (che magari potremo inserire anche all’interno del file /etc/rc.local per l’avvio automatico dopo ogni riavvio della macchina) è il seguente:

swatch -c /etc/swatchaudit.conf -t /var/log/audit/audit.log --daemon

Di seguito, invece, riporto la configurazione dell’host di Nagios (localhost) per il quale occorre monitorare gli eventi di auditing:

define service{
        use                             local-service
        host_name                       localhost
        service_description             time_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             system_locale_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             shadow_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             group_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             sudoers_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             selinux_changes auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             module_insertion auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             webserver_watch_tmp auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             sshd_config auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             httpd_config auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             ntp_config auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

define service{
        use                             local-service
        host_name                       localhost
        service_description             iptables_config auditing rule
        check_command                   check_passive
        passive_checks_enabled          1
        active_checks_enabled           0
        max_check_attempts              1
        is_volatile                     1
        check_freshness                 1
        freshness_threshold             6
        flap_detection_enabled          0
        }

Come al solito, lanciamo un reload di Nagios per rendere operative le suddette modifiche:

[root@linuxbox ~]# service nagios reload

ed abbiamo finito.

Alla prossima.

CentOS 6: configurare delle regole di auditing mediante auditd

In questo post ho accennato all’importanza che le operazioni di auditing ricoprono durante la messa in sicurezza dei nostri server.

Nella fattispecie, per la distro CentOS 6, l’applicativo che si occupa di “monitorare” (inteso come controllo degli accessi ed accounting) il sistema operativo, prende il nome di auditd.

Vedremo adesso come fare a configurare delle regole di auditing secondo le nostre esigenze, come ricercare determinati eventi negli appositi file di log e come creare dei report giornalieri (da inviare via email al nostro indirizzo di posta) contenenti l’insieme degli eventi di auditing individuati.

audit

Creazione delle regole

Per effettuare tale operazione è necessario utilizzare il tool auditctl. Esso è in grado di creare delle regole il cui scopo è quello di monitorare le operazioni (lettura, scrittura, esecuzione e modifica degli attributi) su un dato file o su di una specifica directory. Non solo: sarà possibile anche configurare dei meccanismi di monitoraggio delle chiamate di sistema (in gergo SYSCALL) che vengono eseguite durante il normale funzionamento della nostra macchina.

Ecco alcuni esempi di regole per il monitoraggio delle SYSCALL:

[root@linuxbox ~]# auditctl -a always,exit -F arch=b64 -S settimeofday -k time_changes
[root@linuxbox ~]# auditctl -a always,exit -F arch=b64 -S clock_settime -k time_changes
[root@linuxbox ~]# auditctl -a always,exit -F arch=b64 -S sethostname -S setdomainname -k system_locale_changes

In particolare, vengono monitorate le chiamate di sistema settimeofday, clock_settime, sethostname e setdomainname (specificate mediante la flag -S), le quali indicano palesemente delle modifiche macroscopiche relative alla configurazione del sistema operativo. Occorre precisare, inoltre, che è stato necessario indicare (mediante la flag -F) l’architettura della CPU (recuperabile mediante il comando uname -m), poichè i codici numerici che identificano ciascuna SYSCALL variano a seconda che si tratti di una x86 (32 bit) oppure di una x86_64 (64 bit). Qui trovate la lista delle chiamate di sistema per x86 e qui trovate quella per x86_64.

Per il monitoraggio di file e directory l’opzione da utilizzare è -w, seguita dal nome del file (o della directory) di interesse. Inoltre, mediante la flag -p, potranno essere specificate le operazioni di cui tenere traccia (a per modifica degli attributi, x per esecuzione, w per scrittura ed r per lettura). Infine, mediante la flag -k sarà possibile associare un’etichetta a ciascuna regola di auditing precedentemente definita. Eccco alcuni esempi:

[root@linuxbox ~]# auditctl -w /etc/shadow -p wa -k shadow-file
[root@linuxbox ~]# auditctl -w /etc/passwd -p wa -k passwd_changes
[root@linuxbox ~]# auditctl -w /etc/group -p wa -k group_changes
[root@linuxbox ~]# auditctl -w /etc/sudoers -p wa -k sudoers_changes
[root@linuxbox ~]# auditctl -w /etc/selinux/ -p wa -k selinux_changes
[root@linuxbox ~]# auditctl -w /sbin/insmod -p x -k module_insertion
[root@linuxbox ~]# auditctl -w /tmp -p x -k webserver_watch_tmp
[root@linuxbox ~]# auditctl -w /etc/ssh/sshd_config -p wa -k sshd_config
[root@linuxbox ~]# auditctl -w /etc/httpd/conf/httpd.conf -p wa -k httpd_config
[root@linuxbox ~]# auditctl -w /etc/ntp.conf -p wa -k ntp_config

Possiamo, inoltre, elencare le regole finora definite utilizzando il comando:

[root@linuxbox ~]# auditctl -l

il cui output sarà simile al seguente:

-a always,exit -F arch=x86_64 -S settimeofday -F key=time_changes
-a always,exit -F arch=x86_64 -S clock_settime -F key=time_changes
-a always,exit -F arch=x86_64 -S sethostname,setdomainname -F key=system_locale_changes
-w /etc/shadow -p wa -k shadow-file
-w /etc/passwd -p wa -k passwd_changes
-w /etc/group -p wa -k group_changes
-w /etc/sudoers -p wa -k sudoers_changes
-w /etc/selinux/ -p wa -k selinux_changes
-w /sbin/insmod -p x -k module_insertion
-w /tmp/ -p x -k webserver_watch_tmp
-w /etc/ssh/sshd_config -p wa -k sshd_config
-w /etc/httpd/conf/httpd.conf -p wa -k httpd_config
-w /etc/ntp.conf -p wa -k ntp_config
-w /etc/sysconfig/iptables -p wa -k iptables_config

Ricerca degli eventi

Il file di log in cui vengono salvati gli eventi di auditing è /var/log/audit/audit.log. Attraverso l’applicativo ausearch, che punta al suddetto file, è possibile effettuare delle ricerche mirate specificando il file di interesse, ad esempio:

[root@linuxbox ~]# ausearch -f /etc/passwd

oppure l’etichetta precedentemente assegnata all’evento:

[root@linuxbox ~]# ausearch -k webserver_watch_tmp

Generazione dei report

E’ possibile generare dei report contenenti gli eventi di auditing di interesse. Per fare ciò è sufficiente utilizzare l’applicativo aureport, comprensivo di flag -k, -ts (time start) e -te (time end). Ad esempio:

aureport -k -ts yesterday 00:00:00 -te yesterday 23:59:59

Per schedulare il suddetto report ad intervalli di tempo regolari (ad esempio ogni giorno) e fare in modo di riceverlo sulla propria casella di posta elettronica, è necessario creare un apposito file di cronjob all’interno della directory /etc/cron.d:

[root@linuxbox cron.d]# nano ausearchreport

il cui contenuto dovrà essere:

MAILTO=vostro.indirizzo@email.it

00    00   * * *     root          /sbin/aureport -k -ts yesterday 00:00:00 -te yesterday 23:59:59

Da notare che tutte le regole precedentemente create mediante auditctl hanno durata temporanea (ovvero vengono eliminate dopo un reboot). Per renderle quindi permanenti, occorre inserirle all’interno del file /etc/audit/audit.rules, il cui contenuto dovrà essere simile al seguente:

# First rule - delete all
-D

# Increase the buffers to survive stress events.
# Make this bigger for busy systems
-b 320

# Feel free to add below this line. See auditctl man page

-a always,exit -F arch=b64 -S settimeofday -k time_changes
-a always,exit -F arch=b64 -S clock_settime -k time_changes
-a always,exit -F arch=b64 -S sethostname -S setdomainname -k system_locale_changes
-w /etc/shadow -p wa -k shadow-file
-w /etc/passwd -p wa -k passwd_changes
-w /etc/group -p wa -k group_changes
-w /etc/sudoers -p wa -k sudoers_changes
-w /etc/selinux/ -p wa -k selinux_changes
-w /sbin/insmod -p x -k module_insertion
-w /tmp -p x -k webserver_watch_tmp
-w /etc/ssh/sshd_config -p wa -k sshd_config
-w /etc/httpd/conf/httpd.conf -p wa -k httpd_config
-w /etc/ntp.conf -p wa -k ntp_config
-w /etc/sysconfig/iptables -p wa -k iptables_config

# Disable adding any additional rules - note that adding new rules will require a reboot
-e 2

Come si può facilmente notare, in coda alla lista ho aggiunto l’opzione -e 2 che impedisce l’aggiunta di nuove regole on-the-fly (per questioni di sicurezza).

E’ sufficiente quindi restartare il demone auditd per rendere operative le suddette regole:

[root@linuxbox ~]# service auditd restart

Per ora è tutto. Nei prossimi post vedremo come monitorare in modo proattivo le regole di auditing mediante swatch, NRDP e Nagios.

Alla prossima.