Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ldap_connect in place of ldap_new to trigger the bind early #18145

Closed

Conversation

dwelch-r7
Copy link
Contributor

The auxiliary/gather/ldap_hashdump added in #13906 was using ldap_new instead of ldap_connect this lead to the logic checking for a valid connection to always claim a connection was established since ldap.get_operation_result.code will always be 0 (indicating no errors) before connecting

Simple fix to just replace ldap_new with ldap_connect

Before

    89:     ldap_new do |ldap|
    90:       require 'pry-byebug'; binding.pry
 => 91:       if ldap.get_operation_result.code == 0
    92:         vprint_status("#{peer} LDAP connection established")
    93:       else
    94:         # Even if we get "Invalid credentials" error, we may proceed with anonymous bind
    95:         print_ldap_error(ldap)
    96:       end


[1] pry(#<Msf::Modules::Auxiliary__Gather__Ldap_hashdump::MetasploitModule>)> ldap.instance_variable_get :@open_connection
=> nil

After

    89:     ldap_connect do |ldap|
    90:       require 'pry-byebug'; binding.pry
 => 91:       if ldap.get_operation_result.code == 0
    92:         vprint_status("#{peer} LDAP connection established")
    93:       else
    94:         # Even if we get "Invalid credentials" error, we may proceed with anonymous bind
    95:         print_ldap_error(ldap)
    96:       end

[1] pry(#<Msf::Modules::Auxiliary__Gather__Ldap_hashdump::MetasploitModule>)> ldap.instance_variable_get :@open_connection
=> #<Net::LDAP::Connection:0x00007f91f79da6d0 @conn=#<Socket:fd 13>, @message_queue={1=>[]}, @msgid=1>

Verification Steps

@dwelch-r7 dwelch-r7 added the rn-fix release notes fix label Jun 29, 2023
@@ -86,7 +86,7 @@ def run_host(ip)
entries_returned = 0

print_status("#{peer} Connecting...")
ldap_new do |ldap|
Copy link
Contributor

@adfoster-r7 adfoster-r7 Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this now skips the monkey patching logic that was explicitly put in to catch some bind errors:

def ldap_new(opts = {})
ldap = Net::LDAP.new(get_connect_opts.merge(opts))
# NASTY, but required
# monkey patch ldap object in order to ignore bind errors
# Some servers (e.g. OpenLDAP) return result even after a bind
# has failed, e.g. with LDAP_INAPPROPRIATE_AUTH - anonymous bind disallowed.
# See: https://www.openldap.org/doc/admin23/security.html#Authentication%20Methods
# "Note that disabling the anonymous bind mechanism does not prevent anonymous
# access to the directory."
# Bug created for Net:LDAP at https://github.com/ruby-ldap/ruby-net-ldap/issues/375
#
# @yieldparam conn [Net::LDAP] The LDAP connection handle to use for connecting to
# the target LDAP server.
# @param args [Hash] A hash containing options for the ldap connection
def ldap.use_connection(args)
if @open_connection
yield @open_connection
else
begin
conn = new_connection
conn.bind(args[:auth] || @auth)
# Commented out vs. original
# result = conn.bind(args[:auth] || @auth)
# return result unless result.result_code == Net::LDAP::ResultCodeSuccess
yield conn
ensure
conn.close if conn
end
end
end
yield ldap
end

Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I didn't spot that, not intentional no, I'll take another pass at this, would even just reverting this change and removing the if ldap.get_operation_result.code == 0 section be viable?

@gwillcox-r7
Copy link
Contributor

Happy to review this when ready but seems like this may need some revision. Going to await Dean's updates, then feel free to ping me when you'd like a review.

@dwelch-r7 dwelch-r7 marked this pull request as draft July 4, 2023 10:03
@dwelch-r7
Copy link
Contributor Author

Closing this for now, this doesn't seem like the right solution

@dwelch-r7 dwelch-r7 closed this Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rn-fix release notes fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants