Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท IDS/IPS and Monitoring

Anomaly Detection and Baselines

Learn how anomaly-based detection builds a baseline of normal network behavior, flags outliers with z-scores, spots periodic beaconing and keeps false alarms manageable.

Signatures answer the question "is this a thing I already know is bad?" Anomaly detection asks a different one: "is this different from what is normal here?" That makes it the main way to notice something new: a stolen account downloading far more data than usual, a printer suddenly talking to a country it never contacted, or an infected laptop calling home every sixty seconds.

The price is certainty. A signature hit says what happened. An anomaly only says this is unusual, and unusual is not the same as malicious. A new backup job or a marketing campaign can look exactly like an attack. Good anomaly detection is therefore as much about context and tuning as about mathematics.

The baseline

Everything starts with a baseline: a measured description of normal. You can baseline many things.

  • Volume: bytes per hour per host, connections per minute, DNS queries per client.
  • Shape: which ports, protocols and destinations a host usually uses.
  • Time: working hours versus nights and weekends.
  • Peers: which hosts normally talk to which (a database server rarely talks to the internet).

Two rules make baselines trustworthy. First, build them while the network is believed clean; learning from a compromised network teaches the detector that the attack is normal. Second, refresh them, because normal changes (new office, new application) and a frozen baseline drifts into false alarms.

flowchart LR
    C["Collect metrics"] --> B["Build baseline<br/>(mean, spread, peers)"]
    B --> S["Score new data"]
    S --> T{"Above threshold?"}
    T -- "yes" --> A["Alert + context"]
    T -- "no" --> U["Update baseline"]
    U --> B

The detector keeps learning from data it judged normal.

Statistical outliers with the z-score

The simplest scoring method measures how many standard deviations a value sits from the mean:

TEXT
z = (value - mean) / standard_deviation

A common starting threshold is |z| above 3. The program below baselines the number of outbound megabytes per hour for one workstation, then scores new hours.

Python Z-score on hourly outbound traffic

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 two night-time hours stand out dramatically. Mean and standard deviation have a weakness, though: one huge outlier inflates both numbers and hides the next one. A more robust version uses the median and the median absolute deviation (MAD).

Python Robust scoring with the median

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 polluted standard deviation makes 120 MB look fine; the robust score sees it clearly. When your data contains occasional spikes, prefer robust statistics.

Beaconing: regularity is suspicious

Human traffic is bursty. Software that phones home on a timer is not. Beaconing is the regular check-in pattern of many malware families (and of harmless software like update checkers, which is why you always investigate before concluding). The detector measures the gaps between connections to one destination and looks at the coefficient of variation (standard deviation divided by mean). A very low value means suspiciously regular timing.

Python Spotting regular intervals

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.

Attackers add jitter (random delay) to defeat exactly this check, so real systems combine several features: interval regularity, small constant payload sizes, long-lived destinations, rare destinations and unusual hours. No single number decides.

Behavioral and peer baselines

Besides numbers, you can baseline relationships. A web server that has never initiated an outbound connection suddenly opening one to the internet is a high-value signal, and it needs no statistics at all: a rule "servers in the DMZ may not initiate outbound" plus an alert when it breaks. Peer-group comparison is another strong idea: if 200 accountants' laptops all talk to five internal servers and one talks to eighty, investigate that one.

Keeping false positives manageable

  • Alert on combinations. Unusual volume and a rare destination and off-hours is far stronger than any one signal.
  • Use allow-lists for known-noisy but benign events (the nightly backup).
  • Add context to every alert: who owns the host, what role it has, what the baseline was. Analysts triage faster with context.
  • Rank, don't just alert. A score lets analysts start with the top ten instead of a flat list of thousands.
  • Review and tune. Track how many alerts were real; retire or fix rules with a bad record. This ties directly into log analysis workflows.

Machine learning detectors follow the same loop: learn from normal data, score new data, tune the threshold. They help with many dimensions at once, but they inherit every pitfall above, including poisoned training data. Understand the simple baseline first; it is also your yardstick for deciding whether a complex model is worth it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does an anomaly detector flag?

    Anomaly detection compares current behavior with a baseline of normal and flags the difference.
  2. 2. The baseline mean is 12 and the standard deviation is 2. A new value is 24. What is the z-score?

    z = (24 - 12) / 2 = 6, far beyond the usual threshold of 3.
  3. 3. Why might robust statistics (median and MAD) be better than mean and standard deviation?

    One extreme value inflates the mean and standard deviation, which can hide later anomalies; the median barely moves.
  4. 4. A connection to one host happens every 60 seconds with almost no variation. What might that indicate?

    Very regular intervals suggest software on a timer, which deserves investigation to decide whether it is benign.
  5. 5. Why should a baseline be built while the network is believed clean?

    Learning from malicious activity makes it part of 'normal' and hides it.

Summary

  • Anomaly detection flags deviation from a baseline, so it can find unknown attacks but cannot say what they are.
  • Build baselines from clean data and refresh them as normal changes.
  • Z-scores measure distance from the mean; median and MAD resist outliers.
  • Beaconing shows up as unusually regular timing; attackers add jitter, so combine features.
  • Peer-group and relationship baselines catch things volume statistics miss.
  • Reduce false positives with combined conditions, allow-lists, context and ranking.

Next: Snort and Suricata Rules