Class: Rex::Post::Meterpreter::Extensions::Stdapi::Railgun::Railgun

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb

Overview

The Railgun class to dynamically expose the Windows API.

Constant Summary collapse

BUILTIN_LIBRARIES =

Railgun::Library's that have builtin definitions.

If you want to add additional library definitions to be preloaded create a definition class 'rex/post/meterpreter/extensions/stdapi/railgun/def/$platform/'. Naming is important and should follow convention. For example, if your library's name was “my_library” file name: def_my_library.rb class name: Def_my_library entry below: 'my_library'

{
  'linux' => [
    'libc'
  ].freeze,
  'osx' => [
    'libc',
    'libobjc'
  ].freeze,
  'windows' => [
    'kernel32',
    'ntdll',
    'user32',
    'ws2_32',
    'iphlpapi',
    'advapi32',
    'shell32',
    'netapi32',
    'crypt32',
    'wlanapi',
    'wldap32',
    'version',
    'psapi'
  ].freeze
}.freeze
@@cached_libraries =

These libraries are loaded lazily and then shared amongst all railgun instances. For safety reasons this variable should only be read/written within #get_library.

{}
@@cache_semaphore =

if you are going to touch @@cached_libraries, wear protection

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Railgun

Returns a new instance of Railgun.


119
120
121
122
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 119

def initialize(client)
  self.client = client
  self.libraries = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(lib_symbol, *args) ⇒ Object

Fake having members like user32 and kernel32. reason is that

...user32.MessageBoxW()

is prettier than

...libraries["user32"].functions["MessageBoxW"]()

293
294
295
296
297
298
299
300
301
302
303
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 293

def method_missing(lib_symbol, *args)
  lib_name = lib_symbol.to_s

  unless known_library_names.include? lib_name
    raise "Library #{lib_name} not found. Known libraries: #{PP.pp(known_library_names, '')}"
  end

  lib = get_library(lib_name)

  return LibraryWrapper.new(lib, client)
end

Instance Attribute Details

#clientObject

Contains a reference to the client that corresponds to this instance of railgun


108
109
110
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 108

def client
  @client
end

#librariesObject

Returns a Hash containing libraries added to this instance with #add_library as well as references to any frozen cached libraries added directly in #get_library and copies of any frozen libraries (added directly with #add_function) that the user attempted to modify with #add_function.

Keys are friendly library names and values are the corresponding library instance


104
105
106
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 104

def libraries
  @libraries
end

Class Method Details

.builtin_librariesObject


124
125
126
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 124

def self.builtin_libraries
  BUILTIN_LIBRARIES[client.platform]
end

Instance Method Details

#add_function(lib_name, function_name, return_type, params, remote_name = nil, calling_conv = 'stdcall') ⇒ Object

Adds a function to an existing library definition.

If the library definition is frozen (ideally this should be the case for all cached libraries) an unfrozen copy is created and used henceforth for this instance.


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 206

def add_function(lib_name, function_name, return_type, params, remote_name=nil, calling_conv='stdcall')
  unless known_library_names.include?(lib_name)
    raise "Library #{lib_name} not found. Known libraries: #{PP.pp(known_library_names, '')}"
  end

  lib = get_library(lib_name)

  # For backwards compatibility, we ensure the library is thawed
  if lib.frozen?
    # Duplicate not only the library, but its functions as well, frozen status will be lost
    lib = Marshal.load(Marshal.dump(lib))

    # Update local libraries with the modifiable duplicate
    libraries[lib_name] = lib
  end

  lib.add_function(function_name, return_type, params, remote_name, calling_conv)
end

#add_library(lib_name, remote_name = lib_name) ⇒ Object Also known as: add_dll

Adds a library to this Railgun.

The remote_name is the name used on the remote system and should be set appropriately if you want to include a path or the library name contains non-ruby-approved characters.

Raises an exception if a library with the given name has already been defined.


235
236
237
238
239
240
241
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 235

def add_library(lib_name, remote_name=lib_name)
  if libraries.has_key? lib_name
    raise "A library of name #{lib_name} has already been loaded."
  end

  libraries[lib_name] = Library.new(remote_name, constant_manager)
end

#api_constantsObject

Return this Railgun's platform specific ApiConstants class.


142
143
144
145
146
147
148
149
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 142

def api_constants
  if @api_constants.nil?
    require "rex/post/meterpreter/extensions/stdapi/railgun/def/#{client.platform}/api_constants"
    @api_constants = Def.const_get('DefApiConstants_' << client.platform)
  end

  return @api_constants
end

#const(str) ⇒ Object

Return a constant matching str.


308
309
310
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 308

def const(str)
  return constant_manager.parse(str)
end

#constant_managerObject

Return this Railgun's ConstManager instance, initially populated with constants defined in ApiConstants.


155
156
157
158
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 155

def constant_manager
  # Loads lazily
  return api_constants.manager
end

#get_library(lib_name) ⇒ Object Also known as: get_dll

Attempts to provide a library instance of the given name. Handles lazy loading and caching. Note that if a library of the given name does not exist then nil is returned.


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 253

def get_library(lib_name)
  # If the library is not local, we now either load it from cache or load it
  # lazily. In either case, a reference to the library is stored in the
  # collection "libraries". If the library can not be found/created, no
  # actions are taken.
  unless libraries.has_key? lib_name
    # use a platform-specific name for caching to avoid conflicts with
    # libraries that exist on multiple platforms, e.g. libc.
    cached_lib_name = "#{client.platform}.#{lib_name}"
    # We read and write to @@cached_libraries and rely on state consistency
    @@cache_semaphore.synchronize do
      if @@cached_libraries.has_key? cached_lib_name
        libraries[lib_name] = @@cached_libraries[cached_lib_name]
      elsif BUILTIN_LIBRARIES[client.platform].include? lib_name
        # I highly doubt this case will ever occur, but I am paranoid
        if lib_name !~ /^\w+$/
          raise "Library name #{lib_name} is bad. Correct Railgun::BUILTIN_LIBRARIES['#{client.platform}']"
        end

        require "rex/post/meterpreter/extensions/stdapi/railgun/def/#{client.platform}/def_#{lib_name}"
        lib = Def.const_get("Def_#{client.platform}_#{lib_name}").create_library(constant_manager).freeze

        @@cached_libraries[cached_lib_name] = lib
        libraries[lib_name] = lib
      end
    end

  end

  return libraries[lib_name]
end

#known_library_namesObject


244
245
246
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 244

def known_library_names
  return BUILTIN_LIBRARIES[client.platform] | libraries.keys
end

#memread(address, length) ⇒ Object

Read data from a memory address on the host (useful for working with LPVOID parameters)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 164

def memread(address, length)

  raise "Invalid parameters." if(not address or not length)

  request = Packet.create_request(COMMAND_ID_STDAPI_RAILGUN_MEMREAD)

  request.add_tlv(TLV_TYPE_RAILGUN_MEM_ADDRESS, address)
  request.add_tlv(TLV_TYPE_RAILGUN_MEM_LENGTH, length)

  response = client.send_request(request)
  if(response.result == 0)
    return response.get_tlv_value(TLV_TYPE_RAILGUN_MEM_DATA)
  end

  return nil
end

#memwrite(address, data, length = nil) ⇒ Object

Write data to a memory address on the host (useful for working with LPVOID parameters)


185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 185

def memwrite(address, data, length=nil)

  length = data.length if length.nil?
  raise "Invalid parameters." if(not address or not data or not length)

  request = Packet.create_request(COMMAND_ID_STDAPI_RAILGUN_MEMWRITE)
  request.add_tlv(TLV_TYPE_RAILGUN_MEM_ADDRESS, address)
  request.add_tlv(TLV_TYPE_RAILGUN_MEM_DATA, data)
  request.add_tlv(TLV_TYPE_RAILGUN_MEM_LENGTH, length)

  response = client.send_request(request)
  return response.result == 0
end

#multi(functions) ⇒ Object

The multi-call shorthand ([“kernel32”, “ExitProcess”, [0]])


315
316
317
318
319
320
321
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 315

def multi(functions)
  if @multicaller.nil?
    @multicaller = MultiCaller.new(client, self, constant_manager)
  end

  return @multicaller.call(functions)
end

#utilObject

Return this Railgun's Util instance.


131
132
133
134
135
136
137
# File 'lib/rex/post/meterpreter/extensions/stdapi/railgun/railgun.rb', line 131

def util
  if @util.nil?
    @util = Util.new(self, client.arch)
  end

  return @util
end