Home / Knowledge hub / SQL injection
Knowledge hub
SQL injection
Input a database ends up running as part of a query.
What it means
SQL injection happens when input from outside ends up as part of a database query instead of as data inside it. The database cannot tell the difference, so it runs what you handed it.
From there an attacker reads tables you never meant to show, or quietly changes records. In some setups they reach the server the database runs on, which is remote code execution.
Why it matters
It sits behind a great many of the breaches you have read about, and it is the class most likely to expose everything at once. One vulnerable parameter can be enough to take a whole customer table.
It is also decades old and completely understood, which makes it an expensive thing for a buyer to find in your application. No incident report carries a sympathetic explanation for it. The damage rarely stops at one table either. Once an attacker can rewrite a query, they usually reach everything that database user can touch.
How it shows up
It lives wherever code assembles a query by joining strings: a search box, a sort order, a filter in a report, an id in a URL. Modern frameworks make the safe version the default, so the flaw usually turns up in the one hand written query somebody added under pressure.
Parameterised queries fix it. You tell the database the structure first and the values separately, so input can never change the shape of the query. The database account matters as well. An application connecting as an administrator turns a read into a rewrite, and narrowing those permissions limits what a mistake becomes. Test the paths nobody demonstrates, too. Export routines, admin filters and report builders all reach the database, and none of them appear in a product demo.
Questions people ask
SQL injection, answered
Is SQL injection still a thing?
Yes. It still appears in breach reports, and it still appears in new code, usually in the one query somebody assembled by hand.
The technique is old. The supply of hand written queries is not.
Does an ORM protect me from SQL injection?
Mostly, while you use it normally, because an ORM parameterises queries underneath.
Every ORM also offers a raw query escape hatch, and that is where injection comes back. Search your codebase for those calls and read each one.
Do prepared statements stop every kind of SQL injection?
They stop the values. You cannot parameterise a table name, a column name or a sort direction.
Those need an allowed list of permitted values, checked before your code builds the query.
Will a WAF protect us while we fix the code?
It raises the effort for an attacker, and it is not a fix. Encodings and unusual syntax get past filters regularly.
Cyberlop does not assess firewall or WAF configuration. It tests the application sitting behind them.
How do I check my own site for SQL injection?
Start with the parameters that reach a query: search, filters, sort orders and identifiers in URLs.
PortSwigger's Web Security Academy has labs that show the behaviour to look for, which is a better starting point than a list of payloads.
Related
Terms that sit next to this one
Injection
Input that ends up running as a command instead of being read as text.
Input validation
Checking that what arrives is the shape and range you expected.
Untrusted input
Anything that arrived from outside your own code, and is therefore suspect.
Data breach
Information you hold ends up somewhere it should not be.
Remote code execution
Getting a server to run commands of an attacker's choosing.
One hand written query left somewhere?
Injection counts as one of the ten classes Cyberlop tests, and a finding arrives with the request, the response and the source line behind it. Try one application for $199.
Or start with a $199 pilot on one application: thirty days, success criteria agreed before day one, credited against the annual if you convert.