[kerference] nefarious sql - 김동호(kert)

164
NEFARIOUS SQL 일시: 2016-7-23 장소:공대9호관 김동호

Upload: naver-d2

Post on 07-Jan-2017

1.625 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: [Kerference] Nefarious SQL - 김동호(KERT)

NEFARIOUS

SQL

일시: 2016-7-23장소:공대9호관 김동호

Page 2: [Kerference] Nefarious SQL - 김동호(KERT)

1Explaining what SQL injection really is in depth.

Moreover, why we need to know about SQL Injection

2

What is SQL Injection?

Variety of SQL

Injection Method

Table of

Contents

Explaining different types of SQL Injection method and

how it works.

Page 3: [Kerference] Nefarious SQL - 김동호(KERT)

3

4

5

We are going to go through SQL Injection war game to

explain how SQL injection really works in live

SQL Injection

Tutorial

Ways to prevent

SQL Injection

We will go through how we can prevent SQL Injection

happening to our website

We will just quickly review what we have learned today

Review

Page 4: [Kerference] Nefarious SQL - 김동호(KERT)

What is SQL?SQL Database & SQL Injection

Page 5: [Kerference] Nefarious SQL - 김동호(KERT)

What is

SQL?

• Structured Query Language (Standard Language)

• Proprietary extension standard language

• Function

• Select

• Insert

• update,

• Find

• Etc.…

• Variety of Dialects

• T-SQL

• PL/SQL

• JET SQL

• Many more

Page 6: [Kerference] Nefarious SQL - 김동호(KERT)

WHY SQL?

Page 7: [Kerference] Nefarious SQL - 김동호(KERT)

Reason Why

SQL

1 Allows users to access data

in relational database

management systems

2 Allows to embed within other

language using SQL

modules, libraries & pre-

compiers

3 Allows users to set

permissions on tables,

procedures, And views

4 Tons of more reason to use

SQL!!

Page 8: [Kerference] Nefarious SQL - 김동호(KERT)

Process of

SQL

• RDMBS

• SQL Engine

• Components

• Query Dispatcher

• Optimization Engines

• Classic Query Engine

• SQL Query Engine

• Etc.

• Classic Query Engine

• SQL Query Engine

Page 9: [Kerference] Nefarious SQL - 김동호(KERT)

History of

SQL

1970 1974 1978 1986

Dr. Edgar F. “Ted” Codd of

IBM is known as the father of

relational databases. He

described a relational model

for databases

Structured Query Language

appeared

IBM worked to develop

Codd’s ideas and released a

product named System/R

IBM developed the first

prototype of relational

database and standardized

by ANSI. The first relational

database was released by

Relational Software and its

later becoming Oracle

Page 10: [Kerference] Nefarious SQL - 김동호(KERT)

SQL DIAGRAM

Page 11: [Kerference] Nefarious SQL - 김동호(KERT)
Page 12: [Kerference] Nefarious SQL - 김동호(KERT)

Understanding SQL

query statements

Page 13: [Kerference] Nefarious SQL - 김동호(KERT)

Different types of

Query statements

1

2

3SELECT Statement

INSERT Statement

UPDATE Statement

Page 14: [Kerference] Nefarious SQL - 김동호(KERT)

SELECT

Statements

• Retrieving/selecting certain information from the database

• Commonly used with where statement as well

Page 15: [Kerference] Nefarious SQL - 김동호(KERT)
Page 16: [Kerference] Nefarious SQL - 김동호(KERT)
Page 17: [Kerference] Nefarious SQL - 김동호(KERT)
Page 18: [Kerference] Nefarious SQL - 김동호(KERT)
Page 19: [Kerference] Nefarious SQL - 김동호(KERT)
Page 20: [Kerference] Nefarious SQL - 김동호(KERT)
Page 21: [Kerference] Nefarious SQL - 김동호(KERT)
Page 22: [Kerference] Nefarious SQL - 김동호(KERT)
Page 23: [Kerference] Nefarious SQL - 김동호(KERT)
Page 24: [Kerference] Nefarious SQL - 김동호(KERT)
Page 25: [Kerference] Nefarious SQL - 김동호(KERT)
Page 26: [Kerference] Nefarious SQL - 김동호(KERT)
Page 27: [Kerference] Nefarious SQL - 김동호(KERT)
Page 28: [Kerference] Nefarious SQL - 김동호(KERT)

