Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
signature.cpp
Go to the documentation of this file.
1#include "PanoptesYara.h"
2#include <Softpub.h>
3#include <wintrust.h>
4#include <mscat.h>
5#include <bcrypt.h>
6
10static std::wstring ToWide(const std::string& str)
11{
12 int size = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
13 if (size <= 0) {
14 return std::wstring();
15 }
16
17 std::wstring result(size, L'\0');
18 MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &result[0], size);
19 result.resize(size - 1);
20 return result;
21}
22
26static std::string ToUtf8(const std::wstring& str)
27{
28 int size = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
29 if (size <= 0) {
30 return std::string();
31 }
32
33 std::string result(size, '\0');
34 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, &result[0], size, NULL, NULL);
35 result.resize(size - 1);
36 return result;
37}
38
42static std::string GetSignerName(HANDLE stateData)
43{
44 CRYPT_PROVIDER_DATA* providerData = WTHelperProvDataFromStateData(stateData);
45 if (providerData == NULL) {
46 return std::string();
47 }
48
49 CRYPT_PROVIDER_SGNR* signer = WTHelperGetProvSignerFromChain(providerData, 0, FALSE, 0);
50 if (signer == NULL || signer->csCertChain == 0) {
51 return std::string();
52 }
53
54 CRYPT_PROVIDER_CERT* cert = WTHelperGetProvCertFromChain(signer, 0);
55 if (cert == NULL || cert->pCert == NULL) {
56 return std::string();
57 }
58
59 DWORD size = CertGetNameStringW(cert->pCert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0);
60 if (size <= 1) {
61 return std::string();
62 }
63
64 std::wstring name(size, L'\0');
65 CertGetNameStringW(cert->pCert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, &name[0], size);
66 name.resize(size - 1);
67 return ToUtf8(name);
68}
69
74static LONG RunWinVerifyTrust(WINTRUST_DATA& trustData, std::string& signer)
75{
76 GUID policyGuid = WINTRUST_ACTION_GENERIC_VERIFY_V2;
77
78 trustData.cbStruct = sizeof(trustData);
79 trustData.dwUIChoice = WTD_UI_NONE;
80 trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
81 trustData.dwProvFlags = WTD_SAFER_FLAG;
82 trustData.dwStateAction = WTD_STATEACTION_VERIFY;
83
84 LONG status = WinVerifyTrust(static_cast<HWND>(INVALID_HANDLE_VALUE), &policyGuid, &trustData);
85 if (trustData.hWVTStateData != NULL) {
86 signer = GetSignerName(trustData.hWVTStateData);
87 }
88
89 // Release the state data
90 trustData.dwStateAction = WTD_STATEACTION_CLOSE;
91 WinVerifyTrust(static_cast<HWND>(INVALID_HANDLE_VALUE), &policyGuid, &trustData);
92
93 return status;
94}
95
100static bool VerifyCatalogSignature(const std::wstring& filePath, SignatureInfo& info)
101{
102 HANDLE file = CreateFileW(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
103 NULL, OPEN_EXISTING, 0, NULL);
104 if (file == INVALID_HANDLE_VALUE) {
105 return false;
106 }
107
108 bool found = false;
109
110 // Newer catalogs use SHA256 hashes, older ones use SHA1
111 for (LPCWSTR algorithm : { BCRYPT_SHA256_ALGORITHM, BCRYPT_SHA1_ALGORITHM }) {
112 HCATADMIN catAdmin = NULL;
113 if (!CryptCATAdminAcquireContext2(&catAdmin, NULL, algorithm, NULL, 0)) {
114 continue;
115 }
116
117 DWORD hashSize = 0;
118 CryptCATAdminCalcHashFromFileHandle2(catAdmin, file, &hashSize, NULL, 0);
119 std::vector<BYTE> hash(hashSize);
120 if (hashSize == 0 || !CryptCATAdminCalcHashFromFileHandle2(catAdmin, file, &hashSize, hash.data(), 0)) {
121 CryptCATAdminReleaseContext(catAdmin, 0);
122 continue;
123 }
124
125 HCATINFO catInfo = CryptCATAdminEnumCatalogFromHash(catAdmin, hash.data(), hashSize, 0, NULL);
126 if (catInfo != NULL) {
127 CATALOG_INFO catalog = {};
128 catalog.cbStruct = sizeof(catalog);
129 if (CryptCATCatalogInfoFromContext(catInfo, &catalog, 0)) {
130 // The catalog member tag is the file hash as an uppercase hex string
131 std::wstring memberTag;
132 wchar_t hex[3];
133 for (BYTE b : hash) {
134 swprintf_s(hex, L"%02X", b);
135 memberTag += hex;
136 }
137
138 WINTRUST_CATALOG_INFO catalogInfo = {};
139 catalogInfo.cbStruct = sizeof(catalogInfo);
140 catalogInfo.pcwszCatalogFilePath = catalog.wszCatalogFile;
141 catalogInfo.pcwszMemberFilePath = filePath.c_str();
142 catalogInfo.pcwszMemberTag = memberTag.c_str();
143 catalogInfo.hMemberFile = file;
144 catalogInfo.pbCalculatedFileHash = hash.data();
145 catalogInfo.cbCalculatedFileHash = hashSize;
146 catalogInfo.hCatAdmin = catAdmin;
147
148 WINTRUST_DATA trustData = {};
149 trustData.dwUnionChoice = WTD_CHOICE_CATALOG;
150 trustData.pCatalog = &catalogInfo;
151
152 info.trustStatus = RunWinVerifyTrust(trustData, info.signer);
153 info.isSigned = true;
154 info.catalogSigned = true;
155 info.trusted = info.trustStatus == ERROR_SUCCESS;
156 found = true;
157 }
158 CryptCATAdminReleaseCatalogContext(catAdmin, catInfo, 0);
159 }
160
161 CryptCATAdminReleaseContext(catAdmin, 0);
162 if (found) {
163 break;
164 }
165 }
166
167 CloseHandle(file);
168 return found;
169}
170
175SignatureInfo VerifyFileSignature(const std::string& PathToFile)
176{
177 SignatureInfo info;
178 std::wstring filePath = ToWide(PathToFile);
179 if (filePath.empty()) {
180 return info;
181 }
182
183 WINTRUST_FILE_INFO fileInfo = {};
184 fileInfo.cbStruct = sizeof(fileInfo);
185 fileInfo.pcwszFilePath = filePath.c_str();
186
187 WINTRUST_DATA trustData = {};
188 trustData.dwUnionChoice = WTD_CHOICE_FILE;
189 trustData.pFile = &fileInfo;
190
191 LONG status = RunWinVerifyTrust(trustData, info.signer);
192 switch (status) {
193 case TRUST_E_NOSIGNATURE:
194 case TRUST_E_SUBJECT_FORM_UNKNOWN:
195 case TRUST_E_PROVIDER_UNKNOWN:
196 // No embedded signature, check if the file is signed through a catalog
197 info.signer.clear();
198 if (!VerifyCatalogSignature(filePath, info)) {
199 info.trustStatus = status;
200 }
201 break;
202 default:
203 info.isSigned = true;
204 info.trustStatus = status;
205 info.trusted = status == ERROR_SUCCESS;
206 break;
207 }
208
209 return info;
210}
SignatureInfo VerifyFileSignature(const std::string &PathToFile)
Verify the Authenticode signature of a file using WinVerifyTrust. The embedded signature is checked f...
The result of verifying a file's Authenticode signature with WinVerifyTrust.
bool trusted
True if WinVerifyTrust reported the signature as valid and trusted.
std::string signer
The signing certificate's display name (UTF-8), empty if unsigned.
LONG trustStatus
The WinVerifyTrust result, 0 if trusted.
bool isSigned
True if the file has an embedded or catalog signature.
bool catalogSigned
True if the signature came from a catalog file rather than being embedded.