WEBVTT

00:01.340 --> 00:05.520
I'm super excited to be here, first time at DEF CON, so welcome to my talk.

00:05.760 --> 00:10.960
I am very tired, I am severely hungover, so I really hope this goes well.

00:12.200 --> 00:16.740
Now, before we get into the talk itself, I wanted to start with a short apology.

00:17.140 --> 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,

00:32.530 --> 00:34.190
virtualization-based security.

00:34.770 --> 00:38.710
So yeah, sorry for all the VBScript fans, anyone is leaving?

00:38.890 --> 00:39.410
No?

00:39.410 --> 00:40.110
Okay.

00:40.370 --> 00:42.450
So, yeah, I guess we can start.

00:42.770 --> 00:46.610
My name is Uri David, I'm a security researcher at Akamai.

00:46.630 --> 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.

00:53.290 --> 01:00.210
And for the last few months I really focused on the area of virtualization-based security.

01:01.030 --> 01:08.630
I believe this is one of the most significant and interesting advancements in security that Microsoft have implemented in recent years.

01:08.630 --> 01:14.870
So we will start by briefly covering it, understand what it does, how it does it, and why it does it.

01:15.430 --> 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.

01:26.510 --> 01:29.570
And then we'll get to the fun part, abusing VBS enclaves.

01:29.570 --> 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.

01:36.510 --> 01:38.330
Okay, so let's start with VBS.

01:38.390 --> 01:43.510
And to talk about VBS we first need to talk about boundaries, security boundaries.

01:43.510 --> 01:53.070
So traditionally in order to protect the operating system, Microsoft and other operating systems as well, relied on processor rings.

01:53.070 --> 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.

02:05.010 --> 02:07.750
Well, this really proved to be insufficient over time.

02:08.270 --> 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.

02:21.870 --> 02:24.670
And yeah, this really again proved to be insufficient.

02:24.670 --> 02:29.010
So we needed an additional security boundary into the OS.

02:29.330 --> 02:32.050
And this is where VBS comes in.

02:32.150 --> 02:35.490
So VBS came to our life through Hyper-V.

02:35.490 --> 02:36.850
So let's start with that.

02:36.850 --> 02:42.210
Hyper-V, Microsoft's hypervisor, traditionally used to run VMs, either locally or in the cloud.

02:42.870 --> 02:49.830
Now, one of the most important things that Hyper-V has to manage, and any good hypervisor for that, is isolation.

02:49.910 --> 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.

03:01.010 --> 03:06.150
So Hyper-V already comes with a very strong memory isolation capability.

03:06.150 --> 03:08.690
I will not go too deep into how it does it.

03:08.690 --> 03:11.070
There are plenty of good resources online.

03:11.730 --> 03:18.910
But yeah, so we have this very strong ability to restrict one VM from accessing to the memory of another one.

03:19.390 --> 03:25.150
Now, someone at Microsoft, at one point, probably had a good idea.

03:25.250 --> 03:31.010
Let's take this same primitive, isolation primitive, and apply it to the operating system itself.

03:31.010 --> 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.

03:43.090 --> 03:46.110
So this is the root of VBS.

03:46.110 --> 03:51.970
And it turns out that this isolation actually enables us to add additional security boundaries into the OS.

03:52.010 --> 03:55.810
So these are called virtual trust levels, or VTLs.

03:55.910 --> 03:58.210
And here we have what we had before.

03:58.210 --> 04:00.190
So we had two execution modes.

04:00.190 --> 04:03.770
We had user mode, ring three, and kernel mode, ring zero.

04:03.770 --> 04:08.350
Now, when we add VTLs into the mix, things get interesting.

04:08.550 --> 04:10.630
So first we have VTL zero.

04:10.810 --> 04:15.250
This is the least privileged VTL, which is also called normal world.

04:15.250 --> 04:16.730
And you already know it.

04:16.730 --> 04:19.710
So VTL zero contains the normal operating system.

04:19.710 --> 04:23.150
We have their user mode and normal kernel mode.

04:23.310 --> 04:26.990
Things get a bit more interesting when we introduce VTL one.

04:27.050 --> 04:30.370
Now, VTL one is more privileged than VTL zero.

04:30.370 --> 04:34.530
And this privilege comes in the form of memory isolation.

04:34.790 --> 04:40.590
Code running within VTL zero cannot access code or the memory of VTL one.

04:40.590 --> 04:44.170
On the other hand, VTL one can access the memory of VTL zero.

04:44.170 --> 04:48.590
So this is how this privilege comes into effect.

04:48.790 --> 04:52.170
So within VTL one, we have two new execution modes.

04:52.170 --> 04:55.770
We have VTL one, ring zero, for the secure kernel.

04:55.770 --> 05:03.170
This is a more privileged kernel that can actually be used to apply and enforce additional policies to the normal kernel.

05:03.870 --> 05:09.330
And we have isolated user mode, or IOM, which is VTL one, ring three.

05:09.330 --> 05:12.530
Now, on one hand, we are running in ring three.

05:12.530 --> 05:15.530
So we can't perform sensitive kernel operations.

05:15.530 --> 05:19.930
But we still enjoy the advantage of running within VTL one.

05:19.930 --> 05:22.010
Isolation, as the name suggests.

05:22.050 --> 05:28.810
And the thing is that IOM is going to be protected from threat actors that compromise the normal operating system.

05:28.810 --> 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.

05:37.990 --> 05:43.130
Okay, so these are VTLs and the new execution modes that they introduce.

05:43.270 --> 05:48.830
And now we can move on to our next topic, or our main topic, VBS enclaves.

05:49.450 --> 05:54.630
So, an enclave is an isolated region of memory within a process.

05:54.630 --> 05:58.010
The process cannot write into the enclave, it cannot read from the enclave.

05:58.010 --> 06:02.790
The only thing that it can do, is it can invoke methods that are exported by the enclave.

06:03.570 --> 06:06.650
So, why something like that is useful?

06:06.650 --> 06:08.670
Let's consider the following scenario.

06:08.670 --> 06:10.910
Our process handles some sort of secret.

06:10.910 --> 06:14.810
So , either something like an API key, a password.

06:15.370 --> 06:18.850
Now, let's say that the process were to be compromised.

06:18.850 --> 06:23.410
Either directly, or let's say the operating system itself was compromised.

06:23.530 --> 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.

06:31.750 --> 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.

06:41.150 --> 06:46.450
Now, if an attacker compromises the process, they will never be able to exfiltrate the secret.

06:46.510 --> 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.

06:52.370 --> 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.

07:02.310 --> 07:05.430
So, we pass the value and the enclave encrypts it.

07:05.650 --> 07:09.130
So, this is the idea of enclaves.

07:09.130 --> 07:14.770
There are additional usages for them as well, but I think this is a good start for that.

07:16.110 --> 07:18.590
And enclaves are a general concept.

07:19.410 --> 07:23.770
VBS enclaves are Microsoft's implementation of them using VBS.

07:23.770 --> 07:27.870
It is actually a very elegant implementation.

07:27.870 --> 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.

07:40.970 --> 07:47.430
By doing that, we ensure, again, that nothing in VTL0 can access the data within the enclave.

07:48.050 --> 07:55.290
Okay, so let's discuss a few more details about enclaves and how we use them in Windows.

07:55.510 --> 08:01.130
So, any process can create an enclave and load what is called an enclave module into it.

08:01.130 --> 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.

08:10.750 --> 08:14.510
And once we load the DLL, again, we can't access it directly.

08:14.510 --> 08:19.790
We can invoke its exported methods by calling the call enclave API.

08:20.730 --> 08:28.470
And one final note, before we move on, is that we can't just load any arbitrary DLL into an enclave.

08:28.470 --> 08:35.910
Microsoft realizes that running within an enclave should be something that is restricted.

08:35.910 --> 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.

08:47.530 --> 08:52.910
So, this is going to be the main challenge for us for the rest of the talk.

08:53.130 --> 08:58.070
So, now that you understand what VBS enclaves are, let's get to the fun part.

08:58.210 --> 08:59.730
How we can abuse them.

09:00.290 --> 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.

09:07.550 --> 09:14.390
So, the same advantages that are provided to normal applications could apply to attackers as well.

09:14.650 --> 09:17.850
So, first, obviously, we will have inaccessible memory.

