Package killerbee :: Module kbutils
[hide private]
[frames] | no frames]

Source Code for Module killerbee.kbutils

 1  import random 
 2   
3 -def hexdump(src, length=16):
4 ''' 5 Creates a tcpdump-style hex dump string output. 6 @type src: String 7 @param src: Input string to convert to hexdump output. 8 @type length: Int 9 @param length: Optional length of data for a single row of output, def=16 10 @rtype: String 11 ''' 12 FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or '.' for x in range(256)]) 13 result = [] 14 for i in xrange(0, len(src), length): 15 chars = src[i:i+length] 16 hex = ' '.join(["%02x" % ord(x) for x in chars]) 17 printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or '.') for x in chars]) 18 result.append("%04x: %-*s %s\n" % (i, length*3, hex, printable)) 19 return ''.join(result)
20
21 -def randbytes(size):
22 ''' 23 Returns a random string of size bytes. Not cryptographically safe. 24 @type size: Int 25 @param size: Length of random data to return. 26 @rtype: String 27 ''' 28 return ''.join(chr(random.randrange(0,256)) for i in xrange(size))
29
30 -def randmac(length=8):
31 ''' 32 Returns a random MAC address using a list valid OUI's from ZigBee device manufacturers. Data is returned in air-format byte order (LSB first). 33 @type length: String 34 @param length: Optional length of MAC address, def=8. Minimum address return length is 3 bytes for the valid OUI. 35 @rtype: String 36 ''' 37 # Valid OUI prefixes for MAC addresses 38 prefixes = [ "\x00\x0d\x6f", # Ember 39 "\x00\x12\x4b", # TI 40 "\x00\x04\xa3", # Microchip 41 "\x00\x04\x25", # Atmel 42 "\x00\x11\x7d", # ZMD 43 "\x00\x13\xa2", # MaxStream 44 "\x00\x30\x66", # Cirronet 45 "\x00\x0b\x57", # Silicon Laboratories 46 "\x00\x04\x9f", # Freescale Semiconductor 47 "\x00\x21\xed", # Telegesis 48 "\x00\xa0\x50" # Cypress 49 ] 50 51 prefix = prefixes[random.randrange(0, len(prefixes))] 52 suffix = randbytes(length-3) 53 # Reverse the address for use in a packet 54 return ''.join([prefix, suffix])[::-1]
55