@@ -68,33 +68,33 @@ def load_library(opts)
68
68
load_flags = LOAD_LIBRARY_FLAG_LOCAL
69
69
70
70
# No library path, no cookie.
71
- if ( library_path == nil )
71
+ if library_path . nil?
72
72
raise ArgumentError , "No library file path was supplied" , caller
73
73
end
74
74
75
75
# Set up the proper loading flags
76
- if ( opts [ 'UploadLibrary' ] )
76
+ if opts [ 'UploadLibrary' ]
77
77
load_flags &= ~LOAD_LIBRARY_FLAG_LOCAL
78
78
end
79
- if ( opts [ 'SaveToDisk' ] )
79
+ if opts [ 'SaveToDisk' ]
80
80
load_flags |= LOAD_LIBRARY_FLAG_ON_DISK
81
81
end
82
- if ( opts [ 'Extension' ] )
82
+ if opts [ 'Extension' ]
83
83
load_flags |= LOAD_LIBRARY_FLAG_EXTENSION
84
84
end
85
85
86
86
# Create a request packet
87
87
request = Packet . create_request ( 'core_loadlib' )
88
88
89
89
# If we must upload the library, do so now
90
- if ( ( load_flags & LOAD_LIBRARY_FLAG_LOCAL ) != LOAD_LIBRARY_FLAG_LOCAL )
90
+ if ( load_flags & LOAD_LIBRARY_FLAG_LOCAL ) != LOAD_LIBRARY_FLAG_LOCAL
91
91
image = ''
92
92
93
93
::File . open ( library_path , 'rb' ) { |f |
94
94
image = f . read
95
95
}
96
96
97
- if ( image != nil )
97
+ if ! image . nil?
98
98
request . add_tlv ( TLV_TYPE_DATA , image , false , client . capabilities [ :zlib ] )
99
99
else
100
100
raise RuntimeError , "Failed to serialize library #{ library_path } ." , caller
@@ -103,7 +103,7 @@ def load_library(opts)
103
103
# If it's an extension we're dealing with, rename the library
104
104
# path of the local and target so that it gets loaded with a random
105
105
# name
106
- if ( opts [ 'Extension' ] )
106
+ if opts [ 'Extension' ]
107
107
library_path = "ext" + rand ( 1000000 ) . to_s + ".#{ client . binary_suffix } "
108
108
target_path = library_path
109
109
end
@@ -113,17 +113,17 @@ def load_library(opts)
113
113
request . add_tlv ( TLV_TYPE_LIBRARY_PATH , library_path )
114
114
request . add_tlv ( TLV_TYPE_FLAGS , load_flags )
115
115
116
- if ( target_path != nil )
116
+ if ! target_path . nil?
117
117
request . add_tlv ( TLV_TYPE_TARGET_PATH , target_path )
118
118
end
119
119
120
120
# Transmit the request and wait the default timeout seconds for a response
121
121
response = self . client . send_packet_wait_response ( request , self . client . response_timeout )
122
122
123
123
# No response?
124
- if ( response == nil )
124
+ if response . nil?
125
125
raise RuntimeError , "No response was received to the core_loadlib request." , caller
126
- elsif ( response . result != 0 )
126
+ elsif response . result != 0
127
127
raise RuntimeError , "The core_loadlib request failed with result: #{ response . result } ." , caller
128
128
end
129
129
@@ -147,18 +147,22 @@ def load_library(opts)
147
147
# memory on the remote machine
148
148
#
149
149
def use ( mod , opts = { } )
150
- if ( mod == nil )
150
+ if mod . nil?
151
151
raise RuntimeError , "No modules were specified" , caller
152
152
end
153
153
# Get us to the installation root and then into data/meterpreter, where
154
154
# the file is expected to be
155
155
modname = "ext_server_#{ mod . downcase } "
156
156
path = MeterpreterBinaries . path ( modname , client . binary_suffix )
157
157
158
- if ( opts [ 'ExtensionPath' ] )
158
+ if opts [ 'ExtensionPath' ]
159
159
path = opts [ 'ExtensionPath' ]
160
160
end
161
161
162
+ if path . nil?
163
+ raise RuntimeError , "No module of the name #{ modname } .#{ client . binary_suffix } found" , caller
164
+ end
165
+
162
166
path = ::File . expand_path ( path )
163
167
164
168
# Load the extension DLL
@@ -187,24 +191,24 @@ def migrate( pid )
187
191
188
192
# Determine the architecture for the pid we are going to migrate into...
189
193
client . sys . process . processes . each { | p |
190
- if ( p [ 'pid' ] == pid )
194
+ if p [ 'pid' ] == pid
191
195
process = p
192
196
break
193
197
end
194
198
}
195
199
196
200
# We cant migrate into a process that does not exist.
197
- if ( process == nil )
201
+ if process . nil?
198
202
raise RuntimeError , "Cannot migrate into non existent process" , caller
199
203
end
200
204
201
205
# We cant migrate into a process that we are unable to open
202
- if ( process [ 'arch' ] == nil or process [ 'arch' ] . empty? )
206
+ if process [ 'arch' ] . nil? or process [ 'arch' ] . empty?
203
207
raise RuntimeError , "Cannot migrate into this process (insufficient privileges)" , caller
204
208
end
205
209
206
210
# And we also cant migrate into our own current process...
207
- if ( process [ 'pid' ] == client . sys . process . getpid )
211
+ if process [ 'pid' ] == client . sys . process . getpid
208
212
raise RuntimeError , "Cannot migrate into current process" , caller
209
213
end
210
214
@@ -213,10 +217,10 @@ def migrate( pid )
213
217
c . include ( ::Msf ::Payload ::Stager )
214
218
215
219
# Include the appropriate reflective dll injection module for the target process architecture...
216
- if ( process [ 'arch' ] == ARCH_X86 )
220
+ if process [ 'arch' ] == ARCH_X86
217
221
c . include ( ::Msf ::Payload ::Windows ::ReflectiveDllInject )
218
222
binary_suffix = "x86.dll"
219
- elsif ( process [ 'arch' ] == ARCH_X86_64 )
223
+ elsif process [ 'arch' ] == ARCH_X86_64
220
224
c . include ( ::Msf ::Payload ::Windows ::ReflectiveDllInject_x64 )
221
225
binary_suffix = "x64.dll"
222
226
else
@@ -225,7 +229,12 @@ def migrate( pid )
225
229
226
230
# Create the migrate stager
227
231
migrate_stager = c . new ( )
228
- migrate_stager . datastore [ 'DLL' ] = MeterpreterBinaries . path ( 'metsrv' , binary_suffix )
232
+
233
+ dll = MeterpreterBinaries . path ( 'metsrv' , binary_suffix )
234
+ if dll . nil?
235
+ raise RuntimeError , "metsrv.#{ binary_suffix } not found" , caller
236
+ end
237
+ migrate_stager . datastore [ 'DLL' ] = dll
229
238
230
239
blob = migrate_stager . stage_payload
231
240
@@ -253,7 +262,7 @@ def migrate( pid )
253
262
request . add_tlv ( TLV_TYPE_MIGRATE_PID , pid )
254
263
request . add_tlv ( TLV_TYPE_MIGRATE_LEN , blob . length )
255
264
request . add_tlv ( TLV_TYPE_MIGRATE_PAYLOAD , blob , false , client . capabilities [ :zlib ] )
256
- if ( process [ 'arch' ] == ARCH_X86_64 )
265
+ if process [ 'arch' ] == ARCH_X86_64
257
266
request . add_tlv ( TLV_TYPE_MIGRATE_ARCH , 2 ) # PROCESS_ARCH_X64
258
267
else
259
268
request . add_tlv ( TLV_TYPE_MIGRATE_ARCH , 1 ) # PROCESS_ARCH_X86
@@ -301,7 +310,7 @@ def migrate( pid )
301
310
# Update the meterpreter platform/suffix for loading extensions as we may have changed target architecture
302
311
# sf: this is kinda hacky but it works. As ruby doesnt let you un-include a module this is the simplest solution I could think of.
303
312
# If the platform specific modules Meterpreter_x64_Win/Meterpreter_x86_Win change significantly we will need a better way to do this.
304
- if ( process [ 'arch' ] == ARCH_X86_64 )
313
+ if process [ 'arch' ] == ARCH_X86_64
305
314
client . platform = 'x64/win64'
306
315
client . binary_suffix = 'x64.dll'
307
316
else
0 commit comments