09:17.910 --> 09:25.650
EDRs and security analysts, whatever, they won't be able to scan our memory in order to identify malicious behavior.

09:26.470 --> 09:30.490
Another advantage has to do with behavior, not just scanning memory.

09:30.490 --> 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.

09:38.610 --> 09:40.130
So, let's understand this.

09:40.390 --> 09:47.570
When a normal process triggers a Windows API, we go through a process that I believe most of you probably know.

09:47.570 --> 09:51.410
We go through a series of system DLLs, eventually anti-DLL.

09:51.410 --> 09:54.450
From there, we invoke a syscall into the normal kernel.

09:54.670 --> 09:59.450
Now, the thing about this flow is that EDRs have a lot of visibility into it.

09:59.450 --> 10:01.750
They can hook our calls into DLLs.

10:01.750 --> 10:03.790
They can monitor our activity in the kernel.

10:04.010 --> 10:06.450
There are a lot of detection opportunities here.

10:06.510 --> 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.

10:19.010 --> 10:24.810
So, instead of going through anti-DLL, we go through VEL DLL, VBS Enclave Runtime DLL.

10:24.810 --> 10:30.430
And from there, when we invoke a syscall, it is managed by the secure kernel.

10:30.570 --> 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.

10:42.610 --> 10:44.950
So, yeah, another advantage that we gain.

10:45.690 --> 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.

10:58.990 --> 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.

11:07.010 --> 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.

11:15.710 --> 11:18.450
So, attackers could obtain a legitimate signature.

11:18.610 --> 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.

11:31.570 --> 11:37.950
So, they could either compromise an authorized signer or obtain signing privileges legitimately through a shell company.

11:37.950 --> 11:39.410
So, this is one option.

11:39.970 --> 11:49.310
Another option would be to exploit a vulnerability in the Windows loader that is in charge of validating the enclave signature.

11:49.310 --> 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.

12:02.150 --> 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.

12:15.750 --> 12:24.390
Now, these two approaches are certainly valid, and I believe that advanced threat actors, nation-state threat actors, could use them.

12:24.390 --> 12:25.710
But I don't know what about you.

12:25.710 --> 12:28.310
I'm personally not a nation-state threat actor.

12:28.310 --> 12:34.490
So, I wanted to find something that might be more accessible to, well, less sophisticated threat actors.

12:35.170 --> 12:40.370
And I ended up finding two additional paths to get code running within an enclave.

12:40.370 --> 12:45.170
The first one is the abuse of debuggable enclave modules.

12:45.170 --> 12:53.890
So, it turns out that if you want to debug an enclave during development, it's actually not a straightforward task.

12:54.070 --> 12:54.910
Think about it.

12:54.910 --> 12:57.130
Enclaves run within VTL1.

12:58.410 --> 13:01.070
Our debugger runs in VTL0.

13:01.130 --> 13:05.350
If we simply try to debug an enclave, we won't be able to do that, as we can see here.

13:05.350 --> 13:06.490
You can't access the memory.

13:06.490 --> 13:07.950
You can't place breakpoints.

13:08.030 --> 13:13.050
So, yeah, Microsoft, I believe, pretty early realized this issue.

13:13.050 --> 13:16.910
And because of this, they implemented debuggable enclaves.

13:17.230 --> 13:22.970
So, when you compile an enclave, you can specify a flag that will mark it as debuggable.

13:23.070 --> 13:26.950
And when you do that, well, like magic, you can now debug the enclave.

13:26.950 --> 13:29.650
As we can see here, we can now access the memory of the enclave.

13:29.650 --> 13:30.950
We can place breakpoints.

13:31.070 --> 13:32.510
Yeah, we can have a good time.

13:33.070 --> 13:36.870
Now, when I initially saw this, I was kind of confused and surprised.

13:36.870 --> 13:42.750
Because we know that enclaves reside in VTL1, and we are accessing their memory.

13:42.750 --> 13:44.070
So, it's kind of weird.

13:44.390 --> 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.

13:50.610 --> 13:54.890
But upon digging into this, I realized that this is not actually the case.

13:54.890 --> 13:58.590
Debuggable enclaves still reside in isolated user mode.

13:58.590 --> 14:05.150
And to enable the debugging that we have just witnessed, the secure kernel actually implements some exceptions.

14:05.590 --> 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.

14:16.290 --> 14:18.430
And if it is, it will allow us to access it.

14:18.770 --> 14:25.590
Similarly, we have the debug protect virtual memory, which will allow us to change memory permissions on debuggable enclaves.

14:26.570 --> 14:34.270
So, now that I understood more about these special enclaves, I wanted to see how we can abuse them.

14:34.270 --> 14:41.370
So, first, and pretty obviously, it is not a good idea to use a debuggable enclave for a production application.

14:41.370 --> 14:44.810
Microsoft clearly acknowledges this in their documentation.

14:44.810 --> 14:48.510
Because if you're using a debuggable enclave, you're essentially not really using an enclave.

14:48.510 --> 14:50.010
Anyone can access its memory.

14:50.010 --> 14:52.550
You might as well just allocate a normal memory buffer.

14:53.650 --> 14:55.190
But, yeah, so this is a risk.

14:55.190 --> 14:56.190
It is acknowledged.

14:56.570 --> 15:01.290
But it turns out that there's another interesting way that attackers can actually abuse this.

15:01.290 --> 15:07.630
So, let's say that an attacker obtains a signed debuggable enclave.

15:07.630 --> 15:11.350
So, a DLL, which is signed and marked as debuggable.

15:12.230 --> 15:18.070
It turns out that enclave modules are not paired to a specific process.

15:18.070 --> 15:21.410
Meaning that any process can load any module.

15:21.410 --> 15:27.090
So, what an attacker can do is they can load this debuggable enclave into their own process.

15:27.090 --> 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.

15:37.750 --> 15:40.750
So, let's look at this process, how it would look like.

15:40.910 --> 15:47.210
So, here we have an attacker who obtained this type of debuggable enclave and loaded it.

15:47.210 --> 15:50.810
And we can see that the enclave contains an enclave routine in green.

15:50.810 --> 15:53.350
A function which is exported by the enclave.

15:53.350 --> 15:57.190
What the attacker can do is they can obtain the address of this routine.

15:57.570 --> 16:02.790
They can then change their memory permission to allow a page execute read write.

16:02.790 --> 16:05.450
This is possible because the enclave is debuggable.

16:06.070 --> 16:13.110
They can then overwrite the routine and invoke the shellcode by using call enclave.

16:13.110 --> 16:17.990
So, we can't just run arbitrary addresses within the enclave.

16:17.990 --> 16:24.290
But, because this is an exported function that we overwritten, we can trigger it normally by using call enclave.

16:24.650 --> 16:25.250
So, yeah.

16:25.250 --> 16:26.790
This is a cool technique.

16:26.790 --> 16:27.770
It works.

16:27.790 --> 16:30.610
But, there are some caveats to it.

16:30.610 --> 16:31.890
So, let's cover them.

16:32.450 --> 16:37.110
The first one is that the memory access actually goes both ways.

16:37.110 --> 16:43.110
So, just like we can access the memory of the enclave, EDRs can do the same thing.

16:43.110 --> 16:46.330
Because, as we just saw, the exceptions apply to them as well.

16:47.190 --> 16:50.730
Despite that, there are still advantages to using this technique.

16:50.730 --> 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.

16:59.510 --> 17:00.410
So, yeah.

17:00.410 --> 17:05.490
EDRs might not consider this scenario, even though they can theoretically detect it.

17:06.570 --> 17:15.330
And the second advantage is that enclaves, even when they are debuggable, will still use the API call chain in VTL1.

17:15.330 --> 17:18.150
So, hooking their APIs will not be possible.

17:19.290 --> 17:25.510
The second unfortunate catch is that, at the moment, I haven't actually managed to find one.

17:25.570 --> 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.

17:35.730 --> 17:38.930
But, at the moment, I was yet to be successful.

17:39.150 --> 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.

17:52.110 --> 17:57.570
So, the thing about enclaves today is that it is kind of a not really adopted technology.

17:57.570 --> 17:58.890
Not a lot of people use it.

17:58.890 --> 18:05.010
But Microsoft really began pushing towards this direction, implementing a lot of things using enclaves themselves.

18:05.050 --> 18:13.210
And I believe that we will begin to see more third-party drivers, developers utilize them in the future.

