SlideShare a Scribd company logo
CNIT 129S: Securing
Web Applications
Ch 9: Attacking Data Stores
 

Part 2 of 2
Updated 3-16-22
Bypassing Filters
Avoiding Blocked
Characters
• To prevent injection, many apps remove or
encode some character
s

• A single quotation mark is not needed for
injection into a numerical
fi
el
d

• You can also use string functions to
dynamically construct a string containing
fi
ltered characters
CHR or CHAR Function
• These queries work on Oracle and MS-SQL,
respectively
Comment Symbol Blocked
• Code i
s

SELECT * from users WHERE name='uname
'

• Try injecting this value for name
:

' or 1=1 -
-

• To creat
e

SELECT * from users WHERE name='' or 1=1 --
'

• But the "--' is blocked
Crafting Correct Syntax
Without a Comment
• Injecting this value for name
:

' or 'a'='
a

• To creat
e

SELECT * from users WHERE name=''
or 'a'='a'
Circumventing Simple
Validation
• If "SELECT" is blocked, try these bypasses:
Using SQL Comments
• If spaces are blocked, use comments instea
d

• MySQL allows comments within keywords
Second-Order SQL Injection
• Many applications handle data safely when it is
fi
rst entered into the databas
e

• But it may later be processed in unsafe ways
App Adds a Second Quote
• Register an account with this name
:

foo
'

• The correct way to insert that value is by adding a
second quote (link Ch 2a
)

INSERT INTO users (username,
password, ID, privs) VALUES
('foo''', 'secret', 2248, 1)
Password Change
• Requires user to input old password, and
compares it to the password retrieved with
:

SELECT password FROM users WHERE
username = 'foo'
'

• This is a syntax error.
Exploit
• Register a new user with this name
:

' or 1 in (SELECT password FROM users
WHERE username = 'admin')-
-

• Perform a password change, and MS-SQL will
return this error, exposing the administrator
password
Ch 9 Attacking Data Stores (Part 2)
Advanced Exploitation
• The previous attacks had a ready means of
exposing dat
a

• Adding UNION to a query that returns the
result
s

• Returning data in an error messag
e

• What if the results are not exposed?
Denial of Service
• This attack does not steal dat
a

• It's merely destructiv
e

• Turn off an MS-SQL databas
e

' shutdown-
-

• Drop tabl
e

' drop table users--
Retrieving Data as Numbers
• No strings
fi
elds may be vulnerable, because
single quotes are
fi
ltere
d

• Numeric
fi
elds are vulnerable, but only allow
you to retrieve numerical value
s

• Use functions to convert characters to numbers
Ch 9 Attacking Data Stores (Part 2)
Using an Out-of-Band
Channel
• You can inject a query but you can't see the
result
s

• Some databases allow you to make a network
connection inside the query language
MS-SQL 2000 and Earlier
Oracle
• UTL_HTTP makes an HTTP reques
t

• Attacker can use a netcat listener
Oracle
• DNS request is even less likely to be blocked
MySQL
• To retrieve the
fi
le, set up an SMB share on your
serve
r

• Alowing anonymous write access
Leveraging the Operating
System
• Sometimes you can get the ability to execute shell
command
s

• Such as by using a PHP shel
l

• Then you can use built-in commands like
 

• tftp, mail, telne
t

• Or copy data into a
fi
le in the Web root so you can
retrieve it with a browser
9b-1
Conditional Responses:


"Blind SQL Injection"
• Suppose your query doesn't return any data you
can see, an
d

• You can't use an out-of-band channel to
ex
fi
ltrate dat
a

• You can still get data, if there's any detectable
behavior by the database that depends on your
query
Example
• Put in this text for username, and anything for
passwor
d

admin' -
-

• You'll be logged in as admin
True or False?
• This username will log in as admin
:

admin' AND 1=1-
-

• This one will not log i
n

