Build Your Own Brute-Force Detection Engine in Python: A Step-by-Step Guide + Video

Listen to this Post

Featured Image

Introduction:

In the world of cybersecurity, distinguishing between a simple user mistake and a coordinated attack is the difference between a quiet Tuesday and a full-blown incident response. Security Information and Event Management (SIEM) systems excel at this by aggregating and analyzing log data, but understanding the underlying mechanics is what separates a script kiddie from a security engineer. This article breaks down how to build a lightweight, Python-based brute-force detection engine that parses authentication logs, aggregates failures by IP address, and triggers automated alerts—a fundamental skill for anyone serious about threat detection and security automation.

Learning Objectives:

  • Understand the core logic behind log aggregation and threshold-based alerting.
  • Learn to parse and analyze authentication logs using Python.
  • Build a functional script that detects brute-force attempts and simulates automated responses.

You Should Know:

1. The Anatomy of a Brute-Force Attack

A brute-force attack is a trial-and-error method used to obtain information such as a user password or personal identification number (PIN). In the context of authentication logs, it manifests as a high volume of failed login attempts from a single source IP address over a short period. The challenge for security analysts is that logs are noisy; a single failed password could be a typo, but five failures in ten seconds is almost certainly a malicious scan.

Step-by-step guide explaining what this does and how to use it:
– Identify the Log Source: On Linux systems, authentication logs are typically stored in `/var/log/auth.log` (Debian/Ubuntu) or `/var/log/secure` (RHEL/CentOS). On Windows, the Security Event Log (Event ID 4625) serves the same purpose.
– Understand the Signature: Look for patterns like “Failed password for invalid user” or “authentication failure.” These are the breadcrumbs of an attack.
– Define a Threshold: The script uses a configurable threshold (e.g., 3 or 5 failed attempts) to separate noise from signals. This is a critical parameter that must be tuned to the specific environment.

2. Parsing Logs with Python: The Core Engine

The heart of the detection engine is a Python script that ingests log files, extracts relevant fields (like IP addresses and usernames), and aggregates the data. Python’s built-in file handling and dictionary data structures make it an ideal choice for this task.

Step-by-step guide explaining what this does and how to use it:
– Open and Read the Log File: Use `open()` to read the log file line by line. This is memory-efficient for large files.
– Extract the IP Address: Use regular expressions (re module) to find IP addresses. A common pattern is r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'.
– Aggregate Failures: Create a dictionary where the keys are IP addresses and the values are failure counts. For each failed attempt, increment the count for that IP.
– Sample Code Snippet:

import re

def parse_logs(log_file):
ip_failures = {}
ip_pattern = r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}'
with open(log_file, 'r') as file:
for line in file:
if "Failed password" in line:
match = re.search(ip_pattern, line)
if match:
ip = match.group()
ip_failures[bash] = ip_failures.get(ip, 0) + 1
return ip_failures

3. Implementing Threshold-Based Alerting

Once the data is aggregated, the script must evaluate each IP against the predefined threshold. When an IP exceeds the limit, the system must generate a high-severity alert and, ideally, initiate a response.

Step-by-step guide explaining what this does and how to use it:
– Set the Threshold: Define a variable like FAILURE_THRESHOLD = 3.
– Iterate and Evaluate: Loop through the `ip_failures` dictionary. If count > FAILURE_THRESHOLD, trigger an alert.
– Generate Alerts: Print a formatted alert message to the console. For production, this could be integrated with a SIEM or logging system.
– Simulate a Response: The script can simulate adding the offending IP to a firewall block list. This is a crucial step in automated threat mitigation.
– Sample Code Snippet:

FAILURE_THRESHOLD = 3

def check_thresholds(ip_failures):
for ip, count in ip_failures.items():
if count > FAILURE_THRESHOLD:
print(f"[bash] High-severity: IP {ip} has {count} failed attempts!")
print(f"[bash] Simulating firewall block for {ip}")

4. Automating Response with System Commands

While the script simulates blocking, a production-ready version would integrate with actual firewall rules. On Linux, this could involve using `iptables` to drop traffic from the offending IP. On Windows, PowerShell can be used to modify Windows Firewall rules.

Step-by-step guide explaining what this does and how to use it:
– Linux (iptables): Use `subprocess.run([‘iptables’, ‘-A’, ‘INPUT’, ‘-s’, ip, ‘-j’, ‘DROP’])` to block the IP.
– Windows (PowerShell): Use subprocess.run(['powershell', '-Command', f'New-1etFirewallRule -DisplayName "Block {ip}" -Direction Inbound -RemoteAddress {ip} -Action Block']).
– Caution: Always test these commands in a safe environment first. Automating firewall changes can lock you out of your own system.
– Sample Code Snippet:

import subprocess

