r/devops7 • u/Alternative_Pin9598 • 18h ago
Automate it #10: a stolen-key SSH login at 03:41 succeeded like any other. 3 rules flagged it plus 5 harmless logins; an anomaly score flagged only that one (real output)
03:41, Accepted publickey for erin on the bastion. A valid key, so it's a success line like the other 950 in auth.log. The key was stolen.
The chore: score every successful SSH login against what's normal for that account, and look only at the ones the history has never seen.
(This sub is run by the SynapCores team; the table, the rules and the model all run in SynapCores. Sample auth.log, real output from 2.5.1 this morning.)
Step 1: auth.log into a table, three numbers per login
Nov 24 03:40:15 bastion sshd[51845]: Connection closed by authenticating user deploy 203.0.113.77 port 43424 [preauth]
Nov 24 03:41:07 bastion sshd[51846]: Accepted publickey for erin from 203.0.113.77 port 59705 ssh2: ED25519 SHA256:fLzLDEyq35/NtR7kV6gozHKkWHmDG1uXiuLizvxElwUsshwatch.py parses six weeks of lines. For each login it looks at the 14 days before:
hour_seen: how many of the account's logins were within an hour of this time of day (capped at 10; 0 = never at this time)net_seen: how many of the account's logins came from this /24 (capped at 10; 0 = never from this network)ip_accounts: how many other accounts this IP tried, or got into, in the 15 minutes before
One row per login:
CREATE TABLE logins (ts TEXT, account TEXT, ip TEXT, hour_seen FLOAT, net_seen FLOAT, ip_accounts FLOAT)
951 logins and 1443 refused tries; 640 logins scored (after the first 14 days)Step 2: the rules you'd write first
SELECT ts, account FROM logins WHERE ts >= '2026-11-23 08:00' AND net_seen = 0 ORDER BY tsSame with hour_seen = 0 and ip_accounts >= 2, over the last 24 h:
new network 3: alice 09:20, carol 19:40, erin 03:41
never at this hour 2: erin 03:41, frank 06:50
IP tried 2+ other accounts 3: alice 09:04, bob 09:11, erin 03:41All three catch erin. All three also fire on ordinary logins: alice at a conference hotel, carol on call from a phone hotspot, frank up early, the office NAT at standup. Six logins flagged, one real. By Thursday somebody mutes them.
Step 3, the payoff: learn normal from 4 weeks of logins, score inside the SELECT
CREATE EXPERIMENT ssh_exp AS SELECT hour_seen, net_seen, ip_accounts FROM logins WHERE ts < '2026-11-23 08:00'
WITH (task_type = 'anomaly_detection', algorithms = ['isolation_forest'], max_trials = 5, random_seed = 42);
DEPLOY MODEL ssh_model FROM EXPERIMENT ssh_exp;
SELECT ts, account, ip, hour_seen, net_seen, ip_accounts, ANOMALY_SCORE('ssh_model', hour_seen, net_seen, ip_accounts) AS score
FROM logins WHERE ts >= '2026-11-23 08:00' ORDER BY score DESC LIMIT 6;
odd = above the highest score in the history (0.70)
11-24 03:41 erin 203.0.113.77 hour_seen 0 net_seen 0 ip_accounts 3 score 0.77 ODD
11-23 09:20 alice 198.19.88.140 hour_seen 10 net_seen 0 ip_accounts 0 score 0.66
11-23 19:40 carol 198.19.55.2 hour_seen 6 net_seen 0 ip_accounts 0 score 0.66
11-23 14:05 alice 198.19.88.140 hour_seen 10 net_seen 1 ip_accounts 0 score 0.64
...One login over the line. In the 607 logins of history, a new network showed up 6 times, a never-before hour 8 times, a busy IP 21 times, and two of them at once never. Nobody wrote a rule for "all three at once". The model only learned what past logins look like, and erin's 03:41 sits where none of them do. You could AND the rules now that you know this pattern; the score will flag the next odd mix of these three numbers without anyone writing a rule for it.
Why one database? Logins, model and scores sit behind one endpoint: no ML service, no export step. The whole check, training included, took about 0.1 s on a Mac mini (Apple M4).
Wire it up: from cron every 15 minutes, join the rotated auth.log files into one, oldest first (zcat -f reads the .gz ones), load it, check the last 15 minutes, and post ODD lines to the on-call channel. sshwatch.py (stdlib only) and the sample log generator are in the comments.
Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-automate-it-10
Does anything alert on your successful SSH logins, or only on the failed ones?