Update

Statements

• To modify one or more existing rows of data within a table

• Changing Value which it already exist(s)

• Similar to INSERT Statements but it usually contains WHERE statement

Page 29: [Kerference] Nefarious SQL - 김동호(KERT)
Page 30: [Kerference] Nefarious SQL - 김동호(KERT)
Page 31: [Kerference] Nefarious SQL - 김동호(KERT)
Page 32: [Kerference] Nefarious SQL - 김동호(KERT)
Page 33: [Kerference] Nefarious SQL - 김동호(KERT)

INSERT

Statements

• To create new row of data within a table

• Such as new account, audit log, etc….

Page 34: [Kerference] Nefarious SQL - 김동호(KERT)
Page 35: [Kerference] Nefarious SQL - 김동호(KERT)
Page 36: [Kerference] Nefarious SQL - 김동호(KERT)
Page 37: [Kerference] Nefarious SQL - 김동호(KERT)

Additional

Information

• Semicolon

• SQL comment syntax

Page 38: [Kerference] Nefarious SQL - 김동호(KERT)
Page 39: [Kerference] Nefarious SQL - 김동호(KERT)
Page 40: [Kerference] Nefarious SQL - 김동호(KERT)
Page 41: [Kerference] Nefarious SQL - 김동호(KERT)
Page 42: [Kerference] Nefarious SQL - 김동호(KERT)
Page 43: [Kerference] Nefarious SQL - 김동호(KERT)
Page 44: [Kerference] Nefarious SQL - 김동호(KERT)
Page 45: [Kerference] Nefarious SQL - 김동호(KERT)
Page 46: [Kerference] Nefarious SQL - 김동호(KERT)
Page 47: [Kerference] Nefarious SQL - 김동호(KERT)

http://mysite.com/color?colorid=4

Page 48: [Kerference] Nefarious SQL - 김동호(KERT)
Page 49: [Kerference] Nefarious SQL - 김동호(KERT)
Page 50: [Kerference] Nefarious SQL - 김동호(KERT)
Page 51: [Kerference] Nefarious SQL - 김동호(KERT)
Page 52: [Kerference] Nefarious SQL - 김동호(KERT)

Minimal Presentation

Page 53: [Kerference] Nefarious SQL - 김동호(KERT)

What is

SQL Injection?

• Injecting arbitrary pieces of malicious code

• Executed as a piece of code by the back end SQL server

• Giving undesired results

• Executing code through vulnerable input parameters

• Compromising the whole system

Page 54: [Kerference] Nefarious SQL - 김동호(KERT)

History of

SQL Injection

1999 2003 2013 cont

Common Vulnerabilities and

Exposures dictionary has

existed to keep track of and

alert consumers and

developers alike of known

software vulnerabilities

Structured Query Language

appeared on top 10 list of

Common Vulnerabilities and

Exposures dictionary

SQL Injection was top 1

vulnerability chosen by

OWASP

SQL Injection continues to be

on top of the list for

vulnerability on many

organizations

Page 55: [Kerference] Nefarious SQL - 김동호(KERT)

2014 WHS Web

Vulnerability Report

Page 56: [Kerference] Nefarious SQL - 김동호(KERT)
Page 57: [Kerference] Nefarious SQL - 김동호(KERT)

Richard Alan Clarke

If you spend more on coffee than on IT security, you will be hacked. What’s more, you deserve

to be hacked“ “

