본문 바로가기

운동하는 개발자/Windows

c++ windows 디지털서명(코드사인) 유효성(신뢰성) 확인

728x90

특정 파일이 디지털 서명이 되어있는지, 코드사인이 신뢰 가능한지 확인

#include <iostream>
#include <Windows.h>
#include <WinTrust.h>
#include <SoftPub.h>
#include <ImageHlp.h>

#pragma comment (lib, "wintrust")

bool VerifyEmbeddedSignature(const std::wstring& source_file)
{
	LONG lStatus;
	DWORD dwLastError;

	// Initialize the WINTRUST_FILE_INFO structure.

	WINTRUST_FILE_INFO FileData;
	memset(&FileData, 0, sizeof(FileData));
	FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
	FileData.pcwszFilePath = source_file.c_str();
	FileData.hFile = NULL;
	FileData.pgKnownSubject = NULL;

	/*
	WVTPolicyGUID specifies the policy to apply on the file
	WINTRUST_ACTION_GENERIC_VERIFY_V2 policy checks:

	1) The certificate used to sign the file chains up to a root
	certificate located in the trusted root certificate store. This
	implies that the identity of the publisher has been verified by
	a certification authority.

	2) In cases where user interface is displayed (which this example
	does not do), WinVerifyTrust will check for whether the
	end entity certificate is stored in the trusted publisher store,
	implying that the user trusts content from this publisher.

	3) The end entity certificate has sufficient permission to sign
	code, as indicated by the presence of a code signing EKU or no
	EKU.
	*/

	GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
	WINTRUST_DATA WinTrustData;

	// Initialize the WinVerifyTrust input data structure.

	// Default all fields to 0.
	memset(&WinTrustData, 0, sizeof(WinTrustData));

	WinTrustData.cbStruct = sizeof(WinTrustData);

	// Use default code signing EKU.
	WinTrustData.pPolicyCallbackData = NULL;

	// No data to pass to SIP.
	WinTrustData.pSIPClientData = NULL;

	// Disable WVT UI.
	WinTrustData.dwUIChoice = WTD_UI_NONE;

	// No revocation checking.
	WinTrustData.fdwRevocationChecks = WTD_REVOKE_NONE;

	// Verify an embedded signature on a file.
	WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;

	// Default verification.
	WinTrustData.dwStateAction = 0;

	// Not applicable for default verification of embedded signature.
	WinTrustData.hWVTStateData = NULL;

	// Not used.
	WinTrustData.pwszURLReference = NULL;

	// This is not applicable if there is no UI because it changes
	// the UI to accommodate running applications instead of
	// installing applications.
	WinTrustData.dwUIContext = 0;

	// Set pFile.
	WinTrustData.pFile = &FileData;

	// WinVerifyTrust verifies signatures as specified by the GUID
	// and Wintrust_Data.
	lStatus = WinVerifyTrust(
		NULL,
		&WVTPolicyGUID,
		&WinTrustData);

	bool result(false);
	switch (lStatus)
	{
	case ERROR_SUCCESS:
		/*
		Signed file:
			- Hash that represents the subject is trusted.

			- Trusted publisher without any verification errors.

			- UI was disabled in dwUIChoice. No publisher or
				time stamp chain errors.

			- UI was enabled in dwUIChoice and the user clicked
				"Yes" when asked to install and run the signed
				subject.
		*/
		std::wcout << "The file " << source_file << " is signed and the signature "
			"was verified" << std::endl;
		result = true;
		break;

	case TRUST_E_NOSIGNATURE:
		// The file was not signed or had a signature
		// that was not valid.

		// Get the reason for no signature.
		dwLastError = GetLastError();
		if (TRUST_E_NOSIGNATURE == dwLastError ||
			TRUST_E_SUBJECT_FORM_UNKNOWN == dwLastError ||
			TRUST_E_PROVIDER_UNKNOWN == dwLastError)
		{
			// The file was not signed.
			std::wcout << "The file " << source_file << "is not signed." << std::endl;
		}
		else
		{
			// The signature was not valid or there was an error
			// opening the file.
			std::wcout << "An unknown error occurred trying to verify the signature of the "
				<< source_file << "file." << std::endl;
		}

		break;

	case TRUST_E_EXPLICIT_DISTRUST:
		// The hash that represents the subject or the publisher
		// is not allowed by the admin or user.
		std::wcout << "The signature is present, but specifically disallowed." << std::endl;
		break;

	case TRUST_E_SUBJECT_NOT_TRUSTED:
		// The user clicked "No" when asked to install and run.
		std::wcout << "The signature is present, but not trusted." << std::endl;
		break;

	case CRYPT_E_SECURITY_SETTINGS:
		/*
		The hash that represents the subject or the publisher
		was not explicitly trusted by the admin and the
		admin policy has disabled user trust. No signature,
		publisher or time stamp errors.
		*/

		std::wcout << "CRYPT_E_SECURITY_SETTINGS - The hash "
			"representing the subject or the publisher wasn't "
			"explicitly trusted by the admin and admin policy "
			"has disabled user trust. No signature, publisher "
			"or timestamp errors." << std::endl;
		break;

	default:
		// The UI was disabled in dwUIChoice or the admin policy
		// has disabled user trust. lStatus contains the
		// publisher or time stamp chain error.
		std::wcout << "Error is: " << lStatus << std::endl;
		break;
	}
	WinTrustData.dwStateAction = WTD_STATEACTION_CLOSE;
	lStatus = WinVerifyTrust(
		NULL,
		&WVTPolicyGUID,
		&WinTrustData);

	return result;
}

MSDN공식에 있는 에제 코드에서 약간 c++스럽게 변경

첫번째 case에(서명되어있고 신뢰가능)들어갔을때만 리턴 True로 주게 하였다
함수 호출은 파일 path로 호출 

if(VerifyEmbeddedSignature(L"C:\Program Files (x86)\temp\temp.exe"){
	std::cout << "파일 정상 실행" << std::endl;
}
else{
	std::cout << "파일 비정상 삭제" << std::endl;
}​

 

출처 : https://learn.microsoft.com/ko-kr/windows/win32/seccrypto/example-c-program--verifying-the-signature-of-a-pe-file

728x90