@@ -12,6 +12,7 @@ module Exploit::Remote::LDAP
12
12
include Msf ::Exploit ::Remote ::Kerberos ::Ticket ::Storage
13
13
include Msf ::Exploit ::Remote ::Kerberos ::ServiceAuthenticator ::Options
14
14
include Metasploit ::Framework ::LDAP ::Client
15
+ include Msf ::OptionalSession ::LDAP
15
16
16
17
# Initialize the LDAP client and set up the LDAP specific datastore
17
18
# options to allow the client to perform authentication and timeout
@@ -27,8 +28,6 @@ def initialize(info = {})
27
28
super
28
29
29
30
register_options ( [
30
- Opt ::RHOST ,
31
- Opt ::RPORT ( 389 ) ,
32
31
OptBool . new ( 'SSL' , [ false , 'Enable SSL on the LDAP connection' , false ] ) ,
33
32
Msf ::OptString . new ( 'DOMAIN' , [ false , 'The domain to authenticate to' ] ) ,
34
33
Msf ::OptString . new ( 'USERNAME' , [ false , 'The username to authenticate with' ] , aliases : [ 'BIND_DN' ] ) ,
@@ -121,6 +120,7 @@ def get_connect_opts
121
120
# @return [Object] The result of whatever the block that was
122
121
# passed in via the "block" parameter yielded.
123
122
def ldap_connect ( opts = { } , &block )
123
+ return yield session . client if session
124
124
ldap_open ( get_connect_opts . merge ( opts ) , &block )
125
125
end
126
126
@@ -136,6 +136,7 @@ def ldap_connect(opts = {}, &block)
136
136
# @return [Object] The result of whatever the block that was
137
137
# passed in via the "block" parameter yielded.
138
138
def ldap_open ( connect_opts , &block )
139
+ return yield session . client if session
139
140
opts = resolve_connect_opts ( connect_opts )
140
141
Rex ::Proto ::LDAP ::Client . open ( opts , &block )
141
142
end
@@ -160,6 +161,7 @@ def resolve_connect_opts(connect_opts)
160
161
# @yieldparam ldap [Rex::Proto::LDAP::Client] The LDAP connection handle to use for connecting to
161
162
# the target LDAP server.
162
163
def ldap_new ( opts = { } )
164
+ return yield session . client if session
163
165
164
166
ldap = Rex ::Proto ::LDAP ::Client . new ( resolve_connect_opts ( get_connect_opts . merge ( opts ) ) )
165
167
@@ -194,58 +196,6 @@ def ldap.use_connection(args)
194
196
yield ldap
195
197
end
196
198
197
- # # Get the naming contexts for the target LDAP server.
198
- # #
199
- # # @param ldap [Rex::Proto::LDAP::Client] The Rex::Proto::LDAP::Client connection handle for the
200
- # # current LDAP connection.
201
- # # @return [Net::BER::BerIdentifiedArray] Array of naming contexts for the target LDAP server.
202
- # def get_naming_contexts(ldap)
203
- # vprint_status("#{peer} Getting root DSE")
204
- #
205
- # unless (root_dse = ldap.search_root_dse)
206
- # print_error("#{peer} Could not retrieve root DSE")
207
- # return
208
- # end
209
- #
210
- # naming_contexts = root_dse[:namingcontexts]
211
- #
212
- # # NOTE: Rex::Proto::LDAP::Client converts attribute names to lowercase
213
- # if naming_contexts.empty?
214
- # print_error("#{peer} Empty namingContexts attribute")
215
- # return
216
- # end
217
- #
218
- # naming_contexts
219
- # end
220
-
221
- # Discover the base DN of the target LDAP server via the LDAP
222
- # server's naming contexts.
223
- #
224
- # @param ldap [Rex::Proto::LDAP::Client] The Rex::Proto::LDAP::Client connection handle for the
225
- # current LDAP connection.
226
- # @return [String] A string containing the base DN of the target LDAP server.
227
- # def discover_base_dn(ldap)
228
- # # @type [Net::BER::BerIdentifiedArray]
229
- # naming_contexts = get_naming_contexts(ldap)
230
- #
231
- # unless naming_contexts
232
- # print_error("#{peer} Base DN cannot be determined")
233
- # return
234
- # end
235
- #
236
- # # NOTE: Find the first entry that starts with `DC=` as this will likely be the base DN.
237
- # naming_contexts.select! { |context| context =~ /^([Dd][Cc]=[A-Za-z0-9-]+,?)+$/ }
238
- # naming_contexts.reject! { |context| context =~ /(Configuration)|(Schema)|(ForestDnsZones)/ }
239
- # if naming_contexts.blank?
240
- # print_error("#{peer} A base DN matching the expected format could not be found!")
241
- # return
242
- # end
243
- # base_dn = naming_contexts[0]
244
- #
245
- # print_good("#{peer} Discovered base DN: #{base_dn}")
246
- # base_dn
247
- # end
248
-
249
199
# Check whether it was possible to successfully bind to the target LDAP
250
200
# server. Raise a RuntimeException with an appropriate error message
251
201
# if not.
0 commit comments