cybersecurity · intermediate · ~15 min · safe pentest lab
Group log lines by source IP, count occurrences, threshold the result.
Implement:
int detect_brute_force(const char *log, int threshold);
Walk the buffer line by line. Each Failed password ... from <ip> line
counts as one failure for that IP. Return the number of distinct IPs
whose failure count is at least threshold.
| log | threshold | returns |
|---|---|---|
"" |
1 | 0 |
Three Failed from 192.0.2.7 |
3 | 1 |
| Same | 4 | 0 |
Mix of Failed and Accepted |
1 | (count distinct IPs that failed) |
from and precedes port.Accepted lines entirely.strstr(line, "Failed password") filters. strstr(line, "from ") locates the IP start.This exercise is for defensive learning in a local lab only. Do not use it on systems you do not own or have explicit permission to test.
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.