SSH Key Compromise Risks and Countermeasures (2024)

Malware Linux Security Linux Forensics Education

Date
June 26, 2023
Author
The Sandfly Security Team

When it comes to securing Linux systems, Secure Shell (SSH) has been one of the most dependable tools at a sysadmin's disposal. However, with every technology solution, there are potential pitfalls that need to be carefully managed for security. In the case of SSH, key mismanagement can cause immediate risk of mass compromise.

In this article, we will discuss some of the primary risks related with SSH keys not only on Linux, but any system using SSH for remote access. Those risks are:

  • Stolen private keys

  • Malicious keys

  • Duplicate keys

  • Orphan keys

  • Excessive keys

  • Unsecured private keys

  • SSH key options

  • authorized_keys2 files

Stolen Private Keys

SSH private keys are the crown jewels of remote access and a stolen key is a severe risk. Intruders can use stolen keys to impersonate users, access sensitive data, and take total control of a system. They also allow attackers to press lateral movement attacks and move onto other systems quickly and quietly.

We have seen many instances where poorly secured SSH keys can lead to compromise such as the following:

  • Left in a directory with world readable permissions.

  • Posted in chats like Slack.

  • Sent in email.

  • Lost/misplaced USB drives.

  • Compromised administrator or developer systems.

  • Leakage from system automation tooling leaving them behind in exposed directories.

The biggest problem with stolen keys is that there is no evidence they've been stolen. Copying data is completely silent. The only indication most organizations may see are some unusual logins if they are paying attention. Even worse, as SSH keys have no expiration themselves, they can work for quite some time unless they are rotated frequently.

Counteracting stolen keys is best handled by simply not using them. SSH key certificates are a superior solution as they can be set to have a short expiration time (e.g. a few minutes). A stolen certificate simply goes bad and won't work.

If you are using SSH keys (as most are), strategies such as periodic rotation and making sure they are securely handled is a must. If you can prevent personnel from handling them at all through the use of a credential vault this is even better. However, this level of care is difficult to manage in many organziations where keys hang around for many years. The best you can do is implement policies and measures to make sure keys are not allowed to exist beyond a certain age or move to SSH certificates (and here).

Finding Private Keys

First we'd recommend just looking on your systems for private keys hanging around where they shouldn't be. In particular, private keys in a user's home directory on a shared system is a major risk. If any account gets compromised, attackers will immediately search for private SSH keys lying about for all users.

Finding private keys can be as simple as this script below. The keys may be encrypted, but still it would be worth asking if the user requires their private key to be on a system, especially if that system is shared with multiple users. Sandfly users can enable a policy scan that will check for private keys for all users automatically.

#!/bin/bash## This script searches for any private keys under a user's home directory. ## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)for dir in $home_dirs; do # Check if the .ssh directory exists if [ -d $dir/.ssh ]; then # Find all files in the .ssh directory that contain the word "PRIVATE" private_files=$(grep -lR "PRIVATE" $dir/.ssh 2>/dev/null) # If private_files is not empty, report if [ ! -z "$private_files" ]; then echo "User with home directory $dir has files in their .ssh directory that are likely private keys:" echo "$private_files" fi fidone

SSH Key Compromise Risks and Countermeasures (1)

SSH Key Compromise Risks and Countermeasures (2)SSH Key Compromise Risks and Countermeasures (3)

Malicious Keys

Malicious SSH keys are inserted by adversaries to allow backdoor access. Logging onto a box over SSH by stuffing a new key into a user's authorized_keys file is simple and effective. Most users would never know a new key has shown up.

This script will locate any authorized_keys files modified in the past 24 hours, but it needs to be setup to run on all your systems. Other fleet management tools such as OSQuery can also be setup to search for recently modified files and should do so if you run it. Sandfly also has policy alerts that will sweep for this activity as well.

#!/bin/bash## This script searches for any SSH authorized_keys files modified in the last 24 hours. ## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# 24 hours in seconds. Adjust to suit.SECONDS_LIMIT=86400# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root." 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)# Get the current timenow=$(date +%s)for dir in $home_dirs; do # Check if the authorized_keys file exists if [ -f $dir/.ssh/authorized_keys ]; then # Get the last modification time of the file mtime=$(stat -c %Y $dir/.ssh/authorized_keys) # Calculate the difference in seconds between now and the file's mtime diff=$((now - mtime)) # If the file was modified in the last 24 hours (86400 seconds) if [ $diff -le $SECONDS_LIMIT ]; then echo "User with home directory $dir has modified their authorized_keys file in the last 24 hours." fi fidone

SSH Key Compromise Risks and Countermeasures (4)

SSH Key Compromise Risks and Countermeasures (5)SSH Key Compromise Risks and Countermeasures (6)

In whatever way you choose, monitoring for modified authorized_keys will find compromised credentials quickly that otherwise would go unnoticed.

