1 00:00:00,000 --> 00:00:01,080 Hello, good afternoon, everybody. 2 00:00:01,420 --> 00:00:03,060 My name is Mohamed Zupakar, 3 00:00:03,160 --> 00:00:05,120 and today I'm going to give a presentation 4 00:00:05,120 --> 00:00:07,420 called Advance MySQL Exploitation. 5 00:00:07,860 --> 00:00:10,380 So basically, it's how can you conduct 6 00:00:10,380 --> 00:00:12,780 a remote code execution on MySQL platform. 7 00:00:15,780 --> 00:00:17,720 Here's a brief introduction about myself. 8 00:00:17,960 --> 00:00:20,760 I'm Secretary Consultant of securityassessment.com. 9 00:00:20,920 --> 00:00:22,500 We are a New Zealand-based company 10 00:00:22,500 --> 00:00:25,180 focused on penetration testing and security research. 11 00:00:27,400 --> 00:00:30,180 So I'm sure most of the people in this room 12 00:00:30,180 --> 00:00:32,140 have a clear idea about SQL injection. 13 00:00:33,160 --> 00:00:35,780 It's a condition where we are able to manipulate 14 00:00:35,780 --> 00:00:37,520 SQL treatment through the application, 15 00:00:37,900 --> 00:00:40,100 and normally a SQL injection is used 16 00:00:40,100 --> 00:00:42,640 to view, modify, and delete database data. 17 00:00:43,360 --> 00:00:45,740 In some cases, it's possible to conduct 18 00:00:45,740 --> 00:00:47,960 remote code executions by attacking 19 00:00:47,960 --> 00:00:49,020 this type of vulnerability. 20 00:00:53,660 --> 00:00:55,160 So stack query is a way to conduct 21 00:00:55,160 --> 00:00:55,440 a remote code execution on a database. 22 00:00:55,440 --> 00:00:57,400 It's a condition where multiple SQL statements 23 00:00:57,400 --> 00:00:59,160 are not allowed, and they are normally 24 00:00:59,160 --> 00:01:00,840 being separated by semicolons. 25 00:01:01,180 --> 00:01:03,120 Stack query has been known widely 26 00:01:03,120 --> 00:01:06,420 to be abused to write a file on a database server. 27 00:01:07,460 --> 00:01:09,800 In the Black Hat Europe 2009, 28 00:01:10,300 --> 00:01:13,260 Bernardo Demoli demonstrated how stack query 29 00:01:13,260 --> 00:01:15,640 can be abused to conduct a remote code execution 30 00:01:15,640 --> 00:01:17,540 on a few different databases. 31 00:01:18,220 --> 00:01:19,940 And today I'm going to demonstrate 32 00:01:19,940 --> 00:01:22,360 how we can conduct a remote code execution 33 00:01:22,360 --> 00:01:25,040 on the application that do not support stack query. 34 00:01:28,900 --> 00:01:31,320 Here is an example how stack query 35 00:01:31,320 --> 00:01:34,640 can be abused on a MySQL platform. 36 00:01:35,180 --> 00:01:38,200 This technique is known as a UDF injection tenant. 37 00:01:38,580 --> 00:01:41,820 However, it only works on the ASP.NET application 38 00:01:41,820 --> 00:01:43,620 due to the stack query allowed. 39 00:01:47,500 --> 00:01:49,600 If you look at the stack query tables, 40 00:01:49,860 --> 00:01:52,560 on the top side we have different types of applications. 41 00:01:52,800 --> 00:01:54,700 On the left side we have all different types 42 00:01:54,700 --> 00:01:55,100 of databases. 43 00:01:55,160 --> 00:01:56,860 As you can see in this table, 44 00:01:57,040 --> 00:01:58,180 there are only two applications 45 00:01:58,180 --> 00:02:00,380 that do not support stack query by default. 46 00:02:00,660 --> 00:02:03,100 They are known as ASP.NET and PHP. 47 00:02:03,620 --> 00:02:05,460 So what we're going to focus today, 48 00:02:05,540 --> 00:02:07,720 we're going to focus on the PHP MySQL. 49 00:02:07,980 --> 00:02:11,780 As we know, PHP is widely used with MySQL, 50 00:02:12,000 --> 00:02:14,060 and they are part of the software stack 51 00:02:14,060 --> 00:02:15,680 for the LAM and the WAM environment. 52 00:02:19,400 --> 00:02:20,500 So traditionally, 53 00:02:22,320 --> 00:02:25,060 PHP shell can be used to execute commands. 54 00:02:25,160 --> 00:02:27,160 By attacking MySQL PHP. 55 00:02:27,940 --> 00:02:31,340 So if we can just simply execute commands on the web server, 56 00:02:31,420 --> 00:02:32,760 why do we need more reliable shells? 57 00:02:33,560 --> 00:02:36,140 A good answer for this is the Metapredator shells. 58 00:02:36,860 --> 00:02:38,880 Metapredator shells is a powerful shell 59 00:02:38,880 --> 00:02:42,060 that can be very useful in a post-exploitation process. 60 00:02:42,580 --> 00:02:44,800 So we can use Metapredator shells 61 00:02:44,800 --> 00:02:47,740 to conduct a token impersonation attack 62 00:02:47,740 --> 00:02:51,000 to escalate our privilege from a local system 63 00:02:51,000 --> 00:02:52,200 to administrate the user. 64 00:02:52,480 --> 00:02:54,480 And we also can use Metapredator shells 65 00:02:54,480 --> 00:02:54,940 to, 66 00:02:55,160 --> 00:02:56,880 to migrate from one process to another process. 67 00:02:57,740 --> 00:02:59,900 Another example is if in some cases 68 00:02:59,900 --> 00:03:01,880 when we want to have a graphic user interface, 69 00:03:02,100 --> 00:03:03,940 we can just simply use a VNC shell. 70 00:03:04,640 --> 00:03:06,940 And all the shells code for this shell 71 00:03:06,940 --> 00:03:09,220 can easily be created using a Metasflight framework. 72 00:03:12,600 --> 00:03:14,560 So here are some basic techniques 73 00:03:14,560 --> 00:03:17,580 in reading and writing file on a MySQL PHP platform. 74 00:03:17,900 --> 00:03:21,460 So basically we use select load in file 75 00:03:21,460 --> 00:03:22,540 to read a file, 76 00:03:22,700 --> 00:03:25,120 and then select into out file or down file, 77 00:03:25,160 --> 00:03:25,940 to write a file. 78 00:03:26,800 --> 00:03:30,040 And the next one is the remote code execution technique 79 00:03:30,040 --> 00:03:31,100 on a MySQL PHP. 80 00:03:31,420 --> 00:03:33,800 So basically we can upload our arbitrary file, 81 00:03:33,920 --> 00:03:35,480 compress our arbitrary file, 82 00:03:35,700 --> 00:03:38,720 and then this file can be decompressed back 83 00:03:38,720 --> 00:03:40,000 to its original size 84 00:03:40,000 --> 00:03:43,080 before it is executed using a PHP system function. 85 00:03:48,100 --> 00:03:50,900 If the application do not support stack query, 86 00:03:51,160 --> 00:03:53,880 the only method to write a file is through the union select. 87 00:03:54,120 --> 00:03:54,860 And here, 88 00:03:54,860 --> 00:03:57,480 here are some challenges in writing arbitrary file 89 00:03:57,480 --> 00:03:58,320 through union select. 90 00:03:59,360 --> 00:04:01,760 First, the get request only limit us 91 00:04:01,760 --> 00:04:05,800 to only up to 8,190 bytes on Apache. 92 00:04:07,020 --> 00:04:09,400 And also data from our first query 93 00:04:09,400 --> 00:04:11,020 can override our file header. 94 00:04:11,800 --> 00:04:14,080 And then the last one is data from the extra column 95 00:04:14,080 --> 00:04:16,940 can add unnecessary data into our arbitrary file, 96 00:04:17,080 --> 00:04:19,180 and this can potentially corrupt our file. 97 00:04:20,140 --> 00:04:21,900 So I'm going to explain in details 98 00:04:21,900 --> 00:04:23,480 how we can fix all the issue. 99 00:04:26,580 --> 00:04:28,680 So to fix the URL length issue, 100 00:04:28,800 --> 00:04:30,500 we can simply use the Zlib module 101 00:04:30,500 --> 00:04:32,560 to compress our arbitrary file. 102 00:04:33,220 --> 00:04:34,480 In my experiment, 103 00:04:34,640 --> 00:04:36,700 from 9,625 bytes, 104 00:04:36,860 --> 00:04:40,220 I was able to compress my file only up to 630 bytes. 105 00:04:41,280 --> 00:04:42,440 And this will be able, 106 00:04:42,640 --> 00:04:45,560 let me to bypass the maximum limit allowed on Apache. 107 00:04:46,800 --> 00:04:49,300 And this file can then be decompressed back 108 00:04:49,300 --> 00:04:51,520 on the destination before it's been executed. 109 00:04:54,860 --> 00:04:55,620 Let's try it once again. 110 00:04:57,060 --> 00:04:59,680 Union select will combine a result 111 00:04:59,680 --> 00:05:01,460 from the second query and the first query. 112 00:05:01,660 --> 00:05:03,380 And most of the time, you will find 113 00:05:03,380 --> 00:05:06,440 that the result from the first query 114 00:05:06,440 --> 00:05:08,180 will override your file header. 115 00:05:09,900 --> 00:05:11,920 So to prevent this to happen, 116 00:05:12,080 --> 00:05:13,920 we can inject a non-existing value 117 00:05:13,920 --> 00:05:14,840 in the WHERE clause. 118 00:05:16,880 --> 00:05:18,320 If you look at this example, 119 00:05:18,660 --> 00:05:22,520 question equal 169 was written as a string DEFCON. 120 00:05:22,660 --> 00:05:24,840 And this string will override our file header. 121 00:05:24,840 --> 00:05:28,700 header. So if it can find any non-existing value to replace 122 00:05:28,700 --> 00:05:32,680 a variable question, we can force the application to return us 123 00:05:32,680 --> 00:05:36,720 an empty data. And it can prevent our file from 124 00:05:36,720 --> 00:05:37,200 being corrupted. 125 00:05:40,820 --> 00:05:42,640 So fixing the column issue. 126 00:05:44,240 --> 00:05:48,820 So in Union Selects, the second query required the same 127 00:05:48,820 --> 00:05:52,840 amount of column of the first query. In most of the time, you will find you need to 128 00:05:52,840 --> 00:05:56,160 inject some unnecessary data into some of this column. 129 00:05:57,460 --> 00:06:00,660 To prevent data corruptions, our arbitrary 130 00:06:00,660 --> 00:06:04,820 file need to be injected in the first column. This is because 131 00:06:04,820 --> 00:06:08,520 when we compress our file, Zlib uses Adler32 132 00:06:08,520 --> 00:06:12,780 checksums, and this value is added at the end of our compressed 133 00:06:12,780 --> 00:06:16,720 arbitrary file. In IRC, it is stated 134 00:06:16,720 --> 00:06:20,060 that anything after this value will be ignored during the 135 00:06:20,700 --> 00:06:22,680 decompression process. So we can just simply 136 00:06:22,680 --> 00:06:26,640 abuse this functionality. Another method is by 137 00:06:26,640 --> 00:06:30,720 splitting our arbitrary file into the amounts 138 00:06:30,720 --> 00:06:34,540 of columns, and then injects our data by sequence 139 00:06:34,540 --> 00:06:36,220 into each of these columns. 140 00:06:39,380 --> 00:06:42,680 Here is an example. Let's say we required three columns, 141 00:06:42,840 --> 00:06:46,200 and you can see here, the arbitrary data 142 00:06:46,200 --> 00:06:50,900 is injected only in the first column, and then we got two null bytes, and this null 143 00:06:50,900 --> 00:06:52,620 byte is injected on the second column. 144 00:06:52,620 --> 00:06:53,100 So we have a problem on word. 145 00:07:03,500 --> 00:07:06,560 So remote code executions on the LAMP has one limitation. 146 00:07:07,100 --> 00:07:10,440 By default, directory created in Linux is not writable. 147 00:07:11,260 --> 00:07:15,840 But in some cases, you will find the application that required this directory 148 00:07:15,840 --> 00:07:22,480 to be writable. One example is the application that allows users to upload content. 149 00:07:22,600 --> 00:07:27,280 So if we can find this type of application, we can abuse the functionality by uploading 150 00:07:27,280 --> 00:07:33,620 our arbitrary file into this directory. And also, file created through the select 151 00:07:33,620 --> 00:07:38,740 into dump file is not executable by default. But this is not an issue, because we can just 152 00:07:38,740 --> 00:07:48,700 simply use a PHP to read the file contents and write to another file, and then the permission 153 00:07:48,700 --> 00:07:52,000 for this file can be changed using a PHP system function. 154 00:07:56,560 --> 00:07:58,660 Remote code execution on the 155 00:07:58,660 --> 00:08:04,940 WAMP has less limitation compared to the LAMP. This is because, by default, MySQL runs 156 00:08:04,940 --> 00:08:11,180 as a local system. And this user is allowed to write anywhere in the web server directory. 157 00:08:13,180 --> 00:08:18,300 So we can, if we found a WAMP system, we can just simply write our arbitrary file into 158 00:08:18,300 --> 00:08:21,980 one of the web server directory, and then use the same techniques we used on the LAMP, 159 00:08:22,000 --> 00:08:26,260 LAM, which we use a PHP system function to execute this arbitrary file. 160 00:08:31,800 --> 00:08:38,940 So what is MySQLoic? MySQLoic is a SQL injection takeover tool that I wrote as a proof of concept 161 00:08:38,940 --> 00:08:44,540 to demonstrate how we can perform a remote code execution on the LAM, on the WAM environment. 162 00:08:45,740 --> 00:08:51,180 It contains a few different modules, and they are known as SQL injection detections, module 163 00:08:51,180 --> 00:08:55,960 module, fingerprint directory module, fingerprint operating systems, payloads, and exploit module. 164 00:08:57,220 --> 00:09:04,900 And now, I'm going to demonstrate how we can use all these modules to upload and execute Metasploit shellcode. 165 00:09:39,220 --> 00:09:39,740 Hello. 166 00:09:41,160 --> 00:09:42,020 Sorry about that. 167 00:09:45,660 --> 00:09:51,160 Okay, so we're going to try the first module, which is known as SQL injection detections. 168 00:09:51,920 --> 00:09:57,640 So basically, it uses a deep-line injection method, which is known as a timing attack. 169 00:10:04,160 --> 00:10:08,960 As you can see here, SQL injection vulnerabilities are successfully discovered, and it contains 170 00:10:08,960 --> 00:10:14,300 only one column. And the second one is the fingerprint operating system module. So basically, 171 00:10:14,440 --> 00:10:19,280 fingerprint operating system module, using a load-in-file method, which is trying to 172 00:10:19,280 --> 00:10:21,080 load a default file for Linux. 173 00:10:42,120 --> 00:10:49,340 So, we're going to solidify all the libraries in Hadamard and 174 00:10:50,040 --> 00:10:50,860 Black Level. 175 00:10:50,860 --> 00:10:55,840 working directory module so basically it uses two different methods they are 176 00:10:55,840 --> 00:10:59,440 known as an error message method and the other one is load in file method so 177 00:10:59,440 --> 00:11:05,680 basically the error message method is used by injecting a bogus data as part 178 00:11:05,680 --> 00:11:10,980 of SQL query because normally PHP written as an error message and this 179 00:11:10,980 --> 00:11:15,020 error message contains a sensitive information about the working directory 180 00:11:16,120 --> 00:11:20,560 and then the second method is known as a load in file method so basically what 181 00:11:20,560 --> 00:11:25,280 it does it tried to load a default file for upper Apache configuration files 182 00:11:25,280 --> 00:11:29,260 because this file contains a sensitive information or working directory as 183 00:11:29,260 --> 00:11:29,460 well 184 00:11:39,980 --> 00:11:44,960 so as you can see here the first method was failed and now it's trying on the 185 00:11:44,960 --> 00:11:50,480 second method and and you can see here here 186 00:11:50,480 --> 00:11:58,480 is our web server working directory so we can just copy this information at is 187 00:11:58,480 --> 00:12:06,200 it required as pass or the argument in the exploit module and then the next one 188 00:12:06,200 --> 00:12:09,780 is a payload module so basically my squad will integrate with the meta 189 00:12:09,780 --> 00:12:17,920 spike framework to create a suitable payload for our target so let's say I 190 00:12:17,920 --> 00:12:19,120 want to create a reverse 191 00:12:20,480 --> 00:12:24,260 bind shellcode on a pod nine nine nine nine 192 00:12:40,380 --> 00:12:46,100 as you can see here our shellcode is successfully created any bind on port 193 00:12:46,100 --> 00:12:49,920 nine nine nine reverse VNC shellcode and now 194 00:12:50,820 --> 00:12:57,900 is the last model is the export module and as you can see here here are the 195 00:12:57,900 --> 00:13:02,640 word argument required so we can just simply add all the information that we 196 00:13:02,640 --> 00:13:03,160 had before 197 00:13:04,320 --> 00:13:04,920 you 198 00:13:06,160 --> 00:13:06,760 you 199 00:13:10,040 --> 00:13:10,640 you 200 00:13:12,080 --> 00:13:12,680 you 201 00:13:19,220 --> 00:13:21,800 Writeable directory is not required in this case. 202 00:13:21,920 --> 00:13:28,400 This is because we know that our target is running on Windows, and basically MySQL on 203 00:13:28,400 --> 00:13:33,820 Windows runs as a local system, so we can just simply write into any web server directory. 204 00:13:33,820 --> 00:13:38,360 So, in this case, I just choose a web server root directory. 205 00:13:48,940 --> 00:13:55,440 And now you will see some bunch of compressed arbitrary file be uploaded, and then this 206 00:13:55,440 --> 00:14:01,180 file will be decompressed back to its original size before it is executed using a PHP system 207 00:14:01,180 --> 00:14:01,640 function. 208 00:14:02,380 --> 00:14:03,060 OK. 209 00:14:03,460 --> 00:14:03,800 OK. 210 00:14:12,720 --> 00:14:17,760 And you see the Metasploit listener is running on part 999, and it's waiting for the connection. 211 00:14:30,460 --> 00:14:33,160 And you can see the shellcode is successfully executed. 212 00:14:33,160 --> 00:14:37,820 And now we have our VNC shells.