Detection: from log record to event
Packages: internal/netaddr and internal/detect. Everything here is pure: input
text in, a result out, no files, no network, no database. That makes it easy to
test and to fuzz.
The output: an Event
Defined in detect/event.go:
| Field | Meaning | Example |
|---|---|---|
Service |
ssh, nginx, caddy |
ssh |
Detector |
scoring bucket | ssh-auth, nginx-probe, caddy-probe |
Kind |
what happened, from a fixed list | failed-password |
Addr |
the peer address a firewall would see | 203.0.113.50 |
Reported |
visitor claimed through a trusted proxy (information only) | |
Proxied |
Addr is a trusted proxy, so nothing can be banned |
false |
Time |
from the log record | |
Weight |
points | 1 |
HighConfidence |
evidence an ordinary user does not produce | |
Key |
identifies the record, so replays count once | a journal cursor |
There is no field for a user name, URL or raw line. What is not carried cannot be stored or leaked.
Addresses (netaddr)
| Function | Does |
|---|---|
ParseAddr |
a literal IP only; turns ::ffff:1.2.3.4 into 1.2.3.4; rejects zones, ports, brackets and names |
ParsePrefix |
an address or CIDR; rejects host bits set, with a suggestion |
Format |
prints /32 and /128 as plain addresses |
ScopeOf |
loopback, unusable, link-local, private, shared-nat, global |
Set |
a small list with Contains, Covering, Overlapping |
PrefixRange, MergePrefixes, Flatten |
range arithmetic for the firewall |
No function looks up a name. A host name in a log is simply not an address.
OpenSSH
Parsing (ssh.go)
Four patterns, each anchored at the start and end of the message:
| Pattern starts with | Kind | High confidence |
|---|---|---|
Failed password for ... from ADDR port N ssh2 |
failed-password |
only for invalid user |
Invalid user ... from ADDR [port N] |
invalid-user |
yes |
User ... from ADDR not allowed because ... (five fixed reasons) |
user-not-allowed |
yes |
error: PAM: Authentication failure|User not known ... for ... from ADDR |
pam-failure |
only for illegal user |
Why both anchors? The user name is whatever the attacker types. The pattern for a failed password is, in effect:
Plain text
^Failed password for (invalid user )?.* from (\S+) port [0-9]{1,5} ssh2$.* is greedy, so the captured address is the last one, which sshd appended
itself. Messages longer than 2048 bytes or containing control characters are
rejected. Go's regular expressions run in time proportional to the input, so no
crafted message can stall them.
Trust (journal.go)
DecodeJournal reads one JSON line from journalctl. A message that is binary,
repeated or too long returns ErrNoMessage, but the cursor is still returned so
the reader can move past it.
TrustSSH requires:
_COMMissshd,sshd-sessionorsshd-auth;_UIDis0;- if
_EXEis present, an sshd binary in a system directory (an upgraded binary shown as... (deleted)is accepted); _EXEidentifies it, or_SYSTEMD_UNITis a configured unit or a per-connection instance of one.
_EXE is allowed to be missing because journald sometimes cannot read it for a
process that exits quickly.
Parsing happens first and trust second, so that a forged message that looks like
a failure can be counted (journal.untrusted) rather than silently skipped.
Web access logs
Nginx (web.go)
CompileNginxFormat turns a log_format into a list of literals and variables.
It refuses formats it cannot split without guessing:
- two variables with nothing between them;
- a repeated variable;
- missing
$request,$status, an address or a time; - a client-controlled variable before the address.
Parse then walks a line. Each variable ends at the next literal, and the whole
line must be consumed. Two details:
$remote_useris chosen by the client and may contain spaces and brackets. Its end is taken as the last matching separator before the first quote, which only nginx can have written.- If the format has
$realip_remote_addr, that is the peer, and$remote_addrbecomes the reported visitor.
nginx writes a quote inside a value as \x22; ipfence reverses that escaping for
probe matching only.
Caddy
ParseCaddy reads one JSON line (at most 64 KiB) and requires the logger
http.log.access (or a name under it), a request.remote_ip, a numeric status
and a time. client_ip, if present and different, is the reported visitor.
Probes (probes.go)
MatchProbe decodes percent-encoding (twice for payloads, to catch
double-encoding), lowercases, and collapses repeated slashes. Order:
${jndi:in the target, user agent or referrer:jndi-injection.../together withetc/passwdoretc/shadow:path-traversal.- the PHPUnit
eval-stdin.phppath:phpunit-eval. - Only for a 4xx answer: secret files (
env-file,git-metadata,credential-file,wp-config-backup).
The 4xx condition means a site that genuinely serves one of those paths is never penalized.
WebEvent builds the event. If the peer is a trusted proxy it sets Proxied and
copies the reported visitor; otherwise the reported visitor is discarded. The
target of a ban can therefore only ever be the peer.
Tests
| File | Proves |
|---|---|
ssh_test.go |
matches on real OpenSSH 10.0 messages, ignored messages, user-name injection, journal trust cases from testdata/journal_ssh.jsonl, no user name in events |
web_test.go |
both formats and custom ones, hostile user names, rejected lines and formats, Caddy variants, probes and ordinary traffic, forwarded headers, a fuzz target |
netaddr_test.go |
parsing, rejection, ranges, flattening, scopes |
Adding a message or probe: start with a real captured line, add the test, watch it fail, then change the code.