Duplicate Keys

Look at this image below. It has a duplicate SSH key that will allow immediate login. How quickly can you spot it?

SSH Key Compromise Risks and Countermeasures (7)

SSH Key Compromise Risks and Countermeasures (8)SSH Key Compromise Risks and Countermeasures (9)

Actually in the image above, it has three duplicate keys. It's very difficult for a human to determine if a key has a duplicate by taking a quick look (let alone doing it across many systems and users).

A duplicate SSH key present in authorized_keys is what happens when a key is present two or more times. What that means is if you delete a key with the intent of disallowing future logins, the unnoticed duplicates will still allow the key holder to login to the host.

We see a number of customers that have duplicate keys detected by Sandfly. It is very easy to have happen with just some of the mistakes below:

  • User pastes in a key that is already present but they didn't know.

  • A system automation tool managing key files makes an error and inserts a key one or more times.

  • A backup is restored that has an old authorized_keys file with a duplicate key present.

  • A piece of malware inserts a backdoor key one or more times because they are not checking if they have already dropped their key.

Duplicate keys are a unique threat because unlike changing a password, removing a key may not prevent login if it still remains. Because of the difficulty in spotting this kind of situation it must be automated.

Sandfly looks for this activity by default, but this script will search for duplicate keys right now:

#!/bin/bash## This script searches for any SSH authorized_keys files that have duplicate keys present. ## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root." 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)for dir in $home_dirs; do # Check if the authorized_keys file exists if [ -f $dir/.ssh/authorized_keys ]; then # Sort keys, count duplicates, and print any with count > 1 echo "Processing $dir/.ssh/authorized_keys." sort "$dir/.ssh/authorized_keys" | uniq -c | while read count key do if [ "$count" -gt 1 ]; then echo "$key is duplicated $count times" fi done fidone

SSH Key Compromise Risks and Countermeasures (10)

SSH Key Compromise Risks and Countermeasures (11)SSH Key Compromise Risks and Countermeasures (12)

Orphan Keys

Orphan keys are those that remain on a system after the corresponding user has been removed, or their access is no longer required. These keys offer a backdoor entry point if they get compromised. This risk can be mitigated by setting up proper key de-provisioning procedures and regular audits to clean up any orphan keys.

If you have used SSH authorized_keys for any period of time you likely have orphan keys right now. The only way to know is to manually review your authorized_keys files and remove keys you know are no longer needed. It will work better in most cases to simply get a list of keys you know should be allowed access as a user and wipe out the older authorized_keys file and start fresh.

While looking at the timestamp on the authorized_keys file can show you the last time a key was added, it won't give you the dates of older keys so you cannot assign any time to when they first appeared. The only way to determine how long a key has been around is if you were tracking them centrally and can use the first time the key appeared as an anchor value across hosts (like Sandfly SSH Hunter does). File timestamps alone are not going to help you find older keys here.

This is a tedious task and is one of the biggest perils of authorized_keys for access management.

Excessive Keys

Users that have too many keys in theauthorized_keysfile are another potential attack vector. This causes a multitude of problems, the main one being that many people can log in as the same username and this makes auditing much harder. But also it leads to orphan keys (discussed above). Often nobody goes back to remove old keys and the list can grow and grow.

What is an excessive number of keys? Well, we think one key for one user is the limit. But in reality it just depends on your organization. We have seen usernames with nearly a hundred keys that allowed people to login as them (e.g. git or root). We feel this is very likely to lead to compromise. We recommend to keep the keys each user allows for login as short as you can.

This script below will search all local users home directories for authorized_keys files that have 10 or more keys present as a starting point.

#!/bin/bash## This script searches for any SSH authorized_keys files that have excessive keys present. ## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# Set to your limit for excessive keys.KEY_COUNT=10# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root." 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)for dir in $home_dirs; do # Check if the authorized_keys file exists if [ -f $dir/.ssh/authorized_keys ]; then # Count the number of keys (lines) in the file num_keys=$(wc -l < $dir/.ssh/authorized_keys) # If 10 or more keys, report if [ $num_keys -ge $KEY_COUNT ]; then echo "User with home directory $dir has $num_keys keys in their authorized_keys file." fi fidone

SSH Key Compromise Risks and Countermeasures (13)

SSH Key Compromise Risks and Countermeasures (14)SSH Key Compromise Risks and Countermeasures (15)

Unsecured Private Keys

A common mistake is not encrypting private keys. If private keys are not encrypted they can be easily abused if stolen. Keys should be encrypted at rest, and the storage medium (such as a filesystem) should be properly secured as well. The passphrase used to secure a key should be strong.

You may also want to increase the rounds used to check the passphrase to slow down brute force attempts (ssh-keygen -a flag) in case the key is stolen.

