Reduciendo Superficie de Ataque
La superficie de ataque se considera todo lo que está expuesto y puede contener riesgos de seguridad que pueden ser explotados por alguien o algún código malicioso. Si fuéramos a crear categorías podrían ser:
- Aplicaciones
- Redes
- Gestión de Identidad (IAM)
¿Cómo reducir la superficie de ataque?
-
Aplicaciones
- Mantenerlas actualizadas.
- Mantener el kernel de Linux actualizado.
- Eliminar paquetes y aplicaciones innecesarias.
- servicios de impresión
- ftp
- otros
- Cuantos menos paquetes, menor el riesgo.
-
Redes
- Verificar y cerrar puertos abiertos innecesarios.
- Poner la red detrás de un firewall.
- el apiserver debería estar protegido por firewall para que no pueda ser alcanzado desde fuera.
-
IAM
- Restricción de permisos de usuarios.
- Ejecutar aplicaciones como usuario y no como root.
Nodos
- Solo deben ejecutar los componentes de Kubernetes.
- Masters deben ejecutar los componentes del control plane.
- Workers deben ejecutar los componentes del worker.
- Salvo algunas aplicaciones como Falco para análisis comportamental y cosas relacionadas.
- Evite usar distribuciones no preparadas, es decir, un Ubuntu estándar que viene lleno de servicios innecesarios activados.
- Reciclaje de Nodos
- Los nodos deben ser efímeros, así como los contenedores. En cualquier momento debemos ser capaces de deshacernos de un nodo y reemplazarlo por uno nuevo.
- Deben ser creados a través de imágenes para que el reemplazo sea rápido.
A partir de ahora vamos a hacer un overview básico de Linux que nos permite hacer lo mínimo en el examen.
- netstat -plnt (analizar puertos en uso)
- lsof -i :22 (analizar los procesos usando un puerto específico, en este caso el 22)
- systemctl status|start|restart|stop servicename (manipular los servicios)
- ps -aux (ver procesos en ejecución)
Ubuntu viene con el servicio tipo gestor de paquetes llamado snap. Es una "alternativa" para instalar aplicaciones en un formato específico que no sean paquetes .deb.
root@cks-master:~# systemctl list-units --type=service --state=running | grep snap
snapd.service loaded active running Snap Daemon
# Detener el servicio es algo momentáneo. Si el sistema se reinicia volverá a estar activo.
root@cks-master:~# systemctl stop snapd.service
Warning: Stopping snapd.service, but it can still be activated by:
snapd.socket
# Desactivándolo no volverá más.
root@cks-master:~# systemctl disable snapd.service
Removed /etc/systemd/system/multi-user.target.wants/snapd.service.
root@cks-master:~# systemctl status snapd.service
● snapd.service - Snap Daemon
Loaded: loaded (/lib/systemd/system/snapd.service; disabled; vendor preset: enabled)
Active: inactive (dead) since Fri 2024-09-13 16:08:01 UTC; 39s ago
TriggeredBy: ● snapd.socket
Main PID: 2948020 (code=exited, status=0/SUCCESS)
Sep 13 16:07:16 cks-master snapd[2948020]: overlord.go:276: Acquired state lock file
Sep 13 16:07:16 cks-master snapd[2948020]: daemon.go:247: started snapd/2.63+20.04ubuntu0.1 (series 16; classic) ubuntu/20.04 (amd64) linux/5.15.0-1067-gcp.
Sep 13 16:07:16 cks-master snapd[2948020]: daemon.go:340: adjusting startup timeout by 50s (pessimistic estimate of 30s plus 5s per snap)
Sep 13 16:07:16 cks-master snapd[2948020]: backends.go:58: AppArmor status: apparmor is enabled and all features are available
Sep 13 16:07:20 cks-master systemd[1]: Started Snap Daemon.
Sep 13 16:07:58 cks-master systemd[1]: Stopping Snap Daemon...
Sep 13 16:07:58 cks-master snapd[2948020]: main.go:151: Exiting on terminated signal.
Sep 13 16:08:01 cks-master snapd[2948020]: overlord.go:515: Released state lock file
Sep 13 16:08:01 cks-master systemd[1]: snapd.service: Succeeded.
Sep 13 16:08:01 cks-master systemd[1]: Stopped Snap Daemon.
Analizando los puertos que están siendo usados y por qué procesos.
root@cks-master:~# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:46441 0.0.0.0:* LISTEN 10884/containerd
tcp 0 0 127.0.53:53 0.0.0.0:* LISTEN 455/systemd-resolve
tcp 0 0 127.0.0.1:9099 0.0.0.0:* LISTEN 12372/calico-node
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 938/sshd: /usr/sbin
tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 127.0.0.1:2381 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 11660/kubelet
tcp 0 0 127.0.0.1:10249 0.0.0.0:* LISTEN 11808/kube-proxy
tcp 0 0 127.0.0.1:10257 0.0.0.0:* LISTEN 1594246/kube-contro
tcp 0 0 127.0.0.1:10259 0.0.0.0:* LISTEN 1594243/kube-schedu
tcp 0 0 10.128.0.6:2379 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 10.128.0.6:2380 0.0.0.0:* LISTEN 11595/etcd
tcp6 0 0 :::6443 :::* LISTEN 1604406/kube-apiser
tcp6 0 0 :::22 :::* LISTEN 938/sshd: /usr/sbin
tcp6 0 0 :::10256 :::* LISTEN 11808/kube-proxy
tcp6 0 0 :::10250 :::* LISTEN 11660/kubelet
Observe que no tenemos nada fuera de lo normal. Sin embargo, el puerto 22 está disponible para internet, ya que estamos en la máquina. Lo ideal sería que estuviera disponible solo dentro de la red y estuviéramos en una VPN para acceder a esta máquina.
Vamos a instalar algún servicio FTP para ver qué sucede.
root@cks-master:~# apt install vsftpd -y
● vsftpd.service - vsftpd FTP server
Loaded: loaded (/lib/systemd/system/vsftpd.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2024-09-13 17:16:24 UTC; 15s ago
Main PID: 2981767 (vsftpd)
Tasks: 1 (limit: 4680)
Memory: 516.0K
CGroup: /system.slice/vsftpd.service
└─2981767 /usr/sbin/vsftpd /etc/vsftpd.conf
Sep 13 17:16:24 cks-master systemd[1]: Starting vsftpd FTP server...
Sep 13 17:16:24 cks-master systemd[1]: Started vsftpd FTP server.
root@cks-master:~#
root@cks-master:~# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:46441 0.0.0.0:* LISTEN 10884/containerd
tcp 0 0 127.0.53:53 0.0.0.0:* LISTEN 455/systemd-resolve
tcp 0 0 127.0.0.1:9099 0.0.0.0:* LISTEN 12372/calico-node
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 938/sshd: /usr/sbin
tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 127.0.0.1:2381 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 11660/kubelet
tcp 0 0 127.0.0.1:10249 0.0.0.0:* LISTEN 11808/kube-proxy
tcp 0 0 127.0.0.1:10257 0.0.0.0:* LISTEN 1594246/kube-contro
tcp 0 0 127.0.0.1:10259 0.0.0.0:* LISTEN 1594243/kube-schedu
tcp 0 0 10.128.0.6:2379 0.0.0.0:* LISTEN 11595/etcd
tcp 0 0 10.128.0.6:2380 0.0.0.0:* LISTEN 11595/etcd
tcp6 0 0 :::6443 :::* LISTEN 1604406/kube-apiser
tcp6 0 0 :::22 :::* LISTEN 938/sshd: /usr/sbin
tcp6 0 0 :::21 :::* LISTEN 2981767/vsftpd # <<<<
tcp6 0 0 :::10256 :::* LISTEN 11808/kube-proxy
tcp6 0 0 :::10250 :::* LISTEN 11660/kubelet
# Cuando tenemos :::Puerto significa que estamos escuchando en TODAS las interfaces. Si la máquina tiene una dirección pública entonces sí, está expuesto.
# Observe que tenemos el puerto justo después de la interfaz que está usando. En el caso de vsftpd es el puerto 21.
# Aquí el proceso ejecutándose
root@cks-master:~# ps -aux | grep vsf
root 2981767 0.0 0.0 6816 2956 ? Ss 17:16 0:00 /usr/sbin/vsftpd /etc/vsftpd.conf
root 2983246 0.0 0.0 8168 660 pts/0 S+ 17:19 0:00 grep --color=auto vsf
# si encontramos un proceso que debemos dejar de usar para hacer el hardening.
root@cks-master:~# systemctl list-units --type=service | grep vsf
vsftpd.service loaded active running vsftpd FTP server
# Recuerde que desactivar no detiene lo que ya está ejecutándose
root@cks-master:~# systemctl disable vsftpd.service
Synchronizing state of vsftpd.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable vsftpd
Removed /etc/systemd/system/multi-user.target.wants/vsftpd.service.
root@cks-master:~# systemctl status vsftpd.service
● vsftpd.service - vsftpd FTP server
Loaded: loaded (/lib/systemd/system/vsftpd.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2024-09-13 17:16:24 UTC; 8min ago
Main PID: 2981767 (vsftpd)
Tasks: 1 (limit: 4680)
Memory: 516.0K
CGroup: /system.slice/vsftpd.service
└─2981767 /usr/sbin/vsftpd /etc/vsftpd.conf
root@cks-master:~# systemctl stop vsftpd.service
root@cks-master:~# systemctl status vsftpd.service
● vsftpd.service - vsftpd FTP server
Loaded: loaded (/lib/systemd/system/vsftpd.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Sep 13 17:16:24 cks-master systemd[1]: Starting vsftpd FTP server...
Sep 13 17:16:24 cks-master systemd[1]: Started vsftpd FTP server.
Sep 13 17:26:11 cks-master systemd[1]: Stopping vsftpd FTP server...
Sep 13 17:26:11 cks-master systemd[1]: vsftpd.service: Succeeded.
Sep 13 17:26:11 cks-master systemd[1]: Stopped vsftpd FTP server.
Si queremos encontrar el servicio que está ejecutándose en un puerto específico podemos usar lsof.
# buscando quién está usando el puerto 22
# proceso sshd
root@cks-master:~# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 938 root 3u IPv4 27062 0t0 TCP *:ssh (LISTEN)
sshd 938 root 4u IPv6 27064 0t0 TCP *:ssh (LISTEN)
sshd 2989010 root 4u IPv4 15489187 0t0 TCP cks-master.us-central1-c.c.refined-byte-432619-k6.internal:ssh->138.99.34.208:59144 (ESTABLISHED)
sshd 2989116 david 4u IPv4 15489187 0t0 TCP cks-master.us-central1-c.c.refined-byte-432619-k6.internal:ssh->138.99.34.208:59144 (ESTABLISHED)
También es necesario entender un poco sobre usuarios en Linux e investigarlos.
# usuario del momento
root@cks-master:~# whoami
root
# Aquí contiene los usuarios del sistema
# Algunos usuarios se crean durante el proceso de instalación de herramientas, pero no poseen ningún shell asociado entonces es solo de uso interno.
root@cks-master:~# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash # tiene shell
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
systemd-timesync:x:102:104:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false
uuidd:x:107:112::/run/uuidd:/usr/sbin/nologin
tcpdump:x:108:113::/nonexistent:/usr/sbin/nologin
sshd:x:109:65534::/run/sshd:/usr/sbin/nologin
landscape:x:110:115::/var/lib/landscape:/usr/sbin/nologin
pollinate:x:111:1::/var/cache/pollinate:/bin/false
fwupd-refresh:x:112:116:fwupd-refresh user,,,:/run/systemd:/usr/sbin/nologin
_chrony:x:113:120:Chrony daemon,,,:/var/lib/chrony:/usr/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash # tiene shell
david:x:1001:1002::/home/david:/bin/bash # tiene shell
lxd:x:998:100::/var/snap/lxd/common/lxd:/bin/false
dnsmasq:x:114:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
ftp:x:115:123:ftp daemon,,,:/srv/ftp:/usr/sbin/nologin
root@cks-master:~#
# iniciando sesión como ubuntu
root@cks-master:~# su ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@cks-master:/root$ whoami
ubuntu
# ¿ubuntu puede iniciar sesión como root? En este sistema sí.
# Vamos a verificar los procesos de bash que tenemos
ubuntu@cks-master:/root$ sudo -i
# Verificando por la hora podemos hacer análisis de estos bash.
root@cks-master:~# ps aux | grep bash
david 2989120 0.0 0.1 10180 5324 pts/0 Ss 17:30 0:00 -bash # mi usuario cuando abrí la sesión
root 2990358 0.0 0.1 10568 5632 pts/0 S 17:33 0:00 -bash # cuando salí de mi usuario e inicié sesión como root
ubuntu 2992167 0.0 0.1 10008 5100 pts/0 S 17:36 0:00 bash # cuando iniciamos sesión con ubuntu
root 2992416 0.0 0.1 10436 5464 pts/0 S 17:37 0:00 -bash # cuando iniciamos sesión ahora con root nuevamente.
root 2992600 0.0 0.0 8168 720 pts/0 R+ 17:37 0:00 grep --color=auto bash
# Vamos a salir de los terminales y ver cerrando.
root@cks-master:~# exit
logout
ubuntu@cks-master:/root$ ps aux | grep bash
david 2989120 0.0 0.1 10180 5324 pts/0 Ss 17:30 0:00 -bash
root 2990358 0.0 0.1 10568 5632 pts/0 S 17:33 0:00 -bash
ubuntu 2992167 0.0 0.1 10008 5100 pts/0 S 17:36 0:00 bash
ubuntu 2992923 0.0 0.0 8168 656 pts/0 S+ 17:38 0:00 grep --color=auto bash
ubuntu@cks-master:/root$ exit
exit
root@cks-master:~# ps aux | grep bash
david 2989120 0.0 0.1 10180 5324 pts/0 Ss 17:30 0:00 -bash
root 2990358 0.0 0.1 10568 5632 pts/0 S 17:33 0:00 -bash
root 2993072 0.0 0.0 8168 660 pts/0 S+ 17:38 0:00 grep --color=auto bash
# Agregando un usuario toma el shell predeterminado
root@cks-master:~# adduser test
Adding user `test' ...
Adding new group `test' (1003) ...
Adding new user `test' (1002) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
root@cks-master:~# cat /etc/passwd | grep test
test:x:1002:1003:,,,:/home/test:/bin/bash
# usuario test no puede convertirse en root porque no está en el grupo sudo
root@cks-master:~# su test
test@cks-master:/root$ sudo -i
[sudo] password for test:
test@cks-master:/root$ exit
exit
# Aquí está el archivo que define los permisos.
# Quien esté en el grupo sudo o admin tiene permiso a todo.
root@cks-master:~# cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
# ubuntu está en los grupos abajo
root@cks-master:~# groups ubuntu
ubuntu : ubuntu adm dialout cdrom floppy sudo audio dip video plugdev netdev lxd
# test solo está en su grupo, por eso no pudo hacer nada.
root@cks-master:~# groups test
test : test
# Agregando el usuario test al grupo sudo
root@cks-master:~# usermod -aG sudo test
root@cks-master:~# su test
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
# Ahora fue posible escalar el privilegio.
test@cks-master:/root$ sudo -i
[sudo] password for test:
root@cks-master:~# groups test
test : test sudo
root@cks-master:~#