Memory manipulation is a technique that has been around for a long time. It is used for both security and malicious purposes. Today I'm going to cover some of the malicious uses of memory manipulation on Windows and how we can detect them. My name is Conor Morley and I'm a senior researcher with Secure. Formerly, I was a threat hunter for four years and I published multiple white papers and detection POCs. I have also presented at a number of conferences on security related issues. Today, we're going to be looking at what exactly a memory manipulation technique is. Then we'll be looking at inline hooking for exported functions, then for private functions and the detection issues associated with that. We'll then look at kernel level hooking on legacy Windows systems, heaven's gate hooking for Wow64 or x86 processors. And finally, we'll look at vectored exception handlers and how they can be used. For memory manipulation techniques. First off, what is a memory manipulation technique? So memory manipulation revolves around live process memory. So we're not focusing on files or file system manipulation. We're only focusing on alterations made to memory that is currently running within a process. Changes to this can affect the control flow, which allows for various activities to occur, which can change or siphon off information. It can prevent functions from occurring. It can prevent checks on integrity. It can equally execute malicious code. Changes over the past few years have seen that malicious actors have moved away from the idea of manipulating memory within Vixen processes and instead within to changing memory within their own processes. One of the primary reasons for this is that monitoring of alteration in memory of remote processes using virtual remote aliquot or anything like that is a long-standing security concern and there are security vendors are quite hot on detecting this. And as such, it's used sparingly because it is so readily detected. Instead, attackers started manipulating the memory of their own malicious agents or payloads in order to prevent detection. The reason for this is that manipulation of a process's own memory is not malicious in and of itself and a process has full access permissions to all of its own memory space, which means you're not running into some of the areas that you encounter with remote processes. This can be used for two primary purposes. One to prevent monitoring or two in order to redirect the control flow. Now, redirection is normally done in order to try and muddy the waters about what is happening. So you can call one function, which will then hook and call another, and you can make it quite complicated, which can make reverse engineering quite painful. But we're going to be focusing on the preventing of monitoring by security vendors in this case. To that end, I will be covering four very specific areas. Now, once I go through these, you should notice that the techniques that I'm outlining can be used for other activities besides just engineering. For example, evasion techniques where the hooking has been implemented or the memory manipulation has been implemented. Other activities can occur with some development choices and some alterations. But today I'm just going to be focusing on evading detection using these malicious techniques. To that end, I'll be looking at ETW event prevention using DLLs or libraries, library manipulation. Prevention of ETW telemetry via kernel memory manipulation. Interception of x86 or wild64 syscalls in order to evade detection. And finally, vectored exception handler or VEH specific bypasses that can be used in order to circumvent some security vendor techniques. So first of all, let's look at inline hooking for exported functions. Now, first of all, it's important to know what exactly I mean by hooking. So hooking is the alteration of a function's start up. It's a function that creates a file of opcodes in order to perform a secondary action. So imagine you call a function which, let's say, creates a file. Now, if you know where that function is located in memory, if you change the first set of commands to instead jump to a different function, that is what we would call hooking. So what it means is, is that every time that function is called, your hook or your alteration to the memory will cause something else to happen, which changes the control flow of that function in order to perform a secondary action that you have implemented. This is used by security vendors and malicious actors. For security vendors, this is normally done for telemetry collection. So, again, let's say a security vendor has a particular function they want to monitor for because it's high value or it's something they know is used by particular attacker groups. Now, by placing a hook on that particular function, any time a process executes it, the security vendor will know that it's a hooking function. So a security vendor or security software will get an event generated or telemetry generated with the process they called that function, what they were trying to do and when, which is very valuable. Attackers equally can use the same hooking for the same purpose in order to monitor system activity. But equally, they can use it in order to prevent the function from executing or they can redirect the control flow to malicious code or shell code. There's lots of ways that malicious actors can use this. And it is very simple to do. And it is extremely effective. One of the things to note is that the export address table or EAT within the PE header of libraries makes hooking very easy. So the EAT basically outlines all the exported functions, all the functions that are available to external processes from that library and where they're located in that library. This means that if you wanted to develop a hook for a particular function, looking at the EAT would give you information on where it's located in that library. Which allows you to target your manipulation specifically at that address, making it very easy. One of the other things to note is that dynamic information on modules that have been loaded into processes can be acquired either by the process environment block or PEB and through native libraries such as PSAPI. By examining these by either using PSAPI functions against a particular process or by examining the PEB of a process, you can actually enumerate all the modules that are currently loaded and their base address within that process. Which allows for the EAT's information to be cross-referenced to find particular functions within that process's instance of that library or that module. Which makes development very easy for hooking. Let's have a look at how this works in practice. So in this case, we're trying to target ETW event write. Which is located within the NTDLL module or library. Now ETW event write is a core function or exported function which is required for ETW event generation. So one of the things to note here is that NTDLL is loaded into every single Windows process. It is not optional, but this is just an example to show you how this could work. So first of all, an attacker would determine where the process or the function is located. To do this, they can do it in one way or another. In one of two ways. They can either use the load library and get proc address functions in order to load the library of NTDLL. And then find the function address using get proc address against the handler generated by load library. Or they can enumerate the PEB of the process to find the base address of NTDLL. And then cross-reference against the PE header of NTDLL. In order to find the, in order to calculate the RVA of where ETW event write was. The function will be located for that process. Either way, the result should be an address in this case of 7FF891650000. Once you have the address of where the function is located, you can determine what you want to do, how you want to install your handler. So in this case, because we just don't want ETW event write to work because we don't want ETW telemetry to be generated. We're going to install an opcode of value 0xc3. Which in x64 stands for 0xc3. Or ret or return. Now, if you patch or hook the start of ETW event write with a return command. What happens is every time the function is called. The first opcode returns control to the calling function. So if your function decides I want to generate an ETW event, calls ETW event write. As soon as it, as soon as it calls that function. The calling function is returned to with a stack. And it's detected and resolved. And it thinks that ETW event write has been successful. But because of the hook that's been implemented or the ret that's been patched in. We know that the ETW event write has not done anything. And as such, you prevent all ETW telemetry from being generated for that process. So now we know how this is implemented. How do we detect it? So detection methods are all to do with knowing, having a known good and being able to compare what is within live memory. So the first thing to know is that all volatile memory begins as a file. So all the loaded memory that's in volatile memory came from the file system to begin with. And the file system has an unaltered version of what is in volatile memory. Now, the loaded module file. As we said before, the information that can be found dynamically. Primarily through the PEB. Now, what's interesting about the PEB is that it provides a path to the source file within the file system for the module that's been loaded or the library that's been loaded. Now, let's say you wanted to monitor for NTDLL ETW event write. You could use the PEB to find the source file, which would be located in system 32. You could then use the PE header of NTDLL to find the offset of where ETW event write is located. Within the file systems version and use that to calculate the RVA address within the running process. And then you can compare what is in live memory at the address you've just calculated for the RVA against what is within the file systems version. And check whether there is disparity between the two. If there is a disparity, you can say that a hook has been implemented at the start of the process. And this can be done by by by by comparison. So if an alteration occurs within the first hundred. Bytes or something to that effect, that may be excessive, but you get the idea. You can detect whether a deviation has occurred due to editing. And this is very effective at detecting these types of hooks. But targeting is required for such operations. The reason for this is that inline hook or inline patching can be done legitimately. As we've said before, this can be done for security purposes. But equally, there are some processes such as Firefox which do this legitimately to a number of internal processes. And this can be done for a number of functions. So unless you are targeting specific functions of value, you may get a lot of noise or a lot of false positives on particular functions that aren't of security interest. So as such, targeting does need to be done in order to make this accurate and equally to make this resource manageable. Private functions within modules pose a different problem and they have some detection issues. So whereas with exported functions where the export exported function is defined within the EAT, which makes it very easy to locate. Private functions are not exposed to external processes in any way. They're not in the PEB, the PE header or any index. And as such, it makes this much harder to find. But one of the key things to note is that exported functions from a module will almost immediately. Inevitably rely on at least one private function in order to operate. This can be to do with argument integrity check, formatting checking, or it can do with initial connections or anything to do with that sort of effect. And if you know that the exported function that you want to prevent utilizes a private function, it can be just as effective to instead hook on the private function instead of just the exported function. Although it takes a bit more work. Adam Chester, researcher Adam Chester, did a great example of this using DNS query underscore A, which I'll demonstrate now. So in this example, he identified that the private function he wanted had a specific code signature. By scanning the by using a load library or enumerating the PEB, he found the base address or the memory address of where the module was loaded for that particular process and then scanned the module memory address space until he found the corresponding code signature. Which gave him the base address for the function in that module. From the above, we can actually see that the internal function that we're looking at is an internal symbolic function. And as such, it's not exported from this. We know that it's not going to be in the IAT or anything to that sort of effect. So without a signature or a PDB, which I'll come to in a minute, there is no way to know. That this function is there unless you already have the code signature. Now, the code signature was acquired by external reverse engineering and looking through the call stack of what happens before this performs a syscall. And just I've actually found that this particular private function could be hooked quite effectively. Now, in this case, you can actually patch the private function with a RET command. Again, the reason for this is that the private function in this case subsequently calls the ETW publication. So the private function in this case actually generates an event template, which then hands off after two functions to NTDLL event write transfer. But by putting a hook here of RET, you prevent the template from being generated. And the ETW event from being generated within NTDLL. And the calling function doesn't know the wiser. As such, we can see from the bottom window that the RET command has been implemented within the specific VA for where the code signature has been found, which prevents the ETW from being generated. We didn't click on the exported function. We hooked on the subsequent private function, which was called by the exported function. Which, from a detection point of view, creates some problems. The primary one being that private functions are, there's no information on them that's inbuilt into a process. Again, they're not in any of the header files. So you cannot enumerate them from an external process, i.e. they are private. The only way to do this is to have a program database file or PDB. Now, PDBs are generated at compilation time. And they do provide symbolic address and symbol information for private functions. PDB files cannot always be guaranteed to be acquired. The reason for this can be, they could be third party, module files where the PDBs might not be available. Or you may be in an environment where a connection to the Internet and the subsequent Windows symbolic server, where Windows PDBs are available, cannot be accessed. So unless you have a large database of PDB files walking around with your detection system, it can be hard to attribute or find them. So you need to find where particular memory addresses are located to private functions. Secondly, there's a double dependency issue. So what this means is that each module can have an IoT or import address table as well. Effectively, what this means is this module relies on a second module for functional capabilities. Now, because of this, if a module imports other modules, you therefore, and that module, let's say that your first module calls a second module, and your second module has the function that you're looking to monitor for. Any point in the stack from the first module to the second module, any function within that stack can be hooked in order to prevent that second function, the function within the second module, from being executed. And as such, all potential paths against all the double dependency modules need to be monitored in order to prevent hooking at an entire module. Now, if you look at the table in the bottom left, you'll see that the number of internal function symbols for the average module is around 3,000 to 5,000. But the dependent modules can be as high as 806 with an average of 583. Now, because of this, when you start to count up all the internal functions that you're potentially going to have to map against all the modules that are now dependent modules, that number rises very, very, very quickly, which can create a big resource problem. Finally, it's to do with the module version. So between module versions, some internal functions can become obsolete and they're removed, which means that all the addresses are subsequently changed. So when you're monitoring for module private functions, you have to have an accurate module type. And if you want to attribute it, you also have to have an accurate PDB version as well. Otherwise, you run the risk of misattributing an alteration to the wrong address, which can be very problematic. So instead of just having one PDB per module, you now have to have every version of PDB for every potential version of that module, which, again, creates a resource issue. So as such, it becomes much more difficult to detect these private functions being hooking. And there's no real reliable go-to method for monitoring these. They tend to be different. They're context dependent. The manual mapping dependent on the primary function to target function also needs to be ascertained. The reason for this is that different paths will have different values depending on their security vulnerability. And this needs to be done, unfortunately, manually, because all the potential paths can't be mapped automatically. And in order to assign resources to those of the highest value, research will need to be conducted. However, because, as I said before, this needs to be context focused, you tend to find that this is either going to be inaccurate due to the lack of PDB, or it's going to be so resource intensive because of the double linking issue that it becomes ineffective and very slow. So there tends to be a balance between the two, depending on what is available. Colonial hooking on legacy systems. So this is to do with systems pre-build 18950. Post 18950, Microsoft actually targeted this particular vulnerability by adapting patch guard and removing a particular kernel variable, which was susceptible to manipulation. Now, in order to perform kernel level hooking, you need to use a vulnerable kernel driver or an insecure variable type, as we just mentioned. The insecure variable type was something that Microsoft patched specifically. Some of the versions, some of the examples of this are Ghost in the Logs or GITL by BatTech, Infinity Hook by EverDocs, and Kernel Driver Utility KDU by H Firefox. So let's go into how these all fit together. So kernel level patching, first of all, uses the Kernel Driver Utility or KDU in order to effectively use a vulnerability within vulnerable kernel drivers to have ambiguous memory access to kernel space. Now, the vulnerable drivers are still available in Windows 10 and KDU does still work. The drivers have not been removed. This is something that's important to know, because what KDU does is it allows you to write a malicious kernel driver into kernel space by basically piggybacking on one of these vulnerable drivers. So in this case, a malicious executable will load a vulnerable driver, and then by using the vulnerability with the associated CVEs which are available, it will write a malicious driver into kernel space and load it. Now, the malicious driver will then start it to work. Now, using Infinity Hook, the kernel driver and the malicious kernel driver will check the ETW debugger data for the entry circular kernel context logger. Now, this is to do with an array of loggers which run within the kernel, and they all have different ID values. Now, the circular logger has an index ID 2. Now, this is important because the circular logger will run, as you might guess, circularly on every single sys command that's sent to the kernel, and it creates a log for it. Now, by finding this entry within the ETW debugger data, and then by innych And within this is an object called GetCPUClock. Which, unfortunately, is the type pointer. Now, once you've found the circular logger, you can overwrite the GetCPUClock pointer within the WMILogger context in order to point to Hooked Context. Now, once the hook is in place, what this effectively means is every time the logger is called and it tries to grab the current time, it will instead run the hook content that the malicious driver has installed. Typically, this means that the argument data and the syscall context will be siphoned off into an evil collector, which will allow them to monitor system level activity. But one of the really interesting things about an infinity hook is that it actually has the capability to run any syscall by using this hook capability. And the reason for this is that the circular logger actually runs before the syscall is executed, actually just before. So once the hook has been called within the logger, it can walk up the stack, find the function pointer to the syscall that's been targeted. And over time... It can overwrite it with any syscall the malicious actor wants. And from this, they have kernel level syscall operational capabilities by using this hook, which is extremely powerful. Posting the log works in the same way, but where the hook has been implemented, it only monitors one particular syscall function for anti-trace event pointer. Now, when it detects this, it instead diverts. It creates... It will divert. It will divert. It will divert to the hooked function. And the hooked function will determine against an IOCTL or IO control flag from the user space whether or not complication to the anti-trace event will occur. Now, this is important because notion logs allows for user land control of a kernel level monitoring element. Effectively, using IOCTL, a user land process can ambiguously decide, yes, generate an event. Or, no, I want to keep this secret. And this is important because what it allows for the prevention of no data detection. So, whereas with the previous examples where you can just prevent ETW events from being generated at all for a process, you can therefore actually scan for lack of data. So, if you find that there's a process running, but there are no ETW entries for that process, you know that manipulation or malicious action has taken place. But by using ghost in the log... So, you can actually prevent that and publish the nine events and keep secret malicious events from a user land control to a malicious hook that's been implemented, which is really, really cool. Detection for this, again, so it's post Windows 7, but it's pre-build 18950. And any system between that window is susceptible to this kernel level hooking. Now, as we said before, ghost in the logs relies on the kernel driver, a utility, or KDU, in order to utilize a vulnerable driver in order to load its malicious driver into memory in order to utilize infinity hooks and then install its IOCTL-based ETW prevention. Now, but there are some other evasion techniques, like by PG or by patch guard, which rely on... The kernel driver being installed via a different method. So, KDU can be used, but it is not the inbuilt mechanism. So, although kernel driver installation and service installation is something that can be monitored for, it won't necessarily use KDU to do that. But because ghost in the logs does use KDU, it's quite easy to detect, thankfully. The reason for this is that KDU relies on a list of known vulnerable kernel drivers and services in order to allow for the ambiguous kernel. This is the full list, but the ones in red are the ones that ghost in the log uses specifically. So, NAL driver, RT-Core64, GDRV, ATSZIO, and MSIO64. What's interesting to know is that the Intel Ethernet debug driver tends to be the go-to for KDU and ghost in the logs. And it is still present on the most up-to-date Windows 10 system. So, it is something to keep an eye on. But, by monitoring... For any of these kernel services being installed or ran, you can actually monitor for any of this activity because these are not run typically. So, any interaction with these services can be seen as malicious and it's very easy to detect. Alternatively, at the kernel level, by using a kernel level driver with appropriate capabilities, you can monitor for the WMI Logger Connect object instance with an entry Logger ID 2. And check the Get CPU Clock Pointer. Against a known good. Or simply just check that it doesn't point somewhere it's not supposed to. But by monitoring this, you can equally check for Infinity Hook and ByteDG. So, Heaven's Gate hooking for x86. So, this is... Heaven's Gate is the element that switches CPU modes between x32 and x64 when you're using WoW64 or for x86 processors. So, this is... The reason that this occurs is because if you have an x32 process running on an x64 host, the x64 host obviously only has an x64 kernel. But, if an x32 process decides it needs to perform a syscall, it needs to be translated to an x64 CPU mode, an x64 format, before the kernel can handle it. And so, because of this, all syscalls have to go through Heaven's Gate. In order to be translated and switched between CPU mode x32, WoW64, over to the native x64 CPU mode where the syscall can be handled. And because of this, hooking on this particular element means that you can monitor all syscall events from an x32 process running WoW64, regardless of what it is. So, instead of being a targeted hook for a particular function, you can monitor all syscall events from an x32 process running WoW64, regardless of what it is. Instead, you're hooking every syscall that that process is making. As you said, hooks elsewhere can be stealthy, as we saw earlier. You can hook on particular functions or particular exported functions or private functions or anything to do with that sort of thing. But Heaven's Gate gives you a subsystem wide for that process monitoring of all the syscalls it's going to conduct, which allows you to prevent particular syscalls from executing. You can hijack the information. You can manipulate the information. It effectively allows you to do whatever you want, as long as the appropriate hooking is put in place. Let's have a quick look at exactly how this works. So on an x64 system, an x32 program will say, OK, I need to call a function that's going to perform a syscall. So typically that will go to NTDLL, the WoW64 version of NTDLL. What's interesting is x32 processes running WoW64 actually have two versions of NTDLL. I won't go into that, but first of all, we will call the WoW64. The syscall will then need to be translated and then it will hand off to WoW64 CPU, which is where Heaven's Gate resides. Heaven's Gate will then perform what's called a long jump, which is done by a code segment change from the x32 WoW64 CPU mode over to the x64 CPU mode WoW64 CPU module. Once in x64 CPU mode, it will then hand off to the native NTDLL instance, the x64 instance, which will then handle the command running to the kernel and the syscall will be handled. The response will be then fed back down the same pipe. A long jump will be made back into x32, back to the NTDLL WoW64 instance and then back to the x32 process. And that's rudimentary how Heaven's Gate works and how WoW64 syscalls are conducted. But if you decide, if a hook is implemented on Heaven's Gate within WoW64 CPU, all of this becomes compromised. Now, at this point, as soon as the syscall is handed off from NTDLL to WoW64 in order to jump CPU modes, the malicious hook can do anything it wants. It can change the syscall that's being implemented, siphon the data, jump to malicious code, ignore the syscall and just return and not do anything. There are lots of things it can do. But because of this, it does mean that it doesn't matter which function it's calling, what module it's calling from. If it performs a syscall, it will have to go through WoW64 CPU and perform and go through Heaven's Gate and hooking at that gives full control and full access to all the syscall operations. So how would we go about detecting this? Before I move on to detection, it is worth noting that Heaven's Gate in this instance is done through the inherent native Heaven's Gate. But there are alternative functions to Heaven's Gate where the long jump that I've mentioned within WoW64 CPU, which constitutes Heaven's Gate, can actually be implemented manually in order to conduct the CPU mode transfer without going through the native module. Now, this is a malicious technique that can be used for evasion and is something that can be developed into a system. But it isn't manipulation of existing memory. So it's not something we're going to cover here. It's just something I wanted people to be aware of. That Heaven's Gate can be used by a manual method being implemented within malicious X32 code on a WoW64 system. So talking about detection. First thing to note is how do we know where Heaven's Gate is? How do we know where we want to monitor? Well, Huang Bui actually found this in his research on WoW64 processes and found that the TIB of WoW64 processes actually has a field filled at offset 0xc0 for fast syscall. And this is actually the exact address of where Heaven's Gate is stored relative to that process. Now, this is interesting because Jeff Chappell found that in the same offset, if you look at his repository of information, that the same offset is actually WoW32 reserved. Now, this is interesting because they obviously correlate to the two, but have slightly different names. So in a simple way, in a WoW64 process, this offset within a TIB corresponds to Heaven's Gate's address, which allowed you to highlight from any running process where Heaven's Gate is located and where to monitor. So x86, one of the things to note is that the gate location is at an offset of 0x7000 from WoW64 CPU's base address, and it is not changed legitimately. The reason for this is that WoW64 is actually loaded at the initialization stage of all WoW64 processes. So similar to NTDLL, although for very different reasons, WoW64 CPU, unless forced, will always load in the same location for a running process. And from that, you know that the gate location is going to be the base address plus the 0x7000 offset, and you can scan it very confidently. This can be forced, but it is very hard to do, and it's not common. The other thing to note is that x86 processors and WoW64 processors do legitimately change elements within WoW64 CPU as well. In the example below, you can see that the offset is 0x77365000. So it tends to be a modification just prior to where Heaven's Gate is located, but only modifications to the actual Heaven's Gate offset, which is the one that's in the box, are found to be malicious through all experimentation. So although modifications may happen elsewhere in WoW64 CPU, only at the offset for Heaven's Gate are we concerned to keep track of. In order to detect this, it's actually quite simple with the fast Cisco information from the TIB. The reason is that Heaven's Gate is only nine bytes big. Now, if you have a known good process, you can actually take you can actually collect the TIB, find the fast Cisco address, extract the nine bytes and use it as a template for every other process that is running. You can't, as with previous examples, use the file system version. The reason for this is that Heaven's Gate is dynamically allocated. And so the values in the file system are never going to correspond to what is within volatile memory. So you can't compare one to the other. You have to have a known good WoW64 in order to create a base against. But by comparing only those nine bytes, which, as you can imagine, is quite quick, allows you to detect any malicious hook on this particular element, which can be so nefarious. Once, if a malicious hook is detected, you can therefore look at the memory address where the hook goes to and analyze its content by memory dumping or code analysis or anything like that. And one example of that would be this example. So the byte sequence is actually in reverse. In this case, the malicious hook was detected and the hook content was dumped out for analysis. One of the other things to note is that in order for Heaven's Gate hooking to work, a trampoline has to be implemented. Now, a trampoline is effectively where the logisma Heaven's Gate code is stored elsewhere that the malicious hook will know where it's been moved to. And the reason for this is that when the Heaven's Gate logisma location is called, the malicious hook will then be called, which will perform the malicious hook action. And then after that, it can then perform the Heaven's Gate CPU mirror jump by simply jumping to the trampoline or wherever the code is being moved to. Just an extra bit of information. But by using this nine byte check during experimentation, it was one hundred percent accurate and the information produced was it gave great data for further analysis. Finally, let's look at vectored exception handlers. So first of all, let's look at how exception handlers work. So try catch code blocks are an example of exception handlers. So effectively, a try catch block is a frame. So these code blocks, where you have the try code, that code is within the frame of the catch that you've implemented. And what this means is that as programmers should know, if an exception happens within your try block and it corresponds to what you have specified in your catch statement, the exception will be handled by your catch statement. And this effectively creates a frame for that exception. Now, the exception can either be specific or general. Now, exceptions actually originate from the CPU, not from the program itself. The CPU will notice the exception has happened. It will then prompt the kernel in order to with relative information, which will create the environment context, exception records and all this sort of stuff, which is then handed back to user land for handling within the program itself. The kernel determines what kind of exception type it is and what handling mechanisms are required based on the data that's issued by the CPU. The kernel does nothing. It simply interprets what the CPU is telling it and then hands it off to user land to actually handle the issue. And this is typically done through NTDL in user land. So have a quick look at what this looks like. So after the kernel has handed back control, I won't go into how the kernel handles the CPU information and translates it into a trap frame and then the environment and exception records and all that sort of stuff. We're going to look at the user land. So once it's handed back control to user land from kernel using an IREC command, the trap frame environment context and exception record are all available. Within the user stack. But the trap frame will have an EIP, which will trigger the use of KI user exception handler within NTDL. This will subsequently call RTL dispatch exception, which uses the information from the environment context and exception record provided from the kernel by translating the information from the CPU to know what frame the exception has occurred from within which process and from that it then scans the lowest frame, again, provided by the environment context and starts to check the frames outwards. And effectively what this means is if the first frame adequately handles the exception that's been specified, it doesn't need to go any further, but if not, it will move out and out and out and out and out through all the frames containing it until it will reach the end of the exception frames or the exception lists. And if it does that, it then turns into a general exception, which normally causes the program to crash. So I've been saying frames and exceptions, but frame based exceptions are actually what we call structured exception handlers or SEH. So a try catch block is an example of an SEH. It is a framed based exception, which is very specific. But whereas with a try catch block, again, if you have a try block and a catch, but the catch doesn't check the exception, it will then move to the surrounding frame to see whether that can handle the exception and move further and further out. As I just explained, the SEH adds to a chain. So the SEH list is actually connected one after the other, and it will do it as again, from a lowest frame and it will start to move outwards. If the chain is exhausted, it will move to a general handler, which is normally at a kernel system level, which normally causes the program to crash. And if not the program to crash, it can cause the operating system to crash. So not something you want to have happen. And that's why exception handling is quite important. Well, program to crash, not likely the kernel, depends on what you're doing. But what exactly is a vectored exception handler? So VEH are not frame specific. So whereas SEH or structured exception handlers are very frame specific, vectored exception handlers are the alternative. So instead of being frame specific, they override the priority of SEH and have a higher priority. And effectively, the exception that is generated from the kernel within the exception record will be checked against the vectored exception handler first before it moves to the structured exception handlers for that process. This was introduced within Windows XP specifically to be able to override the SEH from a developer standpoint. So that instead of having to find a particular exception frame, you could just install a VEH and it would overwrite the exception you had in place. But because of that, VEH has an inherent higher priority and will run first. And the way Microsoft did this was to install RTL call vectored exception handlers within the exception handler process within ANT DLL. So RTL dispatch exception, which we just saw, hands off and starts to scan through the frames, instead will call RTL call vectored exception handlers and scan the vectored handler list first. Now, this is very important because this means that no matter what you do, VEH, vectored exception handlers, will always have a higher priority. And if an exception handler is found for that exception type within the VEH, it will handle the exception regardless of whether an SEH exists. Now, each handler instance can handle multiple exception types. So whereas with SEH you normally have to have a specific exception type or a general exception type, VEH can handle multiple specific exception types. And it does this by within the handler, actually do a comparison between what is inside the exception context against what is within the vectored exception handler for what it can handle. And this is important because vectored exception handlers can handle vast arrays of exception types depending on how they're built, which can make them quite clunky, but equally can make them very sort of like a catch all, which is quite nice. Adding to the list can be done at the front or the end at the discretion. So you can either have the higher priority or the lower priority, depending on how you want the VEH. But as you can imagine, from a malicious access standpoint, you're going to want to be at the front of the list so that no matter why exception comes in, your VEH is being checked first. But what's interesting is that you can add a VEH on the command just before you know you're going to do something nefarious, which will cause the exception. So you know whether your exception handler is going to be called first. A VEH list is circular. This is something just to know that effectively when it reaches the end of the list, it will go back to the start. So there has to be a check against what the handler entry ID is so you know when you reach the end of the list. And finally, that the handler function pointers are encoded using a process specific cookie. So only by using a process specific cookie can you actually find the handler's legitimate pointer address and find the handler code. So let's look at some of the malicious uses. So as we said, VEH will always be done first, regardless of what SEH is in place and VEH can be added at any point. Because of this, it can prevent code integrity checks, specifically with page guard, which I will show you a great example in a second, it can perform force jumps. This is to do with how VEH exception handlers are added to the VEH list and it can also suddenly bypass functions. This is where the security malicious memory manipulation is going to take place. And I'm going to give you a great example of that in a minute. So VEH can hijack the exception because it's not high priority in SEH and it can need to be done against the victim process as long as there is adequate access. Or it can be done against malicious program simply just to remove any visibility by security vendors by checking for these if there is exception generated behavior. So let's look at code integrity checks. So this is actually from a researcher, Shen, and they violate the page guard exception and then effectively use VEH to they install a VEH seconds before they violate the page guard, which creates a status page guard which guard page violation exception. But their VEH effectively goes, OK, that's happened. But don't worry about it. And because their VEH has a higher priority, the program just goes, yeah, that's fine. The exception has been handled. What they actually do is inside the VEH box, they reassert the guard flag of that particular page. So they violate it, have the exception, reassert the flag within the exception handler and then hand back to the process. They're saying, yeah, everything was handled. So from that, they're able to basically violate page guard as they see fit, as long as the VEH is in place to handle it, which is which can get around a lot of integrity checks, which is quite cool. Force jump, as I said, this is to do with how vector handlers are added. So addVectorExceptionHandler is the primary function of adding VEH to a system or running process, but addVectorExceptionHandler actually only requires a handler pointer or just a pointer to be to be added as an argument. So from this, you can allocate memory with a bunch of malicious shellcode and then hand it off to a V and then add it as a VEH. And then as soon as an exception comes in, as long as you put it at the front of the queue, your shellcode is going to get executed. So you've effectively found an exception based shellcode execution mechanism by using VEH, which is pretty cool. Satellite bypassing is the one that we're really going to look at. So VEH has the capability. So when a handler, when you're generating a VEH handler, when it's handling an exception type, it has two results. It can either have exception continue execution or exception continue search. And what this means is continue execution means the exception has been handled, hand back to the process, continue execution. Continue search means my handler wasn't able to handle the exception generated. Move to the next in the list. Now, because of this, if you deliberately generate errors or you know that an error is going to be generated, as with the page guard, you can actually just say, hey, this has happened, don't do anything about it and move on with the process. But equally, what you could do is you could say, OK, I know that a function is going to be called in a second that I don't want to happen. So let's say I did a divide by zero, which will be an exception. And from that, I'm going to change the function pointer to go just after the command that I don't want to occur and then hand back to the running function. And it will work. From this, you're able to basically jump around the code that you don't want to execute or by basically deliberately triggering exceptions as long as a VEH is in place, because it will have a higher priority, which is very, very cool. So detection of this relies on a number of things, but primarily you need to know where the VEH list is. So prior work done by researcher Dimitri Forney did some really great work and he found that the VEH list was actually stored within NTDLL and it was under the pointer LBRP vector handler list. And what he found was is that by scanning heuristically for a particular pattern within a known NTDLL function, you could find this pointer and from that you could extract the list of VEH and enumerate through it. I really recommend reading his work. He did some great examples of this and a lot of information on how the heuristics work. But effectively, the list is not exported, it is not externally available. So you have to do this, reach into the NTDLL systems in order to find the pointer and extract the list, which is a nuisance. The other thing to note is that the handler pointer, as we said earlier, is actually encoded by the particular process cookie. And in order to decode this, you have to use a combination of query information process and roto-write64. Ollie Whitehouse over at NCC Group did a brilliant POC on VEH enumeration where he actually solved all of these problems and allowed the handler pointer for x64 to be decoded and attributed to their modules and process spaces. Again, really recommend reading that work. He really jump-started everything. It was very impressive. Do recommend reading that. But one of the things that's important from a vector exception handler detection standpoint is what exceptions they are handling. So although you can see when a VEH has been generated and what they're attributed to, that's great. But unless you know what they're doing within the handler or what exception types they're looking for, it's not necessarily going to provide that much value. But by checking the exception type, you can actually check for, again, as the example at the bottom of the window shows, institute divide by zero. So if you're looking at a divide by zero, which is in a randomly named process, you can say that this is likely going to be a VEH malicious instance. Now, this can be done using opcode heuristics by checking for comparison operators, because all handlers will have to check the exception context exception type against a known exception type to see whether it matches and to know whether it needs to be handled, which is a really easy way of finding what exception types are being used. And from this, it allows you to do a process to exception type comparison to see whether there's a mismatch. And this is especially true about certain debugging exceptions, which is actually key to a case I'm going to come to in a second. So when it comes to detection criteria, pointers outside processor module space, i.e. virtual allog just at anywhere for shellcode, pointer to anomalous module functions. So again, if there's a randomly named module and this VEH is pointing to it, it's pretty suspicious. Pointer location has C-O-W in legitimate module. So this is to say, copy on write, which is effectively a page modification in a shared library. If the process, if the VEH is pointing to a process specific memory page within a legitimate module. And we need that page, that's pretty sus and should probably be looked at to make sure that that particular function hasn't been hooked in order to prevent further malicious action by basically triggering her off from that hook. Pointer location containing shellcode. Again, this could be done through memory dumps of the handler code or just through general heuristics and event type handler is unusual. Again, as I said, depending on the process and the handler type, you can actually make an association between what handler types are standard for that process or equally standard for a system. So the example for EDR bypassing using VEH is FireWalker. So FireWalker was developed by MDSEC back in 2020. And it's a brilliant POC. Effectively what it does is it scans for EDR hooks on known functions for their telemetry gathering for security purposes and steps around them using VEH. It does this by scanning for pointer duplication or func scanning and trap frame single step generation by registry manipulation. I won't go into those. The second part about trap frame single step, effectively, they manipulate one of the registries to trigger a single step exception, which is what triggers their VEH or malicious VEH into working, which is the bottom example VEH exception single step handling. And that's where we're going to look. So operation is, it creates a VEH entry for the exception type single step, sets the TF flag in the registry in order to make the exception single step exception be caused on each individual execution that's done. It then calls the desired function, knowing the single step exception is going to fire, then it uses the VEH to scan each subsequent inspection to see whether it's a jump command or a hook command. And if it is, it then scans the memory to see whether there is a subsequent duplication or basically a trampoline back to the same code, which would indicate a hook. If it is found, it then replaces that jump with the trampoline. And then it basically jumps itself back into the legitimate process without executing the EDR's hooked function. So it scans. So you create a VEH, you trigger it to make these exceptions on each subsequent command. You then run the process you want. And you check to see whether it jumps to, it does a hook or a hook jump. And if it does, you then scan to see whether it's duplicated. And if it is, you just jump around it and avoid the EDR telemetry acquisition altogether. And then after that, you can just remove the VEH, reset the TF flag and continue your operation. Brilliant. Really, really, really cool concept. And although it has its limitations, a really cool, really cool. capability. But one of the things about detecting this is the fact that it uses VEH. Now, as I said earlier, the exception types that are monitored for is one of the things that can be really easy to detect on. So exception single step outside the use inside a debugger. Yeah, that's something that should be flagged and really easy to basically say something's not right here and it needs to be looked at. And as I said, the VEH exception type of the process nature easily equals detection. Again, if you have a calculator doing, you know, buffer overload exception handling, but then there's like three instances of it or two instances of it, not going to be right, especially in the VEH, because that should be something that should be SEH, so it's something that can be easily monitored for. Now, people would say that this could be handled by SEH. But again, because SEH could be used instead of VEH in FireWalker's mechanism. But because VEH has a high priority and it's easy to code and it has, it's just more dynamic, attackers will tend to use VEH over SEH. Simply because it has is much easier to use. One of the final things to know about VEH is the x86 issue, namely that the VEH for x86 process is stored within the x64 NTDLL and as such cannot be accessed by x86 processes, which when you are enumerating them and trying to process the handler and see what's inside them makes it very difficult. You end up with this memory access violation where you're trying to access 64-bit memory from x86. Now, this is a physical address extension issue which can be addressed through the kernel MMR. But it goes beyond the scope of what I'm going to be covering today. But it is something that is known to exist. And the only way to do this is to take the physical address, the virtual address of the x64, translate it to a physical address and then translate it to a PAE of x32 to then be run through the x32 handler or you have to bridge between manually between x64 and x64 CPU modes in order to validate what is inside the handler for the x32 process, which is quite complicated. This is something that is still ongoing, but the best method that I know of is to use the MMR within the kernel. So in summary, memory manipulation is, it's not, it's never going away. It's been here for a very long time. And the capabilities that are being implemented with the new technologies introduced by the operating system and equally the change from tactics to victim processes to their own processes is something that shows that they're always finding new ways to utilize tweaks inside live memory in order to make detection or just execute malicious, malicious commands. But monitoring is always a fine balance between accuracy and resources. Ideally, we'd love to monitor every single memory change. And be able to 100% accurately say, you know, this is attributed to this process and these bytes are going here and all this sort of stuff. But the resources required for that are just way too high. So it's always a balancing act between the two and you have to decide where is the best place to monitor. Finally, is the capability to avoid detection in unrestricted operations makes this a high value. And what this means is memory manipulation provides a window to malicious actors to effectively size depth detection multiple ways. So it is never something that they're going to give up on. It's always going to be something they're going to keep trying and keep finding new methods for. And although the methods I learned today are interesting, there are obviously a lot more out there and it's best to give you this ground because this is not going to stop. Thank you for listening. If anyone has any questions, do feel free to email me at my works email. And thank you for listening.