admin' AND 1=2--
Finding One Letter
• This username will log in as admin
:

• This one will not log i
n
Inducing Conditional Errors
• On an Oracle database, this query will produce
an error if the account "DBSNMP" exist
s

• If it doesn't, the "1/0" will never be evaluated
and it won't cause an error
Does User "AAAAA" Exist?
Using Time Delays
• MS-SQL has a built-in WAITFOR comman
d

• This query waits for 5 seconds if the current
database user is 'sa'
Conditional Delays
• You can ask a yes/no question and get the
answer from the delay
Testing Single Bits
• Using bitwise AND operator
&

• And the POWER command
MySQL Delays
• Current versions have a sleep functio
n

• For older versions (prior to 5.0.12), use
benchmark to repeat a calculation many times
Oracle
• No function to cause a delay, but you can use
URL_HTTP to connect to a non-existent serve
r

• Causes a delay until the request times out
Oracle
• This query causes a timeout if the default Oracle
account "DBSNMP" exists
Beyond SQL Injection:


Escalating the Database
Attack
Further Attacks
• SQL injection lets you get the data in the
database, but you can go furthe
r

• If database is shared by other applications,
you may be able to access other application's
dat
a

• Or compromise the OS of the database serve
r

• And then pivot: use the DB server to attack
other servers from inside the network
Further Attacks
• Make network connections back out to your
own computer, to ex
fi
ltrate data and evade
IDS system
s

• Extend database functionality by creating
user-de
fi
ned function
s

• You can reintroduce functionality that has
been removed or disable
d

• This is possible if you get database
administrator privileges
MS-SQL
• xp_cmdshell stored procedur
e

• Allows DBA (Database Administrator) to
execute shell commands
MS-SQL
• Other stored procedures also allow powerful
attack
s

• These read and write to the Registr
y

• xp_regread


• xp_regwrite
Dealing with Default
Lockdowns
• MS-SQL 2005 and later disable xp_cmdshell by
default, but you can just enable it if you are DBA
MySQL
• load_file allows attacker to read a
fi
l
e

• "into out
fi
le" allows attacker to write to a
fi
l
e

• This example makes all hosts trusted on Linux
SQL Exploitation Tools
Algorithm Used by Tools


like SQLMAP
SQLMAP
9b-2
Preventing SQL Injection
Blocking Apostrophes
• Won't stop injection into numerical
fi
eld
s

• If you allow apostrophes into data
fi
elds by
doubling them, you can have second-order SQL
injection vulnerabilities
Stored Procedures
• Makes code re-use easie
r

• But doesn't prevent SQL injection if user input is
included in a parameter
Stored Procedures
• Developer de
fi
nes a procedur
e

• Attacker can still inject with this passwor
d

• Resulting query
Parameterized Queries
Vulnerable Code
• User input inserted into a command, which is
parsed later to match quotes
Parameterized Version
• User input replaces placeholder "?
"

• No parsing required, not vulnerable to SQLi
Provisos
• Use parameterized queries for EVERY quer
y

• Not just the ones that are obviously user-
controllabl
e

• Every item of data should be parameterize
d

• Be careful if user data changes table or column name
s

• Allow only values from an allow-list of known safe
value
s

• You cannot use parameter placeholders for other parts
of the query, such as SORT BY ASC or SORT BY DES
C

• If they must be adjusted, use an allow-list
Defense in Depth
• Application should use low privileges when
accessing the database, not DB
A

• Remove or disable unnecessary functions of D
B

• Apply vendor patche
s

• Subscribe to vulnerability noti
fi
cation
services to work around new, unpatchable
vulnerabilities
Injecting into NoSQL
NoSQL
• Doesn't require structured data like SQ
L

• in SQL,
fi
elds must be de
fi
ned in a Schema,
as Text, Number, etc
.

• In NoSQL, keys and values can be arbitrarily
de
fi
ne
d

