All right. Hello. My name is Andrew King. I'm here to present reflective injection detection. Put together some Python C types in about a week that fairly accurately detects reflective injection the vast majority of the time. It does have a couple of issues with false positives. But something to note about the title, I said defensive vendors in general. It's not exactly the case. Defensive vendors do have products that catch reflective injection. It's just not in your main line home user applications. So a couple of things I thought I should know before I get started on the talk. One is I don't own any of the artwork. It's all Google images. There weren't any notices about, hey, you're going to get in trouble for using this artwork. So that's why I used it. As far as attributing blame to offense, defense, defense, defense, defense, defense, or whatever, I think both sides are equally at fault. But that's just how it goes. My statements here don't reflect past, present, or future employers, obviously. I left the who am I page blank. But if you were really interested, you could jump to LinkedIn. But that's on the DVD. So why would I do a talk that's going to make people angry? I did a talk previously that made some folks a little angry. I'm just going to talk about what I do that's going upset and complaining about a problem often gets people up in arms. But I think it's something that should change and if everybody just goes, well, it's old, okay, then why does it still work in most home user cases? So I think everybody knows what reflective injection is. You're just doing a huge allocation into memory, you're copying a DLL there and then through whatever method you want you're steering execution. Now, on the defensive side, programmers are lazy, yes, but it's not just defensive programmers, it's offensive programmers, too. Perfect example is I threw this code together in about a week and it's not ‑‑ it's very procedural and not awesome as a stellar code example. So there's very little request specific and check going on with ‑‑ I don't know if you can see it. I don't know if you can see it. I don't know if you can see it. Most of your ‑‑ most of your exploitation frameworks, most of them are just going, hey, when I want to inject, I'm just going to request that you allocate, you know, a certain size and you get contiguous pages in memory and the base of those pages tends to be pretty sequential. So if you wanted to optimize your scanning for where reflect injected DLLs might be, one way to do it would be to use from a defensive perspective, intentionally insert yourself into another process and see where the allocation happens and then work backwards from there so that you're not trying to scan all of memory all the time. Just a possible optimization. Let's see. So on the offensive side, what if they ‑‑ what if they start trying to look for PE mapping code? The perfect example for that was I used it in a code obfuscation tutorial, and one of the vendors wound up flagging it as malicious software when it printed hello, you know, if you actually ran it or dropped it in a disassembler or looked at the source. So there's a lot of just ‑‑ if you customize your PE mapping code or you expanded it on disk and then moved it into memory, it would be a lot more effective and that, I believe, is what the interpreter does. But ‑‑ Another component to that is that it changes ‑‑ the way that you map it changes where you have to look for PE headers in memory in order to actually find the reflect injected DLLs, like in the case of ‑‑ I couldn't figure out what I was doing wrong there for a second, because when the interpreter does the interpreter stager, it loads the entire DLL at once and the whole thing is read, write, execute. So you're looking in the executable space for the PE header. But if you're looking with the Joaquin botch code for reflect ‑‑ reflect injected DLLs, you're looking in the reflect injected DLL, then you're going to be looking one page before executable space. So there is ‑‑ there is that. So you just have two pages to check instead of, you know, many. And, yes, they're probably just mostly at this point looking for stager code. They're probably not looking for the injected code. And I think that's kind of a fail. But why does it still work? We could detect it at run time as it happens. But we'd have to be monitoring all memory allocations. And if you started doing that and just going, you know, look for the next copy to that allocation, then, you know, you could ‑‑ you could simply trick your way past that. And so it's very hard to do once, right? So it's a continuous looping through. It's probably why it's not implemented. It's really hard on resources. You're going to have to do it once. It's probably going to be having a really difficult time with making it detect almost immediately after run and not having a significant performance impact. Yes, we can scan memory for it. So from the defensive side of things, to find reflect injected DLLs, we could look for executable space and then walk backwards for that and look for PE headers if they started finding ways around that. We could start looking for something that looks like a DLL. We could start looking for something that looks like cough signatures. Section tables are technically part of the PE header. Then you've got permissions. You're looking for anything executable that's not already inside of a DLL. You'd be ‑‑ which shouldn't happen, but some things use it. Java uses it. Flash uses it. But they're just bad ideas to begin with. Then we've got the predictable layout. And that predictable layout is speed. It's a little bit more complex. It's a little bit more complex than speaking of where allocations happen in memory. So first you could build a white list. You could get a list of all processes, all modules in all processes. And that's essentially your exclusion list. And if you ‑‑ and then you've got the rest of memory to check out and see if it's in use, see what permissions it has, see if it has simple headers or structures. Then you can ‑‑ you could automatically kill anything that you think is reflect injected. You could kill anything that you think is affected. But that's ‑‑ there's an issue with that. Or if you were to do it immediately. There's an issue with delay loaded DLLs where if, say, you turn on, like, Windows media player and you've got this running as hard as it can go, and it scans for loaded DLLs, finds them, and then Windows media player loads another DLL in that moment in between when you've scanned for new modules and to build the exclusion list. And when you start checking memory space, you can accidentally kill applications. So it has to ‑‑ it has to happen. It has to happen. It has to happen. It has to happen a couple times in a row for that to actually be viable. You've also got ‑‑ so the things that you need to do this are just virtual query that you can find out a bunch of data about just about any ‑‑ just about any memory area, just about any process. You can just save all that. You might need it later. Process of elimination, you're looking to find all your legitimate pages. You're looking to find all your legitimate pages. You're looking to find all your legitimate pages. And then eliminate everything that's non ‑‑ eliminate everything that's legitimate and then pick through from there. There are more criteria that you could use to eliminate other than just looking for executable sections in case somebody starts toggling permissions just when they ‑‑ just when they want to go there. So I tend to do things like leave out mapped memory sections where files are mapped into memory. But, again, it's just a matter of yes, you could execute from there. So now that you ‑‑ now that you can find out if it's executable or not, you want to read it and find out if you actually have a PE‑like structure, you can't validate the entire PE header fast enough to be viable. So you're really just looking for two letters at the very beginning. And you, in some cases, possibly have to build an exclusion there. So you're really just looking for two letters at the very beginning. And you, in some cases, possibly have to build an exclusion there. So you're really just looking for two letters at the very beginning. So we could ‑‑ if we wanted to validate the entire PE header and possibly additional structures within the DLL, then we could check the probable allocation space only. And go, hey, you know, if we request memory be allocated and it's probably going to be in this location, then we can only check a certain number of allocations previous to that. Or a certain number of pages previous to that. So now that we've found it, what do we do about it? Well, you could just flip access permissions so that threads that try to execute there get an access violation. And then you can just go to the location and die. Could be a very serious problem with false positives if you had a false positive in some system service, which would be very bad. You could also suspend threads found to be operating there. But if you wanted to do that, since threads don't have attribution, you'd have to do ‑‑ it's ‑‑ for just reflect injected DLLs, it's not horribly important to catch it immediately. If you diverted open socket or socket send, then you could find a way to get it back and find out what threads are actually touching the network. And that would be important to know. Because you could back up on the stack and see if that came from your injected DLL. In this case, a simple example is interpreter. But that's just one example. You could dump it to disk. You could try and reverse the relocations to get yourself the original usable DLL. You could just add another DLL to the project. You could try and reverse the relocations to get yourself the original DLL. You could try toary it into your par ROST processes list and then edit the table of DLLs manually to, in essence, attach it to the process. So then antivirus begins to see it again. That's much more viable than just killing ‑‑ just killing things at random because it doesn't require that you make a judgment call. You just attach ‑‑ attached it to the process and let other tools do what they already do. All right. So that was pretty easy and not altogether new. They've been knowing for quite some time that executable space ‑‑ or writable executable space is a really huge beacon, but that it has false positives in Flash and Java. Just that nobody said, hey, you know, look in the previous two pages for a PE header and nobody has fixed the problem yet. So anyway, home user products, they're not attacking it at all. From the offensive side of things, how could we beat it if they started detecting it? We could rewrite the PE header, it would be really simple. You could just zero out MZ. You could load a large DLL into memory and then instead of doing virtual aux and copying the DLL into the allocated space, you could load some random huge binary and carve it out and just be inside that. So that would be relatively difficult. Relatively difficult to catch with signatures. You could change permissions on the page with the header. You could set it to no access. As long as you don't have to touch it anymore, you're done. Some tools might change the permissions, check, you know, check, change it to readable and then change it back when they're done looking at it. But I would think at least one would fail in that way. So why don't more developers do this? Why don't more developers open source? The reason I bring this up is because I'm giving the really nascent not production grade code away, just here, use it, make it better general how I did the thing. And I think it's because some people, a lot of people build entire product lines on top of open source code and never donate back to the products. Great examples are open SSH, which I have a sneaking suspicion is the base for the SSH servers or clients for an incredible number of network appliances from all types of vendors. And then FFmpeg's wall of shame has been down for quite some time. But it's still there on internet time machine and it still lists a ton of products. All right. So the demo. Oh, come on. Well, if it would launch properly, everything would be dandy. I'm not a fan of Unity, by the way. All right. There we go. Start that one. And that one. And that one. And that one. And that one. Hopefully that's the only time that I upset the demo gods today. So we've got that. And all that's doing is sitting there listening. Oh, come on. Break across the barrier. There we go. And we've got the Windows box here. And you guys can't see the command prompt there at all. All right. There. It's just running. Like I said, I took out the auto kill because it was having some issues. And we've got a little test executable right there. And all we want to do is give it a run. And this would be a little test test. We're just going to have to do that and we're going to be just simulating whatever cause interpreter stages would go off. And it's finding it. And it could easily kill it. But you can see that the PIDs match. And that's all there is to that. I previously did really long demos where I should, you know, killing a process and really looking at things. And I got the impression that that was a bad thing to do. So there. No long, huge demo. Actually, the thing is, we don't need to up all the windows. All right. So if you stop worrying about reflect injecting things and started just because, say, they catch it and you can't find additional ways to fight it because they just get really, really good at catching it, then you might just want to think about run time obfuscation of compiled binaries and, yeah, they're going to see the code, but there's, even if you don't do obfuscation, just not doing anything hinky could, I mean, means you could get away with it for quite some time. Both sides of the fence. Can you it be halted or slowed down? Yeah, I suppose it could be slowed down quite easily. Could it be halted? Probably not. Because there are too many ways that you could, that you can just try and continually change tricking things. Why isn't it being done? It's kind of processor intensive to catch quickly. There is some code that I'm giving out that catches it pretty fast. It is just Python with C types. It's not very heavy. It's not very fast. You'll want to shim in, nice thing up the processor if you plan on leaving it running in the background because it doesn't have any processor control at all. It's not pretty, but it works. Only works, only, I only tested it on x86. There are issues that can happen with x64 because the way you query memory changes and some of the results you get can change. It doesn't have, doesn't have a lot of features. I did have auto kill and setting prejudice levels so that you could try and suspend threads and whatnot. But that, that became trouble some and I didn't want to have to field bugs on, well, suspend broke my system. I have a C port but it's much slower because I used lists instead of maps. The other code that I'm just giving away is an encoder thing that I put together a while ago in, for obfuscating C binaries. I had a bunch of people want a copy of it so I'm giving it away. I had to strip out some things. But I'm here. Fine. I couldn't really in good conscience put a fully weaponized thing out there so I did strip out some of the lists that it needs to operate so you just have to populate those lists with assembly structures you're choosing. What else am I working on? Some interesting things. There's some Python obfuscation stuff going on where you're taking PYCs and actually doing obfuscation on PYCs which is, which is fun. And not hard to do. So I'm going to show you something that's arguably unexpected since it's default installed in everything but Windows at this point. And there's some guys in the audience that deserve a thank you for turning me on to the obfuscating Python thing. And helping me out, trying to grill me on some of the stuff so I could be ready for questions. And questions, yes, I'll, yes, I'll be around. We're doing a lot of work. We've got the Q&A room and evidently I didn't ramble on quite as much as I thought I was going to. All right. So we can take questions here if anybody has questions or they want to complain about my approach or they want to ask when does it fail because I can think of random cases where it would fail. There's some things you can do with dot net that would cause it to have problems with loading DLLs. All right. No questions. I'm sorry? Oh, all right, yeah, I couldn't do that. Have I done any 64 mitigation? That's a good one. It does run on x64. There are a couple of specific cases. I'm not sure. I'll be here, though. But there are a couple of specific reasons that it can fail. And it has to do with when you load 32-bit DLLs into a 64-bit process, then some things can come off the rails. And where is my pointer? All right. I just had an epic fail, guys. Horribly sorry about that. Oh. Turn that off. I can step through the code, but that's horribly ‑‑ that's just not good. I'm not sure what to do at this point. When I was doing this in the ‑‑ at home, I must have rambled on quite a bit. And here I'm talking quite quickly. What do you want me to do? All right. Well, that's essentially all the demo I have with the code that I'm giving out here. I've ‑‑ the other code is on a desktop that does the auto kill and all that. All right. Not necessarily about the particular product, which is awesome. Thank you for talking about it. But we're kind of curious on one of the notes that you pointed out as far as the devs and the vendors being extremely lazy. I mean, just in general. I don't know if you are very lazy that you want to do as little as possible just to turn off the features at any time. Yes. But as a screen engineer, what would you say, not like a top three, if you will, or a couple options that you would say every dev team should do this early in the cycle, or whatever it might be, to not be as lazy as you are? I would ‑‑ Oh, yes. In order to get past the developers are lazy period component, what could we do early in the dev cycle to get past that? And constantly through the dev cycle to get past that. And that would be stuff like if you actually use a UML modeling tool to say these are the features we want and this is how we're going to break them up. And it leads to something. It leads to objective pretty code as opposed to procedural code. And instead of banging out a feature and then going back and making it object oriented and moving things around to fit what you want them to later, if you start with a top down problem solving approach, then you can break it up and just not implement chunks of ‑‑ or chunks or modules until you get around to needing that particular feature. But it's difficult because you expect ‑‑ you expect to have ‑‑ a feature for another feature. Or if you're planning ahead, you need to plan how you're going to access all your features all the time ahead of time. Which is difficult. Does that ‑‑ essentially what I was trying to get across was top down problem solving with modular solutions. And UML helps with that a lot. Do you have any ‑‑ thank you. The difference is kind of ‑‑ do you have any suggestions? Any suggestions around in a dev team, not to run out of pain for everybody, but in a dev team you have, you know, the given dev cycle and the QA cycle that are kind of meshed together. Is there anything that you could do on the QA side? Because a lot of times what we do now or end up doing is kind of basic schemes, kind of the normal stuff that we look at. A lot of times it's just tossed over the wall to the security side to say, hey, make sure this doesn't suck. Is there something that could be done even in the QA sector or the testing sector, outside of just, you know, pen testing or just maybe some high‑level things, like, yeah, these are really cool things to do at that time? Well, as far as ‑‑ the question was during ‑‑ that I responded to the question about what things you can do during the dev side of the cycle. But what things can you do on the QA side of the cycle to try and make sure something doesn't suck as things progress. And on the QA side, it's the test early and test often. And test in under the kind of ‑‑ under the kind of ‑‑ under the kind of stress that it's going to be ‑‑ that it's going to be operating under. Because if you've got some ‑‑ if you've got some network appliance that's running some software and it runs just fine on your test cycle because you only run ‑‑ you only run 5% of its maximum backplane, then when you, you know, when you max out the backplane, what happens? And that could really mess with features. The other part of it is the ‑‑ that if you can break your testing down into individual features, that's nice, but you can't always break down testing into individual features because a bug could depend on one feature being activated and another feature happening at the same time. So that's ‑‑ test‑driven design is good. But test‑driven design goes nowhere to catching that problem. That's a particular issue. Does that essentially address what you were asking on that one? Thank you. Thank you. Well, thanks for having me.