1 00:00:00,000 --> 00:00:13,180 And now for my favorite DEF CON speaker, FX. Thank you, guys. And with me here is Greg, 2 00:00:14,100 --> 00:00:25,200 one of my colleagues. And yeah, welcome to the annual Ravdafucking event. So I learned today 3 00:00:25,200 --> 00:00:34,020 from much that in marriages in the United States, there's traditional gifts for every year. And for 4 00:00:34,020 --> 00:00:46,960 the 20th anniversary, the gift is China. So I didn't know that before. So let's get 5 00:00:46,960 --> 00:00:55,120 started. So you will occasionally see fortune cookies. The strings on the paper slips are 6 00:00:55,200 --> 00:01:05,880 actually from the binaries. So let's talk about Huawei routers and stuff. Who is Huawei? Most 7 00:01:05,880 --> 00:01:15,580 people don't know them or think they're only present in China. It's a $21 billion company. They 8 00:01:15,580 --> 00:01:21,560 essentially make telecom networks, equipment, global services, which means they get to 9 00:01:21,560 --> 00:01:25,120 operate the stuff that they sold. And they make wide-level 10 00:01:25,200 --> 00:01:35,620 products. They do a lot of shit. Here's just different business units. So everything that goes 11 00:01:35,620 --> 00:01:41,980 into communication networks, more or less, can be purchased by Huawei. What we're interested in 12 00:01:41,980 --> 00:01:50,120 here is the routing equipment. So the routing equipment is separated into, like, two series, 13 00:01:50,340 --> 00:01:55,180 the NE and the AR. Of the AR series, we actually have two series. One is the ARM series. And the 14 00:01:55,200 --> 00:02:00,360 have two types of devices to play with, because they're not really easy to get. 15 00:02:01,780 --> 00:02:05,420 The routers are commonly also referred to as quid way. 16 00:02:06,180 --> 00:02:13,960 There is another company, Huawei 3.com, or HEC, as we call them, who essentially make 17 00:02:13,960 --> 00:02:18,160 the same product and run the same software, but it is a different company. 18 00:02:18,160 --> 00:02:24,640 So, if you're vulnerability scanning or something, then make sure that you're taking this in. 19 00:02:25,460 --> 00:02:31,040 They also do security devices, and well, then there's interesting joint ventures. 20 00:02:31,220 --> 00:02:37,820 You know that saying that A-class people hire A-class people, and B-class people, well, 21 00:02:37,940 --> 00:02:38,980 Huawei's the man-tech. 22 00:02:41,820 --> 00:02:47,720 When we look at product security, there is directly accessible from the main web page 23 00:02:47,720 --> 00:02:47,880 . 24 00:02:51,060 --> 00:02:55,480 They're talking about open, transparent, sincere attitude, blah, blah, blah, work with 25 00:02:55,480 --> 00:02:56,040 all the governments. 26 00:02:57,220 --> 00:03:03,340 Their CEO recently called for a global corporation for data protection. 27 00:03:04,860 --> 00:03:05,580 Yes. 28 00:03:06,380 --> 00:03:13,300 The reality is this, there is no externally visible product security group, so neither 29 00:03:13,300 --> 00:03:16,760 Security Focus nor OSVDB lists a vendor contact. 30 00:03:16,760 --> 00:03:19,080 The website of Huawei doesn't either. 31 00:03:19,940 --> 00:03:22,820 They've never released a single product security advisory. 32 00:03:25,240 --> 00:03:27,220 So nobody knows anything. 33 00:03:28,240 --> 00:03:33,800 The product security related patches are not, you know, visible as such. 34 00:03:33,960 --> 00:03:35,440 They just increment the release number. 35 00:03:36,640 --> 00:03:42,040 So apparently it's really someone hacks into the fix and then recompiles the code and this 36 00:03:42,040 --> 00:03:42,700 is your new image. 37 00:03:43,800 --> 00:03:44,200 That's it. 38 00:03:45,060 --> 00:03:45,660 So... 39 00:03:47,640 --> 00:03:48,460 Responsible disclosure. 40 00:03:48,800 --> 00:03:51,260 If I can't reach you, I can't tell you about your bugs. 41 00:03:52,140 --> 00:03:53,260 This is what happens. 42 00:03:57,200 --> 00:03:59,080 So let's talk about VRP. 43 00:04:01,000 --> 00:04:06,100 So the versatile routing platform is Huawei's software. 44 00:04:07,160 --> 00:04:11,120 A general myth in the United States is that they stole Cisco IOS. 45 00:04:11,480 --> 00:04:12,960 Nobody ports that shit. 46 00:04:15,660 --> 00:04:17,840 I mean, seriously, that's just not efficient. 47 00:04:18,160 --> 00:04:25,260 So what actually happened was they, according to Cisco's legal documents, they actually 48 00:04:25,260 --> 00:04:34,380 copied the enhanced IGRP stuff and made the CLI look like a Cisco CLI so people didn't 49 00:04:34,380 --> 00:04:35,260 have to learn new shit. 50 00:04:35,500 --> 00:04:42,140 And since they made the CLI look like the Cisco CLI, they copy and pasted it widely 51 00:04:42,140 --> 00:04:44,520 from the Cisco documentation into their own. 52 00:04:45,880 --> 00:04:53,900 But in reality, the VRP platform is actually VXWorks, version 3 or version 5. 53 00:04:54,240 --> 00:04:58,740 Last year there was VRP 8 coming out, but we haven't seen it yet. 54 00:05:00,000 --> 00:05:03,640 So the versioning, as you see, is not very clear. 55 00:05:04,100 --> 00:05:08,680 I really haven't figured out what the release numbers actually mean. 56 00:05:09,920 --> 00:05:13,520 You will also see different names for the software. 57 00:05:13,520 --> 00:05:16,640 So sometimes it's called VXLS or COMWARE. 58 00:05:17,660 --> 00:05:24,480 And what's really interesting, so in the U.K., Huawei has a Huawei cybersecurity center, 59 00:05:24,680 --> 00:05:27,140 but they don't have a security contact group, right? 60 00:05:27,260 --> 00:05:32,960 But they have a cybersecurity center in which they build a special certified version of 61 00:05:32,960 --> 00:05:37,740 the software for the U.K., commonly referred to as the GCHQ VRP. 62 00:05:40,780 --> 00:05:42,880 So how do you talk with VRP? 63 00:05:43,520 --> 00:05:47,980 Pretty much like every other router, you have default CLI interfaces, web configuration, 64 00:05:50,500 --> 00:05:57,160 netconf, SNMP, and then some more obscure things like the branch intelligent management 65 00:05:57,500 --> 00:05:57,800 system. 66 00:06:00,800 --> 00:06:05,880 What's interesting here is you can configure those things between Chinese and English. 67 00:06:06,060 --> 00:06:07,560 So by default, they run on Chinese. 68 00:06:07,980 --> 00:06:10,360 You can configure them to be in English. 69 00:06:11,040 --> 00:06:11,340 However. 70 00:06:11,440 --> 00:06:13,500 . . . 71 00:06:13,520 --> 00:06:36,900 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 00:06:36,900 --> 00:06:38,100 Otherwise that is actually really clever. 73 00:06:41,320 --> 00:06:45,640 The CLI comes with several levels of hidden commands. 74 00:06:46,460 --> 00:06:53,740 So when you log into a router, you can enter the command underscore and go into hidden 75 00:06:53,740 --> 00:06:59,800 command mode and then depending on the device that you have, there's additional hidden command 76 00:06:59,800 --> 00:07:01,860 codes that you will find. 77 00:07:01,860 --> 00:07:10,300 So, on the hack machine, there is actually, it asks for the secret password, which turns 78 00:07:10,300 --> 00:07:14,260 out to be a string compare and looking for Huawei 3.0. 79 00:07:14,720 --> 00:07:15,640 What is it? 80 00:07:15,940 --> 00:07:17,500 You're interrupting my talk. 81 00:07:17,840 --> 00:07:17,920 I know. 82 00:07:18,080 --> 00:07:23,180 I just want to make sure if you're here for the tenacious D thing, that's tenacious. 83 00:07:23,540 --> 00:07:24,900 The room's switched. 84 00:07:26,400 --> 00:07:27,020 That's it. 85 00:07:27,080 --> 00:07:27,720 Last announcement. 86 00:07:28,100 --> 00:07:30,400 And I wanted to give you an opportunity to get some beer. 87 00:07:31,100 --> 00:07:31,720 Thank you. 88 00:07:31,860 --> 00:07:36,640 Well, if you haven't figured out that this is the other talk by now, then I really suck. 89 00:07:46,300 --> 00:07:50,300 So, I want to repeat myself. 90 00:07:50,500 --> 00:07:52,300 This is verbatim copies from the binary. 91 00:07:54,080 --> 00:07:55,520 So the VRP images. 92 00:07:56,440 --> 00:07:58,780 Image availability is a bitch. 93 00:07:59,080 --> 00:08:00,680 So if you're Chinese. 94 00:08:00,680 --> 00:08:05,120 If you're Chinese and you want to give me VRP images, that would be highly appreciated. 95 00:08:06,520 --> 00:08:11,020 We couldn't find a legal way to get them off the website. 96 00:08:11,840 --> 00:08:14,000 So we solved it the American way. 97 00:08:14,240 --> 00:08:16,360 Can we throw enough money at it? 98 00:08:16,460 --> 00:08:17,520 So the problem goes away. 99 00:08:17,720 --> 00:08:19,020 And simply bought routers. 100 00:08:19,320 --> 00:08:20,640 Because they come with software. 101 00:08:21,140 --> 00:08:25,540 And they come with a default FTP service and you just FTP in and there's a file called 102 00:08:25,540 --> 00:08:25,840 system. 103 00:08:26,020 --> 00:08:26,820 And that's the image. 104 00:08:27,660 --> 00:08:29,560 There's also an HTTP.zip. 105 00:08:30,140 --> 00:08:30,660 I don't know. 106 00:08:30,680 --> 00:08:33,360 Which contains the web content and stuff. 107 00:08:36,880 --> 00:08:40,920 So when you look at the images, they're very similar. 108 00:08:41,620 --> 00:08:46,360 You know, the headers are between 30 bytes and 6 kilobytes. 109 00:08:47,620 --> 00:08:50,000 But what you find after that is ARG. 110 00:08:50,280 --> 00:08:52,900 Anyone from the 90s remember ARG, the packer? 111 00:08:53,360 --> 00:08:57,400 So this is actually they actually use ARG. 112 00:08:58,780 --> 00:08:59,820 We've also seen. 113 00:09:00,680 --> 00:09:02,480 The 7zip file. 114 00:09:03,020 --> 00:09:06,860 So essentially what's in there is one binary that they unpack. 115 00:09:10,180 --> 00:09:14,520 The default services are pretty plenty on those platforms. 116 00:09:14,800 --> 00:09:21,800 So we're looking at SSH by default open, HTTP, web management, FTP. 117 00:09:22,420 --> 00:09:27,720 You also often see Telnet open, X25 over TCP and H323. 118 00:09:27,940 --> 00:09:29,200 What can possibly go wrong? 119 00:09:29,940 --> 00:09:30,300 . 120 00:09:31,680 --> 00:09:39,560 And it's apparently it is a very recent concept at Huawei that a configuration allows you 121 00:09:39,560 --> 00:09:40,980 to turn off a default service. 122 00:09:41,560 --> 00:09:44,900 So from what we hear, this was introduced last year. 123 00:09:45,900 --> 00:09:48,360 So if you have a default service, you have have it. 124 00:09:50,880 --> 00:09:51,760 Code quality. 125 00:09:52,060 --> 00:09:54,400 We were considering to leave this slide empty. 126 00:09:55,620 --> 00:09:56,200 . 127 00:09:57,340 --> 00:09:57,920 . 128 00:09:57,920 --> 00:10:06,700 The smallest box that they made, the AR18, has on the image 10,730 calls to Sprintf. 129 00:10:08,180 --> 00:10:11,600 The next bigger box has 16,420. 130 00:10:12,080 --> 00:10:16,860 So the number of calls to Sprintf seems to be a linear function over the box price. 131 00:10:19,380 --> 00:10:23,160 You find lots of reimplementation of string copy and similar. 132 00:10:23,580 --> 00:10:26,200 They wrote their own SSH server. 133 00:10:26,200 --> 00:10:35,540 However, brave, especially if you actually have to blackmail OpenSSH client to talk to 134 00:10:35,540 --> 00:10:38,640 this thing because it uses a too small RSA model. 135 00:10:40,040 --> 00:10:46,680 And for exploitability, the Nile page is not just mapped, it's read, write, execute mapped. 136 00:10:50,700 --> 00:10:55,000 So, let's look at some of the internals. 137 00:10:56,680 --> 00:10:58,020 So this is the web UI. 138 00:10:59,120 --> 00:11:01,060 Everyone who speaks Chinese has a good time. 139 00:11:01,160 --> 00:11:02,440 Everyone else not. 140 00:11:05,000 --> 00:11:08,420 So they use session IDs. 141 00:11:09,000 --> 00:11:12,980 The session IDs are 32 bit, which is four byte. 142 00:11:13,260 --> 00:11:15,720 One of the four bytes is static. 143 00:11:15,720 --> 00:11:17,860 One of the four bytes is ignored. 144 00:11:18,980 --> 00:11:21,740 One of the four bytes is the concurrency session. 145 00:11:22,500 --> 00:11:24,240 And the rest is counting up. 146 00:11:26,120 --> 00:11:34,060 So if someone is already logged into the router, a very short Perl script gives you full administrative 147 00:11:34,060 --> 00:11:35,100 access to the machine. 148 00:11:36,080 --> 00:11:37,160 That's how it looks like. 149 00:11:37,260 --> 00:11:40,480 So you're getting the config and you can change it and upload it again. 150 00:11:42,660 --> 00:11:45,200 So since they have web management. 151 00:11:48,360 --> 00:11:49,640 I'm glad you like that. 152 00:11:51,080 --> 00:11:55,200 So since they have web management, they actually also need a web server, right? 153 00:11:57,280 --> 00:12:04,620 So the web server tries to figure out whether a piece of content that is, you know, requested 154 00:12:05,060 --> 00:12:07,600 actually requires one of those complicated session IDs. 155 00:12:09,060 --> 00:12:15,000 Instead of looking into the http.zip, which would actually tell them where that is. 156 00:12:15,000 --> 00:12:18,200 They actually have hard coded substring comparisons. 157 00:12:18,680 --> 00:12:21,740 And if anything matches, they copy it into a buffer. 158 00:12:22,120 --> 00:12:23,460 That buffer is too small. 159 00:12:23,640 --> 00:12:25,120 And that buffer is on the stick. 160 00:12:28,440 --> 00:12:30,160 So any of those will work. 161 00:12:31,500 --> 00:12:38,300 And you essentially directly overwrite pre-authentication the instruction pointer. 162 00:12:39,900 --> 00:12:42,360 So this is essentially how much you control. 163 00:12:43,200 --> 00:12:44,980 You not only have instruction pointers. 164 00:12:45,000 --> 00:12:46,620 But also a bunch of registers. 165 00:12:46,800 --> 00:12:47,800 And of course, the stack. 166 00:12:49,720 --> 00:12:50,600 So what do we do? 167 00:12:50,860 --> 00:12:52,120 We look for a jump target. 168 00:12:53,060 --> 00:12:55,440 This being a string overflow, zero bytes are off. 169 00:12:56,100 --> 00:12:57,680 Image base has zero bytes. 170 00:12:57,920 --> 00:12:58,600 ROM is mapped. 171 00:12:59,400 --> 00:13:02,160 For now, we just stay image dependent. 172 00:13:02,460 --> 00:13:08,620 And we simply use a move to counter register and then branch to counter register sequence 173 00:13:08,620 --> 00:13:10,000 from existing code. 174 00:13:10,200 --> 00:13:11,820 So we pivot back to the stack. 175 00:13:13,640 --> 00:13:14,660 And then, of course. 176 00:13:15,780 --> 00:13:17,460 We need code to execute. 177 00:13:19,120 --> 00:13:20,200 And I need a beer. 178 00:13:27,820 --> 00:13:34,640 So instead of having a complicated shell code, we actually use a feature. 179 00:13:34,800 --> 00:13:40,480 So VRP comes with a pair of functions that you can call and you can pass string to it. 180 00:13:41,080 --> 00:13:44,560 Which will then put onto the CLI fully authenticated. 181 00:13:44,560 --> 00:13:45,360 As administrator. 182 00:13:46,160 --> 00:13:49,520 So there's absolutely no privilege separation whatsoever. 183 00:13:51,220 --> 00:13:55,120 The address is in this example image dependent. 184 00:13:55,520 --> 00:14:04,180 But a short fat German hacker presented an alternative method a couple of years back 185 00:14:04,180 --> 00:14:05,920 here at DEF CON for Cisco. 186 00:14:06,140 --> 00:14:12,420 So we actually took one of our existing Cisco shell codes and copied it pretty much verbatim 187 00:14:12,420 --> 00:14:12,720 over. 188 00:14:13,140 --> 00:14:14,480 And it actually worked. 189 00:14:14,560 --> 00:14:14,580 And it's a little bit more complex. 190 00:14:17,640 --> 00:14:23,320 So the strategy for the exploit here is to get around the HTTP limitations with spaces 191 00:14:23,320 --> 00:14:24,320 and stuff. 192 00:14:24,880 --> 00:14:26,120 We encode the commands. 193 00:14:27,120 --> 00:14:28,560 And decode them in place. 194 00:14:28,720 --> 00:14:31,100 And then run the command sequence like you see here. 195 00:14:31,280 --> 00:14:32,900 And we end it with a ping to ourselves. 196 00:14:33,200 --> 00:14:34,580 So we know that everything worked fine. 197 00:14:38,100 --> 00:14:40,220 And this is all shell code you need for that. 198 00:14:40,460 --> 00:14:42,580 So the left side is the decoder. 199 00:14:43,380 --> 00:14:44,380 The right side. 200 00:14:44,380 --> 00:14:45,280 Is the execution. 201 00:14:46,200 --> 00:14:50,360 It's pretty similar to OSX power PC shell code. 202 00:14:51,020 --> 00:14:51,920 Very basic. 203 00:14:52,740 --> 00:14:54,080 And this is the result. 204 00:14:54,200 --> 00:15:01,700 You essentially get, you know, remote management postscripts that allow you to log into routers 205 00:15:01,700 --> 00:15:02,400 and configure them. 206 00:15:06,820 --> 00:15:08,500 Now it's Greg and the heap. 207 00:15:09,900 --> 00:15:10,620 All right. 208 00:15:11,180 --> 00:15:12,060 Hi, everyone. 209 00:15:12,960 --> 00:15:13,520 So. 210 00:15:13,600 --> 00:15:14,220 After all this. 211 00:15:14,220 --> 00:15:16,460 Like sprinter stack overflows that we identified. 212 00:15:16,720 --> 00:15:18,700 We wanted to show you some heap stuff. 213 00:15:19,080 --> 00:15:25,160 And I had a pretty hard time finding a heap overflow between all this like static sprinter 214 00:15:25,160 --> 00:15:25,880 stack overflow. 215 00:15:26,860 --> 00:15:33,400 We actually managed to find one inside the BIMS client which is like a default service 216 00:15:33,400 --> 00:15:33,900 on the device. 217 00:15:35,100 --> 00:15:38,580 BIMS is some proprietary protocol based on HTTP. 218 00:15:39,940 --> 00:15:43,360 The like the real use case is probably not so important. 219 00:15:43,640 --> 00:15:44,200 But. 220 00:15:44,200 --> 00:15:48,260 Because the bug is like just right in the beginning. 221 00:15:48,540 --> 00:15:56,300 So when they connect to an HTTP server and parse the response, then they look for the 222 00:15:56,300 --> 00:15:56,880 content length. 223 00:15:57,380 --> 00:16:02,220 So they call some sub function to find the content length, store the content length, 224 00:16:02,500 --> 00:16:05,240 and then go ahead and malloc so many bytes plus one. 225 00:16:06,680 --> 00:16:09,920 And then they go like, well, we already received some stuff. 226 00:16:10,700 --> 00:16:11,700 Let's copy it there. 227 00:16:11,880 --> 00:16:13,880 So they just mem copy. 228 00:16:13,880 --> 00:16:18,980 All the stuff they already received into this buffer that like you can specify basically 229 00:16:18,980 --> 00:16:19,620 the size of. 230 00:16:19,800 --> 00:16:24,620 So this is like a pretty basic heap overflow. 231 00:16:26,420 --> 00:16:28,420 Of course, unauthenticated and all that. 232 00:16:32,520 --> 00:16:35,660 So the way to trigger it is pretty easy. 233 00:16:35,780 --> 00:16:38,300 You just specify some small content length. 234 00:16:39,120 --> 00:16:42,420 Send way more data than you specified in the content length field. 235 00:16:42,420 --> 00:16:44,580 And then it crashes somewhere. 236 00:16:44,880 --> 00:16:49,900 Like in some weird location telling you, I don't know, the router just starts malfunctioning. 237 00:16:51,700 --> 00:16:57,060 So in order to really exploit that, we need to have a look at the memory allocator of 238 00:16:57,060 --> 00:16:57,500 the system. 239 00:16:58,140 --> 00:17:00,200 And this is what we're going to cover right now. 240 00:17:02,580 --> 00:17:06,700 Due to time constraints, we're just going to like cover the basics here. 241 00:17:07,820 --> 00:17:10,680 And this is why we will only look at malloc. 242 00:17:11,100 --> 00:17:12,120 We'll not show that. 243 00:17:12,120 --> 00:17:14,120 The disassembly of free and all that kind of stuff. 244 00:17:14,340 --> 00:17:19,280 So at some point in time, you'll just probably have to trust my hand waving there. 245 00:17:20,180 --> 00:17:25,100 If you need to know any details, then please feel free to like catch us later on and we 246 00:17:25,100 --> 00:17:25,980 can chat about that stuff. 247 00:17:27,560 --> 00:17:28,500 So here we go. 248 00:17:29,680 --> 00:17:31,280 This is the relevant parts of malloc. 249 00:17:32,400 --> 00:17:40,060 What they do there is they have to request a block size in R31, compare it with like 250 00:17:40,060 --> 00:17:40,880 some values. 251 00:17:40,880 --> 00:17:45,280 And depending on the block size, they store some small integer constant in R5. 252 00:17:46,020 --> 00:17:53,000 And then later on, if R5 is different from 0, they will call some helper function, which 253 00:17:53,000 --> 00:17:53,960 I called malloc worker. 254 00:17:55,080 --> 00:17:59,480 And this helper function will actually use R5 as an index into some table. 255 00:17:59,700 --> 00:18:01,360 We're going to see that on the next slide. 256 00:18:03,780 --> 00:18:10,860 So malloc worker will first try to determine the free list that is actually to be used. 257 00:18:10,860 --> 00:18:16,020 used to like give back chunks of the requested size. So this is a binning memory allocator 258 00:18:16,020 --> 00:18:23,120 which is like pretty common. So they use this small value in R5 to find the right free list. 259 00:18:24,580 --> 00:18:30,140 They take the free list and then will try to like put off the first chunk from the free 260 00:18:30,140 --> 00:18:38,280 list and to return it. However, there are some sanity checks which they probably introduced 261 00:18:38,280 --> 00:18:45,220 because like it was crashing anyway all the time and they wanted to debug it or so. So they have 262 00:18:45,220 --> 00:18:54,480 some sanity checks there. And the sanity checks are basically each heap chunk has to start with a 263 00:18:54,480 --> 00:19:05,260 hard coded value of OXEFEFEFEF. Then at offset four each chunk has to contain a pointer to some 264 00:19:05,900 --> 00:19:07,320 memory allocator structure. 265 00:19:08,280 --> 00:19:38,260 And this structure is going to be important later on. And at offset 14 hex, this structure contains some like string or integer constant which reads like exclamation mark PGH. This is why we're going to call this the PGH structure. But it's just basically some heap management infrastructure that's like placed inside each chunk. And when all this is done, like when all the sanity checks are passed then it just takes the first block from the 266 00:19:38,280 --> 00:19:47,080 free list and puts it off the free list and returns it. And in order to take it off the free list, it will do like the classical pointer exchange that many of you might know from old 267 00:19:47,080 --> 00:20:08,260 school or whatever. So what we learned so far about the heap is the following. Like in the set up that most of you probably know, a heap chunk like has a header part that is not visible to the user actually. And like the real user data. This is like a 268 00:20:08,280 --> 00:20:11,640 And inside the header, we have the fixed value 269 00:20:11,640 --> 00:20:13,020 that we have seen already. 270 00:20:13,220 --> 00:20:15,400 We have the pointer to the management structure 271 00:20:15,400 --> 00:20:18,120 and then some other stuff that is not so important here. 272 00:20:19,580 --> 00:20:22,100 And then it continues with the user data part, 273 00:20:22,280 --> 00:20:25,360 which is actually like the pointer that malloc returns you. 274 00:20:27,300 --> 00:20:29,780 And like if the trunk is in use, 275 00:20:29,860 --> 00:20:32,700 then obviously the user data contains the user data. 276 00:20:33,340 --> 00:20:34,640 If the trunk is free, 277 00:20:34,640 --> 00:20:38,060 the allocator will place some meter data 278 00:20:38,060 --> 00:20:40,780 inside the user part of the trunk, 279 00:20:40,960 --> 00:20:42,020 which is also pretty common. 280 00:20:42,980 --> 00:20:47,100 So it will start with a value of 0xbet0bet0, 281 00:20:47,200 --> 00:20:48,720 just like as a marker. 282 00:20:49,640 --> 00:20:51,580 And then it will place two pointers, 283 00:20:51,800 --> 00:20:54,020 one pointer to the next free trunk in the list 284 00:20:54,020 --> 00:20:56,480 and one pointer to the previous free trunk in the list. 285 00:20:56,600 --> 00:20:57,760 So it's a doubly linked list. 286 00:20:58,100 --> 00:21:02,020 And this list is stored inside the user data portion of free trunks. 287 00:21:06,540 --> 00:21:08,080 Okay, so as we mentioned, 288 00:21:08,300 --> 00:21:12,800 the allocator uses different bins for trunks of different sizes. 289 00:21:13,680 --> 00:21:16,020 And so each bin has its own free list. 290 00:21:17,520 --> 00:21:20,520 And this pg8 structure that we have seen before 291 00:21:22,120 --> 00:21:25,540 actually contains a pointer to the respective free list. 292 00:21:25,740 --> 00:21:27,660 So this is what the structure is used for. 293 00:21:28,960 --> 00:21:30,640 So when, like we call free, 294 00:21:31,230 --> 00:21:32,940 free will go ahead, look at the block, 295 00:21:33,100 --> 00:21:34,340 look at the pointer to the pg8, 296 00:21:35,400 --> 00:21:38,660 find the free list and then insert the trunk into that free list. 297 00:21:39,240 --> 00:21:41,760 And malloc basically does a similar thing. 298 00:21:44,360 --> 00:21:47,260 So malloc will take the first element of the free list 299 00:21:47,260 --> 00:21:49,400 and use this classical pointer exchange 300 00:21:49,400 --> 00:21:51,000 that we have also on the slides here. 301 00:21:51,240 --> 00:21:54,900 So it will go like pref next is this next 302 00:21:54,900 --> 00:21:56,960 and next pref is this pref. 303 00:21:57,160 --> 00:22:01,500 So that like this trunk basically falls out of the list. 304 00:22:03,280 --> 00:22:04,620 And as we see, 305 00:22:05,380 --> 00:22:11,000 those both operations enable us to do some arbitrary memory overwrite 306 00:22:11,000 --> 00:22:13,220 if we control the values there. 307 00:22:13,480 --> 00:22:17,320 Like if we control the pref next and this next pointer, 308 00:22:17,500 --> 00:22:19,460 then we have an arbitrary memory right here. 309 00:22:22,100 --> 00:22:26,140 So this is like the most old school malloc attack 310 00:22:26,140 --> 00:22:27,280 that you can probably imagine. 311 00:22:29,240 --> 00:22:33,560 We use the pointer exchange to write like arbitrary data 312 00:22:33,560 --> 00:22:34,340 to arbitrary memory. 313 00:22:35,280 --> 00:22:36,480 However, to do that, 314 00:22:36,620 --> 00:22:39,520 we need to overwrite the metadata of a free trunk. 315 00:22:39,920 --> 00:22:42,500 So that's the difference to like DL malloc. 316 00:22:43,820 --> 00:22:47,380 And this is why we have like a couple of preconditions here 317 00:22:47,380 --> 00:22:53,880 that we need to fulfill in order to like reliably exploit heap corruptions. 318 00:22:57,380 --> 00:22:59,720 So let's look at those preconditions. 319 00:23:01,180 --> 00:23:04,280 Let's first assume that we have a very basic setup. 320 00:23:05,760 --> 00:23:08,760 For instance, let's assume the device was freshly booted 321 00:23:08,760 --> 00:23:09,660 and the memory is all clean. 322 00:23:10,720 --> 00:23:12,960 And then let's just assume 323 00:23:12,960 --> 00:23:14,860 we have the following sequence of allocations. 324 00:23:16,320 --> 00:23:18,880 First, we allocate some 512 bytes buffer. 325 00:23:19,600 --> 00:23:20,520 We do that again. 326 00:23:21,040 --> 00:23:22,140 Free the second one. 327 00:23:22,260 --> 00:23:23,280 Free the first one. 328 00:23:23,660 --> 00:23:27,820 So the free list is going to look like this on the slides. 329 00:23:29,220 --> 00:23:33,320 The first element in the free list is A followed by B. 330 00:23:33,420 --> 00:23:34,520 And the pointers are all. 331 00:23:34,640 --> 00:23:37,440 They're all intact, like A points to B and B points back to A. 332 00:23:37,900 --> 00:23:40,500 And yeah, that's like the regular way 333 00:23:40,500 --> 00:23:41,980 the free list should look like after that. 334 00:23:45,780 --> 00:23:48,540 Let's further on assume that the trunks A and B 335 00:23:49,060 --> 00:23:51,300 like directly follow each other in memory. 336 00:23:51,520 --> 00:23:54,320 Like there is no third trunk between them, okay? 337 00:23:54,820 --> 00:23:56,380 Always keep that in mind 338 00:23:56,380 --> 00:24:00,300 because that's like an important thing 339 00:24:00,300 --> 00:24:02,520 if you overwrite those trunks. 340 00:24:04,640 --> 00:24:06,740 If there was some trunk in between 341 00:24:06,740 --> 00:24:08,280 and we would like write more bytes 342 00:24:08,280 --> 00:24:10,360 in order to reach the trunk that we actually want to overflow. 343 00:24:11,660 --> 00:24:13,800 Okay, but like to make it simple, 344 00:24:13,880 --> 00:24:15,700 let's just stick with this setup here. 345 00:24:18,340 --> 00:24:21,280 Okay, so this is what the attack is going to look like. 346 00:24:23,560 --> 00:24:25,080 We have our free list 347 00:24:25,080 --> 00:24:27,000 that we already detailed on in the last slide. 348 00:24:28,280 --> 00:24:30,740 And up to now, it's like perfectly in order 349 00:24:30,740 --> 00:24:31,520 and it's all fine. 350 00:24:32,640 --> 00:24:34,360 So the first thing we're going to do 351 00:24:34,360 --> 00:24:37,120 is we are allocating 512 bytes again 352 00:24:37,120 --> 00:24:39,800 and then the free list is going to look like this, 353 00:24:39,960 --> 00:24:41,800 like the second part of the list. 354 00:24:43,880 --> 00:24:46,000 And keep in mind, like in memory, 355 00:24:46,260 --> 00:24:48,620 B directly comes after A. 356 00:24:49,600 --> 00:24:52,180 So now we overflow the A buffer. 357 00:24:52,720 --> 00:24:54,360 That means we write data 358 00:24:54,360 --> 00:24:57,600 into the metadata part of the B buffer, okay? 359 00:24:58,000 --> 00:25:01,600 So now we have a modified free list 360 00:25:01,600 --> 00:25:04,200 that contains B, 361 00:25:04,200 --> 00:25:04,340 and we have a free list that contains B, 362 00:25:04,360 --> 00:25:05,260 which is the first element still. 363 00:25:06,180 --> 00:25:10,220 But B has corrupted next and pref pointers. 364 00:25:10,900 --> 00:25:12,700 So instead of the next pointer, 365 00:25:12,960 --> 00:25:15,480 we now provide some value 366 00:25:15,480 --> 00:25:17,480 that we want to write into memory. 367 00:25:18,220 --> 00:25:19,960 And instead of the pref pointer, 368 00:25:20,320 --> 00:25:23,420 we provide a pointer to a memory location 369 00:25:23,420 --> 00:25:24,740 that we want to override. 370 00:25:26,140 --> 00:25:29,020 Okay, so this is pretty much 371 00:25:29,020 --> 00:25:31,080 the basic heap overflow stuff. 372 00:25:32,240 --> 00:25:32,840 Okay. 373 00:25:34,360 --> 00:25:35,280 In order to do all that, 374 00:25:35,400 --> 00:25:36,940 we need to take care 375 00:25:36,940 --> 00:25:39,160 that the memory layout is all right in that. 376 00:25:40,440 --> 00:25:42,240 So we need to have two consecutive chunks, 377 00:25:43,100 --> 00:25:45,280 and we need also to have a pointer 378 00:25:45,280 --> 00:25:46,860 to this heap management structure. 379 00:25:48,420 --> 00:25:50,000 And we, of course, need to know 380 00:25:50,000 --> 00:25:51,740 what value do we want to write where. 381 00:25:52,380 --> 00:25:55,280 Okay, so we're going to cover each of these items. 382 00:25:57,360 --> 00:25:59,220 Fortunately, influencing the heap layout 383 00:25:59,220 --> 00:25:59,960 is pretty easy, 384 00:26:00,480 --> 00:26:03,240 because in this very bug, 385 00:26:03,340 --> 00:26:04,260 we can specify, 386 00:26:04,360 --> 00:26:05,760 we can specify the size of the buffer 387 00:26:05,760 --> 00:26:08,080 that the system is going to allocate. 388 00:26:09,580 --> 00:26:11,380 So we pick 512 bytes, 389 00:26:11,600 --> 00:26:15,480 because the 512 bytes is not used so frequently 390 00:26:15,480 --> 00:26:16,420 in the rest of the system. 391 00:26:16,940 --> 00:26:18,940 And fortunately, 392 00:26:19,760 --> 00:26:22,720 we can control even more of the heap 393 00:26:22,720 --> 00:26:25,760 by opening TCP connections to device, 394 00:26:26,020 --> 00:26:27,540 because for each TCP connection, 395 00:26:27,780 --> 00:26:30,580 the device will malloc a 512 byte buffer. 396 00:26:31,560 --> 00:26:33,420 So yeah, this is a useful primitive 397 00:26:33,420 --> 00:26:34,340 to do that. 398 00:26:34,360 --> 00:26:35,360 So we need to make the free list 399 00:26:35,360 --> 00:26:37,560 look like we want to make it look like, right? 400 00:26:40,440 --> 00:26:44,240 And let's then cover the addresses that we need to know. 401 00:26:45,320 --> 00:26:48,160 We need to know the address of one of these PDA structures, 402 00:26:49,120 --> 00:26:51,820 or at least we need to know the address 403 00:26:51,820 --> 00:26:54,060 of something that looks like a PDA structure. 404 00:26:56,460 --> 00:26:58,060 We need to know the address 405 00:26:58,060 --> 00:26:59,620 of this important piece of memory 406 00:26:59,620 --> 00:27:00,380 that we want to override. 407 00:27:01,900 --> 00:27:04,340 And we need to know where, in many cases, 408 00:27:04,340 --> 00:27:05,560 in memory, our shellcode is placed. 409 00:27:07,140 --> 00:27:10,120 We can basically solve the first and the last problem 410 00:27:10,120 --> 00:27:11,400 by just using heap spraying, 411 00:27:12,120 --> 00:27:14,340 and thereby making it even more unreliable, 412 00:27:14,400 --> 00:27:15,720 and it probably already is. 413 00:27:16,880 --> 00:27:19,420 So we will just go on and heap spray the system, 414 00:27:19,920 --> 00:27:23,240 and then we know that at some high-memory address, 415 00:27:23,440 --> 00:27:26,520 we'll have our stuff ready, 416 00:27:26,700 --> 00:27:28,120 like the fake PDA structure, 417 00:27:28,520 --> 00:27:30,400 and of course the buffer that holds the shellcode. 418 00:27:32,500 --> 00:27:34,200 Heap spraying here is pretty easy, 419 00:27:34,340 --> 00:27:38,020 because we control the size of the buffer it allocates, 420 00:27:38,080 --> 00:27:39,420 so we just go ahead and tell it, 421 00:27:39,500 --> 00:27:41,140 oh, you know what, please allocate like, 422 00:27:41,260 --> 00:27:42,620 I don't know, 10 megs of memory, 423 00:27:42,820 --> 00:27:46,180 and then we send exactly that amount of data, 424 00:27:46,320 --> 00:27:47,560 like not overflowing anything. 425 00:27:48,300 --> 00:27:50,760 We wait for a second connection, and then we trigger the bug. 426 00:27:51,980 --> 00:27:53,480 So all of this is still in memory. 427 00:27:55,300 --> 00:27:57,800 For other services, you will probably need to come 428 00:27:57,800 --> 00:27:58,540 with another approach, 429 00:27:59,600 --> 00:28:02,700 but then again, that's pretty service-dependent, and yeah. 430 00:28:05,680 --> 00:28:08,120 Next question is, what do we actually want to overwrite? 431 00:28:10,500 --> 00:28:13,280 And this is like a pretty interesting question, 432 00:28:13,440 --> 00:28:15,900 because of course we could now go ahead 433 00:28:15,900 --> 00:28:17,860 and pick some function pointer in the image, 434 00:28:18,040 --> 00:28:20,480 but that again would be image-dependent. 435 00:28:22,380 --> 00:28:24,660 And there is some pretty interesting thing we can do. 436 00:28:25,500 --> 00:28:27,240 The interrupt handlers of the system 437 00:28:27,760 --> 00:28:31,420 reside at low-memory addresses, like starting at 100 hex. 438 00:28:31,920 --> 00:28:33,140 And as I've already mentioned, 439 00:28:33,140 --> 00:28:36,160 this page is mapped read-write-executable. 440 00:28:37,280 --> 00:28:40,380 So we can just overwrite the exception handlers 441 00:28:40,380 --> 00:28:43,480 in order to gain control of the program flow later on. 442 00:28:47,100 --> 00:28:48,840 Pretty much like back in the days, 443 00:28:48,980 --> 00:28:51,560 the exception handlers reside at fixed addresses, 444 00:28:52,960 --> 00:28:55,480 and here they start at 100 hex. 445 00:28:58,220 --> 00:29:02,000 However, here we don't have this like vector table thingy 446 00:29:02,000 --> 00:29:03,120 that you might remember from Intel. 447 00:29:03,140 --> 00:29:08,180 Instead, each exception handler is basically a blob of code 448 00:29:08,180 --> 00:29:10,480 that the CPU is going to execute 449 00:29:10,480 --> 00:29:11,920 as soon as the interrupt arrives. 450 00:29:13,760 --> 00:29:16,980 We're going to overwrite the exception handler at 300 hex, 451 00:29:17,180 --> 00:29:20,460 because this is the handler for invalid memory access. 452 00:29:22,640 --> 00:29:25,320 And this is quite some fortunate thing, 453 00:29:25,960 --> 00:29:29,560 because, let me put it this way, 454 00:29:29,620 --> 00:29:32,880 at least my exploits will most likely bring the system 455 00:29:32,880 --> 00:29:34,820 to an inconsistent state at some point in time. 456 00:29:35,140 --> 00:29:38,420 And this is most likely going to end up 457 00:29:38,420 --> 00:29:39,440 in an invalid memory access. 458 00:29:39,980 --> 00:29:42,240 And this is great, because then we just control 459 00:29:42,240 --> 00:29:43,480 like the control flow of the program. 460 00:29:43,780 --> 00:29:45,520 Like we high-track the exception handler. 461 00:29:45,860 --> 00:29:48,520 So as soon as the system accesses invalid memory, 462 00:29:48,760 --> 00:29:50,620 we gain control and we can fix it up 463 00:29:50,620 --> 00:29:51,980 and like clean all the stuff 464 00:29:51,980 --> 00:29:55,100 and hopefully like keep the router running. 465 00:29:55,500 --> 00:29:57,460 So this is like, we could say, 466 00:29:57,480 --> 00:30:02,280 this is the SEH exploitation of Huawei devices. 467 00:30:03,060 --> 00:30:04,660 Like, yeah, kind of. 468 00:30:06,460 --> 00:30:07,680 So what you need to do, 469 00:30:09,100 --> 00:30:10,760 you overwrite the exception handler 470 00:30:10,760 --> 00:30:12,840 and inside your shellcode, 471 00:30:13,080 --> 00:30:15,600 you will like first need to clean the heap structures. 472 00:30:16,480 --> 00:30:18,760 Like you can basically nuke off elements 473 00:30:18,760 --> 00:30:20,520 from the free list and that stuff 474 00:30:20,520 --> 00:30:22,080 to like repair what you did wrong. 475 00:30:23,360 --> 00:30:24,920 Then do whatever you want to do. 476 00:30:25,100 --> 00:30:28,740 Like basically use the shellcode that FX already provided 477 00:30:28,740 --> 00:30:30,580 to execute shell commands. 478 00:30:31,820 --> 00:30:32,780 And finally, 479 00:30:32,880 --> 00:30:34,760 you properly exit from the interrupt handler, 480 00:30:35,740 --> 00:30:37,800 read the CPU documentation for that. 481 00:30:38,640 --> 00:30:40,460 That's pretty straightforward actually. 482 00:30:42,360 --> 00:30:44,620 And I guess there is not much more to say, 483 00:30:45,820 --> 00:30:48,100 except for like showing the exploit running. 484 00:30:52,400 --> 00:30:54,120 So SFX exploits, 485 00:30:55,090 --> 00:30:58,440 this one also will notify us with a ping when it's done. 486 00:30:59,180 --> 00:31:02,520 So what we see here, we like start TCP dump, 487 00:31:02,520 --> 00:31:03,340 start the exploit. 488 00:31:04,500 --> 00:31:08,040 And we actually see making the device two connections. 489 00:31:08,460 --> 00:31:10,720 The first connection we use for heap spraying. 490 00:31:11,280 --> 00:31:14,900 Like we send our data, wait for it to be placed in memory. 491 00:31:15,100 --> 00:31:16,900 And when the device makes the second connection 492 00:31:16,900 --> 00:31:17,640 to the BIM server, 493 00:31:18,460 --> 00:31:20,160 then we actually trigger the vulnerability. 494 00:31:21,040 --> 00:31:22,780 And the last two lines, 495 00:31:23,240 --> 00:31:25,580 we actually see that the device pings us, 496 00:31:25,660 --> 00:31:28,100 which indicates our shellcode like actually worked. 497 00:31:30,400 --> 00:31:31,900 That's pretty much it. 498 00:31:31,900 --> 00:31:32,620 That's pretty much it from my side. 499 00:31:32,940 --> 00:31:35,660 So I will hand over to FX again now. 500 00:31:38,420 --> 00:31:39,800 So first of all, 501 00:31:39,880 --> 00:31:43,240 many thanks to Nick Farr for this tremendously large 502 00:31:43,240 --> 00:31:45,500 and great Gigameister that he got me. 503 00:31:51,980 --> 00:31:54,720 So what about the larger machines, right? 504 00:31:54,800 --> 00:31:56,360 We talked about AR machines. 505 00:31:58,100 --> 00:31:59,920 If you've seen the movie Ice Age, 506 00:32:00,020 --> 00:32:01,580 they had like three melons. 507 00:32:01,900 --> 00:32:03,080 We have three routers. 508 00:32:03,400 --> 00:32:05,280 We're working on getting the bigger ones, 509 00:32:05,420 --> 00:32:07,980 but we have no idea how they look like so far, right? 510 00:32:08,140 --> 00:32:10,240 So just if anyone else. 511 00:32:13,020 --> 00:32:16,540 Now, we're actually almost at the end. 512 00:32:16,940 --> 00:32:20,180 So wanted to give you like something 513 00:32:20,180 --> 00:32:21,660 to bring home to your managers. 514 00:32:23,580 --> 00:32:27,640 What we're looking at here is 90 style box 515 00:32:27,640 --> 00:32:29,320 and 90 style exploitation. 516 00:32:31,900 --> 00:32:32,140 So you can see here, 517 00:32:32,140 --> 00:32:33,920 we have an operating system hardening at all 518 00:32:33,920 --> 00:32:36,640 and a zero page that is mapped, read, write and execute. 519 00:32:37,140 --> 00:32:39,980 No security advisories and no security releases. 520 00:32:42,060 --> 00:32:43,540 So that's our verdict. 521 00:32:45,120 --> 00:32:47,960 You essentially get what you pay for, sorry. 522 00:32:50,140 --> 00:32:53,900 And before anyone asks the same question, 523 00:32:54,140 --> 00:32:55,600 did we find any backdoors? 524 00:32:56,520 --> 00:32:59,460 If you provide to people that operate the devices 525 00:32:59,460 --> 00:33:01,880 and you have buffer overflows and everything else, 526 00:33:01,900 --> 00:33:05,080 service on the device, nobody needs a backdoor. 527 00:33:05,360 --> 00:33:07,120 This is plausible deniability. 528 00:33:08,860 --> 00:33:12,760 And with that, I would like to conclude and thank you for coming.