18:13.210 --> 18:14.970
And we all know developers.

18:15.150 --> 18:16.610
Developers fuck up.

18:16.610 --> 18:25.390
And one file will find its way into VirusTotal, or somehow into the internet, and this technique now becomes available.

18:25.390 --> 18:28.110
So, yeah, I think it is worth considering.

18:29.130 --> 18:32.590
Okay, so this was the first idea, abusing debuggable enclaves.

18:32.590 --> 18:37.570
And we'll now move on to the second idea, exploiting vulnerable enclave modules.

18:37.710 --> 18:45.730
So, the idea is not mine, let's say, because I kind of applied a very well-known technique.

18:46.130 --> 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.

18:57.730 --> 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.

19:09.010 --> 19:13.490
Well, what I thought was, why not apply the same idea to enclaves?

19:13.490 --> 19:19.890
If we manage to find a vulnerable enclave, we can load it, exploit it, and get our code running within IOM.

19:20.370 --> 19:27.070
So, luckily, such a vulnerability exists, CVE-2023-36880.

19:27.570 --> 19:33.130
This is an information disclosure vulnerability in an enclave module used by Microsoft Edge.

19:34.050 --> 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.

19:45.150 --> 19:47.910
So, I decided to dig into this a bit more.

19:48.870 --> 19:56.450
This vulnerability was discovered by Alex Go from the Chrome security team, who also shared a POC for exploiting it.

19:56.750 --> 20:03.570
It is actually a relatively simple vulnerability, and it allows an arbitrary read-write into the enclave.

20:03.570 --> 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.

20:15.690 --> 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.

20:28.570 --> 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.

20:41.010 --> 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.

20:49.890 --> 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.

21:03.910 --> 21:11.410
In practice, it means we can't allocate new executable pages, and we can't turn existing executable pages writable.

21:11.490 --> 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.

21:31.130 --> 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.

21:46.110 --> 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.

21:54.570 --> 21:56.810
I knew that I had something here.

21:56.810 --> 22:03.270
So, the ability to write and read whatever I want into the enclave felt just too cool to be not useful.

22:03.730 --> 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.

22:12.630 --> 22:16.190
So, yeah, this brings us into bring your own vulnerable enclave round two.

22:17.470 --> 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.

22:24.850 --> 22:30.330
So, the vulnerability actually provides two very interesting capabilities.

22:30.510 --> 22:35.850
So, first, we can write whatever data that we want into the enclave.

22:35.850 --> 22:39.550
So, we can't execute it, but we can load data there.

22:39.650 --> 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.

22:55.890 --> 23:04.330
The second interesting advantage, which is a little less straightforward, is that the vulnerability allows us to actually work in reverse.

23:04.330 --> 23:11.090
We can trigger it in such a way that allows us to write data into VTL0 from VTL1.

23:11.090 --> 23:18.270
So, we can trigger the exploit causing the enclave to write data back into the normal address space of the process.

23:18.630 --> 23:25.990
Now, this is useful because, as we mentioned before, there is no visibility into this interaction.

23:25.990 --> 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.

23:35.990 --> 23:44.270
So, combining these two capabilities, I came up with a memory evasion technique that I dubbed Mirage.

23:44.510 --> 23:47.310
And Mirage is a sleep obfuscation technique.

23:47.310 --> 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.

23:56.870 --> 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.

24:04.410 --> 24:10.270
And the idea is that during sleep we protect our code in a way that will make detection harder.

24:10.270 --> 24:15.190
So, for example, we can mark its memory non-executable or we can encrypt it.

24:15.410 --> 24:23.610
So, my idea was, why not use this vulnerability to hide our shellcode within isolated user mode during sleep.

24:23.910 --> 24:26.850
So, let's understand how this technique will look like.

24:26.870 --> 24:34.730
So, first, Mirage will load the vulnerable enclave and exploit it in order to write shellcode into it.

24:35.270 --> 24:40.430
Now, at this point, if an EDR takes a look, it will not see anything.

24:40.430 --> 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.

24:47.890 --> 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.

25:01.270 --> 25:06.250
We get our shellcode, we run it, and then we override it again with benign data.

