Coverage for /root/GitHubProjects/impacket/impacket/dcerpc/v5/bkrp.py: 82%

# Coverage for /root/GitHubProjects/impacket/impacket/dcerpc/v5/bkrp.py : 82%

## 46 statements

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# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.

2#

3# This software is provided under under a slightly modified version

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

5# for more information.

6#

7# Author: Alberto Solino (@agsolino)

8#

9# Description:

10# [MS-BKRP] Interface implementation

11#

12# Best way to learn how to use these calls is to grab the protocol standard

13# so you understand what the call does, and then read the test case located

14# at https://github.com/SecureAuthCorp/impacket/tree/master/tests/SMB_RPC

15#

16# Some calls have helper functions, which makes it even easier to use.

17# They are located at the end of this file.

18# Helper functions start with "h"<name of the call>.

19# There are test cases for them too.

20#

21# ToDo:

22# [ ] 2.2.2 Client-Side-Wrapped Secret

23 from __future__ import division

24 from __future__ import print_function

25 from impacket.dcerpc.v5.ndr import NDRCALL, NDRPOINTER, NDRUniConformantArray

26 from impacket.dcerpc.v5.dtypes import DWORD, NTSTATUS, GUID, RPC_SID, NULL

27 from impacket.dcerpc.v5.rpcrt import DCERPCException

28 from impacket import system_errors

29 from impacket.uuid import uuidtup_to_bin, string_to_bin

30 from impacket.structure import Structure

31

32 MSRPC_UUID_BKRP = uuidtup_to_bin(('3dde7c30-165d-11d1-ab8f-00805f14db40', '1.0'))

33

34 class DCERPCSessionError(DCERPCException):

35 def __init__(self, error_string=None, error_code=None, packet=None):

36 DCERPCException.__init__(self, error_string, error_code, packet)

37

38 def __str__( self ):

39 key = self.error_code

40 if key in system_errors.ERROR_MESSAGES:

41 error_msg_short = system_errors.ERROR_MESSAGES[key][0]

42 error_msg_verbose = system_errors.ERROR_MESSAGES[key][1]

43 return 'BKRP SessionError: code: 0x%x - %s - %s' % (self.error_code, error_msg_short, error_msg_verbose)

44 else:

45 return 'BKRP SessionError: unknown error code: 0x%x' % self.error_code

46

47################################################################################

48# CONSTANTS

49################################################################################

50

51 BACKUPKEY_BACKUP_GUID = string_to_bin("7F752B10-178E-11D1-AB8F-00805F14DB40")

52 BACKUPKEY_RESTORE_GUID_WIN2K = string_to_bin("7FE94D50-178E-11D1-AB8F-00805F14DB40")

53 BACKUPKEY_RETRIEVE_BACKUP_KEY_GUID = string_to_bin("018FF48A-EABA-40C6-8F6D-72370240E967")

54 BACKUPKEY_RESTORE_GUID = string_to_bin("47270C64-2FC7-499B-AC5B-0E37CDCE899A")

55

56################################################################################

57# STRUCTURES

58################################################################################

59 class BYTE_ARRAY(NDRUniConformantArray):

60 item = 'c'

61

62 class PBYTE_ARRAY(NDRPOINTER):

63 referent = (

64 ('Data', BYTE_ARRAY),

65 )

66

67# 2.2.4.1 Rc4EncryptedPayload Structure

68 class Rc4EncryptedPayload(Structure):

69 structure = (

70 ('R3', '32s=""'),

71 ('MAC', '20s=""'),

72 ('SID', ':', RPC_SID),

73 ('Secret', ':'),

74 )

75

76# 2.2.4 Secret Wrapped with Symmetric Key

77 class WRAPPED_SECRET(Structure):

78 structure = (

79 ('SIGNATURE', '<L=1'),

80 ('Payload_Length', '<L=0'),

81 ('Ciphertext_Length', '<L=0'),

82 ('GUID_of_Wrapping_Key', '16s=""'),

83 ('R2', '68s=""'),

84 ('_Rc4EncryptedPayload', '_-Rc4EncryptedPayload', 'self["Payload_Length"]'),

85 ('Rc4EncryptedPayload', ':'),

86 )

87

88################################################################################

89# RPC CALLS

90################################################################################

91# 3.1.4.1 BackuprKey(Opnum 0)

92 class BackuprKey(NDRCALL):

93 opnum = 0

94 structure = (

95 ('pguidActionAgent', GUID),

96 ('pDataIn', BYTE_ARRAY),

97 ('cbDataIn', DWORD),

98 ('dwParam', DWORD),

99 )

100

101 class BackuprKeyResponse(NDRCALL):

102 structure = (

103 ('ppDataOut', PBYTE_ARRAY),

104 ('pcbDataOut', DWORD),

105 ('ErrorCode', NTSTATUS),

106 )

107

108################################################################################

109# OPNUMs and their corresponding structures

110################################################################################

111 OPNUMS = {

112 0 : (BackuprKey, BackuprKeyResponse),

113}

114

115################################################################################

116# HELPER FUNCTIONS

117################################################################################

118 def hBackuprKey(dce, pguidActionAgent, pDataIn, dwParam=0):

119 request = BackuprKey()

120 request['pguidActionAgent'] = pguidActionAgent

121 request['pDataIn'] = pDataIn

122 if pDataIn == NULL:

123 request['cbDataIn'] = 0

124 else:

125 request['cbDataIn'] = len(pDataIn)

126 request['dwParam'] = dwParam

127 return dce.request(request)