[ SSH Gymnastics and Tunneling with ProxyChains ]

I wrote this in 2012 (and tidied it up in 2015) around OpenSSH's dynamic -D forwarding and the original ProxyChains. The technique has not changed, though these days you would likely reach for the maintained proxychains-ng fork.

[ Overview ]

'\v/`
     (o 0)  m0o.
 (_)  /
"Over the router, through the firewall.. 0ff to grandmas house we go..."

Last Edit: Feb 9 2012 <work in progress>
Updated: March 2015: Document clean up

[ Introduction ]

Jumping through hoops

Ever heard the expression "I had to jump through hoops to get there"?

With the ever increasing complexness of network topologies and designs,
along with the security defenses to protect them. Jumping through hoops
is a fairly common practice in the security world. This paper will
examine how to create tunnels into a network using some SSH Gymnastics
and Proxychains. In the end it will cover scanning anonymously using the
TOR network and proxychains.

[ ProxyChains ]

Proxychains is a tool that can intercept any TCP and/or UDP traffic from
a specific process and tunnel it over a proxy. The proxies supported by
proxychains are: HTTP, SOCKS4, and SOCKS5.
Network overview: the Internet, two firewalls, the 192.168.2.x/24 LAN and the 10.10.10.x/24 DMZ, with one LAN host multi-homed into the DMZ
Diagram 1 - Network Overview

[ Launch Pad ]

The pivot machine

If you were to target the internal network in the diagram above with only
external access, you will have a very limited view into the inside of the
network. Using SSH and proxychains this can be very easily worked around
by creating a dynamic port forwarder using SSH on the attackers localhost
and connecting to one of the host(s) you have access to externally via
SSH. You first need to create the dynamic listener on attacker machine on
a specific port using the ssh -D option. Once you have established a
connection to the remote machine, you will then need to configure
proxychains to use the listening port created for this tunnel in order to
access the internal network.

    ssh -D 127.0.0.1:9050
Taken from ssh(1)
     -D [bind_address:]port
          Specifies a local "dynamic" application-level port forwarding.
          This works by allocating a socket to listen to port on the
          local side, optionally bound to the specified bind_address.
          Whenever a connection is made to this port, the connection is
          forwarded over the secure channel, and the application protocol
          is then used to determine where to connect to from the remote
          machine.  Currently the SOCKS4 and SOCKS5 protocols are
          supported, and ssh will act as a SOCKS server.  Only root can
          forward privileged ports.  Dynamic port forwardings can also be
          specified in the configuration file.

          IPv6 addresses can be specified with an alternative syntax:
          [bind_address/]port or by enclosing the address in square
          brackets.  Only the superuser can forward privileged ports.  By
          default, the local port is bound in accordance with the
          GatewayPorts setting.  However, an explicit bind_address may be
          used to bind the connection to a specific address.  The
          bind_address of "localhost" indicates that the listening port
          be bound for local use only, while an empty address or '*'
          indicates that the port should be available from all
          interfaces.

[ Configuring ]

The proxychains config file is located at: /etc/proxychains.conf.
Under the [ProxyList] directive, make sure the config file contains
the following:

    socks4 127.0.0.1 9050

[ Scenario 1 ]

Attack Scenario #1:

The attacker begins by establishing a connection to 210.210.210.160 on
the SSH service. The SSH service has been port-forwarded by the remote
firewall to the internal host at 192.168.2.5. Using the -D option the
attacker then creates a local dynamic listener on port 9050 for this
connection.

    ssh -D 127.0.0.1:9050 [email protected]
The attacker opening an SSH session through the firewall to the pivot host (external 210.210.210.160, internal 192.168.2.2)
Diagram 2 - Establishing an SSH session to the pivot machine
Once the SSH session had been established, the attacker would then have
visibility into the network (TCP/UDP). You will then be able to scan
machines on the internal network located at 192.168.2.5 and
192.168.2.10 using the tunnel that has just been created.

    proxychains nmap -PN -n -sTV 192.168.2.X -p1-65535
An established SSH tunnel giving the attacker visibility into the internal network
Diagram 3 - Overview of an established tunnel
The machine that you are using as a pivot point does NOT have to straddle
the firewall in any sort of way. It either needs layer 2 connectivity
into the others or have rules on the firewall allowing the traffic. In
this example, the machines located at 192.168.2.5 and 192.168.2.10 will
see the source of the attack as being from 210.210.210.160 or
192.168.2.2.

[ Scenario 2 ]

Attack Scenario #2 - The Rabbit hole does go deeper. Setting up
advanced tunnels

Note: This works with dual homed host, or host with special rules
through a firewall segment.
The dual-homed machine (LAN 192.168.2.5, DMZ 10.10.10.9) that bridges the LAN and the DMZ host at 10.10.10.1
Diagram 4 - Advanced tunnels using dual homed host(s) overview
Now that you have configured the first basic tunnel, you can now use this
tunnel to go deeper into the network. As an example, imagine there was
yet another network '10.10.10.0/24' living behind 192.168.2.5, which has
been discovered to be a dual homed host on the DMZ network. Using proxy
chains we can piggy back the existing tunnel to create a new one into the
DMZ network.

    proxychains ssh -D 127.0.0.1:9051 [email protected]

Once this tunnel is established into the DMZ network, the host at
10.10.10.1 will see the source connection coming from: 192.168.2.2 on
the LAN side.
The attacker chaining ProxyChains and SSH through the dual-homed machine (192.168.2.5 / 10.10.10.9) into the 10.10.10.x/24 DMZ
Diagram 5 - Advanced tunnels with dual homed host(s), example of an established session

[ Recap ]

The first step was to create a dynamic listener on the attackers machine
listening on port 9050 which gets the connection into 192.168.2.2 via
SSH. Then another dynamic listener was created on the attackers machine
listening on port 9051, which has a tunnel into 10.10.10.1. These tunnels
can very easily go on and on, as you should get the idea by now. Each
port in use should have its very own proxychains.conf file. To do so you
simply copy the /etc/proxychains.conf into a newly created directory. Ie
port9051/. After copying the config file into this directory, you can
then modify proxychains.conf and change the socks4 port.

    cat /etc/proxychains.conf | sed "s/127.0.0.1 9050/127.0.0.1 9051/g" > ./proxychains.conf

Proxychains will search for a config file within the current working
directory. Once the file has been changed, you can now issue the
proxychains command from this directory with whatever command you choose.

    proxychains nmap...

To be continued...eventually.

[ See Also ]