WeCodeAcademy

Step 6: Spot suspicious activity

Logs are also a security tool. Four 401 (Unauthorized) responses in a row from one address on the login page looks like someone guessing passwords. Count failures per IP and alert above a threshold. Then print that IP's other requests for context, the way a real investigator would.

Examplepython
Open in editor »

10.0.0.9 failed four times in 8 seconds. A real system would respond automatically (block the address, slow it down, add a captcha), but the first step is always detection, and it is just counting.

Your turn: write the code

Write suspicious(counts, limit) that takes a dictionary of failure counts per IP and returns the sorted list of IPs whose count is at least limit. Print suspicious({"a": 5, "b": 1, "c": 3}, 3).

Quick check

What pattern suggests a password-guessing attack?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.