This talk is Max Exploitation and I'm the Rook. What we'll be talking about today is remote code execution vulnerabilities specifically against PHP nuke. In this case we have a PHP nuke install running on a virtual machine and we'll be breaking into it. My exploit was released back in May. This is the code here. It's very simple. In this case we're going to be giving it a cookie. It's .php, it's fire and forget. We give it a domain name. In this case we have to give it a cookie. The reason why we're giving it a cookie is because we'll go into it in a bit. In earlier versions this exploit spans three different versions. 7.0, 8.135, 8.1. It's very simple. Hit enter. I apologize. One moment. The configuration was incorrect. Okay. All right. It's using the cookie we provided it, which is a user cookie. Fuck. . Fuck. . Anyway. It's uploading a back door. And finally exploits it. Right now it uploaded a very simple eval into front end .php. It dropped this php file. The reason why I chose this is because there's a back end .php and I figured it would be fitting to have a front end. Now, Now what we're going to do is recently, just after I released this exploit, Metasploit released a very nice reverse shell for PHP. So in this case it's the exploit slash Unix slash web app PHP eval. We set a local host and our host and our port and you are a path to the front end we just dropped. And then type exploit. Now it's dropped me to a shell so you name it dash A. So what was the fallout from some of this attack? What I really wanted was a very simple to use exploit that affected many systems. Here is a Malaysian government website. I'm not going to try and pronounce that. But about a week after releasing my exploit it showed up on zone H, defaced. Yeah, popping a shell on a .gov. The very next day, defaced again by the same crew. So then what would they do? They would go to the vendor's website to get a patch, correct? Vendor got hacked. Phpnuke.org . Thank you. So what happened here, the vendor's website wasn't defaced like the others. Instead it added an iframe. In fact, most of the websites, they added this iframe to the bottom of the page. And what this is doing is it's a part of the Eleanor exploit pack. And in Eleanor, it tries a number of exploits trying to exploit Flash, IE, or Java. It is believed from some of the write-up of these antivirus companies, it's believed that the same ‑‑ a few days after phpnuke.org was owned, the U.S. Treasury website was also owned. And it's believed that these were the same people behind this attack. And it's believed that these were the same people behind this attack. And they were using that because they can do all of these things. So then it was called the Eleanor Exploit Pack. And it was one point two million in size. This is how modern systems are broken into. You deface websites. You add an iframe. And you start hacking into clients. So why do this? There's a very important quote by Richard Feynman. He's a Nobel Prize winning physicist, one of the fathers of the bomb. What I cannot understand ‑‑ what I cannot create ‑‑ I do not understand, I cannot do. I cannot do. I cannot do. I can't understand, I cannot do. I cannot do. I cannot do. understand. So the problem with security today is that there are a lot of white hats that don't understand the exploitation process, yet they're building systems to stop it. These systems are fundamentally flawed. I don't care if you're the most Aryan of white hats, you must understand the exploitation process. So there are two other quotes. Layers of security. I mean, this is something that's a part of nature and something that's been probably prehistory. And then there's Bruce Schneier. Complexity is the worst enemy of security. Now these two ideas are at conflict because by adding layers, you're also adding complexity to the system. And this level of complexity is vulnerable to attack. For instance, there are buffer overflows found in anti-viruses and even firewalls are vulnerable to attack. In this case, Managed Engine Firewall Analyzer 5, I found two vulnerabilities in the software, CSRF and the CSRF. The problem is, is that on this firewall, you're able to execute SQL queries, very similar to phpMyAdmin. So in this same request, I could execute SQL on the database and the results from that SQL could be JavaScript and could be executed on the client. This is one request that fails twice. But they're not alone. There's ProFence Web Application Firewall and their catch line is defenses against all of the OWASP top ten. Well, someone should tell them that CSRF and XSRF are the same thing. They're the same thing. They're the same thing. They're the same thing. You can do stuff like change what server it's going through. It acts as an intermediary between the Internet and the Web application. So you can just change what Web site it's being hosted at. Or you could shut it down altogether. But what we're talking about is phpNuke. And one thing that I've ‑‑ this exploit spans almost ten years now of releases from November 2004 until now. Currently, it's only been a couple of years. It's only been a couple of years. This is unpatched. Although the vendor's Web site hasn't been hacked so there may be a patch in existence, the patch has not been made public. So anyone running phpNuke is currently vulnerable to this exploit. And the exploit code is on your DEF CON CD. So one thing to note, though, is that in the phpNuke 7.0 branch, there were less exploits required, less vulnerabilities required in order to gain remote code execution. If you can see on the right ‑‑ I ‑‑ there was more level of complexity. As time goes by, the application itself has gotten more secure. But what we're going to focus on is this chain here. Affecting 8.135. So we're using SQL injection to obtain administrative credentials out of the database. We use those administrative credentials to leverage a broken authentication and session management, OAuth A3. And ultimately to get ‑‑ well, I don't know. I don't know. I don't know. Then to get information disclosure about the system, which is required for later exploits. So for these three steps, I can do manually. I'm going to do it in a Web browser. So let's do it. Demo. I'm going to clear out my cookies real quick. Just from the beginning. Okay. Okay. Currently we're logged in as a user. We're going to go to the journal module. We're going to list all journals. We're going to add a new entry. Test body. And we're going to use the evil face for this one. Now we're going to fire up tamper data. So often what will happen is if you run a web application scanner, what it's going to tell you is it's going to give you the variable name that's vulnerable to SQL injection and what page it's on. Sometimes you can't modify that variable directly. So we're going to have to use tamper data to modify it. So tamper data is set and it's going to capture the next query. We're going to tamper that. Mood. Devilish. Well, that's where the SQL injection is. So we're going to do ‑‑ we're going to do a ‑‑ we're going to do a test. We're going to start off and we need to figure out how many variables are in this insert statement. So one, two, three. I know that it's five. But you can count this up. This can be exploited completely black box. You don't really have to know what's going on behind it. In this case I'm just counting up one, two, three, four, five and then a comment to finish off the rest of the query. So as we fire it off, I've modified this application to print out the query for educational purposes. But it's not required. It does help sometimes when you're exploiting SQL to print out the query. So I'm going to fire off this query on phpMyAdmin. And it's being very slow. VMware can be a bit slow at times. Although it is very useful. One thing about VMware that was very helpful in this is that it allowed me to break into a virtual system and to be able to fine tune my exploit. So that ultimately it could work on the vendor. Although I stress this. I never tested this on a remote system. Only in virtual environments. And I recommend that you do the same. Wow. Okay. Now, here's the important part. Let me zoom in on this. So this is the part that we injected into the query. And notice our comment here at the end. And this is commenting off the rest of the query. What's important to note here is when you're exploiting an insert statement under MySQL, you can't simply just stack a new query. We can't just say, hey, we want to do an insert or we want to do ‑‑ we have an insert and we want to turn it into a delete. We can't do that. Instead, we have to use a different form of exploitation. So another ‑‑ an important thing to note is that here are the number of columns and we have to have the number of values to correspond to these columns. So we're going to add a new entry now. And this time, we want to pull out the administrative credentials in the ‑‑ in the ‑‑ sorry, sorry, sorry. Fire up tamper data. Tamper. Okay. One moment. Okay. So the ‑‑ okay. So we're going to do this. So we're going to do this. Now, the first query that we entered, we entered 1, 2, 3, 4, 5. So how did that respond? How did the data change? Notice here are the first record and we have 1 and 2. We overwrite the date and the time. When we click on ‑‑ when we actually click on the title itself, we see 1 and 2 and 4 and 5. Three went somewhere. We don't know where it is. But what this is ‑‑ the significance of this is that we know it's not blind SQL injection. We know that we can control some of these fields. And specifically, 1 and 2 look like great places to put our data. So. Instead of putting in 1 and 2, we're going to select the user name, in this case it's AID, from ‑‑ we can ‑‑ we have to do a limit 1 because we can only have one piece of data come out ‑‑ only one row come out from this subselect statement. Subselects are limited. You don't have a full ‑‑ you don't have full access to all of the operators available. But what we can do, it allows us to do this. It allows us to pull data from a different table. Correct. This is the query that we injected. If we go back to our ‑‑ if we go back to our ‑‑ it should look something like this. Where we would have the admin, user name and the password. What this allows us to do is then move on to the next step, which is be able to create an administrative credential, a cookie for the ‑‑ for this user. So we can do a make cookie. And what this is, is a very simple ‑‑ a very simple script. That allows us to create this cookie. Basically, all I'm doing is concatenating the two values together and sending it to base 64 encode. Now, if we go into the min.php, it's asking for user name and password. What we're going to do is using JavaScript in the URL, we can set the administrative credential. Admin equals this value. Hit enter. Basically, this is just telling us, yes, we've set that variable. It's still asking us for the user name and password. Refresh. Now we're the administrator. Now, if we move into the preferences, what we can do is general ‑‑ what we can do is, by default, what PHP Nuke is doing is it's preventing all errors from reaching the user. This is actually an important step. Because errors give us important information about the application. In some cases, like if you're testing for SQL injection, it's vital that error reporting is turned on because it's a very good way to detect that SQL injection is there. It also allows us to get something even more important in this case, which is the local path. So we're going to enable error reporting. And it immediately vomits up on us. But the most important part is here. Is this. Now, there is actually a problem with this install. And that's why it's displaying so many errors. It shouldn't normally do this. Instead, what we would have to do is trigger the error. And in that case, what we have to do is ‑‑ there is an easy way to trigger error under PHP. And that is by passing an array to many functions. In this case, we're going to use MD5, but it could be HTML special characters or a number of functions. If you pass an array and it's expecting to see a string, it will vomit up and throw an error. So we're going to make a bunk user. We're going to use tamper data again. Start tamper. We're going to add a new user. We only want to tamper this one request. So admin add pass is going to be passed to MD5. Instead, we're going to create a new. We're going to say add pass. And then we're going to add the brackets to it, which now makes it an array. Chunk. Click okay. So, warning, MD5 expects parameter 1 to be a string array given. And it gives us the path, which is vital. So ‑‑ Okay. Okay. Okay. Okay. So now something about php nuke. It's been hacked a lot. It has had a very ‑‑ it has a very poor history of security. But because it's been hacked so many times that it has progressed, it is ‑‑ it has been made better. Although I'm not the first to exploit at least the administrative credentials part, the administrative area has been locked down. In earlier versions of php nuke, there were versions where you was the ability to just modify the HTML on the front page. So you could just add a malicious iframe. Currently we don't have that ability. There was even the ability to just upload PHP files and execute them. Well, that is very ‑‑ that was also removed because so many people were gaining administrative access. In this analogy, the lowest hanging fruit represented by the apple at the bottom of the tree is a vulnerability that doesn't require much user interaction. In fact, in 7.0 to 8.0 all the way up to 8.1, there was an ability to get blind SQL injection in the refer parameter. You didn't have to create a user account. You just fire off the exploit. In my exploit you just type in the IP address and you hit enter. It is a bit slow so I didn't choose to use that method while on stage. Now in the center of the tree is the user area. These vulnerabilities are less valuable but are still useful for exploiting the system. This level ‑‑ there are a number of vulnerabilities here, but it's not as rich. At the very top of the tree is the administrative area. And that is ripe with vulnerabilities. Very few researchers have gone after vulnerabilities in this area because it requires a lot to build off of. So in this case, OASP A1 injection. Exploitability, easy. Yes, very easy. Impact severe. Yes. It is also severe. I agree. Detectability average. In this case, detectability ‑‑ if you fire off a canned tool like Acunetix or the open source with PD, you will not find this vulnerability. Instead, I had to find it using more of a manual source code analysis. It turns out they have a function call called filter. If you pass in filter with 1, filter handles both XSS and SQL injection, which is terrible. When I see this behavior, it's a dead flag. These two vulnerabilities have very little in common, and there is ‑‑ they share almost no control characters. You should not be mixing these two filters. So if someone is doing this, then they probably don't understand what's going on. In this case, that is very true. By passing in 1, it would still prevent XSS, but it made it vulnerable to SQL injection. I wrote up a simple regular expression in order to find any case where a filter was being passed 1. And that's how I was able to find this. Now, another thing, broken authentication session management. Technical impact, severe, yes, we're able to get administrative access. Exploitability, average, I'd say it's very easy. Detectability, average, that could also be easy. How I found this flaw is I was searching the code base for set cookie. Any time I see set cookie, about 9 times out of 10, when an application is doing a set cookie, it's vulnerable. You should not be handling your own session identifier. In PHP, there's session start, which is a far more secure approach. There have been vulnerabilities found in PHP's session management, even this year. But they were more minor and easy to patch. Whereas if you write your own session, such as what PHP nuke did, you're going to get hacked. 9 times out of 10. So this is what it looks like. In the code at the top, PHP nuke is pulling out the password and doing a comparison or pulling out the password based on the user name and just doing this raw string comparison. This is equivalent to having a plain text user name and password. We don't actually have to break that. We don't have to break the hash in order to log in. They are using MD5. MD5 is proven to be insecure. It's no longer supported by NIST and it's easy to generate collisions. Collisions have been generated. SHA-1, on the other hand, is still approved by NIST, and that's because no one has generated a collision, although there have been vulnerabilities found against it. SHA-256 is an ideal choice. In this case, at the bottom, an example of a secure logon, we're doing assaulted SHA-256 hash and using parameterized queries. The code I used to generate the cookie, it's very simple. Like I said, it's concatenating the user ID and the password with a colon and then base64 encoding it. Now another thing. PHP's default configuration is horribly insecure. One thing that it has is display errors equals on. And this allows the attacker to test your application for SQL injection or even find remote paths. There are other problems with PHP's default configuration. On every platform I've ever looked at. So on for a development system, in the most cases that's okay. However, on a production level system, you must run PHP sec info. PHP sec info is a very simple PHP script. It goes through and looking at your configurations and tells you what's wrong. If there's any red in your PHP sec info, you have to remove it. You should remove as much yellow as possible. And in most cases, you can remove all yellow all together. But okay. Now there's another thing. I needed two SQL injection exploits in order to ultimately get remote code execution. Why did I need two SQL injection exploits? Also, filter bypassing. In order to exploit some of these vulnerabilities, there is actually a makeshift web application firewall built in. And I had to bypass that as well. So we're going to go into my VM. And, oh. Okay. First thing I'm going to show you is a trick on filter bypassing. So oftentimes there are regular expressions used to try and match bad input. In this case, this regular expression was being used to match input. And it's kind of cryptic what's being used, although it's simplistic. You could figure this out. But regex buddy makes it really easy. What it's doing is it's matching this code in the input. And it's matching this in the center. So what part of that is being matched? Using the debug feature ‑‑ using the debug feature, it literally allows us to step through a regular expression. So I It starts off and shows us a number of fails but ultimately when we get to step 32 it actually finds a part of the string that is being matched. So we notice that it's the parentheses that's being matched and ultimately at the very end it matches even shows us what part of the query is being matched. So for instance when we're selecting the center it shows that part. When we actually select the parentheses it's showing the beginning of the regular expression. Ultimately at the end we have what's actually being matched. In this case we can use some encoding to bypass this. Specifically URL encode. What's happening with the variable is it's calling a URL decode prior to its use. So what we can do is we can paste in the original code that's being matched and URL encode it again. What's happening with all PHP requests is there's an impasse. There's an implicit URL encode or URL decode. All input is URL decoded at least once in order to preserve the HTTP protocol. This is also useful for bypassing Magic Quotes GPC because if there's any case of URL encode it's simple to decode it again or it's simple to in this case the very beginning we're printing out a quote mark. We URL encode it once and then URL encode it twice. So in this case the URL encode of a single quote is 27 and then it turns into a 2527. The 25 is the percent mark that's actually URL encoded. Now so one important thing to note is that with MySQL exploitation you can both read and write files sometimes. There's the file privileges in MySQL which is the most devastating privilege you can give. It's more devastating than grant. The reason why is because you can't turn a select or insert into a grant statement. However, you can use file I.O. So in this case we have a very simple select statement and then we can tack on a union and we can union. We can union a very simple backdoor such as PHP eval git e which is very similar to our front end. Then we can say into add file. Now this is an important point. Under Ubuntu there's app armor. And so historically the bread and butter for exploiting LAMP systems was to union select a PHP code into the web root. Well this no longer works. If you go to slash VAR and say some random file that PHP, we're going to get an error. Not a syntax error. there. Error code 13. That's pretty cryptic. Basically it's a general error. It's a fail. It's saying that we can't write to that directory. But why? Well, if we pull up the app armor rule set from MySQL, MySQL D, what this is doing is adding another layer of permissions on top of your usual chmod. In this case, it's pretty simple. It's just saying it's giving it read write or read access. Now, one important thing to note are these includes at the very top. What these includes are doing is it's allowing this particular include is user temp. This is giving it read write access to slash TMP. So this is a safe location where we can write files. This vulnerability in app armor is systemic. It affects as far as I know all installs of app armor are affected by this. It is very common that a process needs to write to temp because it is a common requirement. So it's commonly that processes will have a ‑‑ will be including this user temp file. But this whole idea ‑‑ So app armor is trying to separate processes. But by creating header files, you're creating unions or places where two processes have overlapping ‑‑ have an overlap where we can write to a directory slash temp and then we can execute that file. So in our union select ‑‑ If we change it to ‑‑ If we change the location to slash temp, the query succeeds. Well, okay. So we're able to upload PHP code. But now what? Okay. Well, using a local file include vulnerability, we're able to execute this code. So basically with a local file include, we're able to give it a path of PHP code and have it executed. So using this very simple chain, we use SQL injection to be able to drop a file and then we can use a local file include in order to execute that file. And this bypasses app armor. So far I haven't heard back from the app armor team, but Ubuntu is very interested in this vulnerability. Their bug ‑‑ their bug response team is fantastic. I would ‑‑ I just like talking to them. There are cases where ‑‑ it's hard to say who has a better bug response, either Google or Canonical. Ten minutes. But I would ‑‑ I would audit their software for free just to talk to their bug team. It's that good. Okay. So now there's another filter in this case. It's looking for union. In simple cases of union, percent 20, union with comment marks or just union. Well, there's one thing that's very simple. What about union with only a comment on one side of the union of the . This is very simple. I'm is literally just a very minor change to the ‑‑ to my query. I was able to bypass this. Very simple filter. Now there's other cases. So what if this application didn't have a broken authentication in session management? There are other ways to gain access to the administrative area. That is using CSRF. Using CSRF, if we can trick the administrator to click on a link while he's logged in, then we can force him to carry out the request. So we can force him to carry out both the SQL injection and the local and file include without having to worry about the consequence ‑‑ without having to worry about the first part of the chain, which I did manually. Now, there's another thing. This application doesn't have any CSRF protection in the entire ‑‑ anywhere. Absolutely anywhere. But if it did, you can use XSS in order to gain CSRF. This attack was actually used by the SAMI worm where it was able to use XML HTTP request to pull out the CSRF token to force the request. Now there's another attack, DNS rebinding, which is very popular from Dan Kaminski. This would also allow us to attack this administrative interface. So I wrote an exploit against PHPMyAdmin in which they were ‑‑ they did have very good CSRF protection for most variables. They did, however, have a whitelist here where any variable name on this list could be sent from any domain. It was exempt from the CSRF token. So in this case, DB and table, I used ‑‑ I used ‑‑ were actually vulnerable to SQL injection. This is not ‑‑ I can't tell you how many times I've used this. I couldn't just execute any query, but I was able to influence a query that was being built. So in this case, DB has to be set to any database, in this case, information schema. And table, I'm filling off the query. This is doing a union select in the app file. So using local file include, we can gain remote code execution or remote system. App arm will not allow us to write ‑‑ anyway, there's another thing to note. SELinux is far more secure than app armor. And SELinux will throw this error when trying to pull off this attack. Although it will allow MySQL to write to slash temp, what it does is it looks at the source ‑‑ it looks at the owner of the file. So it's saying, hey, MySQL owns this file, but it's trying to be executed by HTTPD and throws an error. Now, there's an important thing to note. SELinux throws a whole lot of errors. It throws errors against ‑‑ in fact, it will not even let you install PHP nuke because of ‑‑ because of errors. You have to add an exception. It's common, because of this restriction, that people just get upset with SELinux and install it all together. And that is a flaw in SELinux. I will admit that. Although it breaks my exploits. And it breaks this exploit in particular. But under the 7.0 branch, there is a very nice eval remote code execution. I was running an old version of PHP BB using preg replace slash e. With the slash e modifier, it allows you to eval whatever parameters are passed to it. So in this case, this actually does work with SELinux. We don't have to introduce new code. It's already there. We can just ‑‑ we can just access it. Now there's a very cool paper, a study in Scarlett. This paper is a bit old. But when I read this, it was a precursor to writing my first exploit. I highly recommend that you grab a copy of this paper. It goes over a lot of the syncs and the other things that it does. And problems with PHP, such as preg replace slash e. Anyway, end. That is my talk. Thank you.