Ejemplo n.º 1
0
def modify(name, vm_visible):
    session = Session()

    image = session.query(Image).filter_by(name=name).first()
    if not image:
        log.error("Image not found: %s", name)
        exit(1)

    if image.mode != "normal":
        log.error("You can't modify this image as you have already made "
                  "snapshots with it!")
        log.error("Please vmcloak-clone it and modify the clone.")
        exit(1)

    m, h = initvm(image)

    m.start_vm(visible=vm_visible)
    wait_for_host(image.ipaddr, image.port)

    log.info("The Virtual Machine has booted and is ready to be modified!")
    log.info("When you shut it down, all changes will be saved.")

    m.wait_for_state(shutdown=True)

    m.remove_hd()
    m.compact_hd(image.path)
    m.delete_vm()
Ejemplo n.º 2
0
def test_win10x64():
    ip, port = "192.168.56.106", 13340

    name, snapshot = genname("win10x64"), genname("win10x64-snapshot")
    call(
        main.init,
        name,
        "--win10x64",
        "--ip",
        "192.168.56.7",
        "--port",
        port,
        "--tempdir",
        dirpath,
    )
    call(main.snapshot, name, snapshot, ip)

    m = vm.VirtualBox(snapshot)
    m.restore_snapshot()
    m.start_vm()

    misc.wait_for_host(ip, port)

    a = agent.Agent(ip, port)
    assert a.environ()["SYSTEMDRIVE"] == "C:"

    a.shutdown()
    m.wait_for_state(shutdown=True)

    m.delete_snapshot("vmcloak")
    m.remove_hd()
    m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 3
0
def test_winxp():
    ip, port = "192.168.56.103", 13337

    name, snapshot = genname("winxp"), genname("winxp-snapshot")
    call(
        main.init, name, "--winxp",
        "--ip", "192.168.56.3", "--port", port,
        "--tempdir", dirpath, "--serial-key", config["winxp"]["serialkey"]
    )
    call(main.snapshot, name, snapshot, ip)

    m = vm.VirtualBox(snapshot)
    m.restore_snapshot()
    m.start_vm()

    misc.wait_for_host(ip, port)

    # Very basic integrity checking of the VM.
    a = agent.Agent(ip, port)
    assert a.environ()["SYSTEMDRIVE"] == "C:"

    a.shutdown()
    m.wait_for_state(shutdown=True)

    m.delete_snapshot("vmcloak")
    m.remove_hd()
    m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 4
0
def modify(name, vm_visible):
    session = Session()

    image = session.query(Image).filter_by(name=name).first()
    if not image:
        log.error("Image not found: %s", name)
        exit(1)

    if image.mode != "normal":
        log.error("You can't modify this image as you have already made "
                  "snapshots with it!")
        log.error("Please vmcloak-clone it and modify the clone.")
        exit(1)

    m, h = initvm(image)

    m.start_vm(visible=vm_visible)
    wait_for_host(image.ipaddr, image.port)

    log.info("The Virtual Machine has booted and is ready to be modified!")
    log.info("When you shut it down, all changes will be saved.")

    m.wait_for_state(shutdown=True)

    m.remove_hd()
    m.compact_hd(image.path)
    m.delete_vm()
Ejemplo n.º 5
0
def test_winxp():
    ip, port = "192.168.56.103", 13337

    name, snapshot = genname("winxp"), genname("winxp-snapshot")
    call(main.init, name, "--winxp", "--ip", "192.168.56.3", "--port", port,
         "--tempdir", dirpath, "--serial-key", config["winxp"]["serialkey"])
    call(main.snapshot, name, snapshot, ip)

    m = vm.VirtualBox(snapshot)
    m.restore_snapshot()
    m.start_vm()

    misc.wait_for_host(ip, port)

    # Very basic integrity checking of the VM.
    a = agent.Agent(ip, port)
    assert a.environ()["SYSTEMDRIVE"] == "C:"

    a.shutdown()
    m.wait_for_state(shutdown=True)

    m.delete_snapshot("vmcloak")
    m.remove_hd()
    m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 6
