Module: Msf::DBManager::Cred

Included in:
Msf::DBManager
Defined in:
lib/msf/core/db_manager/cred.rb

Constant Summary collapse

PERMITTED_CORE_UPDATE_ATTRIBUTES =

List of allow-listed option keys, when performing operations on credentials. Only these can be modified by the caller.

%i[workspace public private origin created_at updated_at].freeze

Instance Method Summary collapse

Instance Method Details

#create_credential_core(opts = {}) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/msf/core/db_manager/cred.rb', line 327

def create_credential_core(opts = {})
  core = super
  return core if core.nil?
  origin = opts[:origin]
  if origin.is_a?(Metasploit::Credential::Origin::Service)
    retry_transaction do
       = Metasploit::Credential::Login.where(
        core_id: core.id,
        service_id: origin.service_id
      ).first_or_initialize
      .status = Metasploit::Model::Login::Status::UNTRIED if .status.blank?
      .save!
    end
  end
  core
end

#create_credential_service(opts = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/msf/core/db_manager/cred.rb', line 316

def create_credential_service(opts = {})
  opts = opts.merge(service_name: opts[:service_name].to_s.downcase) if opts[:service_name]
  service = super(opts)
  return service if service.nil?
  if opts.key?(:info)
    service.info = opts[:info].nil? ? '' : opts[:info]
  end
  service.save! if service.changed?
  service
end

#creds(opts) ⇒ Object

This methods returns a list of all credentials in the database



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/msf/core/db_manager/cred.rb', line 15

def creds(opts)
  query = nil
  ::ApplicationRecord.connection_pool.with_connection {
    # If :id exists we're looking for a specific record, skip the other stuff
    if opts[:id] && !opts[:id].to_s.empty?
      return Array.wrap(Metasploit::Credential::Core.find(opts[:id]))
    end

    wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)
    search_term = opts[:search_term]

    query = Metasploit::Credential::Core.where( workspace_id: wspace.id )
    query = query.includes(:private, :public, :logins, :realm).references(:private, :public, :logins, :realm)
    query = query.includes(logins: [ :service, { service: :host } ])

    if opts[:type].present?
      query = query.where('"metasploit_credential_privates"."type" = ?', opts[:type].to_s)
    end

    if opts[:jtr_format].present?
      query = query.where('"metasploit_credential_privates"."jtr_format" = ?', opts[:jtr_format])
    end

    if opts[:svcs].present?
      query = query.where(Mdm::Service[:name].in(opts[:svcs]))
    end

    if opts[:ports].present?
      query = query.where(Mdm::Service[:port].in(opts[:ports]))
    end

    if opts.key?(:realm)
      if opts[:realm].nil?
        query = query.where( realm: nil )
      else
        query = query.where('"metasploit_credential_realms"."value" = ?', opts[:realm])
      end
    end

    if opts[:user].present?
      query = query.where('"metasploit_credential_publics"."username" = ?', opts[:user])
    end

    if opts[:pass].present?
      query = query.where('"metasploit_credential_privates"."data" = ?', opts[:pass])
    end

    if opts[:host_ranges] || opts[:ports] || opts[:svcs]
      # Only find Cores that have non-zero Logins if the user specified a
      # filter based on host, port, or service name
      query = query.where(Metasploit::Credential::Login[:id].not_eq(nil))
    end

    if search_term && !search_term.empty?
      core_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Metasploit::Credential::Core, search_term, ['created_at', 'updated_at'])
      public_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Metasploit::Credential::Public, search_term, ['created_at', 'updated_at'])
      private_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Metasploit::Credential::Private, search_term, ['created_at', 'updated_at'])
      realm_search_conditions = Msf::Util::DBManager.create_all_column_search_conditions(Metasploit::Credential::Realm, search_term, ['created_at', 'updated_at'])
      column_search_conditions = core_search_conditions.or(public_search_conditions).or(private_search_conditions).or(realm_search_conditions)
      query = query.where(column_search_conditions)
    end
  }
  query
end

#delete_credentials(opts) ⇒ Object

Raises:

  • (ArgumentError)


297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/msf/core/db_manager/cred.rb', line 297

def delete_credentials(opts)
  raise ArgumentError.new("The following options are required: :ids") if opts[:ids].nil?

  ::ApplicationRecord.connection_pool.with_connection {
    deleted = []
    opts[:ids].each do |cred_id|
      cred = Metasploit::Credential::Core.find(cred_id)
      begin
        deleted << cred.destroy
      rescue # refs suck
        elog("Forcibly deleting #{cred}")
        deleted << cred.delete
      end
    end

    return deleted
  }
end

#each_cred(wspace = framework.db.workspace, &block) ⇒ Object

This method iterates the creds table calling the supplied block with the cred instance of each entry.



82
83
84
85
86
87
88
# File 'lib/msf/core/db_manager/cred.rb', line 82

def each_cred(wspace=framework.db.workspace,&block)
::ApplicationRecord.connection_pool.with_connection {
  wspace.creds.each do |cred|
    block.call(cred)
  end
}
end

#find_or_create_cred(opts) ⇒ Object

Find or create a credential matching this type/data



91
92
93
# File 'lib/msf/core/db_manager/cred.rb', line 91

def find_or_create_cred(opts)
  report_auth_info(opts)
end

#report_auth_info(opts = {}) ⇒ Object Also known as: report_auth, report_cred

Store a set of credentials in the database.

report_auth_info used to create a note, now it creates an entry in the creds table. It's much more akin to report_vuln() now.

opts MUST contain :host:: an IP address or Host object reference :port:: a port number

opts can contain :user:: the username :pass:: the password, or path to ssh_key :ptype:: the type of password (password(ish), hash, or ssh_key) :proto:: a transport name for the port :sname:: service name :active:: by default, a cred is active, unless explicitly false :proof:: data used to prove the account is actually active.

Sources: Credentials can be sourced from another credential, or from a vulnerability. For example, if an exploit was used to dump the smb_hashes, and this credential comes from there, the source_id would be the Vuln id (as reported by report_vuln) and the type would be "Vuln".

:source_id:: The Vuln or Cred id of the source of this cred. :source_type:: Either Vuln or Cred

TODO: This is written somewhat host-centric, when really the Service is the thing. Need to revisit someday.

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/msf/core/db_manager/cred.rb', line 125

def report_auth_info(opts={})
  return if not active
  raise ArgumentError.new("Missing required option :host") if opts[:host].nil?
  raise ArgumentError.new("Missing required option :port") if (opts[:port].nil? and opts[:service].nil?)

  if (not opts[:host].kind_of?(::Mdm::Host)) and (not validate_ips(opts[:host]))
    raise ArgumentError.new("Invalid address or object for :host (#{opts[:host].inspect})")
  end

