Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 ยท Projects and Practice

Project: Detect Beaconing in Network Logs

Build a beacon detector in Python: group connections by host, measure the interval and jitter, score regularity and flag likely command-and-control traffic in made-up logs.

Many pieces of malware phone home on a schedule: every 60 seconds, every 5 minutes, perhaps with a little random variation. This regular check-in is called beaconing. Humans browse in bursts, with irregular pauses; a program waking up on a timer does not. That difference is the basis of one of the most widely used network detections, and it needs nothing more than timestamps and destinations.

In this project you build a detector that reads connection logs, groups them by destination, measures the spacing between connections and flags the regular ones. The logs are generated from a seeded random generator, all domains use the reserved example names and nothing touches a network. The ideas appear in real tools for proxy, DNS and flow data, and connect to the behavioural patterns lesson.

Beacon or human?

Beacon Even gaps, small jitter.
Human Bursts and long pauses.
Updater Also regular, but benign.

Regularity alone is not proof.

1 / 2

Step 1: generate realistic logs

The generator makes four kinds of traffic for one workstation over a few hours: a beacon to a command-and-control-style domain every 60 s with up to 10 % jitter; human browsing with random gaps; a software updater polling every 15 minutes exactly; and a slow beacon every 10 minutes with 30 % jitter. Seeds make every run identical.

Python Step 1: generate seeded connection logs

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.

Step 2: intervals and jitter

Group events by destination, sort by time, take the differences between consecutive connections and compute the mean, the standard deviation and the coefficient of variation (standard deviation divided by mean, a unit-free measure of spread). A small value means very regular. A beacon with 10 % jitter gives a coefficient of about 0.06; random human traffic gives about 1.

Python Step 2: compute interval statistics per destination

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 exact 15-minute updater has a coefficient of zero, the 60-second beacon is small, the slow beacon is somewhat larger, and the three human destinations (each only a third of the random traffic) are bigger still. A raw ranking by regularity puts the harmless updater at the top, which shows why you need the next steps.

Step 3: score and reduce false positives

Regularity is necessary but not sufficient. Legitimate software also polls on timers: updaters, health checks, mail clients, telemetry. Combine several signals into an explainable score:

  • Regularity (low coefficient of variation) adds points.
  • Enough samples (at least 10 connections) adds confidence.
  • Rare destination: a domain the organisation's other hosts never contact is more suspicious than a popular vendor. We model this with a "seen by many hosts" allow-list.
  • Suspicious naming (an algorithmic-looking or very new domain) could add points; here the lesson uses a simple stand-in check.
  • Small, constant payload sizes are a beacon trait, if your logs include bytes.
Python Step 3: score and flag likely beacons

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 60-second beacon and the slow beacon both score high: they are regular enough, frequent enough and unknown elsewhere in the organisation. The exact-interval updater is the most regular of all, yet it drops down because every other host also contacts the same vendor domain. Human destinations are neither regular nor rare and stay low. That reasoning is the heart of detection engineering: a baseline of normal turns a noisy statistic into a useful alert.

Limits, evasion and improvements

  • Jitter: authors add random delays specifically to defeat strict regularity checks. A coefficient of variation of 0.3 to 0.5 can still be far more regular than humans, and tools also use frequency analysis (looking for a dominant period in the connection times) instead of a simple ratio.
  • Long sleeps: a beacon every few hours needs days of data. Keep long retention for metadata even if you drop payloads.
  • Legitimate regularity: monitoring agents, NTP, cloud sync clients. Maintain an allow-list built from baselines, not guesses.
  • Domain generation algorithms and fast-flux hosting spread beacons across many names, so group by IP, certificate, JA3/JA4 TLS fingerprint or user agent as well as by name.
  • DNS and other protocols: the same method works on DNS query logs and on flow records (source, destination, time, byte counts).

Always validate a flagged beacon before acting: check what process on the host makes the connection, what the destination hosts, and whether the pattern matches known software. Detection is a lead for analysis, never a verdict.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a coefficient of variation near zero indicate for connection intervals?

    A small ratio of standard deviation to mean means the gaps are nearly equal.
  2. 2. Why does the exact 15-minute updater rank lower in the final score than the 60-second beacon?

    Baselines of normal destinations remove regular but legitimate traffic.
  3. 3. How do beacon authors try to evade interval checks?

    Jitter and long intervals make the timing look less regular.
  4. 4. What should happen after a beacon is flagged?

    A flag is a lead that needs validation, not a verdict.

Summary

  • Beaconing is regular, automated callback traffic; humans produce bursty, irregular patterns.
  • Group connections by source and destination, then measure mean gap, standard deviation and coefficient of variation.
  • Combine regularity with sample count, destination rarity and other context into an explainable score.
  • Baselines of normal traffic eliminate legitimate polling such as updaters.
  • Jitter, long sleeps and rotating infrastructure require frequency analysis and wider grouping.
  • The logs here are seeded and invented; real detections run on your proxy, DNS or flow data.

Next: Malware Analysis Practice Questions