Port Forwarding and Tunnelling Cheatsheet
Port Forwarding and Tunnelling Cheatsheet
Port Forwarding and Tunnelling Cheatsheet
in 1|Page
Table of Contents
ABSTRACT 3
LAB CONFIGURATION 7
PORT FORWARDING 11
Port Forwarding using Metasploit 12
SSH Local Port Forwarding 14
Port Forwarding using Socat 15
TUNNELLING 17
Lab Requirements 17
Sshuttle 18
Chisel 21
Rpivot using Socks4 proxy 27
Dynamic SSH Tunnelling 30
Local SSH Tunnelling 32
Local SSH Tunnelling using Plink.exe 33
Dynamic SSH Tunnelling using Plink.exe 34
Tunnelling using Revsocks 36
Tunneling With Metasploit 38
Tunnelling with DNScat2 42
ICMP Tunnelling 48
Conclusion 51
ABOUT US 52
www.hackingarticles.in 2|Page
Page |3
Abstract
Port forwarding can be defined as implementation of the Network Address Translation to send
requests to communicate from one IP address and port number to other when you are located outside
the network. It basically transmits the traffic from the outside network to the local network.
Tunnelling can be defined as a protocol that lets secure passage of data from one network to another
by allowing the communication of private network to be sent across a public network, by making use
of encapsulation. Tunnelling has proven to be highly beneficial as it lets an organization create their
Virtual Private Network with the help of the public network and provide huge cost benefits for users
on both the end.
www.hackingarticles.in 3|Page
Page |4
APACHE
VIRTUAL
HOST
www.hackingarticles.in 4|Page
Page |5
www.hackingarticles.in 5|Page
Page |6
LAB
CONFIGURATION
www.hackingarticles.in 6|Page
Page |7
Lab Configuration
The lab requirements comprise of:
VMware Workstation
Ubuntu
Kali Linux
Let us start with configuring Apache2 services. To do this you will need to have Apache installed in
your Linux systems. You can install it using.
www.hackingarticles.in 7|Page
Page |8
mkdir /sbin/test
Then go to the /etc/apache2 directory and edit the file ports.conf and add ‘Listen 127.0.0.1:8080‘
before ‘Listen 80’ as in the image below.
cd /etc/apache2
nano ports.conf
cat ports.conf
Now let us create the test.conf file and add the following code in /etc/apache2/sites-
available/nano /etc/apache2/sites-available/test.conf
www.hackingarticles.in 8|Page
Page |9
nano /etc/apache2/sites-available/test.conf
<VirtualHost 127.0.0.1:8080>
DocumentRoot /sbin/test/
ServerName localhost
AllowEncodedSlashes NoDecode
<Directory "/sbin/test/">
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
</Directory>
</VirtualHost>
Now let us make use the tool a2ensite to enable our website and the let us restart our apache2.
a2ensite test.conf
systemctl restart apache2
Therefore, here we finish the setup of our lab by creating a virtual host.
www.hackingarticles.in 9|Page
P a g e | 10
PORT
FORWARDING
www.hackingarticles.in 10 | P a g e
P a g e | 11
Port Forwarding
Port forwarding is establishing a secure connection between a remote user and local machines. In
organisations on can give their source and destination port numbers to make use of tunnelling with
the help of Linux. Along with this, they should also mention the destination which can be the IP address
or name of the host.
Let’s switch on the Kali Linux machine and check if the webpage is being hosted. But here it shows
that it is unavailable. So, to let us see how the local address and port can be forwarded to the remote
host. This can be achieved using various methods, so let’s see them one-by-one.
www.hackingarticles.in 11 | P a g e
P a g e | 12
use auxiliary/scanner/ssh/ssh_login
set rhosts 192.168.1.108
set username raj
set password 123
exploit
sessions -u 1
sessions 2
netstat -antp
www.hackingarticles.in 12 | P a g e
P a g e | 13
Here we make use of portfwd to forward all the traffic to the Kali machine, where you mention the
local and the remote port and the local address.
When we load this page on the web browser using 127.0.0.1:8081 in the Kali machine, we see that
the contents of the web page are displayed.
www.hackingarticles.in 13 | P a g e
P a g e | 14
Here we can see that the contents of the web page are displayed when we load this page on the web
browser using 127.0.0.1:8081 in the Kali machine.
www.hackingarticles.in 14 | P a g e
P a g e | 15
Socat is generally a command-line utility in the Linux which is used to transfer data between two
hosts. Here we use it for port forwarding where all the TCP connections to 127.0.0.1:8080 will be
redirected to port 1234.
When we load this page on the web browser using 192.168.1.108:1234 in the Kali machine, we see
that the contents of the web page are displayed.
www.hackingarticles.in 15 | P a g e
P a g e | 16
TUNNELLING
www.hackingarticles.in 16 | P a g e
P a g e | 17
Tunnelling
Tunnelling is the process of accessing resources remotely using the public network. The tunnels which
are established are point-to-point and remote users can be linked at the other end of the tunnels. The
job of the tunnelling protocols is to encapsulate the traffic from a user situated remotely and it is sent
to the other end of the public network which is then decapsulated and sent to its destined user. The
tunnel by default is not encrypted and its level of security is determined with the help of TCP/IP
protocol that has selected.
Let us look at how we can perform Tunneling using various methods and tools.
Lab Requirements
Kali Linux with IP address 192.168.1.2
Ubuntu with 2 NIC, consisting of two IP addresses – 192.68.1.108, 192.168.226.128
Metasploitable 2 with IP address 192.168.226.129
www.hackingarticles.in 17 | P a g e
P a g e | 18
Sshuttle
Sshuttle facilitates to generate a VPN connection from a local machine to a remote Kali Linux with the
help of SSH. For the proper functioning, one must have root access on the local machine but the
remote Kali Linux can have any type of account. Sshuttle can run more than once concurrently on a
particular client machine.
Let’s see how we can use Sshuttle to get the access of a Metasploitable 2 machine which has a
different subnet using Ubuntu machine which has two internet addresses with different subnets but
also has the subnet in which the Kali Linux is present.
Now let’s check the IP addresses of the Kali Linux machine.
On checking the IP address of the Ubuntu machine, we see that it has two IP addresses with different
subnets.
www.hackingarticles.in 18 | P a g e
P a g e | 19
A connection is created remotely with the Ubuntu ([email protected]) and then the address of
Metasploitable 2(192.168.226.129) using sshuttle. Mention the password of Ubuntu and hence you
are connected.
www.hackingarticles.in 19 | P a g e
P a g e | 20
Subsequently, when you put the Metasploitable 2 IP address in your Kali Linux’s browser, you will
able to access the Metasploitable 2 on port 80.
Hence, here we saw that using Sshuttle, we first connected the Kali Linux with Ubuntu. Once the
connection with Ubuntu was made, using that, a connection between Kali Linux and Metasploitable 2
was created.
www.hackingarticles.in 20 | P a g e
P a g e | 21
Chisel
It is a TCP/UDP tunnel, which helps in transporting over and is secured using SSH. It includes both, the
client and the Kali Linux. It is generally used in passing through firewalls but can also be used to provide
a secure connection to one’s network. Let us see how this works.
First, let us install Chisel and golang in our Kali Linux machines.
Note:
Golang is the programming language in which Chisel has been written,
so for proper functioning we also install golang.
www.hackingarticles.in 21 | P a g e
P a g e | 22
Now as we now have a copy of the chisel source, we can now proceed to build our binaries for Linux
land hence compile the packages of the chisel using go build to begin.
To listen on port 8000 on the Kali Linux and allow clients to specify reverse port forwarding. Here the
reverse tunnelling has been activated.
www.hackingarticles.in 22 | P a g e
P a g e | 23
After this is done, let’s run chisel on Ubuntu to connect Kali Linux and Metasploitable 2.
Open the web browser in the Kali Linux machine to check the connection between the Kali Linux and
Metasploitable 2 which is created on the local address and port 5000.
http://127.0.0.1:5000
www.hackingarticles.in 23 | P a g e
P a g e | 24
We can follow the initial set up steps in Ubuntu and Kali Linux as seen in the chisel above proceed
ahead.
To listen on port 8000 on the Kali Linux and allow clients to specify reverse port forwarding. Here the
reverse tunnelling has been activated.
In ubuntu machine, the next step is to connect to our client using the new reverse socks option.
www.hackingarticles.in 24 | P a g e
P a g e | 25
Here we point our Socks5 client which is Metasploitable 2 to the Kali Linux using Ubuntu.
Now let’s open the web browser in the Kali Linux and go to configure the proxy settings. Here we are
manually configuring the proxy, therefore, mention the SOCKS host address as the local address i.e.,
127.0.0.1 and choose socks5 proxy on port 1080. Also, mention the local address in the ‘no proxy for’
box.
www.hackingarticles.in 25 | P a g e
P a g e | 26
When you open the web browser in the Kali Linux machine and add the Metasploitable 2 IP, you see
that the Kali Linux is connected to the Metasploitable 2.
www.hackingarticles.in 26 | P a g e
P a g e | 27
Rpivot generally provides tunnel traffic into the internal network using socks 4 proxy. Its working is
like SSH dynamic port forwarding but is in the opposite direction. It also has a client-server
architecture. When a run client on the machine it will tunnel the traffic through and for that the Kali
Linux should be enabled so that it can listen to the connections from the client.
Let’s install Rpivot in the Kali Linux machine. Then go to its directory and start the listener on port
9999, which creates socks version 4 proxy on 127.0.0.1 on a port while connecting with the client.
Now install rpivot in the Ubuntu machine and connect it with the Kali Linux.
www.hackingarticles.in 27 | P a g e
P a g e | 28
Now go to the web browser in your Kali Linux machine, and manually configure the proxy. Set the
Socks host address as local address and port as 1080. Select the Socks version 4 and mention the local
address for ’no proxy for’.
www.hackingarticles.in 28 | P a g e
P a g e | 29
Now when you open the web browser in your Kali Linux machine, but the IP address of the
Metasploitable 2 and hence you will be able to see the connection.
www.hackingarticles.in 29 | P a g e
P a g e | 30
Dynamic SSH Tunneling provides a connection with the range of ports by making SSH work like a
SOCKS proxy Kali Linux. A SOCKS proxy is an SSH tunnel where applications send their traffic using a
tunnel where the proxy sends it traffic like how it is sent to the internet. In SOCKS proxy, it is
mandatory to configure the individual client. Dynamic Tunneling can receive connections from
numerous ports.
In Kali Linux machine let’s run the command to connect with the Ubuntu using Dynamic SSH tunnelling.
Once the connection between the Kali Linux and Ubuntu is made, let’s open the browser in the Kali
Linux machine and configure the proxy in the settings. Choose to manually configure the proxy and
mention the local address as the socks host and the port number as 7000. Now select the socks
version 5 and mention the local address in ‘no proxy for’ section.
www.hackingarticles.in 30 | P a g e
P a g e | 31
Hence when you put the IP of the Metasploitable 2 in the browser of the Kali Linux, you will have an
accessible connection Metasploitable 2 using dynamic Tunnelling.
www.hackingarticles.in 31 | P a g e
P a g e | 32
Here, all the connections which are trying to connect with the Metasploitable 2 using Ubuntu with the
local destination and port. The -L indicates the local port.
In the Kali Linux machine, add the localhost and then the Metasploitable 2 username and password to
create local SSH tunnelling.
You can open the Kali Linux’s browser and mention the local address along with the port 7000 on
which the traffic was transferred.
www.hackingarticles.in 32 | P a g e
P a g e | 33
Here we are making use of command-line utility in windows machine for tunnelling, where a
command-line tool for PuTTY is being used called plink.exe. Here all the connections which are trying
to connect with the Metasploitable 2 using Ubuntu with the local destination and port.
Now open the web browser in the window’s machine and put the local address and the port 7000 on
which the traffic of Metasploitable 2 was forwarded. You see that there was local SSH Tunnelling
between Metasploitable 2 and the Kali Linux using plink.exe.
www.hackingarticles.in 33 | P a g e
P a g e | 34
Plink.exe is the windows command line for putty in the windows machine which we will use for
Dynamic Tunneling can receive connections from numerous ports.
In windows machine let’s run the command to connect with the Ubuntu using Dynamic SSH tunnelling.
Once the connection between the windows and Ubuntu is established, let us open the browser in the
windows machine and configure the proxy in the settings. Choose to manually configure the proxy
and mention the local address as the socks host and the port number as 8000. Now select the socks
version 5 and mention the local address in ‘no proxy for’ section.
www.hackingarticles.in 34 | P a g e
P a g e | 35
Hence when you put the IP of the Metasploitable 2 in the browser of the Windows, you will have an
accessible connection Metasploitable 2 using dynamic SSH tunnelling with the help of plink.exe.
www.hackingarticles.in 35 | P a g e
P a g e | 36
Revsocks stands for Reverse socks5 tunneler. You can download it from here in the windows operating
system. Here in the windows system, we are trying to connect with Ubuntu using socks5.
Now let’s open Ubuntu and download revsocks for Linux. Here we connect Ubuntu with
Metasploitable 2 and then we move to proxy settings.
Now in the Windows machine, open the browser and open proxy settings. Here, choose to manually
configure the manual proxy configuration and mention the local address in the socks host and mention
the port number as 1080. Choose the socks version 5 and then mention the local address in the ’no
proxy for’ space.
www.hackingarticles.in 36 | P a g e
P a g e | 37
When you open the web browser in the windows machine and mention the IP address of the
Metasploitable 2, you will be connected with the Metasploitable 2 using revsocks.
www.hackingarticles.in 37 | P a g e
P a g e | 38
Here we start Metasploit in the Kali machine. Then a connection is established with Ubuntu using the
auxiliary module with the help of SSH. Once the connection is established, a meterpreter session was
created. Then we make use of post module with autoroute. The autoroute post module will help
create an additional route through the meterpreter which will allow us to dive deeper in the network.
Here we will connect with Metasploitable 2. Next, we will use the auxiliary module for socks5. This is
now a deprecated module. Set the localhost address and exploit. The auxiliary module will then start
running.
use post/multi/manage/autoroute
use auxiliary/server/socks5
set srvhost 127.0.0.1
exploit
Now go to the web browser in the Kali Linux machine, open the browser and open proxy settings.
Here, choose to manually configure the manual proxy configuration and mention the local address in
the socks host and mention the port number as 1080. Choose the socks version 5 and then mention
the local address in the ’no proxy for’ space.
www.hackingarticles.in 38 | P a g e
P a g e | 39
When you open the web browser in the Kali Linux and mention the IP address of the Metasploitable
2, you will be connected with the Metasploitable 2 using Metasploit.
www.hackingarticles.in 39 | P a g e
P a g e | 40
Now let’s start Metasploit in the Kali machine where the connection is established with Ubuntu with
the help of auxiliary module using SSH. Then a meterpreter session was created. Then we will use the
post-module where we will use autoroute. The autoroute post module will help to create additional
routes through the meterpreter which will allow us to dive deeper in the network. Here we will
connect with Metasploitable 2. Next, we will use the auxiliary module for socks4a. This is now a
deprecated module. Instead, we can use the new module Set the localhost address and exploit. The
auxiliary module will then start running.
use post/multi/manage/autoroute
use auxiliary/server/socks4a
set srvhost 127.0.0.1
exploit
Hence, open the web browser in the Kali Linux machine, and open proxy settings. Now, choose to
manually configure the manual proxy configuration and mention the local address in the socks host
and mention the port number as 1080. Choose the socks version 4a and then mention the local
address in the ’no proxy for’ space.
www.hackingarticles.in 40 | P a g e
P a g e | 41
When you open the web browser in the Kali Linux and mention the IP address of the Metasploitable
2, you will be connected with the Metasploitable 2 using Metasploit.
www.hackingarticles.in 41 | P a g e
P a g e | 42
DNScat2 is a tool which can be used to create a tunnel with the help of DNS protocol. A connection
to port 53 should be established to access any data. DNScat2 mainly consists of a client and a server.
In our scenario, we need to establish a connection between Metasploitable 2 and Kali Linux using
Ubuntu as the medium.
Tunnelling on Port 22
Let’s begin with installing DNScat2 in the Kali Linux machine using apt install which will automatically
build dependencies.
www.hackingarticles.in 42 | P a g e
P a g e | 43
In the Ubuntu machine, we will install dnscat2 using git clone. Here we will have to install the
dependencies manually to get the tool started.
Now let's establish a connection between the Kali Linux and Ubuntu.
www.hackingarticles.in 43 | P a g e
P a g e | 44
./dnscat --dns=server=192.168.1.2,port=53
Once the connection is successfully established, a session will be created on the Kali Linux’s end. Now
let us check the sessions that are available and interact with them and then send in a request to create
a shell. Once the request is accepted a new window will open the session 2.
session
session -i 1
shell
www.hackingarticles.in 44 | P a g e
P a g e | 45
Using the second session, you now have access to the Ubuntu machine. So now let's check the IP
address of the client one machine. Here we see that Ubuntu has two NIC cards installed within it.
session -i 2
ifconfig
www.hackingarticles.in 45 | P a g e
P a g e | 46
Now we will connect the Metasploitable 2 port 22 to the port 8888 to create a DNS tunnel between
them using the shell.
Open a new tab in the Kali Linux machine and login to the Metasploitable 2 machine with its
credentials and now you will be able to communicate with Metasploitable 2 using the Kali Linux.
www.hackingarticles.in 46 | P a g e
P a g e | 47
Tunnelling on Port 80
When you open the web browser in the Kali Linux machine and mention the URL of the Metasploitable
2 machines, you will see that the connection was successfully established between the Kali Linux and
the Metasploitable 2 using Ubuntu.
The same can be done in the windows system Follow this link here to download a suitable dnscat2
client for your system of windows. To get a detailed explanation on DNScat2 you can read here.
www.hackingarticles.in 47 | P a g e
P a g e | 48
ICMP Tunnelling
The main aim of the ICMP tunnel is to send TCP connection where an SSH session will be used in an
encapsulated form of ICMP packets. Let’s first configure the ICMP tunnel on the Ubuntu machine. You
can read a detailed article from here.
We will first download and install icmptunnel on the server-side and compile the file by unpacking its
components.
Then we will disable ICMP echo reply on both the Ubuntu and the Kali Linux. This halts the kernel from
responding to any of its packets.
www.hackingarticles.in 48 | P a g e
P a g e | 49
Now let’s start the ICMP tunnel on Ubuntu on server mode and assign it a new IP address for
tunnelling.
Now let’s install and set up ICMP tunnel on the client side i.e Kali Linux like we did in Ubuntu.
www.hackingarticles.in 49 | P a g e
P a g e | 50
Once the other IP address for tunnelling is created in the Kali machine, let’s connect over SSH with
the credentials of the server-side with IP address 10.0.0.1.
www.hackingarticles.in 50 | P a g e
P a g e | 51
When you open Wireshark and capture the packets, you only see that all the packets of SSH which is
a TCP protocol is being transported on the ICMP protocol.
Conclusion
Therefore, in this article, we have seen the effectiveness of various port forwarding and Tunnelling
methods to provide a secure and encrypted connection.
www.hackingarticles.in 51 | P a g e
JOIN OUR
TRAINING PROGRAMS
H ERE
CLICK BEGINNER
Network Pentest
Wireless Pentest
ADVANCED
Advanced CTF
Android Pentest Metasploit
EXPERT
Privilege Escalation
APT’s - MITRE Attack Tactics
Windows
Active Directory Attack
Linux
MSSQL Security Assessment
www.ignitetechnologies.in