尝试使用ctypes调用wincred api

2024-07-03 07:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用cTypesAPI读取Windows凭据保险库,但我不确定如何将函数结果转换回可用的ctypes.结构. 在

import ctypes
class CREDENTIALS(ctypes.Structure):
    _fields_ = [
        ("Flags", ctypes.c_int),
        ("Type", ctypes.c_int),
        ("TargetName", ctypes.c_wchar_p),
        ("Comment", ctypes.c_wchar_p),
        ("CredentialBlobSize", ctypes.c_int),
        ("CredentialBlob", ctypes.c_wchar_p),
        ("AttributeCount", ctypes.c_int),
        ("Attributes", ctypes.c_wchar_p),
        ("TargetAlias", ctypes.c_wchar_p),
        ("UserName", ctypes.c_wchar_p)
]
advapi32 = ctypes.oledll.LoadLibrary('Advapi32.dll')
advapi32.CredReadW.restype = ctypes.c_bool
advapi32.CredReadW.argtypes = [ctypes.c_wchar_p, ctypes.c_int, ctypes.c_int, ctypes.POINTER(CREDENTIALS)]
target = "login.example.com"
pcred = ctypes.pointer(CREDENTIALS())
ok = advapi32.CredReadW(target,1,0,pcred)
cred = pcred.contents
print ok, pcred, cred.UserName, cred.CredentialBlob

结果:

1 <__main__.LP_CREDENTIALS object at 0x012CECB0> None None

函数返回true,因此它可以工作,但指针内容似乎为空。我做错什么了?在


Tags: 函数nonetargetusernameokctypesintcredentials
1条回答
网友
1楼 · 发布于 2024-07-03 07:42:54

oledll应该是windlloledll用于返回HRESULT的函数。在

CREDENTIAL的定义缺少一些字段(LastWritten和{})。定义(link)是:

typedef struct _CREDENTIAL {
  DWORD                 Flags;
  DWORD                 Type;
  LPTSTR                TargetName;
  LPTSTR                Comment;
  FILETIME              LastWritten;
  DWORD                 CredentialBlobSize;
  LPBYTE                CredentialBlob;
  DWORD                 Persist;
  DWORD                 AttributeCount;
  PCREDENTIAL_ATTRIBUTE Attributes;
  LPTSTR                TargetAlias;
  LPTSTR                UserName;
} CREDENTIAL, *PCREDENTIAL;

相关问题 更多 >