It's important to remember that passphrases are a stopgap measure. If an attacker has access to your system then you must assume they have stolen your passphrase with a keylogger or they can launch a brute force attack offline. However as an additional safety measure it does help to have them encrypted in case the keys are lost in other ways (such as stolen laptop, lost USB drive, etc.).

SSH Key Options

The use of SSH key options is another article in and of itself, but in general be wary of the following options and how they are being used. A full list of options is available in the man pages for OpenSSH, but these options in particular need to be closely watched:

  • agent-forwarding - Can allow someone controlling the remote system to impersonate the user logging in and it should be configured correctly to be secure.

  • command - Can allow someone to run commands as the user when they login with this key.

  • environment - Can allow setting of environment variables for the user logging in with the key. This can cause security issues if the remote server allows them to be passed into the terminal session.

  • port-forwarding - Can allow client system to forward ports bypassing firewalls and network controls.

  • user-rc - Can allow system to run ~/.ssh/rc upon login to execute commands as the user.

  • X11-forwarding - Permits X11 protocol to be forwarded over SSH, again bypassing firewall and network controls.

You can set the restrict option on all keys to automatically disable most of the above and other potentially risky options available in the future.

The script below searches for all of the above options in any authorized_keys file:

#!/bin/bash## This script searches for any SSH authorized_keys files with options set that should have closer scrutiny.## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)for dir in $home_dirs; do # Check if the authorized_keys file exists if [ -f $dir/.ssh/authorized_keys ]; then # Check if the file contains any lines that have options keywords present. options_set=$(egrep -l '^(command|environment|agent-forwarding|port-forwarding|user-rc|X11-forwarding|.*,\s*(command|environment|agent-forwarding|port-forwarding|user-rc|X11-forwarding))' $dir/.ssh/authorized_keys 2>/dev/null) # If options_set is not empty, report if [ ! -z "$options_set" ]; then echo "User with home directory $dir has options set in their authorized_keys file:" echo "$options_set" fi fidone

SSH Key Compromise Risks and Countermeasures (16)

SSH Key Compromise Risks and Countermeasures (17)SSH Key Compromise Risks and Countermeasures (18)

Options set can be innocent, but you should ensure they are what you expect and do not look suspicious.

SSH authorized_keys2 Files

No, that's not a typo. There is an SSH authorized_keys2 file that works like authorized_keys but is deprecated. If this file exists, it may still allow login on older servers. Its presence is almost always a malicious attempt to conceal access.

If you see this file in a user's home directory you should be asking some pointed questions. If the user doesn't know why the file is there then the system should be immediately investigated. Again on newer systems it may not work, but it's still worth investigating if it is present because someone put it there thinking it may work.

This script will locate authorized_keys2 files under any local user's directory:

#!/bin/bash## This script searches for any SSH authorized_keys2 files in any user's home directory.## (c) 2023 Sandfly Security# https://www.sandflysecurity.com## MIT Licensed# This script needs to be run as root to be able to read all user's .ssh directoriesif [ "$(id -u)" != "0" ]; then echo "This script must be run as root." 1>&2 exit 1fi# Get list of all home directorieshome_dirs=$(awk -F':' '{ print $6 }' /etc/passwd)for dir in $home_dirs; do # Check if the authorized_keys2 file exists if [ -f $dir/.ssh/authorized_keys2 ]; then echo "An authorized_keys2 file was found at: $dir/.ssh/authorized_keys2." fidone

SSH Key Compromise Risks and Countermeasures (19)

SSH Key Compromise Risks and Countermeasures (20)SSH Key Compromise Risks and Countermeasures (21)

Sources for Above

The above scripts are available over at Github in a free open source repository. You can view and clone it below:

Sandfly SSH Security Scanner

Automate SSH Threat Hunting on Linux

There are a myriad of ways that SSH keys can be abused and often using scripts or other tools to check for these problems is tedious and prone to miss serious risks. This is why we have Sandfly and our SSH Hunter.

SSH Hunter will find the above attacks plus many others against your SSH infrastructure. Being able to monitor this critical asset agentlessly means you can get a handle on your SSH security immediately.

SSH Hunter is available to all Sandfly users, even on the free tier. Get a license and try it today. If you want to see it in action you can watch the video below or contact us for a full demo.

Conclusions

SSH is a massive step up in security over legacy protocols like Telnet. However, the management of SSH keys is notoriously difficult and open to theft. If your organization does not monitor SSH key usage, there is a high chance a compromise could happen at some point.

We strongly recommend monitoring SSH keys for abuse as it is a silent stalker for intruders wishing to get into Linux systems and remain undetected. Use the techniques listed above to check your systems, or try Sandfly to see how agentless Linux security can find SSH key problems in your enterprise.

SSH Key Compromise Risks and Countermeasures (2024)

FAQs

What are the risks of SSH keys? ›

