Hello, good afternoon, everybody. My name is Mohamed Zupakar, and today I'm going to give a presentation called Advance MySQL Exploitation. So basically, it's how can you conduct a remote code execution on MySQL platform. Here's a brief introduction about myself. I'm Secretary Consultant of securityassessment.com. We are a New Zealand-based company focused on penetration testing and security research. So I'm sure most of the people in this room have a clear idea about SQL injection. It's a condition where we are able to manipulate SQL treatment through the application, and normally a SQL injection is used to view, modify, and delete database data. In some cases, it's possible to conduct remote code executions by attacking this type of vulnerability. So stack query is a way to conduct a remote code execution on a database. It's a condition where multiple SQL statements are not allowed, and they are normally being separated by semicolons. Stack query has been known widely to be abused to write a file on a database server. In the Black Hat Europe 2009, Bernardo Demoli demonstrated how stack query can be abused to conduct a remote code execution on a few different databases. And today I'm going to demonstrate how we can conduct a remote code execution on the application that do not support stack query. Here is an example how stack query can be abused on a MySQL platform. This technique is known as a UDF injection tenant. However, it only works on the ASP.NET application due to the stack query allowed. If you look at the stack query tables, on the top side we have different types of applications. On the left side we have all different types of databases. As you can see in this table, there are only two applications that do not support stack query by default. They are known as ASP.NET and PHP. So what we're going to focus today, we're going to focus on the PHP MySQL. As we know, PHP is widely used with MySQL, and they are part of the software stack for the LAM and the WAM environment. So traditionally, PHP shell can be used to execute commands. By attacking MySQL PHP. So if we can just simply execute commands on the web server, why do we need more reliable shells? A good answer for this is the Metapredator shells. Metapredator shells is a powerful shell that can be very useful in a post-exploitation process. So we can use Metapredator shells to conduct a token impersonation attack to escalate our privilege from a local system to administrate the user. And we also can use Metapredator shells to, to migrate from one process to another process. Another example is if in some cases when we want to have a graphic user interface, we can just simply use a VNC shell. And all the shells code for this shell can easily be created using a Metasflight framework. So here are some basic techniques in reading and writing file on a MySQL PHP platform. So basically we use select load in file to read a file, and then select into out file or down file, to write a file. And the next one is the remote code execution technique on a MySQL PHP. So basically we can upload our arbitrary file, compress our arbitrary file, and then this file can be decompressed back to its original size before it is executed using a PHP system function. If the application do not support stack query, the only method to write a file is through the union select. And here, here are some challenges in writing arbitrary file through union select. First, the get request only limit us to only up to 8,190 bytes on Apache. And also data from our first query can override our file header. And then the last one is data from the extra column can add unnecessary data into our arbitrary file, and this can potentially corrupt our file. So I'm going to explain in details how we can fix all the issue. So to fix the URL length issue, we can simply use the Zlib module to compress our arbitrary file. In my experiment, from 9,625 bytes, I was able to compress my file only up to 630 bytes. And this will be able, let me to bypass the maximum limit allowed on Apache. And this file can then be decompressed back on the destination before it's been executed. Let's try it once again. Union select will combine a result from the second query and the first query. And most of the time, you will find that the result from the first query will override your file header. So to prevent this to happen, we can inject a non-existing value in the WHERE clause. If you look at this example, question equal 169 was written as a string DEFCON. And this string will override our file header. header. So if it can find any non-existing value to replace a variable question, we can force the application to return us an empty data. And it can prevent our file from being corrupted. So fixing the column issue. So in Union Selects, the second query required the same amount of column of the first query. In most of the time, you will find you need to inject some unnecessary data into some of this column. To prevent data corruptions, our arbitrary file need to be injected in the first column. This is because when we compress our file, Zlib uses Adler32 checksums, and this value is added at the end of our compressed arbitrary file. In IRC, it is stated that anything after this value will be ignored during the decompression process. So we can just simply abuse this functionality. Another method is by splitting our arbitrary file into the amounts of columns, and then injects our data by sequence into each of these columns. Here is an example. Let's say we required three columns, and you can see here, the arbitrary data is injected only in the first column, and then we got two null bytes, and this null byte is injected on the second column. So we have a problem on word. So remote code executions on the LAMP has one limitation. By default, directory created in Linux is not writable. But in some cases, you will find the application that required this directory to be writable. One example is the application that allows users to upload content. So if we can find this type of application, we can abuse the functionality by uploading our arbitrary file into this directory. And also, file created through the select into dump file is not executable by default. But this is not an issue, because we can just simply use a PHP to read the file contents and write to another file, and then the permission for this file can be changed using a PHP system function. Remote code execution on the WAMP has less limitation compared to the LAMP. This is because, by default, MySQL runs as a local system. And this user is allowed to write anywhere in the web server directory. So we can, if we found a WAMP system, we can just simply write our arbitrary file into one of the web server directory, and then use the same techniques we used on the LAMP, LAM, which we use a PHP system function to execute this arbitrary file. So what is MySQLoic? MySQLoic is a SQL injection takeover tool that I wrote as a proof of concept to demonstrate how we can perform a remote code execution on the LAM, on the WAM environment. It contains a few different modules, and they are known as SQL injection detections, module module, fingerprint directory module, fingerprint operating systems, payloads, and exploit module. And now, I'm going to demonstrate how we can use all these modules to upload and execute Metasploit shellcode. Hello. Sorry about that. Okay, so we're going to try the first module, which is known as SQL injection detections. So basically, it uses a deep-line injection method, which is known as a timing attack. As you can see here, SQL injection vulnerabilities are successfully discovered, and it contains only one column. And the second one is the fingerprint operating system module. So basically, fingerprint operating system module, using a load-in-file method, which is trying to load a default file for Linux. So, we're going to solidify all the libraries in Hadamard and Black Level. working directory module so basically it uses two different methods they are known as an error message method and the other one is load in file method so basically the error message method is used by injecting a bogus data as part of SQL query because normally PHP written as an error message and this error message contains a sensitive information about the working directory and then the second method is known as a load in file method so basically what it does it tried to load a default file for upper Apache configuration files because this file contains a sensitive information or working directory as well so as you can see here the first method was failed and now it's trying on the second method and and you can see here here is our web server working directory so we can just copy this information at is it required as pass or the argument in the exploit module and then the next one is a payload module so basically my squad will integrate with the meta spike framework to create a suitable payload for our target so let's say I want to create a reverse bind shellcode on a pod nine nine nine nine as you can see here our shellcode is successfully created any bind on port nine nine nine reverse VNC shellcode and now is the last model is the export module and as you can see here here are the word argument required so we can just simply add all the information that we had before you you you you Writeable directory is not required in this case. This is because we know that our target is running on Windows, and basically MySQL on Windows runs as a local system, so we can just simply write into any web server directory. So, in this case, I just choose a web server root directory. And now you will see some bunch of compressed arbitrary file be uploaded, and then this file will be decompressed back to its original size before it is executed using a PHP system function. OK. OK. And you see the Metasploit listener is running on part 999, and it's waiting for the connection. And you can see the shellcode is successfully executed. And now we have our VNC shells.