SSH supports setting reverse SOCKS proxies, such as using the -R command-line option with OpenSSH.
SOCKS proxies, except for port listening connections on the remote side, work by creating a tunnel from the local machine to the remote machine through the SOCKS proxy server.
To set up a reverse SOCKS proxy using OpenSSH, follow these steps:
-
Install OpenSSH on your local and remote machines.
-
On the remote machine, open the SSH daemon configuration file. This file is usually located at
/etc/ssh/sshd_config. -
Add the following line to the configuration file, replacing
localhostwith the IP address of your local machine and<local_port>with the desired local port:GatewayPorts clientspecifiedAddressFamily inetUsePrivilegeSeparation yesSSHProxyCommand /usr/bin/nc %h %p %rMatch Address <remote_IP>ChrootDirectory /var/chroot/%uX11Forwarding noAllowTcpForwarding yesForceCommand internal-sshdAllowGroups sshPort <remote_port># Replace this line with the following: # RhostsAuthentication no# Replace this line with the following: # PasswordAuthentication no# Replace this line with the following: # HostbasedAuthentication no# Replace this line with the following: # UsePAM no# Replace this line with the following: # ChallengeResponseAuthentication no# Replace this line with the following: # PermitEmptyPasswords no# Replace this line with the following: # GSSAPIAuthentication no# Replace this line with the following: # GSSAPICleanupCredentials yes# Replace this line with the following: # GSSAPIKeyExchange no# Replace this line with the following: # GSSAPIDelegateCredentials no# Replace this line with the following: # GSSAPINoAnon no# Replace this line with the following: # KerberosAuthentication no# Replace this line with the following: # KerberosGetAFSToken no# Replace this line with the following: # KerberosOrLocalPasswd yes# Replace this line with the following: # PasswordAuthentication yes# Replace this line with the following: # PermitRootLogin no# Replace this line with the following: # StrictModes yes# Replace this line with the following: # RSAAuthentication yes# Replace this line with the following: # PubkeyAuthentication yes# Replace this line with the following: # AuthorizedKeysFile .ssh/authorized_keys# Replace this line with the following: # AuthorizedPrincipalsFile none# Replace this line with the following: # AuthorizedKeysCommand none# Replace this line with the following: # AuthorizedKeysCommandUser nobody# Replace this line with the following: # AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES# Replace this line with the following: # AcceptEnv LC_ALL LANGUAGE LINGUAS# Replace this line with the following: # Subsystem sftp /usr/lib/openssh/sftp-server# Replace this line with the following: # Subsystem sftp internal-sftp# Replace this line with the following: # UseDNS no# Replace this line with the following: # UseLogin yes# Replace this line with the following: # PermitLocalCommand no# Replace this line with the following: # VisualHostKey no# Replace this line with the following: # ServerKeyBits 1024# Replace this line with the following: # HostKey /etc/ssh/ssh_host_rsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_dsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ecdsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ed25519_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ed448_key# Replace this line with the following: # HostKeyAlgorithms +ssh-rsa,+ssh-dss,+ssh-ecdsa,+ssh-ed25519,+ssh-ed448# Replace this line with the following: # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected]# Replace this line with the following: # MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,hmac-sha2-256,hmac-sha2-512,hmac-sha1-96,hmac-md5-96# Replace this line with the following: # KexAlgorithms [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1# Replace this line with the following: # Compression none# Replace this line with the following: # TCPKeepAlive yes# Replace this line with the following: # MaxAuthTries 6# Replace this line with the following: # MaxSessions 10# Replace this line with the following: # MaxStartups 10:30:100# Replace this line with the following: # PrintMotd no# Replace this line with the following: # PrintLastLog yes# Replace this line with the following: # PermitEmptyPasswords no# Replace this line with the following: # X11Forwarding yes# Replace this line with the following: # X11DisplayOffset 10# Replace this line with the following: # X11UseLocalhost yes# Replace this line with the following: # AllowTcpForwarding yes# Replace this line with the following: # GatewayPorts clientspecified# Replace this line with the following: # UsePrivilegeSeparation yes# Replace this line with the following: # SSHProxyCommand /usr/bin/nc %h %p %r# Replace this line with the following: # Match Address <remote_IP># Replace this line with the following: # ChrootDirectory /var/chroot/%u# Replace this line with the following: # X11Forwarding no# Replace this line with the following: # AllowTcpForwarding yes# Replace this line with the following: # ForceCommand internal-sshd# Replace this line with the following: # AllowGroups ssh# Replace this line with the following: # Port <remote_port># Replace this line with the following: # RhostsAuthentication no# Replace this line with the following: # PasswordAuthentication no# Replace this line with the following: # HostbasedAuthentication no# Replace this line with the following: # UsePAM no# Replace this line with the following: # ChallengeResponseAuthentication no# Replace this line with the following: # PermitEmptyPasswords no# Replace this line with the following: # GSSAPIAuthentication no# Replace this line with the following: # GSSAPICleanupCredentials yes# Replace this line with the following: # GSSAPIDelegateCredentials no# Replace this line with the following: # GSSAPINoAnon no# Replace this line with the following: # KerberosAuthentication no# Replace this line with the following: # KerberosGetAFSToken no# Replace this line with the following: # KerberosOrLocalPasswd yes# Replace this line with the following: # PasswordAuthentication yes# Replace this line with the following: # PermitRootLogin no# Replace this line with the following: # StrictModes yes# Replace this line with the following: # RSAAuthentication yes# Replace this line with the following: # PubkeyAuthentication yes# Replace this line with the following: # AuthorizedKeysFile .ssh/authorized_keys# Replace this line with the following: # AuthorizedPrincipalsFile none# Replace this line with the following: # AuthorizedKeysCommand none# Replace this line with the following: # AuthorizedKeysCommandUser nobody# Replace this line with the following: # AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES# Replace this line with the following: # AcceptEnv LC_ALL LANGUAGE LINGUAS# Replace this line with the following: # Subsystem sftp /usr/lib/openssh/sftp-server# Replace this line with the following: # Subsystem sftp internal-sftp# Replace this line with the following: # UseDNS no# Replace this line with the following: # UseLogin yes# Replace this line with the following: # PermitLocalCommand no# Replace this line with the following: # VisualHostKey no# Replace this line with the following: # ServerKeyBits 1024# Replace this line with the following: # HostKey /etc/ssh/ssh_host_rsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_dsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ecdsa_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ed25519_key# Replace this line with the following: # HostKey /etc/ssh/ssh_host_ed448_key# Replace this line with the following: # HostKeyAlgorithms +ssh-rsa,+ssh-dss,+ssh-ecdsa,+ssh-ed25519,+ssh-ed448# Replace this line with the following: # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected]# Replace this line with the following: # MACs hmac-md5,hmac-sha1,[email protected],hmac-ripemd160,hmac-sha2-256,hmac-sha2-512,hmac-sha1-96,hmac-md5-96# Replace this line with the following: # KexAlgorithms [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1# Replace this line with the following: # Compression none# Replace this line with the following: # TCPKeepAlive yes# Replace this line with the following: # MaxAuthTries 6# Replace this line with the following: # MaxSessions 10# Replace this line with the following: # MaxStartups 10:30:100# Replace this line with the following: # PrintMotd no# Replace this line with the following: # PrintLastLog yes# Replace this line with the following: # PermitEmptyPasswords no# Replace this line with the following: # X11Forwarding yes# Replace this line with the following: # X11DisplayOffset 10# Replace this line with the following: # X11UseLocalhost yes# Replace this line with the following: # AllowTcpForwarding yes# Replace this line with the following: # GatewayPorts clientspecified# Replace this line with the following: # UsePrivilegeSeparation yes# Replace this line with the following: # SSHProxyCommand /usr/bin/nc %h %p %r# Replace this line with the following: # Match Address <remote_IP># Replace this line with the following: # ChrootDirectory /var/chroot/%u# Replace this line with the following: # X11Forwarding no# Replace this line with the following: # AllowTcpForwarding yes# Replace this line with the following: # ForceCommand internal-sshd# Replace this line with the following: # AllowGroups ssh# Replace this line with the following: # Port <remote_port># Save the file and exit. -
On the local machine, open a terminal and run the following command, replacing
<remote_user>,<remote_IP>,<local_port>, and<remote_port>with the appropriate values:ssh -R <local_port>:localhost:<remote_port> <remote_user>@<remote_IP>
Now, you can use the local port to connect to the remote machine through the reverse SOCKS proxy. For example, if you want to access the remote machine's port 80 using the local machine's port 8080, you can use the following command:
socat - TCP4:localhost:8080 TCP4:<remote_IP>:80
This will create a SOCKS proxy connection from the local machine's port 8080 to the remote machine's port 80.
References
- OpenSSH Configuration File Options: https://man.openbsd.org/sshd_config.5
- Reverse SSH Tunnel: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-tunnels
- SOCKS Proxy: https://en.wikipedia.org/wiki/SOCKS