lib/msf/core/post/windows/ldap.rb
# Performs a query on the LDAP session
#
# @param [Handle] LDAP Session Handle
# @param [Integer] Pointer to string that contains distinguished name of entry to start the search
# @param [Integer] Search Scope
# @param [String] Search Filter
# @param [Array] Attributes to retrieve
# @return [Hash] Entries found
Performs a query on the LDAP session
Performs a query on the LDAP session
def query_ldap(session_handle, base, scope, filter, fields)
    vprint_status ("Searching LDAP directory")
    search = wldap32.ldap_search_sA(session_handle, base, scope, filter, nil, 0, 4)
    vprint_status("search: #{search}")
    if search['return'] == LDAP_SIZELIMIT_EXCEEDED
      print_error("LDAP_SIZELIMIT_EXCEEDED, parsing what we retrieved, try increasing the MAX_SEARCH value [0:LDAP_NO_LIMIT]")
    elsif search['return'] != Error::SUCCESS
      print_error("No results")
      wldap32.ldap_msgfree(search['res'])
      return
    end
    search_count = wldap32.ldap_count_entries(session_handle, search['res'])['return']
    if(search_count == 0)
      print_error("No entries retrieved")
      wldap32.ldap_msgfree(search['res'])
      return
    end
    print_status("Entries retrieved: #{search_count}")
    pEntries = []
    entry_results = []
    if datastore['MAX_SEARCH'] == 0
      max_search = search_count
    else
      max_search = [datastore['MAX_SEARCH'], search_count].min
    end
    0.upto(max_search - 1) do |i|
      if(i==0)
        pEntries[0] = wldap32.ldap_first_entry(session_handle, search['res'])['return']
      end
      if(pEntries[i] == 0)
        print_error("Failed to get entry")
        wldap32.ldap_msgfree(search['res'])
        return
      end
      vprint_status("Entry #{i}: 0x#{pEntries[i].to_s(16)}")
      entry = get_entry(pEntries[i])
      if client.platform =~ /x64/
        pEntries[i+1] = entry[4]
      else
        pEntries[i+1] = entry[3]
      end
      ber = get_ber(entry)
      field_results = []
      fields.each do |field|
        vprint_status("Field: #{field}")
        value_results = ""
        values = get_values_from_ber(ber, field)
        values_result = ""
        values_result = values.join(',') if values
        vprint_status("Values #{values}")
        field_results << values_result
      end
      entry_results << field_results
    end
    return {
        :fields  => fields,
        :results => entry_results
    }
  end
def query_ldap ( session_handle , base , scope , filter , fields ) vprint_status ( "Searching LDAP directory" ) search = wldap32 . ldap_search_sA ( session_handle , base , scope , filter , nil , 0 , 4 ) vprint_status ( "search: #{search}" ) if search [ 'return' ] == LDAP_SIZELIMIT_EXCEEDED print_error ( "LDAP_SIZELIMIT_EXCEEDED, parsing what we retrieved, try increasing the MAX_SEARCH value [0:LDAP_NO_LIMIT]" ) elsif search [ 'return' ] != Error :: SUCCESS print_error ( "No results" ) wldap32 . ldap_msgfree ( search [ 'res' ] ) return end search_count = wldap32 . ldap_count_entries ( session_handle , search [ 'res' ] ) [ 'return' ] if ( search_count == 0 ) print_error ( "No entries retrieved" ) wldap32 . ldap_msgfree ( search [ 'res' ] ) return end print_status ( "Entries retrieved: #{search_count}" ) pEntries = [ ] entry_results = [ ] if datastore [ 'MAX_SEARCH' ] == 0 max_search = search_count else max_search = [ datastore [ 'MAX_SEARCH' ] , search_count ] . min end 0 . upto ( max_search - 1 ) do | i | if ( i == 0 ) pEntries [ 0 ] = wldap32 . ldap_first_entry ( session_handle , search [ 'res' ] ) [ 'return' ] end if ( pEntries [ i ] == 0 ) print_error ( "Failed to get entry" ) wldap32 . ldap_msgfree ( search [ 'res' ] ) return end vprint_status ( "Entry #{i}: 0x#{pEntries[i].to_s(16)}" ) entry = get_entry ( pEntries [ i ] ) if client . platform =~ / x64 / pEntries [ i + 1 ] = entry [ 4 ] else pEntries [ i + 1 ] = entry [ 3 ] end ber = get_ber ( entry ) field_results = [ ] fields . each do | field | vprint_status ( "Field: #{field}" ) value_results = "" values = get_values_from_ber ( ber , field ) values_result = "" values_result = values . join ( ',' ) if values vprint_status ( "Values #{values}" ) field_results << values_result end entry_results << field_results end return { :fields => fields , :results => entry_results } end
Performs a query on the LDAP session
Performs a query on the LDAP session
# Entries are a linked list...
None
Hash
session_handle
base
scope
filter
fields
Attributes to retrieve
Attributes to retrieve