Page 58: [Kerference] Nefarious SQL - 김동호(KERT)

HOW SQL

INJECTION WORKS

Attacker Sends data containing SQL FragmentsCUSTOM CODE

Database

1

3 Attackers views unauthorized data

Example: $sql= “SELECT*FORM table WHERE id=‘”.$_REQUEST[‘id’].”’”;

Applications sends modified queries to get the database and query return values

2

Page 59: [Kerference] Nefarious SQL - 김동호(KERT)

Types of SQL Injection

Union Based

InclusiveError Based

Boolean

Executing

Time

Schema Discovery

Tautology

Single quote

Page 60: [Kerference] Nefarious SQL - 김동호(KERT)

Error Based SQL Injection

Page 61: [Kerference] Nefarious SQL - 김동호(KERT)

Single Character Injection Test

Page 62: [Kerference] Nefarious SQL - 김동호(KERT)
Page 63: [Kerference] Nefarious SQL - 김동호(KERT)
Page 64: [Kerference] Nefarious SQL - 김동호(KERT)
Page 65: [Kerference] Nefarious SQL - 김동호(KERT)
Page 66: [Kerference] Nefarious SQL - 김동호(KERT)

CarsByCylinders?Cylinders=V12

SELECTED Id, Name FROM Supercar WHERE Cylinders = ‘V12’

Page 67: [Kerference] Nefarious SQL - 김동호(KERT)

CarsByCylinders?Cylinders=V12

SELECTED Id, Name FROM Supercar WHERE Cylinders = ‘V12’‘

Page 68: [Kerference] Nefarious SQL - 김동호(KERT)

CarsByCylinders?Cylinders=V12

SELECTED Id, Name FROM Supercar WHERE Cylinders = ‘V12’‘

‘ or 1=1--

Page 69: [Kerference] Nefarious SQL - 김동호(KERT)

CarsByCylinders?Cylinders=V12

SELECTED Id, Name FROM Supercar WHERE Cylinders = ‘V12’‘ or 1=1--

‘ or 1=1--

Page 70: [Kerference] Nefarious SQL - 김동호(KERT)
Page 71: [Kerference] Nefarious SQL - 김동호(KERT)
Page 72: [Kerference] Nefarious SQL - 김동호(KERT)

TautologyA tautology is a formula that is true in every possible interpretation

Page 73: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access Controls

Page 74: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access ControlsUserName = johnsmithPassword = p@ssword

Page 75: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access Controls

UserName = johnsmithPassword = p@ssword

SELECT COUNT (*) FROM Users WHERE UserName=‘johnsmith’ AND Password=‘p@ssword’

Page 76: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access Controls

UserName = johnsmith’ or 1=1--Password = p@ssword

SELECT COUNT (*) FROM Users WHERE UserName=‘johnsmith’ or 1=1--’ AND Password=‘p@ssword’

Page 77: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access Controls

UserName = johnsmith’ and 1=1--Password = p@ssword

SELECT COUNT (*) FROM Users WHERE UserName=‘johnsmith’ and 1=1--’ AND

Password=‘p@ssword’

Page 78: [Kerference] Nefarious SQL - 김동호(KERT)

Circumventing Access Controls

UserName = johnsmith’--Password = p@ssword

SELECT COUNT (*) FROM Users WHERE UserName=‘johnsmith’--’ AND Password=‘p@ssword’

Page 79: [Kerference] Nefarious SQL - 김동호(KERT)

Modifying Data and DB Objects

UserName= johnsmith’;update users set password=‘foo’--

Page 80: [Kerference] Nefarious SQL - 김동호(KERT)

Modifying Data and DB Objects

UserName= johnsmith’;update users set password=‘foo’--

UserName= johnsmith’;update item set price=price-1--

Page 81: [Kerference] Nefarious SQL - 김동호(KERT)

Modifying Data and DB Objects

UserName= johnsmith’;update users set password=‘foo’--

