Skip to content

Commit 6af3280

Browse files
committed
feat(metsrv): refactor CryptAcquireContext calls for better compatibility
1 parent 1ba3b59 commit 6af3280

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

c/meterpreter/source/metsrv/packet_encryption.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
#include "remote.h"
33
#include "packet_encryption.h"
44

5+
// The provider name strings below (MS_ENH_RSA_AES_PROV, MS_ENHANCED_PROV) are
6+
// TCHAR — LPCWSTR under UNICODE (MSVC vcxproj build), LPCSTR under the mingw
7+
// docker build. Route through the matching A/W wrapper so both toolchains build.
8+
#ifdef UNICODE
9+
#define WINAPI_CryptAcquireContext_TCHAR met_api->win_api.advapi32.CryptAcquireContextW
10+
#else
11+
#define WINAPI_CryptAcquireContext_TCHAR met_api->win_api.advapi32.CryptAcquireContextA
12+
#endif
13+
514
typedef struct _CryptProviderParams
615
{
716
const TCHAR* provider;
@@ -353,10 +362,10 @@ DWORD public_key_encrypt(BYTE* publicKeyDer, UINT publicKeyDerLen, BYTE* data, D
353362

354363
dprintf("[ENC] Key algo: %s", pubKeyInfo->Algorithm.pszObjId);
355364

356-
if (!met_api->win_api.advapi32.CryptAcquireContextA(&rsaProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
365+
if (!WINAPI_CryptAcquireContext_TCHAR(&rsaProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
357366
{
358367
dprintf("[ENC] Failed to create the RSA provider with CRYPT_VERIFYCONTEXT");
359-
if (!met_api->win_api.advapi32.CryptAcquireContextA(&rsaProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
368+
if (!WINAPI_CryptAcquireContext_TCHAR(&rsaProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
360369
{
361370
result = GetLastError();
362371
dprintf("[ENC] Failed to create the RSA provider with CRYPT_NEWKEYSET: %u (%x)", result, result);
@@ -490,7 +499,7 @@ DWORD create_enc_ctx_from_key(Remote* remote, LPBYTE key, DWORD keySize)
490499

491500
for (int i = 0; i < _countof(AesProviders); ++i)
492501
{
493-
if (!met_api->win_api.advapi32.CryptAcquireContextA(&ctx->provider, NULL, AesProviders[i].provider, AesProviders[i].type, AesProviders[i].flags))
502+
if (!WINAPI_CryptAcquireContext_TCHAR(&ctx->provider, NULL, AesProviders[i].provider, AesProviders[i].type, AesProviders[i].flags))
494503
{
495504
result = GetLastError();
496505
dprintf("[ENC] failed to acquire the crypt context %d: %d (%x)", i, result, result);
@@ -553,7 +562,7 @@ DWORD request_negotiate_aes_key(Remote* remote, Packet* packet)
553562

554563
for (int i = 0; i < _countof(AesProviders); ++i)
555564
{
556-
if (!met_api->win_api.advapi32.CryptAcquireContextA(&ctx->provider, NULL, AesProviders[i].provider, AesProviders[i].type, AesProviders[i].flags))
565+
if (!WINAPI_CryptAcquireContext_TCHAR(&ctx->provider, NULL, AesProviders[i].provider, AesProviders[i].type, AesProviders[i].flags))
557566
{
558567
result = GetLastError();
559568
dprintf("[ENC] failed to acquire the crypt context %d: %d (%x)", i, result, result);

0 commit comments

Comments
 (0)