Skip to content

Commit 4becd0d

Browse files
authored
fix netbios name encoding and decoding (#469)
1 parent 4174c93 commit 4becd0d

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

dpkt/netbios.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
from . import dpkt
99
from . import dns
10+
from .compat import compat_ord
1011

1112

1213
def encode_name(name):
1314
"""Return the NetBIOS first-level encoded name."""
1415
l = []
15-
for c in struct.pack('16s', name):
16-
c = ord(c)
16+
for c in struct.pack('16s', name.encode()):
17+
c = compat_ord(c)
1718
l.append(chr((c >> 4) + 0x41))
1819
l.append(chr((c & 0xf) + 0x41))
1920
return ''.join(l)
@@ -29,6 +30,7 @@ def decode_name(nbname):
2930
((ord(nbname[i + 1]) - 0x41) & 0xf)))
3031
return ''.join(l).split('\x00', 1)[0]
3132

33+
3234
# RR types
3335
NS_A = 0x01 # IP address
3436
NS_NS = 0x02 # Name Server
@@ -122,9 +124,11 @@ def unpack_rdata(self, buf, off):
122124
self.nodenames = l
123125
# XXX - skip stats
124126

127+
# FIXME: dns.DNS.pack_name does not exist; dns.pack_name has a different signature
125128
def pack_name(self, buf, name):
126129
return dns.DNS.pack_name(self, buf, encode_name(name))
127130

131+
# FIXME: dns.DNS.unpack_name does not exist; dns.unpack_name?
128132
def unpack_name(self, buf, off):
129133
name, off = dns.DNS.unpack_name(self, buf, off)
130134
return decode_name(name), off
@@ -167,3 +171,24 @@ class Datagram(dpkt.Packet):
167171
DGRAM_QUERY = 0x14
168172
DGRAM_POSITIVE = 0x15
169173
DGRAM_NEGATIVE = 0x16
174+
175+
176+
def test_encode_name():
177+
assert encode_name('The NetBIOS name') == 'FEGIGFCAEOGFHEECEJEPFDCAGOGBGNGF'
178+
# rfc1002
179+
assert encode_name('FRED ') == 'EGFCEFEECACACACACACACACACACACACA'
180+
# https://github.com/kbandla/dpkt/issues/458
181+
assert encode_name('*') == 'CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
182+
183+
184+
def test_decode_name():
185+
assert decode_name('FEGIGFCAEOGFHEECEJEPFDCAGOGBGNGF') == 'The NetBIOS name'
186+
# original botched example from rfc1001
187+
assert decode_name('FEGHGFCAEOGFHEECEJEPFDCAHEGBGNGF') == 'Tge NetBIOS tame'
188+
assert decode_name('CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA') == '*'
189+
190+
191+
if __name__ == '__main__':
192+
test_encode_name()
193+
test_decode_name()
194+
print('Tests Successful...')

0 commit comments

Comments
 (0)