A single log line rarely proves an attack. Several together do: failed logins, then a success from the same address, then a large download.
What to build
Find addresses whose log entries match a brute-force-then-steal pattern, and report each as an incident.
Requirements
- Each line is
time ip event detail, with time in whole seconds (increasing). Events:fail USER,ok USER,download MB. - An incident for an address: at least 3
failentries, then anokwithin 300 seconds after the last of those failures (failures counted since the address's previous ok), then adownloadof 100 MB or more within 600 seconds after that ok. - Report each address at most once, in the order its incident completed, as
incident: IP user USER fails F download MB at TIME, where USER is the account of the ok and F the number of failures before it. - If none, print
no incidents.
Input
One log line per line.
Output
As described.
Constraints
Up to 1000 log lines.
Example
Input:
10 9.9.9.9 fail rahim
20 9.9.9.9 fail rahim
30 9.9.9.9 fail rahim
40 9.9.9.9 ok rahim
100 9.9.9.9 download 250
Output:
incident: 9.9.9.9 user rahim fails 3 download 250 at 100
Why it works
Each step alone is common (people mistype, people log in, people download), but in this order, close together and from one address, they describe someone guessing a password and then taking data, which is the kind of correlation alerting systems are built on.
Examples
10 9.9.9.9 fail rahim 20 9.9.9.9 fail rahim 30 9.9.9.9 fail rahim 40 9.9.9.9 ok rahim 100 9.9.9.9 download 250
incident: 9.9.9.9 user rahim fails 3 download 250 at 100
10 1.1.1.1 fail anika 20 1.1.1.1 ok anika 30 1.1.1.1 download 500
no incidents
Hints
Hint 1
Keep per-address state: failures since the last ok, the time of the last failure, and the last successful login (time, user, failures before it).
Solution
Show one correct solution (try it yourself first)
const state = new Map();
const reported = new Set();
let any = false;
let line = prompt();
while (line !== null) {
if (line.trim()) {
const [timeText, ip, event, detail] = line.trim().split(/\s+/);
const time = Number(timeText);
const s = state.get(ip) ?? { fails: 0, lastFail: -Infinity, login: null };
if (event === "fail") {
s.fails++;
s.lastFail = time;
} else if (event === "ok") {
s.login = s.fails >= 3 && time - s.lastFail <= 300 ? { time, user: detail, fails: s.fails } : null;
s.fails = 0;
} else if (event === "download" && s.login && !reported.has(ip)) {
if (Number(detail) >= 100 && time - s.login.time <= 600) {
reported.add(ip);
any = true;
console.log(`incident: ${ip} user ${s.login.user} fails ${s.login.fails} download ${detail} at ${time}`);
}
}
state.set(ip, s);
}
line = prompt();
}
if (!any) console.log("no incidents");
▶ Open this solution in the compiler, then press Visualize to watch it run step by step