I did a client-server software assessment recently and needed to perform a SSL MITM attack. Since i have not done this before i decided to document some key requirements. The setup here assumes that we control a host on the same LAN as the victim host.
First, we need to enable IP forwaring on our attacking host to work like a gateway:
echo "1" > /proc/sys/net/ipv4/ip_forward
Then, we need to install the dsniff tool suite, as it contains a tool to perform ARP poisoning, called arpspoof. With the help of this tool we can force a victim host to route its traffic across the machine we control. Following is the command we need to execute:
arpspoof -i interface -t target host
Just replace interface with the network interface to use, target with the IP address of the target machine, and host with IP address of the host we want to intercept packets for. In our case the IP address of gateway machine.
In case the application we want to fool uses DNS to find its target we can use the dnsspoof tool to redirect the querying host to a different location. Otherwise we need to intercept the specific SSL connection at our host. For this reason we create an IPTables rule to route, for example, all connections that target port 443 to our fake gateway host:
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 127.0.0.1:443
At this point all connections to port 443 are redirected to our machine. Since we do not have the required application server running we still need to forward the connection to its original host. For this purpose we use the tool stunnel. This tool simulates a SSL server on one end and proxies the request to the original server by simluating a client on the other end. The following command starts the server:
stunnel -p certificate.pem -d 443 -r 8080
This opens a SSL server on our host to which the victim connects and redirects it internally to port 8080. At this point the SSL encrypted session is terminated at our host and we can investigate the clear traffic. Next, we need to complete the connection to the original host:
stunnel -c -d 8080 -r originalhost:443
Now we can sniff the traffic of an otherwise encrypted SSL session. Of course, this process only works if the client does not verify the server certificate, which in this case is our own that we provided to the stunnel. From the server-side it is impossible to distinguish our hijacked connection from a legit client.