Saturday, 14 January 2012

keychain: Set Up Secure Passwordless SSH Access For Backup Scripts

keychain: Set Up Secure Passwordless SSH Access For Backup Scripts

We establish connections to remote systems without supplying a password, however I do not want to store my password less keys ( passphrase-free keys) on my servers. ssh-agent, takes care of keys with passphase, which allowing me to easily have ssh-agent process per system per login session. How do I dramatically reduces the number of times I've to punch my passphrase from once per new login session to once every time my local server is rebooted? How do I use keychain utility for all my backup scripts for secure passwordless login?

OpenSSH offers RSA and DSA authentication to remote systems without supplying a password. keychain is a special bash script designed to make key-based authentication incredibly convenient and flexible. It offers various security benefits over passphrase-free keys.
How Does Keychain Make It Better Than a Key Less Passphrase?

If attacker broken into server with passphrase-free keys, all other your servers / workstation on which keys are used are also security risk (they can be easily breached). With keychain or ssh-agent attacker won't able to touch your remote systems without breaking your passphrase. Another example, if your laptop or harddisk stolen, an attacker can simply copy your key and use it anywhere as it is not protected by a passphrase.

keychain is a manager for ssh-agent, typically run from ~/.bash_profile. It allows your shells and cron jobs to share a single ssh-agent process. By default, the ssh-agent started by keychain is long-running and will continue to run, even after you have logged out from the system. If you want to change this behavior, take a look at the --clear and --timeout options, described below. Our sample setup is as => Remote Backup Server. Works in pull only mode. It will backup and => My desktop computer. => General purpose remote server. => General purpose remote web / mail / proxy server.

Install keychain software on so that it can login securely to other two servers for backup.

Install keychain on CentOS / RHEL / Fedora Linux

You need RPMForge repo enabled to install keychain package.
# yum install keychain
Install keychain on Debian / Ubuntu Linux

# apt-get update && apt-get install keychain
Install keychain on FreeBSD

# portsnap fetch update
# cd /usr/ports/security/keychain
# make install clean
How Do I Setup SSH Keys With passphrase?

Simply type the following commands:
$ ssh-keygen -t rsa
$ ssh-keygen -t dsa

Assign the pass phrase when prompted. See the following step-by-step guide for detailed information:
Howto Linux / UNIX setup SSH with DSA public key authentication (password less login)
Howto use multiple SSH keys for password less login
How Do I Use Keychain?

Once OpenSSH keys are configured with a pass phrase, update your $HOME/.bash_profile file which is your personal initialization file, executed for login BASH shells:

$ vi $HOME/.bash_profile
Append the following code

:### START-Keychain ### # Let re-use ssh-agent and/or gpg-agent between logins /usr/bin/keychain $HOME/.ssh/id_dsa source $HOME/.keychain/$HOSTNAME-sh ### End-Keychain ###

Now you've keychanin configured to call keychain tool every login. Just log out and log back in to server from your desktop to test your setup:

$ ssh
Sample Output:
keyhcain is up and running. Now, all you have to do is append your servers key file $HOME/.ssh/ to other UNIX / Linux / BSD boxes:
# scp $HOME/.ssh/
# scp $HOME/.ssh/
# ssh cat ~/pubkey >> ~/.ssh/authorized_keys2; rm ~/pubkey
# ssh cat ~/pubkey >> ~/.ssh/authorized_keys2; rm ~/pubkey
# ssh
# ssh
Task: Clear / Delete All Of Ssh-agent's Key

# keychain --clear
Security Task: Make Sure Intruder Cannot Use Your Existing SSH-Agent's Keys (only allow cron jobs to use password less login)

The idea is pretty simply only allow backup shell scripts and other cron job to do password less login but all users including an intruder must provide a passphrase-key for interactive login. This is done by deleting all of ssh-agent's keys. This option will increases security, it still allows your cron jobs to use your ssh keys when you're logged out. Update your ~/.bash_profile as follows:/usr/bin/keychain --clear $HOME/.ssh/id_dsa

If you are using RSA, use:/usr/bin/keychain --clear $HOME/.ssh/id_rsa

Now, just log in to remote server box once :
$ ssh
Log out (only grant access to cron jobs such as backup)
# logout
Task: Use Keychain With Backup Scripts for Passwordless login via cron

Add the following before your rsync, tar over ssh or any other network backup command:source $HOME/.keychain/$HOSTNAME-sh

Here is a sample rsync script:

# Remote Server Rsync backup Replication Shell Script 
# Local dir location LOCALBAKPOINT=/iscsi LOCALBAKDIR=/backups/ 
# Remote ssh server setup SSHUER=root SSHBACKUPROOT=/wwwroot 
# Make sure you can log in to remote server without a password source $HOME/.keychain/$HOSTNAME-sh 
# Make sure local backup dir exists [ ! -d ${LOCALBAKPOINT}${LOCALBAKDIR} ] && mkdir -p ${LOCALBAKPOINT}${LOCALBAKDIR} 
# Start backup /usr/bin/rsync --exclude '*access.log*' --exclude '*error.log*' -avz -e 'ssh ' ${SSHUER}@${SSHSERVER}:${SSHBACKUPROOT} ${LOCALBAKPOINT}${LOCALBAKDIR} 
# See if backup failed or not to /var/log/messages file [ $? -eq 0 ] && logger 'RSYNC BACKUP : Done' || logger 'RSYNC BACKUP : FAILED!'

If you are using rsnaphot backup server (see how to setup RHEL / CentOS / Debian rsnapshot backup server) add the following to your
/etc/rsnapshot.conf file

# Get ssh login info via keychain cmd_preexec source /root/.keychain/

Final Note About Keychain and Security
Cracker with an advanced attacking with deadly coding skills can still get key from memory. However, keychain makes it pretty difficult for normal users and attackers to steal your keys and use it.
OpenSSH sshd server offers two additional options to protect abuse of keys. First, make sure root login disabled (PermitRootLogin yes). Second, specify which user accounts on the server are allowed to be used for authentication by adding AuthorizedKeysFile %h/.ssh/authorized_keys_FileName. See sshd_config man page for further details.

1 comment:

Twitter Bird Gadget