© 2018 Caendra Inc. - Hera For Waptv3 - SQL Injection
© 2018 Caendra Inc. - Hera For Waptv3 - SQL Injection
© 2018 Caendra Inc. - Hera For Waptv3 - SQL Injection
Once you are connected in VPN to the lab environment, all the web applications will be
available at the following URL: http://info.sqli.site/.
There are three main sections for each type of lab: Video, Lab, Challenges.
• Video section contains web applications used during video lessons. Therefore, if
you need any information about the scenario, the attacks and so on, please refer to
the corresponding video.
• Labs section contains web application where you can practice the techniques of the
specific module and have solutions. You can find them later in this manual
• Challenges labs do not have solutions; otherwise, why call them challenges? If you
study the course and think like a penetration tester, you will achieve the goal!
The best tool is, as usual, your brain. Then you may need of:
• Web Browser
• Burp Suite
• Sqlmap
Once you have your virtual network ready, configure the following IP address as default
DNS: 10.100.13.37
• WINDOWS: change the property of the TAP network device, adding as first DNS
server of the IP of the server.
• LINUX: add an entry into /etc/resolv.conf file with the IP address of the server
You know that the DBMS is MySQL and that the parameter Header User-agent on the
page getBrowserInfo.php is not sanitized correctly by the web application. This means
that it is processed by the DBMS, and is injectable.
The injection is a BLIND SQL injection, so you cannot use the UNION technique to extract
your data.
You will detect the vulnerable page, then exploit it to extract any data that you can from the
database.
They have put up a website where they can share with the world their reading preferences,
preferred authors and photos.
The club has a Vice President of Bookshelf and a Club President. They are the most sage
and blessed members and live a life of books, introspection and asceticism
You want to find SQL injection vulnerabilities in the website, determining the type of SQL
injection and its exploitability You will also have to extract information from the database
exploiting the vulnerability
They have put up a website where they can share with the world their reading preferences,
preferred authors and photos.
The club has a Vice President of Bookshelf and also a Club President. They are the most
sage and blessed members and live a life of books, introspection and asceticism.
You want to exploit an Error based SQL injection manually in order to dump data from the
database. You will have to first determine the remote DB version, current user and selected
database. Then proceed to schema enumeration and data dump
• Username: giovanni
• Password: mycoolpass
This bank is particularly arrogant and makes fun of poor account holders. You happen to be
the poorest account holders (hence frustration).
You want to find SQL injection vulnerabilities in the website, determining the type of SQL
injection and its exploitability. You will also have to extract information from the database
exploiting the vulnerability
They have put up a website where they can share with the world their reading preferences,
preferred authors and photos.
The club has a Vice President of Bookshelf and also a Club President. They are the most
sage and blessed members and live a life of books, introspection and asceticism.
Website has recently undergone further development and bug fixes although database
structure has not changed.
You want to find SQL injection vulnerabilities in the website, determining the type of SQL
injection and its exploitability.
You will also have to extract information from the database exploiting the vulnerability.
This time you have to determine whether you can use a tool and eventually if you can use
any other manual and more direct technique
The attacker can only tell that the injection has been run (with or without a result set) and
nothing else. In other words, the web server returns only a BOOLEAN value but no
database data.
The attacker must ask a simple Boolean question to the web server to determine success.
For example, to get the first character of the current schema, the attacker could ask:
This approach can take a very long time and is not optimal, of course. For example, to get
the complete name of the current schema (suppose a characters domain: [a-z]) the attacker
could perform 26*N (the length of the schema name) injection queries in the worst case.
Most of the injection tools make use of bisection algorithms. These algorithms perform
operations on the ASCII encoding of the characters and reduce the characters domain to
test.
In the previous example, for instance, an attacker could ask the following question:
• Is the encoding ASCII of the first character of the current schema < encoding
ASCII (‘o’)?
If the web application replies with TRUE, the next attempt will be tried from all characters
with ASCII encoding < ‘o’ : [a-o]. If the web app replies with FALSE, the next attempt will be
from among all characters with ASCII encoding > ‘o’ : [p-z].
This step is fundamental in building the injection payload (Tasks 2 and 3). You need to find
out which input data forces the query to return a result set and which does not.
You don’t know how the web application has been developed but it is likely that the web
application makes use of a similar SQL query to process the browser.
The web application reacts by sending the message ‘Detected a new browser’; this message
is obviously related to the FALSE condition and you will receive it when your payload
causes the query to have an empty result set.
If the web server replies with the message related to the TRUE condition (“It’s nothing
new”), the answer to your question was true, otherwise the question is wrong and you
must ask a new one.
For example, to get the first character of the current schema name, you could iterate all the
following queries until you reach the TRUE condition.
In this task, you will extract some data manually. As an example, you will need to get only
the current DB user running the queries for the web application.
After that, you will use sqlmap to extract any extra data from the database.
The first step is to get the current user of the database. We know that the DBMS is MySQL,
so you can use the following query to get it:
Let us demonstrate how to run the exploit. You will use the bisection technique to get the
user value, character by character quickly, and you can assume that the character domain
of the user value is [a-z].
The web application returns the message related to the FALSE condition, so you can tell
that the target character will be in the smaller interval [n-z].
The web application returns the message related to the FALSE condition, so you are sure
that the target character will be in the reduced interval [u-z].
The web application returns the message related to the TRUE condition, so you are sure
that the target character will be in the reduced interval [u-w].
The web application returns the message related to the TRUE condition, so you are sure
that the target character will be in the reduced interval [u-v].
The web application returns the message related to the TRUE condition, as the interval
contains only one element you can conclude that it is the right one. So the first character of
the user name is ‘u’.
If you continue with the other characters of the name, you will get this value:
• user
sqlmap.py -u http://s1-
17395bzxy.roma.coliseumlab.net/getBrowserInfo.php --dbs --dump -
-technique=B --keep-alive --level=3
Where
• --dbs
o Enumerate DBMS databases
• --dump
o Dump DBMS database table entries
• --technique=B
o SQL injection technique to test for
o B stands for Boolean based SQL injection
• --keep-alive
o Use persistent HTTP(s) connections
• --level=3
o Level of tests to perform
o The HTTP User-agent header is tested against SQL injection only if the
level is set to 3 or above
The web application can be designed to receive parameters via different methods. The
most commonly used methods are:
• POST
o The parameters are available in the content of the HTTP POST request.
• GET
o The parameters are available in the URL of the HTTP GET request.
• COOKIE
o The parameters are available in the COOKIE header of the HTTP request.
But, the web application could process parameters from any HTTP Request header, for
example:
• User-Agent
• Referrer
• Accept-Language
• Etc.
Your first step is to configure Burp proxy to draw a map of the HTTP requests made from
your browser. First, configure the scope, copy the main URL of the web application and use
the Paste URL button in Burp proxy.
Note that in this step, there is really no need to intercept the requests / responses because
your goal is simply to draw a map.
Come back to Burp suite to gather the HTTP requests made by your web application and
select the Target tab, then the Site map sub-tab.
Here, you can review each of the HTTP requests made by your browser. By default, the
HTTP requests in the list are filtered by MIME type: you will not be able to see any requests
for images or .css files.
• GET /getBookInfo.php?id=<bookID>
• GET /getBrowserInfo.php
The first request is a parameterized request and uses the parameter id to get information
about a given book from the database.
The second request does not use a GET/POST parameter but the name related to the page
(getBrowserInfo) could suggest that the User-Agent HTTP header is being processed by
the web application.
Note that as the attacker, you do not know how the web application has been coded. A
complete pen test against SQL injections would include a check of each possible parameter
(on GET, POST or any HTTP header). This would, of course, take an extremely long time.
So, you have restricted your search to few pages. Now, you should check if any of the
following parameters are vulnerable:
• GET.id
o On the URL /getBookInfo.php?id=<bookID>
• Header User-Agent
For each parameter that you found above, you should send a probe payload. This is a
special payload that you will use to check whether the parameter is vulnerable or not.
You will need to send this probe data and then compare the behavior of the web
application with the default behavior. (i.e.: you need to determine if the server’s response
changes with differing inputs.)
Parameter GET.id
First, select the request that you need to send probe data to from the Site Map and send it
to Burp Repeater.
• id: '
Invalid query: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the
right syntax to use near ''' at line 1
It should be pretty clear, now, that the web application does not filter the quote character,
so you should take this as a hint to try and inject your payload here.
It’s likely that the web application makes use of a SQL query to get data about a book using
its id.
Or you can use the following payload to make the query always return FALSE:
The injection can be exploited with the UNION technique, because the HTTP response after
the injected payload returns data from database (the TRUE Boolean condition tested above
returned data about the Merchant of Venice book).
Select the HTTP request from the Site map and send to Burp Repeater.
Now go to the Repeater tab, update the HTTP User-agent request header with your test
probe and press the Go button:
If you repeat that HTTP request, you will get the same message; note that this behavior
differs from default.
If you send multiple requests with the same User-Agent (for example: Chrome) you will get
two different responses:
• For the first request, you will get a response containing the message
‘Detected a new browser’
• For all subsequent identical requests, you will get a response containing the
message ‘It's nothing new’
At first glance, you can guess that the header User-agent seems to be processed by the
web application and stored into the database.
When the web application receives a request from a new browser, that request seems to be
stored, and the message ‘Detected a new browser’ will be sent.
It is likely that the web application makes use of a similar SQL query to check if the browser
has previously been detected.
It is also likely that the web application makes use of a similar query to add the browser to
the detected browsers list.
Don’t forget that as the attacker, you may not know the code of the web application but you
must think like the developer of that web application.
You have, of course, noticed some strange behavior : the web application responds with the
message (‘Detected a new browser’) when you send a test probe using the following
payload:
It could be a blind SQL injection; to confirm that, you should build two different payloads
verifying two Boolean conditions.
You can use the following payload to make the query always TRUE (TRUE Boolean
condition):
You can use the following payload to make the query always FALSE (FALSE Boolean
condition):
In both of the preceding cases, the web application does not return any information from
the DB or any error message but the injection is obviously available.
This type of injection is called Blind SQL injection; to exploit these, the attacker can run
queries satisfying simple Boolean conditions.
Let us try to show you a simple example. Consider a web application vulnerable to SQL
injection in the parameter id of the URL
• http://www.elsfoo.com/product.php?id=999 .
We will assume that the web application uses Oracle as its DBMS.
The attacker can insert special input that can force the DBMS to generate an error; for
example, by requesting the following URL:
http://www.elsfoo.com/product.php?id=999||UTL_INADDR.GE
T_HOST_NAME( (SELECT user FROM DUAL) )--
He will be able to read the information he really wants (the user) from the error returned
by the DBMS.
Three conditions are necessary to determine the existence of an Error-Based SQL injection:
1. The DBMS
o Currently only MSSQL and Oracle display this error
2. The web application must collect DBMS errors and returns them to the client.
3. The web application does not filter input data properly permitting to an attacker
to run arbitrary SQL code.
If any of the previous conditions are not verified, the application may be immune to error-
based injection or SQLi altogether.
If you insert the probe input ' you will get the following error from the MSSQL database:
The error message that you got indicates that the parameter id is vulnerable to SQL
injections and that the injection is Error-based because:
You need to execute some specific queries to get this information. For example, the query
to get the DBMS version or the current user is different from DMBS to DBMS.
You can use this document MSSQL injection cheat sheet as a source for the resources you
may need.
As the attacker, you need to force the DBMS to generate an error that includes the
information that you need.
In the next examples, we will use the CAST statement to force the DBMS to convert the
necessary information to a string. We will compare this value (string type) to another
value (int type) with the only goal of triggering a DB error and reading the necessary
information from the error.
where
You will get an error message because you are comparing two values of different types and
you will able to extract the target information you need from the error.
Current user
The user used by the web application to connect to the DBMS is : user .
Current database
Use the following payload to get the name of the first table in the schema:
To get the other tables you must perform the same query excluding the tables values that
you’ve already found.
• product
• purchase
• user
Let us show you how to get the columns of the table user.
To get the other columns, you must perform the same query excluding the column values
that you’ve already found.
At the end of this process you know that the table user contains the following columns:
• id
• username
• password
• name
If you want to extract data by using sqlmap, you must run the following command:
./sqlmap.py -u http://s1-172-
sbh.brundisium.coliseumlab.net/getBookInfo.php?id=1 --dbs --dump
--technique=E --keep-alive -p id
Where
• --dbs
o Enumerate DBMS databases
• --dump
o Dump DBMS database table entries
• --technique=E
o SQL injection technique to test for
o E stands for Error-Based SQL injection
• --keep-alive
o Use persistent HTTP(s) connections
• -p id
o Testable parameter
The following snapshot shows all of the content from the table purchase: