@@ -69,7 +69,7 @@ class Railgun
69
69
# class name: Def_my_dll
70
70
# entry below: 'my_dll'
71
71
#
72
- BUILTIN_DLLS = {
72
+ BUILTIN_LIBRARIES = {
73
73
'linux' => [
74
74
'libc'
75
75
] . freeze ,
@@ -95,33 +95,34 @@ class Railgun
95
95
} . freeze
96
96
97
97
##
98
- # Returns a Hash containing DLLs added to this instance with #add_dll
99
- # as well as references to any frozen cached dlls added directly in #get_dll
100
- # and copies of any frozen dlls (added directly with #add_function)
101
- # that the user attempted to modify with #add_function.
98
+ # Returns a Hash containing DLLs added to this instance with #add_library as
99
+ # well as references to any frozen cached libraries added directly in
100
+ # #get_library and copies of any frozen libraries (added directly with
101
+ # #add_function) that the user attempted to modify with #add_function.
102
102
#
103
- # Keys are friendly DLL names and values are the corresponding DLL instance
104
- attr_accessor :dlls
103
+ # Keys are friendly library names and values are the corresponding library instance
104
+ attr_accessor :libraries
105
105
106
106
##
107
107
# Contains a reference to the client that corresponds to this instance of railgun
108
108
attr_accessor :client
109
109
110
110
##
111
- # These DLLs are loaded lazily and then shared amongst all railgun instances.
112
- # For safety reasons this variable should only be read/written within #get_dll.
113
- @@cached_dlls = { }
111
+ # These libraries are loaded lazily and then shared amongst all railgun
112
+ # instances. For safety reasons this variable should only be read/written
113
+ # within #get_library.
114
+ @@cached_libraries = { }
114
115
115
- # if you are going to touch @@cached_dlls , wear protection
116
+ # if you are going to touch @@cached_libraries , wear protection
116
117
@@cache_semaphore = Mutex . new
117
118
118
119
def initialize ( client )
119
120
self . client = client
120
- self . dlls = { }
121
+ self . libraries = { }
121
122
end
122
123
123
- def self . builtin_dlls
124
- BUILTIN_DLLS [ client . platform ]
124
+ def self . builtin_libraries
125
+ BUILTIN_LIBRARIES [ client . platform ]
125
126
end
126
127
127
128
#
@@ -196,106 +197,109 @@ def memwrite(address, data, length=nil)
196
197
end
197
198
198
199
#
199
- # Adds a function to an existing DLL definition.
200
+ # Adds a function to an existing library definition.
200
201
#
201
- # If the DLL definition is frozen (ideally this should be the case for all
202
- # cached dlls ) an unfrozen copy is created and used henceforth for this
202
+ # If the library definition is frozen (ideally this should be the case for all
203
+ # cached libraries ) an unfrozen copy is created and used henceforth for this
203
204
# instance.
204
205
#
205
- def add_function ( dll_name , function_name , return_type , params , remote_name = nil , calling_conv = "stdcall" )
206
-
207
- unless known_dll_names . include? ( dll_name )
208
- raise "DLL #{ dll_name } not found. Known DLLs: #{ PP . pp ( known_dll_names , "" ) } "
206
+ def add_function ( lib_name , function_name , return_type , params , remote_name = nil , calling_conv = 'stdcall' )
207
+ unless known_library_names . include? ( lib_name )
208
+ raise "Library #{ lib_name } not found. Known libraries: #{ PP . pp ( known_library_names , '' ) } "
209
209
end
210
210
211
- dll = get_dll ( dll_name )
211
+ lib = get_library ( lib_name )
212
212
213
- # For backwards compatibility, we ensure the dll is thawed
214
- if dll . frozen?
215
- # Duplicate not only the dll , but its functions as well. Frozen status will be lost
216
- dll = Marshal . load ( Marshal . dump ( dll ) )
213
+ # For backwards compatibility, we ensure the library is thawed
214
+ if lib . frozen?
215
+ # Duplicate not only the library , but its functions as well, frozen status will be lost
216
+ lib = Marshal . load ( Marshal . dump ( lib ) )
217
217
218
- # Update local dlls with the modifiable duplicate
219
- dlls [ dll_name ] = dll
218
+ # Update local libraries with the modifiable duplicate
219
+ libraries [ lib_name ] = lib
220
220
end
221
221
222
- dll . add_function ( function_name , return_type , params , remote_name , calling_conv )
222
+ lib . add_function ( function_name , return_type , params , remote_name , calling_conv )
223
223
end
224
224
225
225
#
226
- # Adds a DLL to this Railgun.
226
+ # Adds a library to this Railgun.
227
227
#
228
228
# The +remote_name+ is the name used on the remote system and should be
229
- # set appropriately if you want to include a path or the DLL name contains
229
+ # set appropriately if you want to include a path or the library name contains
230
230
# non-ruby-approved characters.
231
231
#
232
- # Raises an exception if a dll with the given name has already been
232
+ # Raises an exception if a library with the given name has already been
233
233
# defined.
234
234
#
235
- def add_dll ( dll_name , remote_name = dll_name )
236
-
237
- if dlls . has_key? dll_name
238
- raise "A DLL of name #{ dll_name } has already been loaded."
235
+ def add_library ( lib_name , remote_name = lib_name )
236
+ if libraries . has_key? lib_name
237
+ raise "A library of name #{ lib_name } has already been loaded."
239
238
end
240
239
241
- dlls [ dll_name ] = DLL . new ( remote_name , constant_manager )
240
+ libraries [ lib_name ] = DLL . new ( remote_name , constant_manager )
242
241
end
242
+ alias_method :add_dll , :add_library
243
243
244
-
245
- def known_dll_names
246
- return BUILTIN_DLLS [ client . platform ] | dlls . keys
244
+ def known_library_names
245
+ return BUILTIN_LIBRARIES [ client . platform ] | libraries . keys
247
246
end
248
247
249
248
#
250
- # Attempts to provide a DLL instance of the given name. Handles lazy
251
- # loading and caching. Note that if a DLL of the given name does not
252
- # exist, returns nil
249
+ # Attempts to provide a library instance of the given name. Handles lazy
250
+ # loading and caching. Note that if a library of the given name does not exist
251
+ # then nil is returned.
253
252
#
254
- def get_dll ( dll_name )
255
- # If the DLL is not local, we now either load it from cache or load it lazily.
256
- # In either case, a reference to the dll is stored in the collection "dlls"
257
- # If the DLL can not be found/created, no actions are taken
258
- unless dlls . has_key? dll_name
259
- # We read and write to @@cached_dlls and rely on state consistency
253
+ def get_library ( lib_name )
254
+ # If the library is not local, we now either load it from cache or load it
255
+ # lazily. In either case, a reference to the library is stored in the
256
+ # collection "libraries". If the library can not be found/created, no
257
+ # actions are taken.
258
+ unless libraries . has_key? lib_name
259
+ # use a platform-specific name for caching to avoid conflicts with
260
+ # libraries that exist on multiple platforms, e.g. libc.
261
+ cached_lib_name = "#{ client . platform } .#{ lib_name } "
262
+ # We read and write to @@cached_libraries and rely on state consistency
260
263
@@cache_semaphore . synchronize do
261
- if @@cached_dlls . has_key? dll_name
262
- dlls [ dll_name ] = @@cached_dlls [ dll_name ]
263
- elsif BUILTIN_DLLS [ client . platform ] . include? dll_name
264
+ if @@cached_libraries . has_key? cached_lib_name
265
+ libraries [ lib_name ] = @@cached_libraries [ cached_lib_name ]
266
+ elsif BUILTIN_LIBRARIES [ client . platform ] . include? lib_name
264
267
# I highly doubt this case will ever occur, but I am paranoid
265
- if dll_name !~ /^\w +$/
266
- raise "DLL name #{ dll_name } is bad. Correct Railgun::BUILTIN_DLLS "
268
+ if lib_name !~ /^\w +$/
269
+ raise "Library name #{ lib_name } is bad. Correct Railgun::BUILTIN_LIBRARIES[' #{ client . platform } '] "
267
270
end
268
271
269
- require "rex/post/meterpreter/extensions/stdapi/railgun/def/#{ client . platform } /def_#{ dll_name } "
270
- dll = Def . const_get ( "Def_#{ client . platform } _#{ dll_name } " ) . create_dll ( constant_manager ) . freeze
272
+ require "rex/post/meterpreter/extensions/stdapi/railgun/def/#{ client . platform } /def_#{ lib_name } "
273
+ lib = Def . const_get ( "Def_#{ client . platform } _#{ lib_name } " ) . create_dll ( constant_manager ) . freeze
271
274
272
- @@cached_dlls [ dll_name ] = dll
273
- dlls [ dll_name ] = dll
275
+ @@cached_libraries [ cached_lib_name ] = lib
276
+ libraries [ lib_name ] = lib
274
277
end
275
278
end
276
279
277
280
end
278
281
279
- return dlls [ dll_name ]
282
+ return libraries [ lib_name ]
280
283
end
284
+ alias_method :get_dll , :get_library
281
285
282
286
#
283
287
# Fake having members like user32 and kernel32.
284
288
# reason is that
285
289
# ...user32.MessageBoxW()
286
290
# is prettier than
287
- # ...dlls ["user32"].functions["MessageBoxW"]()
291
+ # ...libraries ["user32"].functions["MessageBoxW"]()
288
292
#
289
- def method_missing ( dll_symbol , *args )
290
- dll_name = dll_symbol . to_s
293
+ def method_missing ( lib_symbol , *args )
294
+ lib_name = lib_symbol . to_s
291
295
292
- unless known_dll_names . include? dll_name
293
- raise "DLL #{ dll_name } not found. Known DLLs : #{ PP . pp ( known_dll_names , '' ) } "
296
+ unless known_library_names . include? lib_name
297
+ raise "Library #{ lib_name } not found. Known libraries : #{ PP . pp ( known_library_names , '' ) } "
294
298
end
295
299
296
- dll = get_dll ( dll_name )
300
+ lib = get_library ( lib_name )
297
301
298
- return DLLWrapper . new ( dll , client )
302
+ return DLLWrapper . new ( lib , client )
299
303
end
300
304
301
305
#
0 commit comments