Skip to main content

A KVM client written in Python

Project description

PyKVM a small KVM client that demonstrates how to use the Kernel-based Virtual Machine (KVM) interface from Python. The client first initializes a barebones VM with a small amount of memory, and then loads and runs a raw binary. Since there is no guest OS, no devices, and no I/O available, all the binary can do is access CPU registers and memory.

This client is compatible with the KVM extensions for symbolic execution provided by S2E. In other words, you can symbolically execute programs with PyKVM.

Running with native KVM

First, build the sample binary. This binary writes 4KB of data at address 0x1000 and then halts the CPU.

git clone https://github.com/s2e/pykvm
cd pykvm
make -C sample

Then, run the compiled sample.bin binary in PyKVM. Make sure that /dev/kvm is accessible.

python -m pykvm.kvm sample/sample.bin

The output will show the state of the memory before and after executing the binary.

Symbolic execution

We will now swap native KVM for a version that implements symbolic execution.

First, you need to build S2E. Please refer to the S2E documentation for more details. In all the commands below, the S2EDIR variables points to the root of your S2E environment set up using s2e-env.

Second, recompile the sample binary with S2E support. This is necessary in order to write symbolic values to memory. At the moment, PyKVM cannot write symbolic data to memory directly, so it must be done from the running binary instead.

make -C sample clean
make -C sample EXTRA_CFLAGS="-DUSE_S2E -I$S2EDIR/source/s2e/guest/common/include"

export S2E_CONFIG=sample/s2e-config.lua

# libs2e.so uses LD_PRELOAD to intercept all calls to /dev/kvm in order to emulate
# native KVM while at the same time providing symbolic execution capabilities.
LD_PRELOAD=$S2EDIR/build/s2e/libs2e-release/x86_64-s2e-softmmu/libs2e.so python -m pykvm.kvm sample/sample.bin

When all the paths complete, libs2e automatically terminates the Python process. You can inspect s2e-last/debug.txt to see symbolic execution output. You will find several test cases corresponding to various execution paths of the sample binary. Please refer to the sample binary’s source code for more details about the expected results.

Projects

Here are some of the interesting things you could try and build on top of PyKVM.

  1. Run PyKVM in GDB together with libs2e. The gdb.ini script contains the required configuration for S2E. Do not forget to adapt the paths in there to your system.

    gdb --init-command gdb.ini --args python -m pykvm.kvm sample/sample.bin
  2. Write a small library that implements an s2e_make_symbolic syscall, so that binaries can get symbolic data easier. You can view this library as an OS, or better yet the BIOS.

  3. Extend PyKVM to load actual ELF/PE files into memory. Of course, they won’t have any imports or OS dependencies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykvm-1.0.6.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pykvm-1.0.6-py2-none-any.whl (11.8 kB view details)

Uploaded Python 2

File details

Details for the file pykvm-1.0.6.tar.gz.

File metadata

  • Download URL: pykvm-1.0.6.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/2.7.15rc1

File hashes

Hashes for pykvm-1.0.6.tar.gz
Algorithm Hash digest
SHA256 5e84fb8352ee59d82bd89262399f2e9c5ffb042f845b794e2833a7ef77e17099
MD5 8b5ca182833c2e191283d1f7e9a44476
BLAKE2b-256 586017139dd1a0de93e732fd2ef73daeb5dcecb09b4c2870dc15b5cfa9a43180

See more details on using hashes here.

File details

Details for the file pykvm-1.0.6-py2-none-any.whl.

File metadata

  • Download URL: pykvm-1.0.6-py2-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.0 CPython/2.7.15rc1

File hashes

Hashes for pykvm-1.0.6-py2-none-any.whl
Algorithm Hash digest
SHA256 81d77750f3cbcf6573c4a59052c5cae5b8dc0a007bac206c329c31ce3d666008
MD5 e9a6a63594cb1c6823ce10e923ac91b6
BLAKE2b-256 02ad52afda0c581e504ee3b4a11a4e369033ff89802499246a6c78f8e761a341

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page