← BackROYAL ANKIT AHIRAN
Skill 01 · Python

Auto-Threat Sentinel

A lightweight Python background log-monitoring script that inspects server logs continuously and flags repeated suspicious IP activity.

DEMO

What Was Built

Auto-Threat Sentinel is a background Python utility designed to read incoming server authentication and access log lines line-by-line, track repeated failed request counts per IP, and surface immediate alerts when an IP crosses a defensive threshold.

How It Looked

Inside a high-contrast black terminal window, healthy requests stream cleanly in green text while repeated unauthorized or anomalous attempts flash immediately in red with clear status prefixes.

What Was Learned

  • Efficient real-time file tailing and safe regex pattern parsing in Python without blocking execution.
  • Using sliding counters and dictionaries to separate normal human traffic from noisy brute-force bursts.
  • Designing clear terminal color cues so an operator can spot abnormal behavior within seconds.

Step-by-Step How to Run

  1. Ensure Python 3 is installed on your machine and create a readable local test log file.
  2. Place the monitoring script in your project folder and point it at your own local log path.
  3. Start the watcher in your terminal and observe green normal entries and red threshold alerts.
sentinel.py — Command
python3 sentinel.py --watch ./local_access.log --threshold 5
Sample Code: sentinel.py

Sample for learning, not my actual file

sentinel.py
#!/usr/bin/env python3
"""Sample for learning, not my actual file."""
import argparse
import re
from collections import Counter
from pathlib import Path

IP_RE = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
SUSPICIOUS_KEYS = ("FAILED", "UNAUTHORIZED", "401", "403")

def scan_log(log_path: Path, threshold: int) -> None:
    if not log_path.exists():
        print(f"[INFO] Create {log_path} first to inspect lines.")
        return
    counts = Counter()
    with log_path.open("r", encoding="utf-8", errors="ignore") as f:
        for line_no, raw in enumerate(f, 1):
            line = raw.strip()
            if not line:
                continue
            ip_match = IP_RE.search(line)
            if ip_match and any(k in line.upper() for k in SUSPICIOUS_KEYS):
                ip = ip_match.group(0)
                counts[ip] += 1
                if counts[ip] >= threshold:
                    print(f"[ALERT] {ip} flagged ({counts[ip]} events at line {line_no})")
            else:
                print(f"[OK] line {line_no}: normal traffic")
    for ip, total in counts.items():
        status = "REVIEW" if total >= threshold else "WATCH"
        print(f"[{status}] {ip} -> {total} failed events")

if __name__ == "__main__":
    p = argparse.ArgumentParser(description="Auto-Threat Sentinel (Sample)")
    p.add_argument("--watch", default="./local_access.log")
    p.add_argument("--threshold", type=int, default=5)
    args = p.parse_args()
    scan_log(Path(args.watch), args.threshold)

Proof

Sample Outputstdout
[OK] Sentinel active on local_access.log
[INFO] 192.168.1.14 -> 200 GET /index.html
[ALERT] 198.51.100.77 exceeded failed threshold (5/5)
[ACTION] IP flagged for defensive rule review
[OK] Log monitoring continuing normally
Interface Capture Slot
● SENTINEL STATUS: ACTIVE
Black terminal log watcher filtering normal green lines and isolating red anomaly spikes cleanly.
Terminal preview frame for Auto-Threat Sentinel output verification.