Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature 6401: authenticode table with catalog file info #6677

Merged
merged 4 commits into from
Oct 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions osquery/tables/system/windows/authenticode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <tlhelp32.h>
#include <wincrypt.h>
#include <Softpub.h>
#include <mscat.h>
#include <iomanip>
// clang-format on

Expand Down Expand Up @@ -125,6 +126,49 @@ void generateRow(Row& row, const SignatureInformation& signature_info) {
}
}

__checkReturn __success(return == true) bool getCatalogPathForFilePath(
__in const std::wstring path, __out std::wstring& catalogFile) {
bool status = false;
HANDLE handle = CreateFile(path.c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE != handle) {
HCATADMIN context;
GUID subsystem = DRIVER_ACTION_VERIFY;
HCATINFO catalog = NULL;

const SIZE_T SHA512_HASH_SIZE = 64;
BYTE hash[SHA512_HASH_SIZE];
DWORD hash_size = ARRAYSIZE(hash);

if (CryptCATAdminAcquireContext(&context, &subsystem, NULL)) {
if (CryptCATAdminCalcHashFromFileHandle(handle, &hash_size, hash, 0)) {
catalog =
CryptCATAdminEnumCatalogFromHash(context, hash, hash_size, 0, NULL);
if (NULL != catalog) {
CATALOG_INFO info = {0};
info.cbStruct = sizeof(info);
if (CryptCATCatalogInfoFromContext(catalog, &info, 0)) {
catalogFile = info.wszCatalogFile;
status = true;
}

CryptCATAdminReleaseCatalogContext(context, catalog, 0);
}
}
CryptCATAdminReleaseContext(context, 0);
}

CloseHandle(handle);
}

return status;
}

Status verifySignature(SignatureInformation::Result& result,
const std::wstring& path) {
WINTRUST_DATA trust_provider_settings = {};
Expand Down Expand Up @@ -410,6 +454,13 @@ Status querySignatureInformation(SignatureInformation& signature_info,

signature_info.path = path;

std::wstring catalog;
// may be a system file whose hash is in the catalog file.
// if so, return the catalog's signature instead.
if (getCatalogPathForFilePath(utf16_path, catalog)) {
utf16_path = catalog;
}

auto status = verifySignature(signature_info.result, utf16_path);
if (!status.ok()) {
return status;
Expand Down