0
def do_snapshot(image, vmname, ipaddr, resolution, ramsize, cpus,
                hostname, adapter, vm_visible):
    m, h = initvm(image, name=vmname, multi=True, ramsize=ramsize, cpus=cpus)

    m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)
    a = Agent(image.ipaddr, image.port)
    a.ping()

    # Assign a new hostname.
    a.hostname(hostname)
    a.reboot()
    a.kill()

    # Wait for the reboot to kick in.
    time.sleep(10)
    wait_for_host(image.ipaddr, image.port)
    a.ping()

    if resolution:
        width, height = resolution.split("x")
        a.resolution(width, height)

    a.remove("C:\\vmcloak")
    a.static_ip(ipaddr, image.netmask, image.gateway, h.interface)

    m.snapshot("vmcloak", "Snapshot created by VM Cloak.")
    m.stopvm()

    # Create a database entry for this snapshot.
    snapshot = Snapshot(image_id=image.id, vmname=vmname, ipaddr=ipaddr,
                        port=image.port, hostname=hostname)
    return snapshot
Ejemplo n.º 7
0
def do_snapshot(image, vmname, ipaddr, resolution, ramsize, cpus,
                hostname, adapter, vm_visible):
    m, h = initvm(image, name=vmname, multi=True, ramsize=ramsize, cpus=cpus)

    m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)
    a = Agent(image.ipaddr, image.port)
    a.ping()

    # Assign a new hostname.
    a.hostname(hostname)
    a.reboot()
    a.kill()

    # Wait for the reboot to kick in.
    time.sleep(10)
    wait_for_host(image.ipaddr, image.port)
    a.ping()

    if resolution:
        width, height = resolution.split("x")
        a.resolution(width, height)

    a.remove("C:\\vmcloak")
    a.static_ip(ipaddr, image.netmask, image.gateway, h.interface)

    m.snapshot("vmcloak", "Snapshot created by VM Cloak.")
    m.stopvm()

    # Create a database entry for this snapshot.
    snapshot = Snapshot(image_id=image.id, vmname=vmname, ipaddr=ipaddr,
                        port=image.port, hostname=hostname)
    return snapshot
Ejemplo n.º 8
0
def test_win10x64():
    ip, port = "192.168.56.106", 13340

    name, snapshot = genname("win10x64"), genname("win10x64-snapshot")
    call(
        main.init, name, "--win10x64",
        "--ip", "192.168.56.7", "--port", port,
        "--tempdir", dirpath,
    )
    call(main.snapshot, name, snapshot, ip)

    m = vm.VirtualBox(snapshot)
    m.restore_snapshot()
    m.start_vm()

    misc.wait_for_host(ip, port)

    a = agent.Agent(ip, port)
    assert a.environ()["SYSTEMDRIVE"] == "C:"

    a.shutdown()
    m.wait_for_state(shutdown=True)

    m.delete_snapshot("vmcloak")
    m.remove_hd()
    m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 9
