Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Write a detection rule

After this lesson: I can write a rule that finds a guessing attack in log lines.

60 min lesson3 commandsIncident response and forensics lab: investigate a compromised server in your browser

A rule that watches while you sleep

A detection rule is a test that a security tool runs on every event to find signs of an attack. When an event passes the test, the tool raises an alert.

Nobody can read a million log lines a day. A good rule reads them all and speaks up only when it matters. You will write one.

By the end of this lesson

Note

I can write a rule that finds a guessing attack in log lines.

Key words

WordWhat it meansExample
Detection rulea test that raises an alert when events look like an attack
Patternthe shape of text a rule looks for
Matcha line that fits the pattern
Thresholdhow many matches it takes to raise an alert5 failures
Time windowthe span of time in which matches are counted60 seconds
Brute forcetrying many passwords until one works

The cells students fill in are left blank in this preview.

A rule starts by matching a pattern

The first step is to say what one bad event looks like. A failed sign-in has the words Failed password in it. That text is the pattern.

grep -E reads a pattern with a few extra symbols. A question mark after a bracket means the part is optional. So one pattern can match both a known user and an unknown one.

Match failed sign-ins in three lines

Command
printf 'Failed password for sam from 192.0.2.5 port 22\nAccepted password for sam from 192.0.2.5 port 22\nFailed password for invalid user admin from 192.0.2.9 port 22\n' | grep -E 'Failed password for (invalid user )?[a-z]+ from'

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

One failure is normal, so count them

People mistype passwords. One failed sign-in is not an attack, so a rule that alerts on every match is mostly noise.

A threshold fixes that: the rule counts matches and speaks up at a number you choose. A guessing attack, called brute force, makes many failures very fast. That speed is what the rule must see.

Worked example: failures at five moments

Seconds into the dayFailures so farInside a 10 second window
1011
1222 (10 and 12)
1433 (10, 12 and 14): alert at threshold 3
30041 (only 300): the old ones are too far back
30552 (300 and 305): no alert

The cells students fill in are left blank in this preview.

A rule over a time window

Code: python
def too_many(times, limit, window):
    for start in times:
        inside = 0
        for other in times:
            if other >= start and other - start <= window:
                inside = inside + 1
        if inside >= limit:
            return True
    return False

# Seconds at which a sign-in failed.
print(too_many([10, 12, 14, 300, 305], 3, 10))
print(too_many([10, 120, 300, 305], 3, 10))
# Change the 3 to a 4 in the first line, then press Run.

Count seconds inside a window

Command
printf '10\n12\n14\n300\n305\n' | awk '$1 >= 10 && $1 <= 20' | wc -l

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a rule should count all the failures in a day. Three failures spread over twenty-four hours are normal. Three failures in ten seconds are not. Always count inside a short window, so that time is part of the rule.

Tuning a rule trades false alarms for misses

A low threshold catches more attacks but raises more false alarms. A high one is quiet but can miss a careful attacker who goes slowly.

A team tests a new rule on old logs first. Then it tunes the numbers until the alerts are few enough to read and still catch the attack.

Count requests for secret files

Command
grep -cE '"(GET|POST) /(\.env|\.git)' case/var/log/nginx/access.log

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

Write a rule in words for a different attack: a web visitor who asks for twenty pages that do not exist in one minute.

State the pattern, the threshold and the window. Say what a normal visitor looks like, and what your rule would do to them.

About this lab and its source: Incident response and forensics lab: investigate a compromised server in your browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.