UserName= johnsmith’;update item set price=price-1--

UserName= johnsmith’;insert into….

Page 82: [Kerference] Nefarious SQL - 김동호(KERT)

Modifying Data and DB Objects

UserName= johnsmith’;update users set password=‘foo’--

UserName= johnsmith’;update item set price=price-1--

UserName= johnsmith’;insert into….

UserName= johnsmith’;drop table users--

Page 83: [Kerference] Nefarious SQL - 김동호(KERT)

Modifying Data and DB Objects

UserName= johnsmith’;update users set password=‘foo’--

UserName= johnsmith’;update item set price=price-1--

UserName= johnsmith’;insert into….

UserName= johnsmith’;drop table users--

UserName= johnsmith’;create login…

Page 84: [Kerference] Nefarious SQL - 김동호(KERT)

Union Based SQL Injection

Page 85: [Kerference] Nefarious SQL - 김동호(KERT)

Understanding Union Operators

Page 86: [Kerference] Nefarious SQL - 김동호(KERT)
Page 87: [Kerference] Nefarious SQL - 김동호(KERT)
Page 88: [Kerference] Nefarious SQL - 김동호(KERT)
Page 89: [Kerference] Nefarious SQL - 김동호(KERT)
Page 90: [Kerference] Nefarious SQL - 김동호(KERT)
Page 91: [Kerference] Nefarious SQL - 김동호(KERT)
Page 92: [Kerference] Nefarious SQL - 김동호(KERT)

Executing Union SQL Injection

Page 93: [Kerference] Nefarious SQL - 김동호(KERT)
Page 94: [Kerference] Nefarious SQL - 김동호(KERT)
Page 95: [Kerference] Nefarious SQL - 김동호(KERT)
Page 96: [Kerference] Nefarious SQL - 김동호(KERT)
Page 97: [Kerference] Nefarious SQL - 김동호(KERT)
Page 98: [Kerference] Nefarious SQL - 김동호(KERT)
Page 99: [Kerference] Nefarious SQL - 김동호(KERT)
Page 100: [Kerference] Nefarious SQL - 김동호(KERT)
Page 101: [Kerference] Nefarious SQL - 김동호(KERT)
Page 102: [Kerference] Nefarious SQL - 김동호(KERT)
Page 103: [Kerference] Nefarious SQL - 김동호(KERT)
Page 104: [Kerference] Nefarious SQL - 김동호(KERT)
Page 105: [Kerference] Nefarious SQL - 김동호(KERT)
Page 106: [Kerference] Nefarious SQL - 김동호(KERT)
Page 107: [Kerference] Nefarious SQL - 김동호(KERT)
Page 108: [Kerference] Nefarious SQL - 김동호(KERT)
Page 109: [Kerference] Nefarious SQL - 김동호(KERT)
Page 110: [Kerference] Nefarious SQL - 김동호(KERT)
Page 111: [Kerference] Nefarious SQL - 김동호(KERT)
Page 112: [Kerference] Nefarious SQL - 김동호(KERT)
Page 113: [Kerference] Nefarious SQL - 김동호(KERT)
Page 114: [Kerference] Nefarious SQL - 김동호(KERT)

Querying System Objects for schema discovery

Page 115: [Kerference] Nefarious SQL - 김동호(KERT)
Page 116: [Kerference] Nefarious SQL - 김동호(KERT)
Page 117: [Kerference] Nefarious SQL - 김동호(KERT)
Page 118: [Kerference] Nefarious SQL - 김동호(KERT)
Page 119: [Kerference] Nefarious SQL - 김동호(KERT)
Page 120: [Kerference] Nefarious SQL - 김동호(KERT)
Page 121: [Kerference] Nefarious SQL - 김동호(KERT)
Page 122: [Kerference] Nefarious SQL - 김동호(KERT)

Extracting Schema Details with Union Injection

