CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/credential.rb
Views: 11777
1
require 'active_model'
2
3
module Metasploit
4
module Framework
5
# This class provides an in-memory representation of a conceptual Credential
6
#
7
# It contains the public, private, and realm if any.
8
class Credential
9
include ActiveModel::Validations
10
11
# @!attribute paired
12
# @return [Boolean] Whether BOTH a public and private are required
13
# (defaults to `true`)
14
attr_accessor :paired
15
# @!attribute parent
16
# @return [Object] the parent object that had .to_credential called on it to create this object
17
attr_accessor :parent
18
# @!attribute private
19
# The private credential component (e.g. password)
20
#
21
# @return [String] if {#paired} is `true` or {#private} is `nil`
22
# @return [String, nil] if {#paired} is `false` or {#private} is not `nil`.
23
attr_accessor :private
24
# @!attribute private_type
25
# The type of private credential this object represents, e.g. a
26
# password or an NTLM hash.
27
#
28
# @return [String]
29
attr_accessor :private_type
30
# @!attribute public
31
# The public credential component (e.g. username)
32
#
33
# @return [String] if {#paired} is `true` or {#public} is `nil`
34
# @return [String, nil] if {#paired} is `false` or {#public} is not `nil`.
35
attr_accessor :public
36
# @!attribute realm
37
# @return [String,nil] The realm credential component (e.g domain name)
38
attr_accessor :realm
39
# @!attribute realm_key
40
# @return [String,nil] The type of {#realm}
41
attr_accessor :realm_key
42
43
validates :paired,
44
inclusion: { in: [true, false] }
45
46
# If we have no public we MUST have a private (e.g. SNMP Community String)
47
validates :private,
48
exclusion: { in: [nil] },
49
if: -> { public.nil? or paired }
50
51
# These values should be #demodularized from subclasses of
52
# `Metasploit::Credential::Private`
53
validates :private_type,
54
inclusion: { in: [ :password, :ntlm_hash, :postgres_md5, :ssh_key ] },
55
if: -> { private_type.present? }
56
57
# If we have no private we MUST have a public
58
validates :public,
59
presence: true,
60
if: -> { private.nil? or paired }
61
62
# @param attributes [Hash{Symbol => String,nil}]
63
def initialize(attributes={})
64
attributes.each do |attribute, value|
65
public_send("#{attribute}=", value)
66
end
67
68
self.paired = true if self.paired.nil?
69
end
70
71
def inspect
72
"#<#{self.class} \"#{self}\" >"
73
end
74
75
def to_s
76
if realm && realm_key == Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN
77
"#{self.realm}\\#{self.public}:#{self.private}"
78
elsif self.private
79
"#{self.public}:#{self.private}#{at_realm}"
80
else
81
self.public
82
end
83
end
84
85
def ==(other)
86
other.respond_to?(:public) && other.public == self.public &&
87
other.respond_to?(:private) && other.private == self.private &&
88
other.respond_to?(:realm) && other.realm == self.realm
89
end
90
91
def to_credential
92
self.parent = self
93
self
94
end
95
96
# This method takes all of the attributes of the {Credential} and spits
97
# them out in a hash compatible with the create_credential calls.
98
#
99
# @return [Hash] a hash compatible with #create_credential
100
def to_h
101
{
102
private_data: private,
103
private_type: private_type,
104
username: public,
105
realm_key: realm_key,
106
realm_value: realm
107
}
108
end
109
110
private
111
112
def at_realm
113
if self.realm.present?
114
"@#{self.realm}"
115
else
116
""
117
end
118
end
119
end
120
end
121
end
122
123