Skip to main content
AI & ML Techniques for Cyber Security

Domain Generation Algorithms and DNS-Based Threat Detection

Published: 2026-08-16
Level: postgraduate
Audience: Postgraduate students in Cyber Security and Machine Learning

Domain Generation Algorithms and DNS-Based Threat Detection

11.1 DNS Security Fundamentals

Why should you care about DNS? Every cyberattack that involves a remote server — from ransomware to botnets to data exfiltration — must somehow communicate over the network. That communication almost always starts with a DNS query. If you understand DNS, you hold the master key to detecting and disrupting malicious network activity.

DNS — the Domain Name System — is the fundamental fabric of internet communication. Every time a human types a domain name into a browser, DNS resolves that human-readable name into a machine-readable IP address. Machines talk in numbers (IPv4 addresses like 142.251.222.142, IPv6 addresses, MAC addresses), but humans remember words. DNS bridges that gap. Without it, humans and machines cannot interact over the network.

Think of DNS as the phone book of the internet. When you want to call a friend, you don't memorize their phone number — you look up their name in your contacts. DNS does the same thing for computers: it maps names humans understand (like google.com) to numbers machines understand (like 142.251.222.142). The analogy breaks down slightly because DNS is distributed across millions of servers worldwide rather than stored in one book, but the core relationship — name-to-number lookup — is identical.

Every connection on the internet starts with DNS. There is no alternative path. When you visit google.com, your browser silently performs a DNS lookup, gets the IP address, and connects. This happens thousands of times a day for any active user, and most people never notice it. The moment you open a browser, DNS is at work.

A quick way to interact with DNS directly is the nslookup command. Running nslookup google.com returns an IP address such as 142.251.222.142. The exact address may vary by region because Google runs high-availability infrastructure with multiple data centers worldwide. On Linux systems, the equivalent command is dig. Both allow forward lookups (domain to IP) and reverse lookups (IP to domain).

Worked example: NSLOOKUP in action. Open a terminal and run:

nslookup google.com

The output will look something like:

Server:  192.168.1.1
Address: 192.168.1.1

Non-authoritative answer:
Name:    google.com
Address: 142.251.222.142

The first two lines show your local DNS resolver (usually your router). The "Non-authoritative answer" means this result came from a cache, not directly from Google's authoritative DNS server. The Address line is the IP your browser will connect to. Try the same with facebook.com or github.com — each will return a different IP. On Linux, use dig google.com for a more detailed response that includes the TTL (time-to-live) value.

DNS operates on port 53, both for UDP and TCP queries. This is critical from a security standpoint: port 53 is almost always open on any network — office laptops, home routers, mobile phones. If port 53 were blocked, internet browsing would stop entirely. No organization blocks port 53 blanket-wide because the business impact would be total. Attackers know this. They exploit this always-open channel for malicious communication, because firewalls inherently trust DNS traffic.

Scope: Port 53 being always open is both DNS's greatest strength and its greatest vulnerability. It means DNS traffic bypasses most firewall rules by default. An attacker who tunnels malicious commands through DNS queries can often evade detection because security teams rarely inspect DNS payloads with the same rigor as HTTP or SSH traffic.

Q: What is DNS? How do we interact with it? A: DNS is an address book mapping domain names to IP addresses because humans cannot remember numbers. The nslookup command resolves domains to IPs. Registrars like GoDaddy handle domain registration.

11.1.1 DNS-Level Filtering and Security Tools

DNS can serve as a security control point. Services like OpenDNS (acquired by Cisco) provide public DNS servers that can filter traffic by category — blocking adult sites, gaming, known malicious domains, and more. Instead of using the ISP's default DNS or Google's 8.8.8.8, a user or organization can configure OpenDNS as the DNS resolver. All DNS queries then route through OpenDNS, which applies category-based filtering before resolving.

This filtering works at the DNS level: if a user tries to visit a blocked category, the DNS server refuses to resolve it or redirects to a block page. The configuration can happen at the individual device level or at the Wi-Fi router level, so all devices on the home network are protected.

A fun hands-on project for network monitoring is Pi-hole, a DNS-based ad and tracker blocker designed for Raspberry Pi. It acts as a local DNS server for your home network, intercepting all DNS queries and blocking requests to known advertising and tracking domains. It can filter out marketing noise across every device on the network — laptops, smart TVs, IoT sensors, even fridges. It is a practical exercise in DNS-level traffic monitoring and filtering.

How OpenDNS filtering works step by step:

  1. You configure your router's DNS to point to OpenDNS servers (208.67.222.222 and 208.67.220.220).
  2. When any device on your network requests a domain, the query goes to OpenDNS instead of your ISP.
  3. OpenDNS checks the domain against its category database.
  4. If the domain is in a blocked category (say, "gambling"), OpenDNS returns a block page IP instead of the real IP.
  5. The user sees a block page in their browser.
  6. If the domain is allowed, OpenDNS resolves it normally and returns the real IP.

The key insight: filtering happens before the connection is made, at the DNS resolution step. No actual traffic to the malicious server ever leaves your network.

11.1.2 DNS as an Attack Vector: Command and Control Communication

Because DNS is always open and trusted, attackers abuse it for command and control (C2) communication. A C2 server is a remote machine controlled by the attacker. Once a victim's system is compromised — through a malicious file, Excel macro, PDF, or phishing link — the malware needs to "phone home" to the attacker's server. It uses DNS as that phone line.

The C2 phase is nearly the last stage of the intrusion kill chain. Once C2 communication is established, the attacker can move to lateral movement, data exfiltration, or achieving their objectives. Blocking C2 is one of the final opportunities for defenders to stop an attack before real damage occurs.

