cybersecurity · intermediate · ~15 min · safe pentest lab
Group log lines by source IP, count occurrences, threshold the result.
Detect brute-force source IPs in an auth log — the simplest brute-force detector and the heart of every SOC routine.
Implement int detect_brute_force(const char *log, int threshold) that returns the number of distinct source IPs whose failure count is at least threshold.
log: a NUL-terminated, newline-separated auth-log buffer baked into the harness. Each Failed password ... from <ip> port ... line is one failure for that IP. Accepted lines are not failures.threshold: the minimum failure count for an IP to be flagged (compared with >=).Returns a non-negative int: the count of distinct IPs meeting the threshold.
"" threshold 1 -> 0
three "Failed" from 192.0.2.7 threshold 3 -> 1
same threshold 4 -> 0
mix of "Failed" from two IPs + "Accepted" threshold 1 -> 2
from and before port.Accepted lines entirely.The simplest brute-force detector you'll ever write — and the heart of every SOC routine.
A NUL-terminated multi-line log buffer + an integer threshold.
Non-negative int — count of IPs over threshold.
No allocation. Small fixed-size table is fine (≤128 entries).
#include <stddef.h>
#include <string.h>
int detect_brute_force(const char *log, int threshold) {
/* TODO */
(void)log; (void)threshold;
return 0;
}
Counting Accepted lines. Adding the same IP twice. Missing the from → IP → port parsing pattern.
Empty log. Threshold higher than any IP's count. Same IP across many lines.
O(n × m) worst-case where m is the number of distinct IPs (small in practice).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.