Hello and good afternoon, everyone. My name is Sid, and I'll be talking about how SQL injection worms can affect the Oracle applications. Most of you must be aware that there was a worm which affected the Internet like a year ago. That mainly targeted the MSSQL backend database. So I'm going to be presenting how these worms could target the Oracle applications. A little bit about me. I work as a senior IT security consultant in the UK. And that's my blog, not-so-secure.com. All the tools and the demos will be available on this blog. I'm a little bit pressed for time, so if you can keep your questions to the end. And if I'm not able to answer them, just catch me after the talk, and I'm happy to answer them. So let's get started. When we talk about SQL injection in Oracle, there can be two broad categories, depending on where your injection point is. Your injection point could be in an anonymous PLSQL blog. Which will typically be called as a PLSQL injection. Or you can have the injection in a single SQL statement, like a SELECT query. So a common example is a WHERE clause in a SELECT query, where you have your injection point. So, if you have a PLSQL injection, mostly things are quite straightforward. It is more or less like having an SQL plus access to an Oracle backend. Where you can just copy-paste stuff, or you know, just write, just throw queries to it, and it will get executed. Things get a little difficult. When you are encountering SQL injection in a single SQL statement, like SELECT query. Why are they different? That's because if you have the injection in the SQL statements, like SELECT query, then Oracle by design does not support semicolon in the single SQL statements. So you cannot end the query and do like, create function. So it gets a little difficult if you are encountering injections there. But if it's a PLSQL injection, things are quite straightforward. You can execute DLN. You can execute DML or DDL statements. You can use autonomous transaction to commit changes. And all in all, things are quite easy. I'll touch upon the SQL injection a little later. So this is how a typical PLSQL injection would look like. Over here, I've created a sample procedure called orassso.test. It takes a variable. And that variable basically then gets embedded into a begin and strings. And it basically is used. In construction of an anonymous PLSQL block, which then gets dynamically executed. So over here, you can clearly see it's a PLSQL injection. And you can take control of that PLSQL block. So you can provide that parameter Q as a null and then execute immediate, maybe grant DBA to public. So over here, you don't have any restrictions. You can use semicolon and execute multiple statements. As I said, exploitation is fairly straightforward. So when we are talking about web applications, when will you typically encounter a PLSQL injection from web applications? A classical example of this is Oracle application servers. Now, Oracle application servers have had a series of vulnerability in the past. This particular example was a flaw in the PLSQL gateway, which was fixed by Oracle in 2006. And the idea to show this as an example is not to demonstrate a vulnerability which was fixed in 2006, but to give you a general idea of how do you go about exploiting PLSQL injection from web applications. So over here, this is how a standard Oracle application server's URL looks like. The part after PLS is called DAD, database access descriptor. So this vulnerability allowed you to inject arbitrary PLSQL. But the PLSQL which you inject gets executed with the privileges of the DAD user. So now, from here on, you can... As long as you can execute PLSQL, you will then be able to exploit the vulnerable packages stored in the backend database, and then, using the vulnerabilities in them, exploit them and become DBA. Once you become DBA, then you will be able to do nasty things like OS code execution. In this particular example, this DAD user happens to be a really low-privileged user. It's called ORA SSO underscore public. SSO stands for single sign-on. It's one of the default packages which it comes... Ships with when you install Oracle application servers. So this user doesn't even have the create function privileges. So, I mean, to get past that, you use something called as cursor-based injection exploits. And there are tons of them available on Millworm. Once you become DBA, then you can execute OS code. Again, in an earlier talk, someone touched about how do you go about executing OS code. I'll be showing a small example. For example, with Java. So, I wrote a simple Perl script called OAP Hacker, which basically exploits this flaw. I'm going to show a quick demo of this. So, what I said earlier, this demo is basically to prove how to... How do you go about exploiting PL-SQL injection from web applications. Right. So... So this is a default install of Oracle application server 10G second release. It comes with a lot of default packages. One of them is called SSO, or a single sign-on, which we will be using here. It is a flaw in the PL-SQL gateway. So it has nothing to do with the applications which you have deployed on the application server. Your applications which you've deployed on this server might be completely flawless. But because it's a flaw in the gateway, you still are vulnerable. So, using this flaw, you can execute SQL queries like select user from dual. And... I don't know if you can see that. If I throw in a query called select user from dual, it says ORA SSO underscore public. So that's the user privileges with which I'm running my SQL queries. So now if I change this query and do something like select star from system, which contains the password hashes, then you will see this will fail because our user doesn't have the privileges to query this table and we can't get the Oracle hashes. But because we can execute PL-SQL, now we can exploit the packages present in the backend database and elevate our privileges. So all you do is take this URL and pass this to this Perl script, oaphacker.pl and you give it the OS command which it wants to execute. Like in this case, maybe ipconfig or whatever. And it will first check if the URL is vulnerable or not. If it's vulnerable, then it will check for the current privileges whether you are DBA or not. If you are not DBA, then it will launch a series of exploits and eventually give you DBA access. Once you have DBA access, then it will build up a Java function with which you can execute OS code. So now it says we are not DBA. It's going to run a privilege escalation exploit. The first exploit maybe didn't work so it's going to run it again. So now it has given us the DBA privileges. Now it's going to build up a function for OS code execution. So that's it. It has now built up a function through which you can execute OS command and get the output in the web browser itself. So you don't really need any other tool. So now all you have to do is copy paste this URL into a browser, the argument which you want to execute, and that's it. You are done. I'm struggling to copy in this example. So I'm going to run the command whoami. If you can see it here, it says ntauthority slash system. That's basically how Oracle runs in Windows. So this was an example of how will you go about exploiting PLSQL injection from your applications. Right. Now we move on to something more difficult and something which you will probably more encounter, which is SQL injection in a single SQL statement. Now as I said earlier, Oracle by design does not support execution of stacked queries in SQL. It does so in PLSQL, but not in SQL. So if you find an injection in a select query such as this, you are really limited. So in order to execute multiple statements, like, you know, even if you want to exploit vulnerable packages, you need to find a way by which you can execute multiple statements. So you essentially need to find a way to inject PLSQL. So one of the ways to inject PLSQL in SQL is if you can call some function which is vulnerable to PLSQL injection. So you can pass the PLSQL as an argument to that function and then call that function in your SQL. Right. This will get, I'll elaborate on this a little bit more. So this is actually something not very new. In 2005 at Black Hat somebody actually touched on this. So there are other functions which you can call in SQL. There are functions which are vulnerable to buffer overflow which you can call here. For example, this one. This was showed in 2005 Black Hat talk. This was a function which basically was vulnerable to buffer overflow. And as you can see, you can just directly execute the command and it gets executed with system privileges. So you don't really need anything over here now. But I'm not going to touch buffer overflows here. I'm going to touch functions which are vulnerable to PLSQL injection. Functions vulnerable to PLSQL injection. Again, you can have two cases there. You can have the function defined with auth id definer or that function defined with auth id current underscore user. If the function is defined by auth id definer and it is owned by a high privilege user such as sys, then what it means is that you are basically injecting PLSQL with sys privileges. So in simple words, it means it's game over, really. If you are injecting PLSQL with sys privileges then you are DBA already. Just execute OS code and it's not very difficult. If you are injecting PLSQL with auth id current user, then it means that now you can execute multiple statements. Although you are not executing those statements with the high privileges, but because you can execute multiple statements, then you can exploit vulnerable packages and again indirectly do privilege escalation and then again become DBA. And essentially, so using these two vectors, once you become DBA, then you can execute OS code. So Oracle has had this function, DBMS export extension, which has had a series of vulnerability in the past. In fact, the 2009 CPU, which was just released, had again addressed issues in this package. In particular, this package has a function called get domain index tables, which is vulnerable to PLSQL injection. Again, this is something which was fixed by Oracle in 2006, but because of the way Oracle patches work, it is still common to find databases which have this package as vulnerable still. So this function, this has the auth ID of definer. It is defined by sys. And it's vulnerable to PLSQL injection. So what it means is that you can now execute PLSQL as sys user. You don't really have to do anything more here. So just call this function in your SQL every time you find injection and select or insert or update query. Call this function pass the argument to this function whatever PLSQL you want to get executed, and that will execute with sys privileges. So this is a sample exploit. This was reported by David Litchfield. And it's actually mentioned in his book Oracle Hacker's Handbook as well. So we've declared this transaction as an autonomous transaction because we want to make it independent. This was fixed in 2006. There is yet another PLSQL injection in the same function, and this has been reported by Alexander Cornburst. But I'm not going to be talking about this. Maybe in the next DEFCON. And these are the vulnerable versions in which this will be affected. So I wrote up a small tool. It's called BSQLBF. There are n number of SQL injection tools out there. So I'm not going to talk about another SQL injection tool here. There are a few features of this tool, which I thought was quite unique. The latest version, version 2.3, is dedicated to Oracle. It basically contains this DBMS export extension exploit. So if you find a SQL injection, and if you find that the database, the application connects to the database with certain unprivileged user in connection string like Scott Tiger or whatever, then you can use this tool. It will do two things. It will do privilege escalation. So normally you can extract data in Oracle, but with privilege escalation, you can extract data with these privileges. So maybe you can extract the password hashes from Oracle. Then it does OS code execution. For OS code execution, again, it takes three subtypes. It can execute OS code with Java. If you are exploiting an Oracle application server express edition, XE, then Java will not be installed. So you can then rely on DBMS scheduler. Or if you are exploiting Oracle 9, then it also supports PLSQL native make utility. It also gives you read-write access, so you can read files from the backend database. I don't really have time to show you the demo for this. But the demo is available on my website, not-so-secure.com. And if you want to go through this. So going back to the original topic, the SQL injection worm, those of you who do a little bit of forensics must have seen this hex string in the logs. This was how the MSSQL worm looked like. All it would do is basically find the web applications which are vulnerable to SQL injection. Then this massive string will look into the database, do a massive update, so that the web frontend now changes. So the end users who now visit these websites, this website now contains some malicious javascripts. And these javascripts basically points to some browser exploits. So it didn't really target the server so much, but it targeted the end users. That was how it operated. So I basically wrote a simple proof of concept on how the similar thing can be achieved under Oracle. Again, it's not just about doing a massive update and hacking the end users. As we have already seen, you can execute OS code and hack the Oracle server itself. So at this stage the possibilities are really endless. So yeah, exactly. What could the worm do from here? We've already seen that we could execute OS code on the server. We could do something like what MSSQL worm did, you know, and do an update and then hack the end users. Again, if you can execute code, then maybe you could do something like a tftp getconflicker and infect the internal network. You could, I mean, there could be something which could hack only Oracle, you know, specifically Oracle. There are quite a few things which are out there, you know, and begging to get hacked actually. Oracle secured backup. There was a remote code execution. So you just send one URL on the internal network and that's it. You get a reverse shell back from it. There was a secure backup exploit. I think it's integrated with Metasploit as well. SQL injection in Oracle Enterprise Manager. TNS listener exploits. There are so many things that a worm could do from this stage. So let me just quickly go through this demo of a sample SQL injection worm. Okay, before I play the demo, just a little background about this demo. Over here we are seeing a blind SQL injection. I have not shown you the demo for BSQLBF, but if you see the demo of BSQLBF, then probably you will be able to correlate this better. Because over there I first used the same application to get OS code execution. But over here we are not going through that. So in this case it's a PHP application, which I think is connecting to the back end Oracle database server, 10G. It is connecting as user Scott Tiger. So we are doing a privilege escalation, becoming a DBA, then doing a massive update. And then that update basically means that the front end, you know, after the worm has infected this website, when I refresh this page, the front end will change and that will point to the autopone Metasploit, browser autopone Metasploit module. So in this video over here, there is a putty session over here in which the attacker is running the browser autopone. And when I launch the worm, the worm basically will open a new window in the website and target and point to the browser autopone. Again, this was based on the DBMS export extension exploit, and this was doing the update query, but as I said, you could do a number of things over here. So I'm setting up the browser autopone right now in this window. So that launches our browser autopone. Now I'll give this URL to the worm. So it basically takes the URL as an argument. Okay, so that's it. It sends this massive URL, which basically injects PLSQL with its privileges and does the massive update. Now if I go and refresh this browser, then you will see the HTML source code now has a JavaScript tag saying window.open and that location is pointing to the browser autopone. So I refresh the website, and now a new window has opened which basically points to the browser autopone. And the browser autopone plugin of Metasploit is now launching a series of exploits in the browser to hack the end user. And as you will see over here, now one of the exploits have worked, and we now have a metapeter session open. So if we attach the session, and this is the metapeter shell not from the server, but from the end users. Yeah. Yeah, that basically concludes it. Thank you for your time.