Skip to content

Commit 1330034

Browse files
committed
Add support for LDAP target URIs
1 parent 0f4c73b commit 1330034

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

lib/msf/core/rhosts_walker.rb

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class RhostsWalker
1515
file
1616
http
1717
https
18+
ldap
19+
ldaps
1820
mysql
1921
postgres
2022
smb
@@ -251,6 +253,52 @@ def parse_http_uri(value, datastore)
251253
end
252254
alias parse_https_uri parse_http_uri
253255

256+
# Parses a uri string such as ldap://user:[email protected] into a hash which can safely be
257+
# merged with a [Msf::DataStore] datastore for setting ldap options.
258+
#
259+
# @see https://datatracker.ietf.org/doc/html/rfc4516
260+
#
261+
# @param value [String] the ldap string
262+
# @return [Hash] A hash where keys match the required datastore options associated with
263+
# the uri value
264+
def parse_ldap_uri(value, datastore)
265+
uri = ::Addressable::URI.parse(value)
266+
result = {}
267+
268+
result['RHOSTS'] = uri.hostname
269+
is_ssl = %w[ssl ldaps].include?(uri.scheme)
270+
result['RPORT'] = uri.port || (is_ssl ? 636 : 389)
271+
result['SSL'] = is_ssl
272+
273+
if uri.path.present?
274+
base_dn = uri.path.delete_prefix('/').split('?', 2).first
275+
result['BASE_DN'] = base_dn if base_dn.present?
276+
end
277+
278+
set_hostname(datastore, result, uri.hostname)
279+
280+
if uri.user && uri.user.include?(';')
281+
domain, user = uri.user.split(';')
282+
result['LDAPDomain'] = domain
283+
set_username(datastore, result, user)
284+
elsif uri.user
285+
result['LDAPDomain'] = ''
286+
set_username(datastore, result, uri.user)
287+
else
288+
result['LDAPDomain'] = ''
289+
result['LDAPUsername'] = ''
290+
end
291+
292+
if uri.password
293+
set_password(datastore, result, uri.password)
294+
else
295+
result['LDAPPasssword'] = ''
296+
end
297+
298+
result
299+
end
300+
alias parse_ldaps_uri parse_ldap_uri
301+
254302
# Parses a uri string such as mysql://user:[email protected] into a hash
255303
# which can safely be merged with a [Msf::DataStore] datastore for setting mysql options.
256304
#
@@ -353,7 +401,7 @@ def set_hostname(datastore, result, hostname)
353401
def set_username(datastore, result, username)
354402
# Preference setting application specific values first
355403
username_set = false
356-
option_names = %w[SMBUser FtpUser Username user USER USERNAME username]
404+
option_names = %w[SMBUser FtpUser LDAPUsername Username user USER USERNAME username]
357405
option_names.each do |option_name|
358406
if datastore.options.include?(option_name)
359407
result[option_name] = username
@@ -372,7 +420,7 @@ def set_username(datastore, result, username)
372420
def set_password(datastore, result, password)
373421
# Preference setting application specific values first
374422
password_set = false
375-
password_option_names = %w[SMBPass FtpPass Password pass PASSWORD password]
423+
password_option_names = %w[SMBPass FtpPass LDAPPassword Password pass PASSWORD password]
376424
password_option_names.each do |option_name|
377425
if datastore.options.include?(option_name)
378426
result[option_name] = password

0 commit comments

Comments
 (0)