Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/meterpreter/extensions/stdapi/sys/config.rb
Views: 11794
# -*- coding: binary -*-12require 'rex/post/process'3require 'rex/post/meterpreter/packet'4require 'rex/post/meterpreter/client'5require 'rex/post/meterpreter/extensions/stdapi/constants'6require 'rex/post/meterpreter/extensions/stdapi/stdapi'78module Rex9module Post10module Meterpreter11module Extensions12module Stdapi13module Sys1415###16#17# This class provides access to remote system configuration and information.18#19###20class Config2122SYSTEM_SID = 'S-1-5-18'2324def initialize(client)25self.client = client26end2728#29# Returns the username that the remote side is running as.30#31def getuid(refresh: true)32if @uid.nil? || refresh33request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_GETUID)34response = client.send_request(request)35@uid = client.unicode_filter_encode( response.get_tlv_value(TLV_TYPE_USER_NAME) )36end37@uid38end3940#41# Gets the SID of the current process/thread.42#43def getsid44request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_GETSID)45response = client.send_request(request)46response.get_tlv_value(TLV_TYPE_SID)47end4849#50# Determine if the current process/thread is running as SYSTEM51#52def is_system?53getsid == SYSTEM_SID54end5556#57# Returns a list of currently active drivers used by the target system58#59def getdrivers60request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_DRIVER_LIST)61response = client.send_request(request)6263result = []6465response.each(TLV_TYPE_DRIVER_ENTRY) do |driver|66result << {67basename: driver.get_tlv_value(TLV_TYPE_DRIVER_BASENAME),68filename: driver.get_tlv_value(TLV_TYPE_DRIVER_FILENAME)69}70end7172result73end7475#76# Returns a hash of requested environment variables, along with their values.77# If a requested value doesn't exist in the response, then the value wasn't found.78#79def getenvs(*var_names)80request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_GETENV)8182var_names.each do |v|83request.add_tlv(TLV_TYPE_ENV_VARIABLE, v)84end8586response = client.send_request(request)87result = {}8889response.each(TLV_TYPE_ENV_GROUP) do |env|90var_name = env.get_tlv_value(TLV_TYPE_ENV_VARIABLE)91var_value = env.get_tlv_value(TLV_TYPE_ENV_VALUE)92result[var_name] = var_value93end9495result96end9798#99# Returns the value of a single requested environment variable name100#101def getenv(var_name)102_, value = getenvs(var_name).first103value104end105106#107# Returns the target's local system date and time.108#109def localtime110request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_LOCALTIME)111response = client.send_request(request)112(response.get_tlv_value(TLV_TYPE_LOCAL_DATETIME) || "").strip113end114115#116# Returns a hash of information about the remote computer.117#118def sysinfo(refresh: false)119request = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_SYSINFO)120if @sysinfo.nil? || refresh121response = client.send_request(request)122123@sysinfo = {124'Computer' => response.get_tlv_value(TLV_TYPE_COMPUTER_NAME),125'OS' => response.get_tlv_value(TLV_TYPE_OS_NAME),126'Architecture' => response.get_tlv_value(TLV_TYPE_ARCHITECTURE),127'BuildTuple' => response.get_tlv_value(TLV_TYPE_BUILD_TUPLE),128'System Language' => response.get_tlv_value(TLV_TYPE_LANG_SYSTEM),129'Domain' => response.get_tlv_value(TLV_TYPE_DOMAIN),130'Logged On Users' => response.get_tlv_value(TLV_TYPE_LOGGED_ON_USER_COUNT)131}132133# make sure we map the architecture across to x64 if x86_64 is returned134# to keep arch consistent across all session/machine types135if @sysinfo['Architecture']136@sysinfo['Architecture'] = ARCH_X64 if @sysinfo['Architecture'].strip == ARCH_X86_64137end138end139@sysinfo140end141142#143# Calls RevertToSelf on the remote machine.144#145def revert_to_self146client.send_request(Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_REV2SELF))147end148149#150# Steals the primary token from a target process151#152def steal_token(pid)153req = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_STEAL_TOKEN)154req.add_tlv(TLV_TYPE_PID, pid.to_i)155res = client.send_request(req)156client.unicode_filter_encode( res.get_tlv_value(TLV_TYPE_USER_NAME) )157end158159#160# Drops any assumed token161#162def drop_token163req = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_DROP_TOKEN)164res = client.send_request(req)165client.unicode_filter_encode( res.get_tlv_value(TLV_TYPE_USER_NAME) )166end167168#169# Updates the current token for impersonation170#171def update_token(token_handle)172req = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_UPDATE_TOKEN)173req.add_tlv(TLV_TYPE_HANDLE, token_handle.to_i)174res = client.send_request(req)175end176177#178# Enables all possible privileges179#180def getprivs181req = Packet.create_request(COMMAND_ID_STDAPI_SYS_CONFIG_GETPRIVS)182ret = []183res = client.send_request(req)184res.each(TLV_TYPE_PRIVILEGE) do |p|185ret << p.value186end187ret188end189190protected191192attr_accessor :client193194end195196end; end; end; end; end; end197198199200