Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Impacket - Collection of Python classes for working with network protocols. 

2# 

3# SECUREAUTH LABS. Copyright (C) 2020 SecureAuth Corporation. All rights reserved. 

4# 

5# This software is provided under a slightly modified version 

6# of the Apache Software License. See the accompanying LICENSE file 

7# for more information. 

8# 

9# Description: 

10# Generate UUID compliant with http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt. 

11# A different, much simpler (not necessarily better) algorithm is used. 

12# 

13# Author: 

14# Javier Kohen (jkohen) 

15# 

16 

17from __future__ import absolute_import 

18from __future__ import print_function 

19import re 

20import binascii 

21 

22from random import randrange 

23from struct import pack, unpack 

24 

25EMPTY_UUID = b'\x00'*16 

26 

27 

28def generate(): 

29 # UHm... crappy Python has an maximum integer of 2**31-1. 

30 top = (1<<31)-1 

31 return pack("IIII", randrange(top), randrange(top), randrange(top), randrange(top)) 

32 

33 

34def bin_to_string(uuid): 

35 uuid1, uuid2, uuid3 = unpack('<LHH', uuid[:8]) 

36 uuid4, uuid5, uuid6 = unpack('>HHL', uuid[8:16]) 

37 return '%08X-%04X-%04X-%04X-%04X%08X' % (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) 

38 

39 

40def string_to_bin(uuid): 

41 # If a UUID in the 00000000000000000000000000000000 format, let's return bytes as is 

42 if '-' not in uuid: 42 ↛ 43line 42 didn't jump to line 43, because the condition on line 42 was never true

43 return binascii.unhexlify(uuid) 

44 

45 # If a UUID in the 00000000-0000-0000-0000-000000000000 format, parse it as Variant 2 UUID 

46 # The first three components of the UUID are little-endian, and the last two are big-endian 

47 matches = re.match(r"([\dA-Fa-f]{8})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})-([\dA-Fa-f]{4})([\dA-Fa-f]{8})", 

48 uuid) 

49 (uuid1, uuid2, uuid3, uuid4, uuid5, uuid6) = [int(x, 16) for x in matches.groups()] 

50 uuid = pack('<LHH', uuid1, uuid2, uuid3) 

51 uuid += pack('>HHL', uuid4, uuid5, uuid6) 

52 return uuid 

53 

54 

55def stringver_to_bin(s): 

56 (maj, min) = s.split('.') 

57 return pack('<H', int(maj)) + pack('<H', int(min)) 

58 

59 

60def uuidtup_to_bin(tup): 

61 if len(tup) != 2: 61 ↛ 62line 61 didn't jump to line 62, because the condition on line 61 was never true

62 return 

63 return string_to_bin(tup[0]) + stringver_to_bin(tup[1]) 

64 

65 

66def bin_to_uuidtup(bin): 

67 assert len(bin) == 20 

68 uuidstr = bin_to_string(bin[:16]) 

69 maj, min = unpack("<HH", bin[16:]) 

70 return uuidstr, "%d.%d" % (maj, min) 

71 

72 

73def string_to_uuidtup(s): 

74 """ 

75 if version is not found in the input string "1.0" is returned 

76 example: 

77 "00000000-0000-0000-0000-000000000000 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') 

78 "10000000-2000-3000-4000-500000000000 version 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') 

79 "10000000-2000-3000-4000-500000000000 v 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') 

80 "10000000-2000-3000-4000-500000000000" returns ('00000000-0000-0000-0000-000000000000','1.0') 

81 

82 :param s: string 

83 :return: tuple (uuid,version) 

84 """ 

85 g = re.search(r"([A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}).*?([0-9]{1,5}\.[0-9]{1,5})", 

86 s + " 1.0") 

87 if g: 87 ↛ 90line 87 didn't jump to line 90, because the condition on line 87 was never false

88 (u, v) = g.groups() 

89 return u, v 

90 return 

91 

92 

93def uuidtup_to_string(tup): 

94 uuid, (maj, min) = tup 

95 return "%s v%d.%d" % (uuid, maj, min)