1 00:00:00,000 --> 00:00:03,100 Good afternoon DEF CON. 2 00:00:03,280 --> 00:00:12,140 Today we have a talk on automobiles, alcohol, blood, sweat and creative reversing of obfuscated car moding tool. 3 00:00:12,500 --> 00:00:14,160 And with our speaker Atlas. 4 00:00:14,940 --> 00:00:16,320 Thank you sir. 5 00:00:16,420 --> 00:00:17,720 Thank you Pauls. 6 00:00:18,620 --> 00:00:20,200 I'm sorry, True. 7 00:00:20,560 --> 00:00:22,220 Hi everyone, welcome. 8 00:00:23,100 --> 00:00:25,920 How many of you came because it had the word automotive in it? 9 00:00:27,580 --> 00:00:28,300 All right. 10 00:00:28,300 --> 00:00:28,780 Good. 11 00:00:29,640 --> 00:00:30,120 Obfuscation. 12 00:00:30,120 --> 00:00:32,160 Who came because it said obfuscation? 13 00:00:35,260 --> 00:00:35,700 Excellent. 14 00:00:35,700 --> 00:00:36,620 Well, thank you all. 15 00:00:36,620 --> 00:00:37,860 I don't care why you came. 16 00:00:37,860 --> 00:00:38,360 You're here. 17 00:00:38,360 --> 00:00:39,840 I'm very grateful for you. 18 00:00:39,900 --> 00:00:41,320 I hope that we have a lot of fun today. 19 00:00:41,740 --> 00:00:50,540 As per usual, if you've come to a talk of mine, I've crammed in way too much stuff and we're going to see just how well and how much we can fit in. 20 00:00:52,780 --> 00:01:00,040 And because of timing, I'm going to probably just come out here and look at the slides that you're seeing because I, yeah, whatever. 21 00:01:00,920 --> 00:01:02,420 So who is this Atlas dork? 22 00:01:02,420 --> 00:01:03,120 Hi. 23 00:01:03,880 --> 00:01:05,240 This is my home. 24 00:01:05,240 --> 00:01:09,240 I was created here at DEF CON in 2013. 25 00:01:11,740 --> 00:01:13,140 And this is my family. 26 00:01:14,160 --> 00:01:15,440 I'm a hacker of things. 27 00:01:15,440 --> 00:01:16,560 I ride a motorcycle. 28 00:01:16,560 --> 00:01:21,060 I've got three amazing daughters who have nothing to do with technology. 29 00:01:24,440 --> 00:01:29,800 If you dare, there is a QR code that has all sorts of ways to get a hold of me. 30 00:01:30,860 --> 00:01:33,260 I'll leave that up for about ten more seconds. 31 00:01:37,790 --> 00:01:42,030 So I work in R&D and cyber physical systems for a company called Grim. 32 00:01:42,250 --> 00:01:44,170 And Grim is represented. 33 00:01:44,170 --> 00:01:45,090 Stand up. 34 00:01:48,530 --> 00:01:51,850 My dear friends down here wore huge Grim T-shirts. 35 00:01:51,850 --> 00:01:55,050 Ask how many of them work for Grim. 36 00:01:57,290 --> 00:01:58,830 A lot of love here. 37 00:01:59,610 --> 00:02:00,490 All right. 38 00:02:00,650 --> 00:02:05,350 So my job is to break into things. 39 00:02:05,350 --> 00:02:08,310 And I set many goals. 40 00:02:08,310 --> 00:02:10,550 Some of them attainable in the near term. 41 00:02:10,550 --> 00:02:12,190 Many of them long-term goals. 42 00:02:12,190 --> 00:02:17,630 Some of them, like, if I do these three projects that are year long, then I'll achieve this thing. 43 00:02:17,630 --> 00:02:19,790 So I think in these many different ways. 44 00:02:21,090 --> 00:02:27,590 But my ultimate goal typically is remote code execution on a device thing in a running program. 45 00:02:28,090 --> 00:02:46,930 For cars, and particularly the small ECUs that run your cars, my favorite way to gain code execution on an ECU, like your brake controller or your power steering controller, is through the update process or diagnostics process. 46 00:02:47,590 --> 00:03:06,190 Your tool plugs in, I'm sorry, runs on Windows, there's some software, have a hardware tool plugs into the OBD2 port on your car and then talks over the vehicle CAN network to tell the car things what to do. 47 00:03:06,190 --> 00:03:14,970 Now, for that, for the really cool stuff that we really like to do, there's an authentication mechanism. 48 00:03:14,970 --> 00:03:20,150 So the tool says, no, I'm not just some Joe Schmo, I'm supposed to be doing this. 49 00:03:20,570 --> 00:03:23,190 Give me all the goods, let me do all the cool things. 50 00:03:23,710 --> 00:03:25,690 Now, there are a couple different ways that this is done. 51 00:03:25,690 --> 00:03:26,750 Well, a number of different ways. 52 00:03:26,750 --> 00:03:29,630 But two primary standards of the way that this is done. 53 00:03:29,710 --> 00:03:37,490 But it generally comes with some sort of back and forth, a seed key exchange or some sort of authentication mechanisms like what you might like. 54 00:03:39,390 --> 00:03:45,970 And then, I won't get into exactly how I gain code execution at that point but there are many different cool things that you can do. 55 00:03:48,150 --> 00:03:54,330 Set diagnostics level is the way that you tell an ECU to go into a certain mode. 56 00:03:54,330 --> 00:03:58,290 Sometimes you can change parameters, sometimes you can update firmware. 57 00:03:59,050 --> 00:04:04,770 But then there's a security access hex 27 or an authentication hex 29. 58 00:04:04,770 --> 00:04:07,570 These are the service numbers that are used in the messages. 59 00:04:08,450 --> 00:04:12,970 And that sets up the back and forth authentication process. 60 00:04:14,550 --> 00:04:21,730 So it could be simple key exchange and we just walked through that basically. 61 00:04:22,870 --> 00:04:24,410 Moving right along. 62 00:04:29,250 --> 00:04:30,010 All right. 63 00:04:30,010 --> 00:04:42,910 So the goal for me in this project that I'm talking about is to gain access to the code that calculates the seed key exchange or the authentication mechanism. 64 00:04:43,390 --> 00:04:46,330 There are many different things that can go into that but I don't care. 65 00:04:46,330 --> 00:04:47,310 I want them all. 66 00:04:48,430 --> 00:04:58,270 This all started a couple years ago when one manufacturer that we already had access to added a new authentication mechanism that we didn't have access to. 67 00:05:00,290 --> 00:05:06,710 And so I made the decision to use this as a mentoring tool. 68 00:05:06,710 --> 00:05:18,790 Instead of going hog wild after it, I leaned into other people who were very capable, very skilled, very passionate and I allowed them to think on it, stew on it and work with me. 69 00:05:19,730 --> 00:05:22,110 But it did slow things down quite a bit. 70 00:05:23,890 --> 00:05:26,110 So I don't know if you can see this. 71 00:05:26,110 --> 00:05:28,210 If you can't, I apologize. 72 00:05:28,250 --> 00:05:31,970 Basically this is showing an obfuscated program. 73 00:05:32,310 --> 00:05:49,470 So when you install this tool, I'm not going to mention who, the tool has one executable program on a Windows system and as soon as you load it up in your favorite disassembler, the entry code does some weird stuff. 74 00:05:49,470 --> 00:05:53,750 It pushes a value onto the stack, it makes a call and then it has a return. 75 00:05:54,290 --> 00:05:56,390 Well, that call is literally a return. 76 00:05:56,390 --> 00:05:57,670 It's a no op call. 77 00:05:58,250 --> 00:06:03,070 So what happens when you push something on the stack and then you return? 78 00:06:03,990 --> 00:06:05,230 Well, that's a jump. 79 00:06:05,230 --> 00:06:10,550 It's very common to, it's a common thing to use to obfuscate what you're trying to do. 80 00:06:10,550 --> 00:06:13,210 Malware does this, software does this. 81 00:06:13,610 --> 00:06:16,650 And it's the makings of a very long day. 82 00:06:16,770 --> 00:06:20,850 Now there are a number of ways to deal with obfuscated programs. 83 00:06:21,890 --> 00:06:24,830 More than I listed here, but these are the two that hit my brain. 84 00:06:24,830 --> 00:06:26,510 They tickle my fancy. 85 00:06:26,510 --> 00:06:30,390 I like to emulate through things and do analysis through emulation. 86 00:06:30,390 --> 00:06:32,990 You probably heard a talk or two from me on that. 87 00:06:33,650 --> 00:06:37,590 And what I'm calling snatch and grab and that's actually what we're going to talk about here today. 88 00:06:38,170 --> 00:06:41,570 From an emulator perspective, you might spin up QMU. 89 00:06:41,570 --> 00:06:52,890 QMU allows you to do all sorts of cool emulation things and it builds in a GDB debugger component where you can halt something and do twiddle and all sorts of fun stuff. 90 00:06:53,050 --> 00:07:07,050 Maybe kernel debugging, if we ever get to a point where we're anti-debugging is really good enough to stop me from using this type of mechanism, then kernel debugging will be another option. 91 00:07:07,050 --> 00:07:13,830 Vivisection has a lot of cool stuff, a tool that I released and actually spoke about last week at Black Hat. 92 00:07:13,990 --> 00:07:15,570 But not this time. 93 00:07:16,290 --> 00:07:18,450 No emulation in the initial things anyway. 94 00:07:18,450 --> 00:07:22,070 Emulation plays into pretty much everything that I do, but not for this. 95 00:07:23,290 --> 00:07:42,530 So snatch and grab, basically you start up the program, you run it, you connect it to the vehicle, you get all the functionality that you're really into and you make sure that it's all working, it's all obviously able to be executed. 96 00:07:42,710 --> 00:07:53,250 You attach with a debugger and there are issues that go around this, but anti-debugging, who's heard of it? 97 00:07:53,670 --> 00:07:57,250 Anybody tried to get around anti-debugging techniques? 98 00:07:58,230 --> 00:08:01,410 Well, we're not even going to worry about it. 99 00:08:01,410 --> 00:08:04,190 We attach, it is very difficult to make it so you can't attach. 100 00:08:04,190 --> 00:08:08,450 If you can't, if you actually can't attach, then kernel debugging becomes a thing. 101 00:08:09,590 --> 00:08:15,890 Then we grab all the data and we store it and analyze it. 102 00:08:15,890 --> 00:08:17,230 I mean, it sounds simple, right? 103 00:08:18,910 --> 00:08:23,270 So for this I used a tool called Vivisect, my favorite reversing tool. 104 00:08:23,570 --> 00:08:35,670 It has a built-in vulnerability debugger so we use VDB, attach to the running program, dump what the reason I wanted to use VDB is because VDB has a V snapshot. 105 00:08:36,570 --> 00:08:37,810 V plays into everything. 106 00:08:37,810 --> 00:08:40,110 It's kind of like V for Vendetta but Visigoth. 107 00:08:43,150 --> 00:08:44,830 And then magic happens. 108 00:08:45,630 --> 00:08:46,770 So here's a picture. 109 00:08:49,300 --> 00:08:52,460 If that's too much in my chart, here's a closer picture. 110 00:08:52,460 --> 00:09:10,800 Basically it's just an attach and we say as soon as the attach is done, run snapshot and it dumps all the memory maps out to a file including processor state and so on and so forth so that we can easily analyze the full system state at that point. 111 00:09:13,000 --> 00:09:15,820 And here's the actual, I had to zoom in yet again. 112 00:09:17,020 --> 00:09:19,380 So snapshot target.vsnap. 113 00:09:24,500 --> 00:09:29,700 The next step was to import the data from the snapshot into a Vivisect work space. 114 00:09:29,700 --> 00:09:36,680 Vivisect work space is basically holds all the things that you're analyzing when working with Vivisect doing reverse engineering. 115 00:09:37,240 --> 00:09:43,740 And you can use this for many, many things including symbolic analysis and emulation but we won't get there right now. 116 00:09:44,100 --> 00:09:47,600 It's basically, it was a manual process when I started this. 117 00:09:49,940 --> 00:09:52,320 It's continued to take shape. 118 00:09:52,420 --> 00:09:57,720 This will be wrapped into Vivisection shortly. 119 00:09:57,760 --> 00:10:00,160 Like within a week or two. 120 00:10:00,160 --> 00:10:01,760 I will be releasing next week. 121 00:10:04,000 --> 00:10:08,020 So basically we copy all of the memory maps. 122 00:10:08,480 --> 00:10:15,280 Show of hands, who has done debugging before and understands what a memory map is? 123 00:10:16,300 --> 00:10:28,020 If you do like info proc maps in GDB or cat in a Linux system with a proc file system, cat slash proc slash PID slash maps and it will list. 124 00:10:28,060 --> 00:10:30,420 Here are the different memory maps that exist. 125 00:10:30,420 --> 00:10:34,940 These four or five are from this program or this library and whatnot and so forth. 126 00:10:35,420 --> 00:10:37,220 That's what I'm talking about. 127 00:10:37,360 --> 00:10:40,120 So we just take, that's all been snapshotted away. 128 00:10:40,120 --> 00:10:42,180 We grab the snapshot memory maps. 129 00:10:42,180 --> 00:10:58,860 We copy them into a Vivisect work space and then we do a little magic that says we have all the data, the PE that's been loaded into memory has all of its own data, symbol data, names, things like that. 130 00:10:59,200 --> 00:11:03,800 And we tell the work space to just steal that data. 131 00:11:05,320 --> 00:11:06,680 I'm giving you eye charts. 132 00:11:06,680 --> 00:11:12,060 We're skipping over quite a few slides here. 133 00:11:12,300 --> 00:11:18,760 Because at the end of the day, this was stolen from the built in Vivisect parser. 134 00:11:18,760 --> 00:11:25,200 We just removed a few things that loaded the memory maps back in again. 135 00:11:25,780 --> 00:11:28,380 And that's the thing that I'm going to be wrapping into Vivisection. 136 00:11:28,380 --> 00:11:29,960 Reach out to me if you want it. 137 00:11:30,180 --> 00:11:32,640 You've got contact information for me on the slides. 138 00:11:32,640 --> 00:11:34,580 The slides should be available online. 139 00:11:36,100 --> 00:11:36,800 Okay. 140 00:11:36,800 --> 00:11:43,120 So there are a number of different ways that obfuscation is generally done. 141 00:11:43,580 --> 00:11:45,660 Any malware reverse engineers here? 142 00:11:47,120 --> 00:11:49,260 How about malware forward engineers? 143 00:11:52,040 --> 00:11:59,040 So there are three primary ones that I run into when I've done malware reversing or obfuscation. 144 00:11:59,280 --> 00:12:02,340 The first one is unpack or decrypt in place. 145 00:12:02,660 --> 00:12:14,740 So the program loads up, the loader creates these memory maps and the initial part of the code does a lot of really annoying things. 146 00:12:15,100 --> 00:12:21,060 But then it gets to a point where it decrypts itself into the existing memory maps. 147 00:12:21,860 --> 00:12:23,840 So that's one thing to be looking for. 148 00:12:23,840 --> 00:12:26,200 Because we're always looking for what's important. 149 00:12:26,200 --> 00:12:27,440 What are we interested in? 150 00:12:27,440 --> 00:12:30,200 The next one is to unpack or decrypt into new memory maps. 151 00:12:30,200 --> 00:12:36,960 So it just calls basically give me a new memory map, I will shit my stuff into it and then we'll execute from there. 152 00:12:36,960 --> 00:12:39,920 So anonymous memory maps may be very interesting as well. 153 00:12:39,920 --> 00:12:43,620 You have named memory maps, you have anonymous memory maps. 154 00:12:45,060 --> 00:12:48,620 And then process hollowing or some variation thereof. 155 00:12:48,620 --> 00:13:00,580 So let's say you've got your malware or your obfuscated thing, starts another process and then decrypts its stuff across into that other process much like a debugger would. 156 00:13:00,880 --> 00:13:03,520 So these are all interesting things we're looking for. 157 00:13:03,520 --> 00:13:05,320 There are many variations on it. 158 00:13:06,560 --> 00:13:16,800 And determine very quickly by comparing what I had dumped into the Vivisect workspace to what the original binary loaded in the Vivisect workspace. 159 00:13:16,800 --> 00:13:20,320 And this was a decrypt in place binary. 160 00:13:20,640 --> 00:13:29,060 So that gives me an understanding of at least where I do need to focus, maybe not cutting out where I don't. 161 00:13:30,380 --> 00:13:31,900 So let's go searching for things. 162 00:13:31,900 --> 00:13:42,010 Reverse engineering, you load a thing up into Ghidra or Ida or Binja and you want to do things, how do you do those things? 163 00:13:42,010 --> 00:13:49,710 I want to go find hex UDS things, hex 29, hex 27 as immediate constants. 164 00:13:50,370 --> 00:13:51,310 Uh-oh. 165 00:13:52,570 --> 00:13:57,110 You see, we don't know anything about what we just dumped into the workspace. 166 00:13:57,710 --> 00:14:01,030 There's no code that's been identified. 167 00:14:01,030 --> 00:14:02,790 There are no strings that have been identified. 168 00:14:02,790 --> 00:14:13,630 You can manually go through the 30 meg of memory maps and manually look for strings, but, yeah, I don't have that kind of time. 169 00:14:17,710 --> 00:14:23,690 So here actually is a picture of Vivisect where I'm looking at a string that I want to find. 170 00:14:24,330 --> 00:14:26,670 But it's not identified as a string . 171 00:14:28,030 --> 00:14:36,530 In fact, nothing is except stuff that the loader told me was a string by going through the magic that we did before. 172 00:14:38,890 --> 00:14:40,930 So here's a different view of it. 173 00:14:41,210 --> 00:14:43,450 Maybe you prefer this view. 174 00:14:44,410 --> 00:14:47,450 And, of course, my laser pointer cannot show up on the screen. 175 00:14:50,170 --> 00:14:59,330 So on the right-hand side, if you can see this slide and you have great eyes, you can see near the bottom, there starts to be some ASCII string looking things. 176 00:14:59,690 --> 00:14:59,930 Okay. 177 00:14:59,930 --> 00:15:00,650 Really nifty. 178 00:15:00,650 --> 00:15:02,350 Including security array. 179 00:15:02,410 --> 00:15:04,550 I'm very interested in security array. 180 00:15:04,550 --> 00:15:08,150 Anything that starts in security is very interesting to me. 181 00:15:09,710 --> 00:15:12,470 But, unfortunately, Viv doesn't know that that's a string yet. 182 00:15:14,790 --> 00:15:19,490 When I first did this, I actually just saved the workspace out and the workspace is unencrypted. 183 00:15:19,590 --> 00:15:25,370 And I just ran strings literally on the workspace to get an idea of what was there. 184 00:15:25,370 --> 00:15:26,950 Because they were decrypted. 185 00:15:26,990 --> 00:15:28,990 They were de-obfuscated. 186 00:15:29,850 --> 00:15:40,410 And when I found an interesting thing, and there are millions of strings, when I found an interesting thing, I went back into Vivisect and found... Hi, there. 187 00:15:41,670 --> 00:15:43,130 I held down the wrong button. 188 00:16:07,540 --> 00:16:08,260 Are we back? 189 00:16:08,260 --> 00:16:08,500 No? 190 00:16:08,500 --> 00:16:09,120 Not yet? 191 00:16:10,180 --> 00:16:10,840 What? 192 00:16:29,820 --> 00:16:31,660 Probably see it just as easily, too. 193 00:16:42,890 --> 00:16:43,750 New pointer. 194 00:16:43,750 --> 00:16:44,790 I apologize. 195 00:16:45,510 --> 00:16:46,830 Not used to it yet. 196 00:16:49,190 --> 00:16:49,990 Okay. 197 00:16:52,710 --> 00:16:54,410 It does go fast, though. 198 00:17:00,560 --> 00:17:03,980 So once I found a string that was interesting, I'd go into the Vivisect CLI. 199 00:17:03,980 --> 00:17:06,820 There's a command line at the bottom and I just type search and I look for that string. 200 00:17:06,820 --> 00:17:10,640 It shows up and I can start turning it into strings by pressing S. 201 00:17:14,640 --> 00:17:14,980 All right. 202 00:17:14,980 --> 00:17:22,460 About a week ago, I wrote a Vivisection plug-in component that allows me just to do that and make that available to you guys as well. 203 00:17:22,840 --> 00:17:25,100 The Vivisection scan for strings. 204 00:17:27,320 --> 00:17:37,540 It brings up a starting memory map, an ending memory map and if that's not granular enough, a starting address and an ending address, do you want to apply the strings when you find them? 205 00:17:37,540 --> 00:17:39,320 Like shove them into the workspace? 206 00:17:39,320 --> 00:17:40,680 And I said no. 207 00:17:41,960 --> 00:17:49,900 Because it turns out strings captures a whole bunch of binary code that just kind of happens to be alphanumeric. 208 00:17:50,660 --> 00:17:51,980 So let's take a look at this. 209 00:17:51,980 --> 00:17:53,280 I'm looking for ASCII strings. 210 00:17:53,280 --> 00:17:57,140 I can look for you in a code and it allows you to set the minimum size. 211 00:17:57,780 --> 00:17:59,340 Defaults to five characters. 212 00:18:00,440 --> 00:18:01,700 That's generally good for me. 213 00:18:02,060 --> 00:18:13,100 So when we're done, if you can see this over here, because I told it not to apply the strings, it popped up a widget that just printed out. 214 00:18:13,100 --> 00:18:14,200 Here's the location. 215 00:18:14,420 --> 00:18:15,560 Here's the strings. 216 00:18:15,620 --> 00:18:19,340 I did not make it so that you could click on the location yet. 217 00:18:19,440 --> 00:18:20,960 That's coming next week. 218 00:18:20,960 --> 00:18:21,920 It's been very busy. 219 00:18:23,460 --> 00:18:30,560 So I get an idea and I'm able to scroll through and go, oh, this chunk of code is actually ‑‑ or this chunk of strings is really not strings. 220 00:18:33,230 --> 00:18:35,410 So I don't want all the strings. 221 00:18:35,650 --> 00:18:44,190 So I zero in on a memory address range where I'm confident that those strings are what I want and they are real strings. 222 00:18:48,550 --> 00:18:50,990 So now I have strings. 223 00:18:50,990 --> 00:18:52,870 Look, security access is right there. 224 00:18:52,870 --> 00:18:56,170 DPF, automotive stuff. 225 00:18:56,170 --> 00:18:57,870 If that makes you happy, that makes you happy. 226 00:18:59,050 --> 00:19:03,730 But I see some interesting things that aren't strings and I'm curious about them. 227 00:19:03,850 --> 00:19:04,950 We'll come back to it. 228 00:19:06,790 --> 00:19:09,790 X86, 32 or 64 bit, doesn't matter. 229 00:19:09,790 --> 00:19:21,690 It's a variable length instruction set and it's really important that we get it right so that when we call something code, it is code indeed. 230 00:19:21,910 --> 00:19:27,430 So if you get off by one byte, you can tank an entire understanding of a segment of code. 231 00:19:29,310 --> 00:19:32,930 So how do we accurately identify code? 232 00:19:34,450 --> 00:19:38,290 Well, it turns out there are 12 stack areas. 233 00:19:39,710 --> 00:19:40,110 All right. 234 00:19:40,110 --> 00:19:40,870 I gave you a hint. 235 00:19:40,870 --> 00:19:42,810 Actually, I'm taking hands. 236 00:19:42,870 --> 00:19:47,550 How do I identify known good code? 237 00:19:48,350 --> 00:19:49,470 Yell it out. 238 00:19:51,290 --> 00:19:56,130 Look, do you know how many things are good op codes that are not real? 239 00:19:59,020 --> 00:19:59,900 What's that? 240 00:20:04,050 --> 00:20:04,810 Ding, ding, ding. 241 00:20:04,810 --> 00:20:07,430 Give Travis a big hug. 242 00:20:07,430 --> 00:20:08,390 I'll get you later. 243 00:20:09,110 --> 00:20:10,070 Yes. 244 00:20:10,270 --> 00:20:13,450 So with the hint, he didn't need it. 245 00:20:14,270 --> 00:20:16,230 We have 12 stacks identified. 246 00:20:16,230 --> 00:20:18,910 One for each thread that's running in the system. 247 00:20:20,130 --> 00:20:22,450 Each one has its own stack. 248 00:20:22,450 --> 00:20:24,750 In the stack, what's stored there? 249 00:20:25,630 --> 00:20:26,690 Return pointers. 250 00:20:27,830 --> 00:20:37,310 And a whole bunch of other fucking pointers that are very annoying and they're all large, but it's a great teaching thing. 251 00:20:37,690 --> 00:20:39,970 How do I know if a pointer is a return ? 252 00:20:40,770 --> 00:20:42,710 Well, first of all, we go to the pointer. 253 00:20:43,050 --> 00:20:48,190 And on the top, you see a 32 bit representation of stuff around the stack. 254 00:20:49,090 --> 00:20:55,450 At this address, 3F7FDD4, we see an address 5143A6. 255 00:20:55,450 --> 00:20:58,430 It's blue because that's a valid memory address. 256 00:20:59,950 --> 00:21:01,550 And we back up. 257 00:21:01,610 --> 00:21:02,770 Two bytes. 258 00:21:03,090 --> 00:21:04,390 And see if it disassembles. 259 00:21:04,390 --> 00:21:10,490 And now we're into what you're saying, but it's ‑‑ we're into what you were saying a second ago, but there's more to it. 260 00:21:10,490 --> 00:21:13,450 So sorry if I shut you down too quickly. 261 00:21:13,890 --> 00:21:15,350 So we back up two bytes. 262 00:21:15,350 --> 00:21:15,770 Why? 263 00:21:15,770 --> 00:21:20,010 Because the smallest call in x86 is two bytes long. 264 00:21:20,270 --> 00:21:22,790 The longest call is like eight bytes. 265 00:21:23,350 --> 00:21:24,290 So we back up two bytes. 266 00:21:24,290 --> 00:21:26,070 Does it disassemble into a call? 267 00:21:26,810 --> 00:21:27,470 Yes. 268 00:21:27,470 --> 00:21:27,810 Okay. 269 00:21:27,810 --> 00:21:28,210 Cool. 270 00:21:28,210 --> 00:21:29,650 Now wait. 271 00:21:30,130 --> 00:21:32,870 Does that call stop? 272 00:21:32,950 --> 00:21:34,870 Because everyone's got its own size. 273 00:21:34,870 --> 00:21:38,850 Does it stop at the pointer that we're trying to investigate? 274 00:21:39,110 --> 00:21:39,570 No. 275 00:21:39,570 --> 00:21:39,990 Shit. 276 00:21:39,990 --> 00:21:40,630 Go back. 277 00:21:40,630 --> 00:21:40,870 Okay. 278 00:21:40,870 --> 00:21:41,610 Three bytes. 279 00:21:41,610 --> 00:21:42,130 Four bytes. 280 00:21:42,130 --> 00:21:42,610 Five. 281 00:21:42,610 --> 00:21:43,510 Six bytes. 282 00:21:43,510 --> 00:21:44,210 Whatever. 283 00:21:44,610 --> 00:21:45,870 We get a call. 284 00:21:46,310 --> 00:21:50,670 The instruction itself ends at the boundary where the pointer is pointing. 285 00:21:51,330 --> 00:21:55,250 Because that's what the ‑‑ that's what a return call is, is to go to the next thing. 286 00:21:57,450 --> 00:22:00,830 And that is a pretty good likelihood that that's a return pointer. 287 00:22:02,930 --> 00:22:07,430 So I go to that location and I say C to make code. 288 00:22:08,750 --> 00:22:13,110 And sure enough we have a call to this address which is also valid. 289 00:22:13,610 --> 00:22:15,590 We have a jump after that. 290 00:22:16,130 --> 00:22:19,670 And then basically a drop down to a RET. 291 00:22:19,670 --> 00:22:23,330 So this is looking very much like very good code. 292 00:22:24,490 --> 00:22:26,730 But that is a fuck ton of work. 293 00:22:27,770 --> 00:22:38,830 And after letting my mentee stew on it for a day, I gave her a script and said, so here's a script that will kind of do that manually or automatically for you. 294 00:22:39,050 --> 00:22:44,030 And that script I was going to include in the deck here, I am not because I wrapped it into Vivisection. 295 00:22:44,030 --> 00:22:46,510 You'll get to see that when I cut the next release. 296 00:22:47,690 --> 00:22:52,230 But, whoops, here's the problem. 297 00:22:52,610 --> 00:22:57,210 When I did that, code started getting identified. 298 00:22:57,370 --> 00:23:02,750 Cross references, strings, like magic was happening and then boom, out of memory. 299 00:23:06,180 --> 00:23:14,560 I ended up analyzing this overnight on a machine where I gave myself an 80 gig swap space just to see if it would work. 300 00:23:14,560 --> 00:23:15,700 Binja did the same thing. 301 00:23:15,700 --> 00:23:16,500 Barfed. 302 00:23:19,580 --> 00:23:26,480 And most of my work ended up being on a 500 plus megabyte work space. 303 00:23:26,580 --> 00:23:32,220 Which I don't know about you or how big your work spaces are, but this takes ten minutes to load. 304 00:23:32,880 --> 00:23:39,440 And if I'm doing it over a network, because Vivisect has a Vivisect server and client mode, it takes 45 minutes to load. 305 00:23:39,660 --> 00:23:40,320 Okay. 306 00:23:40,640 --> 00:23:42,020 Need to work on this one. 307 00:23:42,020 --> 00:23:44,720 And we're in uncut territory here. 308 00:23:44,920 --> 00:23:46,340 That's fine, I guess. 309 00:23:47,640 --> 00:23:52,700 So, it also makes function recon completely useless. 310 00:23:52,700 --> 00:23:57,780 I don't know, I just released function recon in Vivisection so you might not have had a chance to use it. 311 00:23:57,900 --> 00:24:04,960 But function recon basically straps in a specialized partial emulator into a function. 312 00:24:05,540 --> 00:24:09,340 And it follows every code path exactly once. 313 00:24:09,920 --> 00:24:12,320 And then every call, it does that, too. 314 00:24:12,320 --> 00:24:16,860 And it keeps going and going and going until it runs out of code to hit. 315 00:24:16,980 --> 00:24:19,700 And it captures things like what strings did I run into? 316 00:24:19,760 --> 00:24:21,560 What function calls did I run to? 317 00:24:21,560 --> 00:24:23,840 What named functions did I call? 318 00:24:24,000 --> 00:24:29,180 Basically, reconnaissance on what a given subset of a program is going to do. 319 00:24:29,420 --> 00:24:33,120 Function recon is worthless on this binary. 320 00:24:33,120 --> 00:24:34,420 Or on this work space. 321 00:24:34,480 --> 00:24:43,900 Because I analyzed all of NTDLL and user 32 and kernel 32 and all the things that I really don't give a shit about. 322 00:24:44,080 --> 00:24:46,120 Because it turns out we know what that is. 323 00:24:46,120 --> 00:24:47,420 That's not obfuscated. 324 00:24:48,340 --> 00:24:49,880 That would be a really dirty trick. 325 00:24:49,880 --> 00:24:51,560 But I did check. 326 00:24:51,560 --> 00:24:53,920 This is legitimate NTDLL and whatnot. 327 00:24:57,350 --> 00:25:02,290 So, sometime by next week I could probably get done with function recon. 328 00:25:03,430 --> 00:25:07,130 But I think I'd like to make a change and not do that. 329 00:25:07,650 --> 00:25:09,770 Oh, and I broke the Viv server. 330 00:25:10,950 --> 00:25:21,070 Didn't not just work, it literally broke it where it would not try to send the data over the network. 331 00:25:21,150 --> 00:25:22,310 So, I ended up fixing that. 332 00:25:22,310 --> 00:25:23,830 That was months ago. 333 00:25:28,740 --> 00:25:36,720 So, I did wrap this into Vivisection because the goal of Vivisection is to kill all TDM and brain drain. 334 00:25:37,240 --> 00:25:43,560 We need your brains to be able to chew on the more important things, not just do these tedious horrible things. 335 00:25:44,760 --> 00:25:49,320 And we focus on starting maps and ending maps for stack. 336 00:25:49,420 --> 00:25:51,320 So, we look for the pointers here. 337 00:25:51,420 --> 00:25:56,320 And we can restrict where the pointers can point to that we care about. 338 00:25:56,320 --> 00:25:57,880 So, that we can skip NTDLL. 339 00:25:57,880 --> 00:26:00,040 We can skip kernel 32 DLL. 340 00:26:01,060 --> 00:26:11,000 Then I have a pull request in for the proper to allow us to turn on what's called one library. 341 00:26:11,980 --> 00:26:21,620 So, that when you analyze a function and it hits outside that library, it jumps or it calls into NTDLL, for example, it just stops . 342 00:26:21,700 --> 00:26:26,160 Because the NTDLL is going to have a name and that's kind of how we do reversing, right? 343 00:26:29,580 --> 00:26:34,740 So , next, once we found strings, we have this one problem. 344 00:26:35,060 --> 00:26:36,740 There's no cross references. 345 00:26:37,160 --> 00:26:42,880 So, the idea is we go to a string and then we have code that references the string and so the code is instantly interesting to me. 346 00:26:44,140 --> 00:26:45,840 Nothing is referring to these strings. 347 00:26:45,840 --> 00:26:47,420 We just told them that it was strings. 348 00:26:48,480 --> 00:26:49,180 Okay. 349 00:26:49,180 --> 00:26:50,220 Now what? 350 00:26:50,680 --> 00:27:03,820 Well, while doing this particular tool, I noticed that around these groupings of strings that were very interesting to me were a whole bunch of non-strings and it didn't look like code either. 351 00:27:03,820 --> 00:27:05,840 I started seeing a pattern in them. 352 00:27:06,940 --> 00:27:10,440 It was number, number, number, zero. 353 00:27:10,900 --> 00:27:13,860 Number, number, number, zero. 354 00:27:14,060 --> 00:27:15,700 Number, number, number, zero. 355 00:27:15,700 --> 00:27:25,220 And in this area of code, the address space is held within 24 bits. 356 00:27:25,940 --> 00:27:31,280 It is look at the address is 0075D289. 357 00:27:31,280 --> 00:27:39,180 In fact, the target binary starts at 400,000 hex and ends at like 80A000. 358 00:27:39,440 --> 00:27:41,680 That was really interesting to me. 359 00:27:41,760 --> 00:27:52,800 And as you can see, these little byte groupings are valid pointers into this target binary address space. 360 00:27:52,920 --> 00:27:57,500 So, I started going through and just hitting, going to one and hitting P. 361 00:27:57,500 --> 00:27:58,320 Make it a pointer. 362 00:27:58,320 --> 00:28:04,600 And suddenly, that P goes through the analysis process for the pointer and it says, oh, suddenly I'm pointing at a function. 363 00:28:05,080 --> 00:28:06,840 Because it starts with a name sub. 364 00:28:06,920 --> 00:28:08,480 Oh, that's curious. 365 00:28:09,300 --> 00:28:13,520 So, I did that with a whole bunch of them and they were all functions. 366 00:28:14,580 --> 00:28:20,360 So much so that I ended up writing a Vivisection, actually, no, I think I wrapped this into Vivisect itself. 367 00:28:21,880 --> 00:28:27,020 Writing code that just allows you to make pointers and you just say, here's the start, here's the stop, just make them all pointers. 368 00:28:31,540 --> 00:28:36,700 Turns out there are vast arrays of pointers to a whole ton of code. 369 00:28:37,580 --> 00:28:42,860 They end up being virtual function tables for C++. 370 00:28:42,860 --> 00:28:44,760 Is that familiar with anyone? 371 00:28:44,880 --> 00:28:46,360 C++ reversing. 372 00:28:47,140 --> 00:28:48,100 Kudos to you. 373 00:28:48,100 --> 00:28:49,160 It's not fun. 374 00:28:50,700 --> 00:29:10,600 So, we end up with these tables of functions that are used so when you have the class object ball and you want to call the function bounce or get color or change color, they're just held in this table here and the code knows that bounce is offset 4 into this table. 375 00:29:10,620 --> 00:29:12,040 Make sense? 376 00:29:13,780 --> 00:29:23,180 And all of a sudden, while I'm digging through here, making all sorts of pointers, I started seeing cross references to my strings. 377 00:29:24,980 --> 00:29:31,740 So, now I know some code is referring to security array. 378 00:29:32,060 --> 00:29:33,620 I'm getting better. 379 00:29:33,620 --> 00:29:34,780 Getting happier. 380 00:29:35,160 --> 00:29:41,820 And then I made this, so here's the Vivisect code or functionality to create a pointer array. 381 00:29:41,820 --> 00:29:52,300 Actually, if you just hold down shift and hit P, capital P is pointer array, pops up a dialogue, here's a starting address, here's a stop address, or if you'd rather, this many. 382 00:29:52,600 --> 00:29:56,220 So, you can put in a number into the count and it will ignore the stop address. 383 00:29:56,220 --> 00:29:57,340 It will just do that many. 384 00:29:57,660 --> 00:30:03,420 And then if we run into a null pointer, do we care and do we keep going or do we call that the end? 385 00:30:03,560 --> 00:30:07,340 So, these are all useful to me and so I put them in here. 386 00:30:10,250 --> 00:30:20,270 So, the V table entries, I don't know if you can see that, but the first entry in most virtual tables is called a destructor. 387 00:30:20,310 --> 00:30:22,030 C++ programmers? 388 00:30:22,250 --> 00:30:23,230 Again? 389 00:30:23,950 --> 00:30:25,430 Okay, a number of you. 390 00:30:25,470 --> 00:30:30,950 When you create a class, you write code for a constructor and a destructor. 391 00:30:30,950 --> 00:30:44,330 When you instantiate the object, it runs the constructor which basically tells, it sets up the different data structures in place, create strings, create sub objects, other things like that. 392 00:30:44,330 --> 00:30:48,950 A destructor is tasked with making sure that everything gets cleaned up appropriately. 393 00:30:48,950 --> 00:31:06,890 So, if you have a C++ class that has a bunch of strings in it, your destructor will roll through and say, all right, do this destructor thing for the strings here, strings there, strings here and otherwise or other C++ objects that are wrapped in. 394 00:31:08,950 --> 00:31:14,750 So, the destructor is a highly valuable resource. 395 00:31:16,170 --> 00:31:17,490 How much time do I have? 396 00:31:21,430 --> 00:31:22,990 I'm going faster than I thought. 397 00:31:23,610 --> 00:31:25,090 No blood dripping out of your ears? 398 00:31:25,090 --> 00:31:25,950 I'm doing good. 399 00:31:25,950 --> 00:31:26,590 All right. 400 00:31:27,470 --> 00:31:35,610 So, when we get involved in C++ analysis, no, don't worry, I'm not going to take you too far down that road. 401 00:31:35,610 --> 00:31:36,770 I didn't warn you enough. 402 00:31:37,590 --> 00:31:41,110 And it is, actually, I did, it's another talk. 403 00:31:41,110 --> 00:31:43,910 I need to give another talk on this particular thing. 404 00:31:44,830 --> 00:31:47,750 C++ objects, what creates them? 405 00:31:47,750 --> 00:31:52,450 It's really important because once you create a C++ object, you just kind of tote it around. 406 00:31:52,730 --> 00:32:08,290 You know, I make the function call over here, here's the C++ object and the C++ objects, the goal for object oriented programming is that that virtual function table maintains all the logic about how to do everything necessary with that object. 407 00:32:08,290 --> 00:32:09,690 So, it's kind of self contained. 408 00:32:10,270 --> 00:32:16,070 And then I just say, call this function on here's my ball, you know, or whatever the object is. 409 00:32:16,490 --> 00:32:20,610 And then that function says, I know that a ball can do a bounce. 410 00:32:21,170 --> 00:32:26,010 I don't have to know what that thing is, you know, what goes into bouncing that ball. 411 00:32:26,010 --> 00:32:28,950 I know that I can call bounce on that ball and it will be good. 412 00:32:29,890 --> 00:32:34,890 So, tracing V tables, very, very interesting. 413 00:32:34,890 --> 00:32:46,090 So, about a dozen years ago I spent some time writing symbolic analysis for structures and building in structure reconstitution, basically. 414 00:32:46,110 --> 00:32:52,090 Using a set of code and symbolic data about what the code is doing. 415 00:32:52,090 --> 00:33:01,770 And it's a simple or difficult way of saying the symbolic data is this is this pointer plus 12. 416 00:33:01,770 --> 00:33:08,370 Instead of this pointer and then a bunch of instructions that modify what that register is doing. 417 00:33:08,370 --> 00:33:16,290 The symbolic analysis just traces that and makes it easy for me to say, oh, this is offset 12 into that structure, all golden. 418 00:33:21,320 --> 00:33:35,800 So, the first thing that I'm wanting to do when I do C++ structure analysis is to go through all the functions in the V table because the V table lists all functions that know how to do that thing with that ball. 419 00:33:36,180 --> 00:33:39,520 Or that diagnostics utility. 420 00:33:40,560 --> 00:33:51,080 I then analyze how this pointer is being accessed and I can identify what things go where in memory. 421 00:33:51,460 --> 00:33:59,760 That becomes really important when I'm looking at other code so that I know what offset 12 really means. 422 00:34:00,520 --> 00:34:08,170 Other things, as I said, the destructor is very, very powerful. 423 00:34:08,430 --> 00:34:09,870 And I don't know if you can see that. 424 00:34:10,050 --> 00:34:14,890 This is the first function in one of the V tables we looked at earlier. 425 00:34:15,670 --> 00:34:17,550 And it looks really simple. 426 00:34:17,770 --> 00:34:20,630 It calls free object, that's what I call it. 427 00:34:20,630 --> 00:34:23,110 This is sub underscore some number. 428 00:34:23,850 --> 00:34:29,910 So, free object, this object and I like to name things by the address of the virtual function table. 429 00:34:31,230 --> 00:34:40,430 And then down here based on what's returned or actually based on the arg one, we decide whether or not to decrement and free that object. 430 00:34:40,790 --> 00:34:41,090 Okay. 431 00:34:41,090 --> 00:34:42,350 Seems simple enough. 432 00:34:43,790 --> 00:34:47,050 But free object is where all the really fun magic happens. 433 00:34:47,390 --> 00:34:56,090 So, free object goes through whatever this class or this object is and does the real dirty magic. 434 00:34:56,350 --> 00:35:01,210 You'll notice here something that is often found in dealing with C++ code. 435 00:35:01,510 --> 00:35:05,430 At the top of this little box, there's a move D word. 436 00:35:05,730 --> 00:35:09,830 So, ESI is being used as this pointer right now. 437 00:35:10,010 --> 00:35:18,730 And the very first pointer in that object is a pointer to a virtual function table. 438 00:35:19,230 --> 00:35:26,070 And one thing that you'll notice is that as C++ code is used in different ways. 439 00:35:26,070 --> 00:35:33,870 So, for example, we have a subclass that inherits from it called basketball. 440 00:35:34,130 --> 00:35:38,090 And then another one that is very different that inherits from ball called football. 441 00:35:38,390 --> 00:35:41,370 If you don't know what the difference is there, try to bounce them both. 442 00:35:42,390 --> 00:35:45,950 So, they both have their own bounce functions, but they're both a ball . 443 00:35:45,950 --> 00:35:52,910 So, sometimes what we'll do is we'll say, yeah, no, I really want the ball version of bounce . 444 00:35:52,910 --> 00:35:56,090 I don't actually want the basketball to have this control. 445 00:35:57,970 --> 00:36:06,950 So, oftentimes code will jam in the appropriate virtual function table for that when your code asks for that. 446 00:36:07,010 --> 00:36:10,970 So, as we're going through, this free object, ignore the top. 447 00:36:10,970 --> 00:36:14,650 It's basically just memory protection things. 448 00:36:15,910 --> 00:36:18,330 So, we jam in this virtual table. 449 00:36:18,330 --> 00:36:22,030 I just called it 6 because I didn't know what to do with it at that point. 450 00:36:22,790 --> 00:36:25,170 We then move 16 into EDI. 451 00:36:25,170 --> 00:36:27,250 That will be important very shortly. 452 00:36:28,410 --> 00:36:36,090 We compare EDI to this, this plus 608, this 608 offset. 453 00:36:36,290 --> 00:36:39,870 I'll tell you in advance, spoilers, it's a string . 454 00:36:39,870 --> 00:36:44,130 It's a C++ string, which is not like your ASCII string. 455 00:36:46,820 --> 00:36:51,320 We then move 7 into a local variable and move on. 456 00:36:51,320 --> 00:37:02,800 And based on whether or not this offset into that, this string basically, the size of the string is being compared against 16. 457 00:37:03,360 --> 00:37:15,960 If the string is 16 bytes or more, C++ will create a new object of the right size or something that's flexible and move the string into that. 458 00:37:15,960 --> 00:37:24,020 If the string is less than 16 bytes, then there's enough room in the C++ structure for a string to just house the string. 459 00:37:24,020 --> 00:37:30,200 So, this is why I know this is a string and that's a very common element for C++ reversing. 460 00:37:31,220 --> 00:37:32,680 So, we come down here. 461 00:37:33,820 --> 00:37:41,780 If it's greater, if it's 16 bytes or greater, then I know that I have a pointer to the thing that actually holds the ASCII string. 462 00:37:41,900 --> 00:37:43,260 So, I free that. 463 00:37:43,520 --> 00:37:59,040 And then I move down to the next thing and I compare 15 or I move 15 into EBP and I shove that all over to make sure that this string that we are in the process of deconstructing doesn't ever do that again. 464 00:37:59,040 --> 00:38:03,140 So, as soon as we free that memory, we say, okay, it's 15. 465 00:38:04,060 --> 00:38:05,020 That's not true. 466 00:38:05,020 --> 00:38:05,960 Actually, it's going to be zero. 467 00:38:05,960 --> 00:38:07,040 We're going to delete it. 468 00:38:07,060 --> 00:38:12,200 But, so, it moves down, compare EDI against another offset. 469 00:38:12,460 --> 00:38:13,920 So, some other string. 470 00:38:13,920 --> 00:38:16,520 We go down because EDI is still 16. 471 00:38:17,580 --> 00:38:24,640 So, we actually see in this particular object multiple points of clearly this is a string. 472 00:38:28,080 --> 00:38:36,400 In fact, we then run into another embedded object that's special and we call some code to clean it up. 473 00:38:36,400 --> 00:38:38,020 I won't go into it right now. 474 00:38:38,480 --> 00:38:39,740 But then we do another thing. 475 00:38:39,740 --> 00:38:41,420 Compare this thing against EDI. 476 00:38:41,420 --> 00:38:43,140 EDI is still 16. 477 00:38:44,320 --> 00:38:50,020 And if it's greater than 16, call decrement and free. 478 00:38:50,820 --> 00:38:53,480 And so on and so forth. 479 00:38:57,750 --> 00:39:06,490 So, just to recap C++ reconstitution approach, look for a whole bunch of function pointers. 480 00:39:06,490 --> 00:39:07,830 All grouped together. 481 00:39:10,790 --> 00:39:13,910 Look for cross references to the start of the branch. 482 00:39:13,910 --> 00:39:17,810 Is that the beginning of a bunch of pointers? 483 00:39:19,990 --> 00:39:23,870 Is that the end of a bunch of pointers? 484 00:39:25,350 --> 00:39:31,570 Analyze those functions for how they deal with the this pointer that's handed in. 485 00:39:31,570 --> 00:39:37,770 And every call and convention on every architecture has its own this pointer argument. 486 00:39:37,770 --> 00:39:39,010 Typically it's the first one. 487 00:39:39,230 --> 00:39:42,630 On 32 bit, you see a lot of this call that's ECX. 488 00:39:42,630 --> 00:39:43,930 That's why you're seeing that here. 489 00:39:44,370 --> 00:39:47,850 So, analyze the first function in the virtual function table. 490 00:39:51,220 --> 00:39:56,060 If it's a destructor, it will tell you almost everything else that you really need to know. 491 00:39:56,220 --> 00:39:59,180 And then make good use of it. 492 00:40:00,540 --> 00:40:03,040 I hope you didn't come here expecting Oday. 493 00:40:03,620 --> 00:40:05,940 That wasn't my bent. 494 00:40:06,040 --> 00:40:07,180 I'm a mentor . 495 00:40:08,280 --> 00:40:09,740 Hopefully I've taught you how to fish. 496 00:40:09,740 --> 00:40:10,960 Go fish for yourself. 497 00:40:12,180 --> 00:40:14,260 So, the real conclusion. 498 00:40:14,260 --> 00:40:15,600 Think outside the box. 499 00:40:15,940 --> 00:40:18,160 Make your tools do new things. 500 00:40:18,420 --> 00:40:22,360 If your tools are incapable of doing new things, make your own tools. 501 00:40:23,140 --> 00:40:25,860 Understand why things work the way they do. 502 00:40:26,980 --> 00:40:30,640 Because that leads you to be more powerful. 503 00:40:31,220 --> 00:40:32,260 Then go hunting. 504 00:40:32,780 --> 00:40:36,960 Figure out what you think the code or the behavior should look like. 505 00:40:37,500 --> 00:40:41,040 And use your brain to create new tooling. 506 00:40:41,640 --> 00:40:44,400 Use the existing tooling in creative new ways. 507 00:40:45,420 --> 00:40:47,220 Because you're the real power. 508 00:40:47,620 --> 00:40:49,560 Go forth and do great things.