1 00:00:01,340 --> 00:00:05,520 I'm super excited to be here, first time at DEF CON, so welcome to my talk. 2 00:00:05,760 --> 00:00:10,960 I am very tired, I am severely hungover, so I really hope this goes well. 3 00:00:12,200 --> 00:00:16,740 Now, before we get into the talk itself, I wanted to start with a short apology. 4 00:00:17,140 --> 00:00:32,530 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, 5 00:00:32,530 --> 00:00:34,190 virtualization-based security. 6 00:00:34,770 --> 00:00:38,710 So yeah, sorry for all the VBScript fans, anyone is leaving? 7 00:00:38,890 --> 00:00:39,410 No? 8 00:00:39,410 --> 00:00:40,110 Okay. 9 00:00:40,370 --> 00:00:42,450 So, yeah, I guess we can start. 10 00:00:42,770 --> 00:00:46,610 My name is Uri David, I'm a security researcher at Akamai. 11 00:00:46,630 --> 00:00:53,290 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. 12 00:00:53,290 --> 00:01:00,210 And for the last few months I really focused on the area of virtualization-based security. 13 00:01:01,030 --> 00:01:08,630 I believe this is one of the most significant and interesting advancements in security that Microsoft have implemented in recent years. 14 00:01:08,630 --> 00:01:14,870 So we will start by briefly covering it, understand what it does, how it does it, and why it does it. 15 00:01:15,430 --> 00:01:25,690 And then we'll get to VBS enclaves, a feature which is enabled through VBS, and have a lot of very cool security applications. 16 00:01:26,510 --> 00:01:29,570 And then we'll get to the fun part, abusing VBS enclaves. 17 00:01:29,570 --> 00:01:35,610 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. 18 00:01:36,510 --> 00:01:38,330 Okay, so let's start with VBS. 19 00:01:38,390 --> 00:01:43,510 And to talk about VBS we first need to talk about boundaries, security boundaries. 20 00:01:43,510 --> 00:01:53,070 So traditionally in order to protect the operating system, Microsoft and other operating systems as well, relied on processor rings. 21 00:01:53,070 --> 00:02:04,710 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. 22 00:02:05,010 --> 00:02:07,750 Well, this really proved to be insufficient over time. 23 00:02:08,270 --> 00:02:21,430 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. 24 00:02:21,870 --> 00:02:24,670 And yeah, this really again proved to be insufficient. 25 00:02:24,670 --> 00:02:29,010 So we needed an additional security boundary into the OS. 26 00:02:29,330 --> 00:02:32,050 And this is where VBS comes in. 27 00:02:32,150 --> 00:02:35,490 So VBS came to our life through Hyper-V. 28 00:02:35,490 --> 00:02:36,850 So let's start with that. 29 00:02:36,850 --> 00:02:42,210 Hyper-V, Microsoft's hypervisor, traditionally used to run VMs, either locally or in the cloud. 30 00:02:42,870 --> 00:02:49,830 Now, one of the most important things that Hyper-V has to manage, and any good hypervisor for that, is isolation. 31 00:02:49,910 --> 00:03:00,910 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. 32 00:03:01,010 --> 00:03:06,150 So Hyper-V already comes with a very strong memory isolation capability. 33 00:03:06,150 --> 00:03:08,690 I will not go too deep into how it does it. 34 00:03:08,690 --> 00:03:11,070 There are plenty of good resources online. 35 00:03:11,730 --> 00:03:18,910 But yeah, so we have this very strong ability to restrict one VM from accessing to the memory of another one. 36 00:03:19,390 --> 00:03:25,150 Now, someone at Microsoft, at one point, probably had a good idea. 37 00:03:25,250 --> 00:03:31,010 Let's take this same primitive, isolation primitive, and apply it to the operating system itself. 38 00:03:31,010 --> 00:03:42,250 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. 39 00:03:43,090 --> 00:03:46,110 So this is the root of VBS. 40 00:03:46,110 --> 00:03:51,970 And it turns out that this isolation actually enables us to add additional security boundaries into the OS. 41 00:03:52,010 --> 00:03:55,810 So these are called virtual trust levels, or VTLs. 42 00:03:55,910 --> 00:03:58,210 And here we have what we had before. 43 00:03:58,210 --> 00:04:00,190 So we had two execution modes. 44 00:04:00,190 --> 00:04:03,770 We had user mode, ring three, and kernel mode, ring zero. 45 00:04:03,770 --> 00:04:08,350 Now, when we add VTLs into the mix, things get interesting. 46 00:04:08,550 --> 00:04:10,630 So first we have VTL zero. 47 00:04:10,810 --> 00:04:15,250 This is the least privileged VTL, which is also called normal world. 48 00:04:15,250 --> 00:04:16,730 And you already know it. 49 00:04:16,730 --> 00:04:19,710 So VTL zero contains the normal operating system. 50 00:04:19,710 --> 00:04:23,150 We have their user mode and normal kernel mode. 51 00:04:23,310 --> 00:04:26,990 Things get a bit more interesting when we introduce VTL one. 52 00:04:27,050 --> 00:04:30,370 Now, VTL one is more privileged than VTL zero. 53 00:04:30,370 --> 00:04:34,530 And this privilege comes in the form of memory isolation. 54 00:04:34,790 --> 00:04:40,590 Code running within VTL zero cannot access code or the memory of VTL one. 55 00:04:40,590 --> 00:04:44,170 On the other hand, VTL one can access the memory of VTL zero. 56 00:04:44,170 --> 00:04:48,590 So this is how this privilege comes into effect. 57 00:04:48,790 --> 00:04:52,170 So within VTL one, we have two new execution modes. 58 00:04:52,170 --> 00:04:55,770 We have VTL one, ring zero, for the secure kernel. 59 00:04:55,770 --> 00:05:03,170 This is a more privileged kernel that can actually be used to apply and enforce additional policies to the normal kernel. 60 00:05:03,870 --> 00:05:09,330 And we have isolated user mode, or IOM, which is VTL one, ring three. 61 00:05:09,330 --> 00:05:12,530 Now, on one hand, we are running in ring three. 62 00:05:12,530 --> 00:05:15,530 So we can't perform sensitive kernel operations. 63 00:05:15,530 --> 00:05:19,930 But we still enjoy the advantage of running within VTL one. 64 00:05:19,930 --> 00:05:22,010 Isolation, as the name suggests. 65 00:05:22,050 --> 00:05:28,810 And the thing is that IOM is going to be protected from threat actors that compromise the normal operating system. 66 00:05:28,810 --> 00:05:37,010 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. 67 00:05:37,990 --> 00:05:43,130 Okay, so these are VTLs and the new execution modes that they introduce. 68 00:05:43,270 --> 00:05:48,830 And now we can move on to our next topic, or our main topic, VBS enclaves. 69 00:05:49,450 --> 00:05:54,630 So, an enclave is an isolated region of memory within a process. 70 00:05:54,630 --> 00:05:58,010 The process cannot write into the enclave, it cannot read from the enclave. 71 00:05:58,010 --> 00:06:02,790 The only thing that it can do, is it can invoke methods that are exported by the enclave. 72 00:06:03,570 --> 00:06:06,650 So, why something like that is useful? 73 00:06:06,650 --> 00:06:08,670 Let's consider the following scenario. 74 00:06:08,670 --> 00:06:10,910 Our process handles some sort of secret. 75 00:06:10,910 --> 00:06:14,810 So , either something like an API key, a password. 76 00:06:15,370 --> 00:06:18,850 Now, let's say that the process were to be compromised. 77 00:06:18,850 --> 00:06:23,410 Either directly, or let's say the operating system itself was compromised. 78 00:06:23,530 --> 00:06:31,410 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. 79 00:06:31,750 --> 00:06:41,070 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. 80 00:06:41,150 --> 00:06:46,450 Now, if an attacker compromises the process, they will never be able to exfiltrate the secret. 81 00:06:46,510 --> 00:06:52,370 In order to actually utilize it, what the process can do, is it can invoke the methods that are exported by the enclave. 82 00:06:52,370 --> 00:07:02,310 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. 83 00:07:02,310 --> 00:07:05,430 So, we pass the value and the enclave encrypts it. 84 00:07:05,650 --> 00:07:09,130 So, this is the idea of enclaves. 85 00:07:09,130 --> 00:07:14,770 There are additional usages for them as well, but I think this is a good start for that. 86 00:07:16,110 --> 00:07:18,590 And enclaves are a general concept. 87 00:07:19,410 --> 00:07:23,770 VBS enclaves are Microsoft's implementation of them using VBS. 88 00:07:23,770 --> 00:07:27,870 It is actually a very elegant implementation. 89 00:07:27,870 --> 00:07:40,510 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. 90 00:07:40,970 --> 00:07:47,430 By doing that, we ensure, again, that nothing in VTL0 can access the data within the enclave. 91 00:07:48,050 --> 00:07:55,290 Okay, so let's discuss a few more details about enclaves and how we use them in Windows. 92 00:07:55,510 --> 00:08:01,130 So, any process can create an enclave and load what is called an enclave module into it. 93 00:08:01,130 --> 00:08:10,490 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. 94 00:08:10,750 --> 00:08:14,510 And once we load the DLL, again, we can't access it directly. 95 00:08:14,510 --> 00:08:19,790 We can invoke its exported methods by calling the call enclave API. 96 00:08:20,730 --> 00:08:28,470 And one final note, before we move on, is that we can't just load any arbitrary DLL into an enclave. 97 00:08:28,470 --> 00:08:35,910 Microsoft realizes that running within an enclave should be something that is restricted. 98 00:08:35,910 --> 00:08:47,450 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. 99 00:08:47,530 --> 00:08:52,910 So, this is going to be the main challenge for us for the rest of the talk. 100 00:08:53,130 --> 00:08:58,070 So, now that you understand what VBS enclaves are, let's get to the fun part. 101 00:08:58,210 --> 00:08:59,730 How we can abuse them. 102 00:09:00,290 --> 00:09:07,550 So, the first thing that came, at least into my mind when considering this, was the idea of running malware within an enclave. 103 00:09:07,550 --> 00:09:14,390 So, the same advantages that are provided to normal applications could apply to attackers as well. 104 00:09:14,650 --> 00:09:17,850 So, first, obviously, we will have inaccessible memory. 105 00:09:17,910 --> 00:09:25,650 EDRs and security analysts, whatever, they won't be able to scan our memory in order to identify malicious behavior. 106 00:09:26,470 --> 00:09:30,490 Another advantage has to do with behavior, not just scanning memory. 107 00:09:30,490 --> 00:09:38,610 Well, it turns out that it is much harder, or pretty much impossible, to trace API calls that are performed from an enclave. 108 00:09:38,610 --> 00:09:40,130 So, let's understand this. 109 00:09:40,390 --> 00:09:47,570 When a normal process triggers a Windows API, we go through a process that I believe most of you probably know. 110 00:09:47,570 --> 00:09:51,410 We go through a series of system DLLs, eventually anti-DLL. 111 00:09:51,410 --> 00:09:54,450 From there, we invoke a syscall into the normal kernel. 112 00:09:54,670 --> 00:09:59,450 Now, the thing about this flow is that EDRs have a lot of visibility into it. 113 00:09:59,450 --> 00:10:01,750 They can hook our calls into DLLs. 114 00:10:01,750 --> 00:10:03,790 They can monitor our activity in the kernel. 115 00:10:04,010 --> 00:10:06,450 There are a lot of detection opportunities here. 116 00:10:06,510 --> 00:10:19,010 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. 117 00:10:19,010 --> 00:10:24,810 So, instead of going through anti-DLL, we go through VEL DLL, VBS Enclave Runtime DLL. 118 00:10:24,810 --> 00:10:30,430 And from there, when we invoke a syscall, it is managed by the secure kernel. 119 00:10:30,570 --> 00:10:42,610 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. 120 00:10:42,610 --> 00:10:44,950 So, yeah, another advantage that we gain. 121 00:10:45,690 --> 00:10:58,990 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. 122 00:10:58,990 --> 00:11:06,950 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. 123 00:11:07,010 --> 00:11:15,490 So, we'll start with the straightforward ones, I guess, that I did want to mention before going to the next ones. 124 00:11:15,710 --> 00:11:18,450 So, attackers could obtain a legitimate signature. 125 00:11:18,610 --> 00:11:31,230 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. 126 00:11:31,570 --> 00:11:37,950 So, they could either compromise an authorized signer or obtain signing privileges legitimately through a shell company. 127 00:11:37,950 --> 00:11:39,410 So, this is one option. 128 00:11:39,970 --> 00:11:49,310 Another option would be to exploit a vulnerability in the Windows loader that is in charge of validating the enclave signature. 129 00:11:49,310 --> 00:12:02,150 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. 130 00:12:02,150 --> 00:12:15,750 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. 131 00:12:15,750 --> 00:12:24,390 Now, these two approaches are certainly valid, and I believe that advanced threat actors, nation-state threat actors, could use them. 132 00:12:24,390 --> 00:12:25,710 But I don't know what about you. 133 00:12:25,710 --> 00:12:28,310 I'm personally not a nation-state threat actor. 134 00:12:28,310 --> 00:12:34,490 So, I wanted to find something that might be more accessible to, well, less sophisticated threat actors. 135 00:12:35,170 --> 00:12:40,370 And I ended up finding two additional paths to get code running within an enclave. 136 00:12:40,370 --> 00:12:45,170 The first one is the abuse of debuggable enclave modules. 137 00:12:45,170 --> 00:12:53,890 So, it turns out that if you want to debug an enclave during development, it's actually not a straightforward task. 138 00:12:54,070 --> 00:12:54,910 Think about it. 139 00:12:54,910 --> 00:12:57,130 Enclaves run within VTL1. 140 00:12:58,410 --> 00:13:01,070 Our debugger runs in VTL0. 141 00:13:01,130 --> 00:13:05,350 If we simply try to debug an enclave, we won't be able to do that, as we can see here. 142 00:13:05,350 --> 00:13:06,490 You can't access the memory. 143 00:13:06,490 --> 00:13:07,950 You can't place breakpoints. 144 00:13:08,030 --> 00:13:13,050 So, yeah, Microsoft, I believe, pretty early realized this issue. 145 00:13:13,050 --> 00:13:16,910 And because of this, they implemented debuggable enclaves. 146 00:13:17,230 --> 00:13:22,970 So, when you compile an enclave, you can specify a flag that will mark it as debuggable. 147 00:13:23,070 --> 00:13:26,950 And when you do that, well, like magic, you can now debug the enclave. 148 00:13:26,950 --> 00:13:29,650 As we can see here, we can now access the memory of the enclave. 149 00:13:29,650 --> 00:13:30,950 We can place breakpoints. 150 00:13:31,070 --> 00:13:32,510 Yeah, we can have a good time. 151 00:13:33,070 --> 00:13:36,870 Now, when I initially saw this, I was kind of confused and surprised. 152 00:13:36,870 --> 00:13:42,750 Because we know that enclaves reside in VTL1, and we are accessing their memory. 153 00:13:42,750 --> 00:13:44,070 So, it's kind of weird. 154 00:13:44,390 --> 00:13:50,590 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. 155 00:13:50,610 --> 00:13:54,890 But upon digging into this, I realized that this is not actually the case. 156 00:13:54,890 --> 00:13:58,590 Debuggable enclaves still reside in isolated user mode. 157 00:13:58,590 --> 00:14:05,150 And to enable the debugging that we have just witnessed, the secure kernel actually implements some exceptions. 158 00:14:05,590 --> 00:14:16,290 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. 159 00:14:16,290 --> 00:14:18,430 And if it is, it will allow us to access it. 160 00:14:18,770 --> 00:14:25,590 Similarly, we have the debug protect virtual memory, which will allow us to change memory permissions on debuggable enclaves. 161 00:14:26,570 --> 00:14:34,270 So, now that I understood more about these special enclaves, I wanted to see how we can abuse them. 162 00:14:34,270 --> 00:14:41,370 So, first, and pretty obviously, it is not a good idea to use a debuggable enclave for a production application. 163 00:14:41,370 --> 00:14:44,810 Microsoft clearly acknowledges this in their documentation. 164 00:14:44,810 --> 00:14:48,510 Because if you're using a debuggable enclave, you're essentially not really using an enclave. 165 00:14:48,510 --> 00:14:50,010 Anyone can access its memory. 166 00:14:50,010 --> 00:14:52,550 You might as well just allocate a normal memory buffer. 167 00:14:53,650 --> 00:14:55,190 But, yeah, so this is a risk. 168 00:14:55,190 --> 00:14:56,190 It is acknowledged. 169 00:14:56,570 --> 00:15:01,290 But it turns out that there's another interesting way that attackers can actually abuse this. 170 00:15:01,290 --> 00:15:07,630 So, let's say that an attacker obtains a signed debuggable enclave. 171 00:15:07,630 --> 00:15:11,350 So, a DLL, which is signed and marked as debuggable. 172 00:15:12,230 --> 00:15:18,070 It turns out that enclave modules are not paired to a specific process. 173 00:15:18,070 --> 00:15:21,410 Meaning that any process can load any module. 174 00:15:21,410 --> 00:15:27,090 So, what an attacker can do is they can load this debuggable enclave into their own process. 175 00:15:27,090 --> 00:15:37,750 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. 176 00:15:37,750 --> 00:15:40,750 So, let's look at this process, how it would look like. 177 00:15:40,910 --> 00:15:47,210 So, here we have an attacker who obtained this type of debuggable enclave and loaded it. 178 00:15:47,210 --> 00:15:50,810 And we can see that the enclave contains an enclave routine in green. 179 00:15:50,810 --> 00:15:53,350 A function which is exported by the enclave. 180 00:15:53,350 --> 00:15:57,190 What the attacker can do is they can obtain the address of this routine. 181 00:15:57,570 --> 00:16:02,790 They can then change their memory permission to allow a page execute read write. 182 00:16:02,790 --> 00:16:05,450 This is possible because the enclave is debuggable. 183 00:16:06,070 --> 00:16:13,110 They can then overwrite the routine and invoke the shellcode by using call enclave. 184 00:16:13,110 --> 00:16:17,990 So, we can't just run arbitrary addresses within the enclave. 185 00:16:17,990 --> 00:16:24,290 But, because this is an exported function that we overwritten, we can trigger it normally by using call enclave. 186 00:16:24,650 --> 00:16:25,250 So, yeah. 187 00:16:25,250 --> 00:16:26,790 This is a cool technique. 188 00:16:26,790 --> 00:16:27,770 It works. 189 00:16:27,790 --> 00:16:30,610 But, there are some caveats to it. 190 00:16:30,610 --> 00:16:31,890 So, let's cover them. 191 00:16:32,450 --> 00:16:37,110 The first one is that the memory access actually goes both ways. 192 00:16:37,110 --> 00:16:43,110 So, just like we can access the memory of the enclave, EDRs can do the same thing. 193 00:16:43,110 --> 00:16:46,330 Because, as we just saw, the exceptions apply to them as well. 194 00:16:47,190 --> 00:16:50,730 Despite that, there are still advantages to using this technique. 195 00:16:50,730 --> 00:16:58,530 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. 196 00:16:59,510 --> 00:17:00,410 So, yeah. 197 00:17:00,410 --> 00:17:05,490 EDRs might not consider this scenario, even though they can theoretically detect it. 198 00:17:06,570 --> 00:17:15,330 And the second advantage is that enclaves, even when they are debuggable, will still use the API call chain in VTL1. 199 00:17:15,330 --> 00:17:18,150 So, hooking their APIs will not be possible. 200 00:17:19,290 --> 00:17:25,510 The second unfortunate catch is that, at the moment, I haven't actually managed to find one. 201 00:17:25,570 --> 00:17:35,730 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. 202 00:17:35,730 --> 00:17:38,930 But, at the moment, I was yet to be successful. 203 00:17:39,150 --> 00:17:52,110 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. 204 00:17:52,110 --> 00:17:57,570 So, the thing about enclaves today is that it is kind of a not really adopted technology. 205 00:17:57,570 --> 00:17:58,890 Not a lot of people use it. 206 00:17:58,890 --> 00:18:05,010 But Microsoft really began pushing towards this direction, implementing a lot of things using enclaves themselves. 207 00:18:05,050 --> 00:18:13,210 And I believe that we will begin to see more third-party drivers, developers utilize them in the future. 208 00:18:13,210 --> 00:18:14,970 And we all know developers. 209 00:18:15,150 --> 00:18:16,610 Developers fuck up. 210 00:18:16,610 --> 00:18:25,390 And one file will find its way into VirusTotal, or somehow into the internet, and this technique now becomes available. 211 00:18:25,390 --> 00:18:28,110 So, yeah, I think it is worth considering. 212 00:18:29,130 --> 00:18:32,590 Okay, so this was the first idea, abusing debuggable enclaves. 213 00:18:32,590 --> 00:18:37,570 And we'll now move on to the second idea, exploiting vulnerable enclave modules. 214 00:18:37,710 --> 00:18:45,730 So, the idea is not mine, let's say, because I kind of applied a very well-known technique. 215 00:18:46,130 --> 00:18:57,630 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. 216 00:18:57,730 --> 00:19:09,010 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. 217 00:19:09,010 --> 00:19:13,490 Well, what I thought was, why not apply the same idea to enclaves? 218 00:19:13,490 --> 00:19:19,890 If we manage to find a vulnerable enclave, we can load it, exploit it, and get our code running within IOM. 219 00:19:20,370 --> 00:19:27,070 So, luckily, such a vulnerability exists, CVE-2023-36880. 220 00:19:27,570 --> 00:19:33,130 This is an information disclosure vulnerability in an enclave module used by Microsoft Edge. 221 00:19:34,050 --> 00:19:44,750 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. 222 00:19:45,150 --> 00:19:47,910 So, I decided to dig into this a bit more. 223 00:19:48,870 --> 00:19:56,450 This vulnerability was discovered by Alex Go from the Chrome security team, who also shared a POC for exploiting it. 224 00:19:56,750 --> 00:20:03,570 It is actually a relatively simple vulnerability, and it allows an arbitrary read-write into the enclave. 225 00:20:03,570 --> 00:20:14,490 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. 226 00:20:15,690 --> 00:20:28,570 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. 227 00:20:28,570 --> 00:20:40,770 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. 228 00:20:41,010 --> 00:20:49,890 Well, it wasn't going to be that easy, as I quickly discovered, because enclaves are protected with ACG, arbitrary code guard, by default. 229 00:20:49,890 --> 00:21:03,910 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. 230 00:21:03,910 --> 00:21:11,410 In practice, it means we can't allocate new executable pages, and we can't turn existing executable pages writable. 231 00:21:11,490 --> 00:21:30,510 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. 232 00:21:31,130 --> 00:21:45,690 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. 233 00:21:46,110 --> 00:21:54,230 So, yeah, at this point, I was kind of stuck, and you might say it got into my personal life as well. 234 00:21:54,570 --> 00:21:56,810 I knew that I had something here. 235 00:21:56,810 --> 00:22:03,270 So, the ability to write and read whatever I want into the enclave felt just too cool to be not useful. 236 00:22:03,730 --> 00:22:11,930 So, yeah, after some sleepless nights and many days trying to do something with this, I came up with a pretty interesting technique. 237 00:22:12,630 --> 00:22:16,190 So, yeah, this brings us into bring your own vulnerable enclave round two. 238 00:22:17,470 --> 00:22:24,850 So, I tried to remain positive, and instead of thinking about what we can't do, focus about what we can do. 239 00:22:24,850 --> 00:22:30,330 So, the vulnerability actually provides two very interesting capabilities. 240 00:22:30,510 --> 00:22:35,850 So, first, we can write whatever data that we want into the enclave. 241 00:22:35,850 --> 00:22:39,550 So, we can't execute it, but we can load data there. 242 00:22:39,650 --> 00:22:55,070 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. 243 00:22:55,890 --> 00:23:04,330 The second interesting advantage, which is a little less straightforward, is that the vulnerability allows us to actually work in reverse. 244 00:23:04,330 --> 00:23:11,090 We can trigger it in such a way that allows us to write data into VTL0 from VTL1. 245 00:23:11,090 --> 00:23:18,270 So, we can trigger the exploit causing the enclave to write data back into the normal address space of the process. 246 00:23:18,630 --> 00:23:25,990 Now, this is useful because, as we mentioned before, there is no visibility into this interaction. 247 00:23:25,990 --> 00:23:35,710 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. 248 00:23:35,990 --> 00:23:44,270 So, combining these two capabilities, I came up with a memory evasion technique that I dubbed Mirage. 249 00:23:44,510 --> 00:23:47,310 And Mirage is a sleep obfuscation technique. 250 00:23:47,310 --> 00:23:56,670 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. 251 00:23:56,870 --> 00:24:04,410 The payload then wakes up occasionally based on a defined trigger, executes its code, and then quickly goes back to sleep again. 252 00:24:04,410 --> 00:24:10,270 And the idea is that during sleep we protect our code in a way that will make detection harder. 253 00:24:10,270 --> 00:24:15,190 So, for example, we can mark its memory non-executable or we can encrypt it. 254 00:24:15,410 --> 00:24:23,610 So, my idea was, why not use this vulnerability to hide our shellcode within isolated user mode during sleep. 255 00:24:23,910 --> 00:24:26,850 So, let's understand how this technique will look like. 256 00:24:26,870 --> 00:24:34,730 So, first, Mirage will load the vulnerable enclave and exploit it in order to write shellcode into it. 257 00:24:35,270 --> 00:24:40,430 Now, at this point, if an EDR takes a look, it will not see anything. 258 00:24:40,430 --> 00:24:47,890 So, our normal memory is completely clear, we are completely innocent, all the malicious stuff are in VTL1 where they don't have access. 259 00:24:47,890 --> 00:25:00,390 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. 260 00:25:01,270 --> 00:25:06,250 We get our shellcode, we run it, and then we override it again with benign data. 261 00:25:06,490 --> 00:25:12,310 The cycle can repeat itself, and the idea is that it makes it almost impossible for an EDR to detect us. 262 00:25:12,990 --> 00:25:15,050 So, let's look at a demo of this. 263 00:25:21,670 --> 00:25:25,390 So, here we loaded Mirage. 264 00:25:25,390 --> 00:25:36,710 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. 265 00:25:37,550 --> 00:25:44,650 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. 266 00:25:44,650 --> 00:25:47,690 Everything else is going to occur from the context of the enclave. 267 00:25:47,690 --> 00:25:57,330 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. 268 00:25:57,830 --> 00:26:04,750 And now, we can jump to the shellcode, execute it, and quickly override it with benign data again. 269 00:26:04,750 --> 00:26:08,330 So, that's it, we have our beautiful calculator here, and we're out. 270 00:26:08,330 --> 00:26:10,250 Shellcode is nowhere to be found again. 271 00:26:11,190 --> 00:26:12,950 So, yeah, this is Mirage. 272 00:26:13,010 --> 00:26:13,690 We've released... 273 00:26:14,450 --> 00:26:15,590 Thank you. 274 00:26:20,460 --> 00:26:23,860 We've released a proof-of-concept code for this. 275 00:26:23,860 --> 00:26:26,120 You can find it on Akamai's GitHub. 276 00:26:26,940 --> 00:26:34,620 And before I move on, I just want to mention the amazing work by Cedric van Bokhaven and Matteo Malvica of Outflank. 277 00:26:34,620 --> 00:26:41,740 It turns out that we worked on almost the same exact research in very similar time. 278 00:26:41,740 --> 00:26:46,520 And they published two months ago their own work on the exact same vulnerability. 279 00:26:46,520 --> 00:26:59,820 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. 280 00:26:59,820 --> 00:27:02,520 So, they weren't able to get arbitrary code execution. 281 00:27:02,520 --> 00:27:08,300 What they were able to do was to create a sleep obfuscation technique, but their one relied on a ROP chain. 282 00:27:08,300 --> 00:27:10,260 So, I really encourage you to check out their work. 283 00:27:10,260 --> 00:27:11,980 I think it is more cool than mine. 284 00:27:12,240 --> 00:27:13,520 So, yeah, check it out. 285 00:27:13,520 --> 00:27:26,500 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. 286 00:27:26,500 --> 00:27:33,640 So, the next question that I wanted to try to answer was what can we actually do while running within an enclave. 287 00:27:34,420 --> 00:27:44,560 It turns out that Microsoft really restricts the attack surface within an enclave and they provide them a very, very limited access to APIs. 288 00:27:44,780 --> 00:27:54,180 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. 289 00:27:54,180 --> 00:27:59,460 So, most traditional malware techniques are not going to be accessible to us. 290 00:28:00,460 --> 00:28:01,800 So, what can we do? 291 00:28:03,000 --> 00:28:11,620 So, I was able to come up with a few techniques that attackers can still perform even despite all these restrictions. 292 00:28:11,700 --> 00:28:17,200 So, the first thing that we can do is we can access the VTL0 memory of our host process. 293 00:28:17,400 --> 00:28:24,300 So , the enclave, even though it runs in VTL0, can read and write the normal user mode process. 294 00:28:24,680 --> 00:28:30,920 So , this could be interesting in the context where we, for example, we can inject an enclave. 295 00:28:30,920 --> 00:28:35,260 So, we create a payload which injects an enclave into a remote process. 296 00:28:35,260 --> 00:28:45,520 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. 297 00:28:48,770 --> 00:28:50,570 Let's take a look at another demo. 298 00:28:51,070 --> 00:28:57,410 So, we have here our Qt program that receives a string and echoes it back to us. 299 00:28:57,410 --> 00:28:59,330 So, we enter test, we receive test. 300 00:28:59,530 --> 00:29:04,270 We will now write that VBS is awesome and we will see that VBS is in fact awesome. 301 00:29:04,590 --> 00:29:12,010 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. 302 00:29:12,330 --> 00:29:15,010 So, let's understand what is happening here. 303 00:29:21,170 --> 00:29:28,390 So, we have a very advanced piece of malware here, as you can see, that runs within an enclave. 304 00:29:28,390 --> 00:29:40,830 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. 305 00:29:40,830 --> 00:29:43,210 If it detects it, it replaces it with a good one. 306 00:29:43,290 --> 00:29:46,590 So, yeah, a nation state grade malware here. 307 00:29:47,250 --> 00:29:51,330 But I wanted to show you this because of the memcpy line specifically. 308 00:29:51,530 --> 00:29:53,430 And let's see why it's interesting. 309 00:29:53,430 --> 00:29:57,530 So, as we can see, our enclave will perform the memcpy operation. 310 00:29:57,970 --> 00:30:08,830 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. 311 00:30:09,850 --> 00:30:14,530 And what we would anticipate now is we will see the breakpoint being hit. 312 00:30:14,630 --> 00:30:22,750 But if we enter again a string, we will see that even though the memcpy was being performed, the breakpoint is not hit. 313 00:30:22,910 --> 00:30:29,350 So, yeah, this happens exactly because of the API evasion that I previously described. 314 00:30:29,570 --> 00:30:36,470 And, again, the advantage here is that we can basically interact with the process in a way that cannot be detected. 315 00:30:37,210 --> 00:30:42,150 Another cool thing that we can do is we can execute code within VTL0. 316 00:30:42,150 --> 00:30:44,990 So, we can't do that. 317 00:30:44,990 --> 00:30:48,410 So, we can't directly execute VTL code in VTL1. 318 00:30:48,410 --> 00:30:49,510 This is forbidden. 319 00:30:49,510 --> 00:30:54,690 If you'll try to take an executable VTL0 address and directly execute it, it will not work. 320 00:30:54,990 --> 00:31:02,830 But what we can do is we can remotely invoke a VTL0 code from VTL1. 321 00:31:02,830 --> 00:31:06,010 So, this can be performed by using the call enclave API. 322 00:31:06,010 --> 00:31:09,310 It turns out that this is a dual use function. 323 00:31:09,670 --> 00:31:15,630 So, normally we use it to invoke enclave methods from outside, from the process. 324 00:31:15,630 --> 00:31:20,270 But another thing that we can do is invoke VTL0 methods from the enclave. 325 00:31:20,270 --> 00:31:26,950 So, essentially we can pass any executable address through this and it will execute the code in VTL0. 326 00:31:27,550 --> 00:31:35,150 In terms of evasion, again, it is limited because the code will run in VTL0. 327 00:31:35,150 --> 00:31:41,590 But this is a kind of, again, unusual way and we will see in the demo in a minute how it does it. 328 00:31:47,240 --> 00:31:51,260 So, here we have our malware here. 329 00:31:51,560 --> 00:31:56,180 What it will do is it will allocate a read-write execute buffer in VTL0. 330 00:31:56,300 --> 00:32:01,000 And again, like before, we are allocating the buffer but this is the last time that we are going to touch it. 331 00:32:01,220 --> 00:32:03,300 So, we have our buffer here. 332 00:32:03,300 --> 00:32:04,460 We can see it is empty. 333 00:32:05,800 --> 00:32:09,540 And next what we will do is we will pass execution to the enclave. 334 00:32:09,540 --> 00:32:10,600 That's it. 335 00:32:12,460 --> 00:32:15,940 When we do that, we see that our calculator here popped. 336 00:32:15,940 --> 00:32:17,240 Our shellcode executed. 337 00:32:17,240 --> 00:32:20,300 If we check the buffer again, we will see that shellcode is there. 338 00:32:20,300 --> 00:32:25,420 I left it there for the purpose of this demo but I could have just as well deleted it. 339 00:32:25,580 --> 00:32:33,860 And yeah, from the context of an EDL, you see a call enclave function being triggered and then a new thread launching calculator. 340 00:32:34,020 --> 00:32:40,200 So , there is no direct connection between the two which can help with evasion. 341 00:32:43,150 --> 00:32:44,130 Okay. 342 00:32:46,330 --> 00:32:54,450 So, finally, the last little trick that we can do with enclaves that is my personal favorite, has to do with anti-debugging. 343 00:32:54,450 --> 00:33:00,570 So, enclaves are in a very advantageous situation where it comes to anti-debugging. 344 00:33:00,570 --> 00:33:08,090 In pretty much every case when performing malware analysis, the analysts will use a VTL0 debugger. 345 00:33:08,090 --> 00:33:10,630 Either a kernel-mode one or a user-mode one. 346 00:33:10,750 --> 00:33:15,310 Now, because of this, the enclave has a lot of power over the debugger. 347 00:33:15,430 --> 00:33:24,350 If we can implement an anti-debugging check within an enclave, a debugger will have no way to patch it out and overcome it. 348 00:33:24,350 --> 00:33:26,650 So, yeah, this could be a very cool idea. 349 00:33:27,190 --> 00:33:30,790 The problem is that it is, again, not going to be that easy. 350 00:33:30,790 --> 00:33:39,290 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. 351 00:33:39,770 --> 00:33:45,390 So, despite that, I was able to come up with a few techniques that we can perform from the enclave. 352 00:33:45,670 --> 00:33:52,070 So, first, we can manually inspect the PEB because we have access to the VTL0 memory. 353 00:33:52,250 --> 00:33:57,690 So, if we determine that the process is being debugged through the PEB, we can terminate it from the enclave. 354 00:33:58,930 --> 00:34:06,130 Another thing we can do is, while we don't have access to timing-related APIs, we can run inline assembly within the enclave. 355 00:34:06,130 --> 00:34:19,890 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. 356 00:34:20,030 --> 00:34:21,710 So, yeah, we have some options. 357 00:34:22,610 --> 00:34:26,130 Now, how can we make this particularly devastating? 358 00:34:26,310 --> 00:34:37,790 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. 359 00:34:37,790 --> 00:34:44,810 So, in order to make this particularly devastating, what we can do is we can move critical logic into the enclave. 360 00:34:44,810 --> 00:34:51,870 So, preferably something that will be very hard to statically analyze, decryption routine, we can implement, I don't know, a custom VM. 361 00:34:51,950 --> 00:34:53,590 You put it within the enclave. 362 00:34:54,610 --> 00:34:58,850 And then, from the same call enclave, you also detect the debugger. 363 00:34:58,870 --> 00:35:13,370 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. 364 00:35:13,370 --> 00:35:19,470 But, as soon as they do that, the enclave can detect the debugger and terminate the process. 365 00:35:19,770 --> 00:35:31,170 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. 366 00:35:31,170 --> 00:35:32,690 It's not fun. 367 00:35:33,810 --> 00:35:38,110 So, in conclusion, VBS is pretty cool. 368 00:35:38,170 --> 00:35:47,990 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. 369 00:35:47,990 --> 00:35:52,630 As an ex-red teamer, I can say that it suddenly made my life harder. 370 00:35:53,890 --> 00:35:57,790 VBS enclaves, in particular, have a lot of potential. 371 00:35:57,790 --> 00:36:03,710 I think that we will continue to see more and more applications using them, and that's a good thing. 372 00:36:04,210 --> 00:36:17,430 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. 373 00:36:18,190 --> 00:36:18,970 That's it. 374 00:36:18,970 --> 00:36:20,030 Thank you very much. 375 00:36:24,320 --> 00:36:26,340 If you have any questions, I'll be here.