1 00:00:00,250 --> 00:00:02,610 Hello everyone. 2 00:00:02,630 --> 00:00:05,710 Thank you for coming to our talk, Shimmy What You Got? 3 00:00:06,110 --> 00:00:08,930 Manipulating Shim and Office for Code Injection. 4 00:00:08,930 --> 00:00:11,030 We are very excited to be here. 5 00:00:11,070 --> 00:00:12,570 Let's introduce ourselves. 6 00:00:13,570 --> 00:00:15,630 My name is Ron Ben-Yitzhak. 7 00:00:15,630 --> 00:00:17,810 I'm a security researcher at Deep Instinct. 8 00:00:17,810 --> 00:00:21,590 And this is the third year in a row I'm speaking here at DEF CON. 9 00:00:25,500 --> 00:00:27,900 And my name is David Shandalov. 10 00:00:27,900 --> 00:00:30,360 I'm also a security researcher at Deep Instinct. 11 00:00:30,360 --> 00:00:31,920 And this is my first time here. 12 00:00:37,980 --> 00:00:41,120 This is what we're going to talk about today. 13 00:00:41,120 --> 00:00:48,580 I'll start with an intro about the app compatibility framework, what it is made of, and known techniques to abuse it. 14 00:00:48,580 --> 00:00:53,640 Then I'll dive into our attack surface research on Microsoft Office. 15 00:00:53,640 --> 00:01:04,140 I'll show how we found an interesting RPC method and combined several manipulations into a single attack that achieves both code injection and privilege escalation. 16 00:01:04,520 --> 00:01:07,560 Then we'll show another novel attack. 17 00:01:07,560 --> 00:01:15,080 We'll show the reverse engineering process of the driver and the undocumented structures that make the app compatibility mechanism. 18 00:01:15,080 --> 00:01:19,960 And how we discovered the finest technique to apply a malicious shim on a process. 19 00:01:19,960 --> 00:01:28,280 At the conclusion of the talk, we'll discuss how to detect the attacks shown here and how the community can continue our research. 20 00:01:29,640 --> 00:01:34,260 The app compatibility framework is used for backwards compatibility. 21 00:01:34,260 --> 00:01:40,300 Software developed for old versions of Windows might behave unexpectedly on newer versions. 22 00:01:40,300 --> 00:01:46,100 Microsoft offers many compatibility modes that might look familiar from this window. 23 00:01:46,220 --> 00:01:54,980 Programs might access registry paths or directories that no longer exist, such as the C-documented settings, for those who remember. 24 00:01:55,520 --> 00:02:02,680 Those issues are fixed with API hooking, patching the assembly of the program itself, and other runtime modifications. 25 00:02:03,540 --> 00:02:06,900 Behind the scenes, this is how the mechanism works. 26 00:02:06,900 --> 00:02:14,360 The OS uses shim database files, or SDB files, that contain the fixes and modes various applications require. 27 00:02:14,360 --> 00:02:17,040 The main file is sysmain.sdb. 28 00:02:17,040 --> 00:02:22,120 It is maintained by Microsoft and can only be modified by system updates. 29 00:02:22,180 --> 00:02:28,320 We can view those files using a graphic software provided by Microsoft, the compatibility administrator. 30 00:02:28,320 --> 00:02:35,400 Here we can see that Edge of Empires 3 requires the compatibility mode High DPI Aware. 31 00:02:35,440 --> 00:02:40,900 Users can also install custom SDB files by writing them to these registry paths. 32 00:02:42,120 --> 00:02:46,660 Like every component of the OS, people looked for ways to abuse it. 33 00:02:46,660 --> 00:02:51,680 There were several talks over the years about the malicious implications to the shim framework. 34 00:02:51,680 --> 00:03:00,240 Here you can see a DEF CON talk from almost 10 years ago that shows shims can be used for credential access, network redirection, and more. 35 00:03:00,340 --> 00:03:06,300 Also, Alex UNESCO released several articles about this subject back in 2007. 36 00:03:06,500 --> 00:03:12,400 Because this is such an old attack vector, most security products should detect malicious shim usage. 37 00:03:12,400 --> 00:03:18,960 And the easiest way to do it is by monitoring the registry paths used for installing custom SDB files. 38 00:03:19,820 --> 00:03:25,340 So, once upon a time, David and I wanted to start a new research. 39 00:03:25,340 --> 00:03:29,820 Initially, we thought about bypassing an EDR, or even disabling it. 40 00:03:29,820 --> 00:03:33,340 But apparently, some products just disable themselves. 41 00:03:34,160 --> 00:03:38,740 So, instead of EDR bypass, we decided to start an attack surface research. 42 00:03:38,740 --> 00:03:40,360 And we needed a target. 43 00:03:40,380 --> 00:03:43,700 One of the first choices we came up with is Office. 44 00:03:43,780 --> 00:03:45,960 This software is very popular. 45 00:03:45,960 --> 00:03:48,720 It can be found on almost every Windows machine. 46 00:03:48,720 --> 00:03:50,600 It is also very complex. 47 00:03:50,600 --> 00:03:55,140 It has COM objects, tripped-in engines, cloud synchronization, and more. 48 00:03:55,140 --> 00:03:59,280 The more components there are, the higher the chances to find an issue. 49 00:03:59,460 --> 00:04:03,640 And the most promising feature about Office is the backwards compatibility. 50 00:04:03,900 --> 00:04:06,540 It was released over 30 years ago. 51 00:04:06,540 --> 00:04:11,260 And legacy code is usually the problematic one, which is good for us. 52 00:04:11,820 --> 00:04:14,520 Now, let's talk about the research itself. 53 00:04:15,480 --> 00:04:19,460 We wanted to find RPC servers related to Office. 54 00:04:19,460 --> 00:04:22,960 And that led us to the service ClickToRunSVC. 55 00:04:23,220 --> 00:04:27,180 Here you can see the RPC interfaces it exposes. 56 00:04:27,180 --> 00:04:32,060 And narrowing down this list to files related to Office results in three files. 57 00:04:32,060 --> 00:04:37,320 The actual executable of the service, OfficeClickToRun.exe. 58 00:04:37,320 --> 00:04:45,380 And let's skip the long prefix and just call them virtualization.dll and subsystemcontroller.dll. 59 00:04:46,960 --> 00:04:51,600 We started looking into these files to see what functionality they provide. 60 00:04:51,600 --> 00:04:55,200 And we found an interesting string in the virtualization.dll. 61 00:04:55,360 --> 00:04:58,660 Could not inject subsystem.dll to child process. 62 00:04:59,240 --> 00:05:04,720 And lucky us, this string is used by a function that is exposed through RPC. 63 00:05:04,720 --> 00:05:07,320 This means that you can easily invoke it. 64 00:05:07,340 --> 00:05:16,920 Still, before we start reverse engineering the RPC interface and its methods, we wanted to make sure it actually performs DLL injection. 65 00:05:17,020 --> 00:05:23,580 So we debugged it, put breakpoints on the API used for code injection, and then we launched Word. 66 00:05:23,600 --> 00:05:26,920 That caused the service to call write process memory. 67 00:05:27,100 --> 00:05:30,840 This diagram shows the call stack that led to this API. 68 00:05:30,940 --> 00:05:37,640 The RPC call went through several undocumented functions in a few DLL files until it reached write process memory. 69 00:05:39,120 --> 00:05:41,360 So what is this DLL? 70 00:05:41,360 --> 00:05:47,570 The file description is Microsoft Application Virtualization Client Virtualization Manager Component. 71 00:05:48,040 --> 00:05:51,040 And yes, it says virtualization twice. 72 00:05:51,260 --> 00:05:55,360 This file exposes two undocumented RPC methods. 73 00:05:55,360 --> 00:06:01,740 Instead of diving headfirst into the code, we wanted to find symbols that can shed some light on these functions. 74 00:06:01,960 --> 00:06:07,500 We used PowerShell to find RPC clients, and that led us to a file that has symbols. 75 00:06:08,100 --> 00:06:10,080 subsystem64.dll. 76 00:06:10,080 --> 00:06:12,420 Again, I'm going to skip the long names. 77 00:06:12,420 --> 00:06:17,340 The functions in the client are called notifyNewProcess and notifyNewChildProcess. 78 00:06:17,340 --> 00:06:20,380 We didn't find anything interesting about the first one. 79 00:06:20,380 --> 00:06:24,460 The second function causes the server to call write process memory. 80 00:06:26,500 --> 00:06:30,600 Now we want to understand the flow that leads to the DLL injection. 81 00:06:30,600 --> 00:06:32,900 So we'll start by debugging the client. 82 00:06:33,100 --> 00:06:40,380 We notice that before Word performs the call, it spawns ai.exe as a child process in a suspended state. 83 00:06:40,560 --> 00:06:42,740 Then we reach the call itself. 84 00:06:42,740 --> 00:06:45,840 We reverse-engineered it and came with its definition. 85 00:06:46,020 --> 00:06:48,340 This syntax is for the IDL file. 86 00:06:48,340 --> 00:06:58,020 It sends two parameters, the process ID of ai.exe and the string subsystem64.dll, the name of the DLL that made the call. 87 00:06:58,900 --> 00:07:01,840 Time to switch sides to the RPC server. 88 00:07:01,840 --> 00:07:08,820 Like I said before, those DLL files are undocumented and we want to save time on reverse-engineering them. 89 00:07:08,820 --> 00:07:17,720 We searched for all the files that are related to the app virtualization platform and found out that there are files with similar names in the system32 directory. 90 00:07:17,760 --> 00:07:22,880 Those files have symbols and they show that the detours library is being used. 91 00:07:22,880 --> 00:07:29,740 This is an official library by Microsoft used for hooking and it can also inject code into another process. 92 00:07:29,740 --> 00:07:40,380 The functions detourUpdateProcess.dll and detourCopyPayloadToProcess are used to patch the import address table of the target process and force it to load a DLL. 93 00:07:40,620 --> 00:07:44,600 Comparing the two files with bindif further proves the similarities. 94 00:07:44,780 --> 00:07:51,640 The name primary column belongs to the office file and the name secondary column belongs to the system32 file. 95 00:07:51,640 --> 00:07:54,140 Those functions are a perfect match. 96 00:07:55,200 --> 00:07:59,980 We can easily understand how the injection is done with the source code of detours. 97 00:08:00,000 --> 00:08:07,780 The optional header is modified to point to a new copy of the import table that includes an additional import descriptor to the injected DLL. 98 00:08:07,860 --> 00:08:10,740 This is how WinDebug displays the headers. 99 00:08:10,740 --> 00:08:14,320 We can see the full path to the DLL, which is unusual. 100 00:08:14,420 --> 00:08:16,520 Compilers link the base name. 101 00:08:17,680 --> 00:08:20,700 This is everything done by the RPC server. 102 00:08:20,700 --> 00:08:24,460 Now let's go back to the client and finish going through this process. 103 00:08:24,800 --> 00:08:31,760 Word resumes ai.txt and since the process is not yet initialized, the loader uses the new import table. 104 00:08:31,760 --> 00:08:35,880 Because of that, subsystem64 is loaded into the process. 105 00:08:36,300 --> 00:08:43,180 We looked at what happens if we imitate the RPC call but specify a 32-bit process as the target. 106 00:08:43,180 --> 00:08:48,180 In this case, office click-to-run isn't writing to the memory of this process directly. 107 00:08:48,200 --> 00:08:55,160 It launches the tool mavinject32, which is known as a living off-the-land binary, but the injection is exactly the same. 108 00:08:56,900 --> 00:08:59,300 Now we want to understand exactly... 109 00:08:59,300 --> 00:09:03,700 now that we know exactly how the DLL is injected, what can we gain from it? 110 00:09:03,900 --> 00:09:07,660 Security products detect all kinds of malicious behavior. 111 00:09:07,720 --> 00:09:11,520 File encryption, shellcode execution, and code injection. 112 00:09:11,600 --> 00:09:14,420 But sometimes legitimate programs do that. 113 00:09:14,420 --> 00:09:15,980 We just saw an example. 114 00:09:16,100 --> 00:09:22,700 And to avoid generating false positive events for customers, such programs will be excluded by the products. 115 00:09:22,880 --> 00:09:28,620 This means that if we force office click-to-run to inject a DLL, we'll be able to bypass detection. 116 00:09:29,120 --> 00:09:35,620 We know the definition of the RPC method, and we can imitate it to inject subsystem64.dll. 117 00:09:35,620 --> 00:09:39,880 But the big question is, can we inject another DLL? 118 00:09:40,040 --> 00:09:46,120 As I showed before, the function receives a string, so we can just change the name of the file. 119 00:09:46,140 --> 00:09:51,100 But that means we'll have to write our DLL to the office directory, which is very suspicious. 120 00:09:51,220 --> 00:09:53,580 And it also requires admin privileges. 121 00:09:54,860 --> 00:10:01,020 We tried providing a full path to the file instead of the base name, and this is what happened. 122 00:10:01,080 --> 00:10:07,900 We changed the string to ctemp-injected.dll, and it was appended as it is to the office directory. 123 00:10:07,940 --> 00:10:11,100 This should raise a flag for every security researcher. 124 00:10:11,100 --> 00:10:14,480 It means that there are no sanity checks on the inputs. 125 00:10:14,480 --> 00:10:19,060 So we can go back to the 90s and use directory traversal. 126 00:10:19,060 --> 00:10:22,820 We can manipulate the file lookup to a completely different place. 127 00:10:22,820 --> 00:10:28,960 Let's display the headers of the process again with win-debug, this time after we manipulated the call. 128 00:10:28,960 --> 00:10:34,920 We can see our malicious string was appended to the office directory and written to the memory of the process. 129 00:10:35,900 --> 00:10:39,600 With this trick, we can inject any DLL we want. 130 00:10:39,660 --> 00:10:41,760 But there are still a few limitations. 131 00:10:42,040 --> 00:10:49,460 The modified import address table will be used only if the process was launched as suspended and wasn't resumed until the injection was done. 132 00:10:49,460 --> 00:10:51,460 It must be uninitialized. 133 00:10:51,460 --> 00:10:56,900 Right now, we have a new technique to force a legitimate program to inject a DLL for us. 134 00:10:56,900 --> 00:10:59,520 But what more can we achieve from this? 135 00:10:59,700 --> 00:11:04,820 A nice thing about RPC is that the client can have lower privileges than the server. 136 00:11:05,000 --> 00:11:08,080 The office service runs this anti-authority system. 137 00:11:08,240 --> 00:11:11,120 So, can we perform privilege escalation? 138 00:11:18,850 --> 00:11:21,230 Well, the short answer is no. 139 00:11:21,510 --> 00:11:25,870 The server calls RPC impersonate client before the injection is done. 140 00:11:25,870 --> 00:11:33,310 This means that the thread that handles our request will use our token and not the token of anti-authority system. 141 00:11:34,010 --> 00:11:38,930 This is done to verify that the client has enough permissions to access the remote process. 142 00:11:39,050 --> 00:11:47,230 After the impersonation, the request reaches a subsystem controller.dll which calls open process to gain a handle to the remote process. 143 00:11:47,230 --> 00:11:52,310 If we can't get this handle, so will the RPC server and the call will fail. 144 00:11:52,310 --> 00:11:55,970 But still, maybe we can escalate from admin to system. 145 00:11:56,810 --> 00:12:04,110 The first step to do that will be to launch a process with the privileges of system in a suspended state. 146 00:12:04,110 --> 00:12:10,930 We used API monitoring to find such calls and we found that the task scheduler service does exactly that. 147 00:12:11,170 --> 00:12:20,070 Scheduled tasks are a great target because there are many default ones that are configured to run a system and even some that are related to office. 148 00:12:20,990 --> 00:12:24,510 Such as this one, office automatic updates 2.0. 149 00:12:24,510 --> 00:12:31,470 Our injection will be even less suspicious if the service will inject a DLL to another process that is related to office. 150 00:12:31,470 --> 00:12:33,930 But we still have a few challenges ahead. 151 00:12:34,010 --> 00:12:38,270 The task scheduler service resumes the process shortly after it is launched. 152 00:12:38,270 --> 00:12:42,470 We need to find a way to hold the execution of the task scheduler service. 153 00:12:42,470 --> 00:12:45,810 And we can do that with an opportunistic lock. 154 00:12:47,950 --> 00:12:56,850 Opportunistic locks, or oplocks for short, are a special kind of locks that are used to keep data coherency across computers accessing a file server. 155 00:12:56,910 --> 00:12:58,790 A client can lock a file. 156 00:12:58,790 --> 00:13:02,910 And when another client tries to access the same file, it will wait. 157 00:13:03,070 --> 00:13:06,690 The server will break the lock and notify the first client. 158 00:13:06,770 --> 00:13:13,650 The new data will be flushed, the file will be closed, and only then the second client will be able to access it. 159 00:13:13,650 --> 00:13:19,530 The nice thing about oplocks is that they can be requested from the local file system by a process. 160 00:13:19,710 --> 00:13:25,950 This usage is effectively a semaphore managed by the local file system that synchronizes multiple processes. 161 00:13:27,190 --> 00:13:29,750 So, how does it relate to our attack? 162 00:13:29,910 --> 00:13:41,730 If we could find a file that is accessed by the task scheduler service after the process is created, but before it is resumed, we could lock it, and it will stop the service from resuming the process. 163 00:13:41,730 --> 00:13:45,910 Keeping the process suspended will allow us to inject a DLL to it. 164 00:13:45,910 --> 00:13:55,730 We monitor the system for file access while launching the scheduled tasks, and we saw that sysmain.sdb was accessed. 165 00:13:55,730 --> 00:13:58,190 In this image, we can see the call stack. 166 00:13:58,190 --> 00:14:05,310 Inside createProcessInternal, functions related to the app compatibility were called, and then the shim database was opened. 167 00:14:06,570 --> 00:14:14,810 At this point, we know that the task scheduler sometimes reads sysmain.sdb, but we need to understand which tasks lead to that. 168 00:14:14,810 --> 00:14:19,570 We need to find a task that executes a file with app compatibility settings. 169 00:14:19,570 --> 00:14:28,850 To do that, we locked the sdb file, launched every task registered on the machine, and checked when we were notified about the lock breaking. 170 00:14:28,950 --> 00:14:33,210 This led us to a task that executes Microsoft Edge updates. 171 00:14:33,210 --> 00:14:37,570 This file has app compatibility settings because of the following entry. 172 00:14:37,570 --> 00:14:43,050 The generic installer fix will be applied on every process that has the word update in it. 173 00:14:43,170 --> 00:14:46,510 So, this is good, but still, it's not good enough. 174 00:14:46,510 --> 00:14:51,930 This file is under Program Files x86, which means it is a 32-bit process. 175 00:14:51,930 --> 00:14:54,870 We want to find a target for 64-bit as well. 176 00:14:55,970 --> 00:15:08,530 We continue digging into this subject to understand under which circumstances the sdb file will be read, and we noticed a strange behavior that applies to the OS as a whole, not just the task scheduler service. 177 00:15:08,990 --> 00:15:15,690 The sdb file was read the first time an executable ran since the machine started, even if it didn't need a fix. 178 00:15:15,690 --> 00:15:18,850 And after that, in all other runs, it wasn't. 179 00:15:19,730 --> 00:15:28,030 If we could guarantee that the sdb file will be read any time an executable runs, we could choose any scheduled task as our targets. 180 00:15:28,350 --> 00:15:36,030 And, of course, we could register a custom sdb file that applies some fix to every process, but that is too noisy. 181 00:15:36,030 --> 00:15:39,650 We want to avoid actions that are monitored by security products. 182 00:15:39,930 --> 00:15:43,570 We need to find a way to manage the app compatibility mechanism. 183 00:15:43,830 --> 00:15:48,450 We looked for files related to this mechanism and found apphelp.dll. 184 00:15:48,510 --> 00:15:56,710 By looking at the code of this DLL, we understood that it wraps various operations with the API AntiAppHelpCacheControl. 185 00:15:56,730 --> 00:15:58,950 Let's try to understand how to use it. 186 00:16:00,070 --> 00:16:04,210 This is an undocumented API that is exported by NTDLL. 187 00:16:04,570 --> 00:16:14,130 We searched for references to this API online and in several DLL files until we found that the symbols of combase.dll contain information about it. 188 00:16:14,130 --> 00:16:19,330 This is a screenshot of a function from combase without any reverse engineering. 189 00:16:19,390 --> 00:16:30,690 We can see that the first parameter is an integer that represents the type of operation to be performed, and the second parameter is a structure with various members that are filled based on the operation chosen. 190 00:16:32,490 --> 00:16:38,990 We extracted those definitions and that revealed to us everything that can be done with this API. 191 00:16:39,050 --> 00:16:44,970 We applied the definitions on calls from other DLL files and that helped us understand them better. 192 00:16:45,050 --> 00:16:47,310 Let's look at createProcess, for example. 193 00:16:47,550 --> 00:16:52,970 Now we know that it performs the lookup operation and what information it requires. 194 00:16:53,150 --> 00:16:56,490 Like I said before, we encountered a strange behavior. 195 00:16:56,490 --> 00:17:03,070 The sdb file was read the first time in an executable run since the machine started, even if it didn't need a fix. 196 00:17:03,070 --> 00:17:04,770 And then, it wasn't. 197 00:17:04,770 --> 00:17:10,130 At this point, we had enough information to come up with an assumption on why it happens. 198 00:17:11,370 --> 00:17:14,790 In this scenario, cmd is going to launch calc. 199 00:17:14,790 --> 00:17:20,330 So we have these two processes and also the upheld cache, which is empty at this point. 200 00:17:20,410 --> 00:17:25,090 We know that when we call createProcess, the lookup operation is done on the cache. 201 00:17:25,090 --> 00:17:28,550 So cmd is going to look for an entry about calc. 202 00:17:28,550 --> 00:17:32,430 The cache is empty, so the response is no entry found. 203 00:17:32,430 --> 00:17:39,410 That causes cmd to read the sdb file to check if it contained information about fixes calc requires. 204 00:17:39,410 --> 00:17:41,750 It doesn't require any fix. 205 00:17:41,770 --> 00:17:46,010 So the next step is that the cache is updated and an entry is added. 206 00:17:46,090 --> 00:17:48,110 Calc, no fix needed. 207 00:17:48,450 --> 00:17:55,170 Then, let's say cmd launches calc again, so it queries that cache one more time. 208 00:17:55,430 --> 00:17:58,990 This time, the response is no fix needed. 209 00:17:58,990 --> 00:18:02,910 And that is why cmd doesn't read sysmin.sdb again. 210 00:18:03,070 --> 00:18:09,450 Based on this assumption, if we could remove this entry from the cache, the sdb file will be read again. 211 00:18:10,850 --> 00:18:19,410 We looked at various calls to anti-app-help-cache-controller and found out that both kernel32 and app-help perform the remove operation. 212 00:18:19,410 --> 00:18:22,650 In this pseudocode, we can see the information required. 213 00:18:22,650 --> 00:18:25,390 It requires the path and the handle to the file. 214 00:18:25,790 --> 00:18:32,910 Kernel32 exports a function that does most of the work for us, and it saves us the effort of recreating this call in our code. 215 00:18:33,390 --> 00:18:39,070 After this call, the execution of the file will once again trigger reading the sdb file. 216 00:18:40,330 --> 00:18:43,450 Okay, so we talked about a lot of stuff. 217 00:18:43,450 --> 00:18:46,770 RPC methods, locks, and undocumented API. 218 00:18:46,770 --> 00:18:50,010 So in case you lost track, let's put everything together. 219 00:18:50,110 --> 00:18:51,770 This is the flow of the attack. 220 00:18:51,870 --> 00:18:54,890 We'll start by writing the DLL that will be injected. 221 00:18:55,170 --> 00:18:57,050 Then we'll pick a scheduled task. 222 00:18:57,050 --> 00:18:59,890 Let's say, Office Automatic Updates 2.0. 223 00:18:59,950 --> 00:19:02,330 We need to take care of the shim cache. 224 00:19:02,330 --> 00:19:07,350 The file that will be executed might have an entry, so we're going to remove it. 225 00:19:07,910 --> 00:19:10,490 Then we'll lock sysmin.sdb. 226 00:19:12,010 --> 00:19:18,750 The next step will be to start the task, and that will launch a suspended process with the privileges of system. 227 00:19:19,070 --> 00:19:24,110 The task scheduler will try to read the sdb file and start waiting. 228 00:19:24,330 --> 00:19:31,710 That will send us a notification about the lock breaking, which means it's time to send the RPC request to the office service. 229 00:19:32,250 --> 00:19:38,050 This service will patch the import table of the suspended process and link it to our DLL. 230 00:19:38,210 --> 00:19:46,050 The last step will be to release the lock, and that will cause the task scheduler to resume the process and our DLL will be loaded. 231 00:19:47,810 --> 00:19:49,710 Ready to see if it works? 232 00:19:54,870 --> 00:19:57,570 First, we'll copy our tool to the machine. 233 00:19:57,990 --> 00:19:59,690 And let's even scan it. 234 00:20:01,030 --> 00:20:03,750 And of course, Defender won't detect it. 235 00:20:04,410 --> 00:20:14,870 Now, just to make it clear, we are running the latest version of Office 365, and also the latest Windows 11 build. 236 00:20:16,170 --> 00:20:21,810 Now that we made it clear, let's start monitoring the system and launch our tool. 237 00:20:23,920 --> 00:20:31,620 We can see that Office C to our client loaded our DLL, and that we gained the console with the privileges of... 238 00:20:31,620 --> 00:20:33,640 anti-authority system. 239 00:20:39,630 --> 00:20:41,110 Time to summarize. 240 00:20:41,170 --> 00:20:45,070 We decided to make an attack surface research on Microsoft Office. 241 00:20:45,070 --> 00:20:50,050 We manipulated several components of the OS into a single functioning attack. 242 00:20:50,050 --> 00:20:53,030 And these are the advantages of it. 243 00:20:53,150 --> 00:20:59,930 Security products will most likely ignore code injection done by Office click-to-run, because this is its purpose. 244 00:20:59,970 --> 00:21:01,990 So we'll bypass detection. 245 00:21:01,990 --> 00:21:13,830 It will be difficult to link this attack back to us, because not only the injection is done by another process, but the target process is spawned under the task scheduler, not under us. 246 00:21:14,130 --> 00:21:20,990 We escalate our privileges from admin to system, and the rest of the actions we perform aren't suspicious. 247 00:21:21,190 --> 00:21:28,190 It is very unlikely that someone monitors calls to anti-app-help-cache-control, or monitors using Oprox. 248 00:21:28,830 --> 00:21:31,370 So this is the end of my part of the talk. 249 00:21:31,370 --> 00:21:33,590 Now please welcome David Chandalov. 250 00:21:40,620 --> 00:21:42,100 Thank you, Ron. 251 00:21:42,220 --> 00:21:50,840 As we concluded our first attack surface research, we identified a significant knowledge gap regarding anti-app-help-cache-control. 252 00:21:50,840 --> 00:22:00,080 Despite extensive information available online, we found that crucial details about some data structures and flows remain unclear. 253 00:22:00,500 --> 00:22:10,800 Additionally, with the continuous updates and changes in Windows versions over the years, many publicly shared insights have been altered by Microsoft. 254 00:22:10,840 --> 00:22:16,960 Today, I'll provide a comprehensive overview of the mechanism and the modernized attack. 255 00:22:16,960 --> 00:22:18,940 So, let's dive right in. 256 00:22:19,780 --> 00:22:25,380 As we embarked on our research journey, we were driven by a fundamental question. 257 00:22:25,460 --> 00:22:29,320 What can we achieve with the anti-app-help-cache-control API? 258 00:22:29,460 --> 00:22:36,040 This inquiry became our guiding light, sparking our curiosity and determination to delve deeper. 259 00:22:36,240 --> 00:22:40,840 Our initial exploration gave us a foothold, but we knew we needed more. 260 00:22:40,840 --> 00:22:44,260 We needed to understand the technical intricacies. 261 00:22:44,580 --> 00:22:47,740 After all, this API controls shim fixes. 262 00:22:47,740 --> 00:22:54,020 Maybe we can bend it to our will and control which shim fixes are applied to specific processes. 263 00:22:55,400 --> 00:22:58,040 Our second question was more technical. 264 00:22:58,340 --> 00:23:07,980 As you can see from the screenshots, during the reading of sysmain.sdb, we noticed that anti-app-help-cache-control interacts with the kernel. 265 00:23:08,180 --> 00:23:15,880 This API calls a driver named ahcache.sys, which contains some intriguingly named functions. 266 00:23:15,880 --> 00:23:24,180 Functions like api-lookup and write-to-process and cache-lookup stood out, suggesting deeper mechanisms at play. 267 00:23:24,240 --> 00:23:32,260 These findings hinted at something significant inside of ahcache.sys, something that required us to dig deeper. 268 00:23:32,640 --> 00:23:35,300 So, down the rabbit hole we go. 269 00:23:36,480 --> 00:23:39,200 All right, let's lay the foundation. 270 00:23:39,200 --> 00:23:46,480 To understand how ahcache.sys operates, we need to start with the basics of its initialization process. 271 00:23:46,480 --> 00:23:53,980 When the system boots up, ahcache.sys is loaded, and it requires some initial setup to function properly. 272 00:23:54,080 --> 00:23:57,260 This is where the registry comes into play. 273 00:23:57,820 --> 00:24:03,920 The registry provides essential values that ahcache.sys reads and stores data to. 274 00:24:04,140 --> 00:24:08,640 These values act as a safe location for the driver's operations. 275 00:24:09,660 --> 00:24:15,500 This path contains binary data that the driver sorts into an AVL table. 276 00:24:15,620 --> 00:24:23,060 The cache needs to be properly initialized through the Windows API before these components can start working. 277 00:24:24,140 --> 00:24:28,040 The opposite process happens a shutdown and restart. 278 00:24:28,120 --> 00:24:33,200 The AVL table is dumped into the registry, into the app.compat.cache value. 279 00:24:34,820 --> 00:24:43,620 Let's take a moment to understand the link between sysmain.sdb, registry entries, and the app.compatibility cache. 280 00:24:43,760 --> 00:24:51,580 Each entry in the registry's values data is representing either a file that needs a shim fix or a file that doesn't. 281 00:24:51,640 --> 00:24:59,780 We'll revisit the decision-making process for applying fixes, but for now, let's focus on entries indicating files that need a fix. 282 00:24:59,920 --> 00:25:08,170 These entries contain several fields, the most significant being the exit tag, which points to a specific shim fix. 283 00:25:08,620 --> 00:25:13,880 By examining the data within these entries, we can observe notable differences. 284 00:25:14,140 --> 00:25:20,860 Specifically, free bytes vary across entries, and in the last entry, the free bytes are zeroized. 285 00:25:21,040 --> 00:25:24,020 Is it a sign for a file not needing a fix? 286 00:25:24,380 --> 00:25:31,260 Considering the data's little-endian format, converting it could reveal connections to sysmain.sdb. 287 00:25:33,120 --> 00:25:39,480 With sdbexplorer, we can search for these converted values in a readable version of sysmain.sdb. 288 00:25:39,640 --> 00:25:47,020 Zooming in on the fields that appear in the sdb, we have the name of the file field, and we can see the matching file field. 289 00:25:47,260 --> 00:25:54,040 The matching file field contains just an asterisk, so the whole name field will be compared as it appears. 290 00:25:54,520 --> 00:25:57,540 And lastly, we find the shim reference field. 291 00:25:57,540 --> 00:26:00,440 Each shim reference has a name and an ID. 292 00:26:00,440 --> 00:26:04,100 Some have a command line for additional arguments. 293 00:26:04,300 --> 00:26:07,440 Each field has an hexadecimal number attached. 294 00:26:07,440 --> 00:26:08,420 Why? 295 00:26:09,720 --> 00:26:18,260 It represents the address in the sdb that contains the exit tag as it prefixes the shimfix information block. 296 00:26:19,500 --> 00:26:29,980 Now that we understand what an exit tag is and how the registry cache and sysmain.sdb are linked, we wanted to look under the hood of Ahcache.sys. 297 00:26:30,000 --> 00:26:52,540 We started looking at the dispatch routines for device Ahcache, and we found that, among other things, anti-app-help-cache-control can update the table with hc-api-update that utilizes insert-element-api, lookup a table entry with hc-api-lookup-cdb that uses lookup-element-api, 298 00:26:53,620 --> 00:27:00,380 or it can remove an entry from the table with hc-api-remove using delete-element-api. 299 00:27:00,780 --> 00:27:04,760 We decided to continue looking through the dispatch routines. 300 00:27:06,160 --> 00:27:18,520 As we already have shown, anti-app-help-cache-control uses the Ahc service class enum, but it is also being used in the dispatch routine inside of Ahcache.sys. 301 00:27:18,520 --> 00:27:24,240 The enum has an interestingly sounding member, in its process data for short. 302 00:27:24,240 --> 00:27:27,280 We traced a function that is using this value. 303 00:27:27,940 --> 00:27:32,380 We were surprised to see no mentions of AVL table manipulation. 304 00:27:33,620 --> 00:27:36,040 Maybe this is what we were looking for? 305 00:27:36,040 --> 00:27:38,200 Writing to a process through a kernel driver? 306 00:27:38,200 --> 00:27:39,520 Sounds awesome! 307 00:27:40,460 --> 00:27:45,160 But before we check if we got the good news, let's go through some definitions. 308 00:27:45,160 --> 00:27:49,860 Almost every dispatch routine has a unique structure for its operation. 309 00:27:51,080 --> 00:27:54,840 This structure contains an undocumented data field. 310 00:27:54,840 --> 00:28:03,520 We tried to search the web for definitions, and with some more in-house reverse engineering, we got shim-data. 311 00:28:03,520 --> 00:28:07,760 It contains a fixed magic value and a fixed size value. 312 00:28:07,760 --> 00:28:10,940 Remember this struct, as we will come to visit it later. 313 00:28:12,320 --> 00:28:19,080 Diving deeper inside the function, the calling process and the target process are being checked. 314 00:28:20,000 --> 00:28:21,820 Now here is the tricky part. 315 00:28:21,820 --> 00:28:25,640 We need to have a protected signer app and PPL. 316 00:28:26,260 --> 00:28:29,280 And these two values will be important later. 317 00:28:30,180 --> 00:28:34,120 But the call fails in PSGetProcessProtection. 318 00:28:34,120 --> 00:28:42,780 Looking at every process in Windows 11, we concluded that no Windows apps with this kind of protection exist. 319 00:28:43,180 --> 00:28:45,140 We came to a dead end. 320 00:28:45,520 --> 00:28:50,720 But the undocumented structure I talked about before, it piqued our interest. 321 00:28:51,980 --> 00:28:58,000 We decided to take that strange name we found and try to look for references of this structure. 322 00:28:58,540 --> 00:29:03,480 Maybe we can understand by which operation it is being used and who can be the target. 323 00:29:04,840 --> 00:29:06,760 There were a few hits. 324 00:29:06,760 --> 00:29:16,640 Reverse engineering the functions that reference shim data revealed that this structure represents the shim fixes that need to be applied to a process. 325 00:29:17,960 --> 00:29:20,780 Let's see how a new process is born. 326 00:29:21,860 --> 00:29:28,720 Notepad.exe is the child process that is created on behalf of the parent process cmd.exe. 327 00:29:28,740 --> 00:29:37,160 The parent queries the app help cache to check if the child process requires shim fixes, as we have shown in the first attack. 328 00:29:37,920 --> 00:29:44,000 The parent process reads this main.sdb to retrieve the complete information about the fixes. 329 00:29:45,400 --> 00:29:53,660 The parent builds the structure according to the specific fixes and writes it to the memory of the child process. 330 00:29:55,740 --> 00:30:03,020 The parent modifies the value of the process environment blocks pshim data to point to the new structure. 331 00:30:05,220 --> 00:30:08,920 NTDLL is already loaded in the child process. 332 00:30:10,020 --> 00:30:16,920 So NTDLL's loader loads apphelp.dll if pshim data is not empty. 333 00:30:17,920 --> 00:30:22,060 Apphelp identifies which shim fixes are to be applied. 334 00:30:23,000 --> 00:30:28,000 And so apphelp.dll applies the shim fixes according to the structure. 335 00:30:29,520 --> 00:30:36,560 We also wanted to share the flow of the shim data structure creation for future references. 336 00:30:36,940 --> 00:30:47,060 The flow starts with create process internal in kernel base, goes through kernel 32 and finally ends with apphelp.dll. 337 00:30:47,120 --> 00:30:56,280 The finished struct is built in the sdb pack up compa data function inside of apphelp.dll. 338 00:30:56,740 --> 00:30:58,460 Okay. 339 00:30:58,460 --> 00:31:06,960 Let's consider the implications of manipulating a system component to build and trigger unauthorized fixes. 340 00:31:06,960 --> 00:31:14,560 This could potentially allow us to inject or alter code in a way that bypasses typical security measures. 341 00:31:15,460 --> 00:31:25,860 Our investigation into pshim data begins with understanding what data is being written to this specific part of the process environment block. 342 00:31:27,120 --> 00:31:33,380 We utilized the compatibility administrator to create and apply custom rules. 343 00:31:36,200 --> 00:31:44,320 By extracting the shim data directly from process memory, we gained a first insight into how the data is organized. 344 00:31:45,440 --> 00:31:49,680 We chose to fuzz the inputs to see the effect on the data. 345 00:31:49,680 --> 00:31:54,680 This process helped us identify patterns in the data handling. 346 00:31:55,080 --> 00:32:01,060 By following these steps, we gained a comprehensive understanding of how shim data looks like. 347 00:32:02,220 --> 00:32:12,180 And so, at last, we have reversed engineered the shim data structure, essential for understanding how to manipulate shims to our advantage. 348 00:32:12,960 --> 00:32:17,820 Here we can see the previous version of the shim data, if you can recall. 349 00:32:17,820 --> 00:32:20,380 This is the struct I ask you to remember. 350 00:32:20,400 --> 00:32:28,160 There are quite a few changes, most notably the exit tab type field, the exit tag field, and the exit count field. 351 00:32:28,160 --> 00:32:32,640 All are essential for correct use of shim fix manipulation. 352 00:32:34,400 --> 00:32:39,780 At this stage, we are preparing to use all of our accumulated knowledge for an attack. 353 00:32:39,840 --> 00:32:48,240 We can make a target process load a shim fix simply by building our own shim data struct and pointing to the target pshim data to it. 354 00:32:48,260 --> 00:32:52,720 The process won't check any condition the original rule has configured. 355 00:32:52,820 --> 00:32:56,100 This allows us to apply any fix we want. 356 00:32:56,280 --> 00:33:04,260 We chose to inject a DLL into the target process by itself. 357 00:33:12,530 --> 00:33:20,630 This effectively makes the attack a file-less and register-less DLL injection that can evade detection. 358 00:33:21,910 --> 00:33:33,730 According to this entry, rtvideo.dll will be injected if the process name starts with glj, ends with the temp extension, and it has the correct checksum. 359 00:33:33,730 --> 00:33:39,180 Luckily, these checks are done by the parent process and not by the child process. 360 00:33:39,550 --> 00:33:49,910 This means that once the exit tag in shim data points to this entry, the child process's app.ldll will apply the fix without checking the conditions. 361 00:33:50,990 --> 00:33:54,850 Having said all that, our attack has three limitations. 362 00:33:55,070 --> 00:33:59,090 The target process must be started as a suspended process. 363 00:33:59,830 --> 00:34:12,690 If the injected process executable file resides in this specific path, or the security identifier of the file's owner is a trusted installer, the shim fix won't be loaded. 364 00:34:13,870 --> 00:34:20,230 Injected DLL is not available to 64-bit processes as acgeneral.dll revealed. 365 00:34:20,610 --> 00:34:23,790 And you won't believe it, we found the target. 366 00:34:23,790 --> 00:34:27,250 And it's the Microsoft Edge updater we have shown before. 367 00:34:28,090 --> 00:34:32,990 Microsoft has made our lives easier as all three limitations are not present. 368 00:34:32,990 --> 00:34:34,410 Let's inject. 369 00:34:35,310 --> 00:34:40,690 At this point, we have enough knowledge to abuse the app compatibility mechanism. 370 00:34:40,690 --> 00:34:43,370 So let's go over all of the steps. 371 00:34:43,910 --> 00:34:48,750 We need to write our own DLL as the name of the rule specifies. 372 00:34:49,970 --> 00:34:53,550 We launch our binary as a suspended process. 373 00:34:54,410 --> 00:34:59,190 The struct is built with the correct tag as in our example. 374 00:35:00,650 --> 00:35:04,050 We write the struct to the target process's memory. 375 00:35:04,270 --> 00:35:07,310 We point to the struct. 376 00:35:07,930 --> 00:35:09,890 We resume the execution. 377 00:35:10,690 --> 00:35:14,790 And so app help will help us and apply our desired fix. 378 00:35:14,790 --> 00:35:17,670 An RT video will load to the target process. 379 00:35:17,870 --> 00:35:20,170 Let's see how it looks in action. 380 00:35:23,970 --> 00:35:27,270 shim injector.exe is dropped. 381 00:35:28,730 --> 00:35:34,130 And so we can see the hello message from inside of the child process. 382 00:35:34,510 --> 00:35:37,470 RT video.dll is loaded by our target. 383 00:35:37,630 --> 00:35:42,970 We can see our call stack going from install after init to ns inject dll. 384 00:35:42,970 --> 00:35:45,430 And lastly, to load library. 385 00:35:45,570 --> 00:35:48,310 We also have some logs on the command prompt. 386 00:35:48,310 --> 00:35:54,330 You can see the unique size, magic, exit type and exit tag we defined earlier. 387 00:36:01,420 --> 00:36:03,720 Let's recap our second attack. 388 00:36:03,720 --> 00:36:12,640 There is a long history of shim attacks and how security products typically detect and mitigate malicious shim attacks, attack vectors. 389 00:36:13,360 --> 00:36:19,260 We were able to uncover new definitions and insights on the app compatibility component. 390 00:36:19,260 --> 00:36:23,800 And we used them to change what we think about this attack vector. 391 00:36:24,760 --> 00:36:30,200 This new perspective allowed us to create a new file-less and registry-less attack. 392 00:36:30,940 --> 00:36:41,060 We essentially took care of the problem of creating a custom SDB on a system with all that comes with it, namely registry and disk tracks. 393 00:36:42,680 --> 00:36:56,660 By writing to a child process which is not suspicious and by loading the target dll from the suspended child process before any EDR hook can be established, we effectively bypassed EDR detection. 394 00:36:58,680 --> 00:37:07,220 We chose this specific shim fix because it is applicable to many scenarios and demonstrates our attack in a clear way. 395 00:37:07,480 --> 00:37:11,920 There are many more shim fixes that can be used for malicious purposes. 396 00:37:11,920 --> 00:37:13,780 This was only one of them. 397 00:37:14,940 --> 00:37:16,480 We've come a long way. 398 00:37:16,480 --> 00:37:21,120 Let's summarize both attacks and see what we can take away from this research. 399 00:37:22,520 --> 00:37:28,120 As for detections, let's go over how should someone detect these kinds of attacks. 400 00:37:28,560 --> 00:37:34,300 For the first attack, RPC is a big and early part of the first attack. 401 00:37:34,300 --> 00:37:42,820 So monitoring remote procedure calls made to click-to-run service can reveal the use of non-standard dll paths. 402 00:37:43,300 --> 00:37:50,760 Requests for opportunistic locks on critical system files like sysmain.sdb should be flagged. 403 00:37:52,120 --> 00:38:00,180 Implementing security policies that restrict such high-level memory write operations as it is very suspicious. 404 00:38:00,320 --> 00:38:05,180 Click-to-run service shouldn't write to this type of processes on its own. 405 00:38:06,580 --> 00:38:24,100 Regarding the second attack, keeping an eye on file system activity, particularly for the creation of dll files which are listed in sysmain.sdb and are in directories unrelated to the original files, to the original rules. 406 00:38:25,760 --> 00:38:33,300 Also, we invite the community to take our findings to refine our attacks and make new and better ones. 407 00:38:33,680 --> 00:38:35,520 Some ideas. 408 00:38:35,520 --> 00:38:42,360 The community could find the target for the init process data operation that we didn't find. 409 00:38:43,020 --> 00:38:52,120 There is much more to understand about the shim data structure and how it can affect the execution of the AHCache.sys functions. 410 00:38:53,260 --> 00:39:02,300 There are many more flows that can potentially manipulate the AVL table which in turn will affect which process will get what shim fix. 411 00:39:05,360 --> 00:39:15,280 In conclusion, in our presentation, we detailed our comprehensive methodology for conducting attack surface research. 412 00:39:16,500 --> 00:39:31,420 By leveraging and manipulating various system components, including services, op locks and compatibility mechanisms, we demonstrated how to consolidate these elements into a singular cohesive attack strategy. 413 00:39:32,100 --> 00:39:42,400 The op lock and the compatibility mechanisms could be integral parts of other multi-component sophisticated and complex attacks. 414 00:39:42,480 --> 00:39:53,800 Op lock can be used in many scenarios as a building block and there are many more shim fixes that can be used with malicious intent, even with 64-bit processes. 415 00:39:55,020 --> 00:40:05,260 Our extensive research uncovered new techniques for stealthy injection and privilege escalation, enhancing the effectiveness of these attacks. 416 00:40:06,600 --> 00:40:11,780 We demonstrated two attacks that won't be monitored by EDRs. 417 00:40:11,780 --> 00:40:19,220 The injection occurs in a very early stage, a stage where EDRs can't establish a hook yet. 418 00:40:20,800 --> 00:40:31,400 Through reverse engineering an undocumented API and its associated structures, we gained valuable insights that contributed to the development of these methods. 419 00:40:32,760 --> 00:40:41,880 Additionally , we modernized a previously known malicious technique, transforming it into a more elusive fileless and registryless attack. 420 00:40:42,680 --> 00:40:52,180 Lastly, we once again encourage the community to continue building on our research, exploring the leads we have provided to further advance this field. 421 00:40:53,820 --> 00:40:55,000 Thank you. 422 00:40:55,580 --> 00:40:59,180 Thank you so much for being here and hearing our talk. 423 00:40:59,200 --> 00:41:01,940 The source code of our tools is now public. 424 00:41:01,940 --> 00:41:04,220 If you have any questions, you are free to ask. 425 00:41:04,220 --> 00:41:06,640 You can also find us on Twitter and LinkedIn. 426 00:41:06,680 --> 00:41:07,680 Thanks again.