def init(self): with open("run.json", "r") as file: run = json.load(file) for commands in run: vbox = VirtualBox() session = Session() vm = vbox.find_machine(VBOX_MACHINE_NAME) p = vm.launch_vm_process(session, 'gui', '') p.wait_for_completion(60 * 1000) session = vm.create_session() gs = session.console.guest.create_session(VBOX_USER_NAME, VBOX_PASSWORD, timeout_ms=300 * 1000) for command in commands: print("Command: %s" % (command['command'])) print("Parameters: %s" % (command['parameters'])) print("Sleep: %s" % (command['sleep'])) try: process, stdout, stderr = gs.execute( command['command'], command['parameters'], timeout_ms=30 * 1000) print(stdout) time.sleep(int(command['sleep'])) except: pass self._power_down(session) self._extract_victims( VICTIMS_FOLDER_IN, datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")) self._delete_victims(VICTIMS_FOLDER_IN)
def _power_down(self, session): vbox = VirtualBox() clone = vbox.find_machine(session.machine.name) try: p = session.console.power_down() p.wait_for_completion(60*1000) session.unlock_machine() session = clone.create_session() console = session.console p = console.restore_snapshot() p.wait_for_completion(60*1000) return clone finally: if session.state == SessionState.locked: session.unlock_machine()
def _power_down(self, session): vbox = VirtualBox() clone = vbox.find_machine(session.machine.name) try: p = session.console.power_down() p.wait_for_completion(60 * 1000) session.unlock_machine() session = clone.create_session() console = session.console p = console.restore_snapshot() p.wait_for_completion(60 * 1000) return clone finally: if session.state == SessionState.locked: session.unlock_machine()
def _power_down(self, session): vbox = VirtualBox() clone = vbox.find_machine(session.machine.name) try: p = session.console.power_down() p.wait_for_completion(60 * 1000) try: session.unlock_machine() except OleErrorUnexpected: # session seems to become unlocked automatically after # wait_for_completion is called after the power_down? pass session = clone.create_session() p = session.machine.restore_snapshot() p.wait_for_completion(60 * 1000) return clone finally: if session.state == SessionState.locked: session.unlock_machine()
def _lock(self, timeout_ms=-1): "Exclusive lock over root machine" vbox = VirtualBox() machine = vbox.find_machine(self.machine_name) wait_time = 0 while True: session = Session() try: machine.lock_machine(session, LockType.write) except Exception as exc: if timeout_ms != -1 and wait_time > timeout_ms: raise ValueError("Failed to acquire lock - %s" % exc) time.sleep(1) wait_time += 1000 else: try: yield session finally: session.unlock_machine() break