23CB301 Unit5-4
23CB301 Unit5-4
Database security refers to the measures and protocols used to protect databases from
unauthorized access, misuse, corruption, or theft. Securing a database is critical because
databases often contain sensitive information, including personal data, financial information,
trade secrets, and more. Below are some of the major database security issues:
1. Unauthorized Access
2. SQL Injection
Description: SQL injection is a common attack where malicious SQL code is inserted
into queries via input fields. Attackers can manipulate SQL commands to retrieve or
modify sensitive data.
Risks: SQL injection can result in data breaches, data deletion, or even complete
database control.
Prevention: Input validation, parameterized queries, and the use of stored procedures
can prevent SQL injections.
5. Insider Threats
Description: If backups are not adequately protected, they may become a target for
attackers.
Risks: Unencrypted or poorly secured backups can lead to data theft, especially if
they are stored in insecure locations.
Prevention: Encrypt backups and store them securely in a location with access
controls.
Description: Physical access to database servers can be a risk if data centers or server
rooms are not properly secured.
Risks: If someone gains physical access, they can steal hard drives or tamper with the
system directly.
Prevention: Use physical security measures such as locked server rooms, access
control, surveillance, and biometric security.
Description: Modern databases often interact with other systems through APIs
(Application Programming Interfaces). Poorly secured APIs can be exploited to gain
unauthorized access to data.
Risks: Data leakage via APIs can expose sensitive information, especially if APIs are
not secured or monitored.
Prevention: Secure APIs using strong authentication, encryption, and access controls.
SQL provides two commands for managing access rights, GRANT and REVOKE.
For different versions of SQL, the syntax is slightly different. In general terms, the
GRANT command has the following syntax: (The following syntax definition
conventions are used. Elements separated by a vertical line are alternatives. A list
of alternatives is grouped in curly brackets. Square brackets enclose optional
elements. That is, the elements inside the square brackets may or may not be
present.)
GRANT { privileges | role } [ON table] TO { user | role | PUBLIC } [IDENTIFIED
BY password] [WITH GRANT OPTION]
This command can be used to grant one or more access rights or can be used to
assign a user to a role. For access rights, the command can optionally specify that it
applies only to a specified table. The TO clause specifies the user or role to which
the rights are granted. A PUBLIC value indicates that any user has the specified
access rights. The optional IDENTIFIED BY clause specifies a password that must
be used to revoke the access rights of this GRANT command. The GRANT
OPTION indicates that the grantee can grant this access right to other users, with or
without the grant option.
As a simple example, consider the following statement.
GRANT SELECT ON ANY TABLE TO ricflair
This statement enables user ricflair to query any table in the database. Different
implementations of SQL provide different ranges of access rights.
The following is a typical list:
Select: Grantee may read entire database; individual tables; or specific columns in a
table.
Insert: Grantee may insert rows in a table; or insert rows with values for specific
columns in a table.
Update: Semantics is similar to
INSERT. Delete: Grantee may delete
rows from a table.
References: Grantee is allowed to define foreign keys in another table that refer to the
specified columns.
The REVOKE command has the following syntax:
REVOKE { privileges | role } [ON table] FROM
{ user | role | PUBLIC }
Here are some common types of SQL injection attacks, along with an example:
This is the most common type of SQL injection, where the attacker retrieves data from the
database by manipulating the SQL query directly via an input field.
Example Scenario: Consider a login page where the user inputs a username and password.
The query may look like this:
If the application does not properly sanitize the input, an attacker could input the following:
Username: admin' --
Password: (leave blank)
The query becomes:
ELECT * FROM users WHERE username = 'admin' --' AND password = '';
The -- is an SQL comment symbol, so the part after it is ignored. This effectively turns the
query into:
Since there's no password check anymore, the attacker can log in as the "admin" user.
In blind SQL injection, attackers cannot see the results of the queries directly. Instead, they
infer information based on true/false conditions. This is often done using time delays or error
messages.
Example Scenario: Suppose the application checks if a user ID exists with the following
query:
If the database server takes 5 seconds to respond, the attacker knows that the injection was
successful. They can use this technique to retrieve data bit by bit by manipulating the query
to check specific conditions.
In this method, the attacker uses the UNION SQL operator to combine the results of a
legitimate query with data selected by the attacker. This can allow the attacker to retrieve
information from other tables in the database.
SELECT name, email FROM users WHERE id = 1 UNION SELECT username, password
FROM admin_users;
This would combine the result of the first query with the second query, potentially exposing
sensitive data like administrator usernames and passwords.
4. Error-Based SQL Injection
In this type of SQL injection, the attacker deliberately causes the database to return error
messages that contain useful information about the database structure. These error messages
can be used to craft more sophisticated attacks.
sql
Copy code
1' AND 1=CONVERT(int, (SELECT @@version))--
If the database returns an error containing the database version (due to the @@version query),
the attacker can gather information about the system and use it to plan further attacks.
This form of blind SQL injection uses conditional responses to determine whether certain
statements are true or false. Based on how the application behaves (whether an error is
returned, whether a page loads correctly, etc.), the attacker can infer information.
If this returns the normal result (indicating true), the attacker knows the injection was
successful. Then, they might try:
If this does not return any results (indicating false), the attacker can confirm that the query
is being processed, and from there, start extracting data by asking true/false questions.
In time-based blind SQL injection, the attacker uses SQL functions that cause time delays to
infer whether a query condition is true or false, even if no data is returned.
If the application takes 5 seconds to respond, the attacker knows that the condition 1=1
(which is true) caused the delay, confirming that SQL injection is possible. By changing the
condition (e.g., 1=0), they can determine whether their injected queries are being processed.
4.(i) Role-Based Access Control (RBAC)
It is a method of regulating access to a system or its resources (like databases) based on the roles
assigned to individual users. In the context of database security, RBAC ensures that users can only
perform actions (e.g., read, write, update, delete) that are necessary for their roles, limiting the risk
of unauthorized access or data breaches.
1. Role: A role is a collection of permissions that define what actions a user can
perform. Roles are usually defined based on job functions (e.g., "admin," "developer,"
"auditor").
2. Permissions: These are the rights that define what actions (such as SELECT,
INSERT, DELETE) can be performed on database objects like tables, views, or stored
procedures.
3. User: A user is an individual or system entity that interacts with the database. Users
are assigned roles that determine their access level.
4. Role Assignment: Users are assigned to one or more roles, and each role has a set of
permissions that define what the user can do.
Principle of Least Privilege: Users are granted only the permissions necessary for their role,
reducing the potential damage from compromised accounts.
Simplified Access Management: Administrators manage permissions by role instead of
individual users, making it easier to manage access control for many users.
Security: Reduces the chance of unauthorized access to sensitive data by enforcing clear
access control policies.
Scenario:
Consider a company with an online retail platform. They use a database to store customer
data, order information, and product inventory. Different employees in the company need
different levels of access to the database based on their job roles.
1. Admin
2. Sales Representative
3. Data Analyst
Step-by-Step Example:
sql
Copy code
-- Create roles for each user type
CREATE ROLE admin_role;
CREATE ROLE sales_rep_role;
CREATE ROLE data_analyst_role;
3. Assign Users to Roles Users are assigned roles based on their job responsibilities.
sql
Copy code
-- Create users and assign roles
CREATE USER alice WITH PASSWORD 'password123';
CREATE USER bob WITH PASSWORD 'password123';
CREATE USER charlie WITH PASSWORD 'password123';
sql
Copy code
SELECT * FROM customers; -- Alice can execute this
INSERT INTO products (name, price) VALUES ('New Product',
99.99); -- Alice can also add data
o Bob (Sales Representative): Bob can only view customer and order details,
but he cannot modify the data or access other tables like products.
sql
Copy code
SELECT * FROM customers; -- Bob can execute this
DELETE FROM customers WHERE id = 1; -- Bob cannot execute this
(no DELETE permission)
o Charlie (Data Analyst): Charlie can view summarized sales data, but he
cannot access detailed customer or order information.
sql
Copy code
SELECT * FROM sales_summary_view; -- Charlie can execute this
SELECT * FROM customers; -- Charlie cannot execute this (no
SELECT permission on customers)
Alice (Admin) has all permissions (SELECT, INSERT, UPDATE, DELETE) and can perform any
action across the database.
Bob (Sales Rep) is restricted to viewing customer and order data but cannot modify it.
Charlie (Data Analyst) can only view anonymized sales summary data and has no access to
sensitive customer information.
(ii)Cascading Authorizations
The grant option enables an access right to cascade through a number of users.We
consider a specific access right and illustrate the cascade phenomenon in Figure 5.5. The
figure indicates that Ann grants the access right to Bob at time t = 10 and to Chris at
time t = 20. Assume that the grant option is always used. Thus, Bob is able to grant the
access right to David at t = 30. Chris redundantly grants the access right to David at t =
50. Meanwhile, David grants the right to Ellen, who in turn grants it to Jim; and
subsequently David grants the right to Frank.
Just as the granting of privileges cascades from one user to another using the grant
option, the revocation of privileges also cascaded. Thus, if Ann revokes the access right
to Bob and Chris, then the access right is also revoked to David, Ellen, Jim, and Frank.
A complication arises when a user receives the same access right multiple times, as
happens in the case of David. Suppose that Bob revokes the privilege from David. David
still has the access right because it was granted by Chris at t = 50. However, David
granted the access right to Ellen after receiving the right, with grant option, from Bob
but prior to receiving it from Chris. Most implementations dictate that in this
circumstance, the access right to Ellen and therefore Jim is revoked when Bob revokes
the access right to David. This is because at t = 40, when David granted the access right
to Ellen, David only had the grant option to do this from Bob. When Bob revokes the
right, this causes all subsequent cascaded grants that are traceable solely to Bob via
David to be revoked. Because David granted the access right to Frank after David was
granted the access right with grant option.
from Chris, the access right to Frank remains. These effects are shown in the lower
portion of Figure 5.6.