Hello, hello everybody. Buenos dias a todos los que habláis español. How many of you speak Spanish? Oh, perfect. Well, first of all, thank you for coming to this session about time-based blind SQL injection using heavy queries. Let me introduce myself. My name is Chema Alonso. I'm from Spain. And if you are wondering, no, I'm not a bullfighter, and no, I'm not a flamenco singer, even if you think I look like. I work as a security consultant in a company in Spain called Informatica 64, and I'm also a Microsoft MVP. MVP stands for Most Valuable Professional, but some people said that it stands for Most Valuable Pets. I'm not sure which one is the right one. But MVPs don't work for Microsoft. It's just an award that Microsoft gives to some technicians around the world. And I'm an MVP in Windows Security. So, thank you. And let me introduce my friend, my colleague, my co-speaker today. He does work for Microsoft, and he's happy. I don't know why. And he's a technical guy. He's a technical guy in a marketing department. He works as a Microsoft IT pro evangelist, helping people in the Microsoft TechNet program with Microsoft products and technologies and that kind of things. Go ahead. By the way, my name is Jose. Ah, Jose, of course. He probably is a bullfighter. Okay, thank you, Chema, for your presentation. Thank you all for your interest in this session. Okay. Well, the entire credit for this idea and for the vast majority of the presentation must be given to Chema. As a security consultant, he's an expert in many hacking techniques. And around one and a half years ago, he started his Ph.D. So he started writing a paper on blind vulnerabilities in web applications. He conducted a lot of research in things such as blind XPath injections, blind LDAP injections, and blind SQL injections. In fact, he's the author of one of the most in-depth papers about blind LDAP injections, about LDAP injections in general. And I helped him present that paper on Black Hat Europe 2008, which was held in Amsterdam around four months ago. Everything went well there. So, as you can imagine, we end up in a typical Amsterdam coffee shop smoking some kind of substance. And with the enthusiasm of the moment, we thought, okay, why don't we send another paper to DEFCON in the USA, since even though our English is quite poor, people seem to understand us. Okay, so that's what we did. We sent our paper to DEFCON. The organization here at DEFCON, I don't know why they smoke, but they said, okay, this is good enough. So come on over, and here we are with all of you. So please, Chema, introduce us into today's agenda. Well, this is the agenda for today's session. As you can see, this session has two parts. The first one is about things that you may probably know about topics as code injection, blind attacks, blind SQL injection, and time-based blind SQL injection. After finishing this part, we are going to get into the new part of this session, and it's about how to perform a blind SQL injection, time-based, but without using the normal time delay function that databases have. In this case, we are going to use a new trick. It's a trick to get a time delay using a heavy query. Let's see how it works. At the end, we are going to have several demos, and we are going to release a new tool. This tool is called Marathon Tool because it's a very slow tool, but it works very well. And after that, you are free to do whatever you want here in Las Vegas. So let's start from the beginning. Okay, let's start with that brief summary, 10 to 15 minutes, a review about things you probably know if you've been following SQL injection or code injection attacks for the last five to six years. Code injection has been a security problem since a long time ago. The first time most of us heard about it was back in December 25th, 1998, when a guy that answered to the nickname of Rainforest Puppy published a paper on how to attack ASP application using SQL queries. It's been almost 10 years since then, but still one of the main issues in web security. In fact, last September, OWASP, Open Web Application Security Project, published a document rating the 10 most common security vulnerabilities in web application, and code injection was rated in second place right after cross-site scripting. Nowadays, you can find plenty of documentation, guides, tutorials on how to do techniques such as XPath injection, LDAP injection, or SQL injection. And let's go now into reviewing a little bit about what a blind attack is. Most of you probably already know, but when an attacker is able to inject code into a vulnerable web application, it depends where he can inject the code and what he wants to do. He can follow many methods. Sometimes if he can inject code in the name of the password, he can use that injection to bypass the login page and access a private website. Other times, if he's able to inject code in the URL, he's able to extract directly in the web page response data from the database. But some other times, he's able to inject code, but he doesn't get any data directly out from the database or any error message that he could use to deduce information about the database. So this kind of environment gives the developers and the testing teams, the security testing team, a false sense of security since... Can you hear me? Now, yes. Since data is not directly exposed in the web page. But if the attacker is able to inject code, injecting some LDAP, XPath, or SQL queries, is able to deduce data, if the response page that he obtained, depending on the data, is different or has different behaviors. Okay, so blind attacks, in summary, are possible when web applications behave in a different way when you insert a query that is always true. And when you insert a query that is always false. If you get different response, then that web application is possibly vulnerable. Okay, so let's now go into blind SQL injections attack. If a web application that is using a relational database, the attacker will, of course, inject a SQL query. The most easy way to find out a vulnerable web application to this is injecting something that is always true, like one equals to one, looking at the behavior of the web page, then introducing something that is always false, like one equals to two, looking at the response page, and finding the difference. After you find a vulnerability in a web page, well, the attacker's only job is to find these different behaviors, and then to construct the correct queries to being able to extract the data. So let me now go on how to, more deeply on how to find these different behaviors. Okay, back in 2004, Cameron Hopkins delivered a very good presentation at Black Hat. Let me go here. And title, Blind Attacks, How to Automate Blind Attacks Against a Web Application. In it, he described many methods on how to look for these different web page behaviors. So he used methods using different hatches, using different HTML structures, using different patterns, and even using different linear ASTI sums, okay? Before that, in 2002, Chris Hanley published a very famous paper called More Advanced SQL Injection, a paper in which he described how to use a time response to look for difference in the behavior of the web pages, okay? And finally, let me tell you about how to construct or how these queries are constructed really quickly, okay? So once we've got difference behavior, what to do is to construct the query to extract the data. Okay, normally we use conditional operators, greater than, lesser than, equals to. Et cetera, to compare the value that we want to extract from the database against a fixed value. We, depending on the response that we get, if it's false or true response, then we will change that fixed value, all right, and iterate that process until we find out the correct value. In the example that you have in the slide, you have an example to find the ASTI value for the first letter of the first user name in a database. The example is for my SQL server, MS SQL server, and Oracle database. At the end of the process, and at the end of all the iterations, you are able to get all the complete user name, okay? Well, as you can imagine, this is a tough task to do manually. So last year, many tools have been developed, and right now, there are a lot of tools in the internet to perform these kind of attacks. This one is SQL BF Tools. There are several tools in this package, and it's focused in MySQL databases. This tool is able to extract all the database structure, all the data in tables, all the values of variables, and also it's possible to extract files from the server because this tool uses the load file function that allows MySQL to load files from the server. So it's possible to extract, for instance, using Blind SQL at injection attack, the password file or the init file or whatever in the server. This tool only uses keyboard searching, so it's only for one of the methods that Cameron Hotkey described. And if we are talking about tools for Blind SQL injection, it's impossible to forget AppSynt. AppSynt is based on the principles described by Cameron Hotkey, and it's one of the most powerful tools and a well-ended tool to perform a Blind SQL injection attack. This tool is ready to work with Microsoft SQL Server, PostgreSQL, Sybase, and Oracle, and as you can see in this screenshot, it's possible to work with get and post parameter, with cookies, with HTTP connection and HTTP S connection. This tool should be initialized and is going to throw some always true injection and always false injection, and after that, it's possible to extract all the database structure, the username that the web application is using to connect against the database, the tables, and of course all the data that could be sport in XML files. This tool is not ready to work with time-based Blind SQL injection attacks, and if you want, you have to perform one of these tests, you need to use other tools. In some scenarios, in which it's possible to inject code, but there is no difference between the true answer page and the false answer page, time-based behavior should be used. In those scenarios, we can use the time delay function in the database to delay the answer. So for instance, in Microsoft SQL Server, we can use the wait for function to delay the answer if the condition is true, so when the condition will be true, we will have a delay, if not, we are going to have a quicker answer. We can do something similar in Oracle databases, but in Oracle, we have to use a package in PL SQL language. This package is called dbms-log, and the function is called sleep, but if you want to use this function, you have to inject code in a PL SQL procedure, and it's a little bit complicated to find an environment where to inject code in a PL SQL procedure. In MySQL, we have two options. One of them is the sleep function, that it comes along with the version 5 and the version 6 of this database, and the benchmark function. The benchmark function is a special function that forces the database to work a lot of time, so depending on the parameter set with this function, we can measure six, 20, or 30 seconds that we can use to analyze a true or a false behavior. In this example, you have the injection for Microsoft SQL Server. This injection is used in the wait for function, and the database will stop by a second if the condition, in this case, exceeds select asterisk from user, retrieve any data. If you are interested in this topic, last year here in DEF CON, people from Sense Post delivered a very nice presentation to tell it's all about the time, and they have a very nice paper on the internet, and you can grab it when you want. And this is an example in the real world. This is an exploit published last year in Millworm Web Security. This is an exploit for SolarEmpire WebGain. As you can see, some people cannot stand losing. And this is not the entire exploit. You can look for the entire exploit on the website, and you have the URL in the white paper you have in the CD. And this exploit has two parts. In red color is the time measuring, and in blue color is the injection. As you can see, in this case, the injection takes place in the user agent parameter of the HTTP header, and it's injecting a benchmark function that delays, force the database to delay more than seven seconds. So at the beginning, the exploit takes a timestamp. Then after sending the package, it takes another timestamp and compares both. And if difference is greater than seven, then it means that that letter who had been tried out is part of the password. It's cool, no? Okay. Here in the screenshot, you have another tool, an example of a tool to automate a time-based method to perform a SQL injection attack. The tool is called SQL Ninja. It uses the wait for function described by Chris Handley, and is ready to extract data out from a Microsoft SQL database that is behind a vulnerable web application. Well, until here, you probably knew all this, okay? It's not new stuff. If you've been following SQL injection techniques for the last five, six, seven years, you probably knew all that, okay? It was just a quick summary. But what happens in environments where we can inject code, but we don't get any different behavior in the web page response, okay? Then, normally, what we will use is what Chema just explained. We'll use time delays to perform the attack, okay? But what happens if behind that vulnerable web application, what we got is a database with no time delay functions, okay? We can have a database such as DB2, Microsoft Access, or even an Oracle database where we are not able to inject code in the PL SQL function, okay? Do you think it will be possible to continue our attack and extract information in an environment like that? Okay, how many? Okay. Not many. Well, we are here to explain that, okay? That we are able... Let's finish our presentation. Let's pack and go. Okay. If it was impossible, we wouldn't be here, okay? We are here to explain how it's possible in an environment like that to extract some data. So please, Chema, introduce us to this new topic. Just shouldn't have drank so much whiskey yesterday. Well. Well, before to know how to do this, we are going to analyze how a SQL query will be executing a database engine. Let's suppose this query with more than one condition, in this case with two conditions, and they are joined with an end operator. Let's suppose that the first condition, condition one, lasts 10 seconds, and the second takes 100 seconds to be executed in the database engine. So the question are, which condition should be executed first, and which condition will be executed first in the database we are analyzing? Well. To know this, let's have a look at the table, and let's suppose that the heavy condition is executed in first place. As you can see, the worst condition is executed in all the cases, so in all the cases, we obtain a very bad performance in the response time because the heavy query will be executed. This is a very bad tuning decision and databases try to avoid this situation. As you can see, only when the first one is true, when the heavy condition is true, we are going to obtain 110 seconds, and the difference between the true and the false with the heavy query is very difficult to measure because it's not a long time. On the other hand, if the light condition is executed in first place, we are going to obtain a big difference between the true in the first condition and the false in the first condition. I mean, when the light condition is true, the heavy condition will be executed, so we are going to obtain the worst performance. We are going to obtain a big amount of time. But if the first condition is false, it means that the second condition shouldn't be executed, and then only 10 seconds is going to be executed is going to be the time. So in this case, we have a big difference between the true and the false. It means if we obtain a long time, it means the light condition is true, and if we obtain a short time, the light condition is false. With this, we can construct a binary logic to extract all the information from a database. We are going to construct a light query to extract the data, and a heavy one to make a transaction. This is the time difference. Okay? But which condition will be executed first depending on the database? Well, it depends, because some databases have a special process before they execute the query. This process is an optimization process, and other databases don't have that process. Okay, so let me explain a little bit how all this works. Okay, as Chema has explained, enterprise databases, for performance purpose, they go through an optimization process before they execute a work clause. They try to analyze the conditions inside the work clause, so they try to execute first the conditions that are lighter. Okay? First of all, what they do is they get the statistics, indexes, density of values, etc., and try to find, and find out these different costs in the conditions. Okay? If they are not able to find any difference, they go through what is called a prediction algorithm and try to find these different costs. And if they are not able to do it with this method, then they just behave as default. Okay? Depending on the database, they will evaluate the work clause from left to right, or from right to left. So we got databases like, for example, Oracle, that always execute first the condition on the right side. Okay? So the last condition is the one that is executed first. Sometimes in this kind of environment, the performance algorithms make mistakes, and we may help them and avoid mistakes if we introduce little tricks into the work clause. Okay? To avoid these kind of problems. Other... Okay. Other databases, the ones that don't have optimization process, here is a developer task to establish which condition will be executed first. Okay? Databases such as Microsoft Access don't have performance optimization. So the developer must say which condition will be evaluated first. At the end, it is possible to conduct this type of attacks that we are gonna finish explaining in the next slide in any type of database engine. Okay? But we have to know which behavior the database is following to evaluate the conditions. Okay? Okay. Well, and we know that we can extract all the data with a light, with a heavy condition, and the last thing is how a heavy condition should be constructed. Well, the trick is very easy. We can use a very big cross-joint between a table and crossing itself as many times as we need just to cross records in memory and make... make the database work a lot of time. So in this example, as you can see, we are using the C user table and we are crossing this table eight times with itself and constructing a big query, big amount of data in memory, and as you can see, the light condition is just an always true condition because we are comparing the first letter of the... the value... the actual value of the first letter of the user name with 300, and all you know that it's true. Isn't it? Huh? Huh? Okay. Well, to construct heavy queries in different types of databases, we can use the normal tables. For instance, in SQL databases, Microsoft SQL Server databases, we can use the data dictionary tables. For instance, C user or C subjects or C files or whatever you want. In Oracle, the public views are very useful for this... for this attack. For instance, all user, all objects, or whatever you want. In MySQL, version five and six, we can use the schema tables. And in Microsoft Access, we can use the... the tables. Depending on the version, we can use msys-access-objects or msys-access-storage. Ispa is always... in access databases, we always have access privilege to these tables. So, we... these tables always... we can use it. And if not, if we don't have any table and we don't have access privilege to any of these tables, well, I suppose you can guess any table in the databases. You can use the common suspects like clients, customers, news, logins, user, providers, or whatever. And I'm sure you can imagine one of these. If not, you shouldn't be here. Well, and now we are going to do very... an... an strange demo because this demo is working over Windows Vista. And our virtual PC is the worst environment. So, can you imagine? Well, we have... Hey, stop. We have... in our testing environment, we have this application. This application has four databases. First one is SQL Server 2005. The second one is Oracle 10G. Access 2000 and Access 2007. The first part of the application is a normal application in which we can apply the keyword sets for blind attacks. So, we can try with n equals to 1 and we receive this test. As you see. Oh. And if we inject an always false injection, we obtain nothing. Okay? Well, in this other environment, it's... not possible to obtain this because no matter what we inject, we always obtain the same... the same result. This is just because if any error occurs, the developer give us a default result. In this case, it's the same result. So, in this case, we can try with the wait for method. And we can try, for instance, with the wait for method. This one. This one. This one. Okay. In this case, we are going to use the wait for function in SQL Server. And if the condition is true, the database is going to stop for five seconds. This demo only works if we count in Spanish. One, two. What? I told you. I'm sorry. If not, exit. If exit. Uno, dos, tres, cuatro, cinco. Okay, perfect. And if we change the condition from exit to not exit, we obtain a quicker answer. Okay? But if we have an access to this function, we can use our method with heavy queries. In this case, we are going to perform a time delay using seven times the same table. In this case, the sys user table. As you can see, we use sys user as t1, sys user as t2, sys user as t3, and sys user as t7. Okay? And this condition will only be executed if the like condition, in this case, select count asterisk from sys user, retrieve any data. Okay? Understood? Well. Yes. Yes. Let's copy and let's paste in the web browser, Internet Explorer web browser. Wonderful. Everything? Okay. One, two, tres, cuatro, cinco. I don't want any problems. Well, we cannot set up the time delay, but we can measure time delay, and as you can see. Okay? We can perform the same with Oracle Database. And to answer, in this case, with all users table. Okay, copy the URL. Look them up. Paste. Well, in this case, it's the same trick. In this query, we are using all users table, as the one, as the two, as the three, ta-da. And this query will be execute only if the like condition, in this case, exceeds, from exceeds count aesthetics from all user, retrieve any data. This will be true, and of course, the heavy query will be execute. So, let's start. One, dos. Tres, cuatro, cinco, seis, siete, ocho, nueve, come on, Oracle. Once, doce, trece, catorce, quince, dieciséis. Could you bring me a coffee? Oh, perfect. Well, and if we change the condition, we obtain. This is easy for Oracle. Well, we can perform this with all the databases you can imagine. Well, the problem is how can we automate this, because this is an easy, tough task to do it manually. So, we can use a tool that we developed. It's public from yesterday, and the short code is available, and it's very similar to Absinthe, is this. And we can try it, for instance, with, first of all, with SQL Server. And, let's try. Well, first of all, we have to set up the URL, then the parameter ID, the default value, if the parameter is injectable or not. And this is the time. And this is the time. And we have an option. In this case, we are working in a local environment, so we can go faster than in a real environment. So, with SQL, we can use, for instance, six, one second. One second. Well, in the way people you have in the CD, you have a little explanation of this parameter, but are very easy to understand. Well, let's initiate this. This and let's extract the, and in this case, just the user. Well, we can see what the application is doing in the log. First of all, the application is looking for how many tables I need to construct a heavy query. In this case, it is going to use, the application is going to use eight or nine tables. Okay, and now, once the application has the number of tables it needs to construct the heavy queries, it starts to look for the length of the username, in this case, two, because we have the user that nobody set up in the SQL Server databases, you know? Now, the query changed it. I mean, we are not looking for the length of the username. We are looking for an axi value, so we need to try out a new heavy query, and now it's looking for a new heavy query. And when the application has a new heavy query, it starts looking for the values. In this case, the system user, and as you can imagine, the first one is the S. Can you get the second one? And the second one is, well, come on, come on, come on. Well, it finished. The second one is the A, so at the end you can get the system user. Well, let's do this, but with an access database. In access, we don't have any time delay function, so we have to use mandatory this method if we can extract the information from there. And we clear the log. Yes. Yes. Do you really want to clear the log? Si. Perfect. I love my country. Access 2000 and, 2000. Now we set up access 2000. The same ID. Now we are going to use the same parameters, and as you can see here, we have, uh, a default table. In this case, MC is access object, but we can use whatever we want. And let's initialize. And now, the panel changed, because in access we don't have data dictionary, so we have to use the table name and the column name. Well, you can perform a brute force attack or a dictionary attack looking for table using the same method, okay? And we can get the data, okay? And it's going to perform the same. In this case, the log has a verbosity level, so you can set to see a summary, as we are seeing now, or we can see all the query that is being thrown in. So you can see the URL and the whole query with all the tables and too much information for me at this time in the morning. So, now the engine, now the name of, the number of users in this table is four, and the application is going to be working until it extracts all the data. Let's see the tool working, and we are going to finish the slides. Working over Windows Vista, incredible. Well, in the slide you have all the demos, but with the WebGet tool, and you can see all the queries that we are, that we have thrown against the database. Well, this is the tool, marathon tool, and conclusion. Okay, so conclusion's time. Well, as you have seen, this method works with any kind of database, and it doesn't matter if the database have a delay function or not. It's not a very quick method, it's slow, depending on the queries we have to build, but it's very useful finding vulnerabilities that are not shown up using other methods. It's not a silver bullet, we don't think it's a silver bullet, but it's just another bullet, a bullet in your pocket that you can use if you have to. Okay, so now, we'll leave you here our contact information, and information about some other people that have helped us with this presentation, coding the application, and doing some research. And we are going to be there, and we will be in room 104 for questions on this session, and we'll be heavily enjoying Las Vegas until Sunday. So, have a nice DevCon. Thank you all.