Lecture 8 Database Security
Lecture 8 Database Security
https://www.hacksplaining.com/exercises/sql-injection
2
Database Security principles
Databases are the heart of virtually every modern web application
static and dynamic web pages
3
What is SQL Injection?
https://lankadevelopers.com/ 4
Where does SQL Injection Happens?
Attackers try to find places in the web application where they can input their malicious
SQL query (payload) or enter a command to alter the current query.
Use userinput and passinput
Example:
without validations
Original query:
SELECT * FROM USERS WHERE USERNAME=‘userinput’ AND
PASSWORD=‘passinput’
What does --
mean?
8
https://www.xtrmhack.com/2011/01/sql-injection.html
Where does SQL Injection Happens?
How is SQLi different from XSS?
What did attackers do to find out if the web app is vulnerable to xss?
What did attackers do to find out if the web app is vulnerable to SQLi?
How do they think the original query for this form looks like ?
10
Structured query Language (SQL) injection
SQL injection example:
Attacker’s
input
Classic example of SQL injection is the attack against the login procedure
User’s input
Attacker’s
input
If there is at least one user in the database the attacker will be granted access! 12
The query will return all
SQL injection effects and CIA
Attacker’s
input
What was affected (from the CIA) by the previous SQL injection?
13
SQL injection effects and CIA
What is affected (from the CIA) by the below SQL injections?
Attacker’s
input
14
SQL injection effects and CIA
What is affected (from the CIA) by the below SQL injection?
15
SQL injection effects and CIA
The worst SQL injection attack payload is when the attacker can gain access to
It will enable him to do just about anything he wanted to do to the server, and with
administrative privileges (remove admin, add user, command-line shell,…)
He could read, alter, or delete any data on the system (not limited to data in the database only)
The effects would reach server files
The attacker could install backdoors
The attacker can alter system logs to cover his tracks
All services are affected
It takes a single string argument and then executes that as a command-line call
Example: This command will list the directories and files of the server’s C drive:
However, attackers can re-enable it if the application database user is running with
administrative privileges (we will learn how to change that): Attacker’s
input
Therefore, it’s better to remove it entirely, although it still could be re-created from
scratch but not all attackers know how.
18
Blended threat:
is bundles of
Persistent XSS: attackers can directly send their malicious script to a target website which
stores that script in a persistent storage such as the comments section.
In these blended threat attacks, attackers insert HTML <script> tags into the database records.
find a website vulnerable to SQL injection attack and exploit it to insert HTML <script> tags into
the DB records Attacker’s
input
There is no where
clause means all
middlename will be
change to malicious
UPDATE TABLE UPDATE TABLE
19
java script code
Blended Threat Attack
20
The dangers of detailed errors
How the attackers find out the database table names?
Attackers hope that the web application is set to display detailed error messages
They will try to input a single special SQL character into the input field to produce
a syntax error
Failing closed
Fail securely
21
The dangers of detailed errors
22
The dangers of detailed errors
Attacker’s
Example: enter the following in the filed: ‘ or ; input
23
Blind SQL Injection (Reading
Assignment- P 227)
Attackers can still perform SQL injection attacks even if your system doesn’t display
detailed errors.
Example: Blind SQL injection attack
Ø common table names (users, sales, …..)
Ø ‘ or 1=1 – (no need to know the table name) when you gain access you will get crucial information.
So, not displaying detailed errors is not enough to protect against SQL injection attacks.
24
SQL Injection Defenses
The root cause of SQL injection vulnerabilities is that an attacker can specify data that
is interpreted by the database engine as code. Examples of data:
Form field input values
Stored Procedures 25
SQL Injection Defenses
1-Validate or 2- escape user input
validation –whitelisting blacklisting, escape – encoding for SQL syntax characters in the input.
Example [3]: use PHP’s method real_escape_string($input) to encode characters that has special
meaning in SQL queries such as \n, \r, \, ‘, “
Problems:
It can be bypassed by attackers (check always equals 1=1, ….)
It doesn’t solve the main issue (data can be interpreted as code) 26
SQL Injection Defenses
Prepared Statements (with Parameterized Queries)
It is better to avoid ad-hoc SQL altogether
27
SQL Injection Defenses
Prepared Statements (with Parameterized Queries)
1
2
3 If user enter tom' or '1'=‘1
The DBMS will literally
search for username
Steps: equals to tom' or '1'='1
1 Prepare a parameterized SQL query with empty values as placeholders (with ? for each value).
2 Bind variables to the placeholders by stating each variable, along with its type.
3 Execute query
28
All the previously explained SQLs are called dynamic query
SQL Injection Defenses
Stored procedures
Easier to maintain than dynamic SQL built into the application source code
Make changes to stored procedures without the need to recompile the application
29
SQL Injection Defenses
Stored procedures
Use parameterize SQL queries within the stored procedures, why?
The command EXECUTE will execute any string passed to it. If you use it, with ad-hoc query, you’ll create a stored
procedure that’s vulnerable to SQL injection attacks:
Stored procedure with ad-hoc query (vulnerable to Stored procedure with parameterize query (secure against
SQL injection): SQL injection):
Attacker’s
input
30
Setting database permissions
A good way to reduce the potential attack surface of your application is to explicitly
deny it the permissions to perform actions that’s not supposed to be able to do.
trade off between security and functionality.
32
Setting database permissions
Single Account Security (cont)
you can add or remove necessary privilege either by: GUI, SQL command
Ø Revoke or grant
33
Setting database permissions
Separate Accounts For Separate Roles
Anonymous users
Administrators
Authenticated users
functions
Anonymous users
functions
34
Setting database permissions
Separate Accounts For Separate Roles (cont)
Single database user à one account with a lot of privileges
Create roles for different users. Ex: employees, managers, executives …etc.
35
Finally, to maximize the security of
your database:
37
References
[1] Web Application Security: A Beginner’s Guide
Chapter 7
[2] https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
[3] https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection
38