Page 123: [Kerference] Nefarious SQL - 김동호(KERT)
Page 124: [Kerference] Nefarious SQL - 김동호(KERT)
Page 125: [Kerference] Nefarious SQL - 김동호(KERT)
Page 126: [Kerference] Nefarious SQL - 김동호(KERT)
Page 127: [Kerference] Nefarious SQL - 김동호(KERT)
Page 128: [Kerference] Nefarious SQL - 김동호(KERT)
Page 129: [Kerference] Nefarious SQL - 김동호(KERT)
Page 130: [Kerference] Nefarious SQL - 김동호(KERT)
Page 131: [Kerference] Nefarious SQL - 김동호(KERT)

Blind Based SQL Injection

Page 132: [Kerference] Nefarious SQL - 김동호(KERT)

Basic Attack Success Criteria

Page 133: [Kerference] Nefarious SQL - 김동호(KERT)

Basic Attack Success Criteria

The app needs to return internal exceptions which bubble up from the underlying database

Page 134: [Kerference] Nefarious SQL - 김동호(KERT)

Basic Attack Success Criteria

The app needs to return internal exceptions which bubble up from the underlying database

The query structure needs to allow the union operator to be injected and the vector needs to return results to the app

Page 135: [Kerference] Nefarious SQL - 김동호(KERT)

Basic Attack Success Criteria

The command executed on the database can be manipulated by the attacker

Page 136: [Kerference] Nefarious SQL - 김동호(KERT)

Boolean SQL Injection

Page 137: [Kerference] Nefarious SQL - 김동호(KERT)
Page 138: [Kerference] Nefarious SQL - 김동호(KERT)
Page 139: [Kerference] Nefarious SQL - 김동호(KERT)
Page 140: [Kerference] Nefarious SQL - 김동호(KERT)
Page 141: [Kerference] Nefarious SQL - 김동호(KERT)
Page 142: [Kerference] Nefarious SQL - 김동호(KERT)
Page 143: [Kerference] Nefarious SQL - 김동호(KERT)
Page 144: [Kerference] Nefarious SQL - 김동호(KERT)
Page 145: [Kerference] Nefarious SQL - 김동호(KERT)

How to prevent SQL Injection

Page 146: [Kerference] Nefarious SQL - 김동호(KERT)

Ways to prevent

SQL Injection

1 2Implement Proper

Error Handling

Validating

Untrusted Data

Page 147: [Kerference] Nefarious SQL - 김동호(KERT)

Implementing Proper

Error Handling

Page 148: [Kerference] Nefarious SQL - 김동호(KERT)
Page 149: [Kerference] Nefarious SQL - 김동호(KERT)
Page 150: [Kerference] Nefarious SQL - 김동호(KERT)
Page 151: [Kerference] Nefarious SQL - 김동호(KERT)
Page 152: [Kerference] Nefarious SQL - 김동호(KERT)

Validating

Unstructured Data

Page 153: [Kerference] Nefarious SQL - 김동호(KERT)
Page 154: [Kerference] Nefarious SQL - 김동호(KERT)
Page 155: [Kerference] Nefarious SQL - 김동호(KERT)
Page 156: [Kerference] Nefarious SQL - 김동호(KERT)
Page 157: [Kerference] Nefarious SQL - 김동호(KERT)
Page 158: [Kerference] Nefarious SQL - 김동호(KERT)
Page 159: [Kerference] Nefarious SQL - 김동호(KERT)
Page 160: [Kerference] Nefarious SQL - 김동호(KERT)
Page 161: [Kerference] Nefarious SQL - 김동호(KERT)
Page 162: [Kerference] Nefarious SQL - 김동호(KERT)
Page 163: [Kerference] Nefarious SQL - 김동호(KERT)

More Prevention

Method

1

2

3

4

Query

Parameterization

Stored Procedures

Object Relational

Mappers

Using an IDS or

WAF

Page 164: [Kerference] Nefarious SQL - 김동호(KERT)

Questions?