Last active
October 27, 2022 16:01
-
-
Save dertin/60c4fbb88fe0ce3faadde0f3474bdb0e to your computer and use it in GitHub Desktop.
Compress and Encode with Base64 to a DLL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#pragma comment(lib, "ws2_32") | |
#pragma comment(lib, "crypt32.lib") | |
#pragma comment(lib, "Cabinet.lib") | |
#include <Windows.h> | |
#include <string> | |
#include <compressapi.h> | |
#include <wincrypt.h> | |
#include <fstream> | |
// Headers | |
LPWSTR compressAndBase64Encode(LPCWSTR lpEncFile); | |
std::string wstrtostr(const std::wstring &wstr); | |
int main() | |
{ | |
std::fstream fs; | |
wprintf(L"Compress and Base64 Encode\n"); | |
std::wstring path(L"C:\\Users\\root\\Desktop\\target.dll"); // change file path | |
LPCWSTR finalPath = path.c_str(); | |
LPWSTR outputDLL = compressAndBase64Encode(finalPath); | |
wprintf(L"Save File!\n"); | |
fs.open("C:\\Users\\root\\Desktop\\dllCompressBase64.txt", std::fstream::out); // change file path | |
fs << wstrtostr(outputDLL); | |
fs.close(); | |
wprintf(L"Good Bye!\n"); | |
return 0; | |
} | |
std::string wstrtostr(const std::wstring &wstr) | |
{ | |
std::string strTo; | |
char *szTo = new char[wstr.length() + 1]; | |
szTo[wstr.size()] = '\0'; | |
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, szTo, (int)wstr.length(), NULL, NULL); | |
strTo = szTo; | |
delete[] szTo; | |
return strTo; | |
} | |
LPWSTR compressAndBase64Encode(LPCWSTR lpEncFile) { | |
COMPRESSOR_HANDLE compressor = NULL; | |
PBYTE fileBuffer = NULL; | |
PBYTE compressedBuffer = NULL; | |
PBYTE encodedBuffer = NULL; | |
SIZE_T fileBufferSize, fileDataSize, compressedBufferSize, compressedDataSize; | |
DWORD BytesRead; | |
BOOL bErrorFlag = FALSE; | |
LPWSTR outputBuffer = NULL; | |
//File handle | |
HANDLE encFileHandle = CreateFile(lpEncFile, | |
GENERIC_READ, | |
0, | |
NULL, | |
OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
if (encFileHandle == INVALID_HANDLE_VALUE) { | |
wprintf(L"File handle error!"); | |
return FALSE; | |
} | |
//Allocate Memory | |
fileDataSize = GetFileSize(encFileHandle, NULL); | |
fileBuffer = new BYTE[fileDataSize + 1]; | |
bErrorFlag = ReadFile(encFileHandle, fileBuffer, fileDataSize, &BytesRead, NULL); | |
if (FALSE == bErrorFlag) { | |
CloseHandle(encFileHandle); | |
return FALSE; | |
} | |
if (!CreateCompressor( | |
COMPRESS_ALGORITHM_LZMS, | |
NULL, | |
&compressor | |
)) { | |
wprintf(L"Compressor creation error!"); | |
return FALSE; | |
} | |
if (!Compress( | |
compressor, | |
fileBuffer, | |
fileDataSize, | |
NULL, | |
0, | |
&compressedBufferSize)) { | |
DWORD ErrorCode = GetLastError(); | |
if (ErrorCode != ERROR_INSUFFICIENT_BUFFER) { | |
wprintf(L"Compression buffer determination error!"); | |
return FALSE; | |
} | |
compressedBuffer = (PBYTE)malloc(compressedBufferSize); | |
if (!compressedBuffer) { | |
wprintf(L"Memory allocation error!"); | |
return FALSE; | |
} | |
} | |
if (!Compress( | |
compressor, | |
fileBuffer, | |
fileDataSize, | |
compressedBuffer, | |
compressedBufferSize, | |
&compressedDataSize)) { | |
wprintf(L"Compression process error!"); | |
return FALSE; | |
} | |
// Base64 encode the compressed buffer | |
DWORD dwSize = 0; | |
if (!CryptBinaryToString(compressedBuffer, compressedDataSize, CRYPT_STRING_BASE64, NULL, &dwSize)) { | |
wprintf(L"Determining the size of the outputBuffer error!"); | |
return FALSE; | |
} | |
dwSize++; | |
outputBuffer = new TCHAR[dwSize]; | |
if (!CryptBinaryToString(compressedBuffer, compressedDataSize, CRYPT_STRING_BASE64, outputBuffer, &dwSize)) { | |
wprintf(L"Base64 encoding process error!"); | |
return FALSE; | |
} | |
CloseHandle(encFileHandle); | |
return outputBuffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment