1 00:00:00,000 --> 00:00:06,900 So hello everyone. So it's 5 o'clock now and I think it's nearly beer o'clock so hopefully this 2 00:00:06,900 --> 00:00:12,300 won't take long. My name is Sid and I will be presenting on hacking Oracle databases from 3 00:00:12,300 --> 00:00:18,960 web applications. Specifically I will be talking about some exploitation techniques for 4 00:00:18,960 --> 00:00:24,900 exploiting SQL injections in web applications against Oracle database. So SQL injection is 5 00:00:24,900 --> 00:00:30,180 nothing new. I mean if you don't, if you have not heard of SQL injection you probably are in a 6 00:00:30,180 --> 00:00:37,100 wrong room. So for last 20 years we've had exploitation techniques in SQL injections 7 00:00:37,100 --> 00:00:44,320 refined. We've got some modern state of art tools for exploiting SQL injections but they're 8 00:00:44,320 --> 00:00:50,400 mostly concentrated against MS SQL when it comes to Oracle there are not too many tools. So 9 00:00:50,400 --> 00:00:54,280 hopefully this talk will provide some answers to those questions. 10 00:00:55,280 --> 00:01:02,640 So a little bit about me. I work as a pen tester for a company in UK called 7safe. I'm not an 11 00:01:02,640 --> 00:01:09,500 Oracle geek so it's quite unusual to have an Oracle talk at DEF CON and not talk about zero 12 00:01:09,500 --> 00:01:15,420 days. So this particular talk is not focused on any particular zero days against Oracle 13 00:01:15,420 --> 00:01:23,060 databases but mainly focused on what exploits can be used from web applications to achieve 14 00:01:23,060 --> 00:01:24,080 certain things. 15 00:01:25,620 --> 00:01:35,400 So that's the agenda for today. So I presented this talk at Black Hat yesterday. I have reduced 16 00:01:35,400 --> 00:01:40,700 some slides so we will not be talking about extraction of data and if anybody is interested I 17 00:01:40,700 --> 00:01:45,100 highly recommend that you go through the original slides which I will be releasing on the DEF CON 18 00:01:45,100 --> 00:01:52,440 website. Towards the later half we'll talk about PCI compliance and some holes in PCI compliance. 19 00:01:53,420 --> 00:01:54,580 We'll look into that. 20 00:01:55,120 --> 00:02:04,160 So why am I talking on this topic? Because it's DEF CON and we love SQL injections. So there have been 21 00:02:04,160 --> 00:02:11,400 n number of talks on hacking Oracle databases but sadly most of the talks have been on interactive 22 00:02:11,400 --> 00:02:17,700 exploitation. That is if you can connect to the Oracle database if you are on the internal network 23 00:02:18,200 --> 00:02:24,340 then you can hack Oracle database pretty much. But all these talks have been on interactive 24 00:02:24,900 --> 00:02:30,020 exploitation and not so much from the application side of things. There are no free tools for hacking 25 00:02:30,020 --> 00:02:35,660 Oracle databases by exploiting SQL injection vulnerabilities. Even there are a few commercial tools 26 00:02:35,660 --> 00:02:41,880 which I know of which target Oracle database but the techniques which I found out used by these 27 00:02:41,880 --> 00:02:47,700 commercial tools are outdated so if you are exploiting say an 11G via SQL injection these tools may 28 00:02:47,700 --> 00:02:48,060 not work. 29 00:02:49,900 --> 00:02:54,880 Before we start I would like to thank David Litchfield for his pioneering work in the SQL injection. 30 00:02:54,900 --> 00:03:02,160 Most of this talk is inspired from his work. And we would also like to thank Alexander Kornbust and 31 00:03:02,160 --> 00:03:04,360 Farah Mavituna for helping us prepare this talk. 32 00:03:05,720 --> 00:03:11,440 Okay so those of you who are not aware of Oracle database, when you install an Oracle database it comes 33 00:03:11,440 --> 00:03:18,480 with a number of default packages. These default packages have considerably reduced in 11G. If you 34 00:03:18,480 --> 00:03:24,360 compare an 11G with an 8i maybe the number of default packages have considerably reduced but still there 35 00:03:24,360 --> 00:03:24,880 are quite a few. 36 00:03:24,900 --> 00:03:31,500 And these packages will have a number of procedures and functions. By default these procedures and 37 00:03:31,500 --> 00:03:37,320 functions runs with the privileges of Definer. So an easy way to understand this is to compare this 38 00:03:37,320 --> 00:03:43,780 with SUID files in Linux. Like in SUID files in Linux runs with Definer privileges. Similarly you 39 00:03:43,780 --> 00:03:49,100 have these procedures and functions by default running with Definer privileges. So to change that 40 00:03:49,100 --> 00:03:54,640 mode of execution from Definer to Invoker you must declare this keyword auth ID current underscore 41 00:03:54,640 --> 00:03:58,320 code user in the declaration of that particular procedure or function. 42 00:04:00,340 --> 00:04:05,860 So this is something which we all know about, how do we hack Oracle from internal networks. 43 00:04:06,400 --> 00:04:12,120 In a nutshell, if there is a SQL injection in a procedure owned by a SIS and public has 44 00:04:12,120 --> 00:04:17,960 execute privileges, then whatever you inject gets executed as SIS, so you can elevate your 45 00:04:17,960 --> 00:04:22,280 privileges, become SIS, and once you have SIS permissions, then you can execute OS code. 46 00:04:24,080 --> 00:04:29,040 So in a few steps, that's how you go about hacking Oracle database from internal network. 47 00:04:29,360 --> 00:04:31,700 You first enumerate SID, which is the database name. 48 00:04:32,400 --> 00:04:38,560 You then enumerate some common users, Scott Tiger, SIS change on install, some weak passwords. 49 00:04:39,640 --> 00:04:42,980 Once you have done those two steps, then you connect to the Oracle database. 50 00:04:43,920 --> 00:04:46,740 So you will be connecting with some unprivileged users, hopefully. 51 00:04:47,760 --> 00:04:50,020 Once you connect them, connect to the Oracle database. 52 00:04:50,240 --> 00:04:52,220 Then you find one such procedure. 53 00:04:52,220 --> 00:04:58,340 Which you can execute, which runs with the privileges of SIS, maybe, and has a SQL injection. 54 00:04:58,660 --> 00:05:04,000 That will let you become DBA, grant you DBA role on that user. 55 00:05:04,220 --> 00:05:06,980 And once you have DBA, then you can execute OS code. 56 00:05:07,360 --> 00:05:15,400 So all of this can be done via Metasploit, and this was shown by Chris in last year's 57 00:05:15,400 --> 00:05:15,980 DEF CON talk. 58 00:05:17,860 --> 00:05:21,720 For example, this was one such procedure which was available. 59 00:05:21,720 --> 00:05:25,360 It was available for public and was patched by Oracle, I think, a year ago. 60 00:05:26,780 --> 00:05:29,400 SIS.lt.mergeWorkspace was one such procedure. 61 00:05:29,800 --> 00:05:35,440 So you inject into it the string semicolon and Scott.dba is equal to I. 62 00:05:35,680 --> 00:05:40,960 So essentially, the function Scott.dba, which you created, then executes with the privileges 63 00:05:40,960 --> 00:05:41,600 of SIS. 64 00:05:42,700 --> 00:05:46,880 Hence, the user Scott end up with the DBA privileges. 65 00:05:46,880 --> 00:05:51,920 So it is important to know that Scott.dba must have auth ID, current underscore user 66 00:05:51,920 --> 00:05:52,220 defined. 67 00:05:52,460 --> 00:05:56,600 If you don't define this in your function, then again, Scott.dba will be executed with 68 00:05:56,600 --> 00:05:58,080 Scott, which you probably don't want. 69 00:05:59,900 --> 00:06:05,860 Okay, so that was hacking Oracle from networks, which has very well been documented. 70 00:06:06,760 --> 00:06:12,720 Now when it comes to exploiting SQL injections against Oracle database, it's important to 71 00:06:12,720 --> 00:06:16,860 understand that in Oracle, there are two classes of this work. 72 00:06:16,860 --> 00:06:22,200 So that's because Oracle has two coding languages. 73 00:06:22,440 --> 00:06:23,840 It has PL, SQL, and SQL. 74 00:06:25,200 --> 00:06:29,480 So PL, SQL is one coding language embedded in Oracle, which is really powerful. 75 00:06:30,380 --> 00:06:35,940 An easy way to understand PL, SQL is to consider this as a free-floating code between begin 76 00:06:35,940 --> 00:06:36,340 and end. 77 00:06:36,480 --> 00:06:40,540 So you have begin, end, and in between that, you can have n number of statements. 78 00:06:40,820 --> 00:06:44,380 So you can have procedure one, procedure two, and so on and so forth. 79 00:06:45,180 --> 00:06:45,680 Okay. 80 00:06:46,860 --> 00:06:50,200 But the second language, the SQL in Oracle, is actually quite limited. 81 00:06:51,260 --> 00:06:57,060 And the SQL in Oracle, by design, does not allow execution of multiple statements. 82 00:06:57,400 --> 00:07:02,780 And that is one thing which differentiates exploitation of SQL injections in Oracle against 83 00:07:02,780 --> 00:07:04,920 that of MSSQL. 84 00:07:05,260 --> 00:07:10,200 So like, for example, in MSSQL, if you have a SQL injection in a select statement, then 85 00:07:10,200 --> 00:07:14,700 you can end that statement and issue a next statement like exec XP underscore CMD shell 86 00:07:14,700 --> 00:07:15,640 or drop database. 87 00:07:15,640 --> 00:07:22,200 You can't do that in Oracle, because Oracle, by design, in SQL does not support execution 88 00:07:22,200 --> 00:07:23,380 of multiple statements. 89 00:07:24,700 --> 00:07:28,460 So what are the challenges in exploiting Oracle from web apps? 90 00:07:28,760 --> 00:07:32,160 Obviously, SQL in Oracle does not support multiple statements. 91 00:07:32,620 --> 00:07:36,340 OS code execution is not as simple as executing XP CMD shell. 92 00:07:37,080 --> 00:07:39,640 Not enough documentation and not enough tools available. 93 00:07:41,660 --> 00:07:44,420 So based on these two coding languages. 94 00:07:44,420 --> 00:07:48,220 In Oracle, you can have two classes of vulnerabilities. 95 00:07:48,620 --> 00:07:52,640 You can have a PL SQL injection vulnerability, or you can have a SQL injection vulnerability. 96 00:07:53,360 --> 00:07:59,820 So a PL SQL injection is when the user's input is used in a construction of an anonymous 97 00:07:59,820 --> 00:08:00,740 PL SQL block. 98 00:08:01,040 --> 00:08:03,320 And then that block gets dynamically executed. 99 00:08:03,580 --> 00:08:05,440 That would typically be a PL SQL injection. 100 00:08:05,800 --> 00:08:10,820 And if you find a PL SQL injection, then a PL SQL injection is nothing different from 101 00:08:10,820 --> 00:08:12,820 having an interactive access to the database. 102 00:08:12,940 --> 00:08:14,400 Because then you can run multiple statements. 103 00:08:14,400 --> 00:08:19,360 So then it's as, I mean, that is not different from having an interactive access. 104 00:08:20,380 --> 00:08:24,480 However, you can also have a SQL injection, which is an injection in a single SQL statement. 105 00:08:24,780 --> 00:08:26,580 In that case, there are a few restrictions. 106 00:08:26,840 --> 00:08:31,480 Like we said, there is no semicolon allowed, and you cannot use nested queries. 107 00:08:33,080 --> 00:08:35,660 So here is an example of a PL SQL injection. 108 00:08:37,160 --> 00:08:42,460 So this is one PHP code which connects to the Oracle database with a limited user privilege, 109 00:08:42,600 --> 00:08:43,540 a Scott user. 110 00:08:44,580 --> 00:08:49,560 And so this coder has obviously followed some best practices, and he's taking the 111 00:08:49,560 --> 00:08:55,720 parameter name, and he's binding that parameter into that SQL statement, which is passed on 112 00:08:55,720 --> 00:08:59,020 as an input to the procedure scott.test. 113 00:09:00,380 --> 00:09:05,480 So obviously, that particular statement, if you look at in $SQL, does not look like it's 114 00:09:05,480 --> 00:09:10,060 vulnerable to SQL injection or any such thing, because the parameter is bound to the query. 115 00:09:11,240 --> 00:09:11,720 However. 116 00:09:11,920 --> 00:09:14,380 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 00:09:23,140 --> 00:09:41,520 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 00:09:41,520 --> 00:09:44,640 But one must understand that there are limitations of using bind parameters. 119 00:09:44,900 --> 00:09:47,500 For a functionality such as this, you cannot use bind parameter. 120 00:09:48,500 --> 00:09:52,680 So obviously it does not mean that every time you write code it is vulnerable. 121 00:09:52,980 --> 00:09:54,400 You can use some sanitization functions. 122 00:09:55,460 --> 00:09:59,000 But you cannot use bind parameter for this particular functionality. 123 00:09:59,740 --> 00:10:02,840 So this particular example is vulnerable to PLSQL injection. 124 00:10:04,740 --> 00:10:06,360 So how do we exploit this? 125 00:10:06,360 --> 00:10:12,740 David Litchfield showed an exploit at BlackHat DC this year, which allows the user to just 126 00:10:12,740 --> 00:10:16,120 create session privileges to grant himself Java I.O. permissions. 127 00:10:17,380 --> 00:10:23,440 And once these Java I.O. permissions are obtained, then OS code execution is possible. 128 00:10:24,180 --> 00:10:29,720 This particular issue was fixed by Oracle in CPU of 2010, April. 129 00:10:30,670 --> 00:10:32,300 So this is how the exploit looks like. 130 00:10:32,400 --> 00:10:34,320 And you can use it from applications. 131 00:10:34,740 --> 00:10:35,280 Essentially. 132 00:10:35,280 --> 00:10:39,580 So if you look at our score.test procedure, I have passed the argument as null. 133 00:10:39,840 --> 00:10:45,820 So basically I'm saying the procedure to execute is null, which is okay for Oracle. 134 00:10:46,020 --> 00:10:48,140 And then the next statement is execute immediate. 135 00:10:48,600 --> 00:10:52,820 And that execute immediate has the entire exploit code, which will give the current 136 00:10:52,820 --> 00:10:54,520 user Java I.O. permissions. 137 00:10:55,040 --> 00:11:00,760 This was basically a logical flaw in the package DBMSJVM export permissions. 138 00:11:01,640 --> 00:11:04,860 I mean, this is talked in detail by David Litchfield. 139 00:11:04,860 --> 00:11:05,880 So I'll skip this. 140 00:11:07,120 --> 00:11:11,460 So that particular exploit would give your current user Java I.O. permissions. 141 00:11:11,760 --> 00:11:14,060 So that essentially is a privilege escalation. 142 00:11:14,740 --> 00:11:20,000 Once you have Java I.O. permissions, then you can call a function, which is available 143 00:11:20,000 --> 00:11:22,220 in the package DBMSJVM test. 144 00:11:22,500 --> 00:11:23,920 And the function is called funcall. 145 00:11:24,340 --> 00:11:28,080 This function takes as an argument a class. 146 00:11:28,820 --> 00:11:31,380 The second argument is the method within the class. 147 00:11:31,560 --> 00:11:34,520 And the other arguments are the arguments passed. 148 00:11:34,860 --> 00:11:36,600 To the method of the class. 149 00:11:36,860 --> 00:11:42,600 So there is by default one class in Oracle, which is the Aurora UTL wrapper. 150 00:11:43,140 --> 00:11:46,460 And this default class in Oracle has a main method. 151 00:11:46,620 --> 00:11:49,860 And that main method by default allows OS code execution. 152 00:11:50,360 --> 00:11:54,180 So if you have Java I.O. permissions, you can call this function and pass this argument. 153 00:11:54,480 --> 00:11:56,620 And this will basically result in OS code execution. 154 00:11:58,100 --> 00:12:03,900 So PL, SQL injection vulnerabilities are not very hard to are quite common, actually. 155 00:12:04,860 --> 00:12:08,860 The best place to look for these vulnerabilities is in the Oracle applications itself. 156 00:12:09,160 --> 00:12:10,600 So the code written by Oracle themselves. 157 00:12:11,980 --> 00:12:17,320 If you look at the history of Oracle application servers or Oracle application portals, starting 158 00:12:17,320 --> 00:12:22,180 from maybe 2001, 2002, there has been a series of PL, SQL injection vulnerabilities. 159 00:12:23,100 --> 00:12:28,640 I was going to talk about doing a privilege escalation via PL, SQL injections. 160 00:12:29,000 --> 00:12:34,360 But the fact is that most of the PL, SQL injections in Oracle products have been quite privileged, 161 00:12:34,360 --> 00:12:34,660 already. 162 00:12:34,880 --> 00:12:39,580 So talking about privilege escalation does not make sense because already the code executes 163 00:12:39,580 --> 00:12:40,640 with higher privileged users. 164 00:12:41,940 --> 00:12:42,440 Right. 165 00:12:42,700 --> 00:12:46,480 So now we move on to the main agenda for today, which is SQL injection. 166 00:12:47,200 --> 00:12:53,120 So this is SQL injection 101. 167 00:12:53,920 --> 00:12:55,800 Essentially that does not need any introduction. 168 00:12:56,500 --> 00:12:59,620 Unsanitized users input is used in SQL calls. 169 00:13:00,260 --> 00:13:02,000 So for example, something like this. 170 00:13:02,100 --> 00:13:03,980 If you inject colon R1 equal to 1. 171 00:13:04,360 --> 00:13:10,780 This will alter the logic of the SQL query, and this will return all the rows. 172 00:13:10,980 --> 00:13:15,920 So you can use SQL injection to compromise confidentiality, integrity, availability, 173 00:13:16,460 --> 00:13:17,180 blah, blah. 174 00:13:17,460 --> 00:13:19,780 I mean, this is right. 175 00:13:20,120 --> 00:13:21,560 So let's get started. 176 00:13:21,740 --> 00:13:22,820 Exploiting SQL injections. 177 00:13:23,140 --> 00:13:25,700 What do we do with exploitation of SQL injections? 178 00:13:27,360 --> 00:13:32,260 So when we say exploiting SQL injections, it can have different meaning to like the 179 00:13:32,260 --> 00:13:32,480 picture? 180 00:13:32,920 --> 00:13:33,640 All right. 181 00:13:36,640 --> 00:13:37,080 Okay. 182 00:13:37,180 --> 00:13:40,560 So exploiting SQL injections may mean different thing to different people. 183 00:13:40,980 --> 00:13:45,560 It could mean that somebody may be after sensitive information within the back end database. 184 00:13:45,680 --> 00:13:50,420 If your database contains credit card information, then for me, if I can get the credit card 185 00:13:50,420 --> 00:13:52,000 information, that is sufficient. 186 00:13:52,180 --> 00:13:55,040 I may not be interested in running OS code on the box. 187 00:13:56,020 --> 00:14:01,320 So extraction of data is typically one of the aims of SQL injection, but that is not 188 00:14:01,320 --> 00:14:03,600 the end goal of a SQL injection. 189 00:14:03,600 --> 00:14:09,120 A database may not have sensitive data, but it may be connected to the internal LAN, and 190 00:14:09,120 --> 00:14:13,640 there may be an attack vector by which if you compromise the database, you can pivot 191 00:14:13,640 --> 00:14:16,260 your attack and go on compromising the internal network. 192 00:14:17,740 --> 00:14:23,260 So extraction of data is actually something which has been documented quite well. 193 00:14:23,380 --> 00:14:26,740 And I will not be talking in this 50 minutes of slot. 194 00:14:28,440 --> 00:14:31,660 Essentially extraction of data can be divided into two categories. 195 00:14:31,900 --> 00:14:33,440 When the error messages are enabled. 196 00:14:33,440 --> 00:14:39,400 Then just like in MS SQL, you convert, you do typecast conversion errors and you extract 197 00:14:39,840 --> 00:14:41,740 the data within the error messages. 198 00:14:41,940 --> 00:14:43,660 You can do similar things in Oracle. 199 00:14:44,620 --> 00:14:47,560 When the error messages are disabled, you can call union queries. 200 00:14:47,860 --> 00:14:49,400 You can do blind injection. 201 00:14:52,080 --> 00:14:58,100 Then if the front end does not give you clues about blind injection, you might be able to 202 00:14:58,100 --> 00:14:59,500 call some time delay functions. 203 00:15:00,160 --> 00:15:03,160 And lastly, you can also do out of band channels. 204 00:15:03,440 --> 00:15:03,680 In Oracle. 205 00:15:03,880 --> 00:15:08,380 So all these exploitation techniques are fairly common between Oracle and MS SQL. 206 00:15:08,620 --> 00:15:09,780 And they're not very different. 207 00:15:10,900 --> 00:15:14,960 What I'm more interested in is privilege escalation and OS code execution. 208 00:15:17,240 --> 00:15:20,180 So this is actually an important point. 209 00:15:20,560 --> 00:15:25,480 So you found a SQL injection, and you are injecting some code via the SQL injection 210 00:15:25,960 --> 00:15:27,900 from the web application front end. 211 00:15:28,140 --> 00:15:33,420 Now depending on what, by what privileges your code, your injected query is going to 212 00:15:33,440 --> 00:15:39,120 get executed, I will classify a particular SQL injection as privileged or unprivileged. 213 00:15:39,680 --> 00:15:49,060 So for example, a privileged, a typical privileged SQL injection would be when whatever I am 214 00:15:49,060 --> 00:15:52,780 injecting, whatever SQL I am injecting, gets executed with DBA privileges. 215 00:15:53,360 --> 00:15:57,440 Now it could happen in a number of scenarios. 216 00:15:57,660 --> 00:16:02,100 For example, the application itself connects to the database with DBA privileges. 217 00:16:02,280 --> 00:16:02,800 Right? 218 00:16:03,440 --> 00:16:06,240 So it is not very common to see developers doing that. 219 00:16:06,620 --> 00:16:12,360 I mean, having said that, you often come across applications in ASP and ASPX where 220 00:16:12,360 --> 00:16:18,100 you see the application connects to the database with SA user or any user with sysadmin role. 221 00:16:18,340 --> 00:16:20,900 So it's not very common, but you still come across these. 222 00:16:21,360 --> 00:16:30,880 However, more importantly, you can see DBA privileges in your SQL injection when the 223 00:16:30,880 --> 00:16:32,280 SQL injection is in a procedure. 224 00:16:32,280 --> 00:16:34,820 And that procedure is owned by a high privileged user. 225 00:16:35,000 --> 00:16:40,140 So the application might connect to the database with Scott Tiger permissions, standard permissions. 226 00:16:40,500 --> 00:16:45,340 But the injection point is in a procedure and that procedure runs the high privileged 227 00:16:45,340 --> 00:16:45,640 user. 228 00:16:45,880 --> 00:16:50,240 So in that case, my SQL injection will be privileged, irrespective of the connection 229 00:16:50,240 --> 00:16:50,520 string. 230 00:16:51,320 --> 00:16:54,960 And obviously the second category would be the unprivileged SQL injection. 231 00:16:55,700 --> 00:17:02,260 For example, I mean, if you only have a typical Scott user privileges, that would be an unprivileged. 232 00:17:02,280 --> 00:17:02,820 That would be an unprivileged SQL injection. 233 00:17:04,600 --> 00:17:10,460 So what I mean by privilege escalation is, I know that during interactive exploitation, 234 00:17:10,840 --> 00:17:16,020 like if I can fire a razor SQL or sorry, any SQL client, SQL plus to the Oracle database, 235 00:17:16,880 --> 00:17:21,800 then I might be able to run a procedure which is vulnerable to SQL injection, grant myself 236 00:17:21,800 --> 00:17:23,940 DBA permissions, and then I can do stuff. 237 00:17:24,200 --> 00:17:25,780 That is a typical privilege escalation. 238 00:17:26,180 --> 00:17:30,560 Now those of you familiar with MS SQL more, you may recall MS SQL. 239 00:17:30,860 --> 00:17:32,260 It is still possible to do that. 240 00:17:32,260 --> 00:17:34,200 It is still possible to do privilege escalation by calling open row set. 241 00:17:35,680 --> 00:17:38,320 So I am after something similar in Oracle. 242 00:17:39,440 --> 00:17:43,560 And you should remember, I talked about that in SQL, Oracle does not support execution 243 00:17:43,560 --> 00:17:44,480 of multiple statements. 244 00:17:45,380 --> 00:17:49,980 So you cannot, if there is a SQL injection typically which exists in a select statement, 245 00:17:50,600 --> 00:17:52,940 then you are only restricted to select statement. 246 00:17:53,920 --> 00:17:58,900 So you cannot end that statement and call a procedure and inject into it. 247 00:17:59,140 --> 00:18:02,060 What you can do is call functions in the SQL. 248 00:18:02,060 --> 00:18:05,560 Because SQL supports functions, you can call functions within the SQL. 249 00:18:06,840 --> 00:18:11,320 So for exploiting SQL injections, we are typically looking for vulnerable functions. 250 00:18:11,760 --> 00:18:19,840 Any function which has any vulnerability typically can be used via exploiting SQL injections. 251 00:18:20,460 --> 00:18:25,960 So we are looking for functions which are executable by public and are vulnerable to 252 00:18:25,960 --> 00:18:30,460 either PL SQL injection or allow PL SQL execution as a feature. 253 00:18:32,060 --> 00:18:33,860 We will call the function in the SQL. 254 00:18:34,160 --> 00:18:37,480 And as an argument to that function, we pass PL SQL. 255 00:18:39,140 --> 00:18:43,900 There are a few such functions known, but the exploit is not publicly available. 256 00:18:44,300 --> 00:18:51,420 For example, this particular function, DBMS Java test, a function in this, this is the 257 00:18:51,420 --> 00:18:51,980 name of the package. 258 00:18:52,220 --> 00:18:56,000 A function exists within this package which had a buffer overflow vulnerability. 259 00:18:56,420 --> 00:19:00,900 So in your SQL, you can call this function and cause a buffer overflow because that function 260 00:19:00,900 --> 00:19:01,960 is executable by public. 261 00:19:03,100 --> 00:19:09,680 So out of those few known exploits, the one which is fairly popular is this particular 262 00:19:09,680 --> 00:19:12,180 function, get domain index tables. 263 00:19:13,160 --> 00:19:15,880 This function is vulnerable to PL SQL injection. 264 00:19:17,460 --> 00:19:20,060 Now I have the example of the exploit code in the next slide. 265 00:19:20,200 --> 00:19:24,800 But before we go there, so this function runs with the definer privileges. 266 00:19:25,020 --> 00:19:31,120 So irrespective of what privileges do you have, you can call this function and you can 267 00:19:31,120 --> 00:19:31,480 inject. 268 00:19:32,060 --> 00:19:35,760 And some argument within this function and whatever you inject, that will run as sys. 269 00:19:35,840 --> 00:19:37,940 So you don't really care about any privileges. 270 00:19:38,360 --> 00:19:41,240 And you can directly query the database with sys privileges. 271 00:19:42,400 --> 00:19:46,100 So it allowed privilege escalation and OS code execution from web apps. 272 00:19:47,100 --> 00:19:48,500 Public can execute this function. 273 00:19:49,240 --> 00:19:51,420 Okay, so this flaw is fairly old now. 274 00:19:51,640 --> 00:19:53,980 This was fixed in CPU of April 2006. 275 00:19:54,640 --> 00:19:56,240 And these are the vulnerable versions. 276 00:19:57,260 --> 00:20:00,500 And sadly, I mean, there are no more functions known. 277 00:20:00,580 --> 00:20:01,020 As I said. 278 00:20:01,020 --> 00:20:05,660 There have been advisories published which says these functions are vulnerable, but 279 00:20:05,660 --> 00:20:06,780 the exploit codes are not known. 280 00:20:07,060 --> 00:20:14,760 And this is one particular exploit which is very popular and all Oracle exploitation tools 281 00:20:14,760 --> 00:20:16,180 actually rely on this exploit. 282 00:20:17,060 --> 00:20:18,800 So this is how the exploit looks like. 283 00:20:19,280 --> 00:20:25,980 Essentially, if you look at this, then our injection string, which mainly is, I mean, 284 00:20:26,540 --> 00:20:27,820 we've injected quite a lot in it. 285 00:20:27,880 --> 00:20:31,000 But mainly that statement, grant DBA tool. 286 00:20:31,020 --> 00:20:34,880 The public will run as sys permissions. 287 00:20:35,140 --> 00:20:39,340 So this will just grant the DBA role onto the public user. 288 00:20:39,580 --> 00:20:42,260 And then you can carry out exploitation with DBA privileges. 289 00:20:45,760 --> 00:20:46,320 Right. 290 00:20:46,420 --> 00:20:48,600 Next we move on to the OS code execution. 291 00:20:49,180 --> 00:20:53,560 Again, we can have a SQL injection which is unprivileged or privileged. 292 00:20:54,700 --> 00:21:00,200 And again, if your SQL injection is unprivileged, you can use this particular function to execute 293 00:21:00,200 --> 00:21:01,400 OS code via PL, SQL. 294 00:21:03,040 --> 00:21:07,520 Again, this is the only function which is known by which with absolutely no privileges, 295 00:21:07,720 --> 00:21:10,340 you can do privilege escalation and execute OS code. 296 00:21:11,080 --> 00:21:13,820 For I mean, you can still execute OS code. 297 00:21:14,460 --> 00:21:17,240 But for that, you need some privileges. 298 00:21:17,400 --> 00:21:21,200 You just can't do it with the bare minimum create session user privileges. 299 00:21:22,240 --> 00:21:27,860 So the privileges which are required for OS code execution are either DBA privileges or 300 00:21:27,860 --> 00:21:29,080 Java IO privileges. 301 00:21:30,360 --> 00:21:36,340 I mean, so for example, in MS SQL, we know that you can execute OS code by calling XP 302 00:21:36,340 --> 00:21:37,160 underscore CMD shell. 303 00:21:37,600 --> 00:21:42,860 But to call XP underscore CMD shell, your user must be in the sys admin role. 304 00:21:43,380 --> 00:21:46,340 Only then he can call the XP underscore CMD shell procedure. 305 00:21:46,620 --> 00:21:48,280 So that is basically a feature. 306 00:21:48,500 --> 00:21:54,340 So likewise in Oracle, if you have these two permissions, then you can execute code. 307 00:21:54,540 --> 00:21:56,880 And I would not call this as a vulnerability. 308 00:21:57,060 --> 00:21:58,520 This is essentially a feature. 309 00:21:58,900 --> 00:22:00,080 But just a feature. 310 00:22:00,200 --> 00:22:04,120 Which is probably not very well described by Oracle for obvious reasons. 311 00:22:06,820 --> 00:22:12,460 So I mean, before we move on to the privilege part, this is again the same exploit which 312 00:22:12,460 --> 00:22:14,520 was fixed in 2006. 313 00:22:16,060 --> 00:22:20,860 So we execute OS code by injecting PL SQL within this function. 314 00:22:21,300 --> 00:22:26,620 A number of commercial tools like Pangolin and Core, which I've seen, they actually support 315 00:22:26,620 --> 00:22:27,040 this exploit. 316 00:22:27,240 --> 00:22:28,800 And that's the only exploit which they support. 317 00:22:30,260 --> 00:22:35,920 BSQLBF is one such tool, which is one tool which I support, which also has this exploit. 318 00:22:36,140 --> 00:22:38,660 And we will look at some more exploits later on. 319 00:22:39,840 --> 00:22:40,360 Right. 320 00:22:40,760 --> 00:22:45,760 So moving on, again, that was quite an old issue patched in 2006. 321 00:22:47,060 --> 00:22:53,000 If your connection string user or if your SQL injection is such that whatever code you 322 00:22:53,000 --> 00:22:58,260 are injecting, that particular user has Java IO permissions, then you can execute OS code 323 00:22:58,800 --> 00:22:59,580 on an Oracle database. 324 00:23:00,300 --> 00:23:08,760 You can do that in 10GR2, 11GR1, and 11GR2 by calling these two functions, runJava and 325 00:23:08,760 --> 00:23:09,100 funcall. 326 00:23:10,360 --> 00:23:15,760 Both these functions, they take an Oracle class as an argument and a method of their 327 00:23:15,760 --> 00:23:17,320 class which will allow OS code execution. 328 00:23:18,220 --> 00:23:25,540 So by default, this particular Oracle class, AuroraUtilWrapper, allows OS code execution 329 00:23:26,260 --> 00:23:27,440 within the main method. 330 00:23:29,240 --> 00:23:31,200 So this is how the exploit would look like. 331 00:23:31,360 --> 00:23:35,380 All you do is basically just call this function, dbmsjavatest.funcall. 332 00:23:36,200 --> 00:23:40,600 And as an argument, you pass the Oracle class and whatever code you want to execute. 333 00:23:41,880 --> 00:23:45,200 The only limitation is that you need Java IO permissions. 334 00:23:45,560 --> 00:23:50,040 And without Java IO permissions, you cannot call this class, you cannot execute this class. 335 00:23:50,260 --> 00:23:54,360 You have execute permissions on the function, but you do not have, you cannot execute that 336 00:23:54,360 --> 00:23:56,980 class with your, without Java IO permissions. 337 00:23:57,260 --> 00:23:57,440 Okay. 338 00:23:59,460 --> 00:24:03,140 So you can also execute code with DBA privileges. 339 00:24:03,580 --> 00:24:09,340 Now, so when we talk about DBA privileges, obviously DBA is a higher level privilege. 340 00:24:09,820 --> 00:24:15,460 And so the obvious question which arises is that DBA can grant himself Java IO permissions 341 00:24:15,460 --> 00:24:20,880 and then go back to that step which we showed earlier and execute OS code. 342 00:24:21,140 --> 00:24:22,260 So what's the point? 343 00:24:22,440 --> 00:24:27,400 The point is that I wanted to come up with something generic, so for example, even if 344 00:24:27,400 --> 00:24:28,380 in future Oracle... 345 00:24:29,100 --> 00:24:35,060 Oracle removes this Java class or makes it difficult for you, or revokes the execute 346 00:24:35,060 --> 00:24:40,300 permissions of the public user on that particular function, then we still want to execute OS 347 00:24:40,300 --> 00:24:40,500 code. 348 00:24:41,640 --> 00:24:47,200 So in Oracle 10G and 11G, there is a function called create worker process, sorry, create 349 00:24:47,200 --> 00:24:47,680 master process. 350 00:24:48,300 --> 00:24:51,080 This function executes arbitrary PL, SQL. 351 00:24:51,860 --> 00:24:52,860 This is not a flaw. 352 00:24:53,000 --> 00:24:53,540 This is a feature. 353 00:24:54,100 --> 00:24:55,460 You have to be careful with Oracle. 354 00:24:56,160 --> 00:24:56,660 So... 355 00:24:57,220 --> 00:24:57,720 So... 356 00:24:58,800 --> 00:24:59,820 Oracle executes PL, SQL as a feature. 357 00:24:59,980 --> 00:25:06,400 What we do is, as an argument, we pass on a PL, SQL, which essentially is a DBMS scheduler. 358 00:25:06,440 --> 00:25:09,120 And via DBMS scheduler, we can execute OS code. 359 00:25:10,360 --> 00:25:11,720 So this is how it looks like. 360 00:25:13,360 --> 00:25:15,460 It basically has a number of statements. 361 00:25:16,060 --> 00:25:17,660 First of all, we create a program. 362 00:25:17,900 --> 00:25:23,340 In that program, we basically tell Oracle that this program is to run this particular OS 363 00:25:23,340 --> 00:25:25,120 code with this particular argument. 364 00:25:25,640 --> 00:25:27,100 And then we create a job. 365 00:25:27,100 --> 00:25:30,100 Then we execute the job and drop the job. 366 00:25:30,260 --> 00:25:33,440 So this essentially will result in whatever code you want to execute. 367 00:25:34,120 --> 00:25:34,600 Okay. 368 00:25:34,660 --> 00:25:39,400 So you don't have to remember any of that, because it has already been scripted. 369 00:25:39,480 --> 00:25:42,020 And all you need to remember is the right switches to call. 370 00:25:43,140 --> 00:25:46,740 BSQLBF, as I said, is a free tool, which I support. 371 00:25:47,120 --> 00:25:49,460 And it supports advanced... 372 00:25:50,500 --> 00:25:55,580 It supports OS code execution by exploiting SQL injections against Oracle database. 373 00:25:56,600 --> 00:25:57,080 So... 374 00:25:57,100 --> 00:25:58,140 So for... 375 00:25:59,080 --> 00:26:04,140 I mean, what's new in BSQLBF is the new version, which I will be releasing now, it's already 376 00:26:04,140 --> 00:26:09,160 out there, supports execution of any Metasploits payload on the remote Oracle database. 377 00:26:10,460 --> 00:26:13,540 So I have a small video for you. 378 00:26:21,660 --> 00:26:22,180 Right. 379 00:26:22,300 --> 00:26:25,700 So here is a sample SQL injection. 380 00:26:26,060 --> 00:26:26,580 So... 381 00:26:27,100 --> 00:26:28,520 We're doing a blind SQL injection here. 382 00:26:28,720 --> 00:26:34,180 So if you do one is equal to one, then you see this particular string accounts appearing 383 00:26:34,180 --> 00:26:35,020 in the HTML page. 384 00:26:35,220 --> 00:26:39,020 If you do and one equal to two, then you will see that disappears. 385 00:26:39,400 --> 00:26:44,500 So it's a quite standard blind SQL injection, which we are looking at. 386 00:26:48,920 --> 00:26:49,340 Right. 387 00:26:49,440 --> 00:26:52,360 So the new version of BSQLBF actually comes with an executable. 388 00:26:54,240 --> 00:26:56,120 So if you go to the BSQLBF folder. 389 00:26:56,120 --> 00:26:59,500 So there is a generator.exe and a Perl script. 390 00:27:00,740 --> 00:27:07,460 What you do is you choose the Metasploit payload, which you want to execute on the remote Oracle 391 00:27:07,460 --> 00:27:07,820 database. 392 00:27:08,500 --> 00:27:10,140 So I mean, this is... 393 00:27:11,440 --> 00:27:12,680 So in this... 394 00:27:12,680 --> 00:27:16,220 For example, in this case, we choose a remote VNC shell. 395 00:27:17,140 --> 00:27:22,380 And we choose our IP address on which we want it, the database to throw us a reverse shell. 396 00:27:22,980 --> 00:27:26,060 This will run MSF payload, which we will then upload. 397 00:27:26,060 --> 00:27:32,700 Why our SQL injection on the remote database, and we compress it for better results. 398 00:27:33,480 --> 00:27:37,220 It supports two mode of exploitation, as I said, Java, IO and DBA. 399 00:27:38,800 --> 00:27:40,320 What privileges do you have? 400 00:27:40,440 --> 00:27:43,300 After you've finished it, it then tells you what to do next. 401 00:27:43,620 --> 00:27:46,060 So you have to launch BSQLBF then. 402 00:27:46,220 --> 00:27:48,600 And with what parameters you should launch it. 403 00:27:48,880 --> 00:27:51,980 That's basically a small manual page which comes up. 404 00:27:54,040 --> 00:27:54,560 So... 405 00:27:55,820 --> 00:27:57,640 Well, it basically tells you... 406 00:27:57,640 --> 00:27:58,900 It also has a clean-up mode. 407 00:27:59,020 --> 00:28:03,360 So if you are doing a pen test, you probably do not want to leave Metasploit payload onto 408 00:28:03,360 --> 00:28:05,600 a remote database and things like that. 409 00:28:06,180 --> 00:28:07,980 So hyphen CMD is equal to ref shell. 410 00:28:08,080 --> 00:28:13,520 That is a parameter which is new, which will execute Metasploit's payload. 411 00:28:14,420 --> 00:28:19,080 So you launch the Metasploit to capture your reverse shell. 412 00:28:20,920 --> 00:28:23,400 As you can see, my video editing techniques needs... 413 00:28:24,140 --> 00:28:24,540 Okay. 414 00:28:24,560 --> 00:28:25,020 I need some improvement. 415 00:28:28,740 --> 00:28:29,260 Right. 416 00:28:32,040 --> 00:28:34,060 So you launch BSQLBF. 417 00:28:34,180 --> 00:28:35,680 You provide the vulnerable URL. 418 00:28:37,940 --> 00:28:39,740 You provide the match string. 419 00:28:39,920 --> 00:28:43,620 In this case, it was accounts by which it identifies the true and false response. 420 00:28:44,140 --> 00:28:45,300 You provide the database. 421 00:28:45,540 --> 00:28:46,560 So it is three for Oracle. 422 00:28:47,360 --> 00:28:50,640 Type eight stands for that we have the DBA privileges. 423 00:28:51,180 --> 00:28:54,480 And ref shell stands for execute the Metasploit payload. 424 00:28:54,560 --> 00:28:55,920 Which we have already created. 425 00:28:57,220 --> 00:29:00,280 So if we now start our Metasploit listener. 426 00:29:02,060 --> 00:29:05,480 And let BSQLBF does its job. 427 00:29:06,380 --> 00:29:06,840 Then... 428 00:29:06,840 --> 00:29:09,280 So it will first check if we have the Java IO permissions. 429 00:29:09,580 --> 00:29:09,920 Sorry. 430 00:29:09,980 --> 00:29:11,420 It was Java IO and not DBA. 431 00:29:12,320 --> 00:29:16,060 Then it will upload the corresponding Metasploit's payload. 432 00:29:19,280 --> 00:29:23,640 I mean, depending on what payload you are executing, this can take some time. 433 00:29:24,340 --> 00:29:27,340 Metasploit 3.3, the payloads it creates are quite big. 434 00:29:27,560 --> 00:29:29,680 So it could take some time. 435 00:29:30,300 --> 00:29:30,680 Right. 436 00:29:30,780 --> 00:29:34,260 So when it's successful, it will drop you a VNC shell like that. 437 00:29:35,840 --> 00:29:38,840 So and Oracle in Windows runs a system. 438 00:29:39,020 --> 00:29:40,720 So this is basically complete on edge. 439 00:29:49,720 --> 00:29:51,460 This is fairly generic. 440 00:29:51,600 --> 00:29:53,380 So even if you come across an Oracle database. 441 00:29:53,380 --> 00:29:56,060 Running on a Solaris or an AIX machine. 442 00:29:56,600 --> 00:29:59,940 Then I mean, we are not reinventing the wheel. 443 00:30:00,100 --> 00:30:03,560 All you have to do is create the, generate the right Metasploit's payload. 444 00:30:03,800 --> 00:30:04,980 And send it on to the server. 445 00:30:05,340 --> 00:30:07,200 So this should work in all conditions. 446 00:30:17,060 --> 00:30:17,540 Right. 447 00:30:17,960 --> 00:30:20,820 Next I wanted to talk about a class of vulnerability. 448 00:30:21,080 --> 00:30:23,000 Which I feel is not... 449 00:30:23,000 --> 00:30:24,460 Very much discussed. 450 00:30:24,640 --> 00:30:27,000 And that is non-interactive second order injections. 451 00:30:27,800 --> 00:30:31,180 So what I mean by non-interactive second order injection is this. 452 00:30:31,420 --> 00:30:38,700 So if you imagine an e-commerce website or any website which allows new user registration 453 00:30:38,700 --> 00:30:39,140 functionality. 454 00:30:39,660 --> 00:30:45,060 So imagine an unauthenticated attacker attacking a functionality such as this. 455 00:30:45,300 --> 00:30:50,220 So he tries to inject something like evil functions into some arbitrary random fields. 456 00:30:50,420 --> 00:30:52,780 But the query which gets executed. 457 00:30:53,000 --> 00:30:54,620 On the server side is actually secure. 458 00:30:55,020 --> 00:30:58,200 So as you can see it is using bind parameters. 459 00:30:58,440 --> 00:31:00,400 So that particular field is not vulnerable. 460 00:31:00,780 --> 00:31:02,920 So all his attempts are going to be in vain. 461 00:31:03,060 --> 00:31:04,200 Because this is a safe code. 462 00:31:05,280 --> 00:31:08,260 So whatever he injects gets stored into a table. 463 00:31:08,560 --> 00:31:11,840 And because that particular code is not vulnerable. 464 00:31:12,620 --> 00:31:16,820 Now what happens if an admin user then goes and moderates this particular user. 465 00:31:17,060 --> 00:31:19,760 Which have been created with some crafted functionality. 466 00:31:20,300 --> 00:31:20,820 Sorry. 467 00:31:20,900 --> 00:31:21,760 Crafted strings. 468 00:31:23,000 --> 00:31:27,460 The code which gets executed when the admin user approves this functionality is vulnerable 469 00:31:27,460 --> 00:31:29,080 to SQL injection in this case. 470 00:31:31,860 --> 00:31:37,180 So whatever the malicious user has injected got stored into a row of the table. 471 00:31:37,460 --> 00:31:42,540 That particular value of the row of the table is then being treated as a string. 472 00:31:42,720 --> 00:31:46,280 And then passed on as an argument to var2 over here. 473 00:31:46,560 --> 00:31:49,880 So the functionality which admin user executes is vulnerable to SQL injection. 474 00:31:50,200 --> 00:31:52,980 This functionality is never seen by the admin. 475 00:31:52,980 --> 00:31:53,720 By the attacker. 476 00:31:54,480 --> 00:31:56,260 And the admin user is trusted. 477 00:31:57,940 --> 00:32:04,840 So for an admin screen, this is what is possibly going to happen in the admin session. 478 00:32:05,600 --> 00:32:08,260 But the bottom line is that the admin user is trusted. 479 00:32:08,480 --> 00:32:14,460 So even if the malicious user injected something like which will dump the database version 480 00:32:14,460 --> 00:32:15,580 or any sensitive data. 481 00:32:15,720 --> 00:32:18,200 That would be dumped on the admin user screen. 482 00:32:18,400 --> 00:32:20,120 And not on the attacker screen. 483 00:32:21,620 --> 00:32:22,140 So. 484 00:32:22,200 --> 00:32:22,500 So. 485 00:32:22,980 --> 00:32:25,000 So the attack call is a non-interactive second order injection. 486 00:32:26,060 --> 00:32:27,780 There is definitely a problem. 487 00:32:27,900 --> 00:32:29,440 There is definitely a SQL injection. 488 00:32:29,960 --> 00:32:32,180 But it does not occur within the attacker session. 489 00:32:33,300 --> 00:32:34,100 So example. 490 00:32:34,240 --> 00:32:36,720 Attacker places an order via an e-commerce application. 491 00:32:37,500 --> 00:32:38,960 Admin logs in and approves the order. 492 00:32:39,440 --> 00:32:41,560 The admin session is vulnerable to SQL injection. 493 00:32:42,640 --> 00:32:46,820 And although the attacker does not see it or does not have any method to interact with 494 00:32:46,820 --> 00:32:47,000 it. 495 00:32:47,100 --> 00:32:50,900 The attacker's input does get passed on to the vulnerable SQL calls. 496 00:32:52,980 --> 00:32:58,740 I mean, another example of this vulnerability could be that so the attacker's input which 497 00:32:58,740 --> 00:33:04,820 is stored in a row of a particular table gets acted upon by a trigger. 498 00:33:05,180 --> 00:33:10,880 Now that trigger can be called as a result of a bad job which runs every night. 499 00:33:11,260 --> 00:33:15,280 So what I'm saying is that the attacker has no way to identify this problem. 500 00:33:16,220 --> 00:33:21,560 And the injection does take input from the malicious user. 501 00:33:22,980 --> 00:33:23,480 Right. 502 00:33:23,860 --> 00:33:28,220 Other examples of this vulnerability is if you find a cross-site request forgery in 503 00:33:28,220 --> 00:33:28,780 the admin section. 504 00:33:29,560 --> 00:33:36,060 So for example, consider an admin section, an admin session's screen which is vulnerable 505 00:33:36,060 --> 00:33:37,380 to cross-site request forgery. 506 00:33:37,560 --> 00:33:44,640 And it either allows execution of arbitrary SQL as a feature or because admin screen is 507 00:33:44,640 --> 00:33:45,820 vulnerable to SQL injection. 508 00:33:46,680 --> 00:33:52,480 In either case, whatever attack payload you inject, that will be executed only once. 509 00:33:53,760 --> 00:33:58,620 So how can we make all these scenarios of non-interactive SQL injections interactive? 510 00:33:59,060 --> 00:34:04,980 Can we do something to regain control of the SQL injection which we don't actually see 511 00:34:05,780 --> 00:34:06,260 out there? 512 00:34:06,640 --> 00:34:08,880 So there is a concept of one-click on edge. 513 00:34:08,980 --> 00:34:10,660 The original concept was from Pharaoh. 514 00:34:12,040 --> 00:34:13,220 The idea is simple. 515 00:34:13,900 --> 00:34:17,980 We run MSF payload to create a binary. 516 00:34:18,360 --> 00:34:22,280 And so we need to make sure that we upload this binary. 517 00:34:22,280 --> 00:34:25,200 And execute this binary onto the database server. 518 00:34:26,960 --> 00:34:29,120 So we convert that binary into hex. 519 00:34:29,520 --> 00:34:33,340 If you convert the binary into hex, the size will be double. 520 00:34:33,700 --> 00:34:35,540 So we use some reduction functions. 521 00:34:35,960 --> 00:34:41,540 We write a VB script that can process this hex string and generate a valid binary file. 522 00:34:42,720 --> 00:34:49,680 So the idea is in one single request, we can get a reverse metasploit connection back 523 00:34:49,680 --> 00:34:50,000 to us. 524 00:34:52,280 --> 00:34:57,440 So shell.exe is generated by metasploit, metasploit's MSF payload. 525 00:34:57,940 --> 00:35:03,500 Then shell.exe is compressed and hex encoded with decrypters attached. 526 00:35:04,100 --> 00:35:05,520 So we create this VB script. 527 00:35:05,800 --> 00:35:10,120 When this VB script will be run, it will create the metasploits executable on the target server. 528 00:35:10,360 --> 00:35:12,620 And then we run the metasploits executable. 529 00:35:15,580 --> 00:35:22,080 So this VB script can be deployed using things like XP underscore CMD shell on the MS SQL 530 00:35:22,080 --> 00:35:22,260 server. 531 00:35:26,780 --> 00:35:28,980 So this is how it will look like. 532 00:35:29,080 --> 00:35:32,040 One long, massive string which will do the job for you. 533 00:35:32,840 --> 00:35:38,680 Usually when we talk about getting an interactive access, you will either upload via TFTP or 534 00:35:38,680 --> 00:35:41,640 you will upload bit by bit using debug.exe. 535 00:35:41,780 --> 00:35:43,720 So this solves that problem. 536 00:35:44,200 --> 00:35:48,640 And most importantly, you can just blindly use that as a first string and first into 537 00:35:48,640 --> 00:35:51,920 every field and get control of SQL injections. 538 00:35:51,920 --> 00:35:53,800 Which don't really happen in your session. 539 00:35:54,360 --> 00:35:59,320 So this was the original one click on edge from Ferro, which worked for MS SQL. 540 00:35:59,960 --> 00:36:01,740 So we did the same for Oracle. 541 00:36:02,640 --> 00:36:08,580 So if you have Java IO permissions, then you can, this is the attack string which you can 542 00:36:08,580 --> 00:36:09,300 use for Oracle. 543 00:36:09,520 --> 00:36:13,800 Obviously depending on what public IP you want to throw a reverse connection on, this 544 00:36:13,800 --> 00:36:14,560 hex will change. 545 00:36:17,060 --> 00:36:20,900 Next is, so when we wanted to do this with DBA privileges. 546 00:36:21,020 --> 00:36:21,380 Then. 547 00:36:21,920 --> 00:36:22,680 Things didn't quite work. 548 00:36:23,500 --> 00:36:28,280 So the obvious question was, again, we can grant as a DBA user, we can grant ourselves 549 00:36:28,280 --> 00:36:29,440 Java IO permissions. 550 00:36:29,680 --> 00:36:31,780 And then execute the previous step. 551 00:36:32,740 --> 00:36:33,960 We can do that. 552 00:36:34,140 --> 00:36:39,060 But the problem is, if you grant yourself Java IO permissions as DBA, then those permissions 553 00:36:39,060 --> 00:36:40,880 are not available in the same session. 554 00:36:41,300 --> 00:36:43,520 So technically you have to log out and log back in. 555 00:36:43,720 --> 00:36:46,220 And then you will have those permissions which you have granted. 556 00:36:46,540 --> 00:36:49,680 So technically speaking, it won't quite be one click on edge. 557 00:36:49,840 --> 00:36:51,900 It will require more than one or two. 558 00:36:51,920 --> 00:36:52,480 One, two, three clicks. 559 00:36:53,440 --> 00:36:57,020 You can do something like grant Java permissions to the public role. 560 00:36:57,240 --> 00:36:59,720 And then the permissions will be available in the same session. 561 00:37:00,200 --> 00:37:04,840 But if you are doing pen testing consultancy like I do, then granting something to public 562 00:37:04,840 --> 00:37:06,300 and the client will not be very happy. 563 00:37:09,200 --> 00:37:10,080 So okay. 564 00:37:10,140 --> 00:37:14,560 If you remember with DBA permissions, we were executing code by calling DBMS scheduler. 565 00:37:15,020 --> 00:37:18,740 Now the problem was that, so yeah. 566 00:37:18,820 --> 00:37:20,340 So when we passed on. 567 00:37:20,340 --> 00:37:25,700 When we ran DBMS scheduler, can we not pass the entire thing as an argument to DBMS scheduler? 568 00:37:26,560 --> 00:37:32,060 Well, it would have made my life very easy had that worked. 569 00:37:32,280 --> 00:37:32,980 But it didn't work. 570 00:37:33,240 --> 00:37:38,000 That's because DBMS schedulers create program procedure, which we are calling. 571 00:37:38,180 --> 00:37:41,000 It can only take up to 2000 characters as input. 572 00:37:41,860 --> 00:37:43,060 So that did not work. 573 00:37:45,520 --> 00:37:46,480 So okay. 574 00:37:46,740 --> 00:37:48,440 I think I'm sorry. 575 00:37:48,600 --> 00:37:50,120 The slides are the other way around. 576 00:37:50,340 --> 00:37:53,940 So what finally worked is we created a directory. 577 00:37:54,820 --> 00:38:00,880 Within the directory, we create a procedure to write files on the system using by calling 578 00:38:00,880 --> 00:38:01,840 utl underscore file. 579 00:38:02,580 --> 00:38:07,980 We execute the procedure, this procedure to write the VBScript, which we want to write. 580 00:38:08,580 --> 00:38:13,660 We execute the VBScript to create Metasploits payload, sorry, Metasploits executable on 581 00:38:13,660 --> 00:38:14,620 the target Oracle database. 582 00:38:14,920 --> 00:38:17,780 And then we run this executable all in one request. 583 00:38:17,940 --> 00:38:19,840 And that's how the request looks like. 584 00:38:20,340 --> 00:38:23,180 So we are doing quite a few things in one single request. 585 00:38:25,800 --> 00:38:26,240 Right. 586 00:38:26,320 --> 00:38:29,040 So let's see a demo of one click on edge. 587 00:38:29,520 --> 00:38:31,780 So there is already a tool, Web Raider. 588 00:38:31,940 --> 00:38:36,920 We just wrote a few plug-ins for Web Raider, which will let you do stuff in Oracle. 589 00:38:47,400 --> 00:38:50,320 So we again go back to our blind assembly. 590 00:38:50,340 --> 00:38:50,680 And we run the SQL injection. 591 00:38:51,000 --> 00:38:55,420 Obviously, I mean, in this particular example, we are using this technique for interactive 592 00:38:55,420 --> 00:38:58,260 injections, for exploiting interactive SQL injections. 593 00:38:58,600 --> 00:39:04,720 But the idea behind this is that you use this for non-interactive ones as well, which you 594 00:39:04,720 --> 00:39:05,360 don't really see. 595 00:39:06,820 --> 00:39:11,100 So what you do is you choose your public IP address on which you want to throw the reverse 596 00:39:11,100 --> 00:39:11,420 connection. 597 00:39:11,780 --> 00:39:15,840 You choose your attack, what attack you want to carry out. 598 00:39:15,920 --> 00:39:18,220 You want to carry out a SQL injection in Oracle. 599 00:39:18,360 --> 00:39:19,580 Or with DBA permissions. 600 00:39:19,580 --> 00:39:21,080 Or MS SQL, whatever. 601 00:39:22,340 --> 00:39:33,700 Once you save that, then, okay, this will, let me just pause this for a minute. 602 00:39:34,300 --> 00:39:34,780 Okay. 603 00:39:34,980 --> 00:39:41,660 So the idea behind this is that this will essentially give you the payload which you 604 00:39:41,660 --> 00:39:45,340 want to be injecting for non-interactive SQL injections. 605 00:39:45,700 --> 00:39:48,100 So it will give you a payload which is URL encoded. 606 00:39:48,100 --> 00:39:54,480 So if you are carrying out a SQL, if you are authenticated to the website and you have, 607 00:39:54,640 --> 00:39:59,400 I don't know, some CSRF token protection, so we didn't want to have cookies and everything 608 00:39:59,400 --> 00:40:00,300 added into our tool. 609 00:40:00,460 --> 00:40:04,940 So you can basically just inject, take this string and inject into the relevant get or 610 00:40:04,940 --> 00:40:06,960 post parameter and carry out the job. 611 00:40:08,640 --> 00:40:13,560 So it will give you a string which is URL encoded to carry out injection from web applications. 612 00:40:13,800 --> 00:40:18,080 Or it will give you the other one for interactive, like SQL parameters. 613 00:40:18,100 --> 00:40:18,760 Click plus connections. 614 00:40:19,680 --> 00:40:24,080 And then if it is a simple get request, you can just click on the start button and this 615 00:40:24,080 --> 00:40:29,420 will give you reverse shell, Metasploit's Metaprter shell in one click. 616 00:40:30,740 --> 00:40:31,400 There you go. 617 00:40:35,360 --> 00:40:36,880 So Metaprter session opened. 618 00:40:40,020 --> 00:40:40,540 Okay. 619 00:40:45,000 --> 00:40:45,520 Ooh. 620 00:40:47,040 --> 00:40:47,560 Okay. 621 00:40:49,900 --> 00:40:50,900 So moving on. 622 00:40:51,520 --> 00:40:56,440 Using this particular function, create master process, you can execute DML and DDL statements. 623 00:40:56,760 --> 00:40:59,760 So like you can do grant DBA to any particular user. 624 00:41:00,840 --> 00:41:05,500 If you can execute DDL and DML statements, then obviously you can write SQL injection 625 00:41:05,500 --> 00:41:05,800 worms. 626 00:41:07,100 --> 00:41:10,360 SQL injection worms rose to the scene almost two years ago. 627 00:41:10,740 --> 00:41:15,420 All they do was change the web applications front end to inject malicious JavaScripts 628 00:41:15,420 --> 00:41:17,080 within the iframe of the front end. 629 00:41:17,080 --> 00:41:19,320 And then distribute browser exploits. 630 00:41:19,780 --> 00:41:22,500 Same worms can be written for Oracle-based applications. 631 00:41:23,020 --> 00:41:29,980 For example, if SQL injection worm in MS SQL looks like this, then you can write a corresponding 632 00:41:29,980 --> 00:41:31,820 SQL injection worm in Oracle. 633 00:41:32,020 --> 00:41:34,480 So Oracle applications can be equally vulnerable. 634 00:41:34,820 --> 00:41:37,040 It's just that they have not been exploited in the wild. 635 00:41:38,280 --> 00:41:38,860 Right. 636 00:41:38,980 --> 00:41:39,840 So moving on. 637 00:41:40,080 --> 00:41:45,660 So is there anything that we could do to protect sensitive data in the database? 638 00:41:45,860 --> 00:41:46,360 Okay. 639 00:41:46,360 --> 00:41:53,640 So I mean, you can defend your applications against SQL injection, but what if you want 640 00:41:53,640 --> 00:41:59,640 to have some defense in depth mechanism to say that even there is a SQL injection, the 641 00:41:59,640 --> 00:42:02,200 data which the attacker gets should be pointless. 642 00:42:02,620 --> 00:42:04,900 So that's where I think compliance comes into picture. 643 00:42:06,260 --> 00:42:10,580 So PCI compliance mandates that the card data, the PAN, must be stored encrypted. 644 00:42:11,100 --> 00:42:16,340 The distribution of keys used for encryption decryption. 645 00:42:16,340 --> 00:42:19,440 Should be controlled and regulated. 646 00:42:19,940 --> 00:42:24,320 So what happens when an attacker finds a SQL injection vulnerability in such a website 647 00:42:24,320 --> 00:42:25,720 which follows all this compliance? 648 00:42:26,240 --> 00:42:31,820 Does that mean that the card data which the attacker gets is all encrypted, so it will 649 00:42:31,820 --> 00:42:36,780 be pointless for the attacker to have this data if, obviously, he can't get the keys 650 00:42:36,780 --> 00:42:37,320 for decryption? 651 00:42:38,200 --> 00:42:43,080 So for example, here is a sample from a particular credit card table. 652 00:42:43,280 --> 00:42:45,580 As you can see, the card data is all encrypted. 653 00:42:45,580 --> 00:42:51,420 So even if you were to find a SQL injection, the data which you have without the keys will 654 00:42:51,420 --> 00:42:52,280 be pretty much useless. 655 00:42:53,760 --> 00:42:54,280 Right. 656 00:42:54,360 --> 00:43:01,560 So what I found was, what happens in this regulation, does it talk about where the 657 00:43:01,560 --> 00:43:02,100 encryption occurs? 658 00:43:02,360 --> 00:43:06,380 Does it occur or should it occur at the database level or should it occur at the application 659 00:43:06,380 --> 00:43:06,700 level? 660 00:43:07,060 --> 00:43:10,740 So what if the encryption occurs at the database layer? 661 00:43:11,060 --> 00:43:14,360 So in this particular case, there is an application server. 662 00:43:14,840 --> 00:43:15,360 Okay. 663 00:43:15,580 --> 00:43:16,520 It's different from a database server. 664 00:43:16,660 --> 00:43:17,860 So you have a three-tier architecture. 665 00:43:19,520 --> 00:43:24,700 And so for storing credit card information, we are doing an insert statement. 666 00:43:25,020 --> 00:43:31,100 And while inserting, we are calling a built-in Oracle function, which is a DBMS obfuscation 667 00:43:31,100 --> 00:43:32,520 toolkit dot triple-desk encryption. 668 00:43:33,860 --> 00:43:41,520 And this desk three encrypt function takes the card number as one argument and takes 669 00:43:41,520 --> 00:43:43,180 the encryption key as a second argument. 670 00:43:44,840 --> 00:43:45,360 Okay. 671 00:43:45,580 --> 00:43:47,840 So this is actually quite interesting. 672 00:43:49,120 --> 00:43:54,840 So this makes me think that what is more important here, the data or the query? 673 00:43:55,180 --> 00:44:00,100 Although the data which is stored in the database would be encrypted using this mechanism, 674 00:44:00,600 --> 00:44:05,000 but the query which will be executed on the database server will actually have the clear 675 00:44:05,000 --> 00:44:07,940 text span and the clear text string in it. 676 00:44:08,400 --> 00:44:14,500 So at any point in time, whenever that query executes on the server, if I can get a log 677 00:44:14,500 --> 00:44:16,840 of that SQL query, then I am pretty much done. 678 00:44:17,080 --> 00:44:22,200 If I can obtain that query, then I don't really care about the data, because that query will 679 00:44:22,200 --> 00:44:26,000 have the clear text span, and more importantly, it will have the clear text string. 680 00:44:26,460 --> 00:44:28,460 So I am not interested in data. 681 00:44:28,600 --> 00:44:29,580 I am interested in the query. 682 00:44:31,200 --> 00:44:36,120 So the SQL queries executed on the database server, they can be forensically obtained. 683 00:44:37,040 --> 00:44:42,920 V$SQL is one table in Oracle which lists statistics on shared SQL area. 684 00:44:43,680 --> 00:44:44,480 This table takes the data. 685 00:44:44,500 --> 00:44:46,140 It typically stores last 500 queries. 686 00:44:46,380 --> 00:44:51,420 So if you repeatedly keep querying this table, you will find out all queries which have been 687 00:44:51,420 --> 00:44:52,980 executed on the database server. 688 00:44:54,460 --> 00:45:00,420 Because it's a dynamic view, sometimes the content of this table, sorry, of this view 689 00:45:00,420 --> 00:45:07,560 gets written to this particular table, WRH$SQLTEXT, and in that case, these queries are actually 690 00:45:07,560 --> 00:45:09,200 permanently logged within the database. 691 00:45:10,200 --> 00:45:14,400 Similarly in MS SQL, you can obtain these queries for 692 00:45:14,400 --> 00:45:16,300 forensically by calling the plan cache. 693 00:45:17,960 --> 00:45:21,500 So this is how the query will look like when it will be on the server. 694 00:45:21,840 --> 00:45:26,460 As you can see, the clear text pan and the clear text string would be present in the 695 00:45:26,460 --> 00:45:29,540 SQL query when it runs on the server. 696 00:45:29,720 --> 00:45:34,920 So if you query SQL underscore text column from V$SQL, then you will find the clear text 697 00:45:34,920 --> 00:45:36,760 pan and the clear text string, more importantly. 698 00:45:38,280 --> 00:45:40,940 So these are some screenshots which say these are the strings. 699 00:45:42,240 --> 00:45:44,300 Similarly, you can obtain plan cache in MS SQL. 700 00:45:46,500 --> 00:45:50,680 here is an example of somebody MD5 hashing a credit card 701 00:45:50,680 --> 00:45:55,100 information within the database. So if you look at the bottom 702 00:45:55,690 --> 00:45:59,680 half of the screen, the data in the database is MD5 hashed and 703 00:45:59,680 --> 00:46:03,840 it's safe. But if you obtain the plan cache, then you will find 704 00:46:03,840 --> 00:46:08,840 that the plan cache contains the clear text PAN number. So that to 705 00:46:08,840 --> 00:46:14,820 me is a fail. Right. So when we talk about SQL injections, we 706 00:46:14,820 --> 00:46:17,280 talk about some really complicated hacks and sometimes 707 00:46:17,280 --> 00:46:20,380 we miss out on some easier versions. So we were 708 00:46:20,380 --> 00:46:24,700 investigating a SQL injection, a breach from SQL injection, and 709 00:46:24,700 --> 00:46:27,640 when I found this particular tech technique, which I thought 710 00:46:27,640 --> 00:46:33,040 was quite neat. So let me actually, I've only got five 711 00:46:33,040 --> 00:46:36,980 minutes, so it's best to run the demo before this man throws 712 00:46:36,980 --> 00:46:37,860 me out of here. 713 00:46:39,900 --> 00:46:45,120 Okay. So stealing unencrypted card data from the database when 714 00:46:45,120 --> 00:46:48,800 it's not stored as clear text. So we have our e-commerce 715 00:46:48,800 --> 00:46:52,080 shopping application. Any resemblance to any application 716 00:46:52,080 --> 00:46:57,800 is purely coincidental. Right. So you click on, you choose a 717 00:46:57,800 --> 00:47:00,700 page, you click on buy now. It follows PCI compliance, so it 718 00:47:00,700 --> 00:47:06,240 goes over a secure page. You log in, supply your credentials, go 719 00:47:06,240 --> 00:47:08,820 to the, select the item, enter your card number, do your 720 00:47:08,820 --> 00:47:20,080 details, and basically just buy the product. Right. So what 721 00:47:20,080 --> 00:47:24,620 happens if in such an application you find a SQL injection? As we 722 00:47:24,620 --> 00:47:28,620 saw earlier, that this particular application has a different 723 00:47:28,620 --> 00:47:32,420 application server and a different database server. So even if 724 00:47:32,420 --> 00:47:34,940 you compromise the database server, you will not have the key 725 00:47:34,940 --> 00:47:38,100 because the key is stored in the application server. 726 00:47:38,920 --> 00:47:42,300 You can use the attack vector which I mentioned earlier, but to 727 00:47:42,300 --> 00:47:46,700 query V$SQL table, you must have DBA privileges. So what if you 728 00:47:46,700 --> 00:47:50,740 don't, like in this example, then you cannot obtain the queries 729 00:47:50,740 --> 00:47:54,600 forensically, so you are pretty much struck. So we find a blind 730 00:47:54,600 --> 00:47:59,420 SQL injection in this application. So we do some and one equal to one 731 00:47:59,420 --> 00:48:07,680 and one equal to two. Right. And then we run some tools to exploit 732 00:48:07,680 --> 00:48:08,240 SQL injection. 733 00:48:09,340 --> 00:48:14,020 So we run the SQLBF to exploit this particular vulnerability. And we 734 00:48:14,020 --> 00:48:17,380 find, we do some basic exploitation. So we find, we are running queries 735 00:48:17,380 --> 00:48:20,880 with Scott user, which is a really limited user. So we can't really 736 00:48:20,880 --> 00:48:26,320 query V$SQL to obtain the queries forensically. We find out what the 737 00:48:26,320 --> 00:48:29,660 version is. Maybe we can do, can we do privilege escalation? And it's 738 00:48:29,660 --> 00:48:34,740 actually an Oracle 11g 1.0.6. So, I mean, we don't really know of 739 00:48:34,740 --> 00:48:38,640 any privilege escalation vectors against this database. So it 740 00:48:38,820 --> 00:48:44,400 looks fairly secure. Now, we find a card number table from credit 741 00:48:44,400 --> 00:48:51,280 card table, sorry, and if we extract the data, then we find the data is 742 00:48:51,280 --> 00:48:55,420 actually encrypted. So unless we have the private key, sorry, the 743 00:48:55,420 --> 00:49:01,060 decryption key, the data is virtually useless to me. So this is how the 744 00:49:01,060 --> 00:49:04,160 white box side things look like. The application server connects to 745 00:49:04,160 --> 00:49:08,800 the database server, which is a different IP. It's using the Oracle's triple desk and 746 00:49:08,820 --> 00:49:13,720 encryption for encrypting strings. The string is stored in the application server. I can 747 00:49:13,720 --> 00:49:18,280 possibly compromise the database server, but that will mean that I still don't have the 748 00:49:20,000 --> 00:49:25,820 private key, sorry, the decryption key. Right. Next, what the attacker does is he logs on 749 00:49:25,820 --> 00:49:32,240 to the server and he observes something. What he observes is that when he logs in on that 750 00:49:32,240 --> 00:49:39,780 payment page, he sees his full name displayed on the screen. So this particular data, his 751 00:49:39,780 --> 00:49:44,860 first name or his first name, last name, is actually a session data coming from the database. 752 00:49:45,120 --> 00:49:50,160 And that will be true for every single user who logs on to the application. So what if 753 00:49:50,160 --> 00:49:57,600 he can poison this data for every single user to contain a JavaScript key logger? So he 754 00:49:57,600 --> 00:50:02,200 does that. He runs some massive update statements to alter the data. 755 00:50:02,200 --> 00:50:08,780 So he logs in to the database and contains a malicious JavaScript on that web front end. 756 00:50:09,300 --> 00:50:16,240 So he issues a series of three commands, one after another. Okay. The looks are changing, 757 00:50:16,320 --> 00:50:24,160 so I'll just rush. Okay. So once he does that, and then now the victim logs in, then 758 00:50:24,160 --> 00:50:28,260 you will see that the data coming from the database, the session data, has now changed. 759 00:50:28,380 --> 00:50:32,120 And it now contains a JavaScript, which points to the beef. 760 00:50:33,980 --> 00:50:39,700 So what we are doing over here is we are not hacking the data within the database. We are 761 00:50:39,700 --> 00:50:43,860 hacking the data which is about to be written to the database before it touches the database. 762 00:50:45,560 --> 00:50:52,860 So when an end user logs in, this JavaScript will connect back to the beef server. So if 763 00:50:52,860 --> 00:50:57,520 you configure your beef server, then you will find the zombies connecting back to the beef 764 00:50:57,520 --> 00:51:00,640 server. So here we go. The zombies are connected. 765 00:51:00,640 --> 00:51:12,900 And then if you go back to the victim screen, then what you will find is, okay. Yeah. And 766 00:51:12,900 --> 00:51:18,300 then if he enters his details again onto that page, then you will find all that is 767 00:51:18,920 --> 00:51:24,960 available via our JavaScript key logger, and it is sent out to a beef server. So who really 768 00:51:24,960 --> 00:51:28,380 cares about the data in the database if you can obtain it like that? 769 00:51:29,760 --> 00:51:30,200 Okay. 770 00:51:30,640 --> 00:51:35,340 So I've been asked to leave the stage. That pretty much is the end of the presentation.