Path: blob/master/modules/post/windows/gather/enum_ad_managedby_groups.rb
19715 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Auxiliary::Report7include Msf::Post::Windows::LDAP89USER_FIELDS = [10'cn',11'distinguishedname',12'managedBy',13'description'14].freeze1516def initialize(info = {})17super(18update_info(19info,20'Name' => 'Windows Gather Active Directory Managed Groups',21'Description' => %q{22This module will enumerate AD groups on the specified domain which are specifically managed.23It cannot at the moment identify whether the 'Manager can update membership list' option24option set; if so, it would allow that member to update the contents of that group. This25could either be used as a persistence mechanism (for example, set your user as the 'Domain26Admins' group manager) or could be used to detect privilege escalation opportunities27without having domain admin privileges.28},29'License' => MSF_LICENSE,30'Author' => [31'Stuart Morgan <stuart.morgan[at]mwrinfosecurity.com>'32],33'Platform' => [ 'win' ],34'SessionTypes' => [ 'meterpreter' ],35'Notes' => {36'Stability' => [CRASH_SAFE],37'SideEffects' => [],38'Reliability' => []39}40)41)4243register_options([44OptString.new('ADDITIONAL_FIELDS', [false, 'Additional group fields to retrieve, comma separated.', nil]),45OptBool.new('RESOLVE_MANAGERS', [true, 'Query LDAP to get the account name of group managers.', true]),46OptBool.new('SECURITY_GROUPS_ONLY', [true, 'Only include security groups.', true])47])48end4950def run51@user_fields = USER_FIELDS.dup5253if datastore['ADDITIONAL_FIELDS']54additional_fields = datastore['ADDITIONAL_FIELDS'].gsub(/\s+/, '').split(',')55@user_fields.push(*additional_fields)56end5758max_search = datastore['MAX_SEARCH']5960begin61qs = '(&(objectClass=group)(managedBy=*))'62if datastore['SECURITY_GROUPS_ONLY']63qs = '(&(objectClass=group)(managedBy=*)(groupType:1.2.840.113556.1.4.803:=2147483648))'64end65q = query(qs, max_search, @user_fields)66rescue ::RuntimeError, ::Rex::Post::Meterpreter::RequestError => e67# Can't bind or in a network w/ limited accounts68print_error(e.message)69return70end7172if q.nil? || q[:results].empty?73print_status('No results returned.')74else75@user_fields << 'Manager Account Name' if datastore['RESOLVE_MANAGERS']76results_table = parse_results(q[:results])77print_line results_table.to_s78end79end8081# Takes the results of LDAP query, parses them into a table82def parse_results(results)83results_table = Rex::Text::Table.new(84'Header' => 'Groups with Managers',85'Indent' => 1,86'SortIndex' => -1,87'Columns' => @user_fields88)8990results.each do |result|91row = []9293result.each do |field|94if field.nil?95row << ''96else97row << field[:value]98end99end100if datastore['RESOLVE_MANAGERS']101begin102m = query("(distinguishedName=#{result[2][:value]})", 1, ['sAMAccountName'])103if !m.nil? && !m[:results].empty?104row << m[:results][0][0][:value]105else106row << ''107end108rescue StandardError109row << ''110end111end112results_table << row113end114results_table115end116end117118119