Tuesday, April 7, 2020

SSH SOCKS Proxying with Putty

I'm writing this during the COVID-19 lockdown.  My company's VPN is getting hit really hard since everyone is working from home.  Anything we can do to stay off of it is helpful.

We also keep a host available with SSH exposed publicly (public key auth only).  So, I use that host as an SSH SOCKS proxy and it works great for keeping me off the VPN.

So, if you're in a similar position or simply would like to use SSH as a sort of pseudo-VPN, these instructions might be helpful.

Non-Windows users can do the same thing, you just need to use the ssh command to connect to the remote host and use the -D parameter.  Something like: ssh -D 1337 yourhost

Putty Configuration


  1. Create a new session in Putty
  2. Hostname: yourhost
  3. Port: 22
  4. Go under Category → Connection → Data
    • Auto-login username: <your username>
  5. Category → Connection → Proxy
    • Leave this off
  6. Category → Connection → SSH
    • Enable compression
  7. Category → Connection → SSH → Auth
    • Attempt to authenticate using Pageant
    • Allow agent forwarding
  8. Category → Connection → SSH → Tunnels
    • Source port: 1337
    • Destination: yourhost
    • Radio button: Dynamic
    • Click Add
    • (Just shows D1337, this ok)
  9. Save the new Putty session
  10. Launch the new Putty session

Proxy Configuration

Now, to actually use the proxy, you can go a couple ways.  Originally, I was doing it the manual way, but I found the Chrome extension SOCKS proxy which works great.  It's hassle free and even make it so that DNS requests go over the proxy.  The source code is very small and easily reviewed so you can see it's not doing anything nefarious.

If you can't or won't install an extension, here's the manual method.
  1. Run the inetcpl.cpl control panel. (NOT the new Windows 10 Proxy Settings page.)
  2. Go under the Connections tab
  3. LAN settings button
  4. Uncheck automatic detection
  5. Check Use a proxy server for your LAN
  6. Advanced button.
  7. Fill in ONLY the SOCKS information (not http, secure, or ftp. Uncheck Use the same proxy for all protocols)
    • Socks: 127.0.0.1
    • Port: 1337

DNS Considerations

Now, if you don't have to worry about resolving any private DNS records, you're good to go.  My company has whole zones that aren't resolvable from the public internet.  For these, DNS queries have to originate from the company network.  Chrome, by default, will not send DNS requests over the SOCKS proxy, so there's an additional step required.

I suggest copying your existing Chrome icon and giving it a different name.  Edit this icon and append to the end of the Target: field, after the final quote (not inside the quotes) the following:

--proxy-server="socks5://127.0.0.1:1337" --host-resolver-rules="MAP * ~NOTFOUND , EXCLUDE 127.0.0.1"

I haven't test it myself, but I've heard that Firefox automatically pushes DNS requests over the proxy.

Limitations

So one of the big limitations of this is that it doesn't really help in a heavy Active Directory environment where your PC has to communicate with things over a domain, such as shared drives.

RDP

However, you can tunnel RDP through your SSH host as well.  Configure additional tunnels, one per RDP destination. Back in your new Putty session:

  • Category → Connection → SSH → Tunnels
  • Source port: 38001
    • (This is a made up value of no significance. You'll have to make one up for each RDP destination.)
  • Destination: rdphost:3389
  • Relaunch your Putty session
  • Open RDP
  • Use the destination address: 127.0.0.1:38001
  • Repeat the port forwards with different port numbers for each RDP host you to access.


No comments:

Post a Comment