SQL Injection (SQLi) occurs when an application concatenates unvalidated, user-supplied data into an SQL statement. Because the database parses the entire string as executable code, an attacker can inject additional clauses (e.g., `OR 1=1`, `UNION SELECT`, or stacked queries) to read, modify, or destroy data, bypass authentication, and escalate privileges. Vulnerable code typically builds queries via string interpolation, relies on manual escaping, or exposes error messages that reveal schema details. Defenses center on using parameterized queries or prepared statements (which send SQL and data separately to the server), strict input validation, least-privilege accounts, stored procedures that avoid dynamic SQL, and centralized error handling. Major compliance regimes (PCI-DSS, HIPAA, GDPR) require controls against SQLi. Pen-testers exploit it with tools like sqlmap. Modern ORM libraries default to prepared statements, but developers must avoid raw, interpolated SQL. Blind SQLi, time-based SQLi, and out-of-band SQLi work even when error messages are hidden. Always audit logs and rotate credentials after an incident.
PREPARED STATEMENT, BIND PARAMETERS, STORED PROCEDURE, LEAST PRIVILEGE, INPUT VALIDATION, CROSS SITE SCRIPTING (XSS)
Recognized as a vulnerability in late 1990s web applications
SQL Injection targets the database by manipulating SQL commands, while Cross Site Scripting (XSS) injects malicious scripts into web pages to run in visitors’ browsers.
Stored procedures that avoid dynamic SQL help, but procedures that build queries with concatenation remain vulnerable. Always use parameters.
Use automated scanners like sqlmap, add single-quote fuzzing (`'" or 1=1 --`), and review code for string-built SQL. Run tests in a staging environment only.
Prepared statements may add minimal overhead on first execution, but caching and plan reuse typically make them as fast or faster than ad-hoc SQL.