Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

13
Dynamic SQL Presented by Aaron Buma

Transcript of Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Page 1: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Dynamic SQL

Presented by Aaron Buma

Page 2: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Session OverviewWhat is Dynamic SQL“SQL Injection” + “ ; DROP TABLE”“EXEC” – Adhoc and Sprocs SP_EXECUTESQL

Page 3: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

What is Dynamic SQL

Page 4: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

What is Dynamic SQL

Page 5: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Dynamic SQL

Demo

Page 6: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

What is SQL InjectionModifying a SQL Statement to run malicious

SQL Commands. A possibility when user-entered fields are

concatenated into a SQL Statement

Page 7: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

What is SQL InjectionA user injects malicious SQL that drops the

table:

First Run

Second Run

Page 8: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

SQL Injection

Demo

Page 9: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Execution using: EXEC (“query”)Most often used for executing sprocs

Parameters available for Sprocs, you can’t parameterize queriesVulnerable to SQL injection when concatenating

Does not use Query Plan Cache

Page 10: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

EXEC under the hood

Demo

Page 11: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Execution using: SP_EXECUTESQLAllows for parameters

They must have a defined data typeProtects against SQL Injection

The execution plan is compiled and cached: re-usable!

Page 12: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

SP_ExecuteSQL

Demo

Page 13: Dynamic SQL - Exec and SP_ExecuteSQL with SQL Injection and Query Plans

Questions?What is Dynamic SQL“SQL Injection” + “ ; DROP TABLE”“EXEC” – Adhoc and Sprocs SP_EXECUTESQL