0
def do_snapshot(image, vmname, ipaddr, resolution, ramsize, cpus, hostname,
                adapter, vm_visible, vrde, vrde_port, interactive):
    m, h = initvm(image, name=vmname, multi=True, ramsize=ramsize, cpus=cpus)

    if vrde:
        m.vrde(port=vrde_port)

    m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)
    a = Agent(image.ipaddr, image.port)
    a.ping()

    # Assign a new hostname.
    a.hostname(hostname)
    a.reboot()
    a.kill()

    # Wait for the reboot to kick in.
    time.sleep(10)
    wait_for_host(image.ipaddr, image.port)
    a.ping()

    if resolution:
        width, height = resolution.split("x")
        a.resolution(width, height)

    if interactive:
        a.upload(
            "C:\\vmcloak\\interactive.txt",
            "Please make your final changes to this VM. When you're"
            "done, close this window and we'll create a snapshot.")

        log.info("You've started the snapshot creation in interactive mode!")
        log.info("Please make your last changes to the VM.")
        log.info(
            "When you're done close the spawned notepad process in the VM to take the final snapshot."
        )
        a.execute("notepad.exe C:\\vmcloak\\interactive.txt", async=False)

    a.remove("C:\\vmcloak")
    a.static_ip(ipaddr, image.netmask, image.gateway, h.interface)
    m.snapshot("vmcloak", "Snapshot created by VMCloak.")
    m.stopvm()

    # Create a database entry for this snapshot.
    snapshot = Snapshot(image_id=image.id,
                        vmname=vmname,
                        ipaddr=ipaddr,
                        port=image.port,
                        hostname=hostname)
    return snapshot
Ejemplo n.º 10
0
    def static_ip(self, ipaddr, netmask, gateway):
        """Change the IP address of this machine."""
        command = \
            "netsh interface ip set address " \
            "name=\"Local Area Connection\" static " \
            "%s %s %s 1" % (ipaddr, netmask, gateway)
        try:
            requests.post("http://%s:%s/execute" % (self.ipaddr, self.port),
                          data={"command": command}, timeout=5)
        except requests.exceptions.ReadTimeout:
            pass

        # Now wait until the Agent is reachable on the new IP address.
        wait_for_host(ipaddr, self.port)
        self.ipaddr = ipaddr
Ejemplo n.º 11
0
    def static_ip(self, ipaddr, netmask, gateway, interface):
        """Change the IP address of this machine."""
        command = (
            "netsh interface ip set address name=\"%s\" static %s %s %s 1"
        ) % (interface, ipaddr, netmask, gateway)

        try:
            requests.post("http://%s:%s/execute" % (self.ipaddr, self.port),
                          data={"command": command}, timeout=5)
        except requests.exceptions.ReadTimeout:
            pass

        # Now wait until the Agent is reachable on the new IP address.
        wait_for_host(ipaddr, self.port)
        self.ipaddr = ipaddr
Ejemplo n.º 12
0
def snapshot(name, vmname, ipaddr, resolution, ramsize, cpus, hostname,
             adapter, vm_visible):
    session = Session()

    image = session.query(Image).filter_by(name=name).first()
    if not image:
        log.error("Image not found: %s", name)
        exit(1)

    # From now on this image is multiattach.
    image.mode = "multiattach"
    session.commit()

    m, h = initvm(image, name=vmname)

    m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)
    a = Agent(image.ipaddr, image.port)
    a.ping()

    # Assign a new hostname.
    a.hostname(hostname)
    a.reboot()
    a.kill()

    # Wait for the reboot to kick in.
    time.sleep(10)
    wait_for_host(image.ipaddr, image.port)
    a.ping()

    if resolution:
        width, height = resolution.split("x")
        a.resolution(width, height)

    a.remove("C:\\vmcloak")
    a.static_ip(ipaddr, image.netmask, image.gateway, h.interface)

    m.snapshot("vmcloak", "Snapshot created by VM Cloak.")
    m.stopvm()

    # Create a database entry for this snapshot.
    snapshot = Snapshot(image_id=image.id, vmname=vmname, ipaddr=ipaddr,
                        port=image.port, hostname=hostname)
    session.add(snapshot)
    session.commit()
Ejemplo n.º 13
0
def do_snapshot(image, vmname, ipaddr, resolution, ramsize, cpus,
                hostname, adapter, vm_visible, vrde, vrde_port, interactive):
    m, h = initvm(image, name=vmname, multi=True, ramsize=ramsize, cpus=cpus)

    if vrde:
        m.vrde(port=vrde_port)

    m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)
    a = Agent(image.ipaddr, image.port)
    a.ping()

    # Assign a new hostname.
    a.hostname(hostname)
    a.reboot()
    a.kill()

    # Wait for the reboot to kick in.
    time.sleep(10)
    wait_for_host(image.ipaddr, image.port)
    a.ping()

    if resolution:
        width, height = resolution.split("x")
        a.resolution(width, height)

    if interactive:
        a.upload("C:\\vmcloak\\interactive.txt",
                 "Please make your final changes to this VM. When you're"
                 "done, close this window and we'll create a snapshot.")

        log.info("You've started the snapshot creation in interactive mode!")
        log.info("Please make your last changes to the VM.")
        log.info("When you're done close the spawned notepad process in the VM to take the final snapshot.")
        a.execute("notepad.exe C:\\vmcloak\\interactive.txt", async=False)

    a.remove("C:\\vmcloak")
    a.static_ip(ipaddr, image.netmask, image.gateway, h.interface)
    m.snapshot("vmcloak", "Snapshot created by VMCloak.")
    m.stopvm()

    # Create a database entry for this snapshot.
    snapshot = Snapshot(image_id=image.id, vmname=vmname, ipaddr=ipaddr,
                        port=image.port, hostname=hostname)
    return snapshot
Ejemplo n.º 14
0
    def static_ip(self, ipaddr, netmask, gateway, interface):
        """Change the IP address of this machine."""
        command = (
            "netsh interface ip set address name=\"%s\" static %s %s %s 1"
        ) % (interface, ipaddr, netmask, gateway)

        try:
            session = requests.Session()
            session.trust_env = False
            session.proxies = None
            session.post(
                "http://%s:%s/execute" % (self.ipaddr, self.port),
                data={"command": command}, timeout=5
            )
        except requests.exceptions.ReadTimeout:
            pass

        # Now wait until the Agent is reachable on the new IP address.
        wait_for_host(ipaddr, self.port)
        self.ipaddr = ipaddr
Ejemplo n.º 15
0
    def static_ip(self, ipaddr, netmask, gateway, interface):
        """Change the IP address of this machine."""
        if self.system == 'linux':
            command = (
                "IFACE=`ip route ls | grep '^default' | cut -f 5 -d ' '`; export IFACE; ifconfig $IFACE %s netmask %s; route add default gw %s"
            ) % (ipaddr, netmask, gateway)
        else:
            command = (
                "netsh interface ip set address name=\"%s\" static %s %s %s 1"
            ) % (interface, ipaddr, netmask, gateway)

        try:
            requests.post("http://%s:%s/execute" % (self.ipaddr, self.port),
                          data={"command": command, "shell": 'true'}, timeout=5)
        except requests.exceptions.ReadTimeout:
            pass

        # Now wait until the Agent is reachable on the new IP address.
        wait_for_host(ipaddr, self.port)
        self.ipaddr = ipaddr
Ejemplo n.º 16
0
def test_winxp_many():
    ip, port, count = "192.168.56.201", 13400, 10

    name, snapshot = genname("winxp"), genname("winxp-snapshot")
    call(
        main.init, name, "--winxp",
        "--ip", "192.168.56.4", "--port", port,
        "--tempdir", dirpath, "--serial-key", config["winxp"]["serialkey"]
    )
    call(main.snapshot, name, snapshot, ip, "--count", count)

    snapshots = []
    for x in range(count):
        snapshots.append([
            "%s%d" % (snapshot, x + 1),
            ip, port,
        ])

        ip = misc.ipaddr_increase(ip)

    # We have to remove the VMs in reverse because of VirtualBox dependencies.
    for snapshot, ip, port in snapshots[::-1]:
        m = vm.VirtualBox(snapshot)
        m.restore_snapshot()
        m.start_vm()

        misc.wait_for_host(ip, port)

        # Very basic integrity checking of the VM.
        a = agent.Agent(ip, port)
        assert a.environ()["SYSTEMDRIVE"] == "C:"

        a.shutdown()
        m.wait_for_state(shutdown=True)

        m.delete_snapshot("vmcloak")
        m.remove_hd()
        m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 17
