liveBook · Manning

Injection attack

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

An injection attack is an attack in which untrusted input is interpreted as executable code or commands.

An injection attack occurs when untrusted input is interpreted as executable code or commands. In SQL automation, dynamically joining input into a query can let an attacker alter the statement, including destructive commands. section 7.4.1

Prevent SQL injection by parameterizing values:

  • Write a query with placeholders such as @name instead of interpolating variables into SQL strings. section 7.4.1
  • Pass parameter values through the Invoke-DbaQuery -SqlParameter argument and a hashtable. section 7.4.1
  • Build dynamic WHERE clauses from permitted parameters while keeping their values separate from the SQL text. section 7.4.1

Unsafe versus parameterized filtering

Interpolating a server name can turn the value "';Truncate table xyz;select '" into executable SQL. With `WHERE Name = @name` and `SqlParameter = @{name = 'Srv01'}`, SQL treats the same malicious input as a string. section 7.4.1

$DbaQuery = @{
    SqlInstance = "$($env:COMPUTERNAME)\SQLEXPRESS"
    Database = 'PoshAssetMgmt'
    Query = 'SELECT * FROM Servers WHERE Name = @name'
    SqlParameter = @{name = 'Srv01'}
}
Invoke-DbaQuery @DbaQuery

Inspect a parameterized Invoke-DbaQuery call using a named SQL placeholder and hashtable value. section 7.4.1

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains string-built query risk, destructive payload behavior, and parameterized mitigation without covering broader injection categories.
Examples
Uses server-name filtering, a destructive TRUNCATE payload, Invoke-DbaQuery parameters, and a dynamically assembled WHERE clause.
What you can do after
Apply it via Invoke-DbaQuery by separating SQL placeholders from user-supplied values.
How it connects
Connects SQL injection with parameterized queries, SQL WHERE clauses, PowerShell variables, and database automation.

Also explained in

Compare how 3 books explain injection attack →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.