2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-04-10 10:31:38 +00:00
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/post/process'
|
|
|
|
require 'rex/post/meterpreter/packet'
|
|
|
|
require 'rex/post/meterpreter/client'
|
|
|
|
require 'rex/post/meterpreter/extensions/stdapi/constants'
|
|
|
|
require 'rex/post/meterpreter/extensions/stdapi/stdapi'
|
|
|
|
require 'rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key'
|
|
|
|
require 'rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_value'
|
2011-06-12 00:01:29 +00:00
|
|
|
require 'rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/remote_registry_key'
|
2005-04-10 10:31:38 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
module Extensions
|
|
|
|
module Stdapi
|
2005-04-15 06:23:59 +00:00
|
|
|
module Sys
|
2005-04-10 10:31:38 +00:00
|
|
|
|
2005-04-12 05:53:29 +00:00
|
|
|
###
|
|
|
|
#
|
2009-12-31 15:52:30 +00:00
|
|
|
# This class provides access to the Windows registry on the remote
|
2005-04-12 05:53:29 +00:00
|
|
|
# machine.
|
|
|
|
#
|
|
|
|
###
|
2005-04-10 10:31:38 +00:00
|
|
|
class Registry
|
|
|
|
|
2009-12-31 15:52:30 +00:00
|
|
|
class << self
|
2005-04-10 10:31:38 +00:00
|
|
|
attr_accessor :client
|
|
|
|
end
|
|
|
|
|
2005-04-10 22:30:04 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Registry key interaction
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Opens the supplied registry key relative to the root key with
|
2009-12-31 15:52:30 +00:00
|
|
|
# the supplied permissions. Right now this is merely a wrapper around
|
2005-04-12 05:53:29 +00:00
|
|
|
# create_key.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2011-06-30 05:35:18 +00:00
|
|
|
|
|
|
|
def Registry.load_key(root_key,base_key,hive_file)
|
|
|
|
request = Packet.create_request('stdapi_registry_load_key')
|
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
request.add_tlv(TLV_TYPE_BASE_KEY, base_key)
|
2011-07-21 15:26:15 +00:00
|
|
|
request.add_tlv(TLV_TYPE_FILE_PATH, client.unicode_filter_decode( hive_file ))
|
2011-07-17 17:32:52 +00:00
|
|
|
|
2011-06-30 05:35:18 +00:00
|
|
|
response = client.send_request(request)
|
|
|
|
return response.get_tlv(TLV_TYPE_RESULT).value
|
|
|
|
end
|
|
|
|
|
|
|
|
def Registry.unload_key(root_key,base_key)
|
|
|
|
request = Packet.create_request('stdapi_registry_unload_key')
|
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
request.add_tlv(TLV_TYPE_BASE_KEY, base_key)
|
|
|
|
response = client.send_request(request)
|
|
|
|
return response.get_tlv(TLV_TYPE_RESULT).value
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-04-10 10:31:38 +00:00
|
|
|
def Registry.open_key(root_key, base_key, perm = KEY_READ)
|
2005-11-24 20:54:43 +00:00
|
|
|
# If no base key was provided, just return the root_key.
|
|
|
|
if (base_key == nil or base_key.length == 0)
|
|
|
|
return RegistrySubsystem::RegistryKey.new(client, root_key, base_key, perm, root_key)
|
|
|
|
end
|
|
|
|
|
2011-06-11 23:17:42 +00:00
|
|
|
request = Packet.create_request('stdapi_registry_open_key')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
request.add_tlv(TLV_TYPE_BASE_KEY, base_key)
|
|
|
|
request.add_tlv(TLV_TYPE_PERMISSION, perm)
|
|
|
|
|
|
|
|
response = client.send_request(request)
|
|
|
|
|
|
|
|
return Rex::Post::Meterpreter::Extensions::Stdapi::Sys::RegistrySubsystem::RegistryKey.new(
|
|
|
|
client, root_key, base_key, perm, response.get_tlv(TLV_TYPE_HKEY).value)
|
2005-04-10 22:30:04 +00:00
|
|
|
end
|
2005-04-10 10:31:38 +00:00
|
|
|
|
2011-06-12 00:01:29 +00:00
|
|
|
#
|
|
|
|
# Opens the supplied registry key on the specified remote host. Requires that the
|
|
|
|
# current process has credentials to access the target and that the target has the
|
|
|
|
# remote registry service running.
|
|
|
|
#
|
|
|
|
def Registry.open_remote_key(target_host, root_key)
|
|
|
|
|
|
|
|
request = Packet.create_request('stdapi_registry_open_remote_key')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_TARGET_HOST, target_host)
|
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
|
|
|
|
|
|
|
|
response = client.send_request(request)
|
|
|
|
|
|
|
|
return Rex::Post::Meterpreter::Extensions::Stdapi::Sys::RegistrySubsystem::RemoteRegistryKey.new(
|
|
|
|
client, target_host, root_key, response.get_tlv(TLV_TYPE_HKEY).value)
|
|
|
|
end
|
2011-07-17 17:32:52 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Creates the supplied registry key or opens it if it already exists.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 22:30:04 +00:00
|
|
|
def Registry.create_key(root_key, base_key, perm = KEY_READ)
|
|
|
|
request = Packet.create_request('stdapi_registry_create_key')
|
2009-12-31 15:52:30 +00:00
|
|
|
|
2005-04-10 10:31:38 +00:00
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
request.add_tlv(TLV_TYPE_BASE_KEY, base_key)
|
|
|
|
request.add_tlv(TLV_TYPE_PERMISSION, perm)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
response = client.send_request(request)
|
2005-04-10 10:31:38 +00:00
|
|
|
|
2005-04-15 06:23:59 +00:00
|
|
|
return Rex::Post::Meterpreter::Extensions::Stdapi::Sys::RegistrySubsystem::RegistryKey.new(
|
|
|
|
client, root_key, base_key, perm, response.get_tlv(TLV_TYPE_HKEY).value)
|
2005-04-10 22:30:04 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Deletes the supplied registry key.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-11 01:28:33 +00:00
|
|
|
def Registry.delete_key(root_key, base_key, recursive = true)
|
|
|
|
request = Packet.create_request('stdapi_registry_delete_key')
|
|
|
|
flags = 0
|
|
|
|
|
|
|
|
if (recursive)
|
|
|
|
flags |= DELETE_KEY_FLAG_RECURSIVE
|
|
|
|
end
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_ROOT_KEY, root_key)
|
|
|
|
request.add_tlv(TLV_TYPE_BASE_KEY, base_key)
|
|
|
|
request.add_tlv(TLV_TYPE_FLAGS, flags)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
if (client.send_request(request) != nil)
|
2005-04-11 01:28:33 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
2009-12-31 15:52:30 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Closes the supplied registry key.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 22:30:04 +00:00
|
|
|
def Registry.close_key(hkey)
|
|
|
|
request = Packet.create_request('stdapi_registry_close_key')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
client.send_packet(request)
|
2005-04-10 22:30:04 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Enumerates the supplied registry key returning an array of key names.
|
|
|
|
#
|
2005-04-10 22:51:16 +00:00
|
|
|
def Registry.enum_key(hkey)
|
|
|
|
keys = []
|
|
|
|
request = Packet.create_request('stdapi_registry_enum_key')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
response = client.send_request(request)
|
2005-04-10 22:51:16 +00:00
|
|
|
|
|
|
|
# Enumerate through all of the registry keys
|
|
|
|
response.each(TLV_TYPE_KEY_NAME) { |key_name|
|
|
|
|
keys << key_name.value
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys
|
|
|
|
end
|
|
|
|
|
2005-04-10 22:30:04 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Registry value interaction
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Sets the registry value relative to the supplied hkey.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 22:30:04 +00:00
|
|
|
def Registry.set_value(hkey, name, type, data)
|
|
|
|
request = Packet.create_request('stdapi_registry_set_value')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
request.add_tlv(TLV_TYPE_VALUE_NAME, name)
|
|
|
|
request.add_tlv(TLV_TYPE_VALUE_TYPE, type)
|
|
|
|
|
|
|
|
if (type == REG_SZ)
|
2005-07-10 06:32:13 +00:00
|
|
|
data += "\x00"
|
2006-07-18 20:37:38 +00:00
|
|
|
elsif (type == REG_DWORD)
|
2005-04-11 15:45:33 +00:00
|
|
|
data = [ data.to_i ].pack("V")
|
2005-04-10 22:30:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_VALUE_DATA, data)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
response = client.send_request(request)
|
2005-04-10 22:30:04 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Queries the registry value supplied in name and returns an
|
|
|
|
# initialized RegistryValue instance if a match is found.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 22:30:04 +00:00
|
|
|
def Registry.query_value(hkey, name)
|
|
|
|
request = Packet.create_request('stdapi_registry_query_value')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
request.add_tlv(TLV_TYPE_VALUE_NAME, name)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
response = client.send_request(request)
|
2005-04-10 22:30:04 +00:00
|
|
|
|
|
|
|
data = response.get_tlv(TLV_TYPE_VALUE_DATA).value;
|
|
|
|
type = response.get_tlv(TLV_TYPE_VALUE_TYPE).value;
|
|
|
|
|
|
|
|
if (type == REG_SZ)
|
2006-07-18 20:37:38 +00:00
|
|
|
data = data[0..-2]
|
2005-04-10 22:30:04 +00:00
|
|
|
elsif (type == REG_DWORD)
|
|
|
|
data = data.unpack("N")[0]
|
|
|
|
end
|
|
|
|
|
2005-04-15 06:23:59 +00:00
|
|
|
return Rex::Post::Meterpreter::Extensions::Stdapi::Sys::RegistrySubsystem::RegistryValue.new(
|
|
|
|
client, hkey, name, type, data)
|
2005-04-10 22:30:04 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Deletes the registry value supplied in name from the supplied
|
|
|
|
# registry key.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 22:30:04 +00:00
|
|
|
def Registry.delete_value(hkey, name)
|
|
|
|
request = Packet.create_request('stdapi_registry_delete_value')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
request.add_tlv(TLV_TYPE_VALUE_NAME, name)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
if (client.send_request(request) != nil)
|
2005-04-10 22:30:04 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
2005-04-10 10:31:38 +00:00
|
|
|
end
|
|
|
|
|
2009-12-31 15:52:30 +00:00
|
|
|
#
|
|
|
|
# Queries the registry class name and returns a string
|
|
|
|
#
|
|
|
|
def Registry.query_class(hkey)
|
|
|
|
request = Packet.create_request('stdapi_registry_query_class')
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
|
|
|
|
response = client.send_request(request)
|
|
|
|
cls = response.get_tlv(TLV_TYPE_VALUE_DATA)
|
|
|
|
return nil if not cls
|
|
|
|
data = cls.value.gsub(/\x00.*/, '')
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:53:29 +00:00
|
|
|
# Enumerates all of the values at the supplied hkey including their
|
|
|
|
# names. An array of RegistryValue's is returned.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-11 01:28:33 +00:00
|
|
|
def Registry.enum_value(hkey)
|
|
|
|
request = Packet.create_request('stdapi_registry_enum_value')
|
|
|
|
values = []
|
|
|
|
|
|
|
|
request.add_tlv(TLV_TYPE_HKEY, hkey)
|
|
|
|
|
2005-04-11 01:59:45 +00:00
|
|
|
response = client.send_request(request)
|
2005-04-11 01:28:33 +00:00
|
|
|
|
|
|
|
response.each(TLV_TYPE_VALUE_NAME) { |value_name|
|
2005-04-15 06:23:59 +00:00
|
|
|
values << Rex::Post::Meterpreter::Extensions::Stdapi::Sys::RegistrySubsystem::RegistryValue.new(
|
|
|
|
client, hkey, value_name.value)
|
2005-04-11 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return values
|
|
|
|
end
|
|
|
|
|
2005-11-18 01:12:45 +00:00
|
|
|
#
|
|
|
|
# Return the key value associated with the supplied string. This is useful
|
|
|
|
# for converting HKLM as a string into its actual integer representation.
|
|
|
|
#
|
|
|
|
def self.key2str(key)
|
|
|
|
if (key == 'HKLM' or key == 'HKEY_LOCAL_MACHINE')
|
|
|
|
return HKEY_LOCAL_MACHINE
|
|
|
|
elsif (key == 'HKCU' or key == 'HKEY_CURRENT_USER')
|
|
|
|
return HKEY_CURRENT_USER
|
|
|
|
elsif (key == 'HKU' or key == 'HKEY_USERS')
|
|
|
|
return HKEY_USERS
|
|
|
|
elsif (key == 'HKCR' or key == 'HKEY_CLASSES_ROOT')
|
|
|
|
return HKEY_CLASSES_ROOT
|
|
|
|
elsif (key == 'HKEY_CURRENT_CONFIG')
|
|
|
|
return HKEY_CURRENT_CONFIG
|
|
|
|
elsif (key == 'HKEY_PERFORMANCE_DATA')
|
|
|
|
return HKEY_PERFORMANCE_DATA
|
|
|
|
elsif (key == 'HKEY_DYN_DATA')
|
|
|
|
return HKEY_DYN_DATA
|
|
|
|
else
|
|
|
|
raise ArgumentError, "Unknown key: #{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the integer value associated with the supplied registry value
|
|
|
|
# type (like REG_SZ).
|
|
|
|
#
|
|
|
|
def self.type2str(type)
|
2009-12-31 15:52:30 +00:00
|
|
|
return REG_SZ if (type == 'REG_SZ')
|
|
|
|
return REG_DWORD if (type == 'REG_DWORD')
|
|
|
|
return REG_BINARY if (type == 'REG_BINARY')
|
|
|
|
return REG_EXPAND_SZ if (type == 'REG_EXPAND_SZ')
|
|
|
|
return REG_NONE if (type == 'REG_NONE')
|
2005-11-18 01:12:45 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Split the supplied full registry key into its root key and base key. For
|
|
|
|
# instance, passing HKLM\Software\Dog will return [ HKEY_LOCAL_MACHINE,
|
|
|
|
# 'Software\Dog' ]
|
|
|
|
#
|
|
|
|
def self.splitkey(str)
|
|
|
|
if (str =~ /^(.+?)\\(.*)$/)
|
|
|
|
[ key2str($1), $2 ]
|
|
|
|
else
|
2005-11-24 20:54:43 +00:00
|
|
|
[ key2str(str), nil ]
|
2005-11-18 01:12:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-10 10:31:38 +00:00
|
|
|
end
|
|
|
|
|
2009-12-31 15:52:30 +00:00
|
|
|
end; end; end; end; end; end
|
|
|
|
|