All right, what do we do, boys? Thanks, man. Hi, Defcon. Hi, Defcon. Thanks for sticking with us today. This Sunday is a bit more chill. Some of us are already flying home. Some of us are recovering from Engover. And the last of us are here. Thanks for sticking with us. There's also some zombies there. Okay, so today we are presenting the results of our research, which is, and we will present a new approach for stack spoofing targeting Windows on, like, specifically 64-bit architecture of Windows. Who we are? We are three friends. I'm Alessandro Magnosi. I'm a principal security consultant in BSI, where I do mostly red teaming, offensive tool development, and white box assessments. So source code review, fuzzing, and stuff like that. In my free time, I work for, I maintain a company. I'm also working in some tools, some offensive tools in project industry, which is an initiative by Marcello Salvari to support open source developers. And in the rest of my time, I'm a bug hunter for Sinegra team. Then there is my colleague. Hey, my name is Arash. I work at CyberArk. I have kind of a dual role there. I'm very fortunate. I get to do a lot of threat hunting, detection engineering, and I get to do red teaming when time allows for it. And our friend, Thanos, unfortunately couldn't be here today. He also contributed to this work. He's a red teamer at Netitude. He was also great, and he contributed a lot here. So we want to make sure he's mentioned. So here we are. So a bit of story about this. All of this started back in 2022 when Namazo, a security researcher and developer, released this. He released a POC on Twitter, like an image, where it was showing a technique which then we popularized and fixed also a bit. We were very interested in his research and his work. So we reached down to it. We started developing this in-house. Of course, we didn't know a lot about what was involved with the technique. So we started this research and development project. We developed our own version of the technique showed by Namazo. And after that, we continued researching on this technique. We will also present the detection part for this technique and other additional research avenues that we researched. So just to give you a bit of background, let's define what stack spoofing is. What we mean with stack spoofing. So of course you know that when you start a process on Windows, when you execute a binary, you create a process in Windows which is a container and then one or more threads are tasked with executing the real code of the program. Every thread maintains a call stack which is an area of memory organized as a stack that maintains the information about all active subroutines in a given time for a given thread. to substitute the frames with fake ones and this is usually never done for non-malicious activity. This is always done for malware to hide the execution flow or similar stuff. So you will see here four different frames and why I put this here is just to let you know what a call stack is and you will see that there are certain things that are like a call stack. For example, we define these first two threads for this talk we will just talk about these two frames as thread initialization frames which are usually at the bottom of the call stack like at the top actually of the call stack. Then you have internal frames of a specific program that we will refer to this during the talk as main modules frames and then you have the frames related to a call API an API call. So why this is how this is calculated by Windows so the stack is evaluated by Windows using an unwinding algorithm and this is this uses the information contained in the pdata section of a binary or related DLL and in this pdata section you usually have an exception directory we contain all the information about the function specifically like every entry, runtime function entry in this table contains information about the begin address of a function the end address and a pointer to a structure which is the unwinding infrastructure that contains information about the size of the prologue the count of codes and all the operations that were performed in a prologue of a specific function. Now, why this is important I mean this is used by by Windows to manage all the exceptions but what we are interested in is the usage that this is done to calculate the call stack at runtime and to calculate that what happens is that Windows evaluate the frame a call frame size dynamically using collecting the codes so the operation in the prologue that actually affected that impacted the frame size and the operation are usually push of course you are subbing 8 on the stack and if you allocate memory you are subbing from the from RSP so you allocate stake on the stack so these two main operations are what modify the frame size and then there is the push MAC frame which is really not interesting for our talk but yeah it's another operation that impact the frame size and this is important, this is needed in Windows because if you remember 32 bit architecture in the prologue you always have like a push EBP and then you save the base pointer so you have the base pointer pointed to the top of the to the top of the frame and then you have the ESP pointing to the bottom of the stack and this is useful like the contrary but yeah this is used for this is very useful to evaluate the call stack because you can just navigate back the chain of EBP that are pushed on the stack but in 64 bit this is not possible because RSP is used both as a base pointer and a stack pointer so to evaluate the call stack we need to recursively get the size of of a stack frame and then recover the return address and then evaluate again the call stack frame and then the return address and traverse it back like this so why we are interested in the call stack? We are interested in the call stack because when you execute malware on Windows you can generate some anomalies that can help with detection so we have in memory execution can be detected because when you inject code in memory if you're using especially to run a malware you do process injection for example or code injection and you're using an executable section on the heap what happens is that that section will not have a module associated so that executable memory is not backed by a file on disk if there is no file there is no pdata section if there is no pdata section there is no exception directory there is no way there is no information that the unwinding algorithm can use to dynamically get the size of that specific code so what will happen is that too much sorry what will happen is that you will have something like this so you will have a frame at a certain point that is not resolvable and this is associated associated with an unbacked memory region the other thing you can you can the other anomaly that you can detect from the cold stack is when you use directory indirect syscalls and we are referring to direct system calls actually direct antifunction execution like get proc address ntdll antifunction and then execute the function pointer is calling directly the antifunction and this will generate a cold stack that misses the that misses the why sorry for that that misses the what we the frames associated with high level api call this stack frame is also generated if you are using an indirect system call so if you are just locating the syscall instruction in ntdll and then you jump to it so the frame the stack the stack is pretty much the same and it misses the high level api frames of course but these are the anomalies when you want to detect this well you can do a periodic scan so you have a tool that stop the frames that stop the threads system wide get the context and unlike the stack frame you can have a conditional scan which is a periodic scan with another filter to filter for specific kind of threads that are in specific weight states for example or you can use hooking or kernel tracing which is instead done dynamically when a system call is involved you can use something like wti or another kind of driver to get the to get the user mode call stack and and for detections so to like as tax proofing previous research on the topic lead to the development of techniques to defend from specific scan periodic scan like stack truncation stack cloning and stack hiding or to defend against specific specific specific rule based detection on the call stack when executing a specific api so stack truncation was probably the first one used and it's an evolution it's a variation of normal return address proofing where the where we replace the malicious frame with a with a trampoline usually in a dll or an executable and then we nullify the return the return address prior to the to the trampoline this way we have a cut call stack like this which is surely better than having garbage in the call stack but it still it still has a defect which is this this stack is not unwindable so we still can have a detection for this reason then we have stack crafting stack crafting instead is a technique that is usually asynchronous like we have a system there is a specific function that get called and there are specific processes using that function in a legitimate way so what we do is that we record that execution that call stack at that point and when we need to call our target function we just craft the same kind of of call stack so that we can in this way we can bypass detection like sysmon hunting for specific call stack phrase or similar similar detection then we have stack cloning this as well has been developed to defend from conditional scanning and the idea is simple usually our implants go to sleep for most of like usually sleep most of the time so when I want to sleep using a delay execution like wait delay execution using a sleep or using a wait user requested wait state using wait for single object what I need to do is just hunt for a thread that is already with that state that is sleeping with that state and then clone its call stack this will make the call stack perfect perfectly legitimate or apparently legitimate so we will be defended by conditional scan that hunt for sleeping beacons stack hiding is similar but it uses another another kind of approach so the approach is that I will transform my thread in a I have a malicious code executing in memory I transform my thread in a fiber so I have a fiber thread now and then I create another fiber with a legitimate call stack that I use just for sleeping so when I need to sleep I just switch to the fiber thread to the other fiber that I created for sleeping and then this process gets repeated when I need to go back from sleep and this is what you have like this is the this is the fiber with a legitimate call stack and this is the code in in memory so they you can see that there are two different area two different memory area for the stacks and just one is active at a time so what is our contribution today we want to present two techniques full moon and half moon that are designed to obfuscate the stack at run time to avoid detection kernel based detection mostly or hooking detection so stack moving has some properties like it hides the caller from memory it obfuscates the stack so it's difficult to understand which kind of call stack was actually taken was actually generated when executing a specific system call and then it has an auto restore another restore technique so usually we use ROP to restore the stack we will see this is not always possible but yeah most of them uses like for the most part is use ROP to restore the stack so full moon is the first technique and it operates like that we have a function that we want to spoof an API or whatever and what we do of course we are executing this from our main module that can be in memory or can be like in this case it seems like it's a program but it can be even injected in memory there is no there is no problem this is for main module if you remember I'm referring to all the frames generated by the main module so it's the like I just deduct all the all the internal frames for the main module and what I do is I select three frames well actually four to be a bit more stealthy in our intentions but the needed are three and these are as an set set fpreg frame a pushreg frame other same frame and then yeah we added a concealed frame just to be a bit more stealthy and what they do is just at a certain point in the set fpreg frame what we are doing is that we are restoring the base pointer again and so this happens through this kind of call which is movrbprsp where we save our current rsp which contains the address on the stack that can say the return address of this frame and then with the second frame the pushfpreg frame the pushreg frame we push rbp on the stack now what happens this creates an opportunity for us to modify the address that was pushed in memory because that rbp now is on the stack if you modify it this will be evaluated by the unwinding algorithm as the address that should be used to restore rsp and this gives us an opportunity to tamper with this so that if this rsp points back to our thread initialization frame practically what happens is that the main module will completely disappear from the call stack ok this doesn't solve the memory problem so if you have you know unbacked memory section this will not resolve that ok but from the call stack is not visible anymore because doing just this will of course corrupt the execute the return flow of the program we use the sync gadget which is a job gadget here a jumprbx to hijack the return flow so that we can execute a restore function which is under our control and this restore function will reset the main module to what it was and just remove the fake frames so this is the the the technique in a nutshell and this is how the call stack is set up and as you can see there are the thread initialization frames and then there is no executable there is just kernel base and these frames are all generated by our algorithm now these frames have some logical inconsistencies that can be used for detection and my colleague will sure so I'll talk to you guys a little bit about the IOCs and why this doesn't necessarily create a perfect stack but does create a good looking stack so if we look at the slide we have currently you can see the spoofed frame the kernel base dot dll open state explicit and you actually see a location that plus zero x three nine d that's an offset so what's going to happen is that's the return address we would return to in order to continue execution within the stack now why is this relevant and why is this in IOC because technically if we well let's talk about what a call stack is based on the name a call stack is of course a series of calls so you will see an API call making another API call making another API call and this is often so you can debug and you can see at what API you may have crashed at what section you may have issues etc so it is supposed to show a series of calls to other functions now if we look at that spoofed frame the kernel base dot dll open state explicit we go to the return address but the return address isn't going to have a call because the return address is going to be after the call that's where we continue execution or we return so what we do is we actually go before the return address and we try to look at that little move byte pointer that we see there for the open state explicit function we can see that that is not a call and not only is that not a call to another function it is certainly not a call to create private object security with multiple inheritance so immediately this becomes suspicious and this isn't something a legitimate dev would ever do because it would just hinder their debugging process it would impact them negatively so this is something we would only see an attacker wanting to perform now while I say this is an IOC it's really kind of a difficult IOC you know it's hard to deploy at scale it's not really something you can hunt in your EDRs and if you observe this on every single function there would be very massive performance hits so while this is an IOC we're not necessarily sure there's a realistic detection method yet outside of the possibility of a utility such as Intel pin tracer which does it a little lower level than the operating system itself so if we look here we can tell you what we're looking for these are the call opcodes this is basically the assembly that we're looking for in our eclipse detector to observe full moon lock so we're looking for specific types of calls that mod rexw basically determines if it's like a long call or a short call for example and then we can see a relative 32 call the call r64 calls a register and then of course the system call the NTDLL lowest level call that you will often see because NTDLL often makes the sys call for you but if we were to observe there we would see a sys call instruction so this is the algorithm for all you PhD people out there basically the idea is we're looking for very specific things in our algorithm not just for stack spoofing but the detector is looking for an invalid stack period and not just by unwinding because as we know we can actually fool the unwinder as previously shown so what do we look for we're looking for unbacked memory regions that's the reflective injection detected unbacked memory means some injection was performed a private rx commit memory region was created this isn't backed to any files on disk no DLLs no EXCs so we consider this malicious often we consider some injection of some sort there can be false positives with this to be clear JITS browsers you know C sharp you'll see these private commit memory regions getting created and executing code so while this does help it's not foolproof to be clear the next is we actually look for the desync gadget because what full moonwalk actually does is it desyncs the stack with basically a ROP gadget in order to be able to effectively craft its own stack for restoration as well so we can just look for that gadget and often that can be an IOC as well additionally the call mismatch detected as previously stated message box A is going to make a very specific call to the next function if we look at message box A we look at the previous instruction and it's not the right call to the next function that's an IOC and the wrong address detected as well if the address is just not a call if it's not strict if it's not correct you know it has to be a call and it has to be a call in the correct order I actually think I mixed up the mismatch and the wrong address but the idea is the same in one we're basically looking for a call that's not a call or we're basically looking is it actually calling the next function because that's what a call stack is supposed to do this function calls that function calls that function for investigation so this is full moonwalk running we have a successfully spoofed message box A we've been telling you we're spoofing this message box A now we run our detector on the left you can see our four spoofed frames 8, 9, 10, and 11 our detector first catches create file internal as a spoofed call because the previous address does not the previous instruction is not a call and here's what's interesting we also get PSS query snapshot now if you look at the addresses as I said before this does unwind if you look at the PC address for PSS query snapshot it matches the return address for the create file function so an unwinder is going to think this is legitimate this is in fact what was called this is in fact where it needs to return but our detector knows better our detector goes there and says not only is there no call there's absolutely no call to create file W all we've done is fooled the Microsoft unwinder into thinking this looks legitimate so if you actually use Microsoft's built in unwinding this is what we did here it looks legitimate we only caught this because our detector is looking for more now there are a few false positives as mentioned before we see this a lot with JITs specifically we saw this in WinDBG it seemed to be have a lot of JITing going on we can see right here one of the processes for WinDBG has just these two entirely unbacked memory regions for reasons unknown to us it appears JITs don't always fully unwind and that these memory regions just always exist and so there will be false positives with JIT processes as usual so we did testing to see what the false positive rate actually was we wanted to determine is this feasible is this something we can actually do without you know overloading a team and we determined kind of you know it's not too bad so we'll see we tested this on five different machines standard just means it was a standard Windows user machine this is not a developer this is just a person who logs on watches video does work on Word documents and that's it dev is actually a developer somebody who installs visual studio sets up postman installs WinDBG preview and again you know it makes sense the developer is going to have a higher false positive rate they have more JITs they're developing more tools that are going to have weird unbacked memory regions you know no programming isn't really perfect and there's really no catch all for this we can see on Win10 the false positive rate is actually a little lower than Win11 Enterprise but you know we do see that for the dev machines it actually decreased a bit as far as Windows server goes we see a pretty high false positive rate that's likely associated with you know you're deploying your web servers on this you're deploying your dot net applications you're deploying all sorts of stuff that's going to sort of increase that rate of false positives and you know just to end this as far as detecting all this goes it's like I said for a hunter behind an EDR this isn't really telemetry we get some EDR provide call stacks for very specific functions so as to not be too intensive and we can sort of hunt on that and look for unbacked memory regions but as far as it goes for full moonwalk we just don't really have an effective way to check every single function call and ensure that this call is actually a valid call so in a way this unwindability is pretty effective in today's age So after developing the detection we actually talked about other kind of avenues that we could actually make use of this kind of knowledge and so as we've seen on except for the execute like in memory execution there was another problem which was the correct stack frames allocated when we execute an i-level API instead of going with the indirect syscode so we developed this additional technique which is an extension and it's half moonwalk half moonwalk is because we're just doing half of the technique we are just considering what is what we are calling and not really the caller and so what half moonwalk does is that we can always go to like we can always match an i-level API to a syscode so half moonwalk takes as input an i-level API name and it's relevant syscode instruction and it will trace all like it will trace the execution flow from the i-level API until it finds the relevant antifunction and what we're doing is that doing this tracing we also collect all the information all the exception information so all the information in the pdata section related to the prologue and what we do is that we recreate the frames of the i-level APIs before executing an indirect system call so how we do that we just need to literally emulate the prologue of the i-level API and what happens doing this is that we can recreate the same kind of call stack perfectly legit this time so we also respect the location of the return address which is placed after the call and the call is actually matching what is being called but we never call the i-level API so this is literally a syswhisper type of indirect system call just with added spoofed frame for the i-level API so now we have a little problem that we will solve afterward for the emulate system call which of course is a function that will do this spoofing and it's visible as for now so we will solve this later but so we are concerned about the return flow though if we have these frames they will return back to where they are supposed to return but we didn't actually execute the i-level API we just emulated the prologue would that be enough to actually ensure that the return flow is correct well yes and no so there are three strategies we can use one is the return place the return on unwinding so this is terribly worded what I mean is that we place the return address of the epilogue which of course is just deallocating the prologue it's just like literally rolling by the prologue so that will work but we will be detected by Eclipse again then because we need to move the return address not after the call but on the epilogue itself the other and this is what it happens instead we have the call to empty protected memory in this case instead of placing the return here where it's supposed to do because there are all these tests we just move it to the epilogue and this works of course but as I said we can be detected again by Eclipse with this because if we analyze the instruction before this return address it won't be a call anymore so we have a second strategy which is the targeted setup that works even better but it doesn't scale and the targeted setup is just like I can analyze what are the post call checks what I call post call checks are literally all these checks all these if that are executed after the call and to avoid them from you know branching to avoid the branching I just say what I need to patch I just see what I need to patch and then I patch it before actually executing the call now this is this is pretty much it works but you can't do it anytime and honestly it doesn't scale at all because you would need to know what thing to patch every time you execute an arbitrary function this doesn't scale at all so the latest thing we can do is I don't know what is happening I'm sorry another thing I can do is using other breakpoints so what it happens is that I place a breakpoint on return and we actually say sliding other breakpoints because we don't know how many frames we need to emulate so if they are more than four of course I need a strategy to out update the other breakpoints otherwise I will miss some frame anyway what it happens is that I place another breakpoint at the return address when the function returns back I just execute an exception handler that just execute the epilogue emulate the epilogue and then force the instruction pointers to return and that this way this scales well but I need always executable I always need executable memory because I always need to make the the exception handler like somewhere I need to store the exception handler somewhere so I always need some executable memory floating around so as I said before we also had sort of a problem with the emulate system call because we said well there is no function if you remember before there is no like this thing is actually pushing this frame and then jumping to the syscall instruction here so if someone is looking at this emulate system call it can be problematic so what we can create is an opaque architecture that as this is a if but you can place a switch case for example that just will when this is executed it will execute so now we are stepping through here to emulate system call w this is a frameless function it will execute something on the stack it will corrupt the stack doing this but it will grab the position of this frame function and it will just swap itself swap the frame of the frame function with the frame function this way it will restore the stack that will appear legitimate and here we have what legitimates this sequence of call is that we have a jump here to the restore proc of course this is opaque it's not that great but what we want what is really important about this is that even if we have something with no frame information associated as long as the stack size so the modification on the stack are equal to something that we are creating and is framed we can always replace it and fix the cold stack why this is important because there are ways we can do that using standard windows dlls so we selected this in particular because it's very it was already abused in exploit development to bypass exploit mitigation it's part of the exploit mitigation developed by windows dlls and it's the call it's the call to ndr server call to so we can use this kind of architecture here to completely hide our caller again so how this may work so if we can enforce in our implant maybe a stage zero if we can enforce in our implant this constraint so we want our implant at any time the internal the size of the frames of the internal frames of our implant they should always match the size of the under stop call to stack as you can see here you have a lot of space allocated on the spy on on the stack for this function if we can enforce this uh this constraint we can start a thread pointing to our shell code in memory which is our implant we can force these first two frames in the thread itself then we can start our implant and when the implant needs to spoof a function what we can do is call the call alt moon execute alt moon where whereby the first uh frame spoofed is the invoke function which is an arbitrary call to anything and then execute alt moon with the rest of the a high level api frame and eventually call the spoof function so what this creates is a completely legitimate call stack where we have the frame in the thread frame the the thread initialization frames then we have the first two call to ndr stop call ndr server call to ndr stop call to then we have the execution of our implant we have the return address spoofed with ndr stop call to stack which completely hides our implant frames and then we have alt moon executed with invoke and with and all the island level all the all the remaining uh high level api frames and then we have the system call again executed this will hide completely the um the caller as well so we are just we're just going back and try to you know complete what alt moon was not doing so hiding the caller now the only drawback of this is that you always in order to restore the functionality of the program on return you always need uh another breakpoint place on invoke so you can handle these two frames as much as as you want as we've shown before so target that setup uh or um or uh return on epilogue or um or exception or exception enders but this in invoke function you always need an exception ender to actually restore the return address of the ndr stop call to stack and return to our implant just a word about chat uh we are studying a lot uh into chat with shadow stack mitigation and indirect branching tracing entire branch tracing and so far we didn't find a way around chat uh it's not surprising so what happens with chat is that there is a shadow stack of course a clone like it maintains a list of all the return address uh that are placed just after a call and when on return it will check if this return addresses matches the user mode um like the user mode the uh writeable normal call stack and if they don't match there will be a cp fault uh that will crash the problem that will exit the problem um there is another another thing that is concerning about intel chat is that uh with indirect branch tracing there is no easy way to modify the instruction pointer as well we can't just use exception handler as as much as we do now uh but the good thing is that even in uh machine supporting chat so far uh there will there are always uh processes that are not chat enabled like browsers and stuff like that that we can still abuse for executing this technique so key takeaways you will you always have the public POC available here uh this will be enhanced with additional uh POCs after this talk so in the coming days um this POC of course is uh like both of the techniques are usually like have been developed and designed to obfuscate the stack at runtime so if you're planning of obfuscate the stack when you're going on sleep probably i'd rather use the stack cloning or the stack hiding and the reason is well as you see there are certain logical inconsistencies in this stack so if you leave it there for a long period of time you're increasing the chance of being detected by eclipse etc that an answer is just just go there and see it and do a bit more um analysis manual analysis on on the thread called stack um of course another another another point is that this technique uh can be used wherever you use return address proofing as an arrangement you can use sleep encryption and release the POC uh not not long ago about how to you can integrate uh sleep encryption technique to to to this to this um to this technique to this stack spoofing technique uh but it adds a layer of complexity which is not uh always easy to manage so a word of cautions if you are trying to integrate this in your own implant and yeah i think pretty much it's everything um thanks a lot for joining us and if you have any questions thank you if you want we are at the start sorry yeah so uh if there's time we're happy to take questions but i think he's also saying if you want to meet up with us after the talk we'll be happy to talk to anyone who wants to ask any questions excellent yeah thanks thanks guys thanks a lot