SQL Injection & XSS Slides
SQL Injection & XSS Slides
Purpose
understanding pen-test existing internal application good practice / methods sql injection
prevention in programming
SQL Injection
How strong Firewall rules - easily walk
through port 80
SQL Injection
Malicious User Firewall
Port 80/443
A Threat?
Albert Gonzalez 130 millions credit card number Used SQL - Injection technique Steal data from internal corporate network Sentenced 20 years in March 2010 x-Informer to US secret service to catch
hackers
Tuesday, May 17, 2011
A Threat?
Sample Attacks
comments/inline comments admin - select username,password where
username=admin-- and password=pass;
Sample Attacks
comments/inline comments or 1=1- select username,password where
username=admin and password= or 1=1-- ;
Vulnerability Testing
GET/POST methods unescaped numerical value single quote unescaped string double quotes unescaped string etc
Tuesday, May 17, 2011
Vulnerability Testing
look for page errors? - 500 Server Error redirect page? SQL/ODBC Errors page differences and 1=1-- , and 1=2-Tuesday, May 17, 2011
add some sql statement / blind? ?id=23 and / ?id=23 and {1=1,1=2} error? differences
Tuesday, May 17, 2011
where id = addslashes($_GET[id])
where id = $_GET[id]
information_schema.tables
SELECT group_concat(table_name)
information_schema.columns
SELECT group_concat(column_name)
FROM information_schema.columns WHERE table_name = tname;
Stacking Queries
Random Test
Choose your internal website search for sql injection possibilities do some penetration test
Prevention
Whose Responsibility? No SQL database, connector, or
Monitoring
Never reveal error messages
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1 INSERT INTO user (username, password, admin) VALUES ('Mr. O'Neil', 'password', false); <?php if (! $query) { die (Error: mysql_error() ); } .... Not only does this confuse/anger the visitor, but reveals sensitive information about your application
This is BAD
Monitoring
Error Handling Never show errors in production Log errors so they can be xed or email
them
Log Error
function sql_failure_handler($query, $error) { $msg = htmlspecialchars (Failed Query: {$query}<br>SQL Error: {$error}); error_log ($msg, 3, /home/site/logs/sql_error_log); if ( dened(debug) ) { return $msg; } return Requested page is temporarily unavailable, please try again later.; } mysql_query ( $query ) or die(sql_failure_handler($query, mysql_error()));
Prevention
Escaping Input Prevents SQL Injection.
Prevention
Simply adding addslashes() or magic_quotes
enough?
$id = addslashes($_GET[id]) ?
Escaping Methods
mysql_real_escape_string() addslashes() Class Object method such PDO $pdo->quote() method not available to all DB types !!! multiple escaping method? No. One is enough!
Tuesday, May 17, 2011
Prevention
using addslashes() ? - unescaped numerical
$qry = "SELECT * FROM\ tblTest WHERE \ TestID = " . addslashes($_GET['id']);
Prevention
using mysql_real_escape_string() ? - on
unescaped numerical
$sql = "SELECT * FROM tblTest WHERE TestID=".mysql_real_escape_string($_GET['id']);
Prevention
unescaped numerical - use type casting
(int) $_GET[id]
Magic Quotes
Cannot simply rely on Magic Quotes Turning On Magic Quotes will not solved all
your problems - eg: unescaped numerical variable
Prevention
Quoting all arguments since single quotes are always escaped, however for numerical always numeric
casting
Tuesday, May 17, 2011
Like Quadary
SELECT * messages WHERE subject LIKE
{$sub}%
% used as wild card _ (underscore) represent any character $sub = mysql_real_escape_string(%_) still %_ - no changes
Tuesday, May 17, 2011
Like Quadary
large amount of data queried more memory usage slow down database slow down process / server possibilities of Denial of Service (DOS)
attack
Tuesday, May 17, 2011
Like Quadary
Solution - addcslashes() customs escaped characters
$sub = addcslashes ( mysql_real_escape_string(%something...), %_);
You dont need to deal with escaping data because its done by the PDO library.
No more nasty concatenation No more hoping every programmer escaped query properly
Parameter Placeholder
Query need a dynamic value:
SELECT * from News WHERE id = 254
user input
Parameter Placeholder
Query parameter takes place of dynamic
value: SELECT * from News WHERE id = ?
parameter placeholder
Parameter Placeholder
How the database parse it
SELECT
expr-list
query
FROM
simpletable
News id
WHERE
expr
equality
= ?
parameter placeholder
Parameter Placeholder
How the database execute it
SELECT
expr-list
query
FROM
simpletable
News id
WHERE
expr
equality
= 254
parameter value
Parameter Placeholder
Interpolation
SELECT
expr-list
* id
query
FROM
simpletable
News
equality
= 254
WHERE
expr
OR
254
SQL Injection
Parameter Placeholder
How the database execute it
SELECT
expr-list
query
FROM
simpletable
News id
WHERE
expr
equality
= 254 OR TRUE
Parameter Placeholder
Whitelist Map
http://example.org/news.php?sort=date&dir=up
<?php $sortorder = $_GET[sort]; $direction = $_GET[dir];
unsafe
Whitelist Map
Fix with a Whitelist Map
<?php $sortorders = array ( status => status, date => sysdate); $directions = array ( up => ASC, down => DESC); $sortorder_default $direction_default = status; = ASC;
Whitelist Map
Map User Input to Safe SQL
<?php if ( isset ( $sortorders [ $_GET[sort] ] ) ) { $sortorder = $sortorders [ $_GET[order] ]; } else { $sortorder = $sortorder_default; }
Whitelist Map
Map User Input to Safe SQL
<?php if ( isset ( $directions [ $_GET[dir] ] ) ) { $direction = $directions [ $_GET[order] ]; } else { $direction = $direction_default; }
Whitelist Map
Interpolate Safe SQL
<?php
whitelisted values
Prevention
Limited Database User Access GRANT specic permissions DROP, CREATE, etc should be revoked
from connected DB user
XSS : Denition
computer security vulnerability in web
application
XSS : Example
simple web application that directly output
the user supplied URL parameter
<?php echo Selamat Datang . $_GET[name];
XSS : Example
javascript injection:
lesson1.php?name=</script>alert(/XSS/);</script>
XSS Threat
XSS is most common injection vulnerability Direct output of user input allows injection
of arbitrary content into website
Reective XSS
Simplest form of XSS User input is read from the request
the browser parameters and written directly into the output
Included malicious code is executed within Victims browser has to execute the XSS
triggering request itself
Tuesday, May 17, 2011
Persistent XSS
Stored / permanent XSS User input is read from a request and
stored in RAW
Persistent XSS
victims browser visit a website stored user input is read from database and
directly written into the output within victim browser
XSS Dangers
Displaying annoying pop-ups Redirect - malware Modication of text and images
(defacement)
Manipulation of client side application logic Theft of clipboard, cookies, passwords XSS traverse rewalls - port 80/443
Tuesday, May 17, 2011
XSS Test
Displaying pop-ups most commonly used for diagnose and
demonstration of XSS problems
XSS: Redirection
used by spammers and malware industry harmless if redirect for advertisement
purposes exploits
XSS: Redirection
Just modies document.location
<script> document.location = http://www.malware.org; </script>
Support engineer open ticket steal cookies change submit action - onSubmit
eventhandlet
Tuesday, May 17, 2011
XSS problem is not the possibility to break input lter should use a whitelist of
Injection in Stylesheet
Raw user input is inserted into information Injected are IE expression, Javascript URLs
or Mozillas moz-binding
<style> a { color: expression(alert(1)); } </style> <style> a { color: <? echo $_GET[color]; ?>; } </style>
Injection in Javascript
Raw user input is inserted into javascript Injection is normal Javascript
<script> var str = name: ; alert(123);//; document.write(str); </script> <script> var str = name: <? echo $_GET[name]; ?>; document.write(str); </script>
Thank You
http://blog.xjutsu.com [email protected]