The kill chain context: The intrusion kill chain (Lockheed Martin's model) describes the stages of a cyberattack: Reconnaissance → Weaponization → Delivery → Exploitation → Installation → Command & Control → Actions on Objectives. C2 is stage 6 of 7 — once it is established, the attacker is one step away from achieving their goal (stealing data, encrypting files, etc.). This is why detecting and blocking C2 is so critical: it is the last gate before real damage.

Q: What is command and control server communication? A: A small piece of code is first downloaded to the system, then it connects to the main server controlled by the attacker to download additional instructions and execute them. It is the last or penultimate stage of the kill chain before lateral movement and data exfiltration.

11.1.3 Evolution: IP to Domain Names

A naive attacker might hard-code an IP address into the malware:

C2_SERVER = "192.168.1.50"
# Send hello to C2
requests.get(f"http://{C2_SERVER}/hello")

Worked example: Why hard-coded IPs fail for attackers.

Suppose an attacker sets C2_SERVER = "192.168.1.50" in their malware. Here is what happens:

  1. The malware runs on a victim's machine and sends a request to 192.168.1.50.
  2. The corporate firewall logs the connection to 192.168.1.50.
  3. The security team checks threat intelligence feeds — 192.168.1.50 is flagged as known-bad.
  4. The firewall rule is updated: DENY all traffic to 192.168.1.50.
  5. Every machine in the organization is now blocked from reaching the C2.
  6. The attacker's entire botnet goes dark.

The attacker's only option: rewrite the malware with a new IP, re-weaponize a payload (new malicious Excel, PDF, etc.), re-send it to victims, and hope someone clicks again. This is a painful, slow cycle — exactly what defenders want.

This is easy to block. Corporate firewalls use allowlists — only approved IP addresses pass. Threat intelligence feeds flag known-bad IPs. If the IP is blocked, the entire botnet is disrupted. But this forces the attacker into a painful cycle: rewrite the malware, re-weaponize a new payload (malicious Excel, PDF, etc.), re-send it to victims, and hope someone clicks again. Each time the IP is blocked, the attacker must lift and shift their entire infrastructure to a new address.

The attacker's first improvement is replacing hard-coded IPs with domain names. Instead of 192.168.1.50, the malware contacts malicious-command.com. This decouples the malware from a specific IP address. If the defender blocks the IP that malicious-command.com resolves to, the attacker simply points the domain to a different IP. The malware code does not change. The initial payload does not change. The attacker's infrastructure becomes more resilient.

Worked example: Domain-based C2 resilience.

  1. Attacker registers malicious-command.com and points it to 10.0.0.50.
  2. Malware contacts malicious-command.com → DNS resolves to 10.0.0.50 → C2 established.
  3. Defender blocks 10.0.0.50 at the firewall.
  4. Attacker changes the DNS record: malicious-command.com now points to 10.0.0.99.
  5. Malware tries malicious-command.com again → DNS resolves to 10.0.0.99 → C2 re-established.
  6. No malware code changed. No re-weaponization needed. The attacker just updated a DNS record.

The defender can still block the domain itself, but identifying a cleverly chosen domain like send-flowers-for-charity.com is much harder than blocking a suspicious IP.

Defenders can still block the domain itself, but this requires identifying it first. A clever attacker chooses innocuous-looking domain names — send-flowers-for-charity.com — that raise no suspicion. Domain registration does require identity verification through registrars like GoDaddy, and domain takedowns are possible (organizations can request registrars to seize malicious domains). But these processes are slow and reactive.

This is the context in which Domain Generation Algorithms (DGA) emerged around 2008, representing a revolutionary leap in attacker creativity.

Recap: DNS is the always-open, always-trusted phone book of the internet. Attackers abuse it for C2 communication because port 53 is rarely blocked. The evolution from hard-coded IPs to domain names made attacker infrastructure more resilient — and set the stage for DGA, which we will explore next.

11.2 Anatomy of a Domain Name

Why study domain anatomy? To detect malicious domains, you first need to understand what makes a domain look normal. The structure of a domain name — its length, character patterns, TLD choice — contains hidden signals that machine learning can exploit. Understanding the anatomy is the foundation of feature engineering for DGA detection.

A fully qualified domain name (FQDN) has a hierarchical structure. Understanding this hierarchy is essential because each level carries different information and security implications.

The top-level domain (TLD) is the rightmost part: .com, .org, .edu, .ai, .in. Country-specific TLDs like .in (India) are common. TLDs are managed by the Internet Corporation for Assigned Names and Numbers (ICANN) and delegated to registries. The TLD tells you something about the domain's purpose or origin — .edu is reserved for educational institutions, .gov for government, .com for commercial entities.

The second-level domain is the actual registered name: google, facebook, bits-pilani. This is the part the domain owner chooses and registers through a registrar like GoDaddy, Namecheap, or Google Domains. The second-level domain is the primary identity of a website.

The subdomain sits to the left: app.secondize.co where app is the subdomain. Subdomains are controlled by the domain owner and can be created freely without additional registration. Further nesting into hostnames is possible but less common.

Anatomy of a complete domain name:

Consider mail.google.com:

  • TLD: .com (commercial)
  • Second-level domain: google (the registered name)
  • Subdomain: mail (the specific service — Gmail)

Consider www.cs.bits-pilani.ac.in:

  • TLD: .in (India country code)
  • Second-level domain: bits-pilani (the registered name)
  • Subdomain: www.cs (nested — cs is a subdomain of bits-pilani, and www is a subdomain of cs)
  • Note: .ac.in is a second-level TLD for academic institutions in India

The key insight: the further left you go, the more specific the location. The TLD tells you the country or category; the second-level domain tells you the organization; subdomains tell you the specific service or department.

Suspicious TLDs exist — .tk, .ml, .ga, .cf — often free and associated with countries that lack robust domain registration infrastructure. Attackers exploit these because registration requires minimal or no identity verification. This is one clue among many: a domain on a suspicious TLD warrants extra scrutiny, though it is not proof of malice.

Assumption: A suspicious TLD is a signal, not a verdict. Legitimate websites do use .tk and .ml TLDs. The TLD is one feature among many — it gains predictive power only when combined with other features like domain length, entropy, and registration age.

11.2.1 Normal vs. Malicious Domain Characteristics

Legitimate domains follow human-centered design principles. They are short and memorable because the owner wants people to visit, remember, and return. They use real wordsgoogle, facebook, amazon — and have low entropy, meaning they are pronounceable and not random gibberish. They use common TLDs like .com, .co.in, .org, .edu.

The professor's core insight: legitimate domains want people to remember them; malicious domains by design do not want to be remembered. This asymmetry is itself a detection signal. A domain that is hard for humans to remember or type is suspicious — because why would a legitimate business choose a name nobody can recall?

Malicious domains exhibit the opposite traits. They have high entropy — strings like xj8kf92bc7a.net that no human would remember or type intentionally. They use typosquatting — domains that visually mimic legitimate ones: g00gle.com (zeros instead of o's), amaz0n.com, fl1pkart.com. To a trained eye, these are obvious. To an untrained user — parents, elderly relatives, casual internet users — they are convincing enough, especially when presented with an attractive offer.

Typosquatting in action:

Legitimate Typosquatted Trick used
google.com g00gle.com Replacing 'o' with '0' (zero)
amazon.com amaz0n.com Replacing 'o' with '0'
flipkart.com fl1pkart.com Replacing 'i' with '1'
paypal.com paypa1.com Replacing 'l' with '1'
microsoft.com micr0soft.com Replacing 'o' with '0'

These tricks exploit the visual similarity between letters and numbers. A user clicking a link in a phishing email might not notice the substitution, especially on a small phone screen.

The key takeaway: these observable differences — length, entropy, character patterns, TLD choice — are features that machine learning can exploit to classify domains as benign or malicious. This is the foundation of feature engineering for DGA detection, which we will explore in detail in section 11.5.

Recap: Domain names have a hierarchical structure (TLD → second-level domain → subdomain). Legitimate domains are short, memorable, and use common TLDs. Malicious domains are long, high-entropy, use suspicious TLDs, and employ typosquatting. These observable differences become the features that ML models use to detect DGA domains.

11.3 Command and Control (C2) Communication

Why does C2 matter so much? In the kill chain, C2 is the stage where the attacker gains persistent control over the compromised machine. Without C2, the initial compromise is a one-shot event — the malware runs, does its thing, and that's it. With C2, the attacker can adapt, escalate, and achieve objectives that require ongoing communication. C2 is the difference between a firecracker and a remote-controlled drone.

The C2 phase represents a critical juncture in the attack lifecycle. The compromised machine — whether a laptop, server, phone, or IoT device like a street camera — must reach the attacker's remote server to receive additional instructions.

The initial attack vector is always lightweight. No attacker delivers a 100MB malware file; that would trigger every detection system. The initial payload is a small script or macro — just enough to establish a connection. Once connected, the malware downloads additional instructions: how to move laterally in the network, how to steal data, how to update its own capabilities, how to coordinate with other compromised machines (botnets), or how to distribute attack instructions.

Why small initial payloads? Detection systems flag large file transfers, unusual download volumes, and unknown executables. A 100MB download from a suspicious source raises alarms. But a 50KB script embedded in an Excel macro? That passes through email filters, antivirus scans, and network monitoring without triggering alerts. The attacker's art is in minimizing the footprint of the initial compromise and expanding capabilities only after C2 is established.

The Mirai botnet is a canonical example. Hundreds of thousands of internet-connected street cameras were compromised because they had minimal security — many still used default passwords like admin:admin. These cameras were then used to launch a massive distributed denial-of-service (DDoS) attack against GitHub and other popular sites in October 2016. The coordination between these compromised cameras happened through C2 communication. The Mirai botnet demonstrated that IoT devices — cameras, routers, DVRs, smart home gadgets — are prime targets because they run minimal operating systems, rarely receive security updates, and are always connected to the internet.

How the Mirai botnet worked:

  1. Scanning: The malware scanned the internet for IoT devices with open Telnet ports (port 23).
  2. Brute force: It tried a list of 62 default username/password combinations (like admin:admin, root:root, guest:guest).
  3. Infection: Upon successful login, the malware downloaded a small binary to the device.
  4. C2 registration: The compromised device reported back to the C2 server and waited for commands.
  5. Attack launch: The botnet operator issued DDoS commands, and hundreds of thousands of devices simultaneously flooded the target with traffic.
  6. Scale: At its peak, Mirai generated over 1 Tbps of DDoS traffic — enough to take down major websites.

The key lesson: these cameras had no business communicating with GitHub. If DNS queries from these devices had been monitored, the C2 communication could have been detected and blocked.

11.3.1 The Attacker's Evolution: A Cat-and-Mouse Game

The evolution from IP-based to domain-based C2 illustrates the fundamental cat-and-mouse dynamic of cybersecurity. Each stage represents an escalation in the arms race between attackers and defenders.

The professor's core principle: Defenders must succeed every single time; attackers only need to succeed once. This asymmetry defines cybersecurity. A defender who blocks 99 out of 100 attack attempts has still failed. The attacker who finds one open door has won. This is why detection systems must be comprehensive and why C2 blocking is so critical — it is one of the few places where a single defensive action (blocking a domain or IP) can neutralize an entire botnet.

Stage 1 — Hard-coded IP: The malware contacts a fixed IP address. Defenders block it. The attacker must rewrite the malware, re-weaponize, re-deliver, and hope for another click. Painful for the attacker.

Stage 2 — Domain name: The malware contacts a domain name. If the IP behind the domain is blocked, the attacker points the domain to a new IP. No malware changes needed. The attacker's infrastructure is more resilient.

Stage 3 — DGA: The malware dynamically generates hundreds of domain names per day using a predetermined algorithm. The attacker registers only one or two. The malware tries all generated domains; whichever is registered succeeds. Defenders cannot block what they cannot predict. This is where DGA enters the picture.

Each stage raises the cost of defense and lowers the cost of attack. The pyramid of pain framework (by David Bianco, 2013) underlies this progression. The pyramid classifies indicators of compromise (IOCs) by how difficult they are for attackers to change:

Indicator type Difficulty for attacker to change Example
Hash values Trivial — change one byte MD5/SHA of malware file
IP addresses Easy — get a new server 192.168.1.50
Domain names Moderate — register new domain malicious-command.com
Network/host artifacts Hard — change behavior patterns C2 protocol, User-Agent string
Tools Very hard — rewrite software Custom malware framework
Tactics, techniques, procedures (TTPs) Hardest — change methodology Social engineering approach

Blocking IPs is easy (low on the pyramid). Blocking domains is harder. Blocking algorithm-generated domains requires understanding the algorithm itself — which is why DGA detection is a research problem that requires machine learning.

Recap: C2 communication is the attacker's lifeline to a compromised machine. The evolution from hard-coded IPs to domains to DGAs represents a cat-and-mouse escalation where each stage makes defense harder. The pyramid of pain framework shows why DGA detection is so challenging — you must understand the algorithm, not just block individual indicators.

11.4 Domain Generation Algorithms (DGA)

What problem does DGA solve for attackers? If the defender blocks your C2 domain, you lose control of your botnet. You need a way to generate thousands of potential C2 domains so that even if the defender blocks some, others remain available. DGA is that solution — it is an algorithm that both the malware and the attacker can run independently to produce the same list of domains, without ever communicating that list over the network.

A Domain Generation Algorithm (DGA) is software that algorithmically generates large numbers of domain names according to a predetermined rule. The attacker pre-registers only a small subset of these domains. The malware, running on the victim's machine, generates the same set of domains using the same algorithm and attempts to contact each one. Whichever domain the attacker has registered will respond, establishing the C2 channel.

The genius of DGA is that the domain list is never transmitted. Both sides — the malware and the attacker — compute it independently using the same algorithm and seed. Even if defenders intercept all network traffic, they cannot extract the full domain list from the malware's communications alone. They must reverse-engineer the algorithm.

How DGA works — the core mechanism:

  1. Both the malware and the attacker agree on an algorithm (hardcoded in the malware binary) and a seed (typically the current date).
  2. Each day, both independently compute the same list of 250+ domains.
  3. The attacker registers 1-2 of these domains.
  4. The malware tries all 250 domains in sequence.
  5. The registered domain responds → C2 channel established.
  6. The next day, the process repeats with a new seed → 250 new domains.

The critical property: determinism. Given the same seed, the algorithm always produces the same output. This is what allows the malware and attacker to synchronize without communication.

11.4.1 How DGA Works: The Conficker Example

The Conficker worm (circa 2008-2009) was one of the first large-scale DGA-based attacks. It impacted 9 to 15 million computers — one of the largest botnets ever created. Conficker's algorithm generated approximately 250 domains per day, each 6 to 12 characters long, with high entropy and using multiple TLDs (.com, .net, .org, .info, .biz). The attacker registered only one or two of these 250 domains each day.

The malware on each compromised machine would try contacting all 250 generated domains. The one or two that were registered would respond, and the C2 channel would be established. By the time defenders identified the malicious domain and added it to their blacklist, the next day brought 250 new domains. Previous blacklists became useless.

Worked example: Conficker DGA in action.

Suppose today is January 15, 2009. The Conficker algorithm uses the date as its seed.

Step 1 — Domain generation: The algorithm produces 250 domains like:

kq3mfa7bvx.net
j8xkp2nrtq.com
hb4vq9mzfw.org
... (247 more)

Step 2 — Attacker registration: The attacker picks 2 domains (say kq3mfa7bvx.net and j8xkp2nrtq.com) and registers them, pointing them to the C2 server.

Step 3 — Malware tries all 250: The malware on each infected machine iterates through the list:

  • kq3mfa7x.net → DNS fails (not registered) → skip
  • j8xkp2nrtq.com → DNS resolves → connects → C2 established
  • ... remaining 248 domains tried but not needed

Step 4 — Defender response: The security team identifies j8xkp2nrtq.com as malicious and adds it to their blacklist.

Step 5 — Next day (January 16): The seed changes (new date), producing 250 completely different domains. Yesterday's blacklist is useless. The cycle repeats.

The asymmetry:

  • Attacker cost: Generate 250 domains (a simple loop) + register 1-2 (~USD 10/day)
  • Defender cost: Analyze 250 domains, confirm which are malicious, update all blacklists, deploy to all endpoints — and they must do this correctly every single day

This is the core asymmetry: attackers generate 250 domains cheaply (it is just a loop in code), but defenders must analyze, confirm, and block each one — and they must succeed every single time. The attacker only needs one open door.

Q: 250 domains are created and only 2-3 are registered — how does that give economic advantage to attackers? A: The malware generates all 250 domains and tries each one. The attacker registers only 1-2. By the time the defender identifies and blocks the malicious domain, the next day brings 250 new domains and old blacklists are useless. The asymmetry is: attackers generate cheaply (just a loop), defenders must analyze and block each one and succeed every time.

11.4.2 Types of DGA

DGAs differ in how they generate domain strings. The type of DGA determines how easy or hard it is to detect.

Arithmetic DGA: Uses mathematical operations on a seed value to generate domain strings. A simple example: take a seed, add incrementing numbers, convert to characters, and append a TLD.

Worked example: Arithmetic DGA pseudocode.

import hashlib
import datetime

def arithmetic_dga(seed_date, count=250):
    domains = []
    seed = int(seed_date.strftime("%Y%m%d"))  # e.g., 20090115
    for i in range(count):
        # Arithmetic transform: multiply, add offset, take modulo
        value = (seed * 1103515245 + 12345 + i) % (2**31)
        # Convert to a string of characters
        domain = ""
        temp = value
        length = 6 + (temp % 7)  # domain length between 6 and 12
        for j in range(length):
            domain += chr(97 + (temp % 26))  # a-z
            temp //= 26
        domain += ".net"  # append TLD
        domains.append(domain)
    return domains

# Both attacker and malware run this with the same date:
today = datetime.date(2009, 1, 15)
domains = arithmetic_dga(today)
# domains = ["kq3mfa.net", "j8xkp2.net", "hb4vq9.net", ...]

The key property: given the same seed_date, both the attacker and the malware produce the exact same list. No communication needed.

Dictionary-based DGA: Selects words from a predefined dictionary and combines them. Domains like cloud-connect-secure.com or protect-verify-online.net look meaningful and pass visual inspection. These are harder to detect than random-character DGAs because they have lower entropy, appear legitimate to analysts, pass pronunciation tests, and blend with legitimate traffic.

Worked example: Dictionary-based DGA.

Suppose the malware carries a word list:

["cloud", "connect", "protect", "secure", "verify", "online", "service", "data"]

The algorithm selects words based on the seed:

  • Seed 20090115 → picks indices [0, 1, 3]cloud-connect-secure.com
  • Seed 20090116 → picks indices [2, 4, 6]protect-verify-service.net

These domains look like legitimate business names. A human analyst glancing at cloud-connect-secure.com would not immediately flag it as malicious. This is precisely why dictionary-based DGAs are harder to detect — they pass the "eye test" that random-character DGAs fail.

Q: Why are dictionary-based DGAs harder to detect than random character DGAs? A: Random characters have high entropy and are easier to catch. Dictionary-based domains have meaningful words, lower entropy, pass pronunciation tests, and blend with legitimate traffic. They look legitimate to analysts and require more sophisticated features to detect.

Hash-based DGA: Applies a cryptographic hash function (SHA-256, MD5) to a seed value, then encodes the hash output as a domain string. Since hash functions are deterministic — the same input always produces the same output — both the malware and the attacker can independently generate the same set of domains without communicating.

Worked example: Hash-based DGA.

import hashlib

def hash_dga(seed, count=250):
    domains = []
    current = seed.encode()
    for i in range(count):
        hash_val = hashlib.sha256(current).hexdigest()
        # Take first 8 characters as domain name
        domain = hash_val[:8] + ".com"
        domains.append(domain)
        # Chain: use hash output as next input
        current = hash_val.encode()
    return domains

# Both sides compute:
domains = hash_dga("2009-01-15")
# domains = ["a3f2b1c9.com", "d4e5f6a7.com", ...]

Hash-based DGAs produce domains that look random (high entropy) but are reproducible. The chaining property means even knowing one domain does not easily reveal the next — you need the full hash chain.

Word list permutation: The malware carries a finite list of words and permutes them to generate domain names. The permutations are deterministic given the same word list and ordering rule.

The key insight about dictionary-based DGAs: they are harder to detect than random-character DGAs precisely because they look legitimate. Random strings have high entropy and are easy to flag. Dictionary-based domains have lower entropy, real English words, and pass all superficial tests. They require more sophisticated feature engineering to detect.

Pitfall: Assuming all DGAs produce random-looking domains. This was true for early DGAs like Conficker (2008), but modern DGAs use dictionary-based approaches that produce domains indistinguishable from legitimate business names. A detection system that only looks for high entropy will miss these entirely. This is why feature engineering must include linguistic features (pronounceability, word patterns) and not just statistical ones (entropy, character distribution).

11.4.3 Real-World DGA Families

Conficker (2008): 250 domains/day, 6-12 character length, high entropy, multiple TLDs. The first major DGA-based botnet. Infected 9-15 million machines worldwide. Used arithmetic DGA with the date as seed.

CryptoLocker (2013): Ransomware that chained DGA with encryption attacks. Generated up to 1,000 domains per day — a significant escalation in volume. The ransomware encrypted victims' files and demanded Bitcoin payment. DGA ensured the C2 channel remained available even as defenders tried to block it.

Zeus (2014): Evolved to use advanced peer-to-peer protocols with hybrid DGAs and multi-level C2 structures, making detection even harder. Zeus variants used both arithmetic and dictionary-based DGAs, and incorporated P2P communication so that even if the C2 server was taken down, the botnet could self-heal.

The evolution timeline shows continuous escalation: 2008 Conficker → 2013 CryptoLocker → 2014 Zeus → 2016+ modern DGAs with domain shadowing and other advanced techniques. Researchers continue to study novel DGA families as they emerge.

Think like an attacker: The professor emphasized this repeatedly — "you must think like the bad folk to understand what they would do next." If you were an attacker and defenders were blocking your random-character DGAs, what would you do? You would make your domains look more like legitimate ones — which is exactly what dictionary-based DGAs do. Understanding the attacker's motivation is essential for predicting and defending against future evolution.

The key takeaway: DGAs evolved from simple random generation to sophisticated dictionary-based attacks that mimic legitimate domain patterns. Each evolutionary step was driven by defenders successfully blocking the previous generation — a classic cat-and-mouse escalation.

Recap: DGA is the attacker's solution to the domain-blocking problem. By algorithmically generating hundreds of domains per day, attackers shift the economic burden to defenders. Types include arithmetic, dictionary-based, hash-based, and permutation-based DGAs. Real-world families (Conficker, CryptoLocker, Zeus) show continuous escalation in sophistication. Dictionary-based DGAs are the hardest to detect because they look legitimate.

11.5 Feature Engineering for Domain Detection

Why is feature engineering so important? The professor's advice: "Spend more time on problem definition so your solution becomes clearer if not easier." In DGA detection, the choice of features often matters more than the choice of model. A well-designed feature set with a simple model can outperform a sophisticated deep learning model on raw data. Feature engineering is where domain expertise meets data science.

Machine learning models cannot process raw domain strings directly. Feature engineering is the critical step that transforms a domain name into a numerical representation that captures the characteristics distinguishing benign from malicious domains. The quality of feature engineering often matters more than the choice of model.

What is feature engineering? It is the process of extracting meaningful numerical properties (features) from raw data (domain strings) that a machine learning model can use for classification. For a domain like xj8kf92bc7a.net, features might include: length = 12, digit count = 4, entropy = 3.2, vowel ratio = 0.0, contains real word = false. Each feature captures one aspect of what makes a domain look benign or malicious.

The professor emphasized that effective feature engineering requires thinking about the problem from multiple angles. No single feature separates good from bad domains — the power comes from combining features from different perspectives: lexical, linguistic, statistical, and DNS metadata.

11.5.1 Lexical Features

These operate at the character level of the domain string. They are the simplest features to compute and capture basic structural differences between benign and malicious domains.

  • Domain length: Malicious domains tend to be longer or follow unusual length patterns. Legitimate domains are typically short (google.com = 10 characters) while DGA domains are often longer (xj8kf92bc7a.net = 14 characters).
  • Digit count: Count of numeric characters in the domain. Legitimate domains are predominantly alphabetic. A domain like xj8kf92bc7a has a high digit-to-letter ratio — a red flag.
  • Alphabet-to-number ratio: The proportion of alphabetic to numeric characters. Mixed alphanumeric patterns in unusual ratios are suspicious. A legitimate domain like google.com has a ratio of 10:0 (all letters). A DGA domain like xj8kf92bc7a has a ratio of 7:4 (significantly more numbers).
  • Special character analysis: Hyphens, underscores, and their positions. Legitimate domains use hyphens sparingly (e.g., bits-pilani.ac.in). DGA domains rarely use hyphens because they add complexity without benefit to the algorithm.

Comparing lexical features:

Feature google.com xj8kf92bc7a.net
Domain length 10 14
Digit count 0 4
Letter count 10 8
Digit-to-letter ratio 0.0 0.5
Hyphen count 0 0
Has real words Yes ("google") No

The lexical features alone can separate these two domains with high confidence.

11.5.2 Entropy

Entropy measures the randomness of a string. In information theory, Shannon entropy quantifies the average information content per character. For a string of characters, the formula is:

where is the frequency of character in the string. Higher entropy means more randomness.

Google.com has low entropy — it is a recognizable English word where certain characters (like 'o') repeat predictably. a8f3x92bc7d.net has high entropy — it appears random, with each character appearing roughly equally often. A simple entropy calculation over the character distribution of the domain can distinguish legitimate from malicious domains with reasonable accuracy. This feature was one of the earliest and most intuitive signals for DGA detection.

Entropy comparison:

For google (6 characters): character frequencies are g=1, o=2, l=1, e=1

For a8f3x92bc7d (11 characters): each character appears once, so:

The DGA domain has ~54% higher entropy. This gap widens with longer domains.

Pitfall: Dictionary-based DGAs have low entropy. Entropy alone cannot detect dictionary-based DGAs like cloud-connect-secure.com because these domains use real English words and have entropy similar to legitimate domains. This is why entropy must be combined with other features — it catches random-character DGAs but misses dictionary-based ones.

11.5.3 N-gram Analysis

Unigrams are individual characters, bigrams are pairs of consecutive characters, trigrams are triples. N-gram frequency distributions capture character-level language patterns.

English text has characteristic n-gram distributions — common bigrams like "th", "he", "in", "er", "an", common trigrams like "the", "and", "ing", "ion". Legitimate domain names, being English words, follow these distributions. DGA domains, especially random-character ones, have unusual n-gram frequencies that deviate sharply from English norms.

N-gram analysis in practice:

For google:

  • Bigrams: go, oo, og, gl, le → "oo" appears twice (unusual for English but common in this word)
  • Trigrams: goo, oog, ogl, gle → all contain common English patterns

For a8f3x92bc7d:

  • Bigrams: a8, 8f, f3, 3x, x9, 92, 2b, bc, c7, 7d → none are common English bigrams
  • Trigrams: a8f, 8f3, f3x, 3x9, x92, 92b, 2bc, bc7, c7d → none are common English trigrams

The n-gram entropy of "google" will be low and predictable. The n-gram entropy of a8f3x92bc7d will be high and anomalous. This analysis can be extended to 4-grams, 5-grams, and beyond for finer-grained discrimination.

11.5.4 Linguistic Features

These test whether a domain name behaves like a real word in a human language. They are particularly effective against random-character DGAs but less effective against dictionary-based DGAs.

  • Pronounceability: Can the domain be spoken aloud? Legitimate domains are almost always pronounceable. A vowel-consonant pattern analysis can score how "speakable" a string is. Random DGA strings fail this test. A simple heuristic: count the ratio of vowels (a, e, i, o, u) to consonants. English words typically have a vowel ratio of 0.3-0.5. A domain like xj8kf has zero vowels — immediately suspicious.
  • Vowel-consonant patterns: English words follow predictable vowel-consonant alternations. Strings like xj8kf have no vowels at all — immediately suspicious.
  • Real word detection: Does the domain contain recognizable English words? Dictionary-based DGAs pass this test, which is precisely why they are harder to detect.
  • Consonant clusters: Unusual consonant clusters (like "xjkf") that never appear in natural language are strong signals. English rarely has more than 2-3 consecutive consonants without a vowel.

Linguistic feature comparison:

Feature google xj8kf92bc7d cloud-connect-secure
Pronounceability High Very low High
Vowel ratio 0.5 (2/6) 0.0 (0/11) 0.35 (5/14)
Contains real word Yes No Yes (3 words)
Max consonant cluster 1 ("gl") 4 ("xj8kf") 2 ("ct", "nc")

Notice how cloud-connect-secure (a dictionary-based DGA domain) looks almost identical to legitimate domains on linguistic features. This is why detecting dictionary-based DGAs requires additional features beyond linguistics.

11.5.5 Statistical Features

  • Comparison to English norms: Calculate how far the domain's character distribution deviates from standard English letter frequencies (e, t, a, o, i, n, s, h, r are the most common). English text has a well-known frequency distribution where 'e' appears ~13% of the time, 't' ~9%, 'a' ~8%, and so on. DGA domains have a flatter distribution where characters appear more uniformly.
  • Deviation metrics: Statistical measures (chi-squared test, KL divergence) of how anomalous the distribution is compared to English.
  • Consecutive character patterns: Repeated characters, sequential digits, or other patterns unusual in legitimate domains. For example, aaa or 123 in a domain are suspicious.

Chi-squared test for character distribution:

The chi-squared statistic measures how far observed character frequencies deviate from expected (English) frequencies:

where is the observed frequency of character and is the expected frequency based on English norms.

  • google: Low chi-squared (character frequencies roughly match English norms)
  • xj8kf92bc7d: High chi-squared (characters like 'x', 'j', 'k', '8', '9' are rare in English)

This feature captures the "does this look like English?" question quantitatively.

11.5.6 DNS Metadata Features

Beyond the domain string itself, metadata from DNS registration provides powerful signals that complement lexical and linguistic features.

  • TLD analysis: Certain TLDs (.tk, .ml, .ga, .cf) are disproportionately associated with malicious activity because they are free and have minimal registration verification. A domain on one of these TLDs warrants extra scrutiny.
  • Domain registration age: Newly registered domains are far more likely to be malicious than domains with years of history. Many security systems refuse to trust domains younger than a threshold (e.g., 30 days). DGAs generate new domains daily, so DGA domains are almost always newly registered.
  • WHOIS data: Registration details — registrant name, address, email — can reveal patterns.

The PIN code anecdote: Early DGA attackers randomized all registration fields (names, addresses, emails) to avoid correlation across domains. But they made one mistake: they reused the same postal code (PIN code) across multiple domains. This single repeated field allowed researchers to link the domains to a common actor and take them down. The lesson: attackers who think they are being clever often overlook small details. Feature engineering that captures these details can be surprisingly effective.

The key takeaway: effective feature engineering combines multiple perspectives — lexical, linguistic, statistical, and DNS metadata. No single feature is sufficient. The combination of features from different angles is what makes detection robust.

The professor's core warning: "No single feature separates good from bad — effective feature engineering combines lexical, linguistic, statistical, and DNS metadata perspectives." A detection system that relies on entropy alone will miss dictionary-based DGAs. A system that relies on linguistic features alone will miss random-character DGAs. The robustness comes from the combination — this is why ensemble methods (Random Forest, Gradient Boosting) that combine multiple features tend to perform well on DGA detection tasks.

Recap: Feature engineering transforms raw domain strings into numerical features for ML models. Four categories of features capture different aspects: lexical (length, digit count), linguistic (pronounceability, vowel patterns), statistical (entropy, n-gram analysis, English deviation), and DNS metadata (TLD, registration age, WHOIS). The combination of all four is what makes detection robust — no single feature is sufficient.

11.6 Machine Learning Detection Approaches

The problem definition mindset: The professor's advice applies directly here — "Spend more time on problem definition so your solution becomes clearer if not easier." Before choosing a model, you must define the problem precisely: What is the input? What is the output? What are the evaluation criteria? How do you handle class imbalance? These decisions shape everything that follows.

11.6.1 Problem Definition

The first step is defining the problem precisely. The simplest formulation is binary classification: given a domain name, classify it as malicious (DGA) or benign (legitimate). The input is the feature vector extracted from the domain (length, entropy, n-gram frequencies, linguistic scores, etc.), and the output is a binary label: 0 (benign) or 1 (malicious). This is the most common and practical formulation.

A more nuanced formulation would classify the DGA family (Conficker vs. CryptoLocker vs. Zeus), but binary detection is the starting point. Multi-class classification is harder because each DGA family has different generation characteristics, and new families emerge constantly.

The classification pipeline:

Raw domain string
    ↓
Feature extraction (lexical, linguistic, statistical, metadata)
    ↓
Feature vector [length, entropy, vowel_ratio, ngram_entropy, ...]
    ↓
ML model (Random Forest, Gradient Boosting, LSTM, etc.)
    ↓
Output: benign (0) or malicious (1)

The model never sees the raw string — it only sees the numerical feature vector. This is why feature engineering is so critical: the model's performance is bounded by the quality of the features it receives.

11.6.2 Data Sources

Benign domains: The Alexa top 1 million websites list provides known-good domains. This is a research dataset (not the voice assistant) that ranks the most popular websites globally. The CSV is freely available on GitHub. Using popular websites as benign samples works because DGA domains are unlikely to appear in the top million — they are too short-lived and niche.

Malicious domains: DGA feed datasets provide known-malicious domain samples. One example mentioned is a dataset of 31,000 DGA domain samples from December 2024. Additional sources include security research repositories and threat intelligence platforms.

Class imbalance: A typical dataset might have 1 million benign domains but only 2,000-31,000 malicious ones. This class imbalance must be addressed through sampling techniques:

  • Oversampling the minority class: Duplicate or synthetically generate more malicious samples. SMOTE (Synthetic Minority Over-sampling Technique) creates synthetic malicious domains by interpolating between existing ones.
  • Undersampling the majority class: Randomly remove benign domains to balance the dataset. This loses data but can improve training speed.
  • Class weighting: Assign higher misclassification cost to the minority class during training, so the model penalizes missing a DGA domain more heavily than misclassifying a benign one.

Why class imbalance matters in security: If 99% of domains are benign and 1% are malicious, a model that always predicts "benign" achieves 99% accuracy — but catches zero DGA domains. This is the accuracy trap that the professor warned about. In security contexts, false negatives (missing a DGA domain) are far more costly than false positives (flagging a legitimate domain). Evaluation metrics must reflect this: precision, recall, F1, and AUC are more informative than raw accuracy.

11.6.3 Traditional ML Pipeline

The standard pipeline is: raw domains → feature extraction → train-test split → model training → evaluation → deployment.

Recommended algorithms:

  • Random Forest: High accuracy, handles mixed feature types well, relatively interpretable through feature importance. An ensemble of decision trees that votes on the final classification. Each tree sees a random subset of features and data, reducing overfitting.
  • Gradient Boosting: Often the highest accuracy among traditional ML methods. Builds trees sequentially, where each new tree corrects the errors of the previous ones. Handles complex feature interactions well.
  • Logistic Regression: More interpretable — useful when you need to explain why a domain was flagged. Outputs a probability score that can be thresholded.
  • SVM: Effective in high-dimensional feature spaces, especially when the domain name length varies greatly. Finds the optimal hyperplane that separates benign from malicious domains.

How Random Forest works for DGA detection:

  1. Training: Build 100 decision trees, each trained on a random subset of the data and features.
  2. Feature importance: Each tree ranks features by how well they split the data. Entropy might be the top feature in 80 trees, vowel ratio in 65 trees, domain length in 60 trees.
  3. Classification: For a new domain, each tree votes "benign" or "malicious." The majority vote wins.
  4. Advantage: If one feature fails (e.g., entropy is low for a dictionary-based DGA), other features (linguistic patterns, n-gram analysis) can still catch it. The ensemble is more robust than any single tree.

Feature importance from Random Forest also tells you which features matter most — a valuable insight for improving feature engineering.

Evaluation metrics: Precision, recall, F1 score, area under the ROC curve (AUC), and confusion matrix analysis. Since the cost of missing a malicious domain (false negative) is much higher than flagging a benign one (false positive), recall is often prioritized over precision.

Evaluation metrics explained in the security context:

  • Precision = TP / (TP + FP) — Of all domains flagged as DGA, how many are actually DGA? High precision means few false alarms.
  • Recall = TP / (TP + FN) — Of all actual DGA domains, how many did we catch? High recall means few missed threats.
  • F1 = 2 × (Precision × Recall) / (Precision + Recall) — Harmonic mean balancing both.
  • AUC — Area under the ROC curve. Measures discrimination ability across all thresholds. AUC = 1.0 is perfect; AUC = 0.5 is random guessing.

In security, recall is king. Missing one DGA domain (false negative) could mean a botnet maintains C2. Flagging a legitimate domain (false positive) causes minor inconvenience. The professor's guidance: "When answering evaluation metrics questions, go beyond accuracy — explain why each metric matters in the security context."

11.6.4 N-gram Classification

Instead of hand-crafting features, n-gram frequency vectors can be used directly as input to classifiers. The domain string is decomposed into character n-grams (unigrams, bigrams, trigrams), and the frequency distribution of these n-grams becomes the feature vector. This captures language-specific patterns without manual feature engineering.

N-gram feature vector for google:

N-gram Count
g 1
o 2
l 1
e 1
go 1
oo 1
og 1
gl 1
le 1
goo 1
oog 1
ogl 1
gle 1

This sparse vector (most n-grams have count 0) becomes the input to a classifier. DGA domains will have very different n-gram distributions — more uniform, less English-like.

11.6.5 Deep Learning Approaches

Deep learning offers automatic feature learning — the model learns relevant features from the data rather than requiring manual specification. This is particularly powerful for DGA detection because:

  • Sequential patterns: DGAs that use time-based generation have inherent sequences. Recurrent Neural Networks (RNNs), especially LSTM (Long Short-Term Memory) networks, naturally capture sequential dependencies. An LSTM processes the domain character by character, maintaining a hidden state that encodes what it has seen so far.
  • Local patterns: Convolutional Neural Networks (CNNs) can capture local character patterns — recurring substrings, word fragments — that are characteristic of specific DGA families. CNNs scan the domain with sliding windows (filters) that detect specific character patterns.
  • Generalization: Deep learning models can generalize to novel DGA families better than models trained on hand-crafted features, because they learn the underlying structure rather than memorizing specific feature thresholds.

Character-level RNN/LSTM architecture: The domain string is fed character by character. Each character is embedded into a vector (a learned representation). The embedding passes through one or more LSTM layers. A dense layer produces the final classification probability (DGA vs. benign). This end-to-end approach eliminates the need for manual feature engineering.

LSTM processing of google:

Input:  g → o → o → g → l → e
         ↓    ↓    ↓    ↓    ↓    ↓
Embed:  [v1] [v2] [v3] [v4] [v5] [v6]
         ↓    ↓    ↓    ↓    ↓    ↓
LSTM:   h1 → h2 → h3 → h4 → h5 → h6
                                ↓
                          Dense layer
                                ↓
                          P(DGA) = 0.02 → benign

Each character is converted to an embedding vector. The LSTM processes them sequentially, building up a representation that encodes the character patterns. The final hidden state (h6) is passed to a dense layer that outputs the probability of the domain being DGA.

CNN for DGA detection: CNNs scan the domain string with convolutional filters, capturing local patterns (common character sequences, word fragments). They are faster to train than RNNs and effective at detecting dictionary-based DGAs where specific word patterns repeat.

Hybrid models: Combining CNN (for local pattern extraction) with LSTM (for sequential modeling) often yields the best results. The CNN captures local features, and the LSTM captures how those features evolve across the domain string.

Attention mechanisms: Attention layers allow the model to focus on the most important parts of the domain string. For example, the attention mechanism might highlight suspicious character sequences or unusual patterns that are most discriminative. This is the same mechanism used in large language models (LLMs).

Transfer learning: If a large corpus of DGA domains is available, a model can be pre-trained on the general DGA detection task and then fine-tuned for specific families (Conficker, Zeus, etc.). This allows rapid adaptation to new threats.

Pitfalls of deep learning for DGA detection:

  1. The accuracy trap: Deep learning can achieve 98-99% accuracy on character-level DGA detection. But examine the confusion matrix: if the dataset has 99% benign domains, a model that always predicts "benign" gets 99% accuracy. Precision, recall, F1, and AUC matter more than raw accuracy.
  1. Interpretability: When a deep learning model flags a domain as DGA, explaining why is difficult. In a security operations center, analysts need to understand why an alert was generated. Random Forest can say "this domain was flagged because of high entropy and unusual n-gram patterns." LSTM says "the model's internal state activated" — much less useful for human decision-making.
  1. Data hunger: Deep learning requires large datasets (thousands to millions of samples) to learn effectively. Traditional ML with hand-crafted features can work well with smaller datasets.
  1. Computational cost: Training deep learning models requires GPUs and significant compute time. Inference can also be slower than traditional ML, which matters for real-time detection at millisecond latency.

Recap: DGA detection is a binary classification problem. Traditional ML (Random Forest, Gradient Boosting) with hand-crafted features is the baseline. Deep learning (LSTM, CNN, hybrid) offers automatic feature learning and better generalization but sacrifices interpretability. Class imbalance must be addressed. Evaluation must go beyond accuracy to precision, recall, F1, and AUC — in security, recall is often the most critical metric.

11.7 Real-Time Detection Systems

The deployment gap: Training a model in a Jupyter notebook is one thing. Deploying it to classify thousands of DNS queries per second in a live network is a completely different engineering challenge. This section bridges that gap — from research prototype to production system.

11.7.1 Detection Pipeline

A production DGA detection system follows this pipeline:

  1. DNS traffic capture: Passively capture DNS queries from network traffic or DNS server logs. This is typically done at the network level using tools like tcpdump, Wireshark, or by tapping into DNS server logs directly. The capture must be passive — it should not introduce latency into the DNS resolution process.
  1. Domain extraction: Parse the DNS logs to extract individual domain names. DNS queries contain the full domain name being requested. Extract the second-level domain and TLD for analysis (strip subdomains to reduce noise).
  1. Feature extraction: Compute lexical, linguistic, statistical, and metadata features for each domain. This step must be optimized for speed — feature computation adds latency to every DNS query.
  1. Classification: Apply the trained ML model to classify each domain. The model outputs a probability score (0 to 1) that the domain is DGA. A threshold (typically 0.5) converts this to a binary label.
  1. Alert generation: Flag detected malicious domains for analyst review. Alerts should include the domain, classification confidence, the features that triggered the classification, and context (when the query was seen, which internal host made it).
  1. SIEM integration: Feed alerts into a Security Information and Event Management (SIEM) system for correlation with other security events. SIEM platforms (like Splunk, IBM QRadar, or Elastic SIEM) aggregate alerts from multiple sources and provide a unified view of the security posture.

The pipeline in production:

DNS Server Logs → Domain Extraction → Feature Computation → ML Model → Alert → SIEM
     (raw)           (parse)           (compute)          (predict)   (flag)  (correlate)

Each stage has latency and throughput requirements. The entire pipeline must process each DNS query in under 1 millisecond to avoid impacting network performance.

11.7.2 Performance Requirements

DNS queries happen at high volume — thousands per second in any active network. The detection system must classify each domain in millisecond-level latency. This is a hard constraint: if the detection system adds seconds of delay to every DNS query, it will break the network.

Why latency matters so much: DNS is on the critical path of every network connection. If DNS resolution is slow, every web page load, every API call, every email send is delayed. Users notice delays above 100ms. Network timeouts occur at 30-60 seconds. A detection system that adds even 1 second of latency to DNS queries would make the network unusable.

To meet latency requirements:

  • Feature caching: Pre-compute and cache features for known domains to avoid recomputation. If google.com has been seen before, its features are already computed. Cache lookups take microseconds vs. milliseconds for fresh computation.
  • Model optimization: Techniques like model quantization (reducing numerical precision from 32-bit floats to 8-bit integers), pruning (removing unnecessary model weights), and knowledge distillation (training a smaller model to mimic a larger one) can reduce inference time. A pruned model might be 10x smaller and 5x faster with minimal accuracy loss.
  • Stream processing: Technologies like Apache Kafka (for message queuing) and Apache Flink (for real-time stream processing) handle the high-throughput data pipeline. Flink is currently the leading choice in the industry for real-time stream processing, supporting both streaming and batch modes.

Stream processing architecture:

DNS Servers → Kafka (queue) → Flink (stream processor) → ML Model → Alert System
                ↓                    ↓
           Buffering          Feature extraction
           & routing          & classification

Kafka acts as a buffer, absorbing bursts of DNS traffic. Flink processes the stream in real-time, extracting features and running the ML model. This architecture decouples the DNS servers from the detection system, so a spike in DNS traffic doesn't crash the detector.

Q: What stream processing technology is currently popular in the market? A: Apache Flink is currently the leading stream processing technology, supporting both real-time stream processing and batch processing. Kafka is still popular but getting dated for stream processing specifically.

11.7.3 False Positive Reduction

In production, false positives are a major operational concern. If the system flags 1,000 domains per day as suspicious and 900 are legitimate, analysts will quickly lose trust in the system and start ignoring alerts.

  • Whitelist: Maintain a whitelist of known-good domains to prevent false positives on popular sites. The Alexa top 1 million list can seed this whitelist. If google.com or facebook.com triggers an alert, something is wrong with the model or features, not with the domain.
  • Reputation scoring: Integrate with reputation scoring systems that provide context beyond the domain string. Services like VirusTotal, Cisco Talos, or IBM X-Force assign reputation scores to domains based on multiple intelligence sources. A domain with a high reputation score from multiple services is unlikely to be DGA.
  • Analyst feedback loop: Implement an analyst feedback loop: when a human analyst confirms or rejects a classification, that feedback is used to retrain and improve the model continuously. This creates a virtuous cycle — the model gets better over time as it learns from analyst decisions.
  • Concept drift: Watch for concept drift — attackers evolve their techniques, and models trained on old data become less effective over time. Continuous monitoring and periodic retraining are essential. A model trained on 2020 DGA families may not detect 2025 DGA families.

Concept drift in security: Unlike many ML applications where the data distribution is stable, security is adversarial — attackers actively try to evade detection. A DGA family that was detected by entropy features will evolve to use dictionary-based generation with lower entropy. The model must be retrained regularly with new data to stay effective. This is why the analyst feedback loop is critical — it provides fresh labeled data for retraining.

11.7.4 Incident Response

When a malicious domain is detected, several response actions are available:

  • Sinkholing: Redirect the malicious domain to a non-existent or controlled IP address. All queries to that domain go nowhere, breaking the C2 channel. A famous example: a researcher discovered that WannaCry malware was communicating with a specific domain (iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com). He registered that domain and pointed it to a sinkhole server. The malware checked whether the domain was registered — if it was, the malware stopped spreading. By registering the domain, the researcher effectively stopped the global WannaCry attack, stopping the worldwide outbreak. This is the only known case where registering a single domain stopped a worldwide cyberattack.

The WannaCry sinkhole incident (May 2017):

  1. WannaCry ransomware spread globally, encrypting files on hundreds of thousands of computers in 150 countries.
  2. Security researcher Marcus Hutchins (MalwareTech) analyzed the malware code.
  3. He found that the malware checked if a specific domain (iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com) was registered.
  4. If the domain was NOT registered, the malware would continue spreading. If it WAS registered, the malware would stop.
  5. He registered the domain for USD 10.99 and pointed it to a sinkhole server.
  6. Within hours, the global WannaCry outbreak stopped.
  7. The domain registration acted as a "kill switch" — a flaw in the malware's design that the researcher exploited.

Lesson: Understanding how malware communicates (its C2 mechanism) can reveal unexpected defensive opportunities. This is why studying DGA and C2 communication is not just academic — it has real-world impact.

  • Firewall blocking: Add the domain or IP to the network firewall's deny list. This is the immediate response — block the known-bad domain at the perimeter while the broader investigation proceeds.
  • Host quarantine: Isolate infected machines to prevent lateral movement. Once a machine is identified as compromised (it tried to contact a DGA domain), it should be isolated from the network until it can be cleaned.
  • Threat intelligence sharing: Report the malicious domain to threat intelligence feeds so other organizations can block it preemptively. Platforms like MISP (Malware Information Sharing Platform) and STIX/TAXII standards enable automated sharing of threat indicators.

11.7.5 Deployment Options

  • Edge deployment: Run the detection model on network appliances close to the traffic source for minimum latency. Edge devices (firewalls, routers, DNS servers) can run lightweight models that provide initial screening. This is the fastest option but limited by the compute resources of edge hardware.
  • Cloud deployment: Use cloud compute for more complex models, accepting slightly higher latency. Cloud-based detection can run larger, more accurate models and has access to broader threat intelligence. The trade-off is latency (network round-trip to the cloud) and dependency on internet connectivity.
  • Hybrid: Fast, simple models at the edge for initial screening; complex models in the cloud for deeper analysis of flagged domains. This is the most common production architecture — the edge model catches obvious DGA domains instantly, and the cloud model provides detailed analysis for borderline cases.

Recap: Production DGA detection requires millisecond-level latency. The pipeline flows from DNS capture through feature extraction to classification and alerting. Stream processing (Kafka, Flink) handles high throughput. False positive reduction uses whitelists, reputation scoring, and analyst feedback loops. Incident response includes sinkholing, firewall blocking, host quarantine, and threat intelligence sharing. The WannaCry sinkhole is a real-world example of how understanding C2 can stop a global attack.

11.8 Exam Guidance Summary

11.8.1 Exam and Assignment Notes

Exam note: The exam is open book. This means the emphasis is on application and understanding, not memorization. Expect questions that require applying concepts to scenarios, not just recalling definitions. Bring your notes, but make sure you understand the concepts well enough to apply them to novel situations.

Exam note: Only the post-midsem portion of the course will be covered in the final exam. DGA, network intrusion detection, encrypted traffic analysis, and adversarial ML are all fair game. Review the lecture notes for Lectures 8-12 thoroughly.

Exam note: When answering evaluation metrics questions, go beyond accuracy. Discuss precision, recall, F1 score, AUC, and confusion matrix — and explain why each metric matters in the security context (e.g., false negatives in malware detection are far more costly than false positives). A complete answer would include:

  • Precision: Of all flagged domains, how many are actually malicious? Matters because false alarms waste analyst time.
  • Recall: Of all malicious domains, how many did we catch? Matters because missed DGA domains mean the botnet maintains C2.
  • F1: Harmonic mean balancing precision and recall.
  • AUC: Overall discrimination ability across all thresholds.
  • Confusion matrix: The full picture of TP, FP, TN, FN.

Exam note (Assignment): Submit the report as a comprehensive document describing what was done and the outcomes. The Jupyter notebook (code) should be submitted alongside. If data and model files are too large for direct upload, include a Google Drive link in the report. A Git repo link can be included in the report for reference, but the primary code must be submitted directly (not just as a repo link) to ensure a time-frozen submission.

Exam note: If you received unexpected zero marks on any evaluation question, raise a re-evaluation request through the proper channel. Include your student number, batch, course name, and the specific question with your answer. This provides the evaluator with full context for review.

AMTCS Lecture 11 notes · Domain Generation Algorithms and DNS-Based Threat Detection

AI & ML Techniques for Cyber Security· postgraduate· 2026-08-16

Sections Breakdown

1DNS Security Fundamentals

DNS as the internet address book, port 53 exploitation, C2 communication

2Anatomy of a Domain Name

Domain hierarchy, TLDs, legitimate vs malicious domain characteristics

3Command and Control Communication

C2 lifecycle, kill chain, pyramid of pain framework

4Domain Generation Algorithms

DGA mechanism, types (arithmetic, dictionary, hash), real-world families

5Feature Engineering for Domain Detection

Lexical, linguistic, statistical, and DNS metadata features

6Machine Learning Detection Approaches

Traditional ML and deep learning for DGA classification

7Real-Time Detection Systems

Production pipeline, stream processing, incident response

8Exam Guidance Summary

Exam strategy, evaluation metrics, assignment guidelines

Postgraduate students in Cyber Security and Machine Learning

Exam Revision Notes

Below is the distilled, exam-ready core. Every entry comes from the full explanation above. Use this section for rapid review; return to the main notes when a point needs more context.

DNS Security Fundamentals

Must-know: DNS maps domain names to IP addresses on port 53. Attackers abuse this always-open channel for C2 communication. The evolution from IPs to domains to DGAs represents escalating attacker sophistication.

Top pitfall: Confusing DNS filtering (blocking at resolution time) with firewall blocking (blocking at connection time). DNS filtering prevents the connection from ever being established.

Self-check: Why is port 53 almost never blocked on a network, and how do attackers exploit this?

Connects to: Section 11.2, Section 11.3, Section 11.4

Anatomy of a Domain Name

Must-know: Domain anatomy: TLD, second-level domain, subdomain. Legitimate vs malicious domain characteristics differ in length, entropy, TLD choice, and pronounceability. Typosquatting exploits visual similarity between letters and numbers.

Top pitfall: Assuming a suspicious TLD alone proves a domain is malicious — it is only one feature among many.

Self-check: What is the difference between a TLD and a second-level domain? Give an example of each.

Connects to: Section 11.1, Section 11.5

Command and Control (C2) Communication

Must-know: C2 is the penultimate stage of the kill chain. Initial payloads are lightweight to evade detection. The pyramid of pain framework explains why DGA detection is harder than IP blocking — you must understand the algorithm, not just block indicators.

Top pitfall: Thinking that blocking an IP or domain ends the threat — with DGA, the attacker generates hundreds of new domains daily.

Self-check: Explain the pyramid of pain. Why is blocking a domain name harder than blocking an IP address?

Connects to: Section 11.1, Section 11.4

Domain Generation Algorithms (DGA)

Must-know: DGA generates 250+ domains/day; attacker registers 1-2. Types: arithmetic (date seed + math), dictionary (real words combined), hash (SHA-256/MD5 chains), permutation. Dictionary-based DGAs are hardest to detect because they look legitimate.

Top pitfall: Assuming all DGAs produce random-looking domains. Modern dictionary-based DGAs produce domains indistinguishable from legitimate business names.

Self-check: Why does the attacker only need to register 1-2 out of 250 generated domains? What happens to the other 248?

Connects to: Section 11.3, Section 11.5, Section 11.6

Feature Engineering for Domain Detection

Must-know: Four categories of features for DGA detection: lexical, linguistic, statistical, DNS metadata. Shannon entropy measures randomness. N-gram analysis captures language patterns. No single feature is sufficient — combination is key.

Top pitfall: Relying on entropy alone — dictionary-based DGAs have low entropy and will be missed.

Self-check: Why is entropy alone insufficient to detect dictionary-based DGAs? What additional features help?

Connects to: Section 11.4, Section 11.6

Machine Learning Detection Approaches

Must-know: Binary classification: benign vs DGA. Traditional ML uses feature vectors; deep learning learns features automatically. Random Forest is interpretable; LSTM generalizes better. Class imbalance: use oversampling/undersampling/class weighting. Metrics: precision, recall, F1, AUC — recall is most critical in security.

Top pitfall: The accuracy trap — 98-99% accuracy sounds great but examine confusion matrix, precision, recall, and F1 in the security context.

Self-check: Why is recall more important than precision in DGA detection? What happens if recall is low?

Connects to: Section 11.5, Section 11.7

Real-Time Detection Systems

Must-know: Detection pipeline: DNS capture → feature extraction → classification → alerts → SIEM. Millisecond latency is critical. Stream processing (Kafka, Flink) for throughput. False positive reduction: whitelists, reputation, feedback loops. Incident response: sinkholing, blocking, quarantine, threat sharing. WannaCry sinkhole stopped a global attack by registering one domain.

Top pitfall: Ignoring false positive rates — a system that flags too many legitimate domains will be ignored by analysts.

Self-check: What is sinkholing? How did it stop the WannaCry attack?

Connects to: Section 11.6, Section 11.8

Exam Guidance Summary

Must-know: Open book exam. Post-midsem only. Evaluation metrics: precision, recall, F1, AUC — explain why each matters in security. Assignment: report + notebook + code submission.

Top pitfall: Only discussing accuracy in evaluation metrics questions — must discuss precision, recall, F1, AUC and explain security implications.

Self-check: Why is recall more important than precision in malware detection? Give a concrete example.

Connects to: Section 11.6

Was this lecture useful?

Loading comments…
🤖

BitsNotes AI Assistant

Subject Notes Assistant

Configure AI Chat

Choose how to access the chatbot
Have your own API key?

Switch to "Bring Your Own Key" tab above for unlimited access with any OpenAI-compatible provider.

🔑 Enter API key above to fetch live models from provider, or enter model name manually.
OpenAI-Compatible API Support

Choose any provider preset (Gemini, DeepSeek, Kimi, GLM, MiniMax, Qwen, OpenAI, Groq, Ollama, etc.) or enter a custom endpoint URL.

Security & Privacy First

Your API key is sent directly from your browser to your specified provider. BitsNotes servers never store or see your key.