25:06.490 --> 25:12.310
The cycle can repeat itself, and the idea is that it makes it almost impossible for an EDR to detect us.

25:12.990 --> 25:15.050
So, let's look at a demo of this.

25:21.670 --> 25:25.390
So, here we loaded Mirage.

25:25.390 --> 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.

25:37.550 --> 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.

25:44.650 --> 25:47.690
Everything else is going to occur from the context of the enclave.

25:47.690 --> 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.

25:57.830 --> 26:04.750
And now, we can jump to the shellcode, execute it, and quickly override it with benign data again.

26:04.750 --> 26:08.330
So, that's it, we have our beautiful calculator here, and we're out.

26:08.330 --> 26:10.250
Shellcode is nowhere to be found again.

26:11.190 --> 26:12.950
So, yeah, this is Mirage.

26:13.010 --> 26:13.690
We've released...

26:14.450 --> 26:15.590
Thank you.

26:20.460 --> 26:23.860
We've released a proof-of-concept code for this.

26:23.860 --> 26:26.120
You can find it on Akamai's GitHub.

26:26.940 --> 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.

26:34.620 --> 26:41.740
It turns out that we worked on almost the same exact research in very similar time.

26:41.740 --> 26:46.520
And they published two months ago their own work on the exact same vulnerability.

26:46.520 --> 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.

26:59.820 --> 27:02.520
So, they weren't able to get arbitrary code execution.

27:02.520 --> 27:08.300
What they were able to do was to create a sleep obfuscation technique, but their one relied on a ROP chain.

27:08.300 --> 27:10.260
So, I really encourage you to check out their work.

27:10.260 --> 27:11.980
I think it is more cool than mine.

27:12.240 --> 27:13.520
So, yeah, check it out.

27:13.520 --> 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.

27:26.500 --> 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.

27:34.420 --> 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.

27:44.780 --> 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.

27:54.180 --> 27:59.460
So, most traditional malware techniques are not going to be accessible to us.

28:00.460 --> 28:01.800
So, what can we do?

28:03.000 --> 28:11.620
So, I was able to come up with a few techniques that attackers can still perform even despite all these restrictions.

28:11.700 --> 28:17.200
So, the first thing that we can do is we can access the VTL0 memory of our host process.

28:17.400 --> 28:24.300
So , the enclave, even though it runs in VTL0, can read and write the normal user mode process.

28:24.680 --> 28:30.920
So , this could be interesting in the context where we, for example, we can inject an enclave.

28:30.920 --> 28:35.260
So, we create a payload which injects an enclave into a remote process.

28:35.260 --> 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.

28:48.770 --> 28:50.570
Let's take a look at another demo.

28:51.070 --> 28:57.410
So, we have here our Qt program that receives a string and echoes it back to us.

28:57.410 --> 28:59.330
So, we enter test, we receive test.

28:59.530 --> 29:04.270
We will now write that VBS is awesome and we will see that VBS is in fact awesome.

29:04.590 --> 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.

29:12.330 --> 29:15.010
So, let's understand what is happening here.

29:21.170 --> 29:28.390
So, we have a very advanced piece of malware here, as you can see, that runs within an enclave.

29:28.390 --> 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.

29:40.830 --> 29:43.210
If it detects it, it replaces it with a good one.

29:43.290 --> 29:46.590
So, yeah, a nation state grade malware here.

29:47.250 --> 29:51.330
But I wanted to show you this because of the memcpy line specifically.

29:51.530 --> 29:53.430
And let's see why it's interesting.

29:53.430 --> 29:57.530
So, as we can see, our enclave will perform the memcpy operation.

29:57.970 --> 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.

30:09.850 --> 30:14.530
And what we would anticipate now is we will see the breakpoint being hit.

30:14.630 --> 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.

30:22.910 --> 30:29.350
So, yeah, this happens exactly because of the API evasion that I previously described.

30:29.570 --> 30:36.470
And, again, the advantage here is that we can basically interact with the process in a way that cannot be detected.

30:37.210 --> 30:42.150
Another cool thing that we can do is we can execute code within VTL0.

30:42.150 --> 30:44.990
So, we can't do that.

30:44.990 --> 30:48.410
So, we can't directly execute VTL code in VTL1.

30:48.410 --> 30:49.510
This is forbidden.

