Skip to content

Commit f920ff5

Browse files
authored
Merge pull request #86 from hakril/add_ndr_tests
Add tests for NdrCString & NdrWString packing. Fix #84
2 parents c46f086 + d6ad334 commit f920ff5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

tests/test_apisetmap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def dumped_apisetmap_base_and_version(request):
2020
ctypes_data = ctypes.c_buffer(data)
2121
yield ctypes.addressof(ctypes_data), version
2222

23-
KNOWN_APISETMAP_PREFIX = ["api-", "ext-", "MS-Win-"]
23+
KNOWN_APISETMAP_PREFIX = ["api-", "ext-", "MS-Win-", "SchemaExt-"]
2424

2525
def verify_apisetmap_parsing(apisetmap_base, version=None):
2626
if version is not None:

tests/test_ndr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class ComplexAlignementStructure(ndr.NdrStructure):
5858
NDR_PACK_TEST_CASE = [
5959
# Simple case
6060
(ndr.make_structure([ndr.NdrLong, ndr.NdrLong]), (2, 2), b"\x02\x00\x00\x00\x02\x00\x00\x00"),
61+
# String case, test packing works + \x00 is added if not present in string
62+
(ndr.NdrCString, "Hello", b"\x06\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00Hello\x00PP"),
63+
(ndr.NdrCString, "Hello\x00", b"\x06\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00Hello\x00PP"),
64+
(ndr.NdrWString, "Hello", b"\x06\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00H\x00e\x00l\x00l\x00o\x00\x00\x00"),
65+
(ndr.NdrWString, "Hello\x00", b"\x06\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00H\x00e\x00l\x00l\x00o\x00\x00\x00"),
6166
# Test GUID packing
6267
(ndr.NdrGuid, gdef.GUID.from_string("42424242-42424242-4242-4242-424242424242"), b"BBBBBBBBBBBBBBBB"),
6368
# Test CtxHandle packing

windows/rpc/ndr.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ def pack(cls, data):
160160
return None
161161
if not data.endswith('\x00'):
162162
data += '\x00'
163+
# Technically windows NDR seems to accept any bitstream that ends with '\x00\x00' here
164+
# And not limited to valid utf-16
165+
# Exemple: b'\x41\x00\x00\xD8'
163166
data = data.encode("utf-16-le")
164167
l = (len(data) // 2)
165168
result = struct.pack("<3I", l, 0, l)
@@ -179,7 +182,7 @@ def unpack(cls, stream):
179182

180183
@classmethod
181184
def get_alignment(self):
182-
# Not sur, but size is on 4 bytes so...
185+
# Not sure, but size is on 4 bytes so...
183186
return 4
184187

185188
class NdrCString(object):
@@ -190,6 +193,10 @@ def pack(cls, data):
190193
return None
191194
if not data.endswith('\x00'):
192195
data += '\x00'
196+
# Windows NDR seems to accept any bitstream in a FC_C_CSTRING
197+
# I was able to send range(1, 256) + b"\x00"
198+
# For now play safe for user and only accept encoded with always keep the same number of bytes
199+
data = data.encode("ascii")
193200
l = len(data)
194201
result = struct.pack("<3I", l, 0, l)
195202
result += data

0 commit comments

Comments
 (0)