Module: Msf::Exploit::Remote::SMB::Server::HashCapture

Includes:
Auxiliary::Report
Included in:
Share, Payload::Adapter::Fetch::Server::SMB
Defined in:
lib/msf/core/exploit/remote/smb/server/hash_capture.rb

Overview

This mixin provides support for reporting captured SMB creds

Defined Under Namespace

Classes: HashCaptureAuthenticator, HashCaptureNTLMProvider

Instance Method Summary collapse

Methods included from Auxiliary::Report

#active_db?, #create_cracked_credential, #create_credential, #create_credential_and_login, #create_credential_login, #db, #db_warning_given?, #get_client, #get_host, #inside_workspace_boundary?, #invalidate_login, #mytask, #myworkspace, #myworkspace_id, #report_auth_info, #report_client, #report_exploit, #report_host, #report_loot, #report_note, #report_service, #report_vuln, #report_web_form, #report_web_page, #report_web_site, #report_web_vuln, #store_cred, #store_local, #store_loot

Methods included from Metasploit::Framework::Require

optionally, optionally_active_record_railtie, optionally_include_metasploit_credential_creation, #optionally_include_metasploit_credential_creation, optionally_require_metasploit_db_gem_engines

Instance Method Details

#bin_to_hex(str) ⇒ Object



173
174
175
# File 'lib/msf/core/exploit/remote/smb/server/hash_capture.rb', line 173

def bin_to_hex(str)
  str.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
end

#build_jtr_file_name(jtr_format) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/msf/core/exploit/remote/smb/server/hash_capture.rb', line 151

def build_jtr_file_name(jtr_format)
  # JTR NTLM hash format NTLMv1
  # Username::Domain:LMHash:NTHash:Challenge
  #
  # JTR NTLM hash format NTLMv2
  # Username::Domain:Challenge:NTHash[0...16]:NTHash[16...-1]

  path = File.expand_path(datastore['JOHNPWFILE'], Msf::Config.install_root)

  # if the passed file name does not contain an extension
  if File.extname(File.basename(path)).empty?
    path += "_#{jtr_format}"
  else
    path_parts = path.split('.')

    # inserts _jtr_format between the last extension and the rest of the path
    path = "#{path_parts[0...-1].join('.')}_#{jtr_format}.#{path_parts[-1]}"
  end

  path
end

#on_ntlm_type3(address:, ntlm_type1:, ntlm_type2:, ntlm_type3:) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/msf/core/exploit/remote/smb/server/hash_capture.rb', line 142

def on_ntlm_type3(address:, ntlm_type1:, ntlm_type2:, ntlm_type3:)
  report_ntlm_type3(
    address: address,
    ntlm_type1: ntlm_type1,
    ntlm_type2: ntlm_type2,
    ntlm_type3: ntlm_type3
  )
end

#report_ntlm_type3(address:, ntlm_type1:, ntlm_type2:, ntlm_type3:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/msf/core/exploit/remote/smb/server/hash_capture.rb', line 31

