Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Duplicate Paths Attack: Get Elevated Privilege ...

adr
August 23, 2019

Duplicate Paths Attack: Get Elevated Privilege from Forged Identities

adr

August 23, 2019
Tweet

More Decks by adr

Other Decks in Technology

Transcript

  1. 萬 ⽤ 劫 持 本 地 提 权 情報滲透 越

    級 注 入 PS C:\> [System.Convert]::ToBase64String([Sy ext.Encoding]::UTF8.GetByte cmd.exe /c "dir" 414141414141414141 AAAAAAAAAAAAAAAAAAAAAA [email protected] 遠程後⾨ 網軍⾏動 Duplicate Paths Attack: Get Elevated Privilege from Forged Identities
  2. $_whoami #Windows #Reversing #Pwn #Exploit • Master degree at CSIE,

    NTUST • Security Researcher - chrO.ot • Speaker - BlackHat, DEFCON, VXCON, HITCON • 30cm.tw • Hao's Arsenal • [email protected]
  3. [email protected] 1. UAC Design > Privilege Duplicate > Double Trust

    Auth 2. Issues > Path Normalization 3. A Combo, inject unlimited agents $_cat ./agenda
  4. syscall Ring0 Ring3 Parent Process (A.) CreateProcess Child Process (B.)

    Child Proess Created, EXE File Mapped, Gained the Same Privilege and New Thread pointed to RtlUserThreadStart (C.) Kernel Create a new Thread: RtlUserThreadStart →LdrInitializeThunk →LdrpInitializeProcess (D.) Jump into AddressOfEntry
  5. AppInfo!RAiLaunchAdminProcess syscall Ring0 Ring3 Parent Process (A.) RunAs, CreateProcessAsUser or

    CreateProcessWithToken UAC Service (B.) Send a task by RPC message to UAC service for creating a different privilege child process RPC Priv Auth (C.) verify new process is qualified or not Child Process Task Cancelled Y N (D.) Child process is created by CreateProcessAsUser with specific token by Parent Process
  6. syscall Ring0 Ring3 Parent Process (A.) RunAs, CreateProcessAsUser or CreateProcessWithToken

    (B.) Send a task by RPC message to UAC service for creating a different privilege child process RPC (C.) verify new process is qualified or not Child Process Task Cancelled Y N (D.) Child process is created by CreateProcessAsUser with specific token by Parent Process AppInfo!RAiLaunchAdminProcess UAC Service Priv Auth UAC Protection Logic
  7. Some points about UAC protection we're interested in: • How

    the UAC process verifies processes get higher privilege • Security issues • Bypassing Vectors syscall Ring0 Ring3 Parent Process (A.) RunAs, CreateProcessAsUser or CreateProcessWithToken UAC Service (B.) Send a task by RPC message to UAC service for creating a different privilege child process RPC Priv Auth (C.) verify new process is qualified or not Child Process Task Cancelled Y N (D.) Child process is created by CreateProcessAsUser with specific token by Parent Process AppInfo!RAiLaunchAdminProcess
  8. if you can see me, remember it's discovered by reversing

    and not talked about on Internet. I have no idea it's correct or not :/
  9. [email protected] void __fastcall RAiLaunchAdminProcess( struct _RPC_ASYNC_STATE *rpcStatus, RPC_BINDING_HANDLE rpcBindingHandle, wchar_t

    *exePath, wchar_t *fullCommand, int dwCreationFlags, LPVOID lpEnvironment, wchar_t *lpCurrentDirectory, unsigned __int16 *a8, struct _APPINFO_STARTUPINFO *lpStartupInfo, __int64 a10, int millSecond, struct _PROCESS_INFORMATION *lpProcessInformation, unsigned int *a13)
  10. [email protected] void AipCheckSecurePFDirectory(_UNICODE_STRING *exePath, uint *trustedFlag, __int64 caseSenstive) { *trustedFlag

    |= 0x2000u; int pos = 0; do { // \??\C:\Program Files\Windows Defender, \??\C:\Program Files (x86)\Windows Defender // \??\C:\Program Files\Windows Journal, \??\C:\Program Files (x86)\Windows Journal // \??\C:\Program Files\Windows Media Player, \??\C:\Program Files (x86)\Windows Media Player // \??\C:\Program Files\Windows Multipoint Server if ( RtlPrefixUnicodeString(&(&g_IncludedPF)[2 * pos], exePath, caseSenstive = true) ) break; ++pos; } while ( pos < 8 ); if ( pos != 8 ) *trustedFlag_1 |= 0x4000u; // 0x4000, trusted windows system application } $_./trustAuth_A
  11. [email protected] // \??\C:\Windows\System32\Sysprep\sysprep.exe // \??\C:\Windows\System32\inetsrv\InetMgr.exe int index = 0, pos

    = 0; while ( !RtlEqualUnicodeString( g_IncludedXmtExe[index ++], exeUni_FullPath, true) ); if (index < 2) { if ( AipMatchesOriginalFileName(exeUni_FullPath) ) { tmpTrustFlagToAdd |= (0x800000u | 0x200000u); trustedFlag = tmpTrustFlagToAdd; } else { tmpTrustFlagToAdd |= (0x400000u | 0x200000u); trustedFlag = tmpTrustFlagToAdd; } } else ... $_./trustAuth_A
  12. [email protected] else { // \??\C:\Windows\SysWow64\ // \??\C:\Windows\System32\ for (pos =

    0; pos < 2; pos++) if ( RtlPrefixUnicodeString(g_IncludedSysDir[pos], exeUni_FullPath, true) ) break; if (pos != 2) { tmpTrustFlagToAdd |= 0x200000u; trustedFlag = tmpTrustFlagToAdd; } } $_./trustAuth_A
  13. [email protected] filemappingPtr = CreateFileMappingW(exeFileHandle, 0i64, 0x11000002, 0, ...); if (

    filemappingPtr ) { exeRawData = MapViewOfFile(filemappingPtr, 4u, 0, 0, 0i64); if ( exeRawData ) if ( LdrResSearchResource(exeRawData, &buf, 3i64, 48i64 ..., 64) >= 0 ) { actCtx = CreateActCtxW(&Dst); if ( actCtx != -1i64 ) { if ( QueryActCtxSettingsW( 0, actCtx, 0i64, L"autoElevate", &pvBuffer, ...) ) // pvBuffer = (wchar_t*)L"true" // tryAutoElevFlag = ( 't' - 'T'(0x54) & 0xffdf ) == 0 --> case insentive tryAutoElevFlag = ((pvBuffer - 'T') & 0xFFDF) == 0; ... if ( tryAutoElevFlag ) goto markedAutoElev; $_./trustAuth_B
  14. [email protected] tryToVerify: ... tryAutoElevFlag = false; filemappingPtr = CreateFileMappingW(exeFileHandle, 0i64,

    0x11000002, 0, ...); if ( filemappingPtr ) { exeRawData = MapViewOfFile(filemappingPtr, 4u, 0, 0, 0i64); if ( exeRawData ) if ( LdrResSearchResource(exeRawData, &buf, 3i64, 48i64 ..., 64) >= 0 ) { actCtx = CreateActCtxW(&Dst); if ( actCtx != -1i64 ) { if ( QueryActCtxSettingsW( 0, actCtx, 0i64, L"autoElevate", &pvBuffer, ...) ) // pvBuffer = (wchar_t*)L"true" // tryAutoElevFlag = ( 't' - 'T'(0x54) & 0xffdf ) == 0 --> case insentive tryAutoElevFlag = ((pvBuffer - 'T') & 0xFFDF) == 0; ... if ( tryAutoElevFlag ) goto markedAutoElev; markedAutoElev: if ( _wcsicmp(L"mmc.exe", *mmc) ) { // autoElev request marked flag *trustFlag |= 0x1010000u; goto bye; } // ... chk for the arguments for mmc $_./trustAuth_B
  15. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput)
  16. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput)
  17. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput)
  18. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput)
  19. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput)
  20. [email protected] > TenableSecurity: UAC Bypass by Mocking Trusted Directories by

    David Wells > Google Project Zero: The Definitive Guide on Win32 to NT Path Conversion by James Forshaw > MSDN Developer Blog: Path Normalization by Jeremy Kuhne Path Format Overview by Jeremy Kuhne /?path_Normaliz
  21. [email protected] /?path_Format DOS Paths (2.0) C:\Test\Foo.txt A full volume name.

    If it doesn't start with all 3 characters it is considered to be partially qualified or relative to the current directory.
  22. [email protected] /? UNC Paths \\Server\Share\Test\Foo.txt start with two separators. The

    first component is the host name (server), which is followed by the share name. path_Format
  23. [email protected] • Identifying the Path and Legacy Devices • Applying

    the Current Directory • Canonicalizing Separators • Evaluating Relative Components • Trimming Characters • Skipping Normalization /?path_Normaliz Path Normalization by Jeremy Kuhne
  24. [email protected] • Identifying the Path and Legacy Devices • Applying

    the Current Directory • Canonicalizing Separators • Evaluating Relative Components • Trimming Characters • Skipping Normalization /?path_Normaliz Path Normalization by Jeremy Kuhne If the path doesn't end in a separator, all trailing periods and \x20 will be removed. If the last segment is simply a single or double period it falls under the relative components rule above. This rule leads to the possibly surprising ability to create a directory with a trailing space. You simply need to add a trailing separator to do so.
  25. [email protected] • Identifying the Path and Legacy Devices • Applying

    the Current Directory • Canonicalizing Separators • Evaluating Relative Components • Trimming Characters • Skipping Normalization /?path_Normaliz Path Normalization by Jeremy Kuhne An important exception- if you have a device path that begins with a question mark instead of a period. It must use the canonical backslash- if the path does not start with exactly \\?\ it will be normalized.
  26. [email protected] NtOpenProcess RpcImpersonateClient High Priv Low Priv NtDuplicateToken(-2) RpcRevertToSelf RpcImpersonateClient

    ExeFileHandle = CreateFileW $p = ToDosName(GetLongPathNameW(pathInput)) TrustAuth_A($p) TrustAuth_B($p) RpcRevertToSelf AiLaunchConsentUI AiLaunchProcess(pathInput) /?Issue
  27. [email protected] RtlDosPathNameToRelativeNtPathName_U_WithStatus( GetLongPathNameW(L"C:\Windows \System32\a.exe") ) RtlDosPathNameToRelativeNtPathName_U_WithStatus( L"C:\Windows\System32\a.exe" ) $p =

    L"\??\C:\Windows\System32\a.exe" /?trustAuth_A AiLaunchProcess(L"C:\Windows \System32\a.exe") We have no privilege to write files inside C:\Windows\System32 due to Windows DACL But it's okay for us to create a dictionary "Windows\x20" via the \\?\ prefix to avid Path Normalization
  28. [email protected] • TrustAuth_A - Path Normalization Issues → \\?\ prefix

    to bypass • TrustAuth_B - Whitelisted EXE Files with Trusted Signature - AutoElevated Marked EXE Files → DLL Side-Loading Tricks to hijack AutoElevated Marked EXE Files /?attack Vectors
  29. [email protected] • TrustAuth_A used for verifying child process launched from

    a trustable dircctory • if trusted, TrustAuth_B check child process signed with legal signature or marked as AutoElevate • Consent.exe launched, and the UAC prompt pops up if child process isn't full trusted • TrustAuth_A/B is an extra design. The different paths between verification and Forking Process lead to EoP $_./Recap
  30. 萬 ⽤ 劫 持 本 地 提 权 情報滲透 越

    級 注 入 PS C:\> [System.Convert]::ToBase64String([Sy ext.Encoding]::UTF8.GetByte cmd.exe /c "dir" 414141414141414141 AAAAAAAAAAAAAAAAAAAAAA [email protected] 遠程後⾨ 網軍⾏動 Thanks! Slide Github @aaaddress1 Facebook