You are on page 1of 7

This Lecture

• General Database Security


• Privileges
Database
Database Security
Security • Granting
• Revoking
• Views
Database
Thanhs
Thanks to Michael Systems
Pond for sharing slides • SQL Insertion Attacks
Michael Pound • Further Reading
• The Manga Guide to Databases, Chapter 5
• Database Systems, Chapter 20

Database Security DBMS Security Support


• Database security is • Many aspects to • DBMSs can provide • The DBMS verifies
about controlling access consider for security some security password and checks
to information • Physical security a user’s permissions
• Each user has an
• Some information • OS/Network security
should be available
account, username when they try to
• Encryption and
freely and password • Retrieve data
passwords
• Other information • DBMS security • These are used to • Modify data
should only be available identify a user and
to certain people or • This lecture we will • Modify the database
control their access
groups focus mainly on DBMS structure
to information
security

Permissions and Privilege Privileges in SQL


• SQL uses privileges to • The owner (creator) of a GRANT <privileges> • <users> is a list of
control access to tables database has all ON <object> user
and other database privileges on all objects TO <users>
objects. E.g. in the database, and [WITH GRANT OPTION]
• <object> is the
• SELECT privilege can grant these to name of a table or view
• INSERT privilege others <privileges> is a list of
• UPDATE privilege • The owner (creator) of SELECT (<columns>),
• CREATE privilege an object has all INSERT (<columns>), • WITH GRANT
privileges on that object DELETE, OPTION means that
• In MySQL there are
and can pass them on UPDATE (<columns>), the users can pass their
actually 30 distinct
to others or simply ALL privileges on to others
privileges

1
Privileges Examples Removing Privileges
GRANT ALL ON GRANT SELECT, • If you want to remove a • For example:
Employee UPDATE(Salary) privilege you have
TO Manager ON Employee granted you use REVOKE
WITH GRANT OPTION; TO Finance; UPDATE(Salary)
ON Employee
REVOKE FROM Finance
• The user ‘Manager’ can • The user ‘Finance’ can
do anything to the view the entire Employee <privileges>
Employee table, and can table, and can change REVOKE ALL
ON <object>
allow other users to do Salary values, but cannot PRIVILEGES, GRANT
the same (by using change other values or FROM <users>; OPTION FROM
GRANT statements) pass on their privilege Manager

Removing Privileges Removing Privileges


• Example • Manager’ revokes ALL
• ‘Admin’ grants ALL Admin from ‘Personnel’ Admin
privileges to ‘Manager’, • ‘Personnel’ still has
and SELECT to ‘Finance’ SELECT ALL SELECT privileges from SELECT ALL
with grant option ‘Finance’
• ‘Manager’ grants ALL to Finance Manager Finance Manager
Personnel
• ‘Finance’ grants SELECT SELECT ALL SELECT

to Personnel
Personnel Personnel

Removing Privileges Views


• Manager’ revokes ALL • Privileges work at the • Views provide ‘virtual’
from ‘Personnel’ Admin level of tables tables
• ‘Personnel’ still has • You can restrict access • A view is the result of a
SELECT privileges from ALL by column SELECT statement which
‘Finance’ • You cannot restrict is treated like a table
Finance Manager • You can SELECT from
• ‘Admin’ revokes SELECT access by row
from ‘Finance’ • Views, along with (and sometimes UPDATE
etc) views just like tables
• Personnel also loses privileges, allow for
SELECT Personnel customised access

2
Creating Views View Example
CREATE VIEW <name> • Example Student
AS • We want each university
<select statement>; tutor to be able to see
sID sFirst sLast sYear
marks of only those
students they actually Enrolment
• <name> is the name of teach sID mCode eMark eYearTaken
the new view • We will assume our
• <select statement> is a database is structured
Module
query that returns the with Student, Enrolment,
Tutors and Module mCode mTitle mCredits
rows and columns of tables similar to those
the view seen in previous lectures
Tutors Lecturers
lID sID lID lName lDept

View Example Database Integrity


CREATE VIEW TuteeMarks • Database Security • Database Integrity
AS • Database security makes • Ensures that authorised
SELECT sID, sFirst, sLast, mCode, eMark sure that the user is users only input
FROM Student INNER JOIN Enrolment USING(sID) authorised to access consistent data into the
INNER JOIN Module USING (mCode) information database
WHERE sID IN (SELECT sID FROM Tutors • Beyond security, checks • Usually consists of a
WHERE lID = CURRENT_USER); should be made that series of constraints and
user mistakes are assertions on data
GRANT SELECT ON TuteeMarks TO 'user'@'%'; detected and prevented

Note: You should grant for all Tutors in MySQL, in Oracle you can
grant to PUBLIC. In Oracle CURRENT_USER is called USER

Database Integrity Connections to a DBMS


• Integrity constraints come in a number of • A major concern with database security
forms: should be when your application connects to
• CREATE DOMAIN can be used to create custom the DBMS
types with specific values
• The user doesn’t connect to the DBMS, the
• CREATE ASSERTION can be used to check
application does
manipulation of tables against some test, that
must always be true • This often happens with elevated privileges
• CHECK constraints (more widely suppoted) are • If the application isn’t well secured, it could
used to check row-level constraints provide a conduit for malicious code
• Oracle supports CHECK constraints. MySQL
can emulate them with triggers

3
SQL Injection Attacks SQL Injection Attacks
• It is common for user input to be read, and form
part of an SQL query. For example, in PHP:

$query = "SELECT * FROM Products


An SQL Injection attack is an exploit where a WHERE pName LIKE '%" . $searchterm . "%'";
user is able to insert malicious code into an SQL
query, resulting in an entirely new query • If a user is able to pass the application malicious
information, this information may be combined
with regular SQL queries
• The resulting query may have a very different
effect

