Clark's Home page

Tecnicismi vari di un Sysadmin Linux ma anche qualcosa della sua vita

Hardening del sistema —

Una volta installata la macchina base passiamo a fare un minimo di hardening del sistema a livello di kernel e di applicativi che ci possono essere d’aiuto.
Per prima cosa rimuoviamo i pacchetti non necessari, tipicamente tutti i -dev possibili anche se ad onor del vero ubuntu-server non installa molta roba inutile, giusto il necessario per girare l’installazione base e’ davvero base, una volta individuati e rimossi  i pacchetti inutili conviene secondo me installare quei tools utili anche se non indispensabili per facilitarci la vita nell’amministrazione della macchina un esempio per tutti logchek e relative dipendenze.
Riconfiguriamo la shared memory, Per impostazione predefinita, /dev/shm è montato in lettura / scrittura, con il permesso di eseguire programmi, negli ultimi anni, molte mailing list di sicurezza hanno notato molti exploit in cui si usa /dev/shm in un attacco contro un servizio in esecuzione, ad esempio httpd.
La maggior parte di questi attacchi tuttavia si basano su applicazioni insicure piuttosto che una vulnerabilità di Apache o Ubuntu, in linea di massima salvo applicazioni particolari ad esempio un touchpad synaptics  non serve montare /dev/shm su un server però giusto perchè non ho idea precisa di come funzioni tutto il sistema riduco i permessi su /dev/shm aggiungendo a /etc/fstab la riga:

tmpfs     /dev/shm     tmpfs     defaults,noexec,nosuid     0     0
e rendo operativo il cambiamento con un mount -o remount /dev/shm
Successivamente  andiamo a modificare il file /etc/ssh/sshd_config
[orig]#Port 22
[mod] Port 30922
[orig] LoginGraceTime 120
[mod]LoginGraceTime 20
[orig] PermitRootLogin yes
[mod] PermitRootLogin no
[orig] #X11Forwarding yes
[mod] X11Forwarding no
[orig] Subsystem sftp /usr/lib/openssh/sftp-server
[mod] #Subsystem sftp /usr/lib/openssh/sftp-server

In altre parole spostiamo la porta su cui il demone ascolta molto in alto per evitare che gli scan facciano infinite query, riduciamo il tempo in cui un utente può autenticarsi riducendo così la possibilità di DoS,  impediamo la login come root,disabilitiamo la possibilità di forwardare X via ssh (non è installato ma va bene inibirlo lo stesso) e disabilitiamo sftp che almeno adesso come adesso non ci serve.

Finita la modifica al demone di ssh passiamo a istruire il kernel tramite /etc/sysctl.conf poiché le modifiche a questo file sono molte e importanti riporto interamente il file

#
# /etc/sysctl.conf – Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables
# See sysctl.conf (5) for information.
#

#kernel.domainname = example.com

# Uncomment the following to stop low-level messages on console
kernel.printk = 3 4 1 3
### attivato il 15/10/2012
##############################################################3
# Functions previously found in netbase
##disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.all.rp_filter=1
### abilitato in data 15/10/2012
# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
### attivato il 15/10/2012
# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1

# Uncomment the next line to enable packet forwarding for IPv6
#  Enabling this option disables Stateless Address Autoconfiguration
#  based on Router Advertisements for this host
#net.ipv6.conf.all.forwarding=1

###################################################################
# Additional settings – these settings can improve the network
# security of the host and prevent against some network attacks
# including spoofing attacks and man in the middle attacks through
# redirection. Some network environments, however, require that these
# settings are disabled so review and enable them as needed.
#
# Do not accept ICMP redirects (prevent MITM attacks)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
### abilitati in data 15/10/2012
# _or_
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
# Log Martian Packets
net.ipv4.conf.all.log_martians = 1
### attivato in data 15/10/2012
#
### aggiunti in data 15/10/2012
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
### aggiunti in data 17/10/2012
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Modify system limits for Ensim WEBppliance
fs.file-max = 65000

# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 15

# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1800

# Turn off the tcp_window_scaling
net.ipv4.tcp_window_scaling = 0

# Turn off the tcp_sack ( Need to turn on for traffic to internet)
#net.ipv4.tcp_sack = 0

# Turn off the tcp_timestamps
net.ipv4.tcp_timestamps = 0

# Set maximum amount of memory allocated to shm to 256MB
kernel.shmmax = 268435456

# Increase the maximum total TCP buffer-space allocatable
net.ipv4.tcp_mem = 57344 57344 65536

# Increase the maximum TCP write-buffer-space allocatable
net.ipv4.tcp_wmem = 32768 65536 524288

# Increase the maximum TCP read-buffer space allocatable
net.ipv4.tcp_rmem = 98304 196608 1572864

# Increase the maximum and default receive socket buffer size
net.core.rmem_max = 524280
net.core.rmem_default = 524280

# Increase the maximum and default send socket buffer size
net.core.wmem_max = 524280
net.core.wmem_default = 524280

# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 1440000

# Allowed local port range
net.ipv4.ip_local_port_range = 16384 65536

# Increase the maximum memory used to reassemble IP fragments

net.ipv4.ipfrag_high_thresh = 512000
net.ipv4.ipfrag_low_thresh = 446464

# Increase the maximum amount of option memory buffers
net.core.optmem_max = 57344

sysctl.conf.tar

Controlliamo che porte abbiamo aperte sul server con un:
netstat -tulp

root@crm:/etc# netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 localhost:mysql         *:*                     LISTEN      867/mysqld
tcp        0      0 *:http                  *:*                     LISTEN      1621/apache2
tcp        0      0 localhost:smtp          *:*                     LISTEN      1196/master
tcp        0      0 *:1722                  *:*                     LISTEN      778/sshd
tcp        0      0 *:https                 *:*                     LISTEN      1621/apache2
tcp6       0      0 [::]:1722               [::]:*                  LISTEN      778/sshd
udp        0      0 crm.zincometal.com:ntp  *:*                                 2326/ntpd
udp        0      0 localhost:ntp           *:*                                 2326/ntpd
udp        0      0 *:ntp                   *:*                                 2326/ntpd
udp6       0      0 [::]:ntp                [::]:*                              2326/ntpd

da rivedere perché nonostante abbia disabilitato ipv6 ci siano ancora tcp6 e udp6 ma al momento non mi preoccupa e proseguiamo con un nmap
pc0:/home/clark# nmap -sTU crm.myfirm.com

Starting Nmap 5.00 ( http://nmap.org ) at 2012-10-17 14:19 CEST
Interesting ports on 213.215.xxx.xxx:
Not shown: 1000 open|filtered ports, 999 filtered ports
PORT    STATE SERVICE
443/tcp open  https

Nmap done: 1 IP address (1 host up) scanned in 12.74 seconds

Appare aperta solo la 443 che e’ quello che ci serve, per quanto possibile abbiamo messo in sicurezza la macchina a livello di sistema.


Categorised as: Linux | Sistemi operativi | Work

Comments are disabled on this post


Comments are closed.


Hide picture