So hello everyone. So it's 5 o'clock now and I think it's nearly beer o'clock so hopefully this won't take long. My name is Sid and I will be presenting on hacking Oracle databases from web applications. Specifically I will be talking about some exploitation techniques for exploiting SQL injections in web applications against Oracle database. So SQL injection is nothing new. I mean if you don't, if you have not heard of SQL injection you probably are in a wrong room. So for last 20 years we've had exploitation techniques in SQL injections refined. We've got some modern state of art tools for exploiting SQL injections but they're mostly concentrated against MS SQL when it comes to Oracle there are not too many tools. So hopefully this talk will provide some answers to those questions. So a little bit about me. I work as a pen tester for a company in UK called 7safe. I'm not an Oracle geek so it's quite unusual to have an Oracle talk at DEF CON and not talk about zero days. So this particular talk is not focused on any particular zero days against Oracle databases but mainly focused on what exploits can be used from web applications to achieve certain things. So that's the agenda for today. So I presented this talk at Black Hat yesterday. I have reduced some slides so we will not be talking about extraction of data and if anybody is interested I highly recommend that you go through the original slides which I will be releasing on the DEF CON website. Towards the later half we'll talk about PCI compliance and some holes in PCI compliance. We'll look into that. So why am I talking on this topic? Because it's DEF CON and we love SQL injections. So there have been n number of talks on hacking Oracle databases but sadly most of the talks have been on interactive exploitation. That is if you can connect to the Oracle database if you are on the internal network then you can hack Oracle database pretty much. But all these talks have been on interactive exploitation and not so much from the application side of things. There are no free tools for hacking Oracle databases by exploiting SQL injection vulnerabilities. Even there are a few commercial tools which I know of which target Oracle database but the techniques which I found out used by these commercial tools are outdated so if you are exploiting say an 11G via SQL injection these tools may not work. Before we start I would like to thank David Litchfield for his pioneering work in the SQL injection. Most of this talk is inspired from his work. And we would also like to thank Alexander Kornbust and Farah Mavituna for helping us prepare this talk. Okay so those of you who are not aware of Oracle database, when you install an Oracle database it comes with a number of default packages. These default packages have considerably reduced in 11G. If you compare an 11G with an 8i maybe the number of default packages have considerably reduced but still there are quite a few. And these packages will have a number of procedures and functions. By default these procedures and functions runs with the privileges of Definer. So an easy way to understand this is to compare this with SUID files in Linux. Like in SUID files in Linux runs with Definer privileges. Similarly you have these procedures and functions by default running with Definer privileges. So to change that mode of execution from Definer to Invoker you must declare this keyword auth ID current underscore code user in the declaration of that particular procedure or function. So this is something which we all know about, how do we hack Oracle from internal networks. In a nutshell, if there is a SQL injection in a procedure owned by a SIS and public has execute privileges, then whatever you inject gets executed as SIS, so you can elevate your privileges, become SIS, and once you have SIS permissions, then you can execute OS code. So in a few steps, that's how you go about hacking Oracle database from internal network. You first enumerate SID, which is the database name. You then enumerate some common users, Scott Tiger, SIS change on install, some weak passwords. Once you have done those two steps, then you connect to the Oracle database. So you will be connecting with some unprivileged users, hopefully. Once you connect them, connect to the Oracle database. Then you find one such procedure. Which you can execute, which runs with the privileges of SIS, maybe, and has a SQL injection. That will let you become DBA, grant you DBA role on that user. And once you have DBA, then you can execute OS code. So all of this can be done via Metasploit, and this was shown by Chris in last year's DEF CON talk. For example, this was one such procedure which was available. It was available for public and was patched by Oracle, I think, a year ago. SIS.lt.mergeWorkspace was one such procedure. So you inject into it the string semicolon and Scott.dba is equal to I. So essentially, the function Scott.dba, which you created, then executes with the privileges of SIS. Hence, the user Scott end up with the DBA privileges. So it is important to know that Scott.dba must have auth ID, current underscore user defined. If you don't define this in your function, then again, Scott.dba will be executed with Scott, which you probably don't want. Okay, so that was hacking Oracle from networks, which has very well been documented. Now when it comes to exploiting SQL injections against Oracle database, it's important to understand that in Oracle, there are two classes of this work. So that's because Oracle has two coding languages. It has PL, SQL, and SQL. So PL, SQL is one coding language embedded in Oracle, which is really powerful. An easy way to understand PL, SQL is to consider this as a free-floating code between begin and end. So you have begin, end, and in between that, you can have n number of statements. So you can have procedure one, procedure two, and so on and so forth. Okay. But the second language, the SQL in Oracle, is actually quite limited. And the SQL in Oracle, by design, does not allow execution of multiple statements. And that is one thing which differentiates exploitation of SQL injections in Oracle against that of MSSQL. So like, for example, in MSSQL, if you have a SQL injection in a select statement, then you can end that statement and issue a next statement like exec XP underscore CMD shell or drop database. You can't do that in Oracle, because Oracle, by design, in SQL does not support execution of multiple statements. So what are the challenges in exploiting Oracle from web apps? Obviously, SQL in Oracle does not support multiple statements. OS code execution is not as simple as executing XP CMD shell. Not enough documentation and not enough tools available. So based on these two coding languages. In Oracle, you can have two classes of vulnerabilities. You can have a PL SQL injection vulnerability, or you can have a SQL injection vulnerability. So a PL SQL injection is when the user's input is used in a construction of an anonymous PL SQL block. And then that block gets dynamically executed. That would typically be a PL SQL injection. And if you find a PL SQL injection, then a PL SQL injection is nothing different from having an interactive access to the database. Because then you can run multiple statements. So then it's as, I mean, that is not different from having an interactive access. However, you can also have a SQL injection, which is an injection in a single SQL statement. In that case, there are a few restrictions. Like we said, there is no semicolon allowed, and you cannot use nested queries. So here is an example of a PL SQL injection. So this is one PHP code which connects to the Oracle database with a limited user privilege, a Scott user. And so this coder has obviously followed some best practices, and he's taking the parameter name, and he's binding that parameter into that SQL statement, which is passed on as an input to the procedure scott.test. So obviously, that particular statement, if you look at in $SQL, does not look like it's vulnerable to SQL injection or any such thing, because the parameter is bound to the query. However. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . But one must understand that there are limitations of using bind parameters. For a functionality such as this, you cannot use bind parameter. So obviously it does not mean that every time you write code it is vulnerable. You can use some sanitization functions. But you cannot use bind parameter for this particular functionality. So this particular example is vulnerable to PLSQL injection. So how do we exploit this? David Litchfield showed an exploit at BlackHat DC this year, which allows the user to just create session privileges to grant himself Java I.O. permissions. And once these Java I.O. permissions are obtained, then OS code execution is possible. This particular issue was fixed by Oracle in CPU of 2010, April. So this is how the exploit looks like. And you can use it from applications. Essentially. So if you look at our score.test procedure, I have passed the argument as null. So basically I'm saying the procedure to execute is null, which is okay for Oracle. And then the next statement is execute immediate. And that execute immediate has the entire exploit code, which will give the current user Java I.O. permissions. This was basically a logical flaw in the package DBMSJVM export permissions. I mean, this is talked in detail by David Litchfield. So I'll skip this. So that particular exploit would give your current user Java I.O. permissions. So that essentially is a privilege escalation. Once you have Java I.O. permissions, then you can call a function, which is available in the package DBMSJVM test. And the function is called funcall. This function takes as an argument a class. The second argument is the method within the class. And the other arguments are the arguments passed. To the method of the class. So there is by default one class in Oracle, which is the Aurora UTL wrapper. And this default class in Oracle has a main method. And that main method by default allows OS code execution. So if you have Java I.O. permissions, you can call this function and pass this argument. And this will basically result in OS code execution. So PL, SQL injection vulnerabilities are not very hard to are quite common, actually. The best place to look for these vulnerabilities is in the Oracle applications itself. So the code written by Oracle themselves. If you look at the history of Oracle application servers or Oracle application portals, starting from maybe 2001, 2002, there has been a series of PL, SQL injection vulnerabilities. I was going to talk about doing a privilege escalation via PL, SQL injections. But the fact is that most of the PL, SQL injections in Oracle products have been quite privileged, already. So talking about privilege escalation does not make sense because already the code executes with higher privileged users. Right. So now we move on to the main agenda for today, which is SQL injection. So this is SQL injection 101. Essentially that does not need any introduction. Unsanitized users input is used in SQL calls. So for example, something like this. If you inject colon R1 equal to 1. This will alter the logic of the SQL query, and this will return all the rows. So you can use SQL injection to compromise confidentiality, integrity, availability, blah, blah. I mean, this is right. So let's get started. Exploiting SQL injections. What do we do with exploitation of SQL injections? So when we say exploiting SQL injections, it can have different meaning to like the picture? All right. Okay. So exploiting SQL injections may mean different thing to different people. It could mean that somebody may be after sensitive information within the back end database. If your database contains credit card information, then for me, if I can get the credit card information, that is sufficient. I may not be interested in running OS code on the box. So extraction of data is typically one of the aims of SQL injection, but that is not the end goal of a SQL injection. A database may not have sensitive data, but it may be connected to the internal LAN, and there may be an attack vector by which if you compromise the database, you can pivot your attack and go on compromising the internal network. So extraction of data is actually something which has been documented quite well. And I will not be talking in this 50 minutes of slot. Essentially extraction of data can be divided into two categories. When the error messages are enabled. Then just like in MS SQL, you convert, you do typecast conversion errors and you extract the data within the error messages. You can do similar things in Oracle. When the error messages are disabled, you can call union queries. You can do blind injection. Then if the front end does not give you clues about blind injection, you might be able to call some time delay functions. And lastly, you can also do out of band channels. In Oracle. So all these exploitation techniques are fairly common between Oracle and MS SQL. And they're not very different. What I'm more interested in is privilege escalation and OS code execution. So this is actually an important point. So you found a SQL injection, and you are injecting some code via the SQL injection from the web application front end. Now depending on what, by what privileges your code, your injected query is going to get executed, I will classify a particular SQL injection as privileged or unprivileged. So for example, a privileged, a typical privileged SQL injection would be when whatever I am injecting, whatever SQL I am injecting, gets executed with DBA privileges. Now it could happen in a number of scenarios. For example, the application itself connects to the database with DBA privileges. Right? So it is not very common to see developers doing that. I mean, having said that, you often come across applications in ASP and ASPX where you see the application connects to the database with SA user or any user with sysadmin role. So it's not very common, but you still come across these. However, more importantly, you can see DBA privileges in your SQL injection when the SQL injection is in a procedure. And that procedure is owned by a high privileged user. So the application might connect to the database with Scott Tiger permissions, standard permissions. But the injection point is in a procedure and that procedure runs the high privileged user. So in that case, my SQL injection will be privileged, irrespective of the connection string. And obviously the second category would be the unprivileged SQL injection. For example, I mean, if you only have a typical Scott user privileges, that would be an unprivileged. That would be an unprivileged SQL injection. So what I mean by privilege escalation is, I know that during interactive exploitation, like if I can fire a razor SQL or sorry, any SQL client, SQL plus to the Oracle database, then I might be able to run a procedure which is vulnerable to SQL injection, grant myself DBA permissions, and then I can do stuff. That is a typical privilege escalation. Now those of you familiar with MS SQL more, you may recall MS SQL. It is still possible to do that. It is still possible to do privilege escalation by calling open row set. So I am after something similar in Oracle. And you should remember, I talked about that in SQL, Oracle does not support execution of multiple statements. So you cannot, if there is a SQL injection typically which exists in a select statement, then you are only restricted to select statement. So you cannot end that statement and call a procedure and inject into it. What you can do is call functions in the SQL. Because SQL supports functions, you can call functions within the SQL. So for exploiting SQL injections, we are typically looking for vulnerable functions. Any function which has any vulnerability typically can be used via exploiting SQL injections. So we are looking for functions which are executable by public and are vulnerable to either PL SQL injection or allow PL SQL execution as a feature. We will call the function in the SQL. And as an argument to that function, we pass PL SQL. There are a few such functions known, but the exploit is not publicly available. For example, this particular function, DBMS Java test, a function in this, this is the name of the package. A function exists within this package which had a buffer overflow vulnerability. So in your SQL, you can call this function and cause a buffer overflow because that function is executable by public. So out of those few known exploits, the one which is fairly popular is this particular function, get domain index tables. This function is vulnerable to PL SQL injection. Now I have the example of the exploit code in the next slide. But before we go there, so this function runs with the definer privileges. So irrespective of what privileges do you have, you can call this function and you can inject. And some argument within this function and whatever you inject, that will run as sys. So you don't really care about any privileges. And you can directly query the database with sys privileges. So it allowed privilege escalation and OS code execution from web apps. Public can execute this function. Okay, so this flaw is fairly old now. This was fixed in CPU of April 2006. And these are the vulnerable versions. And sadly, I mean, there are no more functions known. As I said. There have been advisories published which says these functions are vulnerable, but the exploit codes are not known. And this is one particular exploit which is very popular and all Oracle exploitation tools actually rely on this exploit. So this is how the exploit looks like. Essentially, if you look at this, then our injection string, which mainly is, I mean, we've injected quite a lot in it. But mainly that statement, grant DBA tool. The public will run as sys permissions. So this will just grant the DBA role onto the public user. And then you can carry out exploitation with DBA privileges. Right. Next we move on to the OS code execution. Again, we can have a SQL injection which is unprivileged or privileged. And again, if your SQL injection is unprivileged, you can use this particular function to execute OS code via PL, SQL. Again, this is the only function which is known by which with absolutely no privileges, you can do privilege escalation and execute OS code. For I mean, you can still execute OS code. But for that, you need some privileges. You just can't do it with the bare minimum create session user privileges. So the privileges which are required for OS code execution are either DBA privileges or Java IO privileges. I mean, so for example, in MS SQL, we know that you can execute OS code by calling XP underscore CMD shell. But to call XP underscore CMD shell, your user must be in the sys admin role. Only then he can call the XP underscore CMD shell procedure. So that is basically a feature. So likewise in Oracle, if you have these two permissions, then you can execute code. And I would not call this as a vulnerability. This is essentially a feature. But just a feature. Which is probably not very well described by Oracle for obvious reasons. So I mean, before we move on to the privilege part, this is again the same exploit which was fixed in 2006. So we execute OS code by injecting PL SQL within this function. A number of commercial tools like Pangolin and Core, which I've seen, they actually support this exploit. And that's the only exploit which they support. BSQLBF is one such tool, which is one tool which I support, which also has this exploit. And we will look at some more exploits later on. Right. So moving on, again, that was quite an old issue patched in 2006. If your connection string user or if your SQL injection is such that whatever code you are injecting, that particular user has Java IO permissions, then you can execute OS code on an Oracle database. You can do that in 10GR2, 11GR1, and 11GR2 by calling these two functions, runJava and funcall. Both these functions, they take an Oracle class as an argument and a method of their class which will allow OS code execution. So by default, this particular Oracle class, AuroraUtilWrapper, allows OS code execution within the main method. So this is how the exploit would look like. All you do is basically just call this function, dbmsjavatest.funcall. And as an argument, you pass the Oracle class and whatever code you want to execute. The only limitation is that you need Java IO permissions. And without Java IO permissions, you cannot call this class, you cannot execute this class. You have execute permissions on the function, but you do not have, you cannot execute that class with your, without Java IO permissions. Okay. So you can also execute code with DBA privileges. Now, so when we talk about DBA privileges, obviously DBA is a higher level privilege. And so the obvious question which arises is that DBA can grant himself Java IO permissions and then go back to that step which we showed earlier and execute OS code. So what's the point? The point is that I wanted to come up with something generic, so for example, even if in future Oracle... Oracle removes this Java class or makes it difficult for you, or revokes the execute permissions of the public user on that particular function, then we still want to execute OS code. So in Oracle 10G and 11G, there is a function called create worker process, sorry, create master process. This function executes arbitrary PL, SQL. This is not a flaw. This is a feature. You have to be careful with Oracle. So... So... Oracle executes PL, SQL as a feature. What we do is, as an argument, we pass on a PL, SQL, which essentially is a DBMS scheduler. And via DBMS scheduler, we can execute OS code. So this is how it looks like. It basically has a number of statements. First of all, we create a program. In that program, we basically tell Oracle that this program is to run this particular OS code with this particular argument. And then we create a job. Then we execute the job and drop the job. So this essentially will result in whatever code you want to execute. Okay. So you don't have to remember any of that, because it has already been scripted. And all you need to remember is the right switches to call. BSQLBF, as I said, is a free tool, which I support. And it supports advanced... It supports OS code execution by exploiting SQL injections against Oracle database. So... So for... I mean, what's new in BSQLBF is the new version, which I will be releasing now, it's already out there, supports execution of any Metasploits payload on the remote Oracle database. So I have a small video for you. Right. So here is a sample SQL injection. So... We're doing a blind SQL injection here. So if you do one is equal to one, then you see this particular string accounts appearing in the HTML page. If you do and one equal to two, then you will see that disappears. So it's a quite standard blind SQL injection, which we are looking at. Right. So the new version of BSQLBF actually comes with an executable. So if you go to the BSQLBF folder. So there is a generator.exe and a Perl script. What you do is you choose the Metasploit payload, which you want to execute on the remote Oracle database. So I mean, this is... So in this... For example, in this case, we choose a remote VNC shell. And we choose our IP address on which we want it, the database to throw us a reverse shell. This will run MSF payload, which we will then upload. Why our SQL injection on the remote database, and we compress it for better results. It supports two mode of exploitation, as I said, Java, IO and DBA. What privileges do you have? After you've finished it, it then tells you what to do next. So you have to launch BSQLBF then. And with what parameters you should launch it. That's basically a small manual page which comes up. So... Well, it basically tells you... It also has a clean-up mode. So if you are doing a pen test, you probably do not want to leave Metasploit payload onto a remote database and things like that. So hyphen CMD is equal to ref shell. That is a parameter which is new, which will execute Metasploit's payload. So you launch the Metasploit to capture your reverse shell. As you can see, my video editing techniques needs... Okay. I need some improvement. Right. So you launch BSQLBF. You provide the vulnerable URL. You provide the match string. In this case, it was accounts by which it identifies the true and false response. You provide the database. So it is three for Oracle. Type eight stands for that we have the DBA privileges. And ref shell stands for execute the Metasploit payload. Which we have already created. So if we now start our Metasploit listener. And let BSQLBF does its job. Then... So it will first check if we have the Java IO permissions. Sorry. It was Java IO and not DBA. Then it will upload the corresponding Metasploit's payload. I mean, depending on what payload you are executing, this can take some time. Metasploit 3.3, the payloads it creates are quite big. So it could take some time. Right. So when it's successful, it will drop you a VNC shell like that. So and Oracle in Windows runs a system. So this is basically complete on edge. This is fairly generic. So even if you come across an Oracle database. Running on a Solaris or an AIX machine. Then I mean, we are not reinventing the wheel. All you have to do is create the, generate the right Metasploit's payload. And send it on to the server. So this should work in all conditions. Right. Next I wanted to talk about a class of vulnerability. Which I feel is not... Very much discussed. And that is non-interactive second order injections. So what I mean by non-interactive second order injection is this. So if you imagine an e-commerce website or any website which allows new user registration functionality. So imagine an unauthenticated attacker attacking a functionality such as this. So he tries to inject something like evil functions into some arbitrary random fields. But the query which gets executed. On the server side is actually secure. So as you can see it is using bind parameters. So that particular field is not vulnerable. So all his attempts are going to be in vain. Because this is a safe code. So whatever he injects gets stored into a table. And because that particular code is not vulnerable. Now what happens if an admin user then goes and moderates this particular user. Which have been created with some crafted functionality. Sorry. Crafted strings. The code which gets executed when the admin user approves this functionality is vulnerable to SQL injection in this case. So whatever the malicious user has injected got stored into a row of the table. That particular value of the row of the table is then being treated as a string. And then passed on as an argument to var2 over here. So the functionality which admin user executes is vulnerable to SQL injection. This functionality is never seen by the admin. By the attacker. And the admin user is trusted. So for an admin screen, this is what is possibly going to happen in the admin session. But the bottom line is that the admin user is trusted. So even if the malicious user injected something like which will dump the database version or any sensitive data. That would be dumped on the admin user screen. And not on the attacker screen. So. So. So the attack call is a non-interactive second order injection. There is definitely a problem. There is definitely a SQL injection. But it does not occur within the attacker session. So example. Attacker places an order via an e-commerce application. Admin logs in and approves the order. The admin session is vulnerable to SQL injection. And although the attacker does not see it or does not have any method to interact with it. The attacker's input does get passed on to the vulnerable SQL calls. I mean, another example of this vulnerability could be that so the attacker's input which is stored in a row of a particular table gets acted upon by a trigger. Now that trigger can be called as a result of a bad job which runs every night. So what I'm saying is that the attacker has no way to identify this problem. And the injection does take input from the malicious user. Right. Other examples of this vulnerability is if you find a cross-site request forgery in the admin section. So for example, consider an admin section, an admin session's screen which is vulnerable to cross-site request forgery. And it either allows execution of arbitrary SQL as a feature or because admin screen is vulnerable to SQL injection. In either case, whatever attack payload you inject, that will be executed only once. So how can we make all these scenarios of non-interactive SQL injections interactive? Can we do something to regain control of the SQL injection which we don't actually see out there? So there is a concept of one-click on edge. The original concept was from Pharaoh. The idea is simple. We run MSF payload to create a binary. And so we need to make sure that we upload this binary. And execute this binary onto the database server. So we convert that binary into hex. If you convert the binary into hex, the size will be double. So we use some reduction functions. We write a VB script that can process this hex string and generate a valid binary file. So the idea is in one single request, we can get a reverse metasploit connection back to us. So shell.exe is generated by metasploit, metasploit's MSF payload. Then shell.exe is compressed and hex encoded with decrypters attached. So we create this VB script. When this VB script will be run, it will create the metasploits executable on the target server. And then we run the metasploits executable. So this VB script can be deployed using things like XP underscore CMD shell on the MS SQL server. So this is how it will look like. One long, massive string which will do the job for you. Usually when we talk about getting an interactive access, you will either upload via TFTP or you will upload bit by bit using debug.exe. So this solves that problem. And most importantly, you can just blindly use that as a first string and first into every field and get control of SQL injections. Which don't really happen in your session. So this was the original one click on edge from Ferro, which worked for MS SQL. So we did the same for Oracle. So if you have Java IO permissions, then you can, this is the attack string which you can use for Oracle. Obviously depending on what public IP you want to throw a reverse connection on, this hex will change. Next is, so when we wanted to do this with DBA privileges. Then. Things didn't quite work. So the obvious question was, again, we can grant as a DBA user, we can grant ourselves Java IO permissions. And then execute the previous step. We can do that. But the problem is, if you grant yourself Java IO permissions as DBA, then those permissions are not available in the same session. So technically you have to log out and log back in. And then you will have those permissions which you have granted. So technically speaking, it won't quite be one click on edge. It will require more than one or two. One, two, three clicks. You can do something like grant Java permissions to the public role. And then the permissions will be available in the same session. But if you are doing pen testing consultancy like I do, then granting something to public and the client will not be very happy. So okay. If you remember with DBA permissions, we were executing code by calling DBMS scheduler. Now the problem was that, so yeah. So when we passed on. When we ran DBMS scheduler, can we not pass the entire thing as an argument to DBMS scheduler? Well, it would have made my life very easy had that worked. But it didn't work. That's because DBMS schedulers create program procedure, which we are calling. It can only take up to 2000 characters as input. So that did not work. So okay. I think I'm sorry. The slides are the other way around. So what finally worked is we created a directory. Within the directory, we create a procedure to write files on the system using by calling utl underscore file. We execute the procedure, this procedure to write the VBScript, which we want to write. We execute the VBScript to create Metasploits payload, sorry, Metasploits executable on the target Oracle database. And then we run this executable all in one request. And that's how the request looks like. So we are doing quite a few things in one single request. Right. So let's see a demo of one click on edge. So there is already a tool, Web Raider. We just wrote a few plug-ins for Web Raider, which will let you do stuff in Oracle. So we again go back to our blind assembly. And we run the SQL injection. Obviously, I mean, in this particular example, we are using this technique for interactive injections, for exploiting interactive SQL injections. But the idea behind this is that you use this for non-interactive ones as well, which you don't really see. So what you do is you choose your public IP address on which you want to throw the reverse connection. You choose your attack, what attack you want to carry out. You want to carry out a SQL injection in Oracle. Or with DBA permissions. Or MS SQL, whatever. Once you save that, then, okay, this will, let me just pause this for a minute. Okay. So the idea behind this is that this will essentially give you the payload which you want to be injecting for non-interactive SQL injections. So it will give you a payload which is URL encoded. So if you are carrying out a SQL, if you are authenticated to the website and you have, I don't know, some CSRF token protection, so we didn't want to have cookies and everything added into our tool. So you can basically just inject, take this string and inject into the relevant get or post parameter and carry out the job. So it will give you a string which is URL encoded to carry out injection from web applications. Or it will give you the other one for interactive, like SQL parameters. Click plus connections. And then if it is a simple get request, you can just click on the start button and this will give you reverse shell, Metasploit's Metaprter shell in one click. There you go. So Metaprter session opened. Okay. Ooh. Okay. So moving on. Using this particular function, create master process, you can execute DML and DDL statements. So like you can do grant DBA to any particular user. If you can execute DDL and DML statements, then obviously you can write SQL injection worms. SQL injection worms rose to the scene almost two years ago. All they do was change the web applications front end to inject malicious JavaScripts within the iframe of the front end. And then distribute browser exploits. Same worms can be written for Oracle-based applications. For example, if SQL injection worm in MS SQL looks like this, then you can write a corresponding SQL injection worm in Oracle. So Oracle applications can be equally vulnerable. It's just that they have not been exploited in the wild. Right. So moving on. So is there anything that we could do to protect sensitive data in the database? Okay. So I mean, you can defend your applications against SQL injection, but what if you want to have some defense in depth mechanism to say that even there is a SQL injection, the data which the attacker gets should be pointless. So that's where I think compliance comes into picture. So PCI compliance mandates that the card data, the PAN, must be stored encrypted. The distribution of keys used for encryption decryption. Should be controlled and regulated. So what happens when an attacker finds a SQL injection vulnerability in such a website which follows all this compliance? Does that mean that the card data which the attacker gets is all encrypted, so it will be pointless for the attacker to have this data if, obviously, he can't get the keys for decryption? So for example, here is a sample from a particular credit card table. As you can see, the card data is all encrypted. So even if you were to find a SQL injection, the data which you have without the keys will be pretty much useless. Right. So what I found was, what happens in this regulation, does it talk about where the encryption occurs? Does it occur or should it occur at the database level or should it occur at the application level? So what if the encryption occurs at the database layer? So in this particular case, there is an application server. Okay. It's different from a database server. So you have a three-tier architecture. And so for storing credit card information, we are doing an insert statement. And while inserting, we are calling a built-in Oracle function, which is a DBMS obfuscation toolkit dot triple-desk encryption. And this desk three encrypt function takes the card number as one argument and takes the encryption key as a second argument. Okay. So this is actually quite interesting. So this makes me think that what is more important here, the data or the query? Although the data which is stored in the database would be encrypted using this mechanism, but the query which will be executed on the database server will actually have the clear text span and the clear text string in it. So at any point in time, whenever that query executes on the server, if I can get a log of that SQL query, then I am pretty much done. If I can obtain that query, then I don't really care about the data, because that query will have the clear text span, and more importantly, it will have the clear text string. So I am not interested in data. I am interested in the query. So the SQL queries executed on the database server, they can be forensically obtained. V$SQL is one table in Oracle which lists statistics on shared SQL area. This table takes the data. It typically stores last 500 queries. So if you repeatedly keep querying this table, you will find out all queries which have been executed on the database server. Because it's a dynamic view, sometimes the content of this table, sorry, of this view gets written to this particular table, WRH$SQLTEXT, and in that case, these queries are actually permanently logged within the database. Similarly in MS SQL, you can obtain these queries for forensically by calling the plan cache. So this is how the query will look like when it will be on the server. As you can see, the clear text pan and the clear text string would be present in the SQL query when it runs on the server. So if you query SQL underscore text column from V$SQL, then you will find the clear text pan and the clear text string, more importantly. So these are some screenshots which say these are the strings. Similarly, you can obtain plan cache in MS SQL. here is an example of somebody MD5 hashing a credit card information within the database. So if you look at the bottom half of the screen, the data in the database is MD5 hashed and it's safe. But if you obtain the plan cache, then you will find that the plan cache contains the clear text PAN number. So that to me is a fail. Right. So when we talk about SQL injections, we talk about some really complicated hacks and sometimes we miss out on some easier versions. So we were investigating a SQL injection, a breach from SQL injection, and when I found this particular tech technique, which I thought was quite neat. So let me actually, I've only got five minutes, so it's best to run the demo before this man throws me out of here. Okay. So stealing unencrypted card data from the database when it's not stored as clear text. So we have our e-commerce shopping application. Any resemblance to any application is purely coincidental. Right. So you click on, you choose a page, you click on buy now. It follows PCI compliance, so it goes over a secure page. You log in, supply your credentials, go to the, select the item, enter your card number, do your details, and basically just buy the product. Right. So what happens if in such an application you find a SQL injection? As we saw earlier, that this particular application has a different application server and a different database server. So even if you compromise the database server, you will not have the key because the key is stored in the application server. You can use the attack vector which I mentioned earlier, but to query V$SQL table, you must have DBA privileges. So what if you don't, like in this example, then you cannot obtain the queries forensically, so you are pretty much struck. So we find a blind SQL injection in this application. So we do some and one equal to one and one equal to two. Right. And then we run some tools to exploit SQL injection. So we run the SQLBF to exploit this particular vulnerability. And we find, we do some basic exploitation. So we find, we are running queries with Scott user, which is a really limited user. So we can't really query V$SQL to obtain the queries forensically. We find out what the version is. Maybe we can do, can we do privilege escalation? And it's actually an Oracle 11g 1.0.6. So, I mean, we don't really know of any privilege escalation vectors against this database. So it looks fairly secure. Now, we find a card number table from credit card table, sorry, and if we extract the data, then we find the data is actually encrypted. So unless we have the private key, sorry, the decryption key, the data is virtually useless to me. So this is how the white box side things look like. The application server connects to the database server, which is a different IP. It's using the Oracle's triple desk and encryption for encrypting strings. The string is stored in the application server. I can possibly compromise the database server, but that will mean that I still don't have the private key, sorry, the decryption key. Right. Next, what the attacker does is he logs on to the server and he observes something. What he observes is that when he logs in on that payment page, he sees his full name displayed on the screen. So this particular data, his first name or his first name, last name, is actually a session data coming from the database. And that will be true for every single user who logs on to the application. So what if he can poison this data for every single user to contain a JavaScript key logger? So he does that. He runs some massive update statements to alter the data. So he logs in to the database and contains a malicious JavaScript on that web front end. So he issues a series of three commands, one after another. Okay. The looks are changing, so I'll just rush. Okay. So once he does that, and then now the victim logs in, then you will see that the data coming from the database, the session data, has now changed. And it now contains a JavaScript, which points to the beef. So what we are doing over here is we are not hacking the data within the database. We are hacking the data which is about to be written to the database before it touches the database. So when an end user logs in, this JavaScript will connect back to the beef server. So if you configure your beef server, then you will find the zombies connecting back to the beef server. So here we go. The zombies are connected. And then if you go back to the victim screen, then what you will find is, okay. Yeah. And then if he enters his details again onto that page, then you will find all that is available via our JavaScript key logger, and it is sent out to a beef server. So who really cares about the data in the database if you can obtain it like that? Okay. So I've been asked to leave the stage. That pretty much is the end of the presentation.