Detecting if you are vulnerable

Passphrases

SSH Snake makes use of private keys without a passphrase. When found, it uses them for any host or destination discovered during its network traversal. Keys and destinations are found by inspecting various configuration and bash history files.

Detecting whether a private key does not have a passphrase is done by using a fake one and then parse the generated error messages accordingly.

In Tanium you can use question Get SSH Audit - Private Keys from all machines to identify systems that hold private keys without a passphrase (see column 2 in the picture below).


Picture 2. Identifying passphrase usage in SSH private keys

Picture 2. Identifying passphrase usage in SSH private keys

Note:

SSH Snake only parses bash_history files. The SSH Audit - Private Key sensor parses *sh_history files to also include information from other shells like e.g. zsh (macOS), csh or ksh.

You can use question Get SSH Audit - Authorized Keys[0] from all machines to identify systems where keys have been authorized for use.


Picture 3. Identifying SSH authorized keys. When accepted by the SSH Server a username/password challenge will be skipped.

Picture 3. Identifying SSH authorized keys. When accepted by the SSH Server a username/password challenge will be skipped.

Correlating the output of both (i.e. sending and receiving parts), allows you to identify network paths that are vulnerable, when a tool like SSH Snake successfully gets deployed.

The Tanium SSH Audit content has tooling to allow analysis and visualization with network graphing tools like Cytoscape or Gephi. The picture below was generated with Cytoscape after analyzing various domains with thousands of systems which should not be interconnected.


Picture 4. Cytoscape generated SSH network graph

Picture 4. Cytoscape generated SSH network graph

Finding SSH private keys

Linux and Unix 

On Linux, finding SSH private keys is rather straight forward. SSH Snake checks files in the user home .ssh directories and reads .bash_history files for scp, ssh and rsync commands using the “-i identity_file” option. When potential key files are found the first 50 characters are read and matched against the following header strings: 

known_key_headers=(

    "SSH PRIVATE KEY FILE FORMAT 1.1"

    "-----BEGIN RSA PRIVATE KEY-----"

    "-----BEGIN DSA PRIVATE KEY-----"

    "-----BEGIN EC PRIVATE KEY-----"

    "-----BEGIN OPENSSH PRIVATE KEY-----"

    "-----BEGIN PRIVATE KEY-----"

    "-----BEGIN ENCRYPTED PRIVATE KEY-----"

    "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"

) 

Detecting whether a private key does or does not have a passphrase is done by using a fake passphrase and parse the generated error messages accordingly. 

The sensor SSH Audit - Private Key does the same and reads in addition to bash also command histories from zsh (macOS), csh and ksh. 

Windows

Finding private keys on Windows is a challenging task. They can, of course, reside just like Linux, in the user home .ssh directories, but that is not common practice. I have seen them on Desktops, private folders, shared folders, in PuTTY authentication agents and stored in software-based lockboxes. 

The frequently used PuTTY program (a free SSH and Telnet client) uses .ppk (PuTTY Private Key) files to store OpenSSH private keys. These .ppk files have yet another key header string: "PuTTY-User-Key-File-2: ssh-(rsa|ed25519|...)"

Tanium Reveal can be used to find files having the key headers mentioned above. Once in the Reveal database sensor SSH Audit - Private Keys can query them (not implemented yet) to report for Windows the same information as for Non-Windows.

Known Hosts

SSH Snake parses the known hosts files on systems with vulnerable private keys to identify potential target systems. Tanium question SSH Audit - Known Hosts shows the data that you can find in these files or Windows Registry leaves.


Picture 5. Identifying known hosts

Picture 5. Identifying known hosts

Even when the known hosts information holds one-way-hashed hostnames like in the 2nd line below,

server.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAA…JNfEYAKpr9rENgZVFlH7jtEPla0x3hakB

|1|+…IGpU= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAA…JNfEYAKpr9rENgZVFlH7jtEPla0x3hakB

SSH Snake can effectively brute-force such hashes using a pre-image attack with the ssh-keygen command.

$ ssh-keygen -F 192.168.1.1

# Host 192.168.1.1 found: line 58

|1|+…IGpU= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAA…JNfEYAKpr9rENgZVFlH7jtEPla0x3hakB

If the system has an address like 192.168.55.22, it will brute-force all 65,536 IP addresses from 192.168.0.0-192.168.255.255 (if the system has multiple addresses, it will try the C and D blocks for all of them).

Note:

The brute-force pre-image attack to reveal one-way-hashed hostnames, has not been implemented in the SSH Audit - Known Hosts sensor for performance reasons. A better approach would be to use a package which creates/updates (daily) a text file to be used by the sensor.

PuTTY (on Windows) stores the known host public key in an OpenSSH incompatible format, causing calculating the MD5 or SHA256 fingerprints to be impossible. PuTTY shares a python program (kh2reg.py) showing how the transformation is done but attempts to reverse this transformation have been unsuccessful (including a couple found on git).

Passphrase hijacking

Even private keys with a passphrase can be hijacked they are added to the standard available ssh-agent. System administrators often do this to prevent typing the passphrase many times. They do not realize that once an attacker has acquired root access, he can add the context of the ssh-agent to his own session by setting an environment variable and commands like:

# SSH_AUTH_SOCK=/tmp/ssh-LohY1H3HRf/agent.8758 ssh-add -l

# SSH_AUTH_SOCK=/tmp/ssh-LohY1H3HRf/agent.8758 ssh mysql_cluster

Shortest route to understand these lines are to:

  1. Create as non-root user a private key with a passphrase,

  2. Add its public key to the authorized keys file on a remote system,

  3. Use the ssh-add command to add your private key to the ssh-agent process,

  4. Login (still as non-root user) to the remote system of step 2 (no need to type a passphrase anymore)

  5. As root user, inspect the /tmp/ssh-* files,

  6. Create the SSH_AUTH_SOCK environment variable according to the sample above,

  7. Use the ssh command to login to the remote system of step 2.

This looks very similar to credential dumping on Windows with a tool like mimikatz when there are still remote desktop sessions active.

When your internet facing web-app server has vulnerabilities allowing root access, attackers will try (among others) the above method to move laterally. For non-internet facing servers the threat landscape consists out of supply-chain risks (e.g. log4j, SolarWinds) or internal system/application changes.