Showing posts with label SSH. Show all posts
Showing posts with label SSH. Show all posts

Tuesday, 8 January 2013

SSH Chaining / Bunny Hopping

Sometimes you're in a situation where a server on a restricted network needs SSH access to another server outside of that network. Here I explain how an intermediary server (one accessible from both networks) can be used to allow an end-to-end SSH connection.

Host A is inside a restricted network and cannot SSH to the outside world.
Host B is in a different part of the network, is accessible by Host A and _can_ SSH to the outside world.
Host C is a server off on the internet somewhere

On Host A, we use SSH to create a tunnel via Host B to Host C. This opens a local port on Host A (2222) that will forward packets to port 22 on Host C. Note that the packets are encrypted in the tunnel between Host A and Host B but not necessarily between Host B and Host C.

Run from Host A to setup the tunnel:

ssh -f user@hostB -L 2222:hostC:22

Now, to SSH to Host C from Host A simply connect to the localhost using port 2222 and the packets will be forwarded to Host C via Host B.

ssh user@localhost -p 2222

This is particularly useful for transferring files around, it allows you to neatly bypass most network controls, here's an example using the above and SCP to tunnel data out of a network in an encrypted fashion  Note this is transferring DATA_DIRECTORY from Host A to Host C and placing it in the users home directory

scp -P 2222 -r DATA_DIRECTORY user@localhost:~/

Simples!

Monday, 23 July 2012

Encrypting files using SSH Keypairs and OpenSSL

There are a number of reasons why you don't want to do this, go and use GPG or something similar to perform your encryption. 


That said, if you're in a situation where you have a secret that needs securing before transmission to a specific party and the only credential for the recipient you have is their SSH Public Key then read on...


To start with you're going to need OpenSSL and SSH installed, we need SSH to convert the keys and we'll perform the actual encryption using OpenSSL.


Use SSH to convert the normal public key into something more usable by OpenSSL:

ssh-keygen -f id_rsa.pub -e -m pkcs8 > id_rsa.pub8

Use OpenSSL to encrypt 'secrets.txt' using the public key:
openssl rsautl -encrypt -inkey id_rsa.pub8 -pubin -in secrets.txt -out secrets.ssl

The recipient of the file can decrypt it using their corresponding private key:
openssl rsautl -decrypt -inkey id_rsa -in secrets.ssl -out secrets.txt

Voila! Simple public key encryption using SSH keys.

Be aware that we are doing direct RSA encryption here, which means you can only encrypt very small amounts of data. Anything bigger will typically use a symmetric algorithm like AES for encryption and use RSA to protect the AES key.