Skip to content

Commit 6c2aaab

Browse files
committed
Improved registry querying
1 parent 7c0c746 commit 6c2aaab

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

modules/auxiliary/gather/ldap_esc_vulnerable_cert_finder.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,29 @@ def user_can_write?(authenticated_user_info, security_descriptor)
437437
(write_sids.map(&:value) & ([user_sid] + group_sids)).any?
438438
end
439439

440+
def parse_registry_output(output, property_name)
441+
return nil if output.stderr.present?
442+
443+
stdout = output.stdout if output.stdout.present?
444+
return nil unless stdout
445+
446+
line_with_property = stdout.lines.find { |line| line.strip.match(/^#{Regexp.escape(property_name)}\s*:/) }
447+
return nil unless line_with_property
448+
449+
line_with_property.split(':', 2).last&.strip
450+
end
451+
452+
def run_registry_command(shell, path, property_name, dynamic_value = nil)
453+
full_path = dynamic_value ? "#{path}\\#{dynamic_value}" : path
454+
command = "Get-ItemProperty -Path '#{full_path}' -Name #{property_name}"
455+
output = shell.run(command)
456+
value = parse_registry_output(output, property_name)
457+
if value.nil?
458+
print_error("Registry property '#{property_name}' not found at path '#{full_path}'.")
459+
end
460+
value
461+
end
462+
440463
def enum_registry_values
441464
endpoint = "http://#{datastore['RHOST']}:5985/wsman"
442465
user = datastore['LDAPUsername']
@@ -457,13 +480,8 @@ def enum_registry_values
457480

458481
begin
459482
conn.shell(:powershell) do |shell|
460-
cert_mapping_command = "Get-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\Schannel' -Name CertificateMappingMethods"
461-
cert_mapping_output = shell.run(cert_mapping_command)
462-
registry_values[:certificate_mapping_methods] = parse_registry_output(cert_mapping_output.output)
463-
464-
strong_cert_command = "Get-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Kdc' -Name StrongCertificateBindingEnforcement"
465-
strong_cert_output = shell.run(strong_cert_command)
466-
registry_values[:strong_certificate_binding_enforcement] = parse_registry_output(strong_cert_output.output)
483+
registry_values[:certificate_mapping_methods] = run_registry_command(shell, 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\Schannel', 'CertificateMappingMethods')
484+
registry_values[:strong_certificate_binding_enforcement] = run_registry_command(shell, 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Kdc', 'StrongCertificateBindingEnforcement')
467485
end
468486

469487
if registry_values[:strong_certificate_binding_enforcement] == '1'
@@ -491,11 +509,6 @@ def enum_registry_values
491509
registry_values
492510
end
493511

494-
def parse_registry_output(output)
495-
# Extract the value from the PowerShell output
496-
output.lines.find { |line| line.strip.match(/:/) }&.split(':', 2)&.last&.strip
497-
end
498-
499512
def find_esc9_vuln_cert_templates
500513
esc9_raw_filter = '(&'\
501514
'(objectclass=pkicertificatetemplate)'\

0 commit comments

Comments
 (0)