652 lines
22 KiB
Ruby
652 lines
22 KiB
Ruby
module Msf
|
|
|
|
module Exploit::Remote::VIMSoap
|
|
include Msf::Exploit::Remote::HttpClient
|
|
|
|
def vim_get_session
|
|
soap_data =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveServiceContent xmlns="urn:vim25">
|
|
<_this type="ServiceInstance">ServiceInstance</_this>
|
|
</RetrieveServiceContent>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'data' => soap_data
|
|
}, 25)
|
|
@server_objects = Hash.from_xml(res.body)['Envelope']['Body']['RetrieveServiceContentResponse']['returnval']
|
|
@soap_action = "urn:vim25/#{@server_objects['about']['apiVersion']}"
|
|
if res.headers['Set-Cookie']
|
|
@vim_cookie = res.headers['Set-Cookie']
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
def vim_do_login(user, pass)
|
|
unless vim_get_session
|
|
return false
|
|
end
|
|
soap_data =
|
|
%Q|<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
|
|
<SOAP-ENV:Body>
|
|
<Login xmlns="urn:vim25">
|
|
<_this type="SessionManager">#{@server_objects['sessionManager']}</_this>
|
|
<userName>#{user}</userName>
|
|
<password>#{pass}</password>
|
|
</Login>
|
|
</SOAP-ENV:Body>
|
|
</SOAP-ENV:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_data,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
if res.code == 200
|
|
return :success
|
|
else
|
|
return :fail
|
|
end
|
|
end
|
|
|
|
|
|
def vim_get_dc_name(dc)
|
|
soap_req=
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Datacenter</type>
|
|
<pathSet>name</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Datacenter">#{dc}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
name = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
return name
|
|
end
|
|
|
|
|
|
def vim_get_dcs
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveServiceContent xmlns="urn:vim25">
|
|
<_this type="ServiceInstance">ServiceInstance</_this>
|
|
</RetrieveServiceContent>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrieveServiceContentResponse']['returnval']
|
|
@server_objects.merge!(hash)
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>ServiceInstance</type>
|
|
<pathSet>content</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="ServiceInstance">ServiceInstance</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
hash.delete('xsi:type')
|
|
@server_objects.merge!(hash)
|
|
|
|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Folder</type>
|
|
<pathSet>childEntity</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Folder">#{@server_objects['rootFolder']}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
tmp_dcs = []
|
|
tmp_dcs = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['ManagedObjectReference']
|
|
tmp_dcs.flatten!
|
|
tmp_dcs.each{|dc| @dcs << { 'name' => vim_get_dc_name(dc) , 'ref' => dc}}
|
|
end
|
|
|
|
def vim_get_hosts(datacenter)
|
|
dc_hosts = []
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Datacenter</type>
|
|
<pathSet>hostFolder</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Datacenter">#{datacenter}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
host_folders = []
|
|
host_folders << Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
host_folders.flatten!
|
|
|
|
compute_refs = []
|
|
host_folders.each do |folder|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Folder</type>
|
|
<pathSet>childEntity</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Folder">#{folder}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
ref = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['ManagedObjectReference']
|
|
if ref.nil? or ref.empty?
|
|
return nil
|
|
else
|
|
compute_refs << ref
|
|
end
|
|
end
|
|
compute_refs.flatten!
|
|
|
|
|
|
compute_refs.each do |ref|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>ComputeResource</type>
|
|
<pathSet>host</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="ComputeResource">#{ref}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
next if res.body.include? "<faultstring>"
|
|
dc_hosts << Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['ManagedObjectReference']
|
|
end
|
|
dc_hosts.flatten!
|
|
return dc_hosts
|
|
end
|
|
|
|
def vim_get_all_hosts
|
|
@dcs.each{|dc| @hosts << vim_get_hosts(dc['ref'])}
|
|
@hosts.flatten!
|
|
end
|
|
|
|
def vim_get_host_hw(host)
|
|
vim_setup_references
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>HostSystem</type>
|
|
<pathSet>hardware</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="HostSystem">#{host}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
return hash
|
|
end
|
|
|
|
def vim_get_all_host_summary(hw=false)
|
|
vim_setup_references
|
|
summaries = []
|
|
@hosts.each do |host|
|
|
details = {}
|
|
details[host] = vim_get_host_summary(host)
|
|
if details and hw
|
|
details.merge!(vim_get_host_hw(host))
|
|
end
|
|
summaries << details
|
|
end
|
|
return summaries.flatten.compact
|
|
end
|
|
|
|
def vim_get_vm_datastore(vm)
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>VirtualMachine</type>
|
|
<pathSet>datastore</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="VirtualMachine">#{vm}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body
|
|
></env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
datastore_ref = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['ManagedObjectReference']
|
|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Datastore</type>
|
|
<pathSet>info</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Datastore">#{datastore_ref}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
datastore_name = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['name']
|
|
datastore = { 'name' => datastore_name, 'ref' => datastore_ref}
|
|
return datastore
|
|
|
|
end
|
|
|
|
|
|
|
|
def vim_take_screenshot(vm, user, pass)
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<CreateScreenshot_Task xmlns="urn:vim25">
|
|
<_this type="VirtualMachine">#{vm['ref']}</_this>
|
|
</CreateScreenshot_Task>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
if res.body.include? "NotAuthenticatedFault"
|
|
return :expired
|
|
elsif res.body.include? "<faultstring>"
|
|
return :error
|
|
end
|
|
task_id = Hash.from_xml(res.body)['Envelope']['Body']['CreateScreenshot_TaskResponse']['returnval']
|
|
|
|
state= "running"
|
|
while state == "running"
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Task</type>
|
|
<pathSet>info</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Task">#{task_id}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
state = hash['state']
|
|
screenshot_file = hash['result']
|
|
end
|
|
unless screenshot_file
|
|
return :error
|
|
end
|
|
(ss_folder, ss_file) = screenshot_file.split('/').last(2)
|
|
ss_folder = Rex::Text.uri_encode(ss_folder)
|
|
ss_file = Rex::Text.uri_encode(ss_file)
|
|
ss_path = "#{ss_folder}/#{ss_file}"
|
|
datastore = vim_get_vm_datastore(vm['ref'])
|
|
user_pass = Rex::Text.encode_base64(user + ":" + pass)
|
|
ss_uri = "/folder/#{ss_path}?dcPath=#{vm['dc_name']}&dsName=#{datastore['name']}"
|
|
ss_uri =
|
|
res = send_request_cgi({
|
|
'uri' => ss_uri,
|
|
'method' => 'GET',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'headers' => {
|
|
'SOAPAction' => @soap_action,
|
|
'Authorization' => "Basic #{user_pass}",
|
|
}
|
|
}, 25)
|
|
if res.code == 200
|
|
return res.body
|
|
end
|
|
return :error
|
|
end
|
|
|
|
|
|
def vim_get_host_summary(host)
|
|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>HostSystem</type>
|
|
<pathSet>summary</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="HostSystem">#{host}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
hash['runtime'].delete('healthSystemRuntime')
|
|
hash.delete('xsi:type')
|
|
hash.delete('host')
|
|
return hash
|
|
end
|
|
|
|
def vim_get_vms
|
|
vim_setup_references
|
|
@vmrefs = []
|
|
vmlist= []
|
|
@dcs.each do |dc|
|
|
dc_vm_refs = vim_get_dc_vms(dc['ref'])
|
|
next if dc_vm_refs.nil? or dc_vm_refs.empty?
|
|
dc_vm_refs.flatten!
|
|
dc_vm_refs.compact!
|
|
next if dc_vm_refs.nil? or dc_vm_refs.empty?
|
|
print_status "#{datastore['RHOST']} - DataCenter: #{dc['name']} Found a Total of #{dc_vm_refs.length} VMs"
|
|
print_status "#{datastore['RHOST']} - DataCenter: #{dc['name']} Estimated Time: #{((dc_vm_refs.length * 7) /60)} Minutes"
|
|
dc_vm_refs.each do |ref|
|
|
print_status "#{datastore['RHOST']} - DataCenter: #{dc['name']} - Getting Data for VM: #{ref}..."
|
|
details = vim_get_vm_info(ref)
|
|
if details
|
|
details['ref'] = ref
|
|
details['dc_ref'] = dc['ref']
|
|
details['dc_name'] = dc['name']
|
|
vmlist << details
|
|
end
|
|
end
|
|
end
|
|
return vmlist
|
|
end
|
|
|
|
def vim_get_dc_vms(datacenter)
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Datacenter</type>
|
|
<pathSet>vmFolder</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Datacenter">#{datacenter}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
vmfolder = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>Folder</type>
|
|
<pathSet>childEntity</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="Folder">#{vmfolder}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
vm_index_array = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']['ManagedObjectReference']
|
|
vm_index_array.delete_if{|ref| ref.start_with? "group"} unless vm_index_array.nil? or vm_index_array.empty?
|
|
return vm_index_array
|
|
end
|
|
|
|
def vim_get_vm_info(vm_ref)
|
|
vim_setup_references
|
|
soap_req =
|
|
%Q|<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
<env:Body>
|
|
<RetrieveProperties xmlns="urn:vim25">
|
|
<_this type="PropertyCollector">#{@server_objects['propertyCollector']}</_this>
|
|
<specSet xsi:type="PropertyFilterSpec">
|
|
<propSet xsi:type="PropertySpec">
|
|
<type>VirtualMachine</type>
|
|
<pathSet>summary</pathSet>
|
|
</propSet>
|
|
<objectSet xsi:type="ObjectSpec">
|
|
<obj type="VirtualMachine">#{vm_ref}</obj>
|
|
</objectSet>
|
|
</specSet>
|
|
</RetrieveProperties>
|
|
</env:Body>
|
|
</env:Envelope>|
|
|
res = send_request_cgi({
|
|
'uri' => '/sdk',
|
|
'method' => 'POST',
|
|
'agent' => 'VMware VI Client',
|
|
'cookie' => @vim_cookie,
|
|
'data' => soap_req,
|
|
'headers' => { 'SOAPAction' => @soap_action}
|
|
}, 25)
|
|
if res.body.include? "<faultstring>"
|
|
return nil
|
|
end
|
|
hash = Hash.from_xml(res.body)['Envelope']['Body']['RetrievePropertiesResponse']['returnval']['propSet']['val']
|
|
vm = hash['config']
|
|
vm['runtime'] = hash['runtime']
|
|
vm['guest'] = hash['guest']
|
|
vm['quickStats'] = hash['quickStats']
|
|
return vm
|
|
end
|
|
|
|
def vim_logged_in?
|
|
return true if @vim_cookie
|
|
return false
|
|
end
|
|
|
|
def vim_instance_vars_set?
|
|
return false if @server_objects.nil? or @server_objects.empty?
|
|
return false if @host.nil? or @host.empty?
|
|
return true
|
|
end
|
|
|
|
def vim_setup_references
|
|
unless vim_instance_vars_set?
|
|
@dcs = []
|
|
@hosts = []
|
|
vim_get_dcs
|
|
vim_get_all_hosts
|
|
@hosts.flatten!
|
|
@hosts.compact!
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|