30:49.510 --> 30:54.690
If you'll try to take an executable VTL0 address and directly execute it, it will not work.

30:54.990 --> 31:02.830
But what we can do is we can remotely invoke a VTL0 code from VTL1.

31:02.830 --> 31:06.010
So, this can be performed by using the call enclave API.

31:06.010 --> 31:09.310
It turns out that this is a dual use function.

31:09.670 --> 31:15.630
So, normally we use it to invoke enclave methods from outside, from the process.

31:15.630 --> 31:20.270
But another thing that we can do is invoke VTL0 methods from the enclave.

31:20.270 --> 31:26.950
So, essentially we can pass any executable address through this and it will execute the code in VTL0.

31:27.550 --> 31:35.150
In terms of evasion, again, it is limited because the code will run in VTL0.

31:35.150 --> 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.

31:47.240 --> 31:51.260
So, here we have our malware here.

31:51.560 --> 31:56.180
What it will do is it will allocate a read-write execute buffer in VTL0.

31:56.300 --> 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.

32:01.220 --> 32:03.300
So, we have our buffer here.

32:03.300 --> 32:04.460
We can see it is empty.

32:05.800 --> 32:09.540
And next what we will do is we will pass execution to the enclave.

32:09.540 --> 32:10.600
That's it.

32:12.460 --> 32:15.940
When we do that, we see that our calculator here popped.

32:15.940 --> 32:17.240
Our shellcode executed.

32:17.240 --> 32:20.300
If we check the buffer again, we will see that shellcode is there.

32:20.300 --> 32:25.420
I left it there for the purpose of this demo but I could have just as well deleted it.

32:25.580 --> 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.

32:34.020 --> 32:40.200
So , there is no direct connection between the two which can help with evasion.

32:43.150 --> 32:44.130
Okay.

32:46.330 --> 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.

32:54.450 --> 33:00.570
So, enclaves are in a very advantageous situation where it comes to anti-debugging.

33:00.570 --> 33:08.090
In pretty much every case when performing malware analysis, the analysts will use a VTL0 debugger.

33:08.090 --> 33:10.630
Either a kernel-mode one or a user-mode one.

33:10.750 --> 33:15.310
Now, because of this, the enclave has a lot of power over the debugger.

33:15.430 --> 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.

33:24.350 --> 33:26.650
So, yeah, this could be a very cool idea.

33:27.190 --> 33:30.790
The problem is that it is, again, not going to be that easy.

33:30.790 --> 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.

33:39.770 --> 33:45.390
So, despite that, I was able to come up with a few techniques that we can perform from the enclave.

33:45.670 --> 33:52.070
So, first, we can manually inspect the PEB because we have access to the VTL0 memory.

33:52.250 --> 33:57.690
So, if we determine that the process is being debugged through the PEB, we can terminate it from the enclave.

33:58.930 --> 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.

34:06.130 --> 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.

34:20.030 --> 34:21.710
So, yeah, we have some options.

34:22.610 --> 34:26.130
Now, how can we make this particularly devastating?

34:26.310 --> 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.

34:37.790 --> 34:44.810
So, in order to make this particularly devastating, what we can do is we can move critical logic into the enclave.

34:44.810 --> 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.

34:51.950 --> 34:53.590
You put it within the enclave.

34:54.610 --> 34:58.850
And then, from the same call enclave, you also detect the debugger.

34:58.870 --> 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.

35:13.370 --> 35:19.470
But, as soon as they do that, the enclave can detect the debugger and terminate the process.

35:19.770 --> 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.

35:31.170 --> 35:32.690
It's not fun.

35:33.810 --> 35:38.110
So, in conclusion, VBS is pretty cool.

35:38.170 --> 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.

35:47.990 --> 35:52.630
As an ex-red teamer, I can say that it suddenly made my life harder.

35:53.890 --> 35:57.790
VBS enclaves, in particular, have a lot of potential.

35:57.790 --> 36:03.710
I think that we will continue to see more and more applications using them, and that's a good thing.

36:04.210 --> 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.

36:18.190 --> 36:18.970
That's it.

36:18.970 --> 36:20.030
Thank you very much.

36:24.320 --> 36:26.340
If you have any questions, I'll be here.