::ApplicationRecord.connection_pool.with_connection {
  host = opts[:host]
  ptype = opts[:type] || "password"
  token = [opts[:user], opts[:pass]]
  sname = opts[:sname]
  port = opts[:port]
  proto = opts[:proto] || "tcp"
  proof = opts[:proof]
  source_id = opts[:source_id]
  source_type = opts[:source_type]
  duplicate_ok = opts[:duplicate_ok]
  # Nil is true for active.
  active = (opts[:active] || opts[:active].nil?) ? true : false

  wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework)

  # Service management; assume the user knows what
  # he's talking about.
  service = opts[:service] || report_service(:host => host, :port => port, :proto => proto, :name => sname, :workspace => wspace)

  # Non-US-ASCII usernames are tripping up the database at the moment, this is a temporary fix until we update the tables
  if (token[0])
    # convert the token to US-ASCII from UTF-8 to prevent an error
    token[0] = token[0].unpack("C*").pack("C*")
    token[0] = token[0].gsub(/[\x00-\x1f\x7f-\xff]/n){|m| "\\x%.2x" % m.unpack("C")[0] }
  end

  if (token[1])
    token[1] = token[1].unpack("C*").pack("C*")
    token[1] = token[1].gsub(/[\x00-\x1f\x7f-\xff]/n){|m| "\\x%.2x" % m.unpack("C")[0] }
  end

  ret = {}

  # Check to see if the creds already exist. We look also for a downcased username with the
  # same password because we can fairly safely assume they are not in fact two separate creds.
  # this allows us to hedge against duplication of creds in the DB.

  if duplicate_ok
  # If duplicate usernames are okay, find by both user and password (allows
  # for actual duplicates to get modified updated_at, sources, etc)
    if token[0].nil? or token[0].empty?
      cred = service.creds.where(user: token[0] || "", ptype: ptype, pass: token[1] || "").first_or_initialize
    else
      cred = service.creds.find_by_user_and_ptype_and_pass(token[0] || "", ptype, token[1] || "")
      unless cred
        dcu = token[0].downcase
        cred = service.creds.find_by_user_and_ptype_and_pass( dcu || "", ptype, token[1] || "")
        unless cred
          cred = service.creds.where(user: token[0] || "", ptype: ptype, pass: token[1] || "").first_or_initialize
        end
      end
    end
  else
    # Create the cred by username only (so we can change passwords)
    if token[0].nil? or token[0].empty?
      cred = service.creds.where(user: token[0] || "", ptype: ptype).first_or_initialize
    else
      cred = service.creds.find_by_user_and_ptype(token[0] || "", ptype)
      unless cred
        dcu = token[0].downcase
        cred = service.creds.find_by_user_and_ptype_and_pass( dcu || "", ptype, token[1] || "")
        unless cred
          cred = service.creds.where(user: token[0] || "", ptype: ptype).first_or_initialize
        end
      end
    end
  end

  # Update with the password
  cred.pass = (token[1] || "")

  # Annotate the credential
  cred.ptype = ptype
  cred.active = active

  # Update the source ID only if there wasn't already one.
  if source_id and !cred.source_id
    cred.source_id = source_id
    cred.source_type = source_type if source_type
  end

  # Safe proof (lazy way) -- doesn't chop expanded
  # characters correctly, but shouldn't ever be a problem.
  unless proof.nil?
    proof = Rex::Text.to_hex_ascii(proof)
    proof = proof[0,4096]
  end
  cred.proof = proof

  # Update the timestamp
  if cred.changed?
    msf_assign_timestamps(opts, cred)
    cred.save!
  end

  # Ensure the updated_at is touched any time report_auth_info is called
  # except when it's set explicitly (as it is for imports)
  unless opts[:updated_at] || opts["updated_at"]
    cred.updated_at = Time.now.utc
    cred.save!
  end


  if opts[:task]
    Mdm::TaskCred.create(
        :task => opts[:task],
        :cred => cred
    )
  end

  ret[:cred] = cred
}
end

#update_credential(opts) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/msf/core/db_manager/cred.rb', line 249

def update_credential(opts)
  ::ApplicationRecord.connection_pool.with_connection {
    # process workspace string for update if included in opts
    wspace = Msf::Util::DBManager.process_opts_workspace(opts, framework, false)
    opts = opts.clone()
    opts.delete(:workspace)

    id = opts.delete(:id)
    cred = Metasploit::Credential::Core.find(id)

    update_attrs = permitted_core_update_attributes(opts)
    update_attrs[:workspace] = wspace if wspace

    if opts[:public]
      # The :id should only apply to the cred above, not any of the further lookups we do here so we exclude it
      if opts[:public][:id] && opts[:public][:id].to_s == cred.public_id.to_s
        public = Metasploit::Credential::Public.find(opts[:public][:id])
        public.update(opts[:public].except(:id))
      else
        public = Metasploit::Credential::Public.where(opts[:public].except(:id)).first_or_initialize
      end
      update_attrs[:public] = public
    end
    if opts[:private]
      if opts[:private][:id] && opts[:private][:id].to_s == cred.private_id.to_s
        private = Metasploit::Credential::Private.find(opts[:private][:id])
        private.update(opts[:private].except(:id))
      else
        private = Metasploit::Credential::Private.where(opts[:private].except(:id)).first_or_initialize
      end
      update_attrs[:private] = private
    end
    if opts[:origin]
      # See comment above for :public -- same reasoning applies to :origin.
      if opts[:origin][:id] && opts[:origin][:id].to_s == cred.origin_id.to_s
        origin = Metasploit::Credential::Origin.find(opts[:origin][:id])
        origin.update(opts[:origin].except(:id))
      else
        origin = Metasploit::Credential::Origin.where(opts[:origin].except(:id)).first_or_initialize
      end
      update_attrs[:origin] = origin
    end

    cred.update!(update_attrs)
    return cred
  }
end