0
def test_winxp_many():
    ip, port, count = "192.168.56.201", 13400, 10

    name, snapshot = genname("winxp"), genname("winxp-snapshot")
    call(main.init, name, "--winxp", "--ip", "192.168.56.4", "--port", port,
         "--tempdir", dirpath, "--serial-key", config["winxp"]["serialkey"])
    call(main.snapshot, name, snapshot, ip, "--count", count)

    snapshots = []
    for x in range(count):
        snapshots.append([
            "%s%d" % (snapshot, x + 1),
            ip,
            port,
        ])

        ip = misc.ipaddr_increase(ip)

    # We have to remove the VMs in reverse because of VirtualBox dependencies.
    for snapshot, ip, port in snapshots[::-1]:
        m = vm.VirtualBox(snapshot)
        m.restore_snapshot()
        m.start_vm()

        misc.wait_for_host(ip, port)

        # Very basic integrity checking of the VM.
        a = agent.Agent(ip, port)
        assert a.environ()["SYSTEMDRIVE"] == "C:"

        a.shutdown()
        m.wait_for_state(shutdown=True)

        m.delete_snapshot("vmcloak")
        m.remove_hd()
        m.delete_vm()

    image = session.query(Image).filter_by(name=name).first()
    os.remove(image.path)
Ejemplo n.º 18
0
    def static_ip(self, ipaddr, netmask, gateway, interface):
        """Change the IP address of this machine."""
        if self.system == 'linux':
            command = (
                "IFACE=`ip route ls | grep '^default' | cut -f 5 -d ' '`; export IFACE; ifconfig $IFACE %s netmask %s; route add default gw %s"
            ) % (ipaddr, netmask, gateway)
        else:
            command = (
                "netsh interface ip set address name=\"%s\" static %s %s %s 1"
            ) % (interface, ipaddr, netmask, gateway)

        try:
            requests.post("http://%s:%s/execute" % (self.ipaddr, self.port),
                          data={
                              "command": command,
                              "shell": 'true'
                          },
                          timeout=5)
        except requests.exceptions.ReadTimeout:
            pass

        # Now wait until the Agent is reachable on the new IP address.
        wait_for_host(ipaddr, self.port)
        self.ipaddr = ipaddr
Ejemplo n.º 19
0
def install(name, dependencies, vm_visible, recommended, debug):
    if debug:
        log.setLevel(logging.DEBUG)

    session = Session()

    image = session.query(Image).filter_by(name=name).first()
    if not image:
        log.error("Image not found: %s", name)
        exit(1)

    if image.mode != "normal":
        log.error("You can't install dependencies in this image as you have "
                  "already made snapshots with it!")
        log.error("Please vmcloak-clone it and update the clone.")
        exit(1)

    m, h = initvm(image)

    if image.vm == "virtualbox":
        m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)

    a = Agent(image.ipaddr, image.port)
    a.ping()

    settings = {}
    deps = []

    # Include all recommended dependencies if requested.
    for dependency in vmcloak.dependencies.plugins:
        if recommended and dependency.recommended:
            deps.append((dependency.name, dependency.default))

    # Fetch the configuration settings off of the arguments.
    for dependency in dependencies:
        if "." in dependency and "=" in dependency:
            key, value = dependency.split("=", 1)
            settings[key.strip()] = value.strip()
        elif ":" in dependency:
            dependency, version = dependency.split(":", 1)
            deps.append((dependency, version))
        else:
            deps.append((dependency, None))

    for dependency, version in deps:
        if dependency not in vmcloak.dependencies.names:
            log.error("Unknown dependency %s..", dependency)
            break

        if version:
            log.info("Installing dependency %s %s..", dependency, version)
        else:
            log.info("Installing dependency %s..", dependency)

        try:
            # TODO Recursive install function.
            d = vmcloak.dependencies.names[dependency]

            # Check if there are any "childs" for dependencies.
            if d.depends:
                depends = d.depends
                if isinstance(depends, basestring):
                    depends = [depends]

                for depend in depends:
                    if ":" in depend:
                        depend, dversion = depend.split(":", 1)
                    else:
                        dversion = None

                    if dversion:
                        log.info("Installing child dependency %s %s..", depend,
                                 dversion)
                    else:
                        log.info("Installing child dependency %s..", depend)

                    # Install dependency child before dependency itself.
                    dd = vmcloak.dependencies.names[depend]
                    dd(h, m, a, image, dversion, settings).run()

                # Reboot the VM as we expect most dependencies to be related
                # to KB installs.
                a.reboot()
                time.sleep(10)
                wait_for_host(image.ipaddr, image.port)

            d(h, m, a, image, version, settings).run()
        except DependencyError:
            log.error("The dependency %s returned an error..", dependency)
            break

    if image.vm == "virtualbox":
        a.shutdown()
        m.wait_for_state(shutdown=True)

        m.remove_hd()
        m.compact_hd(image.path)
        m.delete_vm()
    else:
        a.reboot()
