W11-Attacking Data Stores
W11-Attacking Data Stores
ICT2214—Web Security
Compiled vs interpreted languages
Compiled languages:
Interpreted languages:
• Code is converted into machine instructions
• Their execution involves a runtime
before runtime
component that interprets the language’s
• At runtime, these instructions are executed
code and carries out the instructions
directly by the processor
• Vulnerable to code injection attacks
• Not vulnerable to code injection attacks that
• An attacker can supply a crafted input that
leverage the language’s grammar
produces a valid instruction within the
• However, they are vulnerable to injection of
language’s grammar
machine instructions
• Examples: PHP, SQL, Perl, etc.
• Examples: C/C++, Java, etc.
2
SQL injection attacks
3
A simple example: bypassing a login
For example, assume the credentials are stored in a table called users, with two
attributes (columns), namely username and password
A simple SQL query can then be used to validate each login attempt:
This query causes the database to check every row within the users table and
extract the record that matches the given credentials (if any)
4
A simple example: bypassing a login
If an attacker knows that the username of the administrator is admin, he can log in
as that user by supplying any password with the following username: admin' --
1. Provide unexpected syntax that may cause problems within the context of the
particular interpreted language
2. Identify any anomalies in the application’s response that may indicate the presence of
a code injection vulnerability
3. If any error messages are received, examine these to obtain evidence about the
problem that occurred on the server
4. If necessary, systematically modify your initial input in relevant ways in an attempt
to confirm or disprove your tentative diagnosis of a vulnerability
5. Construct a proof-of-concept test that causes a safe command to be executed in
a verifiable way, to conclusively prove that an exploitable code injection flaw
exists
6. Exploit the vulnerability by leveraging the functionality of the target language
and component to achieve your objectives
6
Exploiting a basic vulnerability
The following query retrieves information about employees, based on their last
name and ID:
This query was constructed by the programmer when the application was
created
The expressions King and 100 are supplied by the user via the web application
7
Exploiting a basic vulnerability
String data in SQL queries must be encapsulated within single quotation marks to
separate them from the rest of the query
Assume a user enters the following last name to query the database: O'Reilly
The database system determines that the last name is 'O' and then sees the
expression Reilly' which is not valid SQL syntax
The interpreter will output an error which is an indication that the application is
vulnerable to SQL injection
8
Exploiting a basic vulnerability
We can take advantage of this vulnerability to print the salaries of ALL employees
without knowing their names or IDs
Issue the following query for arbitrary ID and a name with the following format:
John' OR 1=1 --
Because of the OR operator, the WHERE clause matches every database record
Note that, instead of the double dash, you may use a hashtag (#) to mark the
beginning of a SQL comment: John' OR 1=1 #
9
Balancing the quotes
In the previous example, one purpose of the SQL comment (--) is to remove the
trailing quotation mark that would cause a syntax error
Of course, in our example, we also need the comment to remove the AND
operator
John' OR 'a'='a
10
INSERT statements
INSERT statements are used to create a new row of data within a table
The application then inserts the details into the users table as follows:
11
INSERT statements
For the injection to be successful, the supplied data must contain the correct
number of data items of the correct types
For example, injecting into the username field, the attacker can supply the
following:
Assuming that the privs field is used to determine account privileges, this may
enable the attacker to create an administrative user
12
INSERT statements
If you do not know the number and/or type of attributes (columns), you may try
different queries until the operation is successful
foo') --
foo', 1) --
foo', 1, 1) --
foo', 1, 1, 1) --
If the value 1 is rejected, you can try the value 2000, which many databases also
implicitly cast to date-based data types
13
UPDATE statements
UPDATE statements are used to modify one or more existing rows of data within a
database table
It works much like an INSERT statement, except that it usually contains a WHERE
clause to tell the database which rows of the table to update
This query verifies whether the user’s existing password is correct and, if so,
updates it with the new value
You can update the password of the admin user by entering the following
username: admin' --
14
Finding SQL injection bugs
15
Injecting into string data
16
Injecting into string data
17
Injecting into string data
18
Injecting into numeric data
19
Injecting into numeric data
20
Injecting into numeric data
21
Common mistake
Certain characters have special meaning within HTTP requests and, if you want to
include them within your attack payloads, you must URL-encode them
& and = are used to join name/value pairs to create the query string and the block of
POST data. You should encode them using %26 and %3d, respectively
Literal spaces are not allowed in the query string. You should encode them using + or
%20
Because + is used to encode spaces, if you want to include an actual + in your string,
you must encode it using %2b. For example, 1+1 should be submitted as 1%2b1
The semicolon is used to separate cookie fields and should be encoded as %3b
22
Injecting into the query structure
23
Injecting into the query structure
If the column name title in the ORDER BY is specified by the user, it is not
necessary to use a single quote
This means that the response will be the same regardless of what the attacker
submits
24
Injecting into the query structure
• Make a note of any parameters that appear • If the number supplied is greater than the
to control the order or field types within the number of columns in the result set, the
results that the application returns query should fail
• Make a series of requests supplying a • In this situation, you can confirm that further
numeric value in the parameter value, SQL can be injected by checking whether the
starting with the number 1 and incrementing results order can be reversed, using the
it with each subsequent request following: 1 ASC -- or 1 DESC --
• If changing the number in the input affects • If supplying the number 1 causes a set of
the ordering of the results, the input is results with a column containing a 1 in every
probably being inserted into an ORDER BY row, the input is probably being inserted into
clause the name of a column in a SELECT clause
25
The UNION operator
26
The UNION operator
Let's consider the HR application again and how we can leverage the powerful
UNION operator to dump the password table
The UNION operator can be used to inject a second SELECT query and append its
results to those of the first. Just use the following search term for the last name:
27
Conditions for the UNION operator to succeed
28
Identifying the number of columns
• You can exploit the fact that NULL can be • Your next task is to discover a column that
converted to any data type to systematically has a string data type so that you can use
inject queries with different numbers of this to extract arbitrary data
columns until your injected query is • You can do this by injecting a query
executed: containing NULLs, as you did previously, and
systematically replacing each NULL with 'a'
' UNION SELECT NULL --
' UNION SELECT NULL,NULL -- ' UNION SELECT 'a', NULL, NULL --
' UNION SELECT NULL,NULL,NULL -- ' UNION SELECT NULL, 'a', NULL --
... ' UNION SELECT NULL, NULL, 'a' --
Suppose that we have already performed the initial tests, and determined that
the query returns 3 columns and the first 2 contain string data
We can retrieve all the table/column names in the database with the following
search term:
30
Extracting more information
Once we have the names of all tables and columns, we can focus on extracting
sensitive information by examining the listed names
For example, let's look at the dependents table and try to extract the dependent
names and the IDs of the corresponding employees:
Then, we can issue a query to get the names and IDs of all employees, and
combine the results to match employees/dependents
31
Column concatenation
When multiple columns are returned from a target table, these can be
concatenated into a single column
32
Acknowledgements
This set of slides for AY24/25 Tri 2 was prepared/adapted from the hard work of previous
professors who taught this module, namely:
• Prof. Weihan
• Prof. Linda
• Prof. Liming
• Prof. Spiros
33
Web Applications |2024
Goh Weihan | January