Encrypting Communications in Malware: A Dive into Windows API Functions with Python

Den B
3 min readAug 7, 2023

--

This article uses Python and ctypes to illustrate the Windows API functions, but in real-world scenarios, malware is often written in languages like C or C++ that natively interface with the Windows API.

Always practice caution when experimenting with these functions and never deploy malware or other malicious code.

Malware often needs to hide its communications to stay undetected. One way to achieve this is by encrypting the data it sends and receives. The Windows API provides several functions that malware can misuse for this purpose. Let’s explore some common ones.

  1. CryptAcquireContext(): Before any encryption can take place, malware needs access to a cryptographic service provider. This function assists in acquiring a handle to one. For instance, if the malware wishes to use the Microsoft Enhanced Cryptographic Provider:

from ctypes import windll, wintypes, byref

hProv = wintypes.HANDLE()
if not windll.advapi32.CryptAcquireContextW(byref(hProv), None, None, 1, 0):
raise WindowsError()

2. CryptCreateHash(): To ensure data integrity, malware might generate a hash. This function initialises a hash object.

hHash = wintypes.HANDLE()
if not windll.advapi32.CryptCreateHash(hProv, 0x8004, 0, 0, byref(hHash)): # CALG_SHA1
raise WindowsError()

3. CryptEncrypt(): The primary function to encrypt data. Let’s say our malware captured a sensitive string and wishes to encrypt it.

data = "SensitiveData".encode()
dataLen = len(data)
if not windll.advapi32.CryptEncrypt(hHash, 0, True, 0, byref(data), byref(dataLen), len(data)):
raise WindowsError()

3.1 CryptGenKey(): For encryption or decryption to occur, you need a cryptographic key. CryptGenKey() is the function that allows malware (or any application) to generate a cryptographic key.

hKey = wintypes.HANDLE()
if not windll.advapi32.CryptGenKey(hProv, 0x00006610, 0, byref(hKey)): # CALG_AES_256
raise WindowsError()

In the code snippet above, the CALG_AES_256 flag indicates that we want a 256-bit AES key.

3.2 CryptDeriveKey(): Instead of randomly generating a key, malware might want to derive a key from a specific password or passphrase. This can be useful if the malware operator and the malware instance both have a shared secret.

hBaseData = wintypes.HANDLE()
password = "SecretPassword".encode()
windll.advapi32.CryptCreateHash(hProv, 0x00008004, 0, 0, byref(hBaseData)) # CALG_MD5
windll.advapi32.CryptHashData(hBaseData, password, len(password), 0)

hDerivedKey = wintypes.HANDLE()
if not windll.advapi32.CryptDeriveKey(hProv, 0x00006610, hBaseData, 0, byref(hDerivedKey)): # CALG_AES_256
raise WindowsError()

Here, we first create a hash of the password using MD5 (although in modern applications, MD5 should be avoided due to vulnerabilities) and then derive an AES key from it.

3.3 CryptDestroyKey(): It’s always a good practice, be it in malware or legitimate applications, to clean up and destroy any cryptographic keys when they are no longer needed.

windll.advapi32.CryptDestroyKey(hKey)
windll.advapi32.CryptDestroyKey(hDerivedKey)

4. CryptDecrypt(): If the malware receives encrypted commands from its command and control server, it’ll need to decrypt them. This function helps in that.

if not windll.advapi32.CryptDecrypt(hHash, 0, True, 0, byref(data), byref(dataLen)):
raise WindowsError()

5. CryptDestroyHash() and CryptReleaseContext(): Once the operations are complete, cleanup is crucial.

windll.advapi32.CryptDestroyHash(hHash)
windll.advapi32.CryptReleaseContext(hProv, 0)

While these functions form the backbone of legitimate secure communications in many applications, their misuse by malware serves as a reminder for cybersecurity professionals to be vigilant.

To detect and counter such threats, understanding the underlying methods and techniques is essential.

Conclusion

The integration of these functions allows malware to carry out sophisticated cryptographic operations, blending seamlessly with regular applications.

As cybersecurity experts, understanding the interplay of these functions and recognising their misuse can give us an edge in the ongoing cyber warfare.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Den B
Den B

Written by Den B

densecurity.tech / IT Support / Software engineer / Application Security Engineer / Reverse Engineer / Bug hunter

No responses yet

Write a response