Package dpkt :: Module netbios
[hide private]
[frames] | no frames]

Source Code for Module dpkt.netbios

  1  # $Id: netbios.py 23 2006-11-08 15:45:33Z dugsong $ 
  2  # -*- coding: utf-8 -*- 
  3  """Network Basic Input/Output System.""" 
  4  from __future__ import absolute_import 
  5   
  6  import struct 
  7   
  8  from . import dpkt 
  9  from . import dns 
 10   
 11   
12 -def encode_name(name):
13 """Return the NetBIOS first-level encoded name.""" 14 l = [] 15 for c in struct.pack('16s', name): 16 c = ord(c) 17 l.append(chr((c >> 4) + 0x41)) 18 l.append(chr((c & 0xf) + 0x41)) 19 return ''.join(l)
20 21
22 -def decode_name(nbname):
23 """Return the NetBIOS first-level decoded nbname.""" 24 if len(nbname) != 32: 25 return nbname 26 l = [] 27 for i in range(0, 32, 2): 28 l.append(chr(((ord(nbname[i]) - 0x41) << 4) | 29 ((ord(nbname[i + 1]) - 0x41) & 0xf))) 30 return ''.join(l).split('\x00', 1)[0]
31 32 # RR types 33 NS_A = 0x01 # IP address 34 NS_NS = 0x02 # Name Server 35 NS_NULL = 0x0A # NULL 36 NS_NB = 0x20 # NetBIOS general Name Service 37 NS_NBSTAT = 0x21 # NetBIOS NODE STATUS 38 39 # RR classes 40 NS_IN = 1 41 42 # NBSTAT name flags 43 NS_NAME_G = 0x8000 # group name (as opposed to unique) 44 NS_NAME_DRG = 0x1000 # deregister 45 NS_NAME_CNF = 0x0800 # conflict 46 NS_NAME_ACT = 0x0400 # active 47 NS_NAME_PRM = 0x0200 # permanent 48 49 # NBSTAT service names 50 nbstat_svcs = { 51 # (service, unique): list of ordered (name prefix, service name) tuples 52 (0x00, 0): [('', 'Domain Name')], 53 (0x00, 1): [('IS~', 'IIS'), ('', 'Workstation Service')], 54 (0x01, 0): [('__MSBROWSE__', 'Master Browser')], 55 (0x01, 1): [('', 'Messenger Service')], 56 (0x03, 1): [('', 'Messenger Service')], 57 (0x06, 1): [('', 'RAS Server Service')], 58 (0x1B, 1): [('', 'Domain Master Browser')], 59 (0x1C, 0): [('INet~Services', 'IIS'), ('', 'Domain Controllers')], 60 (0x1D, 1): [('', 'Master Browser')], 61 (0x1E, 0): [('', 'Browser Service Elections')], 62 (0x1F, 1): [('', 'NetDDE Service')], 63 (0x20, 1): [('Forte_$ND800ZA', 'DCA IrmaLan Gateway Server Service'), 64 ('', 'File Server Service')], 65 (0x21, 1): [('', 'RAS Client Service')], 66 (0x22, 1): [('', 'Microsoft Exchange Interchange(MSMail Connector)')], 67 (0x23, 1): [('', 'Microsoft Exchange Store')], 68 (0x24, 1): [('', 'Microsoft Exchange Directory')], 69 (0x2B, 1): [('', 'Lotus Notes Server Service')], 70 (0x2F, 0): [('IRISMULTICAST', 'Lotus Notes')], 71 (0x30, 1): [('', 'Modem Sharing Server Service')], 72 (0x31, 1): [('', 'Modem Sharing Client Service')], 73 (0x33, 0): [('IRISNAMESERVER', 'Lotus Notes')], 74 (0x43, 1): [('', 'SMS Clients Remote Control')], 75 (0x44, 1): [('', 'SMS Administrators Remote Control Tool')], 76 (0x45, 1): [('', 'SMS Clients Remote Chat')], 77 (0x46, 1): [('', 'SMS Clients Remote Transfer')], 78 (0x4C, 1): [('', 'DEC Pathworks TCPIP service on Windows NT')], 79 (0x52, 1): [('', 'DEC Pathworks TCPIP service on Windows NT')], 80 (0x87, 1): [('', 'Microsoft Exchange MTA')], 81 (0x6A, 1): [('', 'Microsoft Exchange IMC')], 82 (0xBE, 1): [('', 'Network Monitor Agent')], 83 (0xBF, 1): [('', 'Network Monitor Application')] 84 } 85 86
87 -def node_to_service_name(name_service_flags):
88 name, service, flags = name_service_flags 89 try: 90 unique = int(flags & NS_NAME_G == 0) 91 for namepfx, svcname in nbstat_svcs[(service, unique)]: 92 if name.startswith(namepfx): 93 return svcname 94 except KeyError: 95 pass 96 return ''
97 98
99 -class NS(dns.DNS):
100 """NetBIOS Name Service.""" 101
102 - class Q(dns.DNS.Q):
103 pass
104
105 - class RR(dns.DNS.RR):
106 """NetBIOS resource record.""" 107
108 - def unpack_rdata(self, buf, off):
109 if self.type == NS_A: 110 self.ip = self.rdata 111 elif self.type == NS_NBSTAT: 112 num = ord(self.rdata[0]) 113 off = 1 114 l = [] 115 for i in range(num): 116 name = self.rdata[off:off + 15].split(None, 1)[0].split('\x00', 1)[0] 117 service = ord(self.rdata[off + 15]) 118 off += 16 119 flags = struct.unpack('>H', self.rdata[off:off + 2])[0] 120 off += 2 121 l.append((name, service, flags)) 122 self.nodenames = l
123 # XXX - skip stats 124
125 - def pack_name(self, buf, name):
126 return dns.DNS.pack_name(self, buf, encode_name(name))
127
128 - def unpack_name(self, buf, off):
129 name, off = dns.DNS.unpack_name(self, buf, off) 130 return decode_name(name), off
131 132
133 -class Session(dpkt.Packet):
134 """NetBIOS Session Service.""" 135 __hdr__ = ( 136 ('type', 'B', 0), 137 ('flags', 'B', 0), 138 ('len', 'H', 0) 139 )
140 141 142 SSN_MESSAGE = 0 143 SSN_REQUEST = 1 144 SSN_POSITIVE = 2 145 SSN_NEGATIVE = 3 146 SSN_RETARGET = 4 147 SSN_KEEPALIVE = 5 148 149
150 -class Datagram(dpkt.Packet):
151 """NetBIOS Datagram Service.""" 152 __hdr__ = ( 153 ('type', 'B', 0), 154 ('flags', 'B', 0), 155 ('id', 'H', 0), 156 ('src', 'I', 0), 157 ('sport', 'H', 0), 158 ('len', 'H', 0), 159 ('off', 'H', 0) 160 )
161 162 163 DGRAM_UNIQUE = 0x10 164 DGRAM_GROUP = 0x11 165 DGRAM_BROADCAST = 0x12 166 DGRAM_ERROR = 0x13 167 DGRAM_QUERY = 0x14 168 DGRAM_POSITIVE = 0x15 169 DGRAM_NEGATIVE = 0x16 170