-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
api.py
273 lines (201 loc) · 7.07 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from ctypes import (
POINTER,
Structure,
WinError,
byref,
c_char_p,
c_uint32,
c_wchar_p,
cast,
pointer,
string_at,
windll,
)
from ctypes.wintypes import BOOL, DWORD, LPBYTE, LPVOID
ATTRIBUTE_KEYWORD = 'python-keyring:authstate'
# region ctypes wrapper structures
class FILETIME(Structure):
_fields_ = [
('dwLowDateTime', c_uint32),
('dwHighDateTime', c_uint32),
]
def asdict(self):
return {field: getattr(self, field) for field, _ in self._fields_}
class CREDENTIAL_ATTRIBUTEW(Structure):
_fields_ = [
('Keyword', c_wchar_p),
('Flags', DWORD),
('ValueSize', DWORD),
('Value', LPBYTE),
]
def asdict(self):
result = {}
for field, _ in self._fields_:
value = getattr(self, field)
if field != 'Value':
result[field] = value
else:
result[field] = string_at(value, getattr(self, 'ValueSize'))
return result
class CREDENTIALW(Structure):
_fields_ = [
('Flags', DWORD),
('Type', DWORD),
('TargetName', c_wchar_p),
('Comment', c_wchar_p),
('LastWritten', FILETIME),
('CredentialBlobSize', DWORD),
('CredentialBlob', LPBYTE),
('Persist', DWORD),
('AttributeCount', DWORD),
('Attributes', POINTER(CREDENTIAL_ATTRIBUTEW)),
('TargetAlias', c_wchar_p),
('UserName', c_wchar_p),
]
def asdict(self):
result = {}
for field, _ in self._fields_:
value = getattr(self, field)
if field == 'LastWritten':
result[field] = value.asdict()
elif field == 'Attributes':
attr_dict = {}
for i in range(result['AttributeCount']):
if value[i]: # NULL pointer is treated as False by ctypes
attr = value[i].asdict()
attr_dict[attr['Keyword']] = attr
result[field] = attr_dict
elif field == 'CredentialBlob':
blob = string_at(value, getattr(self, 'CredentialBlobSize'))
result[field] = blob
else:
result[field] = value
return result
# endregion
# region Win32 API declarations
def errcheck(result, func, args):
if not result:
raise WinError()
_CredReadW = windll.advapi32.CredReadW
_CredReadW.argtypes = [c_wchar_p, DWORD, DWORD, POINTER(POINTER(CREDENTIALW))]
_CredReadW.restype = BOOL
_CredReadW.errcheck = errcheck # type: ignore
_CredWriteW = windll.advapi32.CredWriteW
_CredWriteW.argtypes = [POINTER(CREDENTIALW), DWORD]
_CredWriteW.restype = BOOL
_CredWriteW.errcheck = errcheck # type: ignore
_CredDeleteW = windll.advapi32.CredDeleteW
_CredDeleteW.argtypes = [c_wchar_p, DWORD, DWORD]
_CredDeleteW.restype = BOOL
_CredDeleteW.errcheck = errcheck # type: ignore
_CredEnumerateW = windll.advapi32.CredEnumerateW
_CredEnumerateW.argtypes = [
c_wchar_p,
DWORD,
POINTER(DWORD),
POINTER(POINTER(POINTER(CREDENTIALW))),
]
_CredEnumerateW.restype = BOOL
_CredEnumerateW.errcheck = errcheck # type: ignore
_CredFree = windll.advapi32.CredFree
_CredFree.argtypes = [LPVOID]
_CredFree.restype = None
CRED_TYPE_GENERIC = 1
CRED_PERSIST_SESSION = 1
CRED_PERSIST_LOCAL_MACHINE = 2
CRED_PERSIST_ENTERPRISE = 3
CRED_ENUMERATE_ALL_CREDENTIALS = 1
CRED_MAX_VALUE_SIZE = 256
CRED_MAX_ATTRIBUTES = 64
MAX_PASSWORD_BYTES = CRED_MAX_VALUE_SIZE * CRED_MAX_ATTRIBUTES
# endregion
# region API
def CredRead(Type, TargetName):
pcred = pointer(CREDENTIALW())
_CredReadW(TargetName, Type, 0, byref(pcred))
credential = pcred.contents.asdict()
_CredFree(pcred)
return credential
def CredReadFromAttributes(Type, TargetName, Credential=None, encoding='utf-8'):
if Credential is None:
Credential = CredRead(Type, TargetName)
num_attrs = Credential['AttributeCount']
if num_attrs > 0:
attrs = Credential['Attributes']
accum = b''
for i, (key, attr) in enumerate(attrs.items()):
expected_key = '{}:{}'.format(ATTRIBUTE_KEYWORD, str(i))
if key == expected_key:
accum += string_at(attr['Value'], attr['ValueSize'])
else:
break
Credential['CredentialBlob'] = accum.decode(encoding)
Credential['CredentialBlobSize'] = len(Credential['CredentialBlob'])
return Credential
def _create_attribute(keyword, value):
return CREDENTIAL_ATTRIBUTEW(
Keyword=keyword,
Flags=0,
ValueSize=len(value),
Value=cast(c_char_p(value), LPBYTE),
)
def _password_to_attributes(cred_args, encoding):
password = cred_args['CredentialBlob']
pwd_len = len(password)
encoded_password = password.encode(encoding)
cred_args['CredentialBlob'] = None
cred_args['CredentialBlobSize'] = 0
n = CRED_MAX_VALUE_SIZE
max_shards = max((pwd_len + n - 1) // n, 1)
if max_shards > CRED_MAX_ATTRIBUTES:
raise ValueError(
MAX_PASSWORD_BYTES,
'password length {} is greater than max allowed ({})'.format(
pwd_len, MAX_PASSWORD_BYTES
),
)
cred_attrs = (CREDENTIAL_ATTRIBUTEW * max_shards)()
for i in range(0, max_shards):
keyword = '{}:{}'.format(ATTRIBUTE_KEYWORD, str(i))
shard = encoded_password[i * n : (i + 1) * n]
cred_attrs[i] = _create_attribute(keyword, shard)
cred_args['AttributeCount'] = max_shards
cred_args['Attributes'] = cred_attrs
# NOTE utf-16-le encodes without BOM, as does pywin32 and pywin32-ctypes
def CredWrite(credential, Flags, encoding='utf-16-le'):
cred_args = {}
cred_args.update(credential)
cred_args['Flags'] = Flags
if 'TargetAlias' not in cred_args:
cred_args['TargetAlias'] = None
if 'AttributeCount' not in cred_args:
cred_args['AttributeCount'] = 0
if 'Attributes' not in cred_args:
cred_args['Attributes'] = None
password = cred_args['CredentialBlob']
encoded_password = password.encode(encoding)
cred_args['CredentialBlob'] = cast(encoded_password, LPBYTE)
cred_args['CredentialBlobSize'] = len(encoded_password)
cred = CREDENTIALW(**cred_args)
_CredWriteW(byref(cred), Flags)
def CredWriteToAttributes(credential, Flags, encoding='utf-8'):
cred_args = {}
cred_args.update(credential)
cred_args['Flags'] = Flags
if 'TargetAlias' not in cred_args:
cred_args['TargetAlias'] = None
# Sets CredentialBlob, CredentialBlobSize, AttributeCount, Attributes
_password_to_attributes(cred_args, encoding)
cred = CREDENTIALW(**cred_args)
_CredWriteW(byref(cred), Flags)
def CredDelete(Type, TargetName):
_CredDeleteW(TargetName, Type, 0)
def CredEnumerate(Filter=None, Flags=CRED_ENUMERATE_ALL_CREDENTIALS):
pcount = pointer(DWORD())
ppcred = POINTER(POINTER(CREDENTIALW))()
_CredEnumerateW(Filter, Flags, pcount, byref(ppcred))
entries = []
for i in range(pcount.contents.value):
entries.append(ppcred[i].contents.asdict())
return entries
# endregion