def block_ip(ip):
 Linux example
subprocess.run(['iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'])
print(f"[bash] IP {ip} blocked via iptables.")

5. Error Handling and Robustness

A security tool is only as good as its ability to run without crashing. The post mentions that Day 10 focuses on error handling using `try/except` blocks. This is essential for handling malformed log lines, missing files, or permission issues.

Step-by-step guide explaining what this does and how to use it:
– Wrap File Operations: Use `try/except` when opening files to handle `FileNotFoundError` or PermissionError.
– Handle Regex Failures: Ensure the regex pattern doesn’t break on unexpected log formats.
– Graceful Degradation: If a line cannot be parsed, skip it and continue processing. This prevents the entire script from failing on a single bad entry.
– Sample Code Snippet:

try:
with open(log_file, 'r') as file:
 processing logic
except FileNotFoundError:
print(f"[bash] Log file {log_file} not found.")
except PermissionError:
print(f"[bash] Permission denied to read {log_file}.")

6. Integrating with SIEM and Other Tools

While this script is a standalone demonstration, in a real-world SOC, such logic is often embedded within a SIEM platform. The script can be modified to output alerts in a format compatible with SIEM tools (e.g., Syslog, JSON) or to forward data via APIs.

Step-by-step guide explaining what this does and how to use it:
– Output as JSON: Use `json.dumps()` to format the alert data, making it easy for other tools to ingest.
– Send to Syslog: Use the `logging` module to send alerts to a syslog server.
– API Integration: Use the `requests` library to post alerts to a webhook or a SIEM’s REST API.
– Sample Code Snippet:

import json
import requests

def send_alert(ip, count):
alert_data = {"ip": ip, "failed_attempts": count, "severity": "high"}
 Send to a webhook
requests.post('https://your-siem-webhook.com/alert', json=alert_data)

7. Scaling and Performance Considerations

For large environments with millions of log entries, parsing a single file linearly may not be sufficient. Consider using more efficient data structures, parallel processing, or streaming log analysis.

Step-by-step guide explaining what this does and how to use it:
– Use defaultdict: Instead of a standard dictionary, use `collections.defaultdict(int)` for cleaner code.
– Streaming: Use `tail -f` or similar to process logs in real-time.
– Database Storage: For long-term analysis, store aggregated data in a database like SQLite or PostgreSQL.
– Sample Code Snippet:

from collections import defaultdict

ip_failures = defaultdict(int)
 ... increment with ip_failures[bash] += 1

What Undercode Say:

  • Key Takeaway 1: Context is everything in security operations. Aggregating data programmatically turns noise into actionable signals.
  • Key Takeaway 2: Automation is the force multiplier for security teams. By simulating a firewall block, the script demonstrates how to move from detection to response, a critical step in any security framework.

Analysis:

The script built on Day 9 of the 200DaysOfCybersecurity challenge is a perfect example of how foundational programming skills can be applied to solve real-world security problems. It bridges the gap between understanding logs (a core IT skill) and implementing automated threat detection (a core cybersecurity skill). The emphasis on threshold-based alerting is particularly important, as it highlights the need for tuning and understanding the baseline behavior of a network. The progression from simple field extraction to data aggregation and automated response is a microcosm of the security engineering mindset—moving from passive observation to active defense. This approach not only enhances an analyst’s capabilities but also significantly reduces the mean time to detection (MTTD) and response (MTTR).

Prediction:

  • +1 As more organizations adopt DevSecOps practices, lightweight, scriptable security tools like this will become increasingly valuable for embedding security into the CI/CD pipeline.
  • +1 The trend towards automation in cybersecurity will continue to grow, with Python remaining a dominant language due to its simplicity and extensive library support for networking and system administration.
  • -1 However, the reliance on simple threshold-based detection may lead to increased false positives, necessitating the integration of more advanced analytics and machine learning to reduce alert fatigue.
  • -1 The simulated nature of the response highlights a gap; without proper integration into actual firewall or orchestration tools, the script’s value in a live environment is limited. Proper API integrations and secure credential management are essential for production deployments.

▶️ Related Video (86% Match):

🎯Let’s Practice For Free:

🎓 Live Courses & Certifications:

Join Undercode Academy for Verified Certifications

🚀 Request a Custom Project:

Secure, high-velocity infrastructure and disruptive technological engineering. Contact our engineering team for high-tier development and proprietary systems:
[email protected]
💎 Smart Architecture | 🛡️ Secure by Design | ⭐ Trusted by Thousands

IT/Security Reporter URL:

Reported By: Chirag Suthar – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]

💬 Whatsapp | 💬 Telegram

📢 Follow UndercodeTesting & Stay Tuned:

𝕏 formerly Twitter 🐦 | @ Threads | 🔗 Linkedin | 🦋BlueSky