Get a Pentest and security assessment of your IT network.

Cyber Security

Stop Brute Force Attacks with Input Validation

TL;DR

Yes, using tables of valid inputs (whitelists) can significantly reduce the risk of brute force attacks. Instead of trying to block all possible bad inputs (blacklists), you only allow known good ones. This is much more secure and efficient.

How Brute Force Attacks Work

Brute force attacks involve systematically guessing usernames, passwords, or other sensitive data until the correct one is found. They rely on trying every possible combination within a given range. This can be slow, but effective against weak credentials or poorly protected systems.

Why Input Validation Helps

Input validation checks whether user-provided data meets specific criteria before it’s processed. Using a whitelist approach – allowing only pre-defined valid inputs – makes brute force attacks much harder because attackers can’t submit guesses that aren’t in the allowed list.

Steps to Prevent Brute Force Attacks with Input Validation

  1. Identify Sensitive Inputs: Determine which fields are vulnerable to brute force attempts. Common examples include:
    • Login usernames
    • Password reset tokens
    • API keys
    • Form submission data (e.g., specific codes)
  2. Create a Table of Valid Inputs: Compile a list of all acceptable values for each sensitive input. This could be stored in:
    • A database table
    • An array within your code
    • A configuration file

    For example, if you’re allowing only specific API keys:

    valid_api_keys = ["abcdef123456", "ghijkl789012", "mnopqr345678"]
  3. Implement Validation Logic: In your application code, check each input against the whitelist before processing it. Reject any input that isn’t found in the table.

    Here’s a Python example:

    def validate_api_key(api_key):
      if api_key in valid_api_keys:
        return True
      else:
        return False
  4. Handle Invalid Inputs Securely: Don’t just display a generic error message like “Invalid input”. This gives attackers information. Instead, provide a vague message such as “Incorrect details” or log the attempt for security monitoring.
  5. Rate Limiting (Important Addition): Combine whitelisting with rate limiting to further reduce brute force risks. Limit the number of attempts allowed from a single IP address within a specific timeframe. This prevents attackers from rapidly trying multiple valid inputs if they obtain them.

    Example using a simple counter:

    attempt_counts = {}
    
    def check_rate_limit(ip_address):
      if ip_address not in attempt_counts:
        attempt_counts[ip_address] = 0
      attempt_counts[ip_address] += 1
      if attempt_counts[ip_address] > 5:
        return False # Rate limit exceeded
      return True
  6. Regularly Update the Whitelist: If your valid inputs change (e.g., new API keys are generated), ensure you update the whitelist accordingly.
  7. Consider Case Sensitivity: Decide whether your validation should be case-sensitive or not, and implement it consistently. For example, “Admin” and “admin” might need to be treated differently.

Benefits of Whitelisting

  • Increased Security: Significantly reduces the attack surface compared to blacklisting.
  • Improved Performance: Checking against a whitelist is generally faster than complex blacklist rules.
  • Reduced False Positives: Avoids accidentally blocking legitimate users.

Limitations

  • Maintenance Overhead: Requires keeping the whitelist up-to-date, which can be time-consuming for large lists of valid inputs.
  • Not Suitable for All Inputs: Whitelisting isn’t practical for inputs with a very wide range of possible values (e.g., free-form text fields). In those cases, use other validation techniques like input sanitization and length restrictions.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation