Server Jessie Apache2 2.4 Hardening (libapache2-mod-security2)

Proprio la settimana scorsa stavo pistolando con il server, il caldo era alle porte così prima di fare la sauna mi sono lanciato nell’aggiornamento verso la nuova stable , il grande passo quando non si possono interrompere dei servizi per molto tempo.

Wheezy ---> Jessie

Sulla macchina ho un Raid1 così prima di lanciarmi nell’avventura ho pensato di rimuovere un disco e tenerlo come backup,in caso di problemi lo rimontavo e tutto tornava come prima,  poi con un disco pulito che avevo pronto, ho risistemato con mdadm il Raid1.

Dopo aver fatto rullare per bene la macchina con apt-get, tra le cose su cui ho dovuto più lavorare, c’è stato apache2 che passando dalla versione 2.2 alla 2.4 é cambiato  (in meglio ).

Aggiusto qualche cosa quà e là e mi trovo libapache2-mod-security2 installato, ma se pur attivo non sembra fare il suo lavoro.

Vediamo ora come fare alcuni test e attivare il modulo dormiente.

jesus

Partiamo ora nell’avventura

Dai primi passi ho preferito tenere i miei file di configurazione perché avevano modifiche consistenti, e qui devo indagare meglio su cosa è stato messo di nuovo, così in fase di aggiornamento trovo subito qualche errore

Setting up libapache2-mod-security2 (2.8.0-3) ...
Installing new version of config file /etc/modsecurity/modsecurity.conf-recommended ...
apache2_invoke: Enable module security2
apache2_reload: Your configuration is broken. Not restarting Apache 2
apache2_reload: apache2: Syntax error on line 185 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/spdy.load: Cannot load /u
sr/lib/apache2/modules/mod_spdy.so into server: /usr/lib/apache2/modules/mod_spdy.so: undefined symbol: ap_log_cerror

