๐งช 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¶

-
Attacker sends query for random subdomain
Example: random1.example.com -
DNS server forwards request to real DNS
-
Attacker floods fake responses:
-
Different transaction IDs
- Same question
-
Fake authority section
-
If one matches โ accepted
-
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 serverdst='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 requestqr=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