SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If...

42
1 SQL Injections by truncation Bala Neerumalla Microsoft

Transcript of SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If...

Page 1: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

1

SQL Injections by truncation

Bala Neerumalla

Microsoft

Page 2: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

2

Introduction

• Who am I?

• Security Engineer at Microsoft

• Worked on SQL Server 2000 SP3 and SP4

• Worked on SQL Server 2005

• Working in Exchange Hosted Services

• Why am I here?

• Talk about new vulnerabilities we encountered

• Talk about mitigation techniques

Page 3: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

3

Agenda

• Best practices for constructing dynamic

TSQL

• Delimiting Identifiers and Character Strings

• SQL functions

• Truncation Issues

• SQL modification by truncation

• SQL injection by truncation

• Finding and Mitigating truncation issues

Page 4: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

4

Best practices for constructing dynamic

TSQL

Page 5: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

5

Delimiting database object names

• Use delimited Identifiers

• When reserved words are used for object names.

• When you are using characters that are not listed as

qualified identifiers

• Double quotes can be used to delimit identifiers based

on where QUOTED_IDENTIFIER is ON or OFF.

• Never use single quotes to delimit identifiers.

• Always use square brackets (‘[‘ and ‘]’) to delimit

identifiers.

• Double up all occurrences of right square brackets (])

in the object name.

Page 6: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

6

Create a table with name Employee”[]’!

Page 7: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

7

Delimiting character strings

• Double quotes can be used to delimit character

strings based on where QUOTED_IDENTIFIER is

OFF or ON.

• Always use single quotes to delimit character

strings.

• Double up all occurrences of single quotes in the

character strings.

Page 8: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

8

Insert the name Mystery”Man’[]!

Page 9: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

9

SQL Functions

• quotename()

• replace()

Page 10: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

10

quotename() function

Page 11: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

11

Delimiting object names with

quotename()

Page 12: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

12

Delimiting character strings with

quotename()

Page 13: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

13

quotename() function

Page 14: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

14

replace() Function

Page 15: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

15

replace() function cont…

Page 16: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

16

quotename() vs replace()

• QUOTENAME works for character strings of

length less than or equal to 128 characters.

• Use QUOTENAME for quoting all SQL object

names.

• Use REPLACE for character strings of lengths

greater than 128 characters.

• Quotename() = delimiter + replace() + delimiter

– Quotename(@var) = ‘[‘ + replace(@var,’]’,’]]’) + ‘]’

– Quotename(@var,’’’’) = ‘’’’ + replace(@var,’’’’,’’’’’’) + ‘’’’

Page 17: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

17

Dynamic SQL in Stored Procedures

Page 18: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

18

Lets fix it with quotename()

Page 19: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

19

Fix it with replace()

Page 20: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

20

Part 1: Key points

• Double up ] (right brackets) in SQL Identifiers and

delimit them with []s.

• Double up ‘s (single quotes) in character strings

and delimit them with single quotes.

• We can use quotename() or replace() to mitigate

SQL injections.

• The only difference between these functions is

that quotename() adds the beginning and ending

delimiters and in case of replace() we will need to

add them explicitly.

Page 21: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

21

Truncation Issues

Page 22: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

22

What did we fix?

Page 23: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

23

SQL Modification by Truncation

Page 24: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

24

SQL Modification by Truncation

Page 25: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

25

Calculate the buffer lengths properly

Page 26: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

26

Avoid buffers if possible

Page 27: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

27

Avoid using dynamic SQL

Page 28: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

28

One more variant

Page 29: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

29

SQL Injection by truncation

Page 30: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

30

SQL Injection by truncation

Page 31: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

31

SQL Injection by truncation

Page 32: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

32

Calculate the buffers properly

Page 33: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

33

SQL modification by truncation

Page 34: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

34

Check for return values

Page 35: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

35

SQL Injection by truncation

Page 36: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

36

Check for return values

Page 37: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

37

Key points

• SQL modification is enabled by truncating the

command string.

• SQL injection is enabled by truncating the quoted

string.

• Truncation issues are not specific to PL/SQL

code.

Page 38: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

38

Affected Applications

• Applications written in TSQL and C/C++

• Web Applications

• Mid-tier Applications

• Backend Applications

• Tools and client applications

• Internal Maintenance Scripts.

Page 39: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

39

Finding SQL injections

• Identify the calls that execute dynamic SQL

• Review the construction of dynamic SQL

• Review the buffers used for the variables

Page 40: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

40

Mitigating SQL Injections by truncation

• If possible, call QUOTENAME() or REPLACE()

directly inside the dynamic Transact-SQL.

• Calculate the buffer lengths properly.

• Check the return values for truncation errors.

Page 41: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

41

Resources

• http://msdn2.microsoft.com/en-

us/library/ms161953(SQL.90).aspx

Page 42: SQL Injections by truncation - Black Hat · Mitigating SQL Injections by truncation • If possible, call QUOTENAME() or REPLACE() directly inside the dynamic Transact-SQL. • Calculate

42

Questions ?

This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.