1 00:00:00,000 --> 00:00:05,140 This talk is Max Exploitation and I'm the Rook. What we'll be 2 00:00:05,140 --> 00:00:07,960 talking about today is remote code execution vulnerabilities 3 00:00:08,480 --> 00:00:15,760 specifically against PHP nuke. In this case we have a PHP nuke 4 00:00:15,760 --> 00:00:17,800 install running on a virtual machine and we'll be breaking 5 00:00:17,800 --> 00:00:18,220 into it. 6 00:01:09,120 --> 00:01:14,720 My exploit was released back in May. This is the code here. 7 00:01:14,940 --> 00:01:17,780 It's very simple. In this case we're going to be 8 00:01:18,760 --> 00:01:21,720 giving it a cookie. It's .php, it's fire and forget. We give it 9 00:01:21,720 --> 00:01:25,480 a domain name. In this case we have to give it a cookie. The 10 00:01:25,980 --> 00:01:29,620 reason why we're giving it a cookie is because we'll go into 11 00:01:29,620 --> 00:01:33,640 it in a bit. In earlier versions this exploit spans three 12 00:01:33,640 --> 00:01:40,980 different versions. 7.0, 8.135, 8.1. It's very simple. Hit enter. 13 00:01:51,800 --> 00:02:14,820 I apologize. One moment. The configuration was incorrect. 14 00:02:34,520 --> 00:02:38,320 Okay. All right. It's using the cookie we provided it, which is a 15 00:02:38,320 --> 00:02:47,780 user cookie. Fuck. . Fuck. . 16 00:02:50,920 --> 00:03:02,540 Anyway. It's uploading a back door. And finally exploits it. 17 00:03:05,800 --> 00:03:10,420 Right now it uploaded a very simple eval into front end .php. 18 00:03:10,500 --> 00:03:12,920 It dropped this php file. The reason why I chose this is because 19 00:03:12,920 --> 00:03:17,560 there's a back end .php and I figured it would be fitting to have a front end. Now, 20 00:03:17,560 --> 00:03:21,960 Now what we're going to do is recently, just after I released this exploit, Metasploit 21 00:03:21,960 --> 00:03:27,920 released a very nice reverse shell for PHP. So in this case it's the exploit slash Unix 22 00:03:27,920 --> 00:03:33,060 slash web app PHP eval. We set a local host and our host and our port and you are a path 23 00:03:33,060 --> 00:03:40,460 to the front end we just dropped. And then type exploit. Now it's dropped me to a shell 24 00:03:40,460 --> 00:04:03,600 so you name it dash A. So what was the fallout from some of this attack? What I really wanted 25 00:04:03,600 --> 00:04:09,840 was a very simple to use exploit that affected many systems. Here is a Malaysian government 26 00:04:09,840 --> 00:04:10,260 website. 27 00:04:10,260 --> 00:04:15,380 I'm not going to try and pronounce that. But about a week after releasing my exploit it 28 00:04:15,380 --> 00:04:23,100 showed up on zone H, defaced. Yeah, popping a shell on a .gov. The very next day, defaced 29 00:04:23,100 --> 00:04:27,180 again by the same crew. So then what would they do? They would go to the vendor's website 30 00:04:27,180 --> 00:04:34,260 to get a patch, correct? Vendor got hacked. Phpnuke.org . Thank you. 31 00:04:35,800 --> 00:04:40,140 So what happened here, the vendor's website wasn't defaced like the others. Instead it 32 00:04:40,140 --> 00:04:44,380 added an iframe. In fact, most of the websites, they added this iframe to the bottom of the 33 00:04:44,380 --> 00:04:49,920 page. And what this is doing is it's a part of the Eleanor exploit pack. And in Eleanor, 34 00:04:50,080 --> 00:04:58,680 it tries a number of exploits trying to exploit Flash, IE, or Java. It is believed from some 35 00:04:58,680 --> 00:05:02,260 of the write-up of these antivirus companies, it's believed that the same ‑‑ a few 36 00:05:02,260 --> 00:05:07,520 days after phpnuke.org was owned, the U.S. Treasury website was also owned. And it's 37 00:05:07,520 --> 00:05:10,120 believed that these were the same people behind this attack. And it's believed that these were the same people behind this attack. 38 00:05:12,220 --> 00:05:15,600 And they were using that because they can do all of these things. So then it was called 39 00:05:15,600 --> 00:05:22,100 the Eleanor Exploit Pack. And it was one point two million in size. This is how modern systems 40 00:05:22,100 --> 00:05:27,040 are broken into. You deface websites. You add an iframe. And you start hacking into 41 00:05:27,040 --> 00:05:34,440 clients. So why do this? There's a very important quote by Richard Feynman. He's a Nobel Prize 42 00:05:34,440 --> 00:05:38,540 winning physicist, one of the fathers of the bomb. What I cannot understand ‑‑ what 43 00:05:38,540 --> 00:05:40,120 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. 44 00:05:40,120 --> 00:05:46,100 understand. So the problem with security today is that there are a lot of white hats that 45 00:05:46,100 --> 00:05:51,900 don't understand the exploitation process, yet they're building systems to stop it. These 46 00:05:51,900 --> 00:05:56,900 systems are fundamentally flawed. I don't care if you're the most Aryan of white hats, 47 00:05:57,020 --> 00:06:05,460 you must understand the exploitation process. So there are two other quotes. Layers of security. 48 00:06:05,460 --> 00:06:09,160 I mean, this is something that's a part of nature and something that's been probably 49 00:06:09,160 --> 00:06:13,360 prehistory. And then there's Bruce Schneier. Complexity is the worst enemy of security. 50 00:06:13,760 --> 00:06:20,120 Now these two ideas are at conflict because by adding layers, you're also adding complexity 51 00:06:20,120 --> 00:06:25,460 to the system. And this level of complexity is vulnerable to attack. For instance, there 52 00:06:25,460 --> 00:06:29,400 are buffer overflows found in anti-viruses and even firewalls are vulnerable to attack. 53 00:06:29,640 --> 00:06:34,420 In this case, Managed Engine Firewall Analyzer 5, I found two vulnerabilities in the software, 54 00:06:34,600 --> 00:06:35,440 CSRF and the CSRF. 55 00:06:36,460 --> 00:06:42,300 The problem is, is that on this firewall, you're able to execute SQL queries, very similar 56 00:06:42,300 --> 00:06:48,240 to phpMyAdmin. So in this same request, I could execute SQL on the database and the 57 00:06:48,240 --> 00:06:53,980 results from that SQL could be JavaScript and could be executed on the client. This 58 00:06:53,980 --> 00:06:59,400 is one request that fails twice. But they're not alone. There's ProFence Web Application 59 00:06:59,400 --> 00:07:03,720 Firewall and their catch line is defenses against all of the OWASP top ten. Well, someone 60 00:07:03,720 --> 00:07:05,420 should tell them that CSRF and XSRF are the same thing. They're the same thing. They're 61 00:07:05,420 --> 00:07:12,380 the same thing. They're the same thing. You can do stuff like change what server it's 62 00:07:12,380 --> 00:07:16,900 going through. It acts as an intermediary between the Internet and the Web application. 63 00:07:17,160 --> 00:07:20,460 So you can just change what Web site it's being hosted at. Or you could shut it down 64 00:07:20,460 --> 00:07:24,760 altogether. But what we're talking about is phpNuke. 65 00:07:25,060 --> 00:07:33,040 And one thing that I've ‑‑ this exploit spans almost ten years now of releases from 66 00:07:33,480 --> 00:07:35,400 November 2004 until now. Currently, it's only been a couple of years. It's only been a couple 67 00:07:35,400 --> 00:07:39,160 of years. This is unpatched. Although the vendor's Web site hasn't been hacked so there 68 00:07:39,160 --> 00:07:44,080 may be a patch in existence, the patch has not been made public. So anyone running phpNuke 69 00:07:44,080 --> 00:07:47,600 is currently vulnerable to this exploit. And the exploit code is on your DEF CON CD. 70 00:07:50,920 --> 00:07:56,260 So one thing to note, though, is that in the phpNuke 7.0 branch, there were less exploits 71 00:07:56,260 --> 00:08:00,960 required, less vulnerabilities required in order to gain remote code execution. If you 72 00:08:00,960 --> 00:08:02,760 can see on the right ‑‑ 73 00:08:02,760 --> 00:08:11,080 I ‑‑ there was more level of complexity. As time goes by, the application itself has 74 00:08:11,080 --> 00:08:18,440 gotten more secure. But what we're going to focus on is this chain here. Affecting 75 00:08:18,440 --> 00:08:23,400 8.135. So we're using SQL injection to obtain administrative credentials out of the database. 76 00:08:23,800 --> 00:08:27,820 We use those administrative credentials to leverage a broken authentication and session 77 00:08:27,820 --> 00:08:32,740 management, OAuth A3. And ultimately to get ‑‑ well, I don't know. I don't know. I don't know. 78 00:08:32,740 --> 00:08:38,200 Then to get information disclosure about the system, which is required for later exploits. 79 00:08:38,400 --> 00:08:43,800 So for these three steps, I can do manually. I'm going to do it in a Web browser. So let's 80 00:08:43,800 --> 00:08:44,460 do it. Demo. 81 00:09:04,900 --> 00:09:08,000 I'm going to clear out my cookies real quick. Just from the beginning. 82 00:09:14,900 --> 00:09:15,300 Okay. 83 00:09:16,380 --> 00:09:16,780 Okay. 84 00:09:31,000 --> 00:09:32,380 Currently we're logged in as a user. 85 00:09:35,360 --> 00:09:39,680 We're going to go to the journal module. We're going to list all journals. 86 00:09:54,040 --> 00:10:02,640 We're going to add a new entry. Test body. And we're going to use the evil 87 00:10:02,740 --> 00:10:10,080 face for this one. Now we're going to fire up tamper data. So often what will happen is if you run a web 88 00:10:10,080 --> 00:10:13,940 application scanner, what it's going to tell you is it's going to give you the variable name that's 89 00:10:13,940 --> 00:10:18,800 vulnerable to SQL injection and what page it's on. Sometimes you can't modify that variable 90 00:10:18,800 --> 00:10:24,200 directly. So we're going to have to use tamper data to modify it. So tamper data is set and it's 91 00:10:24,200 --> 00:10:30,020 going to capture the next query. We're going to tamper that. Mood. Devilish. Well, that's where 92 00:10:30,020 --> 00:10:32,720 the SQL injection is. So we're going to do ‑‑ we're going to do a ‑‑ we're going to do a 93 00:10:32,740 --> 00:10:37,120 test. We're going to start off and we need to figure out how many variables are in this 94 00:10:38,540 --> 00:10:46,400 insert statement. So one, two, three. I know that it's five. But you can count this up. 95 00:10:46,480 --> 00:10:50,640 This can be exploited completely black box. You don't really have to know what's going 96 00:10:50,640 --> 00:10:54,900 on behind it. In this case I'm just counting up one, two, three, four, five and then a 97 00:10:54,900 --> 00:10:59,780 comment to finish off the rest of the query. So as we fire it off, I've modified this application 98 00:10:59,780 --> 00:11:02,220 to print out the query for educational purposes. 99 00:11:02,220 --> 00:11:08,280 But it's not required. It does help sometimes when you're exploiting SQL to print out the 100 00:11:08,280 --> 00:11:27,100 query. So I'm going to fire off this query on phpMyAdmin. And it's being very slow. 101 00:11:51,620 --> 00:11:55,780 VMware can be a bit slow at times. Although it is very useful. One thing about VMware 102 00:11:55,780 --> 00:11:59,680 that was very helpful in this is that it allowed me to break into a virtual system 103 00:11:59,680 --> 00:12:02,140 and to be able to fine tune my exploit. 104 00:12:02,220 --> 00:12:06,180 So that ultimately it could work on the vendor. Although I stress this. I never tested this 105 00:12:06,180 --> 00:12:11,000 on a remote system. Only in virtual environments. And I recommend that you do the same. 106 00:12:34,300 --> 00:12:34,660 Wow. 107 00:12:56,160 --> 00:13:01,120 Okay. Now, here's the important part. Let me zoom in on this. So this is the part that 108 00:13:01,120 --> 00:13:02,100 we injected into the query. 109 00:13:02,220 --> 00:13:06,040 And notice our comment here at the end. And this is commenting off the rest of the query. 110 00:13:06,760 --> 00:13:12,540 What's important to note here is when you're exploiting an insert statement under MySQL, 111 00:13:12,700 --> 00:13:16,220 you can't simply just stack a new query. We can't just say, hey, we want to do an insert 112 00:13:16,220 --> 00:13:19,360 or we want to do ‑‑ we have an insert and we want to turn it into a delete. We 113 00:13:19,360 --> 00:13:22,380 can't do that. Instead, we have to use a different form of exploitation. 114 00:13:24,560 --> 00:13:28,600 So another ‑‑ an important thing to note is that here are the number of columns and 115 00:13:28,600 --> 00:13:30,840 we have to have the number of values to correspond to these columns. 116 00:13:34,960 --> 00:13:43,820 So we're going to add a new entry now. And this time, we want to pull out the administrative 117 00:13:43,820 --> 00:14:02,200 credentials in the ‑‑ in the ‑‑ sorry, sorry, sorry. Fire up tamper data. Tamper. Okay. 118 00:14:06,420 --> 00:14:32,200 One moment. Okay. So the ‑‑ okay. So we're going to do this. So we're going to do this. 119 00:14:32,220 --> 00:14:37,240 Now, the first query that we entered, we entered 1, 2, 3, 4, 5. So how did that respond? 120 00:14:37,380 --> 00:14:41,960 How did the data change? Notice here are the first record and we have 1 and 2. We overwrite 121 00:14:41,960 --> 00:14:45,200 the date and the time. When we click on ‑‑ when we actually click on the title itself, 122 00:14:45,460 --> 00:14:50,560 we see 1 and 2 and 4 and 5. Three went somewhere. We don't know where it is. But what this 123 00:14:50,560 --> 00:14:54,680 is ‑‑ the significance of this is that we know it's not blind SQL injection. We 124 00:14:54,680 --> 00:14:59,360 know that we can control some of these fields. And specifically, 1 and 2 look like great 125 00:14:59,360 --> 00:15:00,300 places to put our data. 126 00:15:01,120 --> 00:15:01,480 So. 127 00:15:05,600 --> 00:15:10,540 Instead of putting in 1 and 2, we're going to select the user name, in this case it's 128 00:15:10,540 --> 00:15:18,680 AID, from ‑‑ we can ‑‑ we have to do a limit 1 because we can only have one 129 00:15:18,680 --> 00:15:25,000 piece of data come out ‑‑ only one row come out from this subselect statement. Subselects 130 00:15:25,000 --> 00:15:29,280 are limited. You don't have a full ‑‑ you don't have full access to all of the operators 131 00:15:30,140 --> 00:15:32,200 available. But what we can do, it allows us to do this. 132 00:15:32,200 --> 00:15:33,440 It allows us to pull data from a different table. 133 00:15:48,320 --> 00:16:02,180 Correct. This is the query that we injected. If we go back to our ‑‑ if we go back 134 00:16:06,180 --> 00:16:13,140 to our ‑‑ it should look something like this. Where we would have the admin, user 135 00:16:13,140 --> 00:16:18,480 name and the password. What this allows us to do is then move on to the next step, which 136 00:16:18,480 --> 00:16:24,660 is be able to create an administrative credential, a cookie for the ‑‑ for this user. So 137 00:16:27,240 --> 00:16:32,020 we can do a make cookie. And what this is, is a very simple ‑‑ a very simple script. 138 00:16:32,200 --> 00:16:38,180 That allows us to create this cookie. Basically, all I'm doing is concatenating the two values 139 00:16:38,180 --> 00:16:51,500 together and sending it to base 64 encode. Now, if we go into the min.php, it's asking 140 00:16:51,500 --> 00:16:56,700 for user name and password. What we're going to do is using JavaScript in the URL, we can 141 00:16:56,700 --> 00:17:02,180 set the administrative credential. Admin equals this value. Hit enter. Basically, this is 142 00:17:02,200 --> 00:17:06,940 just telling us, yes, we've set that variable. It's still asking us for the user name and 143 00:17:06,940 --> 00:17:09,680 password. Refresh. Now we're the administrator. 144 00:17:18,420 --> 00:17:24,740 Now, if we move into the preferences, what we can do is general ‑‑ what we can 145 00:17:24,740 --> 00:17:30,160 do is, by default, what PHP Nuke is doing is it's preventing all errors from reaching 146 00:17:30,160 --> 00:17:32,100 the user. This is actually an important step. 147 00:17:32,200 --> 00:17:36,680 Because errors give us important information about the application. In some cases, like 148 00:17:36,680 --> 00:17:40,480 if you're testing for SQL injection, it's vital that error reporting is turned on because 149 00:17:41,080 --> 00:17:45,740 it's a very good way to detect that SQL injection is there. It also allows us to get something 150 00:17:45,740 --> 00:17:50,000 even more important in this case, which is the local path. So we're going to enable error 151 00:17:50,000 --> 00:17:58,140 reporting. And it immediately vomits up on us. But the most important part is here. Is 152 00:17:58,140 --> 00:18:01,380 this. Now, there is actually a problem with this install. 153 00:18:01,380 --> 00:18:07,080 And that's why it's displaying so many errors. It shouldn't normally do this. Instead, what 154 00:18:07,080 --> 00:18:11,360 we would have to do is trigger the error. And in that case, what we have to do is ‑‑ 155 00:18:23,560 --> 00:18:30,700 there is an easy way to trigger error under PHP. And that is by passing an array to many 156 00:18:30,700 --> 00:18:31,140 functions. 157 00:18:31,380 --> 00:18:37,020 In this case, we're going to use MD5, but it could be HTML special characters or a number 158 00:18:37,020 --> 00:18:40,800 of functions. If you pass an array and it's expecting to see a string, it will vomit 159 00:18:40,800 --> 00:18:48,700 up and throw an error. So we're going to make a bunk user. We're going to use tamper 160 00:18:48,700 --> 00:18:56,940 data again. Start tamper. We're going to add a new user. We only want to tamper this 161 00:18:56,940 --> 00:18:59,960 one request. So admin add pass is going to be passed to MD5. 162 00:19:01,000 --> 00:19:05,380 Instead, we're going to create a new. We're going to say add pass. And then we're going 163 00:19:05,380 --> 00:19:18,280 to add the brackets to it, which now makes it an array. Chunk. Click okay. So, warning, 164 00:19:18,460 --> 00:19:22,680 MD5 expects parameter 1 to be a string array given. And it gives us the path, which is 165 00:19:22,680 --> 00:19:24,620 vital. So ‑‑ 166 00:19:28,340 --> 00:19:28,720 Okay. 167 00:19:28,720 --> 00:19:28,780 Okay. 168 00:19:28,940 --> 00:19:29,320 Okay. 169 00:19:29,320 --> 00:19:29,360 Okay. 170 00:19:33,920 --> 00:19:40,740 So now something about php nuke. It's been hacked a lot. It has had a very ‑‑ it 171 00:19:40,740 --> 00:19:45,140 has a very poor history of security. But because it's been hacked so many times that it has 172 00:19:45,140 --> 00:19:48,500 progressed, it is ‑‑ it has been made better. 173 00:19:48,840 --> 00:19:53,340 Although I'm not the first to exploit at least the administrative credentials part, the administrative 174 00:19:53,340 --> 00:19:59,340 area has been locked down. In earlier versions of php nuke, there were versions where you 175 00:19:59,340 --> 00:20:03,540 was the ability to just modify the HTML on the front page. So you could just add a malicious 176 00:20:03,540 --> 00:20:07,740 iframe. Currently we don't have that ability. There was even the ability to just upload 177 00:20:07,740 --> 00:20:11,920 PHP files and execute them. Well, that is very ‑‑ that was also removed because 178 00:20:11,920 --> 00:20:16,920 so many people were gaining administrative access. In this analogy, the lowest hanging 179 00:20:16,920 --> 00:20:22,280 fruit represented by the apple at the bottom of the tree is a vulnerability that doesn't 180 00:20:22,280 --> 00:20:32,580 require much user interaction. In fact, in 7.0 to 8.0 all the way up to 8.1, there was 181 00:20:32,580 --> 00:20:36,760 an ability to get blind SQL injection in the refer parameter. You didn't have to create 182 00:20:36,760 --> 00:20:41,020 a user account. You just fire off the exploit. In my exploit you just type in the IP address 183 00:20:41,020 --> 00:20:47,720 and you hit enter. It is a bit slow so I didn't choose to use that method while on stage. 184 00:20:48,840 --> 00:20:51,400 Now in the center of the tree is the user area. 185 00:20:51,400 --> 00:20:56,180 These vulnerabilities are less valuable but are still useful for exploiting the system. 186 00:20:56,880 --> 00:21:03,380 This level ‑‑ there are a number of vulnerabilities here, but it's not as rich. At the very top 187 00:21:03,380 --> 00:21:08,000 of the tree is the administrative area. And that is ripe with vulnerabilities. Very few 188 00:21:08,000 --> 00:21:12,540 researchers have gone after vulnerabilities in this area because it requires a lot to 189 00:21:13,480 --> 00:21:20,120 build off of. So in this case, OASP A1 injection. Exploitability, easy. Yes, very easy. Impact 190 00:21:20,120 --> 00:21:21,300 severe. Yes. 191 00:21:21,300 --> 00:21:27,520 It is also severe. I agree. Detectability average. In this case, detectability ‑‑ if 192 00:21:27,520 --> 00:21:31,820 you fire off a canned tool like Acunetix or the open source with PD, you will not find 193 00:21:31,820 --> 00:21:38,980 this vulnerability. Instead, I had to find it using more of a manual source code analysis. 194 00:21:39,400 --> 00:21:44,860 It turns out they have a function call called filter. If you pass in filter with 1, filter 195 00:21:44,860 --> 00:21:50,680 handles both XSS and SQL injection, which is terrible. When I see this behavior, it's 196 00:21:51,300 --> 00:21:57,000 a dead flag. These two vulnerabilities have very little in common, and there is ‑‑ they 197 00:21:57,940 --> 00:22:03,160 share almost no control characters. You should not be mixing these two filters. So if someone 198 00:22:03,160 --> 00:22:07,920 is doing this, then they probably don't understand what's going on. In this case, that is very 199 00:22:07,920 --> 00:22:12,680 true. By passing in 1, it would still prevent XSS, but it made it vulnerable to SQL injection. 200 00:22:13,140 --> 00:22:17,860 I wrote up a simple regular expression in order to find any case where a filter was 201 00:22:17,860 --> 00:22:20,700 being passed 1. And that's how I was able to find this. 202 00:22:21,300 --> 00:22:25,460 Now, another thing, broken authentication session management. Technical impact, severe, 203 00:22:25,720 --> 00:22:29,220 yes, we're able to get administrative access. Exploitability, average, I'd say it's very 204 00:22:29,220 --> 00:22:34,220 easy. Detectability, average, that could also be easy. How I found this flaw is I 205 00:22:34,220 --> 00:22:39,380 was searching the code base for set cookie. Any time I see set cookie, about 9 times out 206 00:22:39,380 --> 00:22:43,960 of 10, when an application is doing a set cookie, it's vulnerable. You should not be 207 00:22:43,960 --> 00:22:49,840 handling your own session identifier. In PHP, there's session start, which is a far more 208 00:22:49,840 --> 00:22:50,540 secure approach. 209 00:22:51,300 --> 00:22:55,380 There have been vulnerabilities found in PHP's session management, even this year. 210 00:22:55,640 --> 00:23:00,720 But they were more minor and easy to patch. Whereas if you write your own session, such 211 00:23:00,720 --> 00:23:04,340 as what PHP nuke did, you're going to get hacked. 9 times out of 10. 212 00:23:05,320 --> 00:23:11,220 So this is what it looks like. In the code at the top, PHP nuke is pulling out the password 213 00:23:11,220 --> 00:23:15,520 and doing a comparison or pulling out the password based on the user name and just doing 214 00:23:15,520 --> 00:23:19,720 this raw string comparison. This is equivalent to having a plain text user name and password. 215 00:23:19,920 --> 00:23:21,280 We don't actually have to break that. 216 00:23:21,300 --> 00:23:25,860 We don't have to break the hash in order to log in. They are using MD5. MD5 is proven 217 00:23:25,860 --> 00:23:29,960 to be insecure. It's no longer supported by NIST and it's easy to generate collisions. 218 00:23:30,240 --> 00:23:34,040 Collisions have been generated. SHA-1, on the other hand, is still approved by NIST, 219 00:23:34,160 --> 00:23:38,220 and that's because no one has generated a collision, although there have been vulnerabilities 220 00:23:38,220 --> 00:23:44,300 found against it. SHA-256 is an ideal choice. In this case, at the bottom, an example of 221 00:23:44,300 --> 00:23:51,240 a secure logon, we're doing assaulted SHA-256 hash and using parameterized queries. The 222 00:23:51,300 --> 00:24:01,240 code I used to generate the cookie, it's very simple. Like I said, it's concatenating 223 00:24:01,240 --> 00:24:06,720 the user ID and the password with a colon and then base64 encoding it. 224 00:24:07,540 --> 00:24:12,640 Now another thing. PHP's default configuration is horribly insecure. One thing that it has 225 00:24:12,640 --> 00:24:16,840 is display errors equals on. And this allows the attacker to test your application for 226 00:24:16,840 --> 00:24:21,280 SQL injection or even find remote paths. There are other problems with PHP's default configuration. 227 00:24:21,300 --> 00:24:29,260 On every platform I've ever looked at. So on for a development system, in the most 228 00:24:29,260 --> 00:24:38,180 cases that's okay. However, on a production level system, you must run PHP sec info. PHP 229 00:24:38,180 --> 00:24:42,360 sec info is a very simple PHP script. It goes through and looking at your configurations 230 00:24:42,360 --> 00:24:48,560 and tells you what's wrong. If there's any red in your PHP sec info, you have to remove 231 00:24:48,560 --> 00:24:51,280 it. You should remove as much yellow as possible. 232 00:24:51,280 --> 00:24:54,220 And in most cases, you can remove all yellow all together. 233 00:24:55,580 --> 00:25:00,420 But okay. Now there's another thing. I needed two SQL injection exploits in order to ultimately 234 00:25:00,420 --> 00:25:06,220 get remote code execution. Why did I need two SQL injection exploits? Also, filter bypassing. 235 00:25:06,440 --> 00:25:11,480 In order to exploit some of these vulnerabilities, there is actually a makeshift web application 236 00:25:11,480 --> 00:25:17,660 firewall built in. And I had to bypass that as well. So we're going to go into my VM. 237 00:25:22,000 --> 00:25:31,200 And, oh. Okay. First thing I'm going to show you is a trick on filter bypassing. So oftentimes 238 00:25:31,200 --> 00:25:37,360 there are regular expressions used to try and match bad input. In this case, this regular 239 00:25:37,360 --> 00:25:42,540 expression was being used to match input. And it's kind of cryptic what's being used, 240 00:25:42,600 --> 00:25:47,460 although it's simplistic. You could figure this out. But regex buddy makes it really 241 00:25:47,460 --> 00:25:51,260 easy. What it's doing is it's matching this code in the input. And it's matching this 242 00:25:51,280 --> 00:25:58,280 in the center. So what part of that is being matched? Using the debug feature ‑‑ using 243 00:26:08,480 --> 00:26:20,460 the debug feature, it literally allows us to step through a regular expression. So I 244 00:26:21,280 --> 00:26:28,580 It starts off and shows us a number of fails but ultimately when we get to step 32 it actually 245 00:26:28,580 --> 00:26:31,520 finds a part of the string that is being matched. 246 00:26:32,660 --> 00:26:37,600 So we notice that it's the parentheses that's being matched and ultimately at the very end 247 00:26:38,080 --> 00:26:41,620 it matches even shows us what part of the query is being matched. 248 00:26:41,740 --> 00:26:44,120 So for instance when we're selecting the center it shows that part. 249 00:26:44,260 --> 00:26:47,480 When we actually select the parentheses it's showing the beginning of the regular expression. 250 00:26:49,080 --> 00:26:55,660 Ultimately at the end we have what's actually being matched. 251 00:26:55,980 --> 00:27:00,220 In this case we can use some encoding to bypass this. 252 00:27:02,280 --> 00:27:03,860 Specifically URL encode. 253 00:27:04,120 --> 00:27:07,700 What's happening with the variable is it's calling a URL decode prior to its use. 254 00:27:08,140 --> 00:27:12,760 So what we can do is we can paste in the original code that's being matched and URL encode it 255 00:27:12,760 --> 00:27:13,000 again. 256 00:27:13,860 --> 00:27:17,280 What's happening with all PHP requests is there's an 257 00:27:17,280 --> 00:27:17,460 impasse. 258 00:27:17,480 --> 00:27:20,360 There's an implicit URL encode or URL decode. 259 00:27:20,540 --> 00:27:27,420 All input is URL decoded at least once in order to preserve the HTTP protocol. 260 00:27:31,180 --> 00:27:38,200 This is also useful for bypassing Magic Quotes GPC because if there's any case of URL encode 261 00:27:38,200 --> 00:27:47,160 it's simple to decode it again or it's simple to in this case the very beginning we're printing 262 00:27:47,160 --> 00:27:47,720 out a quote mark. 263 00:27:48,020 --> 00:27:50,600 We URL encode it once and then URL encode it twice. 264 00:27:51,400 --> 00:27:56,840 So in this case the URL encode of a single quote is 27 and then it turns into a 2527. 265 00:27:57,320 --> 00:28:00,320 The 25 is the percent mark that's actually URL encoded. 266 00:28:03,380 --> 00:28:17,020 Now so one important thing to note is that with MySQL exploitation you can both read 267 00:28:17,020 --> 00:28:18,160 and write files sometimes. 268 00:28:18,920 --> 00:28:23,820 There's the file privileges in MySQL which is the most devastating privilege you can 269 00:28:23,820 --> 00:28:24,020 give. 270 00:28:24,200 --> 00:28:25,500 It's more devastating than grant. 271 00:28:25,740 --> 00:28:30,060 The reason why is because you can't turn a select or insert into a grant statement. 272 00:28:30,280 --> 00:28:33,620 However, you can use file I.O. 273 00:28:33,660 --> 00:28:43,460 So in this case we have a very simple select statement and then we can tack on a union 274 00:28:45,320 --> 00:28:46,540 and we can union. 275 00:28:46,540 --> 00:28:56,780 We can union a very simple backdoor such as PHP eval git e which is very similar to our 276 00:28:56,780 --> 00:28:57,180 front end. 277 00:28:57,920 --> 00:28:59,400 Then we can say into add file. 278 00:29:00,240 --> 00:29:03,880 Now this is an important point. 279 00:29:05,060 --> 00:29:07,400 Under Ubuntu there's app armor. 280 00:29:07,960 --> 00:29:13,660 And so historically the bread and butter for exploiting LAMP systems was to union select 281 00:29:13,660 --> 00:29:16,420 a PHP code into the web root. 282 00:29:16,540 --> 00:29:17,840 Well this no longer works. 283 00:29:18,500 --> 00:29:24,220 If you go to slash VAR and say some random file that PHP, we're going to get an error. 284 00:29:30,220 --> 00:29:31,240 Not a syntax error. 285 00:29:31,240 --> 00:29:52,640 there. Error code 13. That's pretty cryptic. Basically it's a general error. It's a fail. 286 00:29:53,360 --> 00:30:00,580 It's saying that we can't write to that directory. But why? Well, if we pull up the app armor 287 00:30:00,580 --> 00:30:11,280 rule set from MySQL, MySQL D, what this is doing is adding another layer of permissions 288 00:30:11,280 --> 00:30:16,900 on top of your usual chmod. In this case, it's pretty simple. It's just saying it's 289 00:30:16,900 --> 00:30:20,960 giving it read write or read access. Now, one important thing to note are these includes 290 00:30:20,960 --> 00:30:29,520 at the very top. What these includes are doing is it's allowing this particular include is 291 00:30:29,520 --> 00:30:30,060 user temp. 292 00:30:30,060 --> 00:30:34,960 This is giving it read write access to slash TMP. So this is a safe location where we can 293 00:30:34,960 --> 00:30:41,760 write files. This vulnerability in app armor is systemic. It affects as far as I know all 294 00:30:41,760 --> 00:30:47,100 installs of app armor are affected by this. It is very common that a process needs to 295 00:30:47,100 --> 00:30:55,020 write to temp because it is a common requirement. So it's commonly that processes will have 296 00:30:55,020 --> 00:30:57,860 a ‑‑ will be including this user temp file. 297 00:30:58,120 --> 00:30:59,620 But this whole idea ‑‑ 298 00:31:00,060 --> 00:31:04,980 So app armor is trying to separate processes. But by creating header files, you're creating 299 00:31:04,980 --> 00:31:10,840 unions or places where two processes have overlapping ‑‑ have an overlap where 300 00:31:10,840 --> 00:31:16,800 we can write to a directory slash temp and then we can execute that file. So in our union 301 00:31:16,800 --> 00:31:17,560 select ‑‑ 302 00:31:27,320 --> 00:31:29,840 If we change it to ‑‑ 303 00:31:39,580 --> 00:31:45,820 If we change the location to slash temp, the query succeeds. Well, okay. So we're 304 00:31:45,820 --> 00:31:48,280 able to upload PHP code. But now what? 305 00:31:59,520 --> 00:32:00,040 Okay. 306 00:32:00,060 --> 00:32:08,960 Well, using a local file include vulnerability, we're able to execute this code. So basically 307 00:32:08,960 --> 00:32:13,640 with a local file include, we're able to give it a path of PHP code and have it executed. 308 00:32:13,960 --> 00:32:18,380 So using this very simple chain, we use SQL injection to be able to drop a file and then 309 00:32:18,380 --> 00:32:22,400 we can use a local file include in order to execute that file. And this bypasses app armor. 310 00:32:22,720 --> 00:32:29,500 So far I haven't heard back from the app armor team, but Ubuntu is very interested in this vulnerability. 311 00:32:29,500 --> 00:32:35,140 Their bug ‑‑ their bug response team is fantastic. I would ‑‑ I just like 312 00:32:35,140 --> 00:32:40,640 talking to them. There are cases where ‑‑ it's hard to say who has a better bug response, 313 00:32:40,840 --> 00:32:42,140 either Google or Canonical. 314 00:32:42,700 --> 00:32:43,280 Ten minutes. 315 00:32:49,980 --> 00:32:53,740 But I would ‑‑ I would audit their software for free just to talk to their bug team. It's 316 00:32:53,740 --> 00:32:54,080 that good. 317 00:32:55,820 --> 00:32:56,220 Okay. 318 00:33:15,980 --> 00:33:18,300 So now there's another filter in this case. 319 00:33:18,460 --> 00:33:25,020 It's looking for union. In simple cases of union, percent 20, union with comment marks 320 00:33:25,020 --> 00:33:25,760 or just union. 321 00:33:26,220 --> 00:33:30,740 Well, there's one thing that's very simple. What about union with only a comment on one 322 00:33:30,740 --> 00:33:38,540 side of the union of the . This is very simple. I'm is literally 323 00:33:39,000 --> 00:33:44,580 just a very minor change to the ‑‑ to my query. I was able to bypass this. Very 324 00:33:44,580 --> 00:33:50,920 simple filter. Now there's other cases. So what if this application didn't have a broken 325 00:33:50,920 --> 00:33:55,080 authentication in session management? There are other ways to gain access to the administrative 326 00:33:56,220 --> 00:34:03,300 area. That is using CSRF. Using CSRF, if we can trick the administrator to click on a 327 00:34:03,300 --> 00:34:08,920 link while he's logged in, then we can force him to carry out the request. So we can force 328 00:34:08,920 --> 00:34:13,820 him to carry out both the SQL injection and the local and file include without having 329 00:34:13,820 --> 00:34:23,320 to worry about the consequence ‑‑ without having to worry about the first part of the 330 00:34:23,320 --> 00:34:27,440 chain, which I did manually. Now, there's another thing. This application doesn't have 331 00:34:27,440 --> 00:34:32,460 any CSRF protection in the entire ‑‑ anywhere. Absolutely anywhere. But if it did, you can 332 00:34:32,460 --> 00:34:38,460 use XSS in order to gain CSRF. This attack was actually used by the SAMI worm where it 333 00:34:38,460 --> 00:34:45,160 was able to use XML HTTP request to pull out the CSRF token to force the request. Now there's 334 00:34:45,160 --> 00:34:50,260 another attack, DNS rebinding, which is very popular from Dan Kaminski. This would also 335 00:34:50,260 --> 00:34:52,840 allow us to attack this administrative interface. 336 00:34:52,840 --> 00:35:01,880 So I wrote an exploit against PHPMyAdmin in which they were ‑‑ they did have very 337 00:35:01,880 --> 00:35:06,680 good CSRF protection for most variables. They did, however, have a whitelist here where 338 00:35:07,700 --> 00:35:13,080 any variable name on this list could be sent from any domain. It was exempt from the CSRF 339 00:35:13,080 --> 00:35:20,420 token. So in this case, DB and table, I used ‑‑ I used ‑‑ were actually vulnerable 340 00:35:20,420 --> 00:35:22,820 to SQL injection. This is not ‑‑ I can't tell you how many times I've used this. I 341 00:35:22,840 --> 00:35:27,680 couldn't just execute any query, but I was able to influence a query that was being 342 00:35:27,680 --> 00:35:32,180 built. So in this case, DB has to be set to any database, in this case, information schema. 343 00:35:32,460 --> 00:35:36,640 And table, I'm filling off the query. This is doing a union select in the app file. 344 00:35:38,420 --> 00:35:44,400 So using local file include, we can gain remote code execution or remote system. App arm will 345 00:35:44,400 --> 00:35:52,580 not allow us to write ‑‑ anyway, there's another thing to note. SELinux is far more 346 00:35:52,840 --> 00:35:57,840 secure than app armor. And SELinux will throw this error when trying to pull off this attack. 347 00:35:58,220 --> 00:36:03,460 Although it will allow MySQL to write to slash temp, what it does is it looks at the source ‑‑ 348 00:36:03,460 --> 00:36:08,040 it looks at the owner of the file. So it's saying, hey, MySQL owns this file, but it's 349 00:36:08,040 --> 00:36:11,860 trying to be executed by HTTPD and throws an error. Now, there's an important thing 350 00:36:11,860 --> 00:36:17,640 to note. SELinux throws a whole lot of errors. It throws errors against ‑‑ in fact, it 351 00:36:17,640 --> 00:36:22,560 will not even let you install PHP nuke because of ‑‑ because of errors. You have to 352 00:36:22,560 --> 00:36:27,520 add an exception. It's common, because of this restriction, that people just get upset 353 00:36:27,520 --> 00:36:35,300 with SELinux and install it all together. And that is a flaw in SELinux. I will admit 354 00:36:35,300 --> 00:36:39,040 that. Although it breaks my exploits. And it breaks this exploit in particular. 355 00:36:41,120 --> 00:36:47,340 But under the 7.0 branch, there is a very nice eval remote code execution. I was running 356 00:36:47,340 --> 00:36:51,700 an old version of PHP BB using preg replace slash e. 357 00:36:53,180 --> 00:36:57,620 With the slash e modifier, it allows you to eval whatever parameters are passed to 358 00:36:57,620 --> 00:37:03,060 it. So in this case, this actually does work with SELinux. We don't have to introduce new 359 00:37:03,060 --> 00:37:06,640 code. It's already there. We can just ‑‑ we can just access it. 360 00:37:07,000 --> 00:37:11,840 Now there's a very cool paper, a study in Scarlett. This paper is a bit old. But when 361 00:37:11,840 --> 00:37:16,440 I read this, it was a precursor to writing my first exploit. I highly recommend that 362 00:37:16,440 --> 00:37:21,680 you grab a copy of this paper. It goes over a lot of the syncs and the other things that 363 00:37:21,680 --> 00:37:26,420 it does. And problems with PHP, such as preg replace slash e. 364 00:37:27,740 --> 00:37:29,560 Anyway, end. That is my talk. Thank you.