My SQLi adventure or: why you should make sure your WAF is configured properly
An unsanitised parameter in a GraphQL query was passed into a PostgreSQL stored procedure, allowing SQL injection. The author bypassed a misconfigured Cloudflare WAF using several techniques and could extract arbitrary data from the database.
astrocamel.com
Published on astrocamel.com
In August 2021 I was tasked with performing a Web Application security assessment for a large client. The automated scanning tool returned a possible SQL injection which, just like last time, couldn't be exploited using the said tool.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application failed to use prepared statements or parameterised queries, directly concatenating user input into SQL.
Impact
An attacker could bypass the misconfigured Cloudflare WAF and exfiltrate arbitrary data from the database, including table contents.
What the attacker needed
Access to the /graphql endpoint; Cloudflare WAF with some SQLi rules disabled
The fix
The application should use prepared statements to prevent SQL injection, and the WAF should be properly configured with all relevant rules enabled.
How it was found
- Send a POST request to the /graphql endpoint with a valid email to see an 'OK' response, and an invalid one to see 'NOT OK'.
- Trigger an SQL error to obtain the full backend SQL query, revealing where input is reflected and that it is wrapped in parentheses.
- Use the 'right' function instead of 'substr' to bypass Cloudflare's block on 'substr'.
- Remove spaces in the FROM clause and enclose parts in parentheses to bypass a second WAF block.
- Pass a GraphQL parameter value as an array to introduce a comma, then comment out the comma to form a valid SQL query that bypasses the WAF.
- Automate the extraction of database name, username, version, and table data using a Python script.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
A web application took user input from a GraphQL request and inserted it directly into a database query without cleaning it. This let the tester change the query to read any data in the database. A firewall (Cloudflare) was supposed to block such attacks but was not fully configured, so the tester found ways around it.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.