SQL Injection Attacks and Prevention Methods
1. Classic SQL Injection on Members (first_name)
Description: This attack manipulates a query by injecting a payload into the WHERE clause, causing
it to always evaluate true.
Vulnerable Query:
SELECT * FROM Members WHERE first_name = 'Cristian'' OR 1=1 --';
Effect: Returns all rows from Members table.
Prevention: Use parameterized queries (prepared statements) to separate data from code. Example:
sp_executesql with @firstname parameter.
2. Union-Based Injection on Books (genre)
Description: An attacker uses UNION SELECT to append attacker-controlled results to the original
query output.
Vulnerable Query:
SELECT title, author FROM Books WHERE genre = 'Fantasy' UNION SELECT CAST(members_id
AS NVARCHAR(10)), email FROM Members --';
Effect: Retrieves Members IDs and emails alongside Books data.
Prevention: Parameterize inputs and restrict returned columns. Ensure that user input cannot break
out of the WHERE clause. Use sp_executesql with @genre parameter.
3. LIKE Clause Injection on Book_Requests (book_title)
Description: Injection into a LIKE pattern to bypass filtering.
Vulnerable Query:
SELECT * FROM Book_Requests WHERE book_title LIKE '%The Hobbit%' OR 1=1 --';
Effect: Returns all requests regardless of title.
Prevention: Use parameterized LIKE queries, building the search term in code: WHERE book_title
LIKE @searchTerm, where @searchTerm = '%' + @input + '%'.
4. ORDER BY Clause Injection on Books (ordering by a column)
Description: Injection into the ORDER BY clause allows execution of arbitrary commands.
Vulnerable Query:
SELECT * FROM Books ORDER BY title; DROP TABLE Book_Reviews; --;
Effect: Drops Book_Reviews table.
Prevention: Use server-side whitelisting of allowed columns and QUOTENAME() to bracket
identifiers. Reject any sort column not in the approved list.
5. Stacked Queries Injection on Meetings (location_place)
Description: Injection of stacked queries to execute multiple commands.
Vulnerable Query:
SELECT * FROM Meetings WHERE location_place = 'Library'; DROP TABLE
Book_Recomandations; --';
Effect: Selects meetings and then drops the Book_Recomandations table.
Prevention: Parameterize inputs; do not allow stacked statements. Use sp_executesql with @loc
parameter.