Top Banner
1 SQL INJECTION & COUNTERMEASURES
51

1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Jan 16, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

1

SQL INJECTION&

COUNTERMEASURES

Page 2: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Outline

• Introduce SQL Injection• SQL Injection Attack Types• Prevention of SQL Injection Attack

(Countermeasures)

2

Page 3: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

What is SQL injection?

• A class of code-injection attacks, in which data provided by the user is included in an SQL query in such a way that part of the user’s input is treated as SQL code

3

Page 4: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Example of SQL injection

4

Page 5: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

How does it happen?

5

Page 6: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

How dangerous is it?

• The most critical Web application security risk (OWASP)

6

Page 7: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

SQL injection

• Two important characteristics:– Injection mechanism– Attack intent

7

Page 8: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Injection Mechanism

• Injection through user input• Injection through cookies• Injection through server variables• Second-order injection

8

First-order injection

Page 9: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Injection Mechanism

First-order injection• The application processes

the input, causing the attacker’s injected SQL query to execute.

Second-order injection• The application stores that

input for future use (usually in the database), and responds to the request.

• The attacker submits a second (different) request.

• To handle the second request, the application retrieves the stored input and processes it, causing the attacker’s injected SQL query to execute.

9

Page 10: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Second-order injection

• Example

10

Page 11: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Attack Intent

• Identifying injectable parameters• Performing database finger-printing• Determining database schema• Extracting data• Adding or modifying data

11

Page 12: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Attack Intent

• Performing denial of service• Evading detection• Bypassing authentication• Executing remote commands• Performing privilege escalation

12

Page 13: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Example application

13

SELECT accountsFROM usersWHERE login = ? AND pass = ? AND pin = ?

Page 14: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

SQLIA Types

• Present the different kinds of SQLIAs known to date

• Many of them are used together or sequentially, depending on the specific goals of the attacker

14

Page 15: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Tautologies

• Inject code in one or more conditional statements so that they always evaluate to true

15

SELECT accountsFROM usersWHERE login = ‘’ or 1=1 --’ AND pass = ‘’

AND pin =

Page 16: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Illegal/Logically Incorrect Queries

• Inject statements that cause a syntax, type conversion, or logical error into the database

16

SELECT accountsFROM usersWHERE login = ‘’ AND pass = ‘’

AND pin = convert(int, (select name from sysobjects where xtype = ‘u’))

”Microsoft OLE DB Provider for SQL Server (0x80040E07) Error converting nvarchar value ’CreditCards’ to a column of data type int.”

Page 17: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Union Query

• Inject a statement of the form:UNION SELECT <rest of injected query>

17

SELECT accounts FROM users WHERE login = ‘’ UNION SELECT cardNo from CreditCards whereacctNo = 10032 --‘ AND pass = ‘’ AND pin =

Page 18: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Piggy-Backed Queries

• Include new and distinct queries that “piggy-back” on the original query

18

SELECT accounts FROM users WHERE login = ‘doe’ AND pass = ‘’; drop table users --’ AND pin =

Page 19: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Stored Procedures

• Try to execute stored procedures present in the database

19

Page 20: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Stored Procedures

20

SELECT accounts FROM users WHERE login = ‘doe’ AND pass = ‘’; shutdown;--’ AND pin =

Page 21: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Inference

• Inject commands into the site and then observe how the function/response of the website changes– Blind injection– Timing attacks

21

Page 22: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Blind SQL injection

• The information must be inferred from the behavior of the page by asking the server true/false questions

22

Page 23: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Timing Attacks

• Gain information by observing timing delays in the response of the database

23

Page 24: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Alternate Encoding

• Employ alternate methods of encoding attack strings

24

SELECT accounts FROM users WHERE login = ‘doe’; exec(char(0x73697574646f776e)) --’

AND pass = ‘’ AND pin =

Page 25: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Prevention of SQLIAs

• The root cause of SQL injection vulnerabilities is insufficient input validation

• Solution:– Defensive coding practices– Detection & Prevention techniques

25

Page 26: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Defensive coding practices

• Input type checking• Encoding of inputs• Positive pattern matching• Identification of all input sources

26

Page 27: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Defensive coding practices

• Prone to human error• Not as rigorously & completely applied as

automated techniques• Weakened by the widespread promotion of

so-called “pseudo-remedies”

27

Page 28: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Detection & Prevention techniques

• Web Application SQL Injection Preventer (WASP)

• AMNESIA• SQLrand• ….

28

Page 29: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Web Application SQL Injection Preventer (WASP)

• Basic idea: allow only developer-trusted strings to form sensitive parts of a query

• Solution:– Positive tainting– Syntax-aware evaluation

29

Page 30: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Positive tainting

• Identify & mark trusted data instead of untrusted data

• Some features:– Use a white-list, rather than black-list– Incompleteness -> false positives– Straightforward & less error prone– WASP provides developers with a mechanism for

specifying sources of external data that should be trusted

30

Page 31: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Syntax-aware evaluation

• Cannot simply forbid the use of untrusted data in queries

• Some features:– Consider the context in which trusted & untrusted

data is used: permit untrusted data to be only in string and numeric literals

– Performed right before the query is sent to the database

31

Page 32: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Implementation

32

Page 33: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Empirical Evaluation

• Testing for false negatives• Testing for false positives• Overhead measurements

33

Page 34: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Testing for false negatives

34

Page 35: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Testing for false positives

35

Page 36: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Overhead measurements

36

Page 37: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

AMNESIA

• Analysis and Monitoring for NEutralizing SQL-Injection Attacks

• Basic insights:– Code contains enough information to accurately

model all legitimate queries– A SQLIA will violate the predicted model

37

Page 38: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

AMNESIA

• Solution: uses a combination of static analysis & runtime monitoring

• 4 main steps:– Identify hotspots– Build SQL-query models– Instrument application– Runtime monitoring

38

Page 39: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Identify hotspots

39

Page 40: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Build SQL query model

• Use Java String Analysis to construct character-level automata

• Parse automata to group characters into SQL tokens

40

Page 41: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Instrument application

• For each hotspot, we insert a call to the monitor before the call to the database

41

Page 42: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Runtime monitoring

• Check queries against SQL query model

42

Normal user

Page 43: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Runtime monitoring

• Check queries against SQL query model

43

Malicious user

Page 44: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Implementation

44

Page 45: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

SQLrand

• Extends the application of Instruction-Set Randomization to the SQL: appending a random integer to SQL standard keywords

• Example:

45

Page 46: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

SQLrand system architecture

46

Page 47: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Implementation

• Two primary components:– De-randomization element– Communication protocol between the client &

database system

47

Page 48: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

De-randomization element

• Required a modified SQL parser that expected the suffix of integers applied to all keywords

• Utilized two popular tools for writing compilers and parsers: flex & yacc

48

Page 49: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Communication protocol

• As a “middle man”, the proxy had to conceal its identity by masquerading as the database to the client & vice versa

49

CLIENT PROXY DBMS

Using API the DBMS provides

Simply change port number

Page 50: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

Evaluation

• Evaluation with respect to attack types

50

Page 51: 1 SQL INJECTION & COUNTERMEASURES. Outline Introduce SQL Injection SQL Injection Attack Types Prevention of SQL Injection Attack (Countermeasures) 2.

The end.

51