Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 80 ยท Malware and Endpoint Security

Botnets and Command-and-Control

Understand the botnet lifecycle, C2 architectures (IRC, HTTP, P2P), how defenders spot beaconing and generated domains, and how takedowns and prevention work.

A botnet is a group of internet-connected devices, called bots or zombies, that have been infected with malware and are controlled remotely by an operator known as the botmaster or bot herder. The owners of the devices usually have no idea. One infected laptop is a nuisance; a hundred thousand of them, acting together, are a weapon for sending spam, stealing credentials, mining cryptocurrency, or flooding a website with traffic in a distributed denial-of-service (DDoS) attack.

What turns scattered infections into a botnet is command-and-control (C2): the communication channel the botmaster uses to give orders and collect results. This lesson is about defence. You will learn how botnets are built and run, why the C2 channel is the defender's best opportunity, and how to detect the two classic C2 signals (regular beaconing and machine-generated domain names) with small, safe Python simulations. Nothing here touches a network. For the malware families that recruit bots, see Malware Types and How They Spread.

The botnet lifecycle

Most botnets follow the same stages, which map neatly onto the attack lifecycle:

  1. Infection. A device is compromised through phishing, a malicious download, an unpatched vulnerability, or weak or default credentials (the usual way IoT cameras and routers are recruited).
  2. Rallying. The new bot contacts the C2 infrastructure to announce itself. It often reports basic facts such as operating system, uptime and bandwidth.
  3. Waiting and tasking. The bot sits quietly until it receives instructions: download a module, send a batch of spam, join an attack against a target at a given time.
  4. Persistence and spread. The bot hides itself, restarts after a reboot, and may scan for more victims.
  5. Monetization. The botmaster rents the botnet to others ("DDoS for hire"), sells stolen data, or runs fraud and mining operations.
flowchart LR
    A["Victim device"] -->|"1. infected"| B["Bot"]
    B -->|"2. check-in"| C["C2 server or peers"]
    C -->|"3. tasks"| B
    B -->|"4. results"| C
    D["Botmaster"] --> C
    B -.->|"spam, DDoS, theft, mining"| E["Targets"]

The bot calls out to C2, so defenders can see and block the traffic even though the victim never receives an inbound connection.

C2 architectures

How bots find and talk to their controller has changed as defenders learned to cut each design.

Model How it works Strength for the attacker Weakness
Centralized (IRC, then HTTP/HTTPS) Bots connect to one server or a few. Early botnets used IRC chat channels; modern ones blend into web traffic. Simple, fast commands, easy to manage One server to seize or block is a single point of failure
Peer-to-peer (P2P) Bots pass commands among themselves; there is no single server. Hard to take down Slower to coordinate, more complex code
Domain generation (DGA) Bot and botmaster compute the same list of pseudo-random domain names each day. The botmaster registers one; the bots try them all. Blocking one domain does not help Produces many failed DNS lookups, which is detectable
Abuse of legitimate services C2 messages hidden in social media posts, cloud storage, chat apps or DNS queries Traffic looks normal and is rarely blocked Depends on the service not removing the content

Modern C2 is almost always outbound and encrypted over ports 80 and 443, because firewalls rarely block those. That is why a firewall alone does not stop a botnet (see Firewalls, Segmentation and Zero Trust). The defender's leverage is behaviour: how often a host calls out, to whom, and with what rhythm.

Detecting beaconing

A bot checks in on a schedule, a behaviour called beaconing. Human browsing is bursty and irregular (you read a page, wait, click), but a beacon often fires every 60 seconds, give or take a little. Clever malware adds random jitter to hide, but the intervals are still unusually uniform compared with people.

A simple measure is the coefficient of variation (CV) of the gaps between connections: standard deviation divided by mean. A low CV means very regular timing. The script below builds synthetic connection logs and scores each host.

