@@ -49,17 +49,57 @@ def setup
49
49
@@max_per_service = nil
50
50
end
51
51
52
+ # Yields each {Metasploit::Credential::Core} in the {Mdm::Workspace} with
53
+ # a private type of 'ntlm_hash'
54
+ #
55
+ # @yieldparam [Metasploit::Credential::Core]
56
+ def each_ntlm_cred
57
+ creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::NTLMHash' } , workspace_id : myworkspace . id )
58
+ creds . each do |cred |
59
+ yield cred
60
+ end
61
+ end
62
+
63
+ # Yields each {Metasploit::Credential::Core} in the {Mdm::Workspace} with
64
+ # a private type of 'password'
65
+ #
66
+ # @yieldparam [Metasploit::Credential::Core]
67
+ def each_password_cred
68
+ creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::Password' } , workspace_id : myworkspace . id )
69
+ creds . each do |cred |
70
+ yield cred
71
+ end
72
+ end
73
+
74
+ # Yields each {Metasploit::Credential::Core} in the {Mdm::Workspace} with
75
+ # a private type of 'ssh_key'
76
+ #
77
+ # @yieldparam [Metasploit::Credential::Core]
78
+ def each_ssh_cred
79
+ creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::SSHKey' } , workspace_id : myworkspace . id )
80
+ creds . each do |cred |
81
+ yield cred
82
+ end
83
+ end
84
+
85
+ # Checks whether we should be adding creds from the DB to a CredCollection
86
+ #
87
+ # @return [TrueClass] if any of the datastore options for db creds are selected and the db is active
88
+ # @return [FalseClass] if none of the datastore options are selected OR the db is not active
89
+ def prepend_db_creds?
90
+ ( datastore [ 'DB_ALL_CREDS' ] || datastore [ 'DB_ALL_PASS' ] || datastore [ 'DB_ALL_USERS' ] ) && framework . db . active
91
+ end
92
+
52
93
# This method takes a {Metasploit::Framework::CredentialCollection} and prepends existing NTLMHashes
53
94
# from the database. This allows the users to use the DB_ALL_CREDS option.
54
95
#
55
96
# @param cred_collection [Metasploit::Framework::CredentialCollection]
56
97
# the credential collection to add to
57
98
# @return [Metasploit::Framework::CredentialCollection] the modified Credentialcollection
58
99
def prepend_db_hashes ( cred_collection )
59
- if datastore [ 'DB_ALL_CREDS' ] && framework . db . active
60
- creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::NTLMHash' } , workspace_id : myworkspace . id )
61
- creds . each do |cred |
62
- cred_collection . prepend_cred ( cred . to_credential )
100
+ if prepend_db_creds?
101
+ each_ntlm_cred do |cred |
102
+ process_cred_for_collection ( cred_collection , cred )
63
103
end
64
104
end
65
105
cred_collection
@@ -72,10 +112,9 @@ def prepend_db_hashes(cred_collection)
72
112
# the credential collection to add to
73
113
# @return [Metasploit::Framework::CredentialCollection] the modified Credentialcollection
74
114
def prepend_db_keys ( cred_collection )
75
- if datastore [ 'DB_ALL_CREDS' ] && framework . db . active
76
- creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::SSHKey' } , workspace_id : myworkspace . id )
77
- creds . each do |cred |
78
- cred_collection . prepend_cred ( cred . to_credential )
115
+ if prepend_db_creds?
116
+ each_ssh_cred do |cred |
117
+ process_cred_for_collection ( cred_collection , cred )
79
118
end
80
119
end
81
120
cred_collection
@@ -88,15 +127,27 @@ def prepend_db_keys(cred_collection)
88
127
# the credential collection to add to
89
128
# @return [Metasploit::Framework::CredentialCollection] the modified Credentialcollection
90
129
def prepend_db_passwords ( cred_collection )
91
- if datastore [ 'DB_ALL_CREDS' ] && framework . db . active
92
- creds = Metasploit ::Credential ::Core . joins ( :private ) . where ( metasploit_credential_privates : { type : 'Metasploit::Credential::Password' } , workspace_id : myworkspace . id )
93
- creds . each do |cred |
94
- cred_collection . prepend_cred ( cred . to_credential )
130
+ if prepend_db_creds?
131
+ each_password_cred do |cred |
132
+ process_cred_for_collection ( cred_collection , cred )
95
133
end
96
134
end
97
135
cred_collection
98
136
end
99
137
138
+ # Takes a {Metasploit::Credential::Core} and converts it into a
139
+ # {Metasploit::Framework::Credential} and processes it into the
140
+ # {Metasploit::Framework::CredentialCollection} as dictated by the
141
+ # selected datastore options.
142
+ #
143
+ # @param [Metasploit::Framework::CredentialCollection] the credential collection to add to
144
+ # @param [Metasploit::Credential::Core] the Credential Core to process
145
+ def process_cred_for_collection ( cred_collection , cred )
146
+ msf_cred = cred . to_credential
147
+ cred_collection . prepend_cred ( msf_cred ) if datastore [ 'DB_ALL_CREDS' ]
148
+ cred_collection . add_private ( msf_cred . private ) if datastore [ 'DB_ALL_PASS' ]
149
+ cred_collection . add_public ( msf_cred . public ) if datastore [ 'DB_ALL_USERS' ]
150
+ end
100
151
101
152
102
153
# Checks all three files for usernames and passwords, and combines them into
0 commit comments