• A newer and less mature technology than SQL
Ch 9 Attacking Data Stores (Part 2)
Injecting into MongoDB
Example Login Code
Injection
• Log in with this username, and any passwor
d

Marcus'/
/

• Javascript function becomes this:
Another Injection
• Log in with this username, and any passwor
d

• This is always true (link Ch 9b)
Injecting into XPATH
• XML Data 

Store
Ch 9 Attacking Data Stores (Part 2)
Injection
• This query retrieves a stored credit card number
from a username and passwor
d

• This injection:
Finding XPATH Injection
Flaws
• These strings usually break the synta
x

• These strings change behavior without breaking
syntax
Preventing XPATH Injection
• Filter inputs with a whitelis
t

• Remove these characters
LDAP
• Lightweight Directory Access Protocol (LDAP
)

• Used to store names, phone numbers, email
addresses, etc
.

• Used in Microsoft Active Director
y

• Also in OpenLDAP
LDAP Queries
• Match a usernam
e

• Match any one of these condition
s

• Match all of these conditions
LDAP Injection Limitations
• Possible, but less exploitable because usually
:

• Logical operators come before user-supplied
data, so attacker can't form "or 1=1
"

• Directory attributes to be returned (like
username) are hard-coded and can't be
manipulate
d

• Applications don't return informative error
messages, so exploitation is "blind"
9b-3

More Related Content

PDF
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
Sam Bowne
 
PDF
CNIT 129S: 9: Attacking Data Stores (Part 1 of 2)
Sam Bowne
 
PDF
CNIT 129S: 10: Attacking Back-End Components
Sam Bowne
 
PDF
Ch 10: Attacking Back-End Components
Sam Bowne
 
PDF
CNIT 129S: Ch 3: Web Application Technologies
Sam Bowne
 
PDF
CNIT 129S: Ch 6: Attacking Authentication
Sam Bowne
 
PDF
CNIT 129S: 11: Attacking Application Logic
Sam Bowne
 
PDF
CNIT 129S - Ch 3: Web Application Technologies
Sam Bowne
 
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
Sam Bowne
 
CNIT 129S: 9: Attacking Data Stores (Part 1 of 2)
Sam Bowne
 
CNIT 129S: 10: Attacking Back-End Components
Sam Bowne
 
Ch 10: Attacking Back-End Components
Sam Bowne
 
CNIT 129S: Ch 3: Web Application Technologies
Sam Bowne
 
CNIT 129S: Ch 6: Attacking Authentication
Sam Bowne
 
CNIT 129S: 11: Attacking Application Logic
Sam Bowne
 
CNIT 129S - Ch 3: Web Application Technologies
Sam Bowne
 

What's hot (20)

PDF
CNIT 129S: Ch 4: Mapping the Application
Sam Bowne
 
PDF
Secure coding presentation Oct 3 2020
Moataz Kamel
 
PDF
CNIT 129S: Securing Web Applications Ch 1-2
Sam Bowne
 
PDF
Ch 12 Attacking Users - XSS
Sam Bowne
 
PDF
Ch 1: Web Application (In)security & Ch 2: Core Defense Mechanisms
Sam Bowne
 
PDF
4 Mapping the Application
Sam Bowne
 
PDF
CNIT 129S: 8: Attacking Access Controls
Sam Bowne
 
PDF
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 
PDF
XSS Magic tricks
GarethHeyes
 
PPTX
SQL Injection
Asish Kumar Rath
 
PPTX
Building secure applications with keycloak
Abhishek Koserwal
 
PDF
CNIT 129S: Ch 7: Attacking Session Management
Sam Bowne
 
PDF
Token, token... From SAML to OIDC
Shiu-Fun Poon
 
PPTX
SQL injection prevention techniques
SongchaiDuangpan
 
PDF
Testing Angular
Lilia Sfaxi
 
PDF
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 1 of 2)
Sam Bowne
 
PPTX
Pentesting ReST API
Nutan Kumar Panda
 
