# Impacket - Collection of Python classes for working with network protocols. # # Copyright Fortra, LLC and its affiliated companies # # All rights reserved. # # This software is provided under a slightly modified version # of the Apache Software License. See the accompanying LICENSE file # for more information. # # Description: # RFC 1964 Partial Implementation # RFC 4757 Partial Implementation # RFC 4121 Partial Implementation # RFC 3962 Partial Implementation # # Author: # Alberto Solino (@agsolino) # import struct import random import string from six import b from Cryptodome.Hash import HMAC, MD5 from Cryptodome.Cipher import ARC4 from impacket.structure import Structure from impacket.krb5 import constants, crypto # Our random number generator try: rand = random.SystemRandom() except NotImplementedError: rand = random pass # Constants GSS_C_DCE_STYLE = 0x1000 GSS_C_DELEG_FLAG = 1 GSS_C_MUTUAL_FLAG = 2 GSS_C_REPLAY_FLAG = 4 GSS_C_SEQUENCE_FLAG = 8 GSS_C_CONF_FLAG = 0x10 GSS_C_INTEG_FLAG = 0x20 # Mic Semantics GSS_HMAC = 0x11 # Wrap Semantics GSS_RC4 = 0x10 # 2. Key Derivation for Per-Message Tokens KG_USAGE_ACCEPTOR_SEAL = 22 KG_USAGE_ACCEPTOR_SIGN = 23 KG_USAGE_INITIATOR_SEAL = 24 KG_USAGE_INITIATOR_SIGN = 25 KRB5_AP_REQ = struct.pack('L', sequenceNumber) + b'\x00'*4 else: token['SND_SEQ'] = struct.pack('>L', sequenceNumber) + b'\xff'*4 Ksign = HMAC.new(sessionKey.contents, b'signaturekey\0', MD5).digest() Sgn_Cksum = MD5.new( struct.pack('L', sequenceNumber) + b'\x00'*4 else: token['SND_SEQ'] = struct.pack('>L', sequenceNumber) + b'\xff'*4 # Random confounder :) token['Confounder'] = b(''.join([rand.choice(string.ascii_letters) for _ in range(8)])) Ksign = HMAC.new(sessionKey.contents, b'signaturekey\0', MD5).digest() Sgn_Cksum = MD5.new(struct.pack('L', sequenceNumber), MD5).digest() Sgn_Cksum = HMAC.new(Ksign, Sgn_Cksum, MD5).digest() token['SGN_CKSUM'] = Sgn_Cksum[:8] Kseq = HMAC.new(sessionKey.contents, struct.pack('L', sequenceNumber) + b'\x00'*4 else: token['SND_SEQ'] = struct.pack('>L', sequenceNumber) + b'\xff'*4 # Random confounder :) token['Confounder'] = b(''.join([rand.choice(string.ascii_letters) for _ in range(8)])) Ksign = HMAC.new(sessionKey.contents, b'signaturekey\0', MD5).digest() Sgn_Cksum = MD5.new(struct.pack('L', sequenceNumber), MD5).digest() Sgn_Cksum = HMAC.new(Ksign, Sgn_Cksum, MD5).digest() token['SGN_CKSUM'] = Sgn_Cksum[:8] Kseq = HMAC.new(sessionKey.contents, struct.pack('H=0x0404'), ('Flags','B=0'), ('Filler0','B=0xff'), ('Filler','>L=0xffffffff'), ('SND_SEQ','8s=b""'), ('SGN_CKSUM','12s=b""'), ) # 1.2.2. Per-message Tokens - Wrap class WRAP(Structure): structure = ( ('TOK_ID','>H=0x0504'), ('Flags','B=0'), ('Filler','B=0xff'), ('EC','>H=0'), ('RRC','>H=0'), ('SND_SEQ','8s=b""'), ) def GSS_GetMIC(self, sessionKey, data, sequenceNumber, direction = 'init'): token = self.MIC() pad = _calculateMICPad(data) data += pad checkSumProfile = self.checkSumProfile() token['Flags'] = 4 token['SND_SEQ'] = struct.pack('>Q',sequenceNumber) token['SGN_CKSUM'] = checkSumProfile.checksum(sessionKey, KG_USAGE_INITIATOR_SIGN, data + token.getData()[:16]) return token.getData() def rotate(self, data, numBytes): numBytes %= len(data) left = len(data) - numBytes result = data[left:] + data[:left] return result def unrotate(self, data, numBytes): numBytes %= len(data) result = data[numBytes:] + data[:numBytes] return result def GSS_Wrap(self, sessionKey, data, sequenceNumber, direction = 'init', encrypt=True): token = self.WRAP() cipher = self.cipherType() # Let's pad the data pad = (cipher.blocksize - (len(data) % cipher.blocksize)) & 15 padStr = b'\xFF' * pad data += padStr # The RRC field ([RFC4121] section 4.2.5) is 12 if no encryption is requested or 28 if encryption # is requested. The RRC field is chosen such that all the data can be encrypted in place. rrc = 28 token['Flags'] = 6 token['EC'] = pad token['RRC'] = 0 token['SND_SEQ'] = struct.pack('>Q',sequenceNumber) cipherText = cipher.encrypt(sessionKey, KG_USAGE_INITIATOR_SEAL, data + token.getData(), None) token['RRC'] = rrc cipherText = self.rotate(cipherText, token['RRC'] + token['EC']) #nn = self.unrotate(cipherText, token['RRC'] + token['EC']) ret1 = cipherText[len(self.WRAP()) + token['RRC'] + token['EC']:] ret2 = token.getData() + cipherText[:len(self.WRAP()) + token['RRC'] + token['EC']] return ret1, ret2 def GSS_Unwrap(self, sessionKey, data, sequenceNumber, direction = 'init', encrypt=True, authData=None): from impacket.dcerpc.v5.rpcrt import SEC_TRAILER cipher = self.cipherType() token = self.WRAP(authData[len(SEC_TRAILER()):]) rotated = authData[len(self.WRAP())+len(SEC_TRAILER()):] + data cipherText = self.unrotate(rotated, token['RRC'] + token['EC']) plainText = cipher.decrypt(sessionKey, KG_USAGE_ACCEPTOR_SEAL, cipherText) return plainText[:-(token['EC']+len(self.WRAP()))], None def GSS_Wrap_LDAP(self, sessionKey, data, sequenceNumber, direction = 'init', encrypt=True, use_padding=False): token = self.WRAP() cipher = self.cipherType() pad = 0 if use_padding: # Let's pad the data pad = (cipher.blocksize - (len(data) % cipher.blocksize)) & 15 padStr = b'\xFF' * pad data += padStr # The RRC field ([RFC4121] section 4.2.5) is 12 if no encryption is requested or 28 if encryption # is requested. The RRC field is chosen such that all the data can be encrypted in place. rrc = 28 token['Flags'] = 6 token['EC'] = pad token['RRC'] = 0 token['SND_SEQ'] = struct.pack('>Q',sequenceNumber) cipherText = cipher.encrypt(sessionKey, KG_USAGE_INITIATOR_SEAL, data + token.getData(), None) token['RRC'] = rrc cipherText = self.rotate(cipherText, token['RRC'] + token['EC']) #nn = self.unrotate(cipherText, token['RRC'] + token['EC']) # ret1 = cipherText[len(self.WRAP()) + token['RRC'] + token['EC']:] # ret2 = token.getData() + cipherText[:len(self.WRAP()) + token['RRC'] + token['EC']] ret1 = cipherText ret2 = token.getData() return ret1, ret2 def GSS_Unwrap_LDAP(self, sessionKey, data, sequenceNumber, direction = 'init'): cipher = self.cipherType() token = self.WRAP(data[:16]) rotated = data[16:] cipherText = self.unrotate(rotated, token['RRC'] + token['EC']) plainText = cipher.decrypt(sessionKey, KG_USAGE_ACCEPTOR_SEAL, cipherText) return plainText[:-(token['EC']+ 16)], None class GSSAPI_AES256(GSSAPI_AES): checkSumProfile = crypto._SHA1AES256 cipherType = crypto._AES256CTS class GSSAPI_AES128(GSSAPI_AES): checkSumProfile = crypto._SHA1AES128 cipherType = crypto._AES128CTS