Summary
Injection of arbitrary SQL statements into a query.
To exploit this kind of vulnerability, the query must be appended an unsanitized (or not completely sanitized) user-controlled parameter.
For example, let's imagine this background: ($id being a user-supplied GET/POST parameter without previous sanitization)
In an everyday situation, this query would consist of a different set of numbers
.
However, as the user controls the variable, a query can't also supply an intended id but more characters
to make the query longer and extract data
.
In this example, the injected query would return rows whose id
equals 2
OR
whose user
equals admin
. I can't think of an environment where this injections makes sense, but let's think about this one
.
On the backend, there's a line checking whether the query returns >0
rows. If it does, the login is successful
because there's a user that fits our input.
>0
rows. If it does, the login is successful
because there's a user that fits our input.An query that would be returning the intended
result:
However, as seen before, user or password fields can contain more characters
.
Here's where the most common injection occurs. The famous OR 1=1
. Can you think of what's actually happening here? Well, the thing is that the first query
would return jorge's row
if a user with that user and password existed. In the second one
, it will return the entire database
, as 1 will always equal 1, and an OR operator
is being used.
This is not the only occasion where an injection might be successful. As SQL has lots of methods, this injections can be (non)stacked, and in clauses
such as INSERT, UPDATE, SELECT, WHERE, ORDER BY, etc.
Stacked queries
This queries allow using ;
, so more than one query can be executed.
Last updated