PPTX
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Soroush Dalili
 
PDF
Sql Injection Myths and Fallacies
Karwin Software Solutions LLC
 
PPTX
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
CNIT 129S: Ch 4: Mapping the Application
Sam Bowne
 
Secure coding presentation Oct 3 2020
Moataz Kamel
 
CNIT 129S: Securing Web Applications Ch 1-2
Sam Bowne
 
Ch 12 Attacking Users - XSS
Sam Bowne
 
Ch 1: Web Application (In)security & Ch 2: Core Defense Mechanisms
Sam Bowne
 
4 Mapping the Application
Sam Bowne
 
CNIT 129S: 8: Attacking Access Controls
Sam Bowne
 
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 
XSS Magic tricks
GarethHeyes
 
SQL Injection
Asish Kumar Rath
 
Building secure applications with keycloak
Abhishek Koserwal
 
CNIT 129S: Ch 7: Attacking Session Management
Sam Bowne
 
Token, token... From SAML to OIDC
Shiu-Fun Poon
 
SQL injection prevention techniques
SongchaiDuangpan
 
Testing Angular
Lilia Sfaxi
 
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 1 of 2)
Sam Bowne
 
Pentesting ReST API
Nutan Kumar Panda
 
Going Beyond Microsoft IIS Short File Name Disclosure - NahamCon 2023 Edition
Soroush Dalili
 
Sql Injection Myths and Fallacies
Karwin Software Solutions LLC
 
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Ad

Similar to Ch 9 Attacking Data Stores (Part 2) (20)

PDF
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 
PPTX
SQLi for Security Champions
PetraVukmirovic
 
PDF
10 Deadly Sins of SQL Server Configuration - APPSEC CALIFORNIA 2015
Scott Sutherland
 
PPTX
The Spy Who Loathed Me - An Intro to SQL Server Security
Chris Bell
 
PPTX
SQL Injection Stegnography in Pen Testing
191013607gouthamsric
 
PDF
Practical Approach towards SQLi ppt
Ahamed Saleem
 
PPT
Sql injection attacks
chaitanya Lotankar
 
PDF
Hack your db before the hackers do
fangjiafu
 
PPT
Sql injection attacks
Nitish Kumar
 
PPT
Dealing with SQL Security from ADO.NET
Fernando G. Guerrero
 
PPTX
SQL Injection in JAVA
Hossein Yavari
 
PPTX
Day 6.pptx
atreesgalaxy
 
PPT
Sql injection attacks
Kumar
 
PPT
SQL Injection Attacks
Compare Infobase Limited
 
PPTX
Locking Down Your MySQL Database.pptx
Dave Stokes
 
PPT
Dealing with SQL Security from ADO.NET
Fernando G. Guerrero
 
PPTX
Roman Rehak: 24/7 Database Administration + Database Mail Unleashed
MSDEVMTL
 
PPTX
Web hacking series part 3
Aditya Kamat
 
PPTX
Code injection
Gayatri Patel
 
PPTX
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 
SQLi for Security Champions
PetraVukmirovic
 
10 Deadly Sins of SQL Server Configuration - APPSEC CALIFORNIA 2015
Scott Sutherland
 
The Spy Who Loathed Me - An Intro to SQL Server Security
Chris Bell
 
SQL Injection Stegnography in Pen Testing
191013607gouthamsric
 
Practical Approach towards SQLi ppt
Ahamed Saleem
 
Sql injection attacks
chaitanya Lotankar
 
Hack your db before the hackers do
fangjiafu
 
Sql injection attacks
Nitish Kumar
 
Dealing with SQL Security from ADO.NET
Fernando G. Guerrero
 
SQL Injection in JAVA
Hossein Yavari
 
Day 6.pptx
atreesgalaxy
 
Sql injection attacks
Kumar
 
SQL Injection Attacks
Compare Infobase Limited
 
