gpt4 book ai didi

c - WinVerifyTrust 检查特定签名?

转载 作者:可可西里 更新时间:2023-11-01 12:34:14 24 4
gpt4 key购买 nike

我正在为 Windows 实现进程提升助手。这是一个将在提升模式下运行并以管理员权限启动其他程序而不会显示其他 UAC 提示的程序。出于安全原因,我想确保只能执行使用我公司的 Authenticode key 进行数字签名的二进制文件。

WinVerifyTrust函数让我完成了一半,但它只能确保二进制文件由 一些 key 签名,该 key 是 Microsoft 信任链的一部分。有没有一种相对简单的方法来执行 Authenticode 验证并确保它是由我们的私钥签名的?

最佳答案

我相信您正在寻找的是 CryptQueryObject .

有了它,您应该能够从 PE 中提取涉及的证书,并进行任何您想要的额外检查。


例如,这将使您进入 HCRYPTMSG。从那里你可以使用 CryptMsgGetParam拉出你想要的任何东西。我希望做一些更“健壮”的东西,但这些 API 非常多毛,因为它们需要大量分支来处理所有返回情况。

所以,这是一个 p/invoke-rific c# 示例(我从 C 开始,但那基本上是不可读的):

static class Crypt32
{
//Omitting flag constants; you can look these up in WinCrypt.h

[DllImport("CRYPT32.DLL", EntryPoint = "CryptQueryObject", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool CryptQueryObject(
int dwObjectType,
IntPtr pvObject,
int dwExpectedContentTypeFlags,
int dwExpectedFormatTypeFlags,
int dwFlags,
out int pdwMsgAndCertEncodingType,
out int pdwContentType,
out int pdwFormatType,
ref IntPtr phCertStore,
ref IntPtr phMsg,
ref IntPtr ppvContext);
}

class Program
{
static void Main(string[] args)
{
//Path to executable here
// I tested with MS-Office .exe's
string path = "";

int contentType;
int formatType;
int ignored;
IntPtr context = IntPtr.Zero;
IntPtr pIgnored = IntPtr.Zero;

IntPtr cryptMsg = IntPtr.Zero;

if (!Crypt32.CryptQueryObject(
Crypt32.CERT_QUERY_OBJECT_FILE,
Marshal.StringToHGlobalUni(path),
Crypt32.CERT_QUERY_CONTENT_FLAG_ALL,
Crypt32.CERT_QUERY_FORMAT_FLAG_ALL,
0,
out ignored,
out contentType,
out formatType,
ref pIgnored,
ref cryptMsg,
ref context))
{
int error = Marshal.GetLastWin32Error();

Console.WriteLine((new Win32Exception(error)).Message);

return;
}

//expecting '10'; CERT_QUERY_CONTENT_PKCS7_SIGNED_EMBED
Console.WriteLine("Context Type: " + contentType);

//Which implies this is set
Console.WriteLine("Crypt Msg: " + cryptMsg.ToInt32());

return;
}

关于c - WinVerifyTrust 检查特定签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1072540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com