Database Security

Security is a massive issue in Database systems. There are three major issues:

  1. Running injected SQL code
  2. Account Permissions
  3. Password Cracking

SQL Injection

What happens when we trust user input? Nothing good.

Imagine:

SELECT *
FROM users
WHERE username = 'alice'
AND password = 'apple';

Where do the values alice and apple come from? From some login form:

sql = "SELECT * FROM users WHERE username = '" \
+ username + "' AND password = '" \
+ password + "'"

But what if the username contains code?

' OR 1=1 -- 

Then we can substitute anything into the query:

SELECT *
FROM users
WHERE username = '' OR 1=1 -- ' AND password = 'anything';

what would this do? It would return all the users.

And we could input anything:

What do we do to fix this?
query = f”SELECT * FROM users WHERE username = ‘{username}'”
cursor.execute(query)

Prepared Statements:

# GOOD: '?' acts as a placeholder; data is passed separately as a tuple 
query = "SELECT * FROM users WHERE username = ?"
cursor.execute(query, (username,))

Account Permissions

Two definitions of access:

Authentication: Who are you?
Authorization:  What can you do?

Suppose a student can access our course database. Should that student be able to DROP the entire database?

PRINCIPLE OF LEAST PRIVILEGE
Give each user only the permissions they need.

DBMS’s like MySQL have account names and locations:
‘reader’@’localhost’

Server: *.* All databases
Database: security_demo.* A database
Table: security_demo.users A single table
Column: SELECT (username) Specific columns

So, even if someone injects SQL code from our front end then the WebApp user still doesn’t have the permissions to execute the injected code. A WebApp user should never have drop or delete permissions.

Password Cracking

Passwords are stored on the server in a database:

SELECT * FROM users;

What happens if an attacker steals this table?!

Encryption:
plaintext → ciphertext → plaintext
(with key)

Hashing:
password → hash
(one-way)

So we just encrypt passwords? No! Keys can be stolen

For passwords we never actually need to reverse the password. The server never actually needs to know it. So its better to hash:

Registration:
password = “apple” -> Password hashing algorithm -> Stored hash

Login:
entered password -> Hashing algorithm -> MATCH / NO MATCH

If a hacker gets access to the database, then they just get access to the hashes of passwords.

But:

for guess in dictionary:
    if hash(guess) == stolen_hash:
        print("Password found:", guess)

This is called an offline dictionary attack. It works because the attacker knows the hash functions.

Better:

Salt and hash your passwords.

Finally: database passwords

You probably wrote something like this for your projects:

connection = mysql.connector.connect(
host="localhost",
user="root",
password="supersecret",
database="security_demo"
)

This is not a good idea to have a password in plaintext in your code. We typically use dotenv

There is a .env file in the directory that has strict unix/linux permissions.

load_dotenv()

connection = mysql.connector.connect(
   host=os.environ["DB_HOST"],
   user=os.environ["DB_USER"],
   password=os.environ["DB_PASSWORD"],
   database=os.environ["DB_NAME"]
)