[00:00.000 --> 00:06.860] Good afternoon DEF CON. Today we have a talk on automobiles, alcohol, blood, sweat and [00:06.860 --> 00:14.160] creative reversing of obfuscated car moding tool. And with our speaker Atlas. [00:14.940 --> 00:23.800] Thank you sir. Thank you Pauls. I'm sorry, True. Hi everyone, welcome. How many of you [00:23.800 --> 00:30.580] came because it had the word automotive in it? All right. Good. Obfuscation. Who came [00:30.580 --> 00:37.860] because it said obfuscation? Excellent. Well, thank you all. I don't care why you came. [00:37.860 --> 00:42.400] You're here. I'm very grateful for you. I hope that we have a lot of fun today. As per [00:42.400 --> 00:47.420] usual, if you've come to a talk of mine, I've crammed in way too much stuff and we're going [00:47.420 --> 00:54.840] to see just how well and how much we can fit in. And because of timing, I'm going to [00:54.840 --> 00:59.400] probably just come out here and look at the slides that you're seeing because I, yeah, [00:59.400 --> 01:06.640] whatever. So who is this Atlas dork? Hi. This is my home. I was created here at DEF CON in [01:08.360 --> 01:17.080] 2013. And this is my family. I'm a hacker of things. I ride a motorcycle. I've got three [01:17.080 --> 01:26.880] amazing daughters who have nothing to do with technology. If you dare, there is a QR code [01:26.880 --> 01:32.360] that has all sorts of ways to get a hold of me. I'll leave that up for about ten more [01:32.360 --> 01:43.430] seconds. So I work in R&D and cyber physical systems for a company called Grim. And Grim is [01:43.430 --> 01:52.570] represented. Stand up. My dear friends down here wore huge Grim T-shirts. Ask how many of [01:52.570 --> 02:05.830] them work for Grim. A lot of love here. All right. So my job is to break into things. And I [02:06.790 --> 02:12.330] set many goals. Some of them attainable in the near term. Many of them long-term goals. Some [02:12.330 --> 02:17.770] of them, like, if I do these three projects that are year long, then I'll achieve this thing. So [02:17.770 --> 02:24.150] I think in these many different ways. But my ultimate goal typically is remote code execution [02:24.150 --> 02:33.110] on a device thing in a running program. For cars, and particularly the small ECUs that run your [02:33.110 --> 02:39.590] cars, my favorite way to gain code execution on an ECU, like your brake controller or your power [02:39.590 --> 02:48.990] steering controller, is through the update process or diagnostics process. Your tool plugs [02:48.990 --> 02:56.750] in, I'm sorry, runs on Windows, there's some software, have a hardware tool plugs into the OBD2 [02:56.750 --> 03:06.190] port on your car and then talks over the vehicle CAN network to tell the car things what to do. [03:06.190 --> 03:14.090] Now, for that, for the really cool stuff that we really like to do, there's an authentication [03:14.090 --> 03:20.930] mechanism. So the tool says, no, I'm not just some Joe Schmo, I'm supposed to be doing this. Give [03:20.930 --> 03:25.230] me all the goods, let me do all the cool things. Now, there are a couple different ways that this [03:25.230 --> 03:29.330] is done. Well, a number of different ways. But two primary standards of the way that this is [03:29.330 --> 03:34.510] done. But it generally comes with some sort of back and forth, a seed key exchange or some sort [03:34.510 --> 03:42.730] of authentication mechanisms like what you might like. And then, I won't get into exactly how I [03:42.730 --> 03:48.650] gain code execution at that point but there are many different cool things that you can do. Set [03:48.650 --> 03:55.010] diagnostics level is the way that you tell an ECU to go into a certain mode. Sometimes you can [03:55.010 --> 04:02.530] change parameters, sometimes you can update firmware. But then there's a security access hex [04:02.530 --> 04:09.010] 27 or an authentication hex 29. These are the service numbers that are used in the messages. And [04:09.010 --> 04:19.870] that sets up the back and forth authentication process. So it could be simple key exchange and we [04:19.870 --> 04:33.250] just walked through that basically. Moving right along. All right. So the goal for me in this [04:33.250 --> 04:40.450] project that I'm talking about is to gain access to the code that calculates the seed key [04:40.450 --> 04:45.370] exchange or the authentication mechanism. There are many different things that can go into that [04:45.370 --> 04:52.330] but I don't care. I want them all. This all started a couple years ago when one manufacturer that we [04:52.330 --> 05:03.230] already had access to added a new authentication mechanism that we didn't have access to. And so [05:03.230 --> 05:10.090] I made the decision to use this as a mentoring tool. Instead of going hog wild after it, I leaned [05:10.090 --> 05:15.150] into other people who were very capable, very skilled, very passionate and I allowed them to [05:15.730 --> 05:24.650] think on it, stew on it and work with me. But it did slow things down quite a bit. So I don't [05:24.650 --> 05:31.150] know if you can see this. If you can't, I apologize. Basically this is showing an obfuscated [05:31.150 --> 05:38.130] program. So when you install this tool, I'm not going to mention who, the tool has one [05:38.130 --> 05:44.830] executable program on a Windows system and as soon as you load it up in your favorite [05:44.830 --> 05:51.550] disassembler, the entry code does some weird stuff. It pushes a value onto the stack, it makes [05:51.710 --> 05:58.690] a call and then it has a return. Well, that call is literally a return. It's a no op call. So [05:58.690 --> 06:05.230] what happens when you push something on the stack and then you return? Well, that's a jump. [06:05.230 --> 06:10.810] It's very common to, it's a common thing to use to obfuscate what you're trying to do. Malware [06:10.810 --> 06:17.650] does this, software does this. And it's the makings of a very long day. Now there are a number [06:17.650 --> 06:23.810] of ways to deal with obfuscated programs. More than I listed here, but these are the two that [06:23.810 --> 06:29.490] hit my brain. They tickle my fancy. I like to emulate through things and do analysis through [06:29.490 --> 06:35.050] emulation. You probably heard a talk or two from me on that. And what I'm calling snatch and [06:35.050 --> 06:39.150] grab and that's actually what we're going to talk about here today. From an emulator [06:39.150 --> 06:44.410] perspective, you might spin up QMU. QMU allows you to do all sorts of cool emulation things [06:44.410 --> 06:51.710] and it builds in a GDB debugger component where you can halt something and do twiddle and all [06:51.710 --> 06:59.070] sorts of fun stuff. Maybe kernel debugging, if we ever get to a point where we're anti-debugging [06:59.070 --> 07:05.410] is really good enough to stop me from using this type of mechanism, then kernel debugging [07:05.410 --> 07:10.650] will be another option. Vivisection has a lot of cool stuff, a tool that I released [07:10.650 --> 07:17.310] and actually spoke about last week at Black Hat. But not this time. No emulation in the [07:17.310 --> 07:21.210] initial things anyway. Emulation plays into pretty much everything that I do, but not [07:21.210 --> 07:32.110] for this. So snatch and grab, basically you start up the program, you run it, you connect [07:32.110 --> 07:37.970] it to the vehicle, you get all the functionality that you're really into and you make sure [07:37.970 --> 07:44.190] that it's all working, it's all obviously able to be executed. You attach with a debugger [07:44.190 --> 07:54.110] and there are issues that go around this, but anti-debugging, who's heard of it? Anybody [07:54.110 --> 08:01.130] tried to get around anti-debugging techniques? Well, we're not even going to worry about [08:01.130 --> 08:05.410] it. We attach, it is very difficult to make it so you can't attach. If you can't, if you [08:05.410 --> 08:12.870] actually can't attach, then kernel debugging becomes a thing. Then we grab all the data [08:12.870 --> 08:20.650] and we store it and analyze it. I mean, it sounds simple, right? So for this I used a [08:20.650 --> 08:26.730] tool called Vivisect, my favorite reversing tool. It has a built-in vulnerability debugger [08:26.730 --> 08:33.670] so we use VDB, attach to the running program, dump what the reason I wanted to use VDB is [08:33.670 --> 08:38.810] because VDB has a V snapshot. V plays into everything. It's kind of like V for Vendetta [08:38.810 --> 08:50.380] but Visigoth. And then magic happens. So here's a picture. If that's too much in my [08:50.380 --> 08:55.660] chart, here's a closer picture. Basically it's just an attach and we say as soon as [08:55.660 --> 09:02.960] the attach is done, run snapshot and it dumps all the memory maps out to a file including [09:02.960 --> 09:09.220] processor state and so on and so forth so that we can easily analyze the full system [09:09.220 --> 09:19.380] state at that point. And here's the actual, I had to zoom in yet again. So snapshot target.vsnap. [09:24.500 --> 09:30.000] The next step was to import the data from the snapshot into a Vivisect work space. Vivisect [09:30.000 --> 09:35.420] work space is basically holds all the things that you're analyzing when working with Vivisect [09:35.420 --> 09:41.060] doing reverse engineering. And you can use this for many, many things including symbolic [09:41.060 --> 09:45.840] analysis and emulation but we won't get there right now. It's basically, it was a manual [09:45.840 --> 09:56.700] process when I started this. It's continued to take shape. This will be wrapped into Vivisection [09:56.700 --> 10:06.420] shortly. Like within a week or two. I will be releasing next week. So basically we copy [10:06.420 --> 10:13.900] all of the memory maps. Show of hands, who has done debugging before and understands [10:13.900 --> 10:21.440] what a memory map is? If you do like info proc maps in GDB or cat in a Linux system [10:21.440 --> 10:28.320] with a proc file system, cat slash proc slash PID slash maps and it will list. Here are [10:28.320 --> 10:32.780] the different memory maps that exist. These four or five are from this program or this [10:32.780 --> 10:38.680] library and whatnot and so forth. That's what I'm talking about. So we just take, that's [10:38.680 --> 10:44.660] all been snapshotted away. We grab the snapshot memory maps. We copy them into a Vivisect [10:44.660 --> 10:54.160] work space and then we do a little magic that says we have all the data, the PE that's been [10:54.160 --> 11:00.020] loaded into memory has all of its own data, symbol data, names, things like that. And we [11:00.500 --> 11:07.040] tell the work space to just steal that data. I'm giving you eye charts. We're skipping [11:07.040 --> 11:17.520] over quite a few slides here. Because at the end of the day, this was stolen from the built [11:17.520 --> 11:24.420] in Vivisect parser. We just removed a few things that loaded the memory maps back in [11:24.420 --> 11:28.700] again. And that's the thing that I'm going to be wrapping into Vivisection. Reach out [11:28.700 --> 11:33.000] to me if you want it. You've got contact information for me on the slides. The slides [11:33.000 --> 11:41.120] should be available online. Okay. So there are a number of different ways that obfuscation [11:41.120 --> 11:48.300] is generally done. Any malware reverse engineers here? How about malware forward [11:48.300 --> 11:56.920] engineers? So there are three primary ones that I run into when I've done malware [11:56.920 --> 12:04.560] reversing or obfuscation. The first one is unpack or decrypt in place. So the program loads [12:05.220 --> 12:13.120] up, the loader creates these memory maps and the initial part of the code does a lot of [12:13.120 --> 12:19.960] really annoying things. But then it gets to a point where it decrypts itself into the existing [12:19.960 --> 12:25.560] memory maps. So that's one thing to be looking for. Because we're always looking for what's [12:25.560 --> 12:29.760] important. What are we interested in? The next one is to unpack or decrypt into new memory [12:29.760 --> 12:35.600] maps. So it just calls basically give me a new memory map, I will shit my stuff into it and [12:35.600 --> 12:40.120] then we'll execute from there. So anonymous memory maps may be very interesting as well. You [12:40.120 --> 12:47.360] have named memory maps, you have anonymous memory maps. And then process hollowing or some [12:47.360 --> 12:53.200] variation thereof. So let's say you've got your malware or your obfuscated thing, starts [12:53.200 --> 12:59.760] another process and then decrypts its stuff across into that other process much like a [12:59.760 --> 13:04.580] debugger would. So these are all interesting things we're looking for. There are many [13:04.580 --> 13:12.080] variations on it. And determine very quickly by comparing what I had dumped into the [13:12.080 --> 13:17.820] Vivisect workspace to what the original binary loaded in the Vivisect workspace. And this [13:17.820 --> 13:25.640] was a decrypt in place binary. So that gives me an understanding of at least where I do need [13:25.640 --> 13:32.160] to focus, maybe not cutting out where I don't. So let's go searching for things. Reverse [13:32.760 --> 13:41.050] engineering, you load a thing up into Ghidra or Ida or Binja and you want to do things, how [13:41.050 --> 13:48.630] do you do those things? I want to go find hex UDS things, hex 29, hex 27 as immediate [13:48.630 --> 13:56.330] constants. Uh-oh. You see, we don't know anything about what we just dumped into the [13:56.330 --> 14:02.270] workspace. There's no code that's been identified. There are no strings that have been [14:02.270 --> 14:10.370] identified. You can manually go through the 30 meg of memory maps and manually look for [14:10.370 --> 14:20.230] strings, but, yeah, I don't have that kind of time. So here actually is a picture of [14:20.230 --> 14:26.190] Vivisect where I'm looking at a string that I want to find. But it's not identified as a [14:26.190 --> 14:35.070] string. In fact, nothing is except stuff that the loader told me was a string by going [14:35.070 --> 14:42.490] through the magic that we did before. So here's a different view of it. Maybe you prefer [14:42.490 --> 14:50.850] this view. And, of course, my laser pointer cannot show up on the screen. So on the [14:50.850 --> 14:55.250] right-hand side, if you can see this slide and you have great eyes, you can see near the [14:55.250 --> 15:00.650] bottom, there starts to be some ASCII string looking things. Okay. Really nifty. [15:00.650 --> 15:05.470] Including security array. I'm very interested in security array. Anything that starts in [15:05.470 --> 15:11.710] security is very interesting to me. But, unfortunately, Viv doesn't know that that's a [15:11.710 --> 15:18.210] string yet. When I first did this, I actually just saved the workspace out and the [15:18.210 --> 15:24.650] workspace is unencrypted. And I just ran strings literally on the workspace to get an [15:24.650 --> 15:30.510] idea of what was there. Because they were decrypted. They were de-obfuscated. And when I [15:30.510 --> 15:36.330] found an interesting thing, and there are millions of strings, when I found an [15:36.330 --> 15:42.430] interesting thing, I went back into Vivisect and found... Hi, there. I held down the [15:42.430 --> 16:43.290] wrong button. Are we back? No? Not yet? What? Probably see it just as easily, too. New [16:43.290 --> 17:01.460] pointer. I apologize. Not used to it yet. Okay. It does go fast, though. So once I found a [17:01.460 --> 17:04.860] string that was interesting, I'd go into the Vivisect CLI. There's a command line at the [17:04.860 --> 17:07.840] bottom and I just type search and I look for that string. It shows up and I can start [17:08.300 --> 17:17.040] turning it into strings by pressing S. All right. About a week ago, I wrote a Vivisection [17:17.040 --> 17:21.920] plug-in component that allows me just to do that and make that available to you guys as [17:21.920 --> 17:30.260] well. The Vivisection scan for strings. It brings up a starting memory map, an ending memory [17:30.260 --> 17:35.500] map and if that's not granular enough, a starting address and an ending address, do you [17:35.500 --> 17:39.500] want to apply the strings when you find them? Like shove them into the workspace? And I [17:39.500 --> 17:48.160] said no. Because it turns out strings captures a whole bunch of binary code that just kind [17:48.160 --> 17:53.360] of happens to be alphanumeric. So let's take a look at this. I'm looking for ASCII strings. I [17:53.360 --> 17:58.560] can look for you in a code and it allows you to set the minimum size. Defaults to five [17:58.560 --> 18:04.620] characters. That's generally good for me. So when we're done, if you can see this over [18:04.620 --> 18:12.520] here, because I told it not to apply the strings, it popped up a widget that just printed [18:12.520 --> 18:17.080] out. Here's the location. Here's the strings. I did not make it so that you could click on [18:17.080 --> 18:24.640] the location yet. That's coming next week. It's been very busy. So I get an idea and I'm [18:24.640 --> 18:28.800] able to scroll through and go, oh, this chunk of code is actually ‑‑ or this chunk of [18:28.800 --> 18:38.050] strings is really not strings. So I don't want all the strings. So I zero in on a memory [18:38.050 --> 18:43.350] address range where I'm confident that those strings are what I want and they are real [18:43.350 --> 18:56.290] strings. So now I have strings. Look, security access is right there. DPF, automotive stuff. If [18:56.290 --> 19:01.670] that makes you happy, that makes you happy. But I see some interesting things that aren't [19:01.670 --> 19:09.790] strings and I'm curious about them. We'll come back to it. X86, 32 or 64 bit, doesn't matter. [19:09.790 --> 19:17.190] It's a variable length instruction set and it's really important that we get it right so that [19:17.190 --> 19:24.750] when we call something code, it is code indeed. So if you get off by one byte, you can tank an [19:24.750 --> 19:34.770] entire understanding of a segment of code. So how do we accurately identify code? Well, it [19:34.770 --> 19:41.610] turns out there are 12 stack areas. All right. I gave you a hint. Actually, I'm taking [19:41.610 --> 19:54.350] hands. How do I identify known good code? Yell it out. Look, do you know how many things are [19:54.350 --> 20:07.550] good op codes that are not real? What's that? Ding, ding, ding. Give Travis a big hug. I'll [20:07.550 --> 20:16.670] get you later. Yes. So with the hint, he didn't need it. We have 12 stacks identified. One for [20:16.670 --> 20:23.690] each thread that's running in the system. Each one has its own stack. In the stack, what's [20:23.690 --> 20:30.710] stored there? Return pointers. And a whole bunch of other fucking pointers that are very [20:30.710 --> 20:39.530] annoying and they're all large, but it's a great teaching thing. How do I know if a pointer is a [20:39.530 --> 20:45.690] return? Well, first of all, we go to the pointer. And on the top, you see a 32 bit [20:45.690 --> 20:55.450] representation of stuff around the stack. At this address, 3F7FDD4, we see an address 5143A6. [20:55.450 --> 21:03.490] It's blue because that's a valid memory address. And we back up. Two bytes. And see if it [21:03.490 --> 21:07.730] disassembles. And now we're into what you're saying, but it's ‑‑ we're into what you were [21:07.730 --> 21:14.410] saying a second ago, but there's more to it. So sorry if I shut you down too quickly. So we back [21:14.410 --> 21:21.790] up two bytes. Why? Because the smallest call in x86 is two bytes long. The longest call is like [21:21.790 --> 21:28.310] eight bytes. So we back up two bytes. Does it disassemble into a call? Yes. Okay. Cool. Now [21:28.950 --> 21:37.490] wait. Does that call stop? Because everyone's got its own size. Does it stop at the pointer that [21:37.490 --> 21:42.950] we're trying to investigate? No. Shit. Go back. Okay. Three bytes. Four bytes. Five. Six [21:42.950 --> 21:49.670] bytes. Whatever. We get a call. The instruction itself ends at the boundary where the pointer [21:49.670 --> 21:54.550] is pointing. Because that's what the ‑‑ that's what a return call is, is to go to the next [21:54.550 --> 22:04.410] thing. And that is a pretty good likelihood that that's a return pointer. So I go to that [22:04.410 --> 22:12.350] location and I say C to make code. And sure enough we have a call to this address which is also [22:12.350 --> 22:21.170] valid. We have a jump after that. And then basically a drop down to a RET. So this is looking [22:21.170 --> 22:31.770] very much like very good code. But that is a fuck ton of work. And after letting my mentee stew on [22:31.770 --> 22:36.850] it for a day, I gave her a script and said, so here's a script that will kind of do that [22:36.850 --> 22:41.770] manually or automatically for you. And that script I was going to include in the deck here, I [22:41.770 --> 22:45.670] am not because I wrapped it into Vivisection. You'll get to see that when I cut the next [22:45.670 --> 22:57.210] release. But, whoops, here's the problem. When I did that, code started getting identified. [22:57.370 --> 23:06.920] Cross references, strings, like magic was happening and then boom, out of memory. I ended [23:06.920 --> 23:13.940] up analyzing this overnight on a machine where I gave myself an 80 gig swap space just to see [23:13.940 --> 23:24.400] if it would work. Binja did the same thing. Barfed. And most of my work ended up being on a [23:24.400 --> 23:29.460] 500 plus megabyte work space. Which I don't know about you or how big your work spaces are, [23:29.460 --> 23:35.620] but this takes ten minutes to load. And if I'm doing it over a network, because Vivisect has a [23:35.620 --> 23:42.020] Vivisect server and client mode, it takes 45 minutes to load. Okay. Need to work on this one. [23:42.020 --> 23:50.920] And we're in uncut territory here. That's fine, I guess. So, it also makes function recon [23:51.520 --> 23:55.960] completely useless. I don't know, I just released function recon in Vivisection so you might [23:55.960 --> 24:02.680] not have had a chance to use it. But function recon basically straps in a specialized partial [24:02.680 --> 24:11.460] emulator into a function. And it follows every code path exactly once. And then every call, it [24:11.460 --> 24:17.020] does that, too. And it keeps going and going and going until it runs out of code to hit. And [24:17.020 --> 24:21.800] it captures things like what strings did I run into? What function calls did I run to? What [24:21.800 --> 24:28.080] named functions did I call? Basically, reconnaissance on what a given subset of a program [24:28.080 --> 24:37.780] is going to do. Function recon is worthless on this binary. Or on this work space. Because I [24:37.780 --> 24:43.140] analyzed all of NTDLL and user 32 and kernel 32 and all the things that I really don't give a [24:43.140 --> 24:49.240] shit about. Because it turns out we know what that is. That's not obfuscated. That would be a [24:49.240 --> 24:59.070] really dirty trick. But I did check. This is legitimate NTDLL and whatnot. So, sometime by next [24:59.070 --> 25:06.030] week I could probably get done with function recon. But I think I'd like to make a change and [25:06.030 --> 25:14.510] not do that. Oh, and I broke the Viv server. Didn't not just work, it literally broke it where [25:15.910 --> 25:23.090] it would not try to send the data over the network. So, I ended up fixing that. That was months [25:23.090 --> 25:35.620] ago. So, I did wrap this into Vivisection because the goal of Vivisection is to kill all TDM and [25:35.620 --> 25:41.600] brain drain. We need your brains to be able to chew on the more important things, not just do [25:41.600 --> 25:49.680] these tedious horrible things. And we focus on starting maps and ending maps for stack. So, we [25:49.680 --> 25:55.840] look for the pointers here. And we can restrict where the pointers can point to that we care [25:55.840 --> 26:05.140] about. So, that we can skip NTDLL. We can skip kernel 32 DLL. Then I have a pull request in for [26:05.140 --> 26:14.200] the proper to allow us to turn on what's called one library. So, that when you analyze a [26:14.200 --> 26:21.060] function and it hits outside that library, it jumps or it calls into NTDLL, for example, it just [26:21.060 --> 26:30.140] stops. Because the NTDLL is going to have a name and that's kind of how we do reversing, right? [26:30.140 --> 26:37.600] So, next, once we found strings, we have this one problem. There's no cross references. So, the [26:37.600 --> 26:41.500] idea is we go to a string and then we have code that references the string and so the code is [26:41.500 --> 26:46.960] instantly interesting to me. Nothing is referring to these strings. We just told them that it was [26:46.960 --> 26:57.040] strings. Okay. Now what? Well, while doing this particular tool, I noticed that around these [26:57.040 --> 27:02.560] groupings of strings that were very interesting to me were a whole bunch of non-strings and it [27:02.560 --> 27:08.460] didn't look like code either. I started seeing a pattern in them. It was number, number, [27:08.460 --> 27:17.420] number, zero. Number, number, number, zero. Number, number, number, zero. And in this area of [27:17.420 --> 27:32.220] code, the address space is held within 24 bits. It is look at the address is 0075D289. In fact, [27:32.220 --> 27:40.880] the target binary starts at 400,000 hex and ends at like 80A000. That was really interesting to [27:40.880 --> 27:51.420] me. And as you can see, these little byte groupings are valid pointers into this target binary [27:51.420 --> 27:57.760] address space. So, I started going through and just hitting, going to one and hitting P. Make it [27:57.860 --> 28:01.880] a pointer. And suddenly, that P goes through the analysis process for the pointer and it [28:01.880 --> 28:07.680] says, oh, suddenly I'm pointing at a function. Because it starts with a name sub. Oh, that's [28:07.680 --> 28:15.460] curious. So, I did that with a whole bunch of them and they were all functions. So much so that I [28:15.460 --> 28:20.360] ended up writing a Vivisection, actually, no, I think I wrapped this into Vivisect itself. [28:21.880 --> 28:25.360] Writing code that just allows you to make pointers and you just say, here's the start, here's the [28:25.360 --> 28:36.120] stop, just make them all pointers. Turns out there are vast arrays of pointers to a whole ton of [28:36.120 --> 28:45.260] code. They end up being virtual function tables for C++. Is that familiar with anyone? C++ [28:45.260 --> 28:53.940] reversing. Kudos to you. It's not fun. So, we end up with these tables of functions that are used [28:53.940 --> 29:02.120] so when you have the class object ball and you want to call the function bounce or get color or [29:02.120 --> 29:09.540] change color, they're just held in this table here and the code knows that bounce is offset 4 into [29:09.540 --> 29:17.820] this table. Make sense? And all of a sudden, while I'm digging through here, making all sorts of [29:18.580 --> 29:29.460] pointers, I started seeing cross references to my strings. So, now I know some code is referring to [29:29.460 --> 29:37.380] security array. I'm getting better. Getting happier. And then I made this, so here's the Vivisect [29:37.380 --> 29:44.200] code or functionality to create a pointer array. Actually, if you just hold down shift and hit P, [29:44.200 --> 29:49.140] capital P is pointer array, pops up a dialogue, here's a starting address, here's a stop address, [29:49.140 --> 29:55.700] or if you'd rather, this many. So, you can put in a number into the count and it will ignore the [29:55.700 --> 30:00.660] stop address. It will just do that many. And then if we run into a null pointer, do we care and do [30:00.660 --> 30:06.820] we keep going or do we call that the end? So, these are all useful to me and so I put them in [30:06.820 --> 30:15.870] here. So, the V table entries, I don't know if you can see that, but the first entry in most [30:15.950 --> 30:26.050] virtual tables is called a destructor. C++ programmers? Again? Okay, a number of you. When you [30:26.050 --> 30:31.990] create a class, you write code for a constructor and a destructor. When you instantiate the [30:31.990 --> 30:40.310] object, it runs the constructor which basically tells, it sets up the different data structures in [30:40.310 --> 30:45.750] place, create strings, create sub objects, other things like that. A destructor is tasked with [30:45.750 --> 30:52.410] making sure that everything gets cleaned up appropriately. So, if you have a C++ class that has [30:52.530 --> 30:58.250] a bunch of strings in it, your destructor will roll through and say, all right, do this [30:58.250 --> 31:05.330] destructor thing for the strings here, strings there, strings here and otherwise or other C++ [31:05.330 --> 31:16.930] objects that are wrapped in. So, the destructor is a highly valuable resource. How much time do [31:16.930 --> 31:25.950] I have? I'm going faster than I thought. No blood dripping out of your ears? I'm doing good. [31:25.950 --> 31:34.190] All right. So, when we get involved in C++ analysis, no, don't worry, I'm not going to take you [31:34.190 --> 31:41.330] too far down that road. I didn't warn you enough. And it is, actually, I did, it's another talk. I [31:41.330 --> 31:48.330] need to give another talk on this particular thing. C++ objects, what creates them? It's really [31:48.330 --> 31:53.670] important because once you create a C++ object, you just kind of tote it around. You know, I make [31:53.670 --> 32:00.670] the function call over here, here's the C++ object and the C++ objects, the goal for object [32:00.670 --> 32:06.470] oriented programming is that that virtual function table maintains all the logic about how to do [32:06.470 --> 32:12.130] everything necessary with that object. So, it's kind of self contained. And then I just say, call [32:12.130 --> 32:18.010] this function on here's my ball, you know, or whatever the object is. And then that function says, [32:18.010 --> 32:24.410] I know that a ball can do a bounce. I don't have to know what that thing is, you know, what goes [32:24.410 --> 32:30.570] into bouncing that ball. I know that I can call bounce on that ball and it will be good. So, [32:31.990 --> 32:39.050] tracing V tables, very, very interesting. So, about a dozen years ago I spent some time writing [32:39.050 --> 32:46.850] symbolic analysis for structures and building in structure reconstitution, basically. Using a set [32:46.850 --> 32:54.790] of code and symbolic data about what the code is doing. And it's a simple or difficult way of [32:54.790 --> 33:04.350] saying the symbolic data is this is this pointer plus 12. Instead of this pointer and then a bunch [33:04.350 --> 33:10.610] of instructions that modify what that register is doing. The symbolic analysis just traces that [33:10.610 --> 33:23.100] and makes it easy for me to say, oh, this is offset 12 into that structure, all golden. So, the [33:23.100 --> 33:29.080] first thing that I'm wanting to do when I do C++ structure analysis is to go through all the [33:29.080 --> 33:34.640] functions in the V table because the V table lists all functions that know how to do that thing [33:34.640 --> 33:45.420] with that ball. Or that diagnostics utility. I then analyze how this pointer is being accessed [33:46.220 --> 33:52.740] and I can identify what things go where in memory. That becomes really important when I'm [33:52.740 --> 34:05.870] looking at other code so that I know what offset 12 really means. Other things, as I said, the [34:05.870 --> 34:11.010] destructor is very, very powerful. And I don't know if you can see that. This is the first [34:11.890 --> 34:18.910] function in one of the V tables we looked at earlier. And it looks really simple. It calls free [34:18.910 --> 34:25.530] object, that's what I call it. This is sub underscore some number. So, free object, this object [34:25.530 --> 34:33.070] and I like to name things by the address of the virtual function table. And then down here based [34:33.070 --> 34:39.130] on what's returned or actually based on the arg one, we decide whether or not to decrement and [34:39.130 --> 34:46.130] free that object. Okay. Seems simple enough. But free object is where all the really fun magic [34:46.130 --> 34:54.830] happens. So, free object goes through whatever this class or this object is and does the real [34:54.830 --> 35:02.210] dirty magic. You'll notice here something that is often found in dealing with C++ code. At the [35:02.210 --> 35:09.830] top of this little box, there's a move D word. So, ESI is being used as this pointer right now. [35:10.010 --> 35:19.470] And the very first pointer in that object is a pointer to a virtual function table. And one [35:19.470 --> 35:30.530] thing that you'll notice is that as C++ code is used in different ways. So, for example, we [35:30.530 --> 35:35.610] have a subclass that inherits from it called basketball. And then another one that is very [35:35.610 --> 35:39.810] different that inherits from ball called football. If you don't know what the difference is [35:39.810 --> 35:45.690] there, try to bounce them both. So, they both have their own bounce functions, but they're both a [35:45.690 --> 35:52.790] ball. So, sometimes what we'll do is we'll say, yeah, no, I really want the ball version of [35:52.790 --> 35:59.730] bounce. I don't actually want the basketball to have this control. So, oftentimes code will [36:00.790 --> 36:07.710] jam in the appropriate virtual function table for that when your code asks for that. So, as [36:07.710 --> 36:13.810] we're going through, this free object, ignore the top. It's basically just memory protection [36:13.810 --> 36:20.290] things. So, we jam in this virtual table. I just called it 6 because I didn't know what to do [36:20.290 --> 36:28.630] with it at that point. We then move 16 into EDI. That will be important very shortly. We [36:28.630 --> 36:39.530] compare EDI to this, this plus 608, this 608 offset. I'll tell you in advance, spoilers, it's a [36:39.530 --> 36:49.820] string. It's a C++ string, which is not like your ASCII string. We then move 7 into a local [36:49.820 --> 36:59.840] variable and move on. And based on whether or not this offset into that, this string basically, the [36:59.840 --> 37:08.360] size of the string is being compared against 16. If the string is 16 bytes or more, C++ will [37:08.360 --> 37:16.320] create a new object of the right size or something that's flexible and move the string into that. If [37:16.320 --> 37:22.360] the string is less than 16 bytes, then there's enough room in the C++ structure for a string to [37:22.360 --> 37:28.280] just house the string. So, this is why I know this is a string and that's a very common element [37:28.280 --> 37:37.300] for C++ reversing. So, we come down here. If it's greater, if it's 16 bytes or greater, then I know [37:37.300 --> 37:43.940] that I have a pointer to the thing that actually holds the ASCII string. So, I free that. And then [37:43.940 --> 37:52.420] I move down to the next thing and I compare 15 or I move 15 into EBP and I shove that all over to [37:52.420 --> 37:58.580] make sure that this string that we are in the process of deconstructing doesn't ever do that [37:58.580 --> 38:05.420] again. So, as soon as we free that memory, we say, okay, it's 15. That's not true. Actually, it's [38:05.420 --> 38:11.480] going to be zero. We're going to delete it. But, so, it moves down, compare EDI against another [38:11.480 --> 38:19.000] offset. So, some other string. We go down because EDI is still 16. So, we actually see in this [38:19.000 --> 38:31.960] particular object multiple points of clearly this is a string. In fact, we then run into another [38:31.960 --> 38:37.300] embedded object that's special and we call some code to clean it up. I won't go into it right [38:37.300 --> 38:45.260] now. But then we do another thing. Compare this thing against EDI. EDI is still 16. And if it's [38:45.260 --> 38:59.350] greater than 16, call decrement and free. And so on and so forth. So, just to recap C++ [39:01.390 --> 39:11.370] reconstitution approach, look for a whole bunch of function pointers. All grouped together. Look [39:11.370 --> 39:20.570] for cross references to the start of the branch. Is that the beginning of a bunch of pointers? Is [39:22.610 --> 39:30.430] that the end of a bunch of pointers? Analyze those functions for how they deal with the this [39:30.430 --> 39:36.670] pointer that's handed in. And every call and convention on every architecture has its own this [39:36.670 --> 39:42.630] pointer argument. Typically it's the first one. On 32 bit, you see a lot of this call that's ECX. [39:42.630 --> 39:47.850] That's why you're seeing that here. So, analyze the first function in the virtual function table. [39:51.220 --> 39:56.660] If it's a destructor, it will tell you almost everything else that you really need to know. And [39:56.660 --> 40:06.740] then make good use of it. I hope you didn't come here expecting Oday. That wasn't my bent. I'm a [40:06.740 --> 40:14.420] mentor. Hopefully I've taught you how to fish. Go fish for yourself. So, the real conclusion. Think [40:14.420 --> 40:21.160] outside the box. Make your tools do new things. If your tools are incapable of doing new things, [40:21.160 --> 40:29.400] make your own tools. Understand why things work the way they do. Because that leads you to be [40:29.400 --> 40:36.140] more powerful. Then go hunting. Figure out what you think the code or the behavior should look [40:36.140 --> 40:44.400] like. And use your brain to create new tooling. Use the existing tooling in creative new ways. [40:45.420 --> 40:49.560] Because you're the real power. Go forth and do great things.