What is SQL Injection?
A SQL injection attack consists of inserting or “injecting” SQL code via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database and modify database data (Insert/Update/Delete). SQL injection attacks are a type of injection attack in which SQL commands are injected into data-plane input to affect the execution of predefined SQL commands.Vulnerable Code in Normo Unsecure PWA
The application contains three SQL injection vulnerabilities inuser_management.py:
Vulnerability 1: Username Check (Line 20)
user_management.py
Vulnerability 2: Password Check (Line 25)
user_management.py
Vulnerability 3: Feedback Insertion (Line 45)
user_management.py
How to Test for SQL Injection
Test with OR 1=1
In the login form, try entering these values:
- Username:
105 OR 1=1 - Password:
105 OR 1=1
Test with Quote Injection
Try these SQL injection patterns:
" OR ""="' OR '1'='1
' and " to ensure the backend SQL query constructs are syntactically correct.Exploitation Examples
Example 1: Bypassing Authentication
Example 1: Bypassing Authentication
Input:The
- Username:
admin' -- - Password:
anything
-- comments out the rest of the query, bypassing the password check entirely.Example 2: Extracting All User Data
Example 2: Extracting All User Data
Input:This returns all users in the database since the condition is always true.
- Username:
' OR '1'='1 - Password:
' OR '1'='1
Example 3: Injecting Malicious Feedback
Example 3: Injecting Malicious Feedback
Input in feedback form:Resulting Query:This attempts to delete the entire users table after inserting the feedback.
How to Fix SQL Injection
The key to preventing SQL injection is to never concatenate user input directly into SQL queries. Always use parameterized queries or prepared statements.
Secure Implementation
Here’s how to fix each vulnerable function:Countermeasures
Implement Defensive Data Handling
Validate and sanitize all user input before processing:
- Whitelist allowed characters
- Enforce length limits
- Validate data types
Require Authentication
Require authentication before accepting any form of input that interacts with the database.
Use a Secure API Layer
Implement an API with built-in security as the interface to the SQL database. Modern frameworks like Flask with SQLAlchemy provide secure database abstractions.
Regular Code Reviews
Conduct regular code reviews specifically looking for SQL injection vulnerabilities. Search for:
- f-strings with database queries
- String concatenation in SQL statements
- Direct user input in queries
References
W3Schools SQL Injection Examples
Comprehensive examples of SQL injection attacks with explanations
File Locations
Line 20: Vulnerable username check with f-string interpolationLine 25: Vulnerable password check with f-string interpolationLine 45: Vulnerable feedback insertion with f-string interpolationLines 9-11: Secure example using parameterized queries (insertUser function)
