// use LocalFree() to free the result
HRESULT GetBase64StringFromStream(IStream *pstm, PWSTR *ppszBase64)
{
    *ppszBase64 = NULL;

    BYTE *pdata;
    UINT cBytes;
    HRESULT hr = IStream_ReadToBuffer(pstm, 256 * 1024, &pdata, &cBytes);
    if (SUCCEEDED(hr))
    {
        DWORD dwStringSize = 0; // requested size here
        hr = CryptBinaryToString(pdata, cBytes, CRYPT_STRING_BASE64, NULL, &dwStringSize) ? S_OK : E_FAIL;
        if (SUCCEEDED(hr))
        {
            *ppszBase64 = (PWSTR)LocalAlloc(LPTR, dwStringSize * sizeof(**ppszBase64));
            hr = *ppszBase64 ? S_OK : E_OUTOFMEMORY;
            if (SUCCEEDED(hr))
            {
                hr = CryptBinaryToString(pdata, cBytes, CRYPT_STRING_BASE64, *ppszBase64, &dwStringSize) ? S_OK : E_FAIL;
                if (FAILED(hr))
                {
                    LocalFree(*ppszBase64);
                    *ppszBase64 = NULL;
                }
            }
        }
        LocalFree(pdata);
    }
    return hr;
}
Beispiel #2
0
BOOL kull_m_file_writeData(PCWCHAR fileName, PBYTE data, DWORD lenght)
{
	BOOL reussite = FALSE;
	DWORD dwBytesWritten = 0, i;
	HANDLE hFile = NULL;
	LPWSTR base64;

	if (isBase64Intercept)
	{
		if (CryptBinaryToString(data, lenght, CRYPT_STRING_BASE64, NULL, &dwBytesWritten))
		{
			if (base64 = (LPWSTR)LocalAlloc(LPTR, dwBytesWritten * sizeof(wchar_t)))
			{
				if (reussite = CryptBinaryToString(data, lenght, CRYPT_STRING_BASE64, base64, &dwBytesWritten))
				{
					kprintf(L"\n===================\nBase64 interception\n===================\n");
					for (i = 0; i < dwBytesWritten; i++)
						kprintf(L"%c", base64[i]);
					kprintf(L"===================\n");
				}
				LocalFree(base64);
			}
		}
	}
	else if ((hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) && hFile != INVALID_HANDLE_VALUE)
	{
		if (WriteFile(hFile, data, lenght, &dwBytesWritten, NULL) && (lenght == dwBytesWritten))
			reussite = FlushFileBuffers(hFile);
		CloseHandle(hFile);
	}
	return reussite;
}
// convert binary signature to hex string
// pointer should be freed after use
PBYTE sig2hex(void)
{
    DWORD len = 0;
    PBYTE hex;
    // determine how much space we need
    CryptBinaryToString(pbSignature, dwSigLen,
                        CRYPT_STRING_HEXRAW | CRYPT_STRING_NOCRLF, NULL, &len);
    // allocate memory
    hex = xmalloc(len);
    // get the string
    CryptBinaryToString(pbSignature, dwSigLen,
                        CRYPT_STRING_HEXRAW | CRYPT_STRING_NOCRLF, hex, &len);
    // return it (ensure pointer is freed after being used)
    return hex;
}
PTCHAR GetBase64FromByte(_In_ PBYTE pbData, _In_ DWORD dwDataSize)
{
	BOOL bRes = FALSE;
	DWORD pcchString = 0;
	PTCHAR tBase64Str = NULL,tBase64StrBkp = NULL;

	if (!pbData)
		return NULL;

	if (dwDataSize == 0)
	{
		tBase64Str = (PTCHAR) HeapAlloc(hCrawlerHeap, HEAP_ZERO_MEMORY, sizeof(TCHAR)); 
		tBase64Str[0] = '\0';
		return tBase64Str;
	}

	CryptBinaryToString(pbData, dwDataSize, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , NULL, &pcchString);
	if (!pcchString)
	{
		DEBUG_LOG(D_ERROR, "Unable to determine base64String length (ErrCode=%d).\r\n.", GetLastError());
		DoExit(D_ERROR);
	}
	else
	{
		tBase64Str = (PTCHAR) HeapAlloc(hCrawlerHeap, HEAP_ZERO_MEMORY, sizeof(TCHAR) * pcchString); 
		if (!tBase64Str)
		{
			DEBUG_LOG(D_ERROR, "Unable to allocate memory (ErrCode=%d).\r\n.", GetLastError());
			DoExit(D_ERROR);
		}
		tBase64StrBkp = tBase64Str;
	}

	if (CryptBinaryToString(pbData, dwDataSize, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF , tBase64Str, &pcchString) != TRUE)
	{
		DEBUG_LOG(D_ERROR, "Unable to convert into base64 (ErrCode=%d).\r\n.", GetLastError());
		DoExit(D_ERROR);
	}

	return tBase64Str;
}
Beispiel #5
0
BOOL CreateSignatureHMACSHA1(wstring src, wstring secretKey, wstring& signature)
{
	signature = L"";

	string hashSrc = "";
	string hashKey = "";
	WtoUTF8(src, hashSrc);
	WtoUTF8(secretKey, hashKey);

	HCRYPTPROV cryptProv = NULL;
	HCRYPTHASH cryptHash = NULL;
	BYTE key[65];
	DWORD keySize = 0;
	BYTE ipad[65];
	BYTE opad[65];
	ZeroMemory(key, 65);
	ZeroMemory(ipad, 65);
	ZeroMemory(opad, 65);

	BYTE* firstHash = NULL;
	DWORD firstHashSize = 0;
	BYTE* secondHash = NULL;
	DWORD secondHashSize = 0;

	WCHAR* base64 = NULL;
	DWORD base64Size = 0;

	if ( CryptAcquireContext(&cryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE ){
		goto Err_End;
	}

	if( hashKey.size() > 64 ){
		if ( CryptCreateHash(cryptProv, CALG_SHA1, 0, 0, &cryptHash) == FALSE ){
			goto Err_End;
		}
		if( CryptHashData(cryptHash, (BYTE*)hashKey.c_str(), (DWORD)hashKey.length(), 0) == FALSE ){
			goto Err_End;
		}
		if( CryptGetHashParam(cryptHash, HP_HASHVAL, NULL, &keySize, 0 ) == FALSE ){
			goto Err_End;
		}
		if( CryptGetHashParam(cryptHash, HP_HASHVAL, key, &keySize, 0 ) == FALSE ){
			goto Err_End;
		}
		CryptDestroyHash(cryptHash);
		cryptHash = NULL;
	}else{
		keySize = (DWORD)hashKey.size();
		memcpy(key, hashKey.c_str(), keySize);
	}
	memcpy(ipad, key, keySize);
	memcpy(opad, key, keySize);
	for( int i=0; i<64; i++ ){
		ipad[i] ^= 0x36;
		opad[i] ^= 0x5c;
	}

	if ( CryptCreateHash(cryptProv, CALG_SHA1, 0, 0, &cryptHash) == FALSE ){
		goto Err_End;
	}
	if( CryptHashData(cryptHash, ipad, 64, 0) == FALSE ){
		goto Err_End;
	}
	if( CryptHashData(cryptHash, (BYTE*)hashSrc.c_str(), (DWORD)hashSrc.size(), 0) == FALSE ){
		goto Err_End;
	}
	if( CryptGetHashParam(cryptHash, HP_HASHVAL, NULL, &firstHashSize, 0 ) == FALSE ){
		goto Err_End;
	}
	firstHash = new BYTE[firstHashSize];
	if( CryptGetHashParam(cryptHash, HP_HASHVAL, firstHash, &firstHashSize, 0 ) == FALSE ){
		goto Err_End;
	}
	CryptDestroyHash(cryptHash);
	cryptHash = NULL;

	if ( CryptCreateHash(cryptProv, CALG_SHA1, 0, 0, &cryptHash) == FALSE ){
		goto Err_End;
	}
	if( CryptHashData(cryptHash, opad, 64, 0) == FALSE ){
		goto Err_End;
	}
	if( CryptHashData(cryptHash, firstHash, firstHashSize, 0) == FALSE ){
		goto Err_End;
	}
	if( CryptGetHashParam(cryptHash, HP_HASHVAL, NULL, &secondHashSize, 0 ) == FALSE ){
		goto Err_End;
	}
	secondHash = new BYTE[secondHashSize];
	if( CryptGetHashParam(cryptHash, HP_HASHVAL, secondHash, &secondHashSize, 0 ) == FALSE ){
		goto Err_End;
	}
	CryptDestroyHash(cryptHash);
	cryptHash = NULL;

	//Base64
	if( CryptBinaryToString( secondHash, secondHashSize, CRYPT_STRING_BASE64, NULL, &base64Size ) == FALSE){
		goto Err_End;
	}
   base64 = new WCHAR[ base64Size + 1 ];
   if( CryptBinaryToString( secondHash, secondHashSize, CRYPT_STRING_BASE64, base64, &base64Size ) == FALSE ){
		goto Err_End;
   }
   signature = base64;
   //最後に\r\n入ってしまうみたいなので除去
   Replace(signature, L"\r\n", L"");

Err_End:
	SAFE_DELETE_ARRAY(base64);
	SAFE_DELETE_ARRAY(secondHash);
	SAFE_DELETE_ARRAY(firstHash);

	if( cryptHash !=NULL ){
		CryptDestroyHash(cryptHash);
	}
	if( cryptProv != NULL ){
		CryptReleaseContext(cryptProv, 0);
	}

	if( signature.size() > 0 ){
		return TRUE;
	}else{
		return FALSE;
	}
}
// Fills dest symbolic links and unique IDs of mounted storage devices
void getStoragesInfo(struct StoragesInfo* storagesInfo)
{
    HANDLE hMountMgr;

    // MOUNTMGR_DOS_DEVICE_NAME is defined as L"\\\\.\\MountPointManager"
    hMountMgr = CreateFile(
        MOUNTMGR_DOS_DEVICE_NAME,
        0,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL, OPEN_EXISTING,
        0,
        NULL);

    if (hMountMgr == INVALID_HANDLE_VALUE)
    {
        return;
    }

    DWORD cbBytesReturned;
    LPTSTR pszLogicalDrives;

    cbBytesReturned = GetLogicalDriveStrings(0, NULL);
    pszLogicalDrives = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbBytesReturned * sizeof(TCHAR));

    cbBytesReturned = GetLogicalDriveStrings(cbBytesReturned, pszLogicalDrives);

    for (LPTSTR pszDriveRoot = pszLogicalDrives; *pszDriveRoot != TEXT('\0'); pszDriveRoot += lstrlen(pszDriveRoot) + 1)
    {
        wchar_t szDeviceName[7] = (L"\\\\.\\");
        szDeviceName[4] = pszDriveRoot[0];
        szDeviceName[5] = _T(':');
        szDeviceName[6] = _T('\0');

        HANDLE hDevice = CreateFile(
            szDeviceName,
            0,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,
            OPEN_EXISTING,
            0,
            NULL
        );

        if (hDevice == INVALID_HANDLE_VALUE)
        {
            return;
        }

        BYTE byBuffer[1024];

        BOOL bSuccess = DeviceIoControl(
            hDevice,
            IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
            NULL,
            0,
            (LPVOID)byBuffer,
            sizeof(byBuffer),
            &cbBytesReturned, (LPOVERLAPPED)NULL
        );

        PMOUNTDEV_NAME pMountDevName = (PMOUNTDEV_NAME)byBuffer;
        bSuccess = CloseHandle(hDevice);

        DWORD dwInBuffer = pMountDevName->NameLength + sizeof(MOUNTMGR_MOUNT_POINT);
        PBYTE pbyInBuffer = (PBYTE)LocalAlloc(LMEM_ZEROINIT, dwInBuffer);

        PMOUNTMGR_MOUNT_POINT pMountPoint = (PMOUNTMGR_MOUNT_POINT)pbyInBuffer;
        pMountPoint->DeviceNameLength = pMountDevName->NameLength;
        pMountPoint->DeviceNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);

        CopyMemory(pbyInBuffer + sizeof(MOUNTMGR_MOUNT_POINT), pMountDevName->Name, pMountDevName->NameLength);

        DWORD dwOutBuffer = 1024 + sizeof(MOUNTMGR_MOUNT_POINTS);
        PBYTE pbyOutBuffer = (PBYTE)LocalAlloc(LMEM_ZEROINIT, dwOutBuffer);

        bSuccess = DeviceIoControl(hMountMgr,
            IOCTL_MOUNTMGR_QUERY_POINTS, pbyInBuffer, dwInBuffer, (LPVOID)pbyOutBuffer,
            dwOutBuffer, &cbBytesReturned, (LPOVERLAPPED)NULL);

        DWORD ccb;

        if (bSuccess)
        {
            PMOUNTMGR_MOUNT_POINTS pMountPoints = (PMOUNTMGR_MOUNT_POINTS)pbyOutBuffer;

            for (int i = 0; i < pMountPoints->NumberOfMountPoints; i++)
            {
                TCHAR szUniqueId[MAX_SIZE_OF_UNIQUEID];
                TCHAR szSymbolicLinkName[MAX_SIZE_OF_SYMBOLICLINKNAME];

                ccb = sizeof(szUniqueId) / sizeof(szUniqueId[0]);

                if (CryptBinaryToString(
                    pbyOutBuffer + pMountPoints->MountPoints[i].UniqueIdOffset,
                    pMountPoints->MountPoints[i].UniqueIdLength,
                    CRYPT_STRING_BASE64,
                    szUniqueId,
                    &ccb
                ) == 0)
                {
                    swprintf(
                        szUniqueId,
                        pMountPoints->MountPoints[i].UniqueIdLength / sizeof(WCHAR),
                        L"%ls",
                        pbyOutBuffer + pMountPoints->MountPoints[i].UniqueIdOffset
                    );

                    swprintf(
                        szSymbolicLinkName,
                        pMountPoints->MountPoints[i].SymbolicLinkNameLength / sizeof(WCHAR),
                        L"%ls\n",
                        pbyOutBuffer + pMountPoints->MountPoints[i].SymbolicLinkNameOffset
                    );

                    wcstombs(
                        storagesInfo->storages[storagesInfo->lastStorageIndex].szUniqueId,
                        szUniqueId,
                        sizeof(storagesInfo->storages[storagesInfo->lastStorageIndex].szUniqueId)
                    );

                    wcstombs(
                        storagesInfo->storages[storagesInfo->lastStorageIndex].szSymbolicLinkName,
                        szSymbolicLinkName,
                        sizeof(storagesInfo->storages[storagesInfo->lastStorageIndex].szSymbolicLinkName)
                    );

                    storagesInfo->lastStorageIndex++;
                }

            }
        }

        pbyInBuffer = (PBYTE)LocalFree(pbyInBuffer);
        pbyOutBuffer = (PBYTE)LocalFree(pbyOutBuffer);
    }

    pszLogicalDrives = (LPTSTR)LocalFree(pszLogicalDrives);
    CloseHandle(hMountMgr);
}