2012年7月5日星期四

WinVerifyTrust.Cpp


/*
检查一个pe文件是不是一个有效的数字签名。
开始以为用检查pe文件结构的办法就能实现,可是这个办法不全面,易出错。
还是用这个方便,安全。
本文修改自msdn.
*/

#include <windows.h>
#include <Softpub.h>
#pragma comment (lib, "wintrust")

BOOL VerifyEmbeddedSignature(LPCWSTR pwszSourceFile)
{
    WINTRUST_FILE_INFO FileData;
    memset(&FileData, 0, sizeof(FileData));
    FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
    FileData.pcwszFilePath = pwszSourceFile;
    FileData.hFile = NULL;
    FileData.pgKnownSubject = NULL;    

    WINTRUST_DATA WinTrustData;
    memset(&WinTrustData, 0, sizeof(WinTrustData));
    WinTrustData.cbStruct = sizeof(WinTrustData);    
    WinTrustData.pPolicyCallbackData = NULL;// Use default code signing EKU.    
    WinTrustData.pSIPClientData = NULL;// No data to pass to SIP.   
    WinTrustData.dwUIChoice = WTD_UI_NONE;// Disable WVT UI.    
    WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE; // No revocation checking.    
    WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;// Verify an embedded signature on a file.    
    WinTrustData.dwStateAction = 0;// Default verification.    
    WinTrustData.hWVTStateData = NULL;// Not applicable for default verification of embedded signature.    
    WinTrustData.pwszURLReference = NULL;// Not used.
    WinTrustData.dwUIContext = 0;    
    WinTrustData.pFile = &FileData;// Set pFile.

    GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
    LONG lStatus = WinVerifyTrust(NULL,&WVTPolicyGUID,&WinTrustData);// 真正的开始.
    if (ERROR_SUCCESS == lStatus)
    {
        MessageBox(0,L"The file you selected is signed and the signature was verified.",0,0);
    }
    else 
    {
        MessageBox(0,L"其他的众多的失败之一",0,0);
        return false;
    }

    return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    VerifyEmbeddedSignature(L"E:\\signature\\exe.exe");
    return 0;
}

没有评论:

发表评论