SQL Injection Attack
SQL injection is a code injection attack where an attacker inserts malicious SQL syntax into user-controlled input that is concatenated directly into a database query, causing the database to execute unintended commands.
SQL injection is a code injection attack where an attacker inserts malicious SQL syntax into user-controlled input that is concatenated directly into a database query, causing the database to execute unintended commands.
It remains one of the most critical web application vulnerabilities (consistently in the OWASP Top 10) because the consequences are severe: unauthorized data disclosure (dumping entire tables), data modification or deletion, authentication bypass, and in some configurations, remote code execution via database features like xp_cmdshell in SQL Server.
The vulnerability arises from string concatenation. Consider a login query built as: "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'". If an attacker enters admin' -- as the username, the resulting query becomes SELECT * FROM users WHERE username = 'admin' --' AND password = '...'. The -- starts a SQL comment, nullifying the password check entirely. The attacker logs in as admin without a valid password.
More destructive payloads use UNION SELECT to extract data from other tables, ; DROP TABLE users; -- to delete data, or boolean-based and time-based blind injection techniques to exfiltrate data one bit at a time even when the application doesn't return query results.
The definitive prevention is parameterized queries (prepared statements). Instead of building SQL strings, you pass the query template and user input as separate values: SELECT * FROM users WHERE username = ? AND password = ?. The database driver handles escaping and ensures user input is always treated as a data value, never executable SQL syntax. ORMs that use parameterized queries under the hood provide the same protection. Input validation (rejecting unexpected characters) and a web application firewall are complementary layers but not substitutes for parameterized queries.