Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unicode encoding error fix #1838

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions impacket/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from __future__ import division
from __future__ import print_function
from struct import pack, unpack, calcsize
from six import b, PY3
from six import ensure_binary, PY3
from binascii import hexlify


Expand Down Expand Up @@ -197,7 +197,7 @@ def pack(self, format, data, field = None):

# quote specifier
if format[:1] == "'" or format[:1] == '"':
return b(format[1:])
return ensure_binary(format[1:])

# code specifier
two = format.split('=')
Expand Down Expand Up @@ -245,26 +245,26 @@ def pack(self, format, data, field = None):
# "printf" string specifier
if format[:1] == '%':
# format string like specifier
return b(format % data)
return ensure_binary(format % data)

# asciiz specifier
if format[:1] == 'z':
if isinstance(data,bytes):
return data + b('\0')
return bytes(b(data)+b('\0'))
return data + ensure_binary('\0')
return bytes(ensure_binary(data)+ensure_binary('\0'))

# unicode specifier
if format[:1] == 'u':
return bytes(data+b('\0\0') + (len(data) & 1 and b('\0') or b''))
return bytes(data+ensure_binary('\0\0') + (len(data) & 1 and ensure_binary('\0') or b''))

# DCE-RPC/NDR string specifier
if format[:1] == 'w':
if len(data) == 0:
data = b('\0\0')
data = ensure_binary('\0\0')
elif len(data) % 2:
data = b(data) + b('\0')
data = ensure_binary(data) + ensure_binary('\0')
l = pack('<L', len(data)//2)
return b''.join([l, l, b('\0\0\0\0'), data])
return b''.join([l, l, ensure_binary('\0\0\0\0'), data])

if data is None:
raise Exception("Trying to pack None")
Expand All @@ -279,7 +279,7 @@ def pack(self, format, data, field = None):
elif isinstance(data, int):
return bytes(data)
elif isinstance(data, bytes) is not True:
return bytes(b(data))
return bytes(ensure_binary(data))
else:
return data

Expand All @@ -288,7 +288,7 @@ def pack(self, format, data, field = None):
if isinstance(data, bytes) or isinstance(data, bytearray):
return pack(format, data)
else:
return pack(format, b(data))
return pack(format, ensure_binary(data))

# struct like specifier
return pack(format, data)
Expand All @@ -315,7 +315,7 @@ def unpack(self, format, data, dataClassOrCode = b, field = None):
# quote specifier
if format[:1] == "'" or format[:1] == '"':
answer = format[1:]
if b(answer) != data:
if ensure_binary(answer) != data:
raise Exception("Unpacked data doesn't match constant value '%r' should be '%r'" % (data, answer))
return answer

Expand Down Expand Up @@ -361,7 +361,7 @@ def unpack(self, format, data, dataClassOrCode = b, field = None):

# asciiz specifier
if format == 'z':
if data[-1:] != b('\x00'):
if data[-1:] != ensure_binary('\x00'):
raise Exception("%s 'z' field is not NUL terminated: %r" % (field, data))
if PY3:
return data[:-1].decode('latin-1')
Expand All @@ -370,7 +370,7 @@ def unpack(self, format, data, dataClassOrCode = b, field = None):

# unicode specifier
if format == 'u':
if data[-2:] != b('\x00\x00'):
if data[-2:] != ensure_binary('\x00\x00'):
raise Exception("%s 'u' field is not NUL-NUL terminated: %r" % (field, data))
return data[:-2] # remove trailing NUL

Expand Down Expand Up @@ -524,11 +524,11 @@ def calcUnpackSize(self, format, data, field = None):

# asciiz specifier
if format[:1] == 'z':
return data.index(b('\x00'))+1
return data.index(ensure_binary('\x00'))+1

# asciiz specifier
if format[:1] == 'u':
l = data.index(b('\x00\x00'))
l = data.index(ensure_binary('\x00\x00'))
return l + (l & 1 and 3 or 2)

# DCE-RPC/NDR string specifier
Expand Down Expand Up @@ -584,7 +584,7 @@ def zeroValue(self, format):
if format in ['z',':','u']:
return b''
if format == 'w':
return b('\x00\x00')
return ensure_binary('\x00\x00')

return 0

Expand Down