Python Spot beaconing by interval regularity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The human hosts score far above the threshold, while both beacons stand out, even the jittery one. Real detection tools combine this timing signal with others: a rare destination, a small and constant payload size, an unusual user agent, or a connection that continues at 3 a.m. A low CV alone is not proof, because legitimate software (update checkers, monitoring agents, email clients) is also regular. You investigate the destination before you conclude anything. This is the same triage mindset as in Spotting Attacks in Logs.

Detecting generated domains

Because a DGA produces names that no person would choose, such as xkqjvzpwtm.example, defenders look at how "random" a name is. One easy measure is Shannon entropy, the average information per character: higher means more random. Another sign is a burst of NXDOMAIN replies (the domain does not exist), since the bot tries many names that were never registered.

Python Score domains for randomness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Entropy is a heuristic, not a verdict. Short brand names and some languages produce false positives, while dictionary-based DGAs (which join random real words) score as normal. Production systems therefore use machine-learning classifiers trained on large domain lists, and they combine the score with how many failed lookups a host makes and how new the domain is. The principle is the same: model what normal looks like and flag deviations.

Takedown and disruption

Defenders attack the weakest link of each architecture:

  • Sinkholing: redirect the botnet's domains to a server controlled by researchers, so bots call a harmless place and the operator loses control. Sinkhole logs also tell you how many bots exist and where.
  • Domain and server seizure: registrars and hosting providers, often with law enforcement, suspend the infrastructure.
  • P2P disruption: poison the peer lists or take over key nodes, which is slow and requires reverse engineering the protocol (see Reverse Engineering Basics).
  • Legal action and arrests: the most lasting effect, because botnets are operated by people.

Takedowns need coordination among vendors, ISPs and police across several countries. Even a successful takedown seldom kills infections: the bots remain on victims' machines until they are cleaned, which is why notification and cleanup matter.

Preventing your devices from joining a botnet

  • Patch operating systems, browsers and router or camera firmware promptly (Vulnerability Management and Patching).
  • Change default passwords on every IoT device and disable remote administration you do not need. Credential guessing against default logins is how many large IoT botnets grew.
  • Filter egress traffic: allow servers and workstations to reach only what they need, and log DNS. Outbound controls turn a silent infection into an alert.
  • Segment networks so an infected camera cannot reach your file server.
  • Deploy endpoint protection and monitoring that spots persistence and unusual processes (Endpoint Protection and System Hardening).
  • Train users to resist phishing (Phishing and Social Engineering).
  • For operators of internet-facing services, rate-limit and use DDoS mitigation so a botnet cannot easily overwhelm you.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the C2 channel the defender's best opportunity to find a botnet?

    Even with encrypted payloads you can see destination, frequency, timing and volume. Outbound beaconing is the common thread across botnet designs.
  2. 2. A host contacts the same external address every 60 seconds, with almost no variation. What is the best next step?

    Regularity is a lead, not proof. Update checkers and monitoring agents are also regular, so you check who the destination is and which process opens the connection.
  3. 3. What problem does a domain generation algorithm (DGA) create for defenders, and what weakness does it have?

    The bot tries many generated names each day, so a single block fails; yet the burst of NXDOMAIN answers and random-looking names is a strong signal.
  4. 4. Which action best removes the botmaster's control without touching victim devices?

    Sinkholing points the bots at a harmless server. The infections remain until cleaned, but the operator can no longer give commands.

Summary

  • A botnet is a set of infected devices controlled through a C2 channel; operators rent or use them for DDoS, spam, theft and mining.
  • The lifecycle is infection, rallying, tasking, persistence and monetization.
  • C2 designs include centralized (IRC/HTTP), P2P, DGA-based and abuse of legitimate services; most traffic is outbound over ports 80 and 443.
  • Defenders detect botnets by behaviour: regular beaconing, random-looking domains, bursts of failed DNS lookups and rare destinations.
  • Takedowns use sinkholing, seizures and legal action, but bots stay infected until cleaned.
  • Prevention is patching, changing default credentials, egress filtering, segmentation, endpoint protection and user training.

Next: Reverse Engineering Basics, to see how analysts study a malware sample to learn how its C2 works.