liveBook · Manning

SQL injection

As explained in 100 SQL Server Mistakes and How to Avoid Them

SQL injection is an attack technique that alters application-generated database statements by supplying crafted input that is interpreted as SQL code.

SQL injection occurs when application input is inserted into a dynamically built SQL statement and interpreted as executable T-SQL. The resulting statement may authenticate an attacker, expose information, destroy data, or enable movement into other environments. Application validation matters, but database administrators remain the last line of defense. section 14.7 section 14.8

Three mistakes make SQL injection especially dangerous:

  • Mistake 1—trusting application developers alone: input validation can be missing or defective, so database permissions and configuration must provide another defensive layer. section 14.7
  • Mistake 2—combining injection with excessive privilege: a highly privileged service account, enabled xp_cmdshell, or an active sa account can turn a login flaw into broader environmental damage. section 14.7

A login page that stops checking passwords

A vulnerable login page constructs a query from the supplied user name and password. Normal input produces a statement such as SELECT * FROM Users WHERE UserName='Pete' AND UserPassword='Password1'. Crafted input can alter the predicate to an always-true condition and comment out the remaining password check. The same test values fail when passed as parameters to a login stored procedure.

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains the attack mechanism, demonstrates authentication bypass, shows enabling conditions, and recommends stored procedures without detailing broader secure coding practice.
Examples
Uses a C# login page, a Users table, normal and injected T-SQL predicates, and a replacement login stored procedure.
What it assumes
Assumes basic security terminology and introduces the database and application context through a simple login example.
What you can do after
Evaluate/understand SQL injection by recognizing altered predicates and favoring stored procedures, coding standards, and strong security posture.
How it connects
Connects SQL injection with least privilege, service accounts, xp_cmdshell, authentication, stored procedures, auditing, and application input validation.

Also explained in

Compare how 9 books explain SQL injection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.