How to Spot SQL Injection Attempts in WAF Logs (and Stop Them)

Identify Sql Injection Attacks

How to Spot SQL Injection Attempts in WAF Logs (and Stop Them)

SQL injection is still one of the most common ways attackers break into web applications. If your application sits behind a web application firewall (WAF), its logs are often the first place an attempt shows up. This guide covers what SQL injection attempts look like in WAF and web server logs, how to tell a failed probe from a possible breach, and how to make sure the attempts can’t succeed in the first place.

What SQL injection is

An application is vulnerable to SQL injection when it builds a database query by pasting user input straight into the SQL text. An attacker can then send input that changes the meaning of the query: reading data they shouldn’t see, logging in without a password, changing records, or in some setups running commands on the database server. Injection has been in the OWASP Top 10 list of the most critical web application risks since the list began.

Signs of SQL injection in your logs

Look at query strings, form fields (in POST bodies, if your WAF logs them), cookies and headers. Decode URL encoding first: attackers often write a space as %20 or + and a quote as %27 to slip past simple filters.

PatternWhat to look for
Quotes and comment markersA single quote, double dash (--), # or /* in a field that should hold a number, a name or a product ID. Attackers use these to close the original query string and cut off the rest of it.
Always-true conditionsAn OR followed by a comparison that is always true, such as a number compared with itself. This is the classic attempt to bypass a login or return every row.
SQL keywords in inputUNION together with SELECT, or references to information_schema, in a search box or ID parameter. These try to read other tables.
Error-based probingInput containing type conversions or database functions, followed by HTTP 500 responses. The attacker is trying to make the database print data in its error messages.
Time delaysFunctions such as SLEEP, WAITFOR DELAY or pg_sleep, and responses that take several seconds longer than normal. This is “blind” injection: the attacker learns the answer from how long the page takes.
Out-of-band callsInput that references DNS lookups or URLs, followed by outbound DNS or HTTP traffic from your database server. The attacker tries to send data to a server they control.
Many small variationsHundreds of requests from one IP address that differ only in the injected part, often within minutes. This is usually an automated scanner such as sqlmap working through payloads.

A quick first pass on a web server access log is a case-insensitive search for the most common markers, after URL decoding. For example:

python3 -c "import sys,urllib.parse;[print(urllib.parse.unquote_plus(l),end='') for l in sys.stdin]" < access.log \
  | grep -Ei "union[[:space:]]+(all[[:space:]]+)?select|information_schema|sleep\(|waitfor[[:space:]]+delay|benchmark\(|pg_sleep"

This finds obvious probes, not every attack. A WAF with a maintained rule set, such as the OWASP Core Rule Set, does this far more thoroughly, and its logs tell you which rule matched.

Blocked probe or real breach?

Most SQL injection traffic is automated scanning that never gets through. These signs suggest an attempt may have worked and needs investigating:

  • The request was allowed by the WAF (or only logged, not blocked) and the application answered with HTTP 200.
  • The response size is much larger than normal for that page, which can mean extra rows were returned.
  • A run of HTTP 500 errors from the same source, followed by successful responses, which suggests the attacker found a working query.
  • Response times that match delays in the input, which means a blind injection is working.
  • Unusual queries, failed logins or new accounts in the database logs at the same time.

If you see these, treat it as a possible incident: preserve the logs, find which endpoint and parameter were used, check what data that query could reach, rotate the database credentials the application uses, and fix the vulnerable code before anything else.

How to prevent SQL injection

A WAF is a safety net, not a fix. Attackers regularly find encodings that slip past rules. The real fix is in the code.

1. Use parameterised queries everywhere. Send the query and the user’s input to the database separately, so input can never be read as SQL. Every major language supports this:

// PHP (PDO)
$stmt = $pdo->prepare( 'SELECT * FROM products WHERE category = ?' );
$stmt->execute( array( $category ) );

// WordPress
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}orders WHERE user_id = %d", $user_id ) );

# Python
cursor.execute( "SELECT * FROM products WHERE category = %s", (category,) )

ORMs such as Django, SQLAlchemy, Hibernate and Eloquent parameterise for you, as long as you don’t build raw SQL strings yourself.

2. Allow-list what can’t be parameterised. Table names, column names and sort directions can’t be passed as parameters. Accept only values from a fixed list, for example a sort field that must be one of name, price or date.

3. Give the application a low-privilege database account. It should have only the permissions it needs on its own tables: no admin rights, no access to other databases, and no ability to read files or run system commands. Then a successful injection reaches far less.

4. Hide database errors from users. Show a generic error page and log the details on the server. Detailed SQL errors give attackers a map of your database.

5. Keep a WAF in blocking mode, and watch its logs. Use a maintained rule set, review what it blocks, and alert on the “possible breach” signs above rather than on every blocked probe.

6. Patch and test. Many SQL injection incidents come from flaws in off-the-shelf software and plugins, not your own code. Keep them updated, and include SQL injection checks in code review and security testing.

Further reading