Good afternoon DEF CON. Today we have a talk on automobiles, alcohol, blood, sweat and creative reversing of obfuscated car moding tool. And with our speaker Atlas. Thank you sir. Thank you Pauls. I'm sorry, True. Hi everyone, welcome. How many of you came because it had the word automotive in it? All right. Good. Obfuscation. Who came because it said obfuscation? Excellent. Well, thank you all. I don't care why you came. You're here. I'm very grateful for you. I hope that we have a lot of fun today. 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. 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. So who is this Atlas dork? Hi. This is my home. I was created here at DEF CON in 2013. And this is my family. I'm a hacker of things. I ride a motorcycle. I've got three amazing daughters who have nothing to do with technology. If you dare, there is a QR code that has all sorts of ways to get a hold of me. I'll leave that up for about ten more seconds. So I work in R&D and cyber physical systems for a company called Grim. And Grim is represented. Stand up. My dear friends down here wore huge Grim T-shirts. Ask how many of them work for Grim. A lot of love here. All right. So my job is to break into things. And I set many goals. Some of them attainable in the near term. Many of them long-term goals. Some of them, like, if I do these three projects that are year long, then I'll achieve this thing. So I think in these many different ways. But my ultimate goal typically is remote code execution on a device thing in a running program. 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. 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. Now, for that, for the really cool stuff that we really like to do, there's an authentication mechanism. So the tool says, no, I'm not just some Joe Schmo, I'm supposed to be doing this. Give me all the goods, let me do all the cool things. Now, there are a couple different ways that this is done. Well, a number of different ways. But two primary standards of the way that this is done. 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. 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. Set diagnostics level is the way that you tell an ECU to go into a certain mode. Sometimes you can change parameters, sometimes you can update firmware. But then there's a security access hex 27 or an authentication hex 29. These are the service numbers that are used in the messages. And that sets up the back and forth authentication process. So it could be simple key exchange and we just walked through that basically. Moving right along. All right. 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. There are many different things that can go into that but I don't care. I want them all. 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. And so I made the decision to use this as a mentoring tool. 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. But it did slow things down quite a bit. So I don't know if you can see this. If you can't, I apologize. Basically this is showing an obfuscated program. 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. It pushes a value onto the stack, it makes a call and then it has a return. Well, that call is literally a return. It's a no op call. So what happens when you push something on the stack and then you return? Well, that's a jump. It's very common to, it's a common thing to use to obfuscate what you're trying to do. Malware does this, software does this. And it's the makings of a very long day. Now there are a number of ways to deal with obfuscated programs. More than I listed here, but these are the two that hit my brain. They tickle my fancy. I like to emulate through things and do analysis through emulation. You probably heard a talk or two from me on that. And what I'm calling snatch and grab and that's actually what we're going to talk about here today. From an emulator perspective, you might spin up QMU. 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. 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. Vivisection has a lot of cool stuff, a tool that I released and actually spoke about last week at Black Hat. But not this time. No emulation in the initial things anyway. Emulation plays into pretty much everything that I do, but not for this. 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. You attach with a debugger and there are issues that go around this, but anti-debugging, who's heard of it? Anybody tried to get around anti-debugging techniques? Well, we're not even going to worry about it. We attach, it is very difficult to make it so you can't attach. If you can't, if you actually can't attach, then kernel debugging becomes a thing. Then we grab all the data and we store it and analyze it. I mean, it sounds simple, right? So for this I used a tool called Vivisect, my favorite reversing tool. 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. V plays into everything. It's kind of like V for Vendetta but Visigoth. And then magic happens. So here's a picture. If that's too much in my chart, here's a closer picture. 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. And here's the actual, I had to zoom in yet again. So snapshot target.vsnap. The next step was to import the data from the snapshot into a Vivisect work space. Vivisect work space is basically holds all the things that you're analyzing when working with Vivisect doing reverse engineering. And you can use this for many, many things including symbolic analysis and emulation but we won't get there right now. It's basically, it was a manual process when I started this. It's continued to take shape. This will be wrapped into Vivisection shortly. Like within a week or two. I will be releasing next week. So basically we copy all of the memory maps. Show of hands, who has done debugging before and understands what a memory map is? 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. Here are the different memory maps that exist. These four or five are from this program or this library and whatnot and so forth. That's what I'm talking about. So we just take, that's all been snapshotted away. We grab the snapshot memory maps. 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. And we tell the work space to just steal that data. I'm giving you eye charts. We're skipping over quite a few slides here. Because at the end of the day, this was stolen from the built in Vivisect parser. We just removed a few things that loaded the memory maps back in again. And that's the thing that I'm going to be wrapping into Vivisection. Reach out to me if you want it. You've got contact information for me on the slides. The slides should be available online. Okay. So there are a number of different ways that obfuscation is generally done. Any malware reverse engineers here? How about malware forward engineers? So there are three primary ones that I run into when I've done malware reversing or obfuscation. The first one is unpack or decrypt in place. 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. But then it gets to a point where it decrypts itself into the existing memory maps. So that's one thing to be looking for. Because we're always looking for what's important. What are we interested in? The next one is to unpack or decrypt into new memory maps. 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. So anonymous memory maps may be very interesting as well. You have named memory maps, you have anonymous memory maps. And then process hollowing or some variation thereof. 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. So these are all interesting things we're looking for. There are many variations on it. And determine very quickly by comparing what I had dumped into the Vivisect workspace to what the original binary loaded in the Vivisect workspace. And this was a decrypt in place binary. So that gives me an understanding of at least where I do need to focus, maybe not cutting out where I don't. So let's go searching for things. 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? I want to go find hex UDS things, hex 29, hex 27 as immediate constants. Uh-oh. You see, we don't know anything about what we just dumped into the workspace. There's no code that's been identified. There are no strings that have been identified. 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. So here actually is a picture of Vivisect where I'm looking at a string that I want to find. But it's not identified as a string. In fact, nothing is except stuff that the loader told me was a string by going through the magic that we did before. So here's a different view of it. Maybe you prefer this view. And, of course, my laser pointer cannot show up on the screen. 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. Okay. Really nifty. Including security array. I'm very interested in security array. Anything that starts in security is very interesting to me. But, unfortunately, Viv doesn't know that that's a string yet. When I first did this, I actually just saved the workspace out and the workspace is unencrypted. And I just ran strings literally on the workspace to get an idea of what was there. Because they were decrypted. They were de-obfuscated. 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. I held down the wrong button. Are we back? No? Not yet? What? Probably see it just as easily, too. New pointer. I apologize. Not used to it yet. Okay. It does go fast, though. So once I found a string that was interesting, I'd go into the Vivisect CLI. There's a command line at the bottom and I just type search and I look for that string. It shows up and I can start turning it into strings by pressing S. All right. 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. The Vivisection scan for strings. 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? Like shove them into the workspace? And I said no. Because it turns out strings captures a whole bunch of binary code that just kind of happens to be alphanumeric. So let's take a look at this. I'm looking for ASCII strings. I can look for you in a code and it allows you to set the minimum size. Defaults to five characters. That's generally good for me. 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. Here's the location. Here's the strings. I did not make it so that you could click on the location yet. That's coming next week. It's been very busy. 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. So I don't want all the strings. 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. So now I have strings. Look, security access is right there. DPF, automotive stuff. If that makes you happy, that makes you happy. But I see some interesting things that aren't strings and I'm curious about them. We'll come back to it. X86, 32 or 64 bit, doesn't matter. 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. So if you get off by one byte, you can tank an entire understanding of a segment of code. So how do we accurately identify code? Well, it turns out there are 12 stack areas. All right. I gave you a hint. Actually, I'm taking hands. How do I identify known good code? Yell it out. Look, do you know how many things are good op codes that are not real? What's that? Ding, ding, ding. Give Travis a big hug. I'll get you later. Yes. So with the hint, he didn't need it. We have 12 stacks identified. One for each thread that's running in the system. Each one has its own stack. In the stack, what's stored there? Return pointers. And a whole bunch of other fucking pointers that are very annoying and they're all large, but it's a great teaching thing. How do I know if a pointer is a return? Well, first of all, we go to the pointer. And on the top, you see a 32 bit representation of stuff around the stack. At this address, 3F7FDD4, we see an address 5143A6. It's blue because that's a valid memory address. And we back up. Two bytes. And see if it disassembles. 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. So sorry if I shut you down too quickly. So we back up two bytes. Why? Because the smallest call in x86 is two bytes long. The longest call is like eight bytes. So we back up two bytes. Does it disassemble into a call? Yes. Okay. Cool. Now wait. Does that call stop? Because everyone's got its own size. Does it stop at the pointer that we're trying to investigate? No. Shit. Go back. Okay. Three bytes. Four bytes. Five. Six bytes. Whatever. We get a call. The instruction itself ends at the boundary where the pointer is pointing. Because that's what the ‑‑ that's what a return call is, is to go to the next thing. And that is a pretty good likelihood that that's a return pointer. So I go to that location and I say C to make code. And sure enough we have a call to this address which is also valid. We have a jump after that. And then basically a drop down to a RET. So this is looking very much like very good code. But that is a fuck ton of work. 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. And that script I was going to include in the deck here, I am not because I wrapped it into Vivisection. You'll get to see that when I cut the next release. But, whoops, here's the problem. When I did that, code started getting identified. Cross references, strings, like magic was happening and then boom, out of memory. 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. Binja did the same thing. Barfed. And most of my work ended up being on a 500 plus megabyte work space. Which I don't know about you or how big your work spaces are, but this takes ten minutes to load. And if I'm doing it over a network, because Vivisect has a Vivisect server and client mode, it takes 45 minutes to load. Okay. Need to work on this one. And we're in uncut territory here. That's fine, I guess. So, it also makes function recon completely useless. I don't know, I just released function recon in Vivisection so you might not have had a chance to use it. But function recon basically straps in a specialized partial emulator into a function. And it follows every code path exactly once. And then every call, it does that, too. And it keeps going and going and going until it runs out of code to hit. And it captures things like what strings did I run into? What function calls did I run to? What named functions did I call? Basically, reconnaissance on what a given subset of a program is going to do. Function recon is worthless on this binary. Or on this work space. 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. Because it turns out we know what that is. That's not obfuscated. That would be a really dirty trick. But I did check. This is legitimate NTDLL and whatnot. So, sometime by next week I could probably get done with function recon. But I think I'd like to make a change and not do that. Oh, and I broke the Viv server. Didn't not just work, it literally broke it where it would not try to send the data over the network. So, I ended up fixing that. That was months ago. So, I did wrap this into Vivisection because the goal of Vivisection is to kill all TDM and brain drain. We need your brains to be able to chew on the more important things, not just do these tedious horrible things. And we focus on starting maps and ending maps for stack. So, we look for the pointers here. And we can restrict where the pointers can point to that we care about. So, that we can skip NTDLL. We can skip kernel 32 DLL. Then I have a pull request in for the proper to allow us to turn on what's called one library. 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. Because the NTDLL is going to have a name and that's kind of how we do reversing, right? So, next, once we found strings, we have this one problem. There's no cross references. 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. Nothing is referring to these strings. We just told them that it was strings. Okay. Now what? 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. I started seeing a pattern in them. It was number, number, number, zero. Number, number, number, zero. Number, number, number, zero. And in this area of code, the address space is held within 24 bits. It is look at the address is 0075D289. In fact, the target binary starts at 400,000 hex and ends at like 80A000. That was really interesting to me. And as you can see, these little byte groupings are valid pointers into this target binary address space. So, I started going through and just hitting, going to one and hitting P. Make it a pointer. And suddenly, that P goes through the analysis process for the pointer and it says, oh, suddenly I'm pointing at a function. Because it starts with a name sub. Oh, that's curious. So, I did that with a whole bunch of them and they were all functions. So much so that I ended up writing a Vivisection, actually, no, I think I wrapped this into Vivisect itself. 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. Turns out there are vast arrays of pointers to a whole ton of code. They end up being virtual function tables for C++. Is that familiar with anyone? C++ reversing. Kudos to you. It's not fun. 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. Make sense? And all of a sudden, while I'm digging through here, making all sorts of pointers, I started seeing cross references to my strings. So, now I know some code is referring to security array. I'm getting better. Getting happier. And then I made this, so here's the Vivisect code or functionality to create a pointer array. 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. So, you can put in a number into the count and it will ignore the stop address. It will just do that many. And then if we run into a null pointer, do we care and do we keep going or do we call that the end? So, these are all useful to me and so I put them in here. 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. C++ programmers? Again? Okay, a number of you. When you create a class, you write code for a constructor and a destructor. 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. A destructor is tasked with making sure that everything gets cleaned up appropriately. 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. So, the destructor is a highly valuable resource. How much time do I have? I'm going faster than I thought. No blood dripping out of your ears? I'm doing good. All right. So, when we get involved in C++ analysis, no, don't worry, I'm not going to take you too far down that road. I didn't warn you enough. And it is, actually, I did, it's another talk. I need to give another talk on this particular thing. C++ objects, what creates them? It's really important because once you create a C++ object, you just kind of tote it around. 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. So, it's kind of self contained. And then I just say, call this function on here's my ball, you know, or whatever the object is. And then that function says, I know that a ball can do a bounce. I don't have to know what that thing is, you know, what goes into bouncing that ball. I know that I can call bounce on that ball and it will be good. So, tracing V tables, very, very interesting. So, about a dozen years ago I spent some time writing symbolic analysis for structures and building in structure reconstitution, basically. Using a set of code and symbolic data about what the code is doing. And it's a simple or difficult way of saying the symbolic data is this is this pointer plus 12. Instead of this pointer and then a bunch of instructions that modify what that register is doing. The symbolic analysis just traces that and makes it easy for me to say, oh, this is offset 12 into that structure, all golden. 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. Or that diagnostics utility. I then analyze how this pointer is being accessed and I can identify what things go where in memory. That becomes really important when I'm looking at other code so that I know what offset 12 really means. Other things, as I said, the destructor is very, very powerful. And I don't know if you can see that. This is the first function in one of the V tables we looked at earlier. And it looks really simple. It calls free object, that's what I call it. This is sub underscore some number. So, free object, this object and I like to name things by the address of the virtual function table. 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. Okay. Seems simple enough. But free object is where all the really fun magic happens. So, free object goes through whatever this class or this object is and does the real dirty magic. You'll notice here something that is often found in dealing with C++ code. At the top of this little box, there's a move D word. So, ESI is being used as this pointer right now. And the very first pointer in that object is a pointer to a virtual function table. And one thing that you'll notice is that as C++ code is used in different ways. So, for example, we have a subclass that inherits from it called basketball. And then another one that is very different that inherits from ball called football. If you don't know what the difference is there, try to bounce them both. So, they both have their own bounce functions, but they're both a ball. So, sometimes what we'll do is we'll say, yeah, no, I really want the ball version of bounce. I don't actually want the basketball to have this control. So, oftentimes code will jam in the appropriate virtual function table for that when your code asks for that. So, as we're going through, this free object, ignore the top. It's basically just memory protection things. So, we jam in this virtual table. I just called it 6 because I didn't know what to do with it at that point. We then move 16 into EDI. That will be important very shortly. We compare EDI to this, this plus 608, this 608 offset. I'll tell you in advance, spoilers, it's a string. It's a C++ string, which is not like your ASCII string. We then move 7 into a local variable and move on. And based on whether or not this offset into that, this string basically, the size of the string is being compared against 16. 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. 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. So, this is why I know this is a string and that's a very common element for C++ reversing. So, we come down here. 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. So, I free that. 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. So, as soon as we free that memory, we say, okay, it's 15. That's not true. Actually, it's going to be zero. We're going to delete it. But, so, it moves down, compare EDI against another offset. So, some other string. We go down because EDI is still 16. So, we actually see in this particular object multiple points of clearly this is a string. In fact, we then run into another embedded object that's special and we call some code to clean it up. I won't go into it right now. But then we do another thing. Compare this thing against EDI. EDI is still 16. And if it's greater than 16, call decrement and free. And so on and so forth. So, just to recap C++ reconstitution approach, look for a whole bunch of function pointers. All grouped together. Look for cross references to the start of the branch. Is that the beginning of a bunch of pointers? Is that the end of a bunch of pointers? Analyze those functions for how they deal with the this pointer that's handed in. And every call and convention on every architecture has its own this pointer argument. Typically it's the first one. On 32 bit, you see a lot of this call that's ECX. That's why you're seeing that here. So, analyze the first function in the virtual function table. If it's a destructor, it will tell you almost everything else that you really need to know. And then make good use of it. I hope you didn't come here expecting Oday. That wasn't my bent. I'm a mentor. Hopefully I've taught you how to fish. Go fish for yourself. So, the real conclusion. Think outside the box. Make your tools do new things. If your tools are incapable of doing new things, make your own tools. Understand why things work the way they do. Because that leads you to be more powerful. Then go hunting. Figure out what you think the code or the behavior should look like. And use your brain to create new tooling. Use the existing tooling in creative new ways. Because you're the real power. Go forth and do great things.