SQL Injection Attacks SQL Injection Attacks


• An application or website is vulnerable to an • Imagine a user login webpage that requests a
injection attack if the programmer hasn’t user ID and password. These are passed from
added code to check for special characters in a form to PHP via $_POST
the input: • $_POST[‘id’] = ‘Michael’
• ' represents the beginning or end of a string • $_POST[‘pass’] = ‘password’
• ; represents the end of a command
• /*...*/ represent comments • The ID is later used for a query:
• -- represents a comment for the rest of a line SELECT uPass FROM Users
WHERE uID = 'Michael';

SQL Injection Attacks SQL Injection Attacks


• In PHP the code for any user might look • If the user enters Name, the command
something like this: becomes:
SELECT uPass FROM Users
$query = "SELECT uPass FROM Users WHERE
uID = '" . $_POST['id'] . "'";
WHERE uID = 'Name';
$result = mysql_query($query);
$row = mysql_fetch_row($result); • But what about if the user entered
$pass = row['uPass']; ';DROP TABLE Users;--
as their name?
• The password would then be compared with the
other field the user entered

4
SQL Injection Attacks SQL Injection Attacks
• The website programmer intended to execute • With the malicious code inserted, the
a single SQL query: meaning of the SQL changes into two queries
and a comment:

SELECT uPass FROM Users WHERE uID = ' Name ' SELECT uPass FROM Users WHERE uID = ' ';DROP TABLE Users;-- '

String Concatenation String Concatenation

SELECT uPass FROM Users WHERE uID = 'Name' SELECT uPass FROM Users WHERE uID = ''; DROP TABLE Users; -- '

SQL Injection Attacks SQL Injection Attacks


• Sometimes the goal isn’t sabotage, but • This attack is aimed at listing all accounts at a
information bank. The SQL becomes a single, altered
• Consider an online banking system: query:

SELECT No, SortCode FROM Accounts WHERE No = ' 11244102 ' SELECT No, SortCode FROM Accounts WHERE No = ' 1' OR 'a' = 'a '

String Concatenation String Concatenation

SELECT No, SortCode FROM Accounts WHERE No = '11244102' SELECT No, SortCode FROM Accounts WHERE No = '1' OR 'a' = 'a'

This is particularly effective with weakly typed languages like PHP

How To Write An SQL Injection Attack Defending Against Injection Attacks


• Defending against SQL injection attacks is not
• Data Protection Act 1998, Section 55(1): difficult, but a lot of people still don't
A person must not knowingly or recklessly, without the • There are numerous ways you can improve
consent of the data controller obtain or disclose
personal data or the information contained in security. You should be doing most of these at
personal data. any time where a user inputs variables that
• Do not do this on a website you do not own will be used in an SQL statement
• "I was just seeing if it would work" is not a • In essence, don't trust that all users will do
valid defence what you expect them to do

5
1. Restrict DBMS Access Privileges 2. Encrypt Sensitive Data
• Assuming an SQL injection attack is successful, • Storing sensitive data inside your database
a user will have access to tables based on the can always lead to problems with security
privileges of the account that the application • If in doubt, encrypt sensitive information so
used to connect to the DBMS that if any breaches occur, damage is minimal
• GRANT an application or website the • Another reason to encrypt data is the
minimum possible access to the database majority of commercial security breaches are
• Do not allow DROP, DELETE etc unless inside jobs by trusted employees
absolutely necessary • Never store unencrypted passwords. Many
• Use Views to hide as much as possible shops still do this

3. Validate Input 3. Validate Input


• Arguably the most important consideration • Always escape special characters. All languages
when creating a database or application that that execute SQL strings will allow this, in PHP:
handles user input
$username = mysql_real_escape_string($input);
• Filter any escape characters and check the $query = "SELECT * FROM Users
length of the input against expected sizes WHERE uID = '" . $username . "'";
$result = mysql_query($query);
• Checking input length should be standard
practice. This applies to programming in • mysql_real_escape_string() will escape any
general, as it also avoids buffer overflow special characters, like ', with \
attacks
• You should do this with any input variables

4. Check Input Types 5. Stored Procedures


• In weakly typed languages, check that the • Some DBMSs allow you to store procedures
user is providing you with a type you'd for use over and over again
expect
• Procedures you might store are SELECTs,
• For example, if you expect the ID to be an int, INSERTSs etc, or other procedural code
make sure it is. In PHP:
• This adds another level of abstraction
if (!is_int($_POST['userid'])) between the user and the tables
{ • If necessary, a stored procedure can access
// ID is not an integer tables that are restricted to the rest of the
} application

6
6. Generic Error Messages 7. Parameterised Input
• While it might seem helpful to output • Parameterised input essentially means that
informative error messages, this actually user input is passed to the database as
supplies users with far too much information parameters, not as part of the SQL string
• For example, if your SQL query fails, do not • This makes injection attacks extremely difficult
show the user mysql_error(), instead output: • Not all DBMSs / Languages support this
A system error has occured. We apologise for • In PHP, you need to use PHP Data Objects
the inconvenience. (PDO)
• You can log the error privately for • Reference:
administrative purposes http://php.net/manual/en/book.pdo.php

PDO
• Rather than building up a string for your SQL and
executing it, given a PDO mysql connection
$conn:
$stmt = $conn->prepare('SELECT * FROM Users
WHERE uName = :name');
$stmt->bindValue(':name', $_POST['username']);
$stmt->execute();

• The statement is pre-compiled during prepare.


While a malicious parameter may still be passed
to the query, it is simply used rather than
executed.

You might also like