Ejemplo n.º 20
0
def install(name, dependencies, vm_visible, recommended, debug):
    if debug:
        log.setLevel(logging.DEBUG)

    session = Session()

    image = session.query(Image).filter_by(name=name).first()
    if not image:
        log.error("Image not found: %s", name)
        exit(1)

    if image.mode != "normal":
        log.error("You can't install dependencies in this image as you have "
                  "already made snapshots with it!")
        log.error("Please vmcloak-clone it and update the clone.")
        exit(1)

    m, h = initvm(image)

    if image.vm == "virtualbox":
        m.start_vm(visible=vm_visible)

    wait_for_host(image.ipaddr, image.port)

    a = Agent(image.ipaddr, image.port)
    a.ping()

    settings = {}
    deps = []

    # Include all recommended dependencies if requested.
    for dependency in vmcloak.dependencies.plugins:
        if recommended and dependency.recommended:
            deps.append((dependency.name, dependency.default))

    # Fetch the configuration settings off of the arguments.
    for dependency in dependencies:
        if "." in dependency and "=" in dependency:
            key, value = dependency.split("=", 1)
            settings[key.strip()] = value.strip()
        elif ":" in dependency:
            dependency, version = dependency.split(":", 1)
            deps.append((dependency, version))
        else:
            deps.append((dependency, None))

    for dependency, version in deps:
        if dependency not in vmcloak.dependencies.names:
            log.error("Unknown dependency %s..", dependency)
            break

        if version:
            log.info("Installing dependency %s %s..", dependency, version)
        else:
            log.info("Installing dependency %s..", dependency)

        try:
            # TODO Recursive install function.
            d = vmcloak.dependencies.names[dependency]

            # Check if there are any "childs" for dependencies.
            if d.depends:
                depends = d.depends
                if isinstance(depends, basestring):
                    depends = [depends]

                for depend in depends:
                    if ":" in depend:
                        depend, dversion = depend.split(":", 1)
                    else:
                        dversion = None

                    if dversion:
                        log.info("Installing child dependency %s %s..", depend, dversion)
                    else:
                        log.info("Installing child dependency %s..", depend)

                    # Install dependency child before dependency itself.
                    dd = vmcloak.dependencies.names[depend]
                    dd(h, m, a, image, dversion, settings).run()

                # Reboot the VM as we expect most dependencies to be related
                # to KB installs.
                a.reboot()
                time.sleep(10)
                wait_for_host(image.ipaddr, image.port)

            d(h, m, a, image, version, settings).run()
        except DependencyError:
            log.error("The dependency %s returned an error..", dependency)
            break

    if image.vm == "virtualbox":
        a.shutdown()
        m.wait_for_state(shutdown=True)

        m.remove_hd()
        m.compact_hd(image.path)
        m.delete_vm()
    else:
        a.reboot()