0% found this document useful (0 votes)
10 views

SQL Injection

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL Injection

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sql injection

SQL Injection (SQLi) Attack Explained

SQL Injection (SQLi) is a code injection technique that exploits vulnerabilities in an


application's software by inserting or "injecting" malicious SQL queries through user input
fields. This allows attackers to interfere with the queries that an application makes to its
database. SQLi can result in unauthorized access to database content, deletion of data, and other
significant damages.

Types of SQL Injection

1. In-band SQLi: The attacker uses the same communication channel to launch and gather
results of the attack.
2. Inferential SQLi (Blind SQLi): The attacker sends payloads to the server and observes
the response and behavior of the server to infer the database structure.
3. Out-of-band SQLi: The attacker uses different communication channels to perform the
attack and retrieve data.

Examples of SQL Injection

Example 1: Basic SQL Injection

Scenario: A login form on a website.

Vulnerable Code:

sql
Copy code
username = input("Enter username:")
password = input("Enter password:")
query = "SELECT * FROM users WHERE username = '" + username + "' AND password
= '" + password + "'"

Attack:

 Input:
o username: admin'--
o password: anything

Injected Query:

sql
Copy code
SELECT * FROM users WHERE username = 'admin'--' AND password = 'anything'

 The -- comment out the rest of the query, resulting in the following:
sql
Copy code
SELECT * FROM users WHERE username = 'admin'

 Outcome: The attacker logs in as admin without needing a valid password.

Example 2: Union-based SQL Injection

Scenario: A web page that displays user details based on the user ID provided in the URL.

Vulnerable URL:

http
Copy code
http://example.com/user?id=1

Vulnerable Code:

sql
Copy code
query = "SELECT name, email FROM users WHERE id = " + user_id

Attack:

 Input: 1 UNION SELECT username, password FROM admin--

Injected Query:

sql
Copy code
SELECT name, email FROM users WHERE id = 1 UNION SELECT username, password
FROM admin--

 Outcome: The attacker retrieves usernames and passwords from the admin table.

Example 3: Blind SQL Injection

Scenario: A search feature that returns results if the query matches any entries in the database.

Vulnerable Code:

sql
Copy code
query = "SELECT * FROM products WHERE name = '" + search_term + "'"

Attack:

 Input: anything' AND 1=1--


 Injected Query:
sql
Copy code
SELECT * FROM products WHERE name = 'anything' AND 1=1--

 Outcome: The query always returns true because 1=1 is always true.

Further Exploitation:

 Input: anything' AND (SELECT COUNT(*) FROM users) > 0--


 Injected Query:

sql
Copy code
SELECT * FROM products WHERE name = 'anything' AND (SELECT COUNT(*) FROM
users) > 0--

 Outcome: The attacker confirms the existence of the users table based on the server's
response.

Preventing SQL Injection

1. Parameterized Queries (Prepared Statements): Use placeholders for user input.

sql
Copy code
query = "SELECT * FROM users WHERE username = ? AND password = ?"
cursor.execute(query, (username, password))

2. Stored Procedures: Encapsulate SQL queries in the database, separating logic from data
input.

sql
Copy code
CREATE PROCEDURE getUserDetails @username NVARCHAR(50), @password
NVARCHAR(50)
AS
BEGIN
SELECT * FROM users WHERE username = @username AND password =
@password
END

3. Input Validation: Validate and sanitize user inputs to ensure they do not contain
malicious characters.
4. Least Privilege Principle: Ensure that database accounts have the minimum permissions
required.
5. Web Application Firewalls (WAFs): Deploy WAFs to detect and block SQL injection
attempts.

Sources
 OWASP SQL Injection
 SQL Injection - Wikipedia
 SQL Injection Prevention Cheat Sheet - OWASP

These examples illustrate the mechanisms and potential impacts of SQL injection attacks,
emphasizing the importance of secure coding practices and proactive security measures.

You might also like