start	end	text
1340	5520	I'm super excited to be here, first time at DEF CON, so welcome to my talk.
5760	10960	I am very tired, I am severely hungover, so I really hope this goes well.
12200	16740	Now, before we get into the talk itself, I wanted to start with a short apology.
17140	32530	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,
32530	34190	virtualization-based security.
34770	38710	So yeah, sorry for all the VBScript fans, anyone is leaving?
38890	39410	No?
39410	40110	Okay.
40370	42450	So, yeah, I guess we can start.
42770	46610	My name is Uri David, I'm a security researcher at Akamai.
46630	53290	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.
53290	60210	And for the last few months I really focused on the area of virtualization-based security.
61030	68630	I believe this is one of the most significant and interesting advancements in security that Microsoft have implemented in recent years.
68630	74870	So we will start by briefly covering it, understand what it does, how it does it, and why it does it.
75430	85690	And then we'll get to VBS enclaves, a feature which is enabled through VBS, and have a lot of very cool security applications.
86510	89570	And then we'll get to the fun part, abusing VBS enclaves.
89570	95610	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.
96510	98330	Okay, so let's start with VBS.
98390	103510	And to talk about VBS we first need to talk about boundaries, security boundaries.
103510	113070	So traditionally in order to protect the operating system, Microsoft and other operating systems as well, relied on processor rings.
113070	124710	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.
125010	127750	Well, this really proved to be insufficient over time.
128270	141430	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.
141870	144670	And yeah, this really again proved to be insufficient.
144670	149010	So we needed an additional security boundary into the OS.
149330	152050	And this is where VBS comes in.
152150	155490	So VBS came to our life through Hyper-V.
155490	156850	So let's start with that.
156850	162210	Hyper-V, Microsoft's hypervisor, traditionally used to run VMs, either locally or in the cloud.
162870	169830	Now, one of the most important things that Hyper-V has to manage, and any good hypervisor for that, is isolation.
169910	180910	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.
181010	186150	So Hyper-V already comes with a very strong memory isolation capability.
186150	188690	I will not go too deep into how it does it.
188690	191070	There are plenty of good resources online.
191730	198910	But yeah, so we have this very strong ability to restrict one VM from accessing to the memory of another one.
199390	205150	Now, someone at Microsoft, at one point, probably had a good idea.
205250	211010	Let's take this same primitive, isolation primitive, and apply it to the operating system itself.
211010	222250	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.
223090	226110	So this is the root of VBS.
226110	231970	And it turns out that this isolation actually enables us to add additional security boundaries into the OS.
232010	235810	So these are called virtual trust levels, or VTLs.
235910	238210	And here we have what we had before.
238210	240190	So we had two execution modes.
240190	243770	We had user mode, ring three, and kernel mode, ring zero.
243770	248350	Now, when we add VTLs into the mix, things get interesting.
248550	250630	So first we have VTL zero.
250810	255250	This is the least privileged VTL, which is also called normal world.
255250	256730	And you already know it.
256730	259710	So VTL zero contains the normal operating system.
259710	263150	We have their user mode and normal kernel mode.
263310	266990	Things get a bit more interesting when we introduce VTL one.
267050	270370	Now, VTL one is more privileged than VTL zero.
270370	274530	And this privilege comes in the form of memory isolation.
274790	280590	Code running within VTL zero cannot access code or the memory of VTL one.
280590	284170	On the other hand, VTL one can access the memory of VTL zero.
284170	288590	So this is how this privilege comes into effect.
288790	292170	So within VTL one, we have two new execution modes.
292170	295770	We have VTL one, ring zero, for the secure kernel.
295770	303170	This is a more privileged kernel that can actually be used to apply and enforce additional policies to the normal kernel.
303870	309330	And we have isolated user mode, or IOM, which is VTL one, ring three.
309330	312530	Now, on one hand, we are running in ring three.
312530	315530	So we can't perform sensitive kernel operations.
315530	319930	But we still enjoy the advantage of running within VTL one.
319930	322010	Isolation, as the name suggests.
322050	328810	And the thing is that IOM is going to be protected from threat actors that compromise the normal operating system.
328810	337010	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.
337990	343130	Okay, so these are VTLs and the new execution modes that they introduce.
343270	348830	And now we can move on to our next topic, or our main topic, VBS enclaves.
349450	354630	So, an enclave is an isolated region of memory within a process.
354630	358010	The process cannot write into the enclave, it cannot read from the enclave.
358010	362790	The only thing that it can do, is it can invoke methods that are exported by the enclave.
363570	366650	So, why something like that is useful?
366650	368670	Let's consider the following scenario.
368670	370910	Our process handles some sort of secret.
370910	374810	So , either something like an API key, a password.
375370	378850	Now, let's say that the process were to be compromised.
378850	383410	Either directly, or let's say the operating system itself was compromised.
383530	391410	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.
391750	401070	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.
401150	406450	Now, if an attacker compromises the process, they will never be able to exfiltrate the secret.
406510	412370	In order to actually utilize it, what the process can do, is it can invoke the methods that are exported by the enclave.
412370	422310	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.
422310	425430	So, we pass the value and the enclave encrypts it.
425650	429130	So, this is the idea of enclaves.
429130	434770	There are additional usages for them as well, but I think this is a good start for that.
436110	438590	And enclaves are a general concept.
439410	443770	VBS enclaves are Microsoft's implementation of them using VBS.
443770	447870	It is actually a very elegant implementation.
447870	460510	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.
460970	467430	By doing that, we ensure, again, that nothing in VTL0 can access the data within the enclave.
468050	475290	Okay, so let's discuss a few more details about enclaves and how we use them in Windows.
475510	481130	So, any process can create an enclave and load what is called an enclave module into it.
481130	490490	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.
490750	494510	And once we load the DLL, again, we can't access it directly.
494510	499790	We can invoke its exported methods by calling the call enclave API.
500730	508470	And one final note, before we move on, is that we can't just load any arbitrary DLL into an enclave.
508470	515910	Microsoft realizes that running within an enclave should be something that is restricted.
515910	527450	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.
527530	532910	So, this is going to be the main challenge for us for the rest of the talk.
533130	538070	So, now that you understand what VBS enclaves are, let's get to the fun part.
538210	539730	How we can abuse them.
540290	547550	So, the first thing that came, at least into my mind when considering this, was the idea of running malware within an enclave.
547550	554390	So, the same advantages that are provided to normal applications could apply to attackers as well.
554650	557850	So, first, obviously, we will have inaccessible memory.
557910	565650	EDRs and security analysts, whatever, they won't be able to scan our memory in order to identify malicious behavior.
566470	570490	Another advantage has to do with behavior, not just scanning memory.
570490	578610	Well, it turns out that it is much harder, or pretty much impossible, to trace API calls that are performed from an enclave.
578610	580130	So, let's understand this.
580390	587570	When a normal process triggers a Windows API, we go through a process that I believe most of you probably know.
587570	591410	We go through a series of system DLLs, eventually anti-DLL.
591410	594450	From there, we invoke a syscall into the normal kernel.
594670	599450	Now, the thing about this flow is that EDRs have a lot of visibility into it.
599450	601750	They can hook our calls into DLLs.
601750	603790	They can monitor our activity in the kernel.
604010	606450	There are a lot of detection opportunities here.
606510	619010	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.
619010	624810	So, instead of going through anti-DLL, we go through VEL DLL, VBS Enclave Runtime DLL.
624810	630430	And from there, when we invoke a syscall, it is managed by the secure kernel.
630570	642610	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.
642610	644950	So, yeah, another advantage that we gain.
645690	658990	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.
658990	666950	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.
667010	675490	So, we'll start with the straightforward ones, I guess, that I did want to mention before going to the next ones.
675710	678450	So, attackers could obtain a legitimate signature.
678610	691230	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.
691570	697950	So, they could either compromise an authorized signer or obtain signing privileges legitimately through a shell company.
697950	699410	So, this is one option.
699970	709310	Another option would be to exploit a vulnerability in the Windows loader that is in charge of validating the enclave signature.
709310	722150	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.
722150	735750	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.
735750	744390	Now, these two approaches are certainly valid, and I believe that advanced threat actors, nation-state threat actors, could use them.
744390	745710	But I don't know what about you.
745710	748310	I'm personally not a nation-state threat actor.
748310	754490	So, I wanted to find something that might be more accessible to, well, less sophisticated threat actors.
755170	760370	And I ended up finding two additional paths to get code running within an enclave.
760370	765170	The first one is the abuse of debuggable enclave modules.
765170	773890	So, it turns out that if you want to debug an enclave during development, it's actually not a straightforward task.
774070	774910	Think about it.
774910	777130	Enclaves run within VTL1.
778410	781070	Our debugger runs in VTL0.
781130	785350	If we simply try to debug an enclave, we won't be able to do that, as we can see here.
785350	786490	You can't access the memory.
786490	787950	You can't place breakpoints.
788030	793050	So, yeah, Microsoft, I believe, pretty early realized this issue.
793050	796910	And because of this, they implemented debuggable enclaves.
797230	802970	So, when you compile an enclave, you can specify a flag that will mark it as debuggable.
803070	806950	And when you do that, well, like magic, you can now debug the enclave.
806950	809650	As we can see here, we can now access the memory of the enclave.
809650	810950	We can place breakpoints.
811070	812510	Yeah, we can have a good time.
813070	816870	Now, when I initially saw this, I was kind of confused and surprised.
816870	822750	Because we know that enclaves reside in VTL1, and we are accessing their memory.
822750	824070	So, it's kind of weird.
824390	830590	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.
830610	834890	But upon digging into this, I realized that this is not actually the case.
834890	838590	Debuggable enclaves still reside in isolated user mode.
838590	845150	And to enable the debugging that we have just witnessed, the secure kernel actually implements some exceptions.
845590	856290	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.
856290	858430	And if it is, it will allow us to access it.
858770	865590	Similarly, we have the debug protect virtual memory, which will allow us to change memory permissions on debuggable enclaves.
866570	874270	So, now that I understood more about these special enclaves, I wanted to see how we can abuse them.
874270	881370	So, first, and pretty obviously, it is not a good idea to use a debuggable enclave for a production application.
881370	884810	Microsoft clearly acknowledges this in their documentation.
884810	888510	Because if you're using a debuggable enclave, you're essentially not really using an enclave.
888510	890010	Anyone can access its memory.
890010	892550	You might as well just allocate a normal memory buffer.
893650	895190	But, yeah, so this is a risk.
895190	896190	It is acknowledged.
896570	901290	But it turns out that there's another interesting way that attackers can actually abuse this.
901290	907630	So, let's say that an attacker obtains a signed debuggable enclave.
907630	911350	So, a DLL, which is signed and marked as debuggable.
912230	918070	It turns out that enclave modules are not paired to a specific process.
918070	921410	Meaning that any process can load any module.
921410	927090	So, what an attacker can do is they can load this debuggable enclave into their own process.
927090	937750	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.
937750	940750	So, let's look at this process, how it would look like.
940910	947210	So, here we have an attacker who obtained this type of debuggable enclave and loaded it.
947210	950810	And we can see that the enclave contains an enclave routine in green.
950810	953350	A function which is exported by the enclave.
953350	957190	What the attacker can do is they can obtain the address of this routine.
957570	962790	They can then change their memory permission to allow a page execute read write.
962790	965450	This is possible because the enclave is debuggable.
966070	973110	They can then overwrite the routine and invoke the shellcode by using call enclave.
973110	977990	So, we can't just run arbitrary addresses within the enclave.
977990	984290	But, because this is an exported function that we overwritten, we can trigger it normally by using call enclave.
984650	985250	So, yeah.
985250	986790	This is a cool technique.
986790	987770	It works.
987790	990610	But, there are some caveats to it.
990610	991890	So, let's cover them.
992450	997110	The first one is that the memory access actually goes both ways.
997110	1003110	So, just like we can access the memory of the enclave, EDRs can do the same thing.
1003110	1006330	Because, as we just saw, the exceptions apply to them as well.
1007190	1010730	Despite that, there are still advantages to using this technique.
1010730	1018530	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.
1019510	1020410	So, yeah.
1020410	1025490	EDRs might not consider this scenario, even though they can theoretically detect it.
1026570	1035330	And the second advantage is that enclaves, even when they are debuggable, will still use the API call chain in VTL1.
1035330	1038150	So, hooking their APIs will not be possible.
1039290	1045510	The second unfortunate catch is that, at the moment, I haven't actually managed to find one.
1045570	1055730	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.
1055730	1058930	But, at the moment, I was yet to be successful.
1059150	1072110	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.
1072110	1077570	So, the thing about enclaves today is that it is kind of a not really adopted technology.
1077570	1078890	Not a lot of people use it.
1078890	1085010	But Microsoft really began pushing towards this direction, implementing a lot of things using enclaves themselves.
1085050	1093210	And I believe that we will begin to see more third-party drivers, developers utilize them in the future.
1093210	1094970	And we all know developers.
1095150	1096610	Developers fuck up.
1096610	1105390	And one file will find its way into VirusTotal, or somehow into the internet, and this technique now becomes available.
1105390	1108110	So, yeah, I think it is worth considering.
1109130	1112590	Okay, so this was the first idea, abusing debuggable enclaves.
1112590	1117570	And we'll now move on to the second idea, exploiting vulnerable enclave modules.
1117710	1125730	So, the idea is not mine, let's say, because I kind of applied a very well-known technique.
1126130	1137630	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.
1137730	1149010	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.
1149010	1153490	Well, what I thought was, why not apply the same idea to enclaves?
1153490	1159890	If we manage to find a vulnerable enclave, we can load it, exploit it, and get our code running within IOM.
1160370	1167070	So, luckily, such a vulnerability exists, CVE-2023-36880.
1167570	1173130	This is an information disclosure vulnerability in an enclave module used by Microsoft Edge.
1174050	1184750	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.
1185150	1187910	So, I decided to dig into this a bit more.
1188870	1196450	This vulnerability was discovered by Alex Go from the Chrome security team, who also shared a POC for exploiting it.
1196750	1203570	It is actually a relatively simple vulnerability, and it allows an arbitrary read-write into the enclave.
1203570	1214490	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.
1215690	1228570	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.
1228570	1240770	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.
1241010	1249890	Well, it wasn't going to be that easy, as I quickly discovered, because enclaves are protected with ACG, arbitrary code guard, by default.
1249890	1263910	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.
1263910	1271410	In practice, it means we can't allocate new executable pages, and we can't turn existing executable pages writable.
1271490	1290510	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.
1291130	1305690	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.
1306110	1314230	So, yeah, at this point, I was kind of stuck, and you might say it got into my personal life as well.
1314570	1316810	I knew that I had something here.
1316810	1323270	So, the ability to write and read whatever I want into the enclave felt just too cool to be not useful.
1323730	1331930	So, yeah, after some sleepless nights and many days trying to do something with this, I came up with a pretty interesting technique.
1332630	1336190	So, yeah, this brings us into bring your own vulnerable enclave round two.
1337470	1344850	So, I tried to remain positive, and instead of thinking about what we can't do, focus about what we can do.
1344850	1350330	So, the vulnerability actually provides two very interesting capabilities.
1350510	1355850	So, first, we can write whatever data that we want into the enclave.
1355850	1359550	So, we can't execute it, but we can load data there.
1359650	1375070	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.
1375890	1384330	The second interesting advantage, which is a little less straightforward, is that the vulnerability allows us to actually work in reverse.
1384330	1391090	We can trigger it in such a way that allows us to write data into VTL0 from VTL1.
1391090	1398270	So, we can trigger the exploit causing the enclave to write data back into the normal address space of the process.
1398630	1405990	Now, this is useful because, as we mentioned before, there is no visibility into this interaction.
1405990	1415710	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.
1415990	1424270	So, combining these two capabilities, I came up with a memory evasion technique that I dubbed Mirage.
1424510	1427310	And Mirage is a sleep obfuscation technique.
1427310	1436670	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.
1436870	1444410	The payload then wakes up occasionally based on a defined trigger, executes its code, and then quickly goes back to sleep again.
1444410	1450270	And the idea is that during sleep we protect our code in a way that will make detection harder.
1450270	1455190	So, for example, we can mark its memory non-executable or we can encrypt it.
1455410	1463610	So, my idea was, why not use this vulnerability to hide our shellcode within isolated user mode during sleep.
1463910	1466850	So, let's understand how this technique will look like.
1466870	1474730	So, first, Mirage will load the vulnerable enclave and exploit it in order to write shellcode into it.
1475270	1480430	Now, at this point, if an EDR takes a look, it will not see anything.
1480430	1487890	So, our normal memory is completely clear, we are completely innocent, all the malicious stuff are in VTL1 where they don't have access.
1487890	1500390	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.
1501270	1506250	We get our shellcode, we run it, and then we override it again with benign data.
1506490	1512310	The cycle can repeat itself, and the idea is that it makes it almost impossible for an EDR to detect us.
1512990	1515050	So, let's look at a demo of this.
1521670	1525390	So, here we loaded Mirage.
1525390	1536710	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.
1537550	1544650	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.
1544650	1547690	Everything else is going to occur from the context of the enclave.
1547690	1557330	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.
1557830	1564750	And now, we can jump to the shellcode, execute it, and quickly override it with benign data again.
1564750	1568330	So, that's it, we have our beautiful calculator here, and we're out.
1568330	1570250	Shellcode is nowhere to be found again.
1571190	1572950	So, yeah, this is Mirage.
1573010	1573690	We've released...
1574450	1575590	Thank you.
1580460	1583860	We've released a proof-of-concept code for this.
1583860	1586120	You can find it on Akamai's GitHub.
1586940	1594620	And before I move on, I just want to mention the amazing work by Cedric van Bokhaven and Matteo Malvica of Outflank.
1594620	1601740	It turns out that we worked on almost the same exact research in very similar time.
1601740	1606520	And they published two months ago their own work on the exact same vulnerability.
1606520	1619820	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.
1619820	1622520	So, they weren't able to get arbitrary code execution.
1622520	1628300	What they were able to do was to create a sleep obfuscation technique, but their one relied on a ROP chain.
1628300	1630260	So, I really encourage you to check out their work.
1630260	1631980	I think it is more cool than mine.
1632240	1633520	So, yeah, check it out.
1633520	1646500	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.
1646500	1653640	So, the next question that I wanted to try to answer was what can we actually do while running within an enclave.
1654420	1664560	It turns out that Microsoft really restricts the attack surface within an enclave and they provide them a very, very limited access to APIs.
1664780	1674180	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.
1674180	1679460	So, most traditional malware techniques are not going to be accessible to us.
1680460	1681800	So, what can we do?
1683000	1691620	So, I was able to come up with a few techniques that attackers can still perform even despite all these restrictions.
1691700	1697200	So, the first thing that we can do is we can access the VTL0 memory of our host process.
1697400	1704300	So , the enclave, even though it runs in VTL0, can read and write the normal user mode process.
1704680	1710920	So , this could be interesting in the context where we, for example, we can inject an enclave.
1710920	1715260	So, we create a payload which injects an enclave into a remote process.
1715260	1725520	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.
1728770	1730570	Let's take a look at another demo.
1731070	1737410	So, we have here our Qt program that receives a string and echoes it back to us.
1737410	1739330	So, we enter test, we receive test.
1739530	1744270	We will now write that VBS is awesome and we will see that VBS is in fact awesome.
1744590	1752010	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.
1752330	1755010	So, let's understand what is happening here.
1761170	1768390	So, we have a very advanced piece of malware here, as you can see, that runs within an enclave.
1768390	1780830	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.
1780830	1783210	If it detects it, it replaces it with a good one.
1783290	1786590	So, yeah, a nation state grade malware here.
1787250	1791330	But I wanted to show you this because of the memcpy line specifically.
1791530	1793430	And let's see why it's interesting.
1793430	1797530	So, as we can see, our enclave will perform the memcpy operation.
1797970	1808830	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.
1809850	1814530	And what we would anticipate now is we will see the breakpoint being hit.
1814630	1822750	But if we enter again a string, we will see that even though the memcpy was being performed, the breakpoint is not hit.
1822910	1829350	So, yeah, this happens exactly because of the API evasion that I previously described.
1829570	1836470	And, again, the advantage here is that we can basically interact with the process in a way that cannot be detected.
1837210	1842150	Another cool thing that we can do is we can execute code within VTL0.
1842150	1844990	So, we can't do that.
1844990	1848410	So, we can't directly execute VTL code in VTL1.
1848410	1849510	This is forbidden.
1849510	1854690	If you'll try to take an executable VTL0 address and directly execute it, it will not work.
1854990	1862830	But what we can do is we can remotely invoke a VTL0 code from VTL1.
1862830	1866010	So, this can be performed by using the call enclave API.
1866010	1869310	It turns out that this is a dual use function.
1869670	1875630	So, normally we use it to invoke enclave methods from outside, from the process.
1875630	1880270	But another thing that we can do is invoke VTL0 methods from the enclave.
1880270	1886950	So, essentially we can pass any executable address through this and it will execute the code in VTL0.
1887550	1895150	In terms of evasion, again, it is limited because the code will run in VTL0.
1895150	1901590	But this is a kind of, again, unusual way and we will see in the demo in a minute how it does it.
1907240	1911260	So, here we have our malware here.
1911560	1916180	What it will do is it will allocate a read-write execute buffer in VTL0.
1916300	1921000	And again, like before, we are allocating the buffer but this is the last time that we are going to touch it.
1921220	1923300	So, we have our buffer here.
1923300	1924460	We can see it is empty.
1925800	1929540	And next what we will do is we will pass execution to the enclave.
1929540	1930600	That's it.
1932460	1935940	When we do that, we see that our calculator here popped.
1935940	1937240	Our shellcode executed.
1937240	1940300	If we check the buffer again, we will see that shellcode is there.
1940300	1945420	I left it there for the purpose of this demo but I could have just as well deleted it.
1945580	1953860	And yeah, from the context of an EDL, you see a call enclave function being triggered and then a new thread launching calculator.
1954020	1960200	So , there is no direct connection between the two which can help with evasion.
1963150	1964130	Okay.
1966330	1974450	So, finally, the last little trick that we can do with enclaves that is my personal favorite, has to do with anti-debugging.
1974450	1980570	So, enclaves are in a very advantageous situation where it comes to anti-debugging.
1980570	1988090	In pretty much every case when performing malware analysis, the analysts will use a VTL0 debugger.
1988090	1990630	Either a kernel-mode one or a user-mode one.
1990750	1995310	Now, because of this, the enclave has a lot of power over the debugger.
1995430	2004350	If we can implement an anti-debugging check within an enclave, a debugger will have no way to patch it out and overcome it.
2004350	2006650	So, yeah, this could be a very cool idea.
2007190	2010790	The problem is that it is, again, not going to be that easy.
2010790	2019290	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.
2019770	2025390	So, despite that, I was able to come up with a few techniques that we can perform from the enclave.
2025670	2032070	So, first, we can manually inspect the PEB because we have access to the VTL0 memory.
2032250	2037690	So, if we determine that the process is being debugged through the PEB, we can terminate it from the enclave.
2038930	2046130	Another thing we can do is, while we don't have access to timing-related APIs, we can run inline assembly within the enclave.
2046130	2059890	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.
2060030	2061710	So, yeah, we have some options.
2062610	2066130	Now, how can we make this particularly devastating?
2066310	2077790	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.
2077790	2084810	So, in order to make this particularly devastating, what we can do is we can move critical logic into the enclave.
2084810	2091870	So, preferably something that will be very hard to statically analyze, decryption routine, we can implement, I don't know, a custom VM.
2091950	2093590	You put it within the enclave.
2094610	2098850	And then, from the same call enclave, you also detect the debugger.
2098870	2113370	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.
2113370	2119470	But, as soon as they do that, the enclave can detect the debugger and terminate the process.
2119770	2131170	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.
2131170	2132690	It's not fun.
2133810	2138110	So, in conclusion, VBS is pretty cool.
2138170	2147990	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.
2147990	2152630	As an ex-red teamer, I can say that it suddenly made my life harder.
2153890	2157790	VBS enclaves, in particular, have a lot of potential.
2157790	2163710	I think that we will continue to see more and more applications using them, and that's a good thing.
2164210	2177430	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.
2178190	2178970	That's it.
2178970	2180030	Thank you very much.
2184320	2186340	If you have any questions, I'll be here.