def report_ntlm_type3(address:, ntlm_type1:, ntlm_type2:, ntlm_type3:)
  ntlm_message = ntlm_type3
  hash_type = nil

  user = ntlm_message.user.force_encoding(::Encoding::UTF_16LE).encode(''.encoding)
  domain = ntlm_message.domain.force_encoding(::Encoding::UTF_16LE).encode(''.encoding)
  challenge = [ntlm_type2.challenge].pack('Q<')
  combined_hash = "#{user}::#{domain}"

  case ntlm_message.ntlm_version
  when :ntlmv1, :ntlm2_session
    hash_type = 'NTLMv1-SSP'
    client_hash = "#{bin_to_hex(ntlm_message.lm_response)}:#{bin_to_hex(ntlm_message.ntlm_response)}"

    combined_hash << ":#{client_hash}"
    combined_hash << ":#{bin_to_hex(challenge)}"
    jtr_format = Metasploit::Framework::Hashes::JTR_NTLMV1
  when :ntlmv2
    hash_type = 'NTLMv2-SSP'
    client_hash = "#{bin_to_hex(ntlm_message.ntlm_response[0...16])}:#{bin_to_hex(ntlm_message.ntlm_response[16..-1])}"

    combined_hash << ":#{bin_to_hex(challenge)}"
    combined_hash << ":#{client_hash}"
    jtr_format = Metasploit::Framework::Hashes::JTR_NTLMV2
  end

  return if hash_type.nil?

  jtr_format = ntlm_message.ntlm_version == :ntlmv1 ? Metasploit::Framework::Hashes::JTR_NTLMV1 : Metasploit::Framework::Hashes::JTR_NTLMV2

  if active_db?
    origin = create_credential_origin_service(
      {
        address: address,
        port: srvport,
        service_name: 'smb',
        protocol: 'tcp',
        module_fullname: fullname,
        workspace_id: myworkspace_id
      }
    )

    credential_options = {
      origin: origin,
      origin_type: :service,
      address: address,
      port: srvport,
      service_name: 'smb',
      username: user,
      server_challenge: challenge,
      client_hash: client_hash,
      # client_os_version: client_os_version,
      private_data: combined_hash,
      private_type: :nonreplayable_hash,
      jtr_format: jtr_format,
      module_fullname: fullname,
      workspace_id: myworkspace_id,
    }
    if domain.present?
      credential_options[:domain] = domain
      credential_options[:realm_key] = Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN
      credential_options[:realm_value] = domain
    end

    # TODO: Re-implement when +client_os_version+ can be determined.
    # found_host = framework.db.hosts.find_by(address: address)
    # found_host.os_name = credential_options[:client_os_version]
    # found_host.save!

    search_options = {
      realm: credential_options[:realm_value],
      user: credential_options[:username],
      hosts: credential_options[:address],
      jtr_format: credential_options[:jtr_format],
      type: Metasploit::Credential::NonreplayableHash,
      workspace: framework.db.workspace
    }
    if framework.db.creds(search_options).count > 0
      vprint_status("Skipping previously captured hash for #{credential_options[:realm_value]}\\#{credential_options[:username]}")
      return
    end

    create_credential(credential_options)
  end

  # TODO: write method for mapping +major+ and +minor+ OS values to human-readable OS names.
  # client_os_version = ::NTLM::OSVersion.read(type1_msg.os_version)
  print_line "[SMB] #{hash_type} Client     : #{address}"
  # print_line "[SMB] #{hash_type} Client OS  : #{client_os_version}"
  print_line "[SMB] #{hash_type} Username   : #{domain}\\#{user}"
  print_line "[SMB] #{hash_type} Hash       : #{combined_hash}"
  print_line

  if datastore['JOHNPWFILE']
    path = build_jtr_file_name(jtr_format)

    File.open(path, 'ab') do |f|
      f.puts(combined_hash)
    end
  end

  # Cain & Abel doesn't support import of NTLMv2 hashes
  if datastore['CAINPWFILE'] && jtr_format == Metasploit::Framework::Hashes::JTR_NTLMV1
    # Cain&Abel hash format
    # Username:Domain:Challenge:LMHash:NTLMHash
    File.open(File.expand_path(datastore['CAINPWFILE'], Msf::Config.install_root), 'ab') do |f|
      f.puts("#{user}:#{domain}:#{server_challenge}:#{client_hash}")
    end
  end
end

#validate_smb_hash_capture_datastore(datastore, ntlm_provider) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/msf/core/exploit/remote/smb/server/hash_capture.rb', line 10

def validate_smb_hash_capture_datastore(datastore, ntlm_provider)
  if datastore['CHALLENGE']
    # Set challenge for all future server responses

    chall = proc { [datastore['CHALLENGE']].pack('H*') }
    ntlm_provider.generate_server_challenge(&chall)
  end

  if datastore['JOHNPWFILE']
    print_status("JTR hashes will be split into two files depending on the hash format.")
    print_status("#{build_jtr_file_name(Metasploit::Framework::Hashes::JTR_NTLMV1)} for NTLMv1 hashes.")
    print_status("#{build_jtr_file_name(Metasploit::Framework::Hashes::JTR_NTLMV2)} for NTLMv2 hashes.")
    print_line
  end

  if datastore['CAINPWFILE']
    print_status("Cain & Abel hashes will be stored at #{File.expand_path(datastore['CAINPWFILE'], Msf::Config.install_root)}")
    print_line
  end
end