Skip to content

Commit 2e51b37

Browse files
authored
Land rapid7#19267, Escape LDAP query string filters
2 parents bccad77 + a6fd6de commit 2e51b37

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lib/msf/core/exploit/remote/ldap.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,14 @@ def validate_query_result!(query_result, filter=nil)
309309
end
310310
end
311311
end
312+
313+
# Return a string suitable for placement in an LDAP filter
314+
# e.g. (certificateTemplates=#{ldap_escape_string(name)})
315+
#
316+
# @param string String The string to escape.
317+
# @return The escaped string.
318+
def ldap_escape_filter(string)
319+
Net::LDAP::Filter.escape(string)
320+
end
312321
end
313322
end

modules/auxiliary/gather/ldap_esc_vulnerable_cert_finder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def query_ldap_server_certificates(esc_raw_filter, esc_name)
178178
def convert_sids_to_human_readable_name(sids_array)
179179
output = []
180180
for sid in sids_array
181-
raw_filter = "(objectSID=#{sid})"
181+
raw_filter = "(objectSID=#{ldap_escape_filter(sid.to_s)})"
182182
attributes = ['sAMAccountName', 'name']
183183
base_prefix = 'CN=Configuration'
184184
sid_entry = query_ldap_server(raw_filter, attributes, base_prefix: base_prefix) # First try with prefix to find entries that may be group specific.
@@ -344,7 +344,7 @@ def find_enrollable_vuln_certificate_templates
344344
# have permissions to enroll in certificates on each server.
345345

346346
@vuln_certificate_details.each_key do |certificate_template|
347-
certificate_enrollment_raw_filter = "(&(objectClass=pKIEnrollmentService)(certificateTemplates=#{certificate_template}))"
347+
certificate_enrollment_raw_filter = "(&(objectClass=pKIEnrollmentService)(certificateTemplates=#{ldap_escape_filter(certificate_template.to_s)}))"
348348
attributes = ['cn', 'dnsHostname', 'ntsecuritydescriptor']
349349
base_prefix = 'CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration'
350350
enrollment_ca_data = query_ldap_server(certificate_enrollment_raw_filter, attributes, base_prefix: base_prefix)
@@ -418,7 +418,7 @@ def get_pki_object_by_oid(oid)
418418

419419
if pki_object.nil?
420420
pki_object = query_ldap_server(
421-
"(&(objectClass=msPKI-Enterprise-Oid)(msPKI-Cert-Template-OID=#{oid}))",
421+
"(&(objectClass=msPKI-Enterprise-Oid)(msPKI-Cert-Template-OID=#{ldap_escape_filter(oid.to_s)}))",
422422
nil,
423423
base_prefix: 'CN=OID,CN=Public Key Services,CN=Services,CN=Configuration'
424424
)&.first

spec/lib/msf/core/exploit/remote/ldap_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@
9090
end
9191
end
9292

93+
describe '#ldap_escape_filter' do
94+
let(:string) do
95+
'John Doe (Developer) *'
96+
end
97+
98+
it do
99+
expect(subject.ldap_escape_filter(string)).to eq("John Doe \\28Developer\\29 \\2A")
100+
end
101+
end
102+
93103
describe '#resolve_connect_opts' do
94104
let(:cred) do
95105
'I am a cred'

0 commit comments

Comments
 (0)