Locking Down Your MySQL Database.pptx
Dave Stokes
 
Dealing with SQL Security from ADO.NET
Fernando G. Guerrero
 
Roman Rehak: 24/7 Database Administration + Database Mail Unleashed
MSDEVMTL
 
Web hacking series part 3
Aditya Kamat
 
Code injection
Gayatri Patel
 
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
Ad

More from Sam Bowne (20)

PDF
Introduction to the Class & CISSP Certification
Sam Bowne
 
PDF
Cyberwar
Sam Bowne
 
PDF
3: DNS vulnerabilities
Sam Bowne
 
PDF
8. Software Development Security
Sam Bowne
 
PDF
3. Attacking iOS Applications (Part 2)
Sam Bowne
 
PDF
12 Elliptic Curves
Sam Bowne
 
PDF
11. Diffie-Hellman
Sam Bowne
 
PDF
2a Analyzing iOS Apps Part 1
Sam Bowne
 
PDF
9 Writing Secure Android Applications
Sam Bowne
 
PDF
12 Investigating Windows Systems (Part 2 of 3)
Sam Bowne
 
PDF
10 RSA
Sam Bowne
 
PDF
12 Investigating Windows Systems (Part 1 of 3
Sam Bowne
 
PDF
9. Hard Problems
Sam Bowne
 
PDF
8 Android Implementation Issues (Part 1)
Sam Bowne
 
PDF
11 Analysis Methodology
Sam Bowne
 
PDF
8. Authenticated Encryption
Sam Bowne
 
PDF
7. Attacking Android Applications (Part 2)
Sam Bowne
 
PDF
7. Attacking Android Applications (Part 1)
Sam Bowne
 
PDF
5. Stream Ciphers
Sam Bowne
 
PDF
6 Scope & 7 Live Data Collection
Sam Bowne
 
Introduction to the Class & CISSP Certification
Sam Bowne
 
Cyberwar
Sam Bowne
 
3: DNS vulnerabilities
Sam Bowne
 
8. Software Development Security
Sam Bowne
 
3. Attacking iOS Applications (Part 2)
Sam Bowne
 
12 Elliptic Curves
Sam Bowne
 
11. Diffie-Hellman
Sam Bowne
 
2a Analyzing iOS Apps Part 1
Sam Bowne
 
9 Writing Secure Android Applications
Sam Bowne
 
12 Investigating Windows Systems (Part 2 of 3)
Sam Bowne
 
10 RSA
Sam Bowne
 
12 Investigating Windows Systems (Part 1 of 3
Sam Bowne
 
9. Hard Problems
Sam Bowne
 
8 Android Implementation Issues (Part 1)
Sam Bowne
 
11 Analysis Methodology
Sam Bowne
 
8. Authenticated Encryption
Sam Bowne
 
7. Attacking Android Applications (Part 2)
Sam Bowne
 
7. Attacking Android Applications (Part 1)
Sam Bowne
 
5. Stream Ciphers
Sam Bowne
 
6 Scope & 7 Live Data Collection
Sam Bowne
 

Recently uploaded (20)

PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
CDH. pptx
AneetaSharma15
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 

Ch 9 Attacking Data Stores (Part 2)

  • 1. CNIT 129S: Securing Web Applications Ch 9: Attacking Data Stores Part 2 of 2 Updated 3-16-22
  • 3. Avoiding Blocked Characters • To prevent injection, many apps remove or encode some character s • A single quotation mark is not needed for injection into a numerical fi el d • You can also use string functions to dynamically construct a string containing fi ltered characters
  • 4. CHR or CHAR Function • These queries work on Oracle and MS-SQL, respectively
  • 5. Comment Symbol Blocked • Code i s SELECT * from users WHERE name='uname ' • Try injecting this value for name : ' or 1=1 - - • To creat e SELECT * from users WHERE name='' or 1=1 -- ' • But the "--' is blocked
  • 6. Crafting Correct Syntax Without a Comment • Injecting this value for name : ' or 'a'=' a • To creat e SELECT * from users WHERE name='' or 'a'='a'
  • 7. Circumventing Simple Validation • If "SELECT" is blocked, try these bypasses:
  • 8. Using SQL Comments • If spaces are blocked, use comments instea d • MySQL allows comments within keywords
  • 9. Second-Order SQL Injection • Many applications handle data safely when it is fi rst entered into the databas e • But it may later be processed in unsafe ways
  • 10. App Adds a Second Quote • Register an account with this name : foo ' • The correct way to insert that value is by adding a second quote (link Ch 2a ) INSERT INTO users (username, password, ID, privs) VALUES ('foo''', 'secret', 2248, 1)
  • 11. Password Change • Requires user to input old password, and compares it to the password retrieved with : SELECT password FROM users WHERE username = 'foo' ' • This is a syntax error.
  • 12. Exploit • Register a new user with this name : ' or 1 in (SELECT password FROM users WHERE username = 'admin')- - • Perform a password change, and MS-SQL will return this error, exposing the administrator password
  • 14. Advanced Exploitation • The previous attacks had a ready means of exposing dat a • Adding UNION to a query that returns the result s • Returning data in an error messag e • What if the results are not exposed?
  • 15. Denial of Service • This attack does not steal dat a • It's merely destructiv e • Turn off an MS-SQL databas e ' shutdown- - • Drop tabl e ' drop table users--
  • 16. Retrieving Data as Numbers • No strings fi elds may be vulnerable, because single quotes are fi ltere d • Numeric fi elds are vulnerable, but only allow you to retrieve numerical value s • Use functions to convert characters to numbers
  • 18. Using an Out-of-Band Channel • You can inject a query but you can't see the result s • Some databases allow you to make a network connection inside the query language
  • 19. MS-SQL 2000 and Earlier
  • 20. Oracle • UTL_HTTP makes an HTTP reques t • Attacker can use a netcat listener
  • 21. Oracle • DNS request is even less likely to be blocked
  • 22. MySQL • To retrieve the fi le, set up an SMB share on your serve r • Alowing anonymous write access
  • 23. Leveraging the Operating System • Sometimes you can get the ability to execute shell command s • Such as by using a PHP shel l • Then you can use built-in commands like • tftp, mail, telne t • Or copy data into a fi le in the Web root so you can retrieve it with a browser
  • 24. 9b-1
  • 25. Conditional Responses: "Blind SQL Injection" • Suppose your query doesn't return any data you can see, an d • You can't use an out-of-band channel to ex fi ltrate dat a • You can still get data, if there's any detectable behavior by the database that depends on your query
  • 26. Example • Put in this text for username, and anything for passwor d admin' - - • You'll be logged in as admin
  • 27. True or False? • This username will log in as admin : admin' AND 1=1- - • This one will not log i n admin' AND 1=2--
  • 28. Finding One Letter • This username will log in as admin : • This one will not log i n
  • 29. Inducing Conditional Errors • On an Oracle database, this query will produce an error if the account "DBSNMP" exist s • If it doesn't, the "1/0" will never be evaluated and it won't cause an error
  • 31. Using Time Delays • MS-SQL has a built-in WAITFOR comman d • This query waits for 5 seconds if the current database user is 'sa'
  • 32. Conditional Delays • You can ask a yes/no question and get the answer from the delay
  • 33. Testing Single Bits • Using bitwise AND operator & • And the POWER command
  • 34. MySQL Delays • Current versions have a sleep functio n • For older versions (prior to 5.0.12), use benchmark to repeat a calculation many times
  • 35. Oracle • No function to cause a delay, but you can use URL_HTTP to connect to a non-existent serve r • Causes a delay until the request times out
  • 36. Oracle • This query causes a timeout if the default Oracle account "DBSNMP" exists
  • 37. Beyond SQL Injection: Escalating the Database Attack
  • 38. Further Attacks • SQL injection lets you get the data in the database, but you can go furthe r • If database is shared by other applications, you may be able to access other application's dat a • Or compromise the OS of the database serve r • And then pivot: use the DB server to attack other servers from inside the network
  • 39. Further Attacks • Make network connections back out to your own computer, to ex fi ltrate data and evade IDS system s • Extend database functionality by creating user-de fi ned function s • You can reintroduce functionality that has been removed or disable d • This is possible if you get database administrator privileges
  • 40. MS-SQL • xp_cmdshell stored procedur e • Allows DBA (Database Administrator) to execute shell commands
  • 41. MS-SQL • Other stored procedures also allow powerful attack s • These read and write to the Registr y • xp_regread • xp_regwrite
  • 42. Dealing with Default Lockdowns • MS-SQL 2005 and later disable xp_cmdshell by default, but you can just enable it if you are DBA
  • 43. MySQL • load_file allows attacker to read a fi l e • "into out fi le" allows attacker to write to a fi l e • This example makes all hosts trusted on Linux
  • 45. Algorithm Used by Tools like SQLMAP
  • 47. 9b-2
  • 49. Blocking Apostrophes • Won't stop injection into numerical fi eld s • If you allow apostrophes into data fi elds by doubling them, you can have second-order SQL injection vulnerabilities
  • 50. Stored Procedures • Makes code re-use easie r • But doesn't prevent SQL injection if user input is included in a parameter
  • 51. Stored Procedures • Developer de fi nes a procedur e • Attacker can still inject with this passwor d • Resulting query
  • 53. Vulnerable Code • User input inserted into a command, which is parsed later to match quotes
  • 54. Parameterized Version • User input replaces placeholder "? " • No parsing required, not vulnerable to SQLi
  • 55. Provisos • Use parameterized queries for EVERY quer y • Not just the ones that are obviously user- controllabl e • Every item of data should be parameterize d • Be careful if user data changes table or column name s • Allow only values from an allow-list of known safe value s • You cannot use parameter placeholders for other parts of the query, such as SORT BY ASC or SORT BY DES C • If they must be adjusted, use an allow-list
  • 56. Defense in Depth • Application should use low privileges when accessing the database, not DB A • Remove or disable unnecessary functions of D B • Apply vendor patche s • Subscribe to vulnerability noti fi cation services to work around new, unpatchable vulnerabilities
  • 58. NoSQL • Doesn't require structured data like SQ L • in SQL, fi elds must be de fi ned in a Schema, as Text, Number, etc . • In NoSQL, keys and values can be arbitrarily de fi ne d • A newer and less mature technology than SQL
  • 61. Injection • Log in with this username, and any passwor d Marcus'/ / • Javascript function becomes this:
  • 62. Another Injection • Log in with this username, and any passwor d • This is always true (link Ch 9b)
  • 63. Injecting into XPATH • XML Data 
 Store
  • 65. Injection • This query retrieves a stored credit card number from a username and passwor d • This injection:
  • 66. Finding XPATH Injection Flaws • These strings usually break the synta x • These strings change behavior without breaking syntax
  • 67. Preventing XPATH Injection • Filter inputs with a whitelis t • Remove these characters
  • 68. LDAP • Lightweight Directory Access Protocol (LDAP ) • Used to store names, phone numbers, email addresses, etc . • Used in Microsoft Active Director y • Also in OpenLDAP
  • 69. LDAP Queries • Match a usernam e • Match any one of these condition s • Match all of these conditions
  • 70. LDAP Injection Limitations • Possible, but less exploitable because usually : • Logical operators come before user-supplied data, so attacker can't form "or 1=1 " • Directory attributes to be returned (like username) are hard-coded and can't be manipulate d • Applications don't return informative error messages, so exploitation is "blind"
  • 71. 9b-3