使用Python win32crypt访问Windows本地计算机存储 [英] Access windows local machine store with Python win32crypt

查看:154
本文介绍了使用Python win32crypt访问Windows本地计算机存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问存储在Windows本地计算机存储中的证书.使用wincertstore不能做到这一点,因为它使用了CertOpenSystemStoreA函数(请参阅备注: https://docs.microsoft.com/zh-cn/windows/win32/api/wincrypt/nf-wincrypt-certopensystemstorea )

I'm trying to access a certificate, that's stored in windows local machine store. This can't be done with wincertstore, as it uses CertOpenSystemStoreA function (see remarks: https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopensystemstorea)

但是pywin32的CertOpenStore可以访问,我只是不知道如何传递正确的参数.这是我的代码:

However CertOpenStore from pywin32 has access, I just don't know how to pass the right parameters. Here's my code:

import win32crypt

# store provider
CERT_STORE_PROV_SYSTEM = 13
#dwFlags
CERT_SYSTEM_STORE_CURRENT_SERVICE = 0x0100
CERT_SYSTEM_STORE_CURRENT_USER = 0x0200
CERT_SYSTEM_STORE_CURRENT_USER_GROUP_POLICY = 0x0400
CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x0800
CERT_SYSTEM_STORE_LOCAL_MACHINE_ENTERPRISE = 0x1000
CERT_SYSTEM_STORE_LOCAL_MACHINE_GROUP_POLICY = 0x2000
CERT_SYSTEM_STORE_SERVICES = 0x4000
CERT_SYSTEM_STORE_USERS = 0x8000

store = win32crypt.CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, None, CERT_SYSTEM_STORE_LOCAL_MACHINE, "MY")
for cert in store.CertEnumCertificatesInStore():
    print("1 Cert: " + str(cert))
    print("2 CertEnumCertificateContextProperties: " + str(cert.CertEnumCertificateContextProperties()))
    print("3 cert.Subject: " + str(win32crypt.CertNameToStr(cert.Subject)))

在运行时,出现异常:(-2147024809,"CertOpenStore",参数错误".)

When running, I get an exception: (-2147024809, 'CertOpenStore', 'Wrong Parameter.')

pywin32中CertOpenStore的文档: http://timgolden.me.uk/pywin32-docs/win32crypt__CertOpenStore_meth.html Windows开发人员中心中CertOpenStore的文档: https://docs.microsoft.com/zh-CN/windows/win32/api/wincrypt/nf-wincrypt-certopenstore

Documentation of CertOpenStore in pywin32: http://timgolden.me.uk/pywin32-docs/win32crypt__CertOpenStore_meth.html Documentaion of CertOpenStore in Windows Dev Center: https://docs.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certopenstore

推荐答案

您弄乱了常量.我不知道您从哪里得到的,但是这些值(至少是您使用的两个值)是错误的,因此我删除了所有这些值(没有查看其他值).它们(或大多数)是 预处理器宏 ,在

You messed up the constants. I don't know where you got them from, but the values (at least both that you used) are wrong, so I deleted them all (didn't look at the others). They (or most of them) are preprocessor macros, defined in [MS.Docs]: wincrypt.h header (part of Windows SDK - unfortunately I didn't find an official location on the web where it could be downloaded from - I have it on my laptop, as it was automatically installed by Visual Studio).

code00.py :

#!/usr/bin/env python

import sys
import win32crypt as wcrypt


# lpszStoreProvider
CERT_STORE_PROV_SYSTEM = 0x0000000A

# dwFlags
CERT_SYSTEM_STORE_LOCAL_MACHINE = 0x00020000


def main(*argv):
    store = wcrypt.CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, None, CERT_SYSTEM_STORE_LOCAL_MACHINE, "My")

    for cert in store.CertEnumCertificatesInStore():
        print("1 Cert: {0:}".format(cert))
        print("2 CertEnumCertificateContextProperties returned: {0:}".format(cert.CertEnumCertificateContextProperties()))
        print("3 cert.Subject: {0:}".format(wcrypt.CertNameToStr(cert.Subject)))


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main(*sys.argv[1:])
    print("\nDone.")

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q061118677]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" code00.py
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] 64bit on win32

1 Cert: <PyCERT_CONTEXT object at 0x0000021CDE3BD740>
2 CertEnumCertificateContextProperties returned: [92, 15, 20, 11, 2, 3, 4, 25, 89]
3 cert.Subject: localhost

Done.

这篇关于使用Python win32crypt访问Windows本地计算机存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