I'm super excited to be here, first time at DEF CON, so welcome to my talk. I am very tired, I am severely hungover, so I really hope this goes well. Now, before we get into the talk itself, I wanted to start with a short apology. So, in case anyone didn't really read the talk title really well, or the abstract, and thought that they were going to hear about the latest developments in VBScript malware, I'm really sorry, because we're not going to be covering VBScript, but rather the other VBS, virtualization-based security. So yeah, sorry for all the VBScript fans, anyone is leaving? No? Okay. So, yeah, I guess we can start. My name is Uri David, I'm a security researcher at Akamai. I have a background in red teaming and threat hunting, and today I cover a wide variety of research topics in the Akamai security research team. And for the last few months I really focused on the area of virtualization-based security. I believe this is one of the most significant and interesting advancements in security that Microsoft have implemented in recent years. So we will start by briefly covering it, understand what it does, how it does it, and why it does it. And then we'll get to VBS enclaves, a feature which is enabled through VBS, and have a lot of very cool security applications. And then we'll get to the fun part, abusing VBS enclaves. We'll see how we can take this security feature and kind of flip it around, and see what attackers can actually do with it. Okay, so let's start with VBS. And to talk about VBS we first need to talk about boundaries, security boundaries. So traditionally in order to protect the operating system, Microsoft and other operating systems as well, relied on processor rings. So the idea was we'll put the OS, the kernel, in ring zero, we'll restrict user applications to ring three, and by doing that we can ensure that attackers will not be able to tamper with the kernel. Well, this really proved to be insufficient over time. Attackers have done things like exploiting the kernel as it exposes a very significant attack surface, or exploit one of the millions of third party drivers through bring your own vulnerable driver techniques. And yeah, this really again proved to be insufficient. So we needed an additional security boundary into the OS. And this is where VBS comes in. So VBS came to our life through Hyper-V. So let's start with that. Hyper-V, Microsoft's hypervisor, traditionally used to run VMs, either locally or in the cloud. Now, one of the most important things that Hyper-V has to manage, and any good hypervisor for that, is isolation. We would never want to allow a situation where one VM can access the memory of, in general, the resources, but for our sake today, the memory of another VM. So Hyper-V already comes with a very strong memory isolation capability. I will not go too deep into how it does it. There are plenty of good resources online. But yeah, so we have this very strong ability to restrict one VM from accessing to the memory of another one. Now, someone at Microsoft, at one point, probably had a good idea. Let's take this same primitive, isolation primitive, and apply it to the operating system itself. So now, instead of isolating two different VMs, what we can do is we can take a region of memory within the operating system and isolate it from the operating system itself. So this is the root of VBS. And it turns out that this isolation actually enables us to add additional security boundaries into the OS. So these are called virtual trust levels, or VTLs. And here we have what we had before. So we had two execution modes. We had user mode, ring three, and kernel mode, ring zero. Now, when we add VTLs into the mix, things get interesting. So first we have VTL zero. This is the least privileged VTL, which is also called normal world. And you already know it. So VTL zero contains the normal operating system. We have their user mode and normal kernel mode. Things get a bit more interesting when we introduce VTL one. Now, VTL one is more privileged than VTL zero. And this privilege comes in the form of memory isolation. Code running within VTL zero cannot access code or the memory of VTL one. On the other hand, VTL one can access the memory of VTL zero. So this is how this privilege comes into effect. So within VTL one, we have two new execution modes. We have VTL one, ring zero, for the secure kernel. This is a more privileged kernel that can actually be used to apply and enforce additional policies to the normal kernel. And we have isolated user mode, or IOM, which is VTL one, ring three. Now, on one hand, we are running in ring three. So we can't perform sensitive kernel operations. But we still enjoy the advantage of running within VTL one. Isolation, as the name suggests. And the thing is that IOM is going to be protected from threat actors that compromise the normal operating system. So even if an attacker were to compromise the kernel itself, it will still not be able to access anything that is stored within IOM. Okay, so these are VTLs and the new execution modes that they introduce. And now we can move on to our next topic, or our main topic, VBS enclaves. So, an enclave is an isolated region of memory within a process. The process cannot write into the enclave, it cannot read from the enclave. The only thing that it can do, is it can invoke methods that are exported by the enclave. So, why something like that is useful? Let's consider the following scenario. Our process handles some sort of secret. So , either something like an API key, a password. Now, let's say that the process were to be compromised. Either directly, or let's say the operating system itself was compromised. There's nothing to stop an attacker from then accessing the memory of the enclave and stealing the secret, excuse me, of the process, and stealing the secret. Now, what we can do, if we design our application with an enclave, is we can store this secret within the protected and isolated enclave. Now, if an attacker compromises the process, they will never be able to exfiltrate the secret. In order to actually utilize it, what the process can do, is it can invoke the methods that are exported by the enclave. So, we won't be able to just access the key and directly encrypt values using it, or authenticate directly, but we can request the enclave to do operations using it. So, we pass the value and the enclave encrypts it. So, this is the idea of enclaves. There are additional usages for them as well, but I think this is a good start for that. And enclaves are a general concept. VBS enclaves are Microsoft's implementation of them using VBS. It is actually a very elegant implementation. What we do is we simply split the processes in half, where the normal process resides in a user mode, VTL0, and the enclave will reside in isolated user mode, VTL1. By doing that, we ensure, again, that nothing in VTL0 can access the data within the enclave. Okay, so let's discuss a few more details about enclaves and how we use them in Windows. So, any process can create an enclave and load what is called an enclave module into it. An enclave module is a DLL that is compiled specifically for the purpose of being run within an enclave, exporting the functions and stuff like that. And once we load the DLL, again, we can't access it directly. We can invoke its exported methods by calling the call enclave API. And one final note, before we move on, is that we can't just load any arbitrary DLL into an enclave. Microsoft realizes that running within an enclave should be something that is restricted. And because of that, in order to load a DLL into an enclave, it must be signed using a Microsoft certificate that is designated specifically for signing enclaves. So, this is going to be the main challenge for us for the rest of the talk. So, now that you understand what VBS enclaves are, let's get to the fun part. How we can abuse them. So, the first thing that came, at least into my mind when considering this, was the idea of running malware within an enclave. So, the same advantages that are provided to normal applications could apply to attackers as well. So, first, obviously, we will have inaccessible memory. EDRs and security analysts, whatever, they won't be able to scan our memory in order to identify malicious behavior. Another advantage has to do with behavior, not just scanning memory. Well, it turns out that it is much harder, or pretty much impossible, to trace API calls that are performed from an enclave. So, let's understand this. When a normal process triggers a Windows API, we go through a process that I believe most of you probably know. We go through a series of system DLLs, eventually anti-DLL. From there, we invoke a syscall into the normal kernel. Now, the thing about this flow is that EDRs have a lot of visibility into it. They can hook our calls into DLLs. They can monitor our activity in the kernel. There are a lot of detection opportunities here. On the other hand, when an enclave triggers an API, we no longer go through the normal VTL0 DLLs, but rather through a separate set of VTL1 resident DLLs. So, instead of going through anti-DLL, we go through VEL DLL, VBS Enclave Runtime DLL. And from there, when we invoke a syscall, it is managed by the secure kernel. Now, the thing about this process is that it occurs exclusively in VTL1, meaning that EDRs and any other security product will have zero visibility into anything that goes there. So, yeah, another advantage that we gain. Okay, so, yeah, we understand why it might be cool to load malware into an enclave, but in order to load a DLL, it needs to be signed. So, in the next section, I'm going to cover a few approaches that attackers can use in order to get malicious code to run within an enclave. So, we'll start with the straightforward ones, I guess, that I did want to mention before going to the next ones. So, attackers could obtain a legitimate signature. Microsoft exposes enclave development to third parties, and we could theoretically see attackers legitimately signing their enclaves as we have seen them do in the past with things like drivers and executables. So, they could either compromise an authorized signer or obtain signing privileges legitimately through a shell company. So, this is one option. Another option would be to exploit a vulnerability in the Windows loader that is in charge of validating the enclave signature. So, in theory, someone could find a vulnerability in this process, which would allow loading unsigned DLLs into enclave, which is exactly what Alex Unescu demonstrated last year. So, while this vulnerability was patched, advanced threat actors could certainly find additional avenues to get past this verification and get unsigned code to run within an enclave. Now, these two approaches are certainly valid, and I believe that advanced threat actors, nation-state threat actors, could use them. But I don't know what about you. I'm personally not a nation-state threat actor. So, I wanted to find something that might be more accessible to, well, less sophisticated threat actors. And I ended up finding two additional paths to get code running within an enclave. The first one is the abuse of debuggable enclave modules. So, it turns out that if you want to debug an enclave during development, it's actually not a straightforward task. Think about it. Enclaves run within VTL1. Our debugger runs in VTL0. If we simply try to debug an enclave, we won't be able to do that, as we can see here. You can't access the memory. You can't place breakpoints. So, yeah, Microsoft, I believe, pretty early realized this issue. And because of this, they implemented debuggable enclaves. So, when you compile an enclave, you can specify a flag that will mark it as debuggable. And when you do that, well, like magic, you can now debug the enclave. As we can see here, we can now access the memory of the enclave. We can place breakpoints. Yeah, we can have a good time. Now, when I initially saw this, I was kind of confused and surprised. Because we know that enclaves reside in VTL1, and we are accessing their memory. So, it's kind of weird. At first, I thought that Microsoft might be pulling some sort of a trick here, where they are emulating the enclave when it is debuggable. But upon digging into this, I realized that this is not actually the case. Debuggable enclaves still reside in isolated user mode. And to enable the debugging that we have just witnessed, the secure kernel actually implements some exceptions. So, for example, if we try to access the memory of a debuggable enclave, we'll go through the secure kernel debug read-write memory function that will determine if the enclave is debuggable. And if it is, it will allow us to access it. Similarly, we have the debug protect virtual memory, which will allow us to change memory permissions on debuggable enclaves. So, now that I understood more about these special enclaves, I wanted to see how we can abuse them. So, first, and pretty obviously, it is not a good idea to use a debuggable enclave for a production application. Microsoft clearly acknowledges this in their documentation. Because if you're using a debuggable enclave, you're essentially not really using an enclave. Anyone can access its memory. You might as well just allocate a normal memory buffer. But, yeah, so this is a risk. It is acknowledged. But it turns out that there's another interesting way that attackers can actually abuse this. So, let's say that an attacker obtains a signed debuggable enclave. So, a DLL, which is signed and marked as debuggable. It turns out that enclave modules are not paired to a specific process. Meaning that any process can load any module. So, what an attacker can do is they can load this debuggable enclave into their own process. What they can do next is they can take advantage of the exceptions that we mentioned before to debug the enclave and achieve code execution within VTL1. So, let's look at this process, how it would look like. So, here we have an attacker who obtained this type of debuggable enclave and loaded it. And we can see that the enclave contains an enclave routine in green. A function which is exported by the enclave. What the attacker can do is they can obtain the address of this routine. They can then change their memory permission to allow a page execute read write. This is possible because the enclave is debuggable. They can then overwrite the routine and invoke the shellcode by using call enclave. So, we can't just run arbitrary addresses within the enclave. But, because this is an exported function that we overwritten, we can trigger it normally by using call enclave. So, yeah. This is a cool technique. It works. But, there are some caveats to it. So, let's cover them. The first one is that the memory access actually goes both ways. So, just like we can access the memory of the enclave, EDRs can do the same thing. Because, as we just saw, the exceptions apply to them as well. Despite that, there are still advantages to using this technique. So, the first is that this is an unusual way to execute code, which I pretty much guarantee that if you do today, no one will detect. So, yeah. EDRs might not consider this scenario, even though they can theoretically detect it. And the second advantage is that enclaves, even when they are debuggable, will still use the API call chain in VTL1. So, hooking their APIs will not be possible. The second unfortunate catch is that, at the moment, I haven't actually managed to find one. So, I have scoured the internet, I have wasted way too much time looking through VirusTotal in an attempt to identify such a file, a signed debuggable enclave. But, at the moment, I was yet to be successful. Despite that, I still believe that I haven't just wasted five minutes of your time, because I really strongly believe that it is only a matter of time before such a file finds its way to the internet. So, the thing about enclaves today is that it is kind of a not really adopted technology. Not a lot of people use it. But Microsoft really began pushing towards this direction, implementing a lot of things using enclaves themselves. And I believe that we will begin to see more third-party drivers, developers utilize them in the future. And we all know developers. Developers fuck up. And one file will find its way into VirusTotal, or somehow into the internet, and this technique now becomes available. So, yeah, I think it is worth considering. Okay, so this was the first idea, abusing debuggable enclaves. And we'll now move on to the second idea, exploiting vulnerable enclave modules. So, the idea is not mine, let's say, because I kind of applied a very well-known technique. If you're not familiar, bring your own vulnerable driver, is the concept where Microsoft enforces signing on drivers, so you can't load a driver into the kernel unless it's signed. So, attackers figured out that what they can do is they can obtain a legitimate signed driver with a known vulnerability, load it into the kernel, and then exploit the vulnerability to get code execution. Well, what I thought was, why not apply the same idea to enclaves? If we manage to find a vulnerable enclave, we can load it, exploit it, and get our code running within IOM. So, luckily, such a vulnerability exists, CVE-2023-36880. This is an information disclosure vulnerability in an enclave module used by Microsoft Edge. And despite it being listed as an info leak, if you check its patch notes, you will see that Microsoft mentions that it could lead to limited code execution. So, I decided to dig into this a bit more. This vulnerability was discovered by Alex Go from the Chrome security team, who also shared a POC for exploiting it. It is actually a relatively simple vulnerability, and it allows an arbitrary read-write into the enclave. So, even though this is a normal, non-debugable enclave, we can, after loading it, write whatever we want into the enclave address space and read whatever we want from any address as well. So, seeing this and understanding this, I was able to obtain a vulnerable version, a signed version of this enclave, and I began attempting to exploit it. So, the idea was, well, we have arbitrary read-write, we could do something like leak an address of the enclave call stack, we can override it with a ROP chain, and then get arbitrary code execution. Well, it wasn't going to be that easy, as I quickly discovered, because enclaves are protected with ACG, arbitrary code guard, by default. So, if you're not familiar, ACG is a mitigation policy that ensures no dynamic code can run within a process, meaning code that wasn't present in the original files as they were loaded, cannot execute. In practice, it means we can't allocate new executable pages, and we can't turn existing executable pages writable. So, we could theoretically create a full ROP chain exploit, which turned out to not be only a theory, I'll get into that later, but at this point of the research, I couldn't find a way to get something useful with this enclave. And here we see a demo of my own enclave that I have written in order to test this, and we can see that when we try to allocate a read-write-execute page, we fail with the error of we can't run dynamic code. So, yeah, at this point, I was kind of stuck, and you might say it got into my personal life as well. I knew that I had something here. So, the ability to write and read whatever I want into the enclave felt just too cool to be not useful. So, yeah, after some sleepless nights and many days trying to do something with this, I came up with a pretty interesting technique. So, yeah, this brings us into bring your own vulnerable enclave round two. So, I tried to remain positive, and instead of thinking about what we can't do, focus about what we can do. So, the vulnerability actually provides two very interesting capabilities. So, first, we can write whatever data that we want into the enclave. So, we can't execute it, but we can load data there. So, this is actually useful because we get this secure vault that we can store whatever information that we want, either shellcode, malware configuration, encryption keys, and EDRs, analysts, things like that, will not be able to touch them. The second interesting advantage, which is a little less straightforward, is that the vulnerability allows us to actually work in reverse. We can trigger it in such a way that allows us to write data into VTL0 from VTL1. So, we can trigger the exploit causing the enclave to write data back into the normal address space of the process. Now, this is useful because, as we mentioned before, there is no visibility into this interaction. So, if they try to monitor write operations to certain memory regions, they will not be able to do so if the write operation is performed by the enclave. So, combining these two capabilities, I came up with a memory evasion technique that I dubbed Mirage. And Mirage is a sleep obfuscation technique. So, the idea with a sleep obfuscation technique is that you create a payload which spends most of its lifetime in a sleeping dormant state. The payload then wakes up occasionally based on a defined trigger, executes its code, and then quickly goes back to sleep again. And the idea is that during sleep we protect our code in a way that will make detection harder. So, for example, we can mark its memory non-executable or we can encrypt it. So, my idea was, why not use this vulnerability to hide our shellcode within isolated user mode during sleep. So, let's understand how this technique will look like. So, first, Mirage will load the vulnerable enclave and exploit it in order to write shellcode into it. Now, at this point, if an EDR takes a look, it will not see anything. So, our normal memory is completely clear, we are completely innocent, all the malicious stuff are in VTL1 where they don't have access. Now, occasionally, we can trigger the vulnerability again, writing our code back into VTL0, again, from the enclave, so an EDR cannot have any trigger for an inspection. We get our shellcode, we run it, and then we override it again with benign data. The cycle can repeat itself, and the idea is that it makes it almost impossible for an EDR to detect us. So, let's look at a demo of this. So, here we loaded Mirage. What it did was it loaded the vulnerable enclave, exploited it to write the shellcode into it, and then allocated a read-write-execute buffer within VTL0. Now, we will now inspect this buffer, but importantly, this is the last time that we are going to interact with this buffer from VTL0. Everything else is going to occur from the context of the enclave. So, now what we did was we triggered the vulnerability again, and as we can see, the shellcode is now in our buffer, written by the enclave. And now, we can jump to the shellcode, execute it, and quickly override it with benign data again. So, that's it, we have our beautiful calculator here, and we're out. Shellcode is nowhere to be found again. So, yeah, this is Mirage. We've released... Thank you. We've released a proof-of-concept code for this. You can find it on Akamai's GitHub. And before I move on, I just want to mention the amazing work by Cedric van Bokhaven and Matteo Malvica of Outflank. It turns out that we worked on almost the same exact research in very similar time. And they published two months ago their own work on the exact same vulnerability. And they were able to actually achieve what I previously described, where they created a full ROP exploit that allowed them to implement a very similar technique to what I just showed you. So, they weren't able to get arbitrary code execution. What they were able to do was to create a sleep obfuscation technique, but their one relied on a ROP chain. So, I really encourage you to check out their work. I think it is more cool than mine. So, yeah, check it out. Okay, so now I hope that I was able to convince you in the last 30 or so minutes that malware could find its way theoretically into an enclave. So, the next question that I wanted to try to answer was what can we actually do while running within an enclave. It turns out that Microsoft really restricts the attack surface within an enclave and they provide them a very, very limited access to APIs. So, we can't perform file operations, we can't do networking, we can't interact with processes, we can't access the registry, the list goes on. So, most traditional malware techniques are not going to be accessible to us. So, what can we do? So, I was able to come up with a few techniques that attackers can still perform even despite all these restrictions. So, the first thing that we can do is we can access the VTL0 memory of our host process. So , the enclave, even though it runs in VTL0, can read and write the normal user mode process. So , this could be interesting in the context where we, for example, we can inject an enclave. So, we create a payload which injects an enclave into a remote process. And from that point, we can reside within VTL1, monitoring and tampering with the activity of the process in a way that can't be detected, or at least easily detected. Let's take a look at another demo. So, we have here our Qt program that receives a string and echoes it back to us. So, we enter test, we receive test. We will now write that VBS is awesome and we will see that VBS is in fact awesome. So, if we get confused and we write that VBS is lame, we will see that it now echoes that VBS is in fact very cool. So, let's understand what is happening here. So, we have a very advanced piece of malware here, as you can see, that runs within an enclave. So, we have an enclave running within the process and what it does is it monitors the buffer that the user supplies their input into and detects the bad string. If it detects it, it replaces it with a good one. So, yeah, a nation state grade malware here. But I wanted to show you this because of the memcpy line specifically. And let's see why it's interesting. So, as we can see, our enclave will perform the memcpy operation. So, if we will continue our demo, we will see that what we do next is we place breakpoints on all instances of memcpy found within the memory address space of the process. And what we would anticipate now is we will see the breakpoint being hit. But if we enter again a string, we will see that even though the memcpy was being performed, the breakpoint is not hit. So, yeah, this happens exactly because of the API evasion that I previously described. And, again, the advantage here is that we can basically interact with the process in a way that cannot be detected. Another cool thing that we can do is we can execute code within VTL0. So, we can't do that. So, we can't directly execute VTL code in VTL1. This is forbidden. If you'll try to take an executable VTL0 address and directly execute it, it will not work. But what we can do is we can remotely invoke a VTL0 code from VTL1. So, this can be performed by using the call enclave API. It turns out that this is a dual use function. So, normally we use it to invoke enclave methods from outside, from the process. But another thing that we can do is invoke VTL0 methods from the enclave. So, essentially we can pass any executable address through this and it will execute the code in VTL0. In terms of evasion, again, it is limited because the code will run in VTL0. But this is a kind of, again, unusual way and we will see in the demo in a minute how it does it. So, here we have our malware here. What it will do is it will allocate a read-write execute buffer in VTL0. And again, like before, we are allocating the buffer but this is the last time that we are going to touch it. So, we have our buffer here. We can see it is empty. And next what we will do is we will pass execution to the enclave. That's it. When we do that, we see that our calculator here popped. Our shellcode executed. If we check the buffer again, we will see that shellcode is there. I left it there for the purpose of this demo but I could have just as well deleted it. And yeah, from the context of an EDL, you see a call enclave function being triggered and then a new thread launching calculator. So , there is no direct connection between the two which can help with evasion. Okay. So, finally, the last little trick that we can do with enclaves that is my personal favorite, has to do with anti-debugging. So, enclaves are in a very advantageous situation where it comes to anti-debugging. In pretty much every case when performing malware analysis, the analysts will use a VTL0 debugger. Either a kernel-mode one or a user-mode one. Now, because of this, the enclave has a lot of power over the debugger. If we can implement an anti-debugging check within an enclave, a debugger will have no way to patch it out and overcome it. So, yeah, this could be a very cool idea. The problem is that it is, again, not going to be that easy. Again, we don't have access to most APIs so the classic things like isDebuggerPresent, getDecount, we can't access files, we can't access processes. So, despite that, I was able to come up with a few techniques that we can perform from the enclave. So, first, we can manually inspect the PEB because we have access to the VTL0 memory. So, if we determine that the process is being debugged through the PEB, we can terminate it from the enclave. Another thing we can do is, while we don't have access to timing-related APIs, we can run inline assembly within the enclave. So, using the RDTSC assembly instruction, we can measure clock cycles of the CPU and, by doing that, we can derive the time and detect breakpoints and other debugging-related activities. So, yeah, we have some options. Now, how can we make this particularly devastating? So, if you think about it, if we simply do a call enclave with an anti-debug check, well, the debugging can simply patch out the call enclave call and then will not even get to the enclave. So, in order to make this particularly devastating, what we can do is we can move critical logic into the enclave. So, preferably something that will be very hard to statically analyze, decryption routine, we can implement, I don't know, a custom VM. You put it within the enclave. And then, from the same call enclave, you also detect the debugger. And now, you create an impossible situation for a defender where, on the one hand, they have to pass execution to the enclave if they want to see what is happening, essentially, and analyze the behavior of it and get the code to run properly. But, as soon as they do that, the enclave can detect the debugger and terminate the process. So , yeah, if implemented correctly, I believe that the only way to overcome this would be to use a hypervisor debugger or a secure kernel debugger, both activities that I highly do not recommend. It's not fun. So, in conclusion, VBS is pretty cool. I think that, again, I think I said it at the start, but it is one of the most significant security advancements that Microsoft has made in recent years. As an ex-red teamer, I can say that it suddenly made my life harder. VBS enclaves, in particular, have a lot of potential. I think that we will continue to see more and more applications using them, and that's a good thing. And, finally, I hope that I was able to convince you that you should also keep an eye on them from an offensive perspective, as we will see more of them in this use as well. That's it. Thank you very much. If you have any questions, I'll be here.