📄 signcsp.cpp
字号:
HANDLE hFileMap = NULL;
memset(&MemInfo, 0, sizeof(MemInfo));
// Load the file as a datafile
if (NULL == (hInst = LoadLibraryEx(szFile, NULL, LOAD_LIBRARY_AS_DATAFILE)))
{
printf("Couldn't load file\n");
goto Ret;
}
if (!GetResourcePtr(hInst, MAC_RESOURCE_NUMBER, &pbMAC, &cbMAC))
{
printf("Couldn't find MAC placeholder\n");
goto Ret;
}
// get image start address
VirtualQuery(hInst, &MemInfo, sizeof(MemInfo));
pbStart = (BYTE*)MemInfo.BaseAddress;
FreeLibrary(hInst); hInst = NULL;
cbMACOffset = (DWORD)(pbMAC - pbStart);
if (cbMAC != (DES_BLOCKLEN + sizeof(DWORD) * 2))
{
printf("Attempt to replace %d zeros with new MAC!\n", cbMAC);
goto Ret;
}
if (INVALID_HANDLE_VALUE == (hFileProv = CreateFile(szFile,
GENERIC_READ | GENERIC_WRITE,
0, // don't share
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0)))
{
printf("Couldn't CreateFile: 0x%x\n", GetLastError());
goto Ret;
}
if (NULL == (hFileMap = CreateFileMapping(
hFileProv,
NULL,
PAGE_READWRITE,
0,
0,
NULL)))
{
printf("Couldn't map file\n");
goto Ret;
}
if (NULL == (pbFilePtr = (PBYTE)MapViewOfFile(
hFileMap,
FILE_MAP_ALL_ACCESS,
0,
0,
0)))
{
printf("Couldn't create view\n");
goto Ret;
}
// copy version, CRC offset and new sig
CopyMemory(pbFilePtr+cbMACOffset, &dwMACVersion, sizeof(dwMACVersion));
cbMACOffset += sizeof(dwMACVersion);
CopyMemory(pbFilePtr+cbMACOffset, &dwCRCOffset, sizeof(dwCRCOffset));
cbMACOffset += sizeof(dwCRCOffset);
CopyMemory(pbFilePtr+cbMACOffset, pbNewMAC, DES_BLOCKLEN);
// compute a new checksum
if (NULL == (pImageNTHdrs = CheckSumMappedFile(pbFilePtr, cbImage,
&OldCheckSum, &NewCheckSum)))
goto Ret;
CopyMemory(&pImageNTHdrs->OptionalHeader.CheckSum, &NewCheckSum, sizeof(DWORD));
if (NULL == (pImageNTHdrs = CheckSumMappedFile(pbFilePtr, cbImage,
&OldCheckSum, &NewCheckSum)))
goto Ret;
if (OldCheckSum != NewCheckSum)
goto Ret;
dwErr = ERROR_SUCCESS;
Ret:
if (pbFilePtr)
UnmapViewOfFile(pbFilePtr);
if (hFileMap)
CloseHandle(hFileMap);
if (hInst)
FreeLibrary(hInst);
if (hFileProv)
CloseHandle(hFileProv);
return dwErr;
}
void MacCSP(
LPCSTR pszInFile
)
{
DWORD cbImage;
DWORD dwCRCOffset;
HANDLE hFileProv = INVALID_HANDLE_VALUE;
BYTE rgbMAC[DES_BLOCKLEN];
HMODULE hInst = NULL;
PBYTE pbFilePtr = NULL;
DWORD cbImageSize, cbMACOffset;
PBYTE pbMAC;
DWORD cbMAC;
memset(rgbMAC, 0, sizeof(rgbMAC));
// check if the MAC resource is in the CSP and exit if not
// Load the file as a datafile
if (NULL == (hInst = LoadLibraryEx(pszInFile,
NULL,
LOAD_LIBRARY_AS_DATAFILE)))
{
printf("Couldn't load file\n");
goto Ret;
}
if (!GetResourcePtr(hInst, MAC_RESOURCE_NUMBER, &pbMAC, &cbMAC))
{
goto Ret;
}
FreeLibrary(hInst);
hInst = NULL;
// get the file size
if ((hFileProv = CreateFile(pszInFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0)) == INVALID_HANDLE_VALUE)
{
printf("CSP specified was not found!\n");
goto Ret;
}
if (0xffffffff == (cbImage = GetFileSize(hFileProv, NULL)))
{
printf("CSP specified was not found!\n");
goto Ret;
}
CloseHandle(hFileProv);
hFileProv = NULL;
if (0 != GetCRCOffset(pszInFile, cbImage, &dwCRCOffset))
{
printf("Unable to get CRC!\n");
goto Ret;
}
// calculate the MAC
if (!MACTheFileWithSig(pszInFile,
cbImage,
dwMACInFileVersion,
dwCRCOffset,
rgbMAC))
{
printf("MAC failed!\n");
goto Ret;
}
//
// Place the MAC into the resource in the file
//
if (ERROR_SUCCESS != SetCryptMACResource(pszInFile,
dwMACInFileVersion,
dwCRCOffset,
rgbMAC,
cbImage))
{
printf("Unable to set the MAC into the file resource!\n");
goto Ret;
}
Ret:
if (hInst)
{
FreeLibrary(hInst);
}
if (INVALID_HANDLE_VALUE != hFileProv)
{
CloseHandle(hFileProv);
}
return;
}
/*++
main:
This is the main entry point of the application.
Arguments:
argc - Count of arguments
argv - array of arguments
Return Value:
0 - Success
1 - Error
Author:
Doug Barlow (dbarlow) 1/25/1996
--*/
extern "C" void __cdecl
main(
int argc,
char *argv[])
{
DWORD exStatus = 1;
DWORD index;
LPCTSTR szBinFile = NULL;
LPCTSTR szInFile = NULL;
BOOL fOutput = FALSE;
int status;
DWORD ThreadId;
HANDLE hThread;
// RPC Specific variables.
RPC_STATUS rpcStatus;
unsigned char * pszUuid = NULL;
char * pszProtocolSequence = "ncacn_np";
unsigned char * pszNetworkAddress = (LPBYTE)"\\\\enigma";
char * pszEndpoint = "\\pipe\\sign";
unsigned char * pszOptions = NULL;
unsigned char * pszStringBinding = NULL;
DWORD dwrt;
DWORD i;
DWORD cbImage;
DWORD dwCRCOffset;
HANDLE hFileProv = 0;
//
// Parse the command line.
//
if ((argc != 2) || (argv[1][0] == '?'))
{
ShowHelp();
exStatus = 0;
goto ErrorExit;
}
szInFile = &argv[1][0];
//
// Command consistency checks.
//
if (NULL == szInFile)
{
printf("No input file specified.\n");
goto ErrorExit;
}
MacCSP(szInFile);
// get the file size
if ((hFileProv = CreateFile(szInFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0)) == INVALID_HANDLE_VALUE)
{
printf("CSP specified was not found!\n");
goto ErrorExit;
}
if (0xffffffff == (cbImage = GetFileSize(hFileProv, NULL)))
{
printf("CSP specified was not found!\n");
goto ErrorExit;
}
CloseHandle(hFileProv);
hFileProv = NULL;
if (ERROR_SUCCESS != GetCRCOffset(szInFile, cbImage, &dwCRCOffset))
{
printf("Unable to get the CRC offset on the file!\n");
goto ErrorExit;
}
//
// Compute the hash.
//
if (!HashTheFile(szInFile, dwCRCOffset, pbDigest, &cbDigestLen, cbImage))
{
printf("Unable to hash the file!\n");
goto ErrorExit;
}
//
// Get the signature.
//
// Try to make rpc connection
rpcStatus = RpcStringBindingCompose(pszUuid,
(unsigned char *) pszProtocolSequence,
pszNetworkAddress,
(unsigned char *) pszEndpoint,
pszOptions,
&pszStringBinding);
#ifdef DEBUG
printf("RpcStringBindingCompose returned 0x%x\n", rpcStatus);
printf("pszStringBinding = %s\n", pszStringBinding);
#endif
if (0 != rpcStatus)
{
printf("Failed to compose binding string for target RPC server.\n");
goto ErrorExit;
}
/* Set the binding handle that will */
/* be used to bind to the server */
rpcStatus = RpcBindingFromStringBinding(pszStringBinding,
&hello_IfHandle);
#ifdef DEBUG
printf("RpcBindingFromStringBinding returned 0x%x\n", rpcStatus);
#endif
if (0 != rpcStatus)
{
printf("Failed to bind to target RPC server.\n");
goto ErrorExit;
}
if ((hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE) CallServer,
NULL,
0,
&ThreadId)) == NULL)
{
printf("Call to CreateThread failed\n");
goto ErrorExit;
}
printf("Sending request to be signed, will wait 5 minutes\n");
for (i = 0; i < 20; i++)
{
printf("Waited %d seconds\n", i*30);
dwrt = WaitForSingleObject(hThread, 15000);
if (dwrt == WAIT_OBJECT_0)
{
break;
}
}
if (i == 20)
{
printf("Call to Server timed out\n");
goto ErrorExit;
}
GetExitCodeThread(hThread, &dwrt);
if (dwrt)
{
goto ErrorExit;
}
//
// Place the signature into the resource in the file
//
if (ERROR_SUCCESS != SetCryptSignatureResource(szInFile, dwSigInFileVersion,
dwCRCOffset, pbSignature,
cbSignatureLen, cbImage))
{
printf("Unable to set the signature into the file resource!\n");
goto ErrorExit;
}
//
// Clean up and return.
//
exStatus = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -