From 31a68fd562e3f652ad20762706b47d7581020e83 Mon Sep 17 00:00:00 2001 From: bcoles Date: Sun, 30 Oct 2022 00:03:56 +1100 Subject: [PATCH] Msf::Post::Windows: Add Msf::Post::Windows::System mixin --- lib/msf/core/post/windows/system.rb | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/msf/core/post/windows/system.rb diff --git a/lib/msf/core/post/windows/system.rb b/lib/msf/core/post/windows/system.rb new file mode 100644 index 000000000000..7384df88c42d --- /dev/null +++ b/lib/msf/core/post/windows/system.rb @@ -0,0 +1,58 @@ +# -*- coding: binary -*- + +module Msf + class Post + module Windows + module System + include Msf::Post::Common + + def initialize(info = {}) + super( + update_info( + info, + 'Compat' => { + 'Meterpreter' => { + 'Commands' => %w[ + stdapi_sys_config_sysinfo + ] + } + } + ) + ) + end + + # + # Gets the hostname of the system + # + # @return [String] hostname + # + def get_hostname + hostname = nil + + if session.type == 'meterpreter' + hostname = session.sys.config.sysinfo['Computer'].to_s + end + + if hostname.blank? && session.type == 'powershell' + hostname = cmd_exec('[System.Net.Dns]::GetHostName()').to_s + end + + if hostname.blank? && command_exists?('hostname') + hostname = cmd_exec('hostname').to_s + end + + if hostname.blank? + hostname = get_env('COMPUTERNAME').to_s + end + + raise if hostname.blank? + + report_host({ host: rhost, name: hostname.downcase }) + hostname.downcase + rescue StandardError + raise 'Unable to retrieve hostname' + end + end + end + end +end