Skip to content

Commit

Permalink
Land rapid7#17196, Add new get_hostname library support for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 authored Nov 3, 2022
2 parents c502dd1 + 31a68fd commit 9f0b6dc
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/msf/core/post/windows/system.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f0b6dc

Please sign in to comment.