{"id":1462,"date":"2021-03-23T01:40:43","date_gmt":"2021-03-23T01:40:43","guid":{"rendered":"https:\/\/dft.wiki\/?p=1462"},"modified":"2026-04-07T12:06:07","modified_gmt":"2026-04-07T16:06:07","slug":"reverse-shell-with-autossh","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=1462","title":{"rendered":"Reverse Shell with AutoSSH"},"content":{"rendered":"<p>Sometimes you need to get SSH access into a host that is inside a network behind a Firewall\/NAT and only outbound connections are allowed.<\/p>\n<p>AutoSSH can be installed on the client and will establish and persist an SSH tunnel into an external server allowing reverse connections into the client.<\/p>\n<p><strong>Install and manually test the connection<\/strong><\/p>\n<pre>sudo apt install autossh\r\nautossh -N -R <span style=\"color: #ff0000;\"><strong>2022<\/strong><\/span>:localhost:<strong><span style=\"color: #008000;\">22<\/span><\/strong> <strong>user@example.com<\/strong><\/pre>\n<p>Note: <strong>user@example.com<\/strong> is the server exposed to the internet ready to receive SSH connections, <span style=\"color: #ff0000;\"><strong>2022<\/strong><\/span>\u00a0is the port that the server will start to listen and forward to the client on port <strong><span style=\"color: #008000;\">22<\/span><\/strong>. If the server firewall exposes port <strong><span style=\"color: #ff0000;\">2022<\/span><\/strong> to the internet, anyone will be able to log into the client through the server at that port.<\/p>\n<p><strong>On the host that will receive the connection<\/strong><\/p>\n<pre>ssh user@localhost -p <strong><span style=\"color: #ff0000;\">2022<\/span><\/strong><\/pre>\n<p>AutoSSH will not type the password to connect to the server, so it requires an SSH-Key [<a href=\"https:\/\/dft.wiki\/?p=423\">Link<\/a>].<\/p>\n<p>It was tested and works fine. Now create a service on the client to enable the AutoSSH on the boot.<\/p>\n<p><strong>Configuring AutoSSH to run as a service<\/strong><\/p>\n<pre>sudo nano \/etc\/systemd\/system\/autossh-tunnel.service<\/pre>\n<p>Add the content:<\/p>\n<pre>[Unit]\r\nDescription=AutoSSH Tunnel Service - Remote Port <strong><span style=\"color: #ff0000;\">2022<\/span><\/strong>\r\n\r\n[Service]\r\n#User=<strong>userName<\/strong>\r\nRestart=always\r\nRestartSec=10\r\nExecStart=\/usr\/bin\/autossh -N -R <span style=\"color: #ff0000;\"><strong>2022<\/strong><\/span>:localhost:<strong><span style=\"color: #008000;\">22<\/span><\/strong> <strong>user@example.com<\/strong>\r\n\r\n[Install]\r\nWantedBy=multi-user.target<\/pre>\n<p>The service will be executed as root, so the root user has to have the private key to be able to authenticate the connection.<\/p>\n<pre>sudo cp ~\/.ssh\/id_rsa \/root\/.ssh\/\r\nsudo cp ~\/.ssh\/id_rsa.pub \/root\/.ssh\/<\/pre>\n<p>Use the systemctl to manage the AutoSSH service:<\/p>\n<pre>sudo systemctl <strong>daemon-reload<\/strong>\r\nsudo systemctl <strong>enable<\/strong> autossh-tunnel.service\r\nsudo systemctl <strong>start<\/strong> autossh-tunnel.service\r\nsudo systemctl <strong>stop<\/strong> autossh-tunnel.service<\/pre>\n<p>The SSH Tunnel can be used to allow connection to different ports client&#8217;s localhost.<\/p>\n<pre>autossh -N -R <span style=\"color: #ff0000;\"><b>8080<\/b><\/span>:localhost:<span style=\"color: #008000;\"><b>80<\/b><\/span> <strong>user@example.com<\/strong><\/pre>\n<p>The example above maps the port <strong><span style=\"color: #ff0000;\">8080<\/span><\/strong> on the server that will be tunneled and get access to the HTTP (port <span style=\"color: #008000;\"><strong>80<\/strong><\/span>) on the client.<\/p>\n<p>The websites LocalHost.Run [<a href=\"http:\/\/localhost.run\/\">Link<\/a>] and Ngrok [<a href=\"https:\/\/ngrok.com\/\">Link<\/a>] offers a service that uses the same ssh tunneling but connecting to their server instead of yours.<\/p>\n<hr \/>\n<p>SSHd Configuration<\/p>\n<p>Possibly the following configuration will have to be enabled on the SSH server:<\/p>\n<pre>...\r\nAllowTCPForwarding yes\r\nGatewayPorts yes\r\n...<\/pre>\n<hr \/>\n<p>SSH is a versatile protocol that allows many other functionalities such as mount a remote directory locally and even set up a temporary VPN.<\/p>\n<p>Mounting a <strong>Remote File System over SSH<\/strong> with SSHFS (client-side only):<\/p>\n<pre>sudo apt install sshfs\r\nsshfs user@example.com:\/shared sshfs<\/pre>\n<p>Setting a <strong>VPN over SSH<\/strong> with SSHuttle (client-site only):<\/p>\n<pre>sudo apt-get install sshuttle -y\r\nsshuttle --dns -vvr user@host 0\/0<\/pre>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>Check out <strong>Pinggy<\/strong> [<a href=\"https:\/\/pinggy.io\/\">Link<\/a>], this service offers a TCP-based connectivity to any port, like NGROK does, with a single command to establish a tunnel (traversing any NAT) to any local port. E.g., SSH or HTTP.<\/p>\n<pre>ssh -p 443 -R0:localhost:22 tcp@free.pinggy.io<\/pre>\n<hr \/>\n<p><strong>READ ALSO<\/strong><\/p>\n<p><strong>Setting Up and Copying SSH Keys<\/strong> [<a href=\"https:\/\/dft.wiki\/?p=423\">Link<\/a>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to get SSH access into a host that is inside a network [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-1462","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1462"}],"version-history":[{"count":15,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions"}],"predecessor-version":[{"id":5417,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions\/5417"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}