Key Theft: One of the primary risks associated with SSH keys is key theft. If an attacker gains access to an SSH key, they can use it to authenticate themselves and gain access to the systems and data that the key is authorized to access.

What are the risks if your private key is compromised? ›

A private key is compromised when an unauthorized person obtains the private key or determines what the private key is that is used to encrypt and decrypt secret information. The compromised key can be used to decrypt encrypted data without the knowledge of the sender of the data.

What are the vulnerabilities of SSH? ›

SSH session hijacking and unauthorized access: Attackers can hijack a user's SSH session by exploiting the trusted communication established between multiple systems via public key authentication in an SSH session. It can occur either by hijacking the SSH agent or by gaining unauthorized access to the agent's socket.

What is the risk of not rotating SSH keys? ›

By rotating SSH keys, organizations can limit the risk of compromising an old key and prevent unauthorized access to the network. If an attacker gains access to an old key, they can use it to impersonate the user and gain access to sensitive data or systems.

Why SSH is not recommended? ›

SSH is not typically considered insecure in and of itself but it is an administrative protocol and some organizations require two or more layers of control to get access to an administrative console. For example connecting via a VPN first then opening an SSH session which connects through that VPN.

How do I keep my SSH key safe? ›

To securely manage SSH keys, organizations need to establish a centralized secrets-management system, enforce MFA, rotate SSH keys, implement least privilege, remove hardcoded SSH keys and regularly audit privileges.

What are some ways keys could be compromised? ›

Top 7 Ways Your Private Keys Get Hacked
  • Phishing Attacks. Phishing attacks are by far the most common way that blockchain private keys are compromised. ...
  • Malware Infections. ...
  • Weak Passwords. ...
  • Insecure Key Storage. ...
  • Weak Key Generation. ...
  • Social Engineering. ...
  • Cloud Storage Breaches.
Jun 25, 2024

What are the safety measures associated with private keys? ›

Private keys should be kept in noncustodial cold storage until you are going to use them. This ensures there is no way for hackers to access them because there is no connection. If you're going to use your keys, transfer only what you need to your wallet, use the keys, and transfer them back to cold storage.

What does it mean when a key is compromised? ›

Key Compromise means a Private Key is said to be compromised if its value has been disclosed to an unauthorized person or an unauthorized person has had access to it.

How secure is SSH key? ›

Using PKI's cryptographic strength and authentication methods, SSH keys are not prone to malicious attacks and valid credentials are not exposed if a server has been compromised.

Are SSH keys insecure? ›

SSH private keys are the crown jewels of remote access and a stolen key is a severe risk. Intruders can use stolen keys to impersonate users, access sensitive data, and take total control of a system.

Which SSH key is most secure? ›

We strongly recommend using only the ed25519 algorithm (an ECDSA variant). It is the most secure SSH key type widely available, and is very well supported in the majority of systems. If you are using an client or server without ed25519 support, you should consider upgrading where possible.

What is the danger of SSH? ›

This pattern of ssh implementations has following kinds of risks: Theft of private key is possible due to presence of other vulnerabilities: In past, local storage locations were considered reasonably secure. But with time, software stacks and applications have become more complex and have increased the attack surface.

How often should SSH keys be changed? ›

Rotate keys: Companies should establish a regular schedule for rotating SSH keys to limit the exposure time if a key is compromised. This can be done on a quarterly or yearly basis, depending on the organization's security requirements.

Is SSH key safer than password? ›

From a security standpoint, using SSH-keys to authenticate a user's identity leads to greater protection of your data. Username/password authentication can often lead to security compromises, in particular, brute force attacks by hackers.

What can someone do with my SSH key? ›

SSH private keys are the crown jewels of remote access and a stolen key is a severe risk. Intruders can use stolen keys to impersonate users, access sensitive data, and take total control of a system. They also allow attackers to press lateral movement attacks and move onto other systems quickly and quietly.

Is it safe to give SSH public key? ›

These keys are paired using extremely strong algorithms, making it infeasible to guess or “fake” a private key, even if you know the public key. While private keys should be kept secret by the authorized person wishing to gain access to a system, public keys may be freely shared.

What are the disadvantages of SSH public key authentication? ›

Despite the benefits, using keys for SSH authentication also has some drawbacks. First, keys are more difficult to set up and maintain than passwords. You need to generate, distribute, and store your keys securely, and update them regularly.

What is a disadvantage to using SSH? ›

SSH requires some software and settings to use, and it may not be compatible with some older or simpler devices. SSH also requires some knowledge and skills to use properly, such as generating and managing keys, choosing encryption algorithms, or troubleshooting errors.

Top Articles
The 10% Rule: Limiting Lifestyle Creep - The Physician Philosopher
Motley Fool Coupons and Promo Codes September 2024: Get up to 50% Off
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 6366

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.