Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/ms_adts/key_credential.rb
Views: 11704
module Rex::Proto::MsAdts1class KeyCredential23KEY_USAGE_NGC = 0x014KEY_USAGE_FIDO = 0x075KEY_USAGE_FEK = 0x0867KEY_CREDENTIAL_VERSION_2 = 0x2008DEFAULT_KEY_INFORMATION = "\x01\x00" # Version and flags910def initialize11self.key_source = 012self.device_id = Rex::Proto::MsDtyp::MsDtypGuid.new13self.device_id.set(Rex::Proto::MsDtyp::MsDtypGuid.random_generate)14self.custom_key_information = DEFAULT_KEY_INFORMATION15end1617# Set the key material for this credential object18# @param public_key [OpenSSL::PKey::RSA] Public key used for authentication19# @param key_usage [Enumeration] From the KEY_USAGE constants in this class20def set_key(public_key, key_usage)21self.key_usage = key_usage2223case self.key_usage24when KEY_USAGE_NGC25result = Rex::Proto::BcryptPublicKey.new26result.key_length = public_key.n.num_bits27n = self.class.int_to_bytes(public_key.n)28e = self.class.int_to_bytes(public_key.e)29result.exponent = e30result.modulus = n31result.prime1 = ''32result.prime2 = ''33self.raw_key_material = result.to_binary_s34else35# Unknown key type36return37end38sha256 = OpenSSL::Digest.new('SHA256')39self.key_id = sha256.digest(self.raw_key_material)40end4142# Approximate time this key was last used43# @return [Time] Approximate time this key was last used44def key_approximate_last_logon_time45ft = key_approximate_last_logon_time_raw46RubySMB::Field::FileTime.new(ft.unpack('Q')[0]).to_time47end4849# Set the approximate last logon time for this credential object50# @param time [Time] Last time this credential was used to log on51def key_approximate_last_logon_time=(time)52self.key_approximate_last_logon_time_raw = RubySMB::Field::FileTime.new(time).to_binary_s53end5455# Approximate time this key was created56# @return [Time] Approximate time this key was created57def key_creation_time58ft = key_creation_time_raw59RubySMB::Field::FileTime.new(ft.unpack('Q')[0]).to_time60end6162# Set the creation time for this credential object63# @param time [Time] Time that this key was created64def key_creation_time=(time)65self.key_creation_time_raw = RubySMB::Field::FileTime.new(time).to_binary_s66end6768# Creates a MsAdtsKeyCredentialStruct, including calculating the value for key_hash69# @return [MsAdtsKeyCredentialStruct] A structured object able to be converted to binary and sent to a DCc70def to_struct71result = MsAdtsKeyCredentialStruct.new72result.version = KEY_CREDENTIAL_VERSION_273add_entry(result, 3, self.raw_key_material)74add_entry(result, 4, [self.key_usage].pack('C'))75add_entry(result, 5, [self.key_source].pack('C'))76add_entry(result, 6, self.device_id.to_binary_s)77add_entry(result, 7, self.custom_key_information)78add_entry(result, 8, self.key_approximate_last_logon_time_raw)79add_entry(result, 9, self.key_creation_time_raw)8081calculate_key_hash(result)8283add_entry(result, 2, self.key_hash, insert_at_end: false)84add_entry(result, 1, self.key_id, insert_at_end: false)8586result87end8889# Construct a KeyCredential object from a MsAdtsKeyCredentialStruct (likely received from a Domain Controller)90# @param cred_struct [MsAdtsKeyCredentialStruct] Credential structure to convert91def self.from_struct(cred_struct)92obj = KeyCredential.new93obj.key_id = get_entry(cred_struct, 1)94obj.key_hash = get_entry(cred_struct, 2)95obj.raw_key_material = get_entry(cred_struct, 3)96obj.key_usage = get_entry(cred_struct, 4).unpack('C')[0]97obj.key_source = get_entry(cred_struct, 5).unpack('C')[0]98obj.device_id = Rex::Proto::MsDtyp::MsDtypGuid.read(get_entry(cred_struct, 6))99obj.custom_key_information = get_entry(cred_struct, 7)100ft = get_entry(cred_struct, 8)101obj.key_approximate_last_logon_time_raw = ft102ft = get_entry(cred_struct, 9)103obj.key_creation_time_raw = ft104105obj106end107108# Properties109attr_accessor :key_id # SHA256 hash of KeyMaterial110attr_accessor :key_hash # SHA256 hash of all entries after this entry111attr_accessor :raw_key_material # Key material of the credential, in bytes112attr_accessor :key_usage # Enumeration113attr_accessor :key_source # Always KEY_SOURCE_AD (0)114attr_accessor :device_id # [MsDtypGuid] Identifier for this credential115attr_accessor :custom_key_information # Two bytes is fine: Version and Flags116attr_accessor :key_approximate_last_logon_time_raw # Raw bytes for approximate time this key was last used117attr_accessor :key_creation_time_raw # Raw bytes for approximate time this key was created118119# Find the entry with the given identifier120# @param struct [MsAdtsKeyCredentialStruct] Structure containing entries to search through121# @param struct [Integer] Identifier to search for, from https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/a99409ea-4982-4f72-b7ef-8596013a36c7122# @return [String] The data associated with this identifier, or nil if not found123def self.get_entry(struct, identifier)124struct.credential_entries.each do |entry|125if entry.identifier == identifier126return entry.data127end128end129end130131# Parse the object's raw key material field into a OpenSSL::PKey::RSA object132# @param obj [KeyCredential] The object for which to parse the key133def public_key134case key_usage135when KEY_USAGE_NGC136if raw_key_material.start_with?([Rex::Proto::BcryptPublicKey::MAGIC].pack('I'))137result = Rex::Proto::BcryptPublicKey.read(raw_key_material)138exponent = OpenSSL::ASN1::Integer.new(bytes_to_int(result.exponent))139modulus = OpenSSL::ASN1::Integer.new(bytes_to_int(result.modulus))140# OpenSSL's API has changed over time - constructing from DER has been consistent141data_sequence = OpenSSL::ASN1::Sequence([modulus, exponent])142143OpenSSL::PKey::RSA.new(data_sequence.to_der)144end145end146end147148private149150# Create a MsAdtsKeyCredentialEntryStruct from the provided data, and insert it in to the provided structure151# @param struct [MsAdtsKeyCredentialStruct] Structure to insert the resulting entry into152# @param identifier [Integer] Identifier associated with this entry, from https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/a99409ea-4982-4f72-b7ef-8596013a36c7153# @param data [String] The data to create an entry from154# @param insert_at_end [Boolean] Whether to insert the new entry at the end of the credential_entries; otherwise will insert at start155def add_entry(struct, identifier, data, insert_at_end: true)156entry = MsAdtsKeyCredentialEntryStruct.new157entry.identifier = identifier158entry.data = data159entry.struct_length = data.length160if insert_at_end161struct.credential_entries.insert(struct.credential_entries.length, entry)162else # Insert at start163struct.credential_entries.insert(0, entry)164end165end166167def self.int_to_bytes(num)168str = num.to_s(16).rjust(2, '0')169170[str].pack('H*')171end172173def bytes_to_int(num)174num.unpack('H*')[0].to_i(16)175end176177# Sets self.key_hash based on the credential_entries value in the provided parameter178# @param struct [MsAdtsKeyCredentialStruct] Its credential_entries value should have only those required to calculate the key_hash value (no key_id or key_hash)179def calculate_key_hash(struct)180sha256 = OpenSSL::Digest.new('SHA256')181self.key_hash = sha256.digest(struct.credential_entries.to_binary_s)182end183end184end185186