Esteemaudit - Windows Smart Card Authentication Exploitation | Lucideus Research

Introduction
 In April, a group known as the “Shadow Brokers” leaked multiple tools to exploit vulnerabilities in various versions of Microsoft Windows. A tool released in this dump is “EsteemAudit”, which exploits CVE-2017-9073, a vulnerability in the Windows Remote Desktop system on Windows XP and Windows Server 2003.
The vulnerability exploited by this attack is related to Smart Card authentication used when logging onto the system via the RDP service.

Vulnerability
 Smart Card logon is supported by all Windows versions after Windows 2000. It contains a chip that stores the user logon information, along with the private key and public certificate key. By inserting it into a Smart Card Reader attached to the computer, and typing in a Personal Identification Number (PIN), a user can securely log onto the Windows system. When logon occurs via the RDP service, the remote machine running the RDP service communicates with the local computer, which then connects to the Smart Card Reader, requests the information in the Smart Card, and verifies the PIN.

The vulnerability is located in the "MyCPAcquireContext()" function in "gpkcsp.dll", which is called by "winlogon.exe" in the new windows session.  The "MyCPAcquireContext()" function is used to set up the Windows Cryptographic Service Providers (CSP) context. It reads the data from the Smart Card and sets the value of the fields of the CSP context structure. If the data read from the Smart Card is overlarge, the field buffer used by CSP context structure overflows and overwrites another field, eventually enabling arbitrary code execution.

Lab Setup
Target Machine: Windows Server 2003 and Windows XP
Target Port: 3389
Target Application: winlogon.exe
Target Component: gpkcsp.dll

Attacker Machine: Kali Linux

Overview

EsteemAudit uses heap overflow in an internal structure on the system heap allocated by gpkcsp.dll, which is a component of Windows Smart Card.

  • There is 0x80 sized buffer (named key_data) in the key_set structure to store smart card information, after which there are two key_object pointers in adjacent memory.
  • There is a call to memcpy in gpkcsp!MyCPAcquireContext with no boundary check, copying the entire user-controlled sized data to the location of 0x80 sized key_data.
  • If the attacker puts more than 0x80 sized data as the source argument of memcpy, the key_object pointer adjacent with key_data will be overflowed.
  • To exploit this, the EsteemAudit code puts the 0xb2-7 size controlled data as the source argument of memcpy, and overflowed key_object pointer with a fixed address 0x080190dc, which is an address of data section of gpkcsp.dll.
  • After triggering the memcpy path to complete the overflow, the exploiter puts user-controlled data in that global variable at a fixed address 0x080190d8 in data section, and then triggers gpkcsp!ReleaseProvider to release the C++ object key_object to get control over EIP. Finally, the SharedUserData technique is used to call VirtualProtect by syscall with number 0x8f and the first stage shellcode is executed.
  • The exploit uses the SharedUserData technique to call KiFastSystemCall to execute VirtualProtect and make the memory 0x080180d8 writable and executable, and to execute the shellcode at address 0x08019148. At this point the exploit has completed the first stage.


Heap Overflow
A heap overflow is a type of buffer overflow that occurs in the heap data area. Heap overflows are exploitable in a different manner to that of stack-based overflows. Memory on the heap is dynamically allocated by the application at run-time and typically contains program data. Exploitation is performed by corrupting this data in specific ways to cause the application to overwrite internal structures such as linked list pointers.

gpkcsp!MyCPAquireContext
The "MyCPAcquireContext()" function is used to set up the Windows Cryptographic Service Providers (CSP) context. It reads the data from the Smart Card and sets the value of the fields of the CSP context structure. If the data read from the Smart Card is overlarge, the field buffer used by CSP context structure overflows and overwrites another field, eventually enabling arbitrary code execution.

gpkcsp.dll
Gemplus Cryptographic Service Provider.
gpkcsp.dll is statically linked to the following files:
  • Msvcrt.dll
  • WinSCard.dll
  • SCARDDLG.dll
  • USER32.dll
  • ADVAPI32.dll
  • KERNEL32.dll
This means that when gpkcsp.dll is loaded, the above files are automatically loaded too. If one of these files is corrupted or missing, gpkcsp.dll won't be loaded.

Exported Functions List
The following functions are exported by this dll:
CPAcquireContext CPCreateHash CPDecrypt CPDeriveKey
CPDestroyHash CPDestroyKey CPEncrypt CPExportKey
CPGenKey CPGenRandom CPGetHashParam CPGetKeyParam
CPGetProvParam CPGetUserKey CPHashData CPHashSessionKey
CPImportKey CPReleaseContext CPSetHashParam CPSetKeyParam
CPSetProvParam CPSignHash CPVerifySignature DllMain
DllRegisterServer DllUnregisterServer



KiFastSystemCall
  • When KiFastSystemCall is invoked, there are two return addresses between the stack pointer and the function parameters.
  • Because SharedUserData is non-writable, the only other place we can target is KiFastSystemCall which is 5 byte.
  • The last byte, 0xC3 (retn), is needed by KiFastSystemCallRet and cannot be modified, which leaves only 4 writable bytes.
  • The sysenter instruction is supported by all modern CPUs and is the fastest way to enter the kernel.
  • KiIntSystemCall has 7 writable bytes, it’s also within short jump range of KiFastSystemCall. We can do a 2 byte short jump from KiFastSystemCall to KiIntSystemCall and then a 32-bit jump from within KiIntSystemCall.

VirtualProtect Function

  • Changes the protection on a region of committed pages in the virtual address space of the calling process.
  • Microsoft says to change the access protection of any process, use the VirtualProtectEx function.
  • Microsoft says it is best to avoid using VirtualProtect to change page protections on memory blocks allocated by GlobalAlloc, HeapAlloc, or LocalAlloc, because multiple memory blocks can exist on a single page. The heap manager assumes that all pages in the heap grant at least read and write access.


Detection and Mitigation
As CVE-2017-9073 only exists on Windows Server 2003 and Windows XP, both of which are no longer supported by Microsoft, users should first consider upgrading to a newer version of Windows as no official patch is available. However, as this vulnerability exists in the smart card module gpkcsp, there are potential work-arounds.

  • Disabling the smart card module through Group Policy or in the registry.
    Do this in the registry: Set/Add key fEnableSmartCard in the path HKLM\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\ to 0 with the type of REG_DWORD.
  • Wherever possible, disable or restrict access to RDP from external sources.



References

https://blog.fortinet.com/2017/05/11/deep-analysis-of-esteemaudit
https://github.com/BlackMathIT/Esteemaudit-Metasploit
https://researchcenter.paloaltonetworks.com/2017/05/unit42-dissection-esteemaudit-windows-remote-desktop-exploit/
https://en.wikipedia.org/wiki/Heap_overflow
https://xpdll.nirsoft.net/gpkcsp_dll.html
https://www.malwaretech.com/2015/04/intercepting-all-system-calls-by.html
https://www.malwaretech.com/2014/06/usermode-system-call-hooking-betabo.html

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx

No comments:

Powered by Blogger.