Skip to content

Commit 34254df

Browse files
committed
Improving NDR packing for some easy to fix corner cases
1 parent 0f49dff commit 34254df

2 files changed

Lines changed: 47 additions & 6 deletions

File tree

windows/rpc/ndr.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ def unpack(self, stream):
266266
rawguid = stream.partial_unpack("16s")[0]
267267
return gdef.IID.from_buffer_copy(rawguid)
268268

269+
@classmethod
270+
def get_alignment(self):
271+
return 1
272+
273+
269274
class NdrContextHandle(object):
270275
@classmethod
271276
def pack(cls, data):
@@ -278,6 +283,11 @@ def unpack(self, stream):
278283
attributes, rawguid = stream.partial_unpack("<I16s")
279284
return gdef.IID.from_buffer_copy(rawguid)
280285

286+
@classmethod
287+
def get_alignment(self):
288+
return 4
289+
290+
281291

282292
class NdrStructure(object):
283293
"""a NDR structure that tries to respect the rules of pointer packing, this class should be subclassed with
@@ -298,15 +308,19 @@ def pack(cls, data):
298308
res_size = 0
299309
pointed = []
300310
outstream = NdrWriteStream()
311+
pointed_to_pack = []
312+
# pointedoutstream = NdrWriteStream()
301313
for i, (member, memberdata) in enumerate(zip(cls.MEMBERS, data)):
302314
if hasattr(member, "pack_in_struct"):
303315
x, y = member.pack_in_struct(memberdata, i)
304-
outstream.align(member.get_alignment())
316+
assert len(x) == 4, "Pointer should be size 4"
317+
# Write the pointer
318+
outstream.align(4)
305319
outstream.write(x)
306-
# res.append(x)
307-
# res_size += len(x)
308320
if y is not None:
309-
pointed.append(y)
321+
# Store the info to the pointed to pack
322+
pointed_to_pack.append((member.subcls.get_alignment(), y))
323+
# pointedoutstream.write(y)
310324
elif hasattr(member, "pack_conformant"):
311325
size, data = member.pack_conformant(memberdata)
312326
outstream.align(member.get_alignment())
@@ -318,7 +332,11 @@ def pack(cls, data):
318332
packed_member = member.pack(memberdata)
319333
outstream.align(member.get_alignment())
320334
outstream.write(packed_member)
321-
return dword_pad(b"".join(conformant_size)) + outstream.get_data() + dword_pad(b"".join(pointed))
335+
# Pack the pointed to the stream
336+
for alignement, pointed_data in pointed_to_pack:
337+
outstream.align(alignement)
338+
outstream.write(pointed_data)
339+
return dword_pad(b"".join(conformant_size)) + outstream.get_data()
322340

323341
@classmethod
324342
def unpack(cls, stream):

windows/wintrust.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def check_signature(filename):
6666
win_trust_data.pPolicyCallbackData = None
6767
win_trust_data.pSIPClientData = None
6868
win_trust_data.dwUIChoice = WTD_UI_NONE
69-
win_trust_data.fdwRevocationChecks = WTD_REVOKE_NONE
69+
# win_trust_data.fdwRevocationChecks = WTD_REVOKE_NONE
70+
win_trust_data.fdwRevocationChecks = WTD_REVOKE_WHOLECHAIN
7071
win_trust_data.dwUnionChoice = WTD_CHOICE_FILE
7172
win_trust_data.dwStateAction = WTD_STATEACTION_VERIFY
7273
win_trust_data.hWVTStateData = None
@@ -119,6 +120,28 @@ def get_file_hash(filename):
119120
raise
120121
return buffer
121122

123+
def get_file_hash2(filename): #POC: name/API will change/disapear
124+
f = open(filename, "rb")
125+
handle = windows.utils.get_handle_from_file(f)
126+
127+
cathand = HANDLE()
128+
h = winproxy.CryptCATAdminAcquireContext2(cathand, None, "SHA256", None, 0)
129+
print(cathand)
130+
131+
size = DWORD(0)
132+
x = winproxy.CryptCATAdminCalcHashFromFileHandle2(cathand, handle, ctypes.byref(size), None, 0)
133+
buffer = (BYTE * size.value)()
134+
try:
135+
x = winproxy.CryptCATAdminCalcHashFromFileHandle2(cathand, handle, ctypes.byref(size), buffer, 0)
136+
except WindowsError as e:
137+
if e.winerror == 1006:
138+
# CryptCATAdminCalcHashFromFileHandle: [Error 1006]
139+
# The volume for a file has been externally altered so that the opened file is no longer valid.
140+
# (returned for empty file)
141+
return None
142+
raise
143+
return buffer
144+
122145

123146
def get_catalog_name_from_handle(handle):
124147
cat_info = CATALOG_INFO()

0 commit comments

Comments
 (0)