azz rimuoviamo il modulo mod-spdy (ottimizzato per apache 2.2) (https://code.google.com/p/mod-spdy/) e proseguiamo .

[....] Restarting web server: apache2[Wed May 27 23:56:56.718733 2015] [core:warn] [pid 29388] AH00117: Ignoring deprecated use of DefaultType in line 159 of /etc/apache2/apache2.conf.
AH02559: The SSLCertificateChainFile directive (/etc/apache2/conf.d/ssl.conf:4) is deprecated, SSLCertificateFile should be used instead
 failed!

un problema di certificati per HTTPS, commentiamo la riga 4, #SSLCertificateChainFile ssl/root.crt ora non é più necessario e proseguiamo.

[warn] The apache2 instance did not start within 20 seconds. Please read the log files to discover problems ... (warning).

Qui c’è qualcosa di nuovo che mi son perso, infatti in /etc/apache2/apache2.conf occorre impostare come sotto :

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
#LockFile /var/lock/apache2/accept.lock - disable this, old value
#PidFile /var/run/apache2.pid

proviamo a riavviare apache2

#/etc/init.d/apache2 start

se abbiamo qualche problema nell’ avviare (start/restart/reload) troviamo la lista dei processi

#pidof /usr/sbin/apache2

e uccidiamo

#kill -9 pid segnalati

Per finire sui server virtuali una nuova direttiva : https://httpd.apache.org/docs/2.4/upgrading.html

* AllowOverride now defaults to None

Con questo ho chiuso con gli errori nel log di apache2, mi resta qualche applicazione che avevo installato con qualche problema ad esempio mediawiki 1.16 da un’errore php e consegna le pagine bianche, ma avendo aggiornato tutto quel che riguarda il web (apache2, php e mysql) qualcosa non ha retto.

Ne approfittiamo per fare pulizia di roba che si usava poco o solo provata.

Partiamo ora con la parte della sicurezza e se libapache2-mod-security2 non è ancora installato andiamo a installarlo.

NOTA : per jessie la nuova versione è libapache2-mod-security2, su wheezy cercate in backports altrimenti la vecchia versione è libapache2-modsecurity,

#aptitude install libapache2-mod-security2

per vedere se il modulo è caricato:

# apachectl -M | grep --color security
 security2_module (shared)

 

per la configurazione dobbiamo editare il file modsecurity.conf che potrebbe essere disattivato

cd /etc/modsecurity
mv modsecurity.conf-recommended modsecurity.conf
nano /etc/modsecurity/modsecurity.conf

cerchiamo questa riga

SecRuleEngine DetectionOnly

e cambiamo con

SecRuleEngine On

riavviare il servizio apache2

per maggiori info anche se riferite alla vecchia versione del modulo potete leggere questo articolo : LINK

ora troveremo anche un nuovo file di log

# ls -l /var/log/apache2/modsec_audit.log
-rw-r----- 1 root root 27196808 Jun  6 01:06 /var/log/apache2/modsec_audit.log

Consiglio di inserire manualmene la rotazione del log

nano /etc/logrotate.d/modsecurity
/var/log/modsec_audit.log {
    rotate 1
    compress
    missingok
    notifempty
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

more info : modsecurity-logrotate-script

Ora che il modulo è attivo per funzionare ha bisogno di regole così cominciamo a mettere qualche link

#ln -s /usr/share/modsecurity-crs/modsecurity_crs_10_setup.conf /etc/modsecurity/modsecurity_crs_10_setup.conf

Ora mettiamo le regole base_rules e come optional per i paranoici optional_rules

#cd /usr/share/modsecurity-crs/base_rules
#for f in * ; do ln -s /usr/share/modsecurity-crs/base_rules/$f /etc/modsecurity/$f ; done
#cd /usr/share/modsecurity-crs/optional_rules
#for f in * ; do ln -s /usr/share/modsecurity-crs/optional_rules/$f /etc/modsecurity/$f ; done

per rendere effettive le modifiche è necessario riavviare il servizio

#service apache2 restart

volendo fare un test possiamo andare sul nostro server e :

http://www.example.com/?test=MY_UNIQUE_TEST_STRING

la risposta dovrebbe essere : 503 “Service Temporarily Unavailable” 

more info : LINK

Facendo un po di prove ho trovato che usando le mie applicazioni avevo qualche problema, modsecurity bloccava troppo !!!

Parto dalla consultazione dei log mentre provo ad accedere ai miei servizi

#tail -f /var/log/apache2/modsec_audit.log

il log parla chiaro e mi indica la regola e la riga esatta in modo che la posso disabilitare commentandola (#) e dopo ogni modifica ricarichiamo il servizio

#service apache2 reload

Volendo fare alcune prove possiamo usare nikto, basta scaricare e scompattare l’archivio ed infine eseguire :

$ perl nikto.pl 
- Nikto v2.1.5
---------------------------------------------------------------------------
+ ERROR: No host specified

       -config+            Use this config file
       -Display+           Turn on/off display outputs
       -dbcheck            check database and other key files for syntax errors
       -Format+            save file (-o) format
       -Help               Extended help information
       -host+              target host
       -id+                Host authentication to use, format is id:pass or id:pass:realm
       -list-plugins       List all available plugins
       -output+            Write output to this file
       -nossl              Disables using SSL
       -no404              Disables 404 checks
       -Plugins+           List of plugins to run (default: ALL)
       -port+              Port to use (default 80)
       -root+              Prepend root value to all requests, format is /directory 
       -ssl                Force ssl mode on port
       -Tuning+            Scan tuning
       -timeout+           Timeout for requests (default 10 seconds)
       -update             Update databases and plugins from CIRT.net
       -Version            Print plugin and database versions
       -vhost+             Virtual host (for Host header)
                + requires a value

        Note: This is the short help output. Use -H for full help text.

mettiamo i dati interessanti come ad esempio

$perl nikto.pl -host xxx.xxx.xxx.xxx -port 443 -output puppa.txt
- Nikto v2.1.5
---------------------------------------------------------------------------
+ Target IP:          xxx.xxx.xxx.xxx
+ Target Hostname:    host***-***-*******.***-***-b.*******.*********.it
+ Target Port:        443
+ Start Time:         2015-06-05 23:01:06 (GMT2)
---------------------------------------------------------------------------
+ Server: Apache
+ The anti-clickjacking X-Frame-Options header is not present.
+ No CGI Directories found (use '-C all' to force check all possible dirs)
+ 6544 items checked: 13 error(s) and 1 item(s) reported on remote host
+ End Time:           2015-06-05 23:24:39 (GMT2) (1413 seconds)
---------------------------------------------------------------------------
+ 1 host(s) tested

 

su una macchina dove non è abilitato the sound is different

- Nikto v2.1.5
---------------------------------------------------------------------------
+ Target IP: xxx.xxx.xxx.xxx
+ Target Hostname: host***-***-****.***-***-b.******.********.it
+ Target Port: 443
---------------------------------------------------------------------------
+ SSL Info: Subject: /CN=*******.****
 Ciphers: ECDHE-RSA-AES256-GCM-SHA384
 Issuer: /O=Root CA/OU=http://www.cacert.org/CN=CA Cert Signing Authority/emailAddress=support@cacert.org
+ Start Time: 2015-06-06 10:43:23 (GMT2)
---------------------------------------------------------------------------
+ Server: Apache
+ Retrieved x-powered-by header: PHP/5.4.39-0+deb7u2
+ The anti-clickjacking X-Frame-Options header is not present.
+ Cookie PHPSESSID created without the secure flag
+ Cookie PHPSESSID created without the httponly flag
+ OSVDB-3268: /cgi-bin/: Directory indexing found.
+ Server leaks inodes via ETags, header found with file /favicon.ico, inode: 3235957, size: 1968, mtime: 0x3ee45eb6bda00
+ DEBUG HTTP verb may show server debugging information. See http://msdn.microsoft.com/en-us/library/e8z01xdh%28VS.80%29.aspx for details.
+ Cookie p7token created without the secure flag
+ Cookie TenantHash created without the secure flag
+ Cookie TenantHash created without the httponly flag
+ /index.php/\"><script><script>alert(document.cookie)</script><: eZ publish v3 and prior allow Cross Site Scripting (XSS). 
+ OSVDB-682: /webalizer/: Webalizer may be installed. Versions lower than 2.01-09 vulnerable to Cross Site Scripting (XSS).
+ OSVDB-12184: /index.php?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000: PHP reveals potentially sensitive information via certain HTTP requests that contain specific QUERY strings.
+ OSVDB-3268: /img/: Directory indexing found.
+ OSVDB-3092: /img/: This might be interesting...
+ Uncommon header 'x-frame-options' found, with contents: DENY
+ Uncommon header 'x-ob_mode' found, with contents: 1
+ Uncommon header 'x-content-security-policy' found, with contents: default-src 'self' ;options inline-script eval-script;img-src 'self' data: ;
+ Uncommon header 'content-security-policy' found, with contents: default-src 'self' ;script-src 'self' 'unsafe-inline' 'unsafe-eval' ;style-src 'self' 'un
safe-inline' ;img-src 'self' data: ;
+ Uncommon header 'x-webkit-csp' found, with contents: default-src 'self' ;script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline
' ;img-src 'self' data: ;
+ Cookie phpMyAdmin created without the httponly flag
+ OSVDB-3268: /temp/: Directory indexing found.
+ OSVDB-3092: /temp/: This might be interesting...
+ OSVDB-3092: /cgi-bin/: This might be interesting... possibly a system shell found.
+ OSVDB-3093: /.bash_history: A user's home directory may be set to the web root, the shell history was retrieved. This should not be accessible via the web.
+ OSVDB-3268: /icons/: Directory indexing found.
+ OSVDB-3233: /icons/README: Apache default file found.
and more
more
more

😉
ora non resta che passare a nuove esperienze

Rispondi

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.