Giter VIP home page Giter VIP logo

memflow-qemu's Introduction

memflow

Crates.io build and test codecov MIT licensed Discord

machine introspection made easy

memflow is a library that enables introspection of various machines (hardware, virtual machines, memory dumps) in a generic fashion. There are 2 primary types of objects in memflow - Connectors and OS layers. Connector provides raw access to physical memory of a machine. Meanwhile, OS layer builds a higher level abstraction over running operating system, providing access to running processes, input events, etc. These objects are incredibly flexible as they can be chained together to gain access to a process running multiple levels of virtualization deep (see figure below).

+-----------+        +-----------+
| native OS |        | leechcore |
+-+---------+        +-+---------+
  |                    |
  |  +-----------+     |  +----------+
  +->|  QEMU VM  |     +->| Win32 OS |
     +-+---------+        +-+--------+
       |                    |
       |  +----------+      |  +-----------+
       +->| Win32 OS |      +->| lsass.exe |
          +-+--------+         +-----------+
            |
            |  +-----------+
            +->|  Hyper-V  |
               +-+---------+
                 |
                 |  +----------+
                 +->| Linux OS |
                    +-+--------+
                      |
                      |  +-----------+
                      +->| SSHD Proc |
                         +-----------+

(Example chains of access. For illustrative purposes only - Hyper-V Connector and Linux OS are not yet available)

As a library user, you do not have to worry about delicacies of chaining - everything is provided, batteries included. See one of our examples on how simple it is to build a chain (excluding parsing). All Connectors and OS layers are dynamically loadable with common interface binding them.

All of this flexibility is provided with very robust and efficient backend - memory interface is batchable and divisible, which gets taken advantage of by our throughput optimized virtual address translation pipeline that is able to walk the entire process virtual address space in under a second. Connectors and OS layers can be composed with the vast library of generic caching mechanisms, utility functions and data structures.

The memflow ecosystem is not bound to just Rust - Connector and OS layer functions are linked together using C ABI, thus users can write code that interfaces with them in other languages, such as C, C++, Zig, etc. In addition, these plugins can too be implemented in foreign languages - everything is open.

Overall, memflow is the most robust, efficient and flexible solution out there for machine introspection.

Getting started

Make sure that your rustc version is at least 1.74.0 or newer.

memflow uses a plugin based approach and is capable of loading different physical memory backends (so-called connectors) at runtime. On top of the physical memory backends memflow is also capable of loading plugins for interfacing with a specific target OS at runtime.

To get started, you want to at least install one connector. For that, use the memflowup utility (use dev channel).

Manual installation

The recommended installation locations for connectors on Linux are:

/usr/lib/memflow/libmemflow_xxx.so
$HOME/.local/lib/memflow/libmemflow_xxx.so

The recommended installation locations for connectors on Windows are:

[Username]/Documents/memflow/libmemflow_xxx.dll

Additionally, connectors can be placed in any directory of the environment PATH or the working directory of the program as well.

For more information about how to get started with memflow please head over to the YouTube series produced by h33p:

Running Examples

You can either run one of the examples with cargo run --release --example. Pass nothing to get a list of examples.

Some connectors like qemu will require elevated privileges. Refer to the readme of the connector for additional information on their required access rights.

To simplify running examples, tests, and benchmarks through different connectors, we added a simple cargo runner script for Linux to this repository. Simply set any of the following environment variables when running the cargo command to elevate privileges:

  • RUST_SUDO will start the resulting binary via sudo.
  • RUST_SETPTRACE will enable PTRACE permissions on the resulting binary before executing it.

Alternatively, you can run the benchmarks via cargo bench (can pass regex filters). Win32 benchmarks currently work only on Linux.

All examples support the memflow connector plugins inventory system. You will have to install at least one connector to use the examples. Refer to the getting started section for more details.

Run memflow/read_keys example with a qemu connector:

RUST_SETPTRACE=1 cargo run --example read_keys -- -vv -c qemu -a [vmname] -o win32

Run memflow/read_bench example with a coredump connector:

cargo run --example read_bench --release -- -vv -c coredump -a coredump_win10_64bit.raw -o win32

Note: In the examples above the qemu connector requires 'CAP_SYS_PTRACE=ep' permissions. The runner script in this repository will set the appropriate flags when the RUST_SETPTRACE environment variable is passed to it.

Documentation

Extensive code documentation can be found at docs.rs (it currently is relatively out of date).

An additional getting started guide as well as a higher level explanation of the inner workings of memflow can be found at memflow.github.io.

If you decide to build the latest documentation you can do it by issuing:

cargo doc --workspace --no-deps --open

Compilation support

memflow currently requires at least rustc version 1.74.0 or newer.

target build tests benches compiles on stable
linux x86_64 ✔️ ✔️ ✔️ ✔️
mac x86_64 ✔️ ✔️ ✔️ ✔️
win x86_64 ✔️ ✔️ ✔️ ✔️
linux aarch64 ✔️ ✔️ ✔️ ✔️
linux i686 ✔️ ✔️ ✔️ ✔️
linux armv7 ✔️ ✔️ ✔️ ✔️
no-std ✔️ ✔️ ✔️

Target support

By default, memflow supports analyzing 64-bit machines on any machine - be it 32 or 64 bit. Using memflow without default_features can disable 64-bit support on 32-bit machines for an efficiency gain, while enabling 128_bit_mem feature can be done for theoretical future 128-bit machine analysis. Note that all connectors and OS layers must be compiled with the same memory features enabled, and memflowup currently only compiles the default set of features.

memflow-win32 is tested on the latest Windows 11 and Windows 10 versions all the way down to Windows NT 4.0. If you found a version that does not work please submit an issue with the major/minor version as well as the build number.

Connectors

All examples provided in this repository are using the plugins inventory to dynamically load a connector at runtime. When using the library programmatically it is possible to just statically link a connector into the code.

Some connectors also require different permissions. Please refer to the individual connector repositories for more information.

These are the currently officially existing connectors:

In case you write your own connector please hit us up with a pull request so we can maintain a list of third-party connectors as well.

Build on memflow

Officialy supported projects:

Additional projects from the community:

Acknowledgements

  • CasualX for his wonderful pelite crate
  • ufrisk for his prior work on the subject and many inspirations

Contributing

Please check CONTRIBUTE.md

memflow-qemu's People

Contributors

h33p avatar ko1n avatar revity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

memflow-qemu's Issues

Resizable Bar breaks Memory Mappings

This issue was reported several times on the discord and needs further investigation with a GPU that supports it (nvidia 30 series onwards). If anyone is willing to help please contact us 👍

[QUESTION] How to use qmp?

Hello
I am exposing qmp on qemu using the example in the readme, but qemu can't find my socket.
I am running inside a docker container

Dockerfile:

---
version: '3'
services:
  readmem:
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Europe/Rome
    pull_policy: build
    build:
      context: .
      dockerfile: "./Dockerfile"
    volumes: 
      - /tmp/qmp-my-vm.sock:/tmp/qmp-my-vm.sock
    restart: "unless-stopped"
    container_name: readmem

As you can see, the socket is mapped on /tmp/qmp-my-vm.sock.

Now how to tell the connector where my socket is?

            const char *conn_name = argc > 1? argv[1]: "qemu";
            const char *conn_arg = argc > 2? argv[2]: "";
            const char *os_name = argc > 2? argv[2]: "win32";
            const char *os_arg = argc > 4? argv[4]: "";

            ConnectorInstance<> connector, *conn = conn_name[0] ? &connector : nullptr;
            OsInstance<> os;

            if (conn) {
                if (inventory_create_connector(inventory, conn_name, conn_arg, &connector)) {
                    printf("unable to initialize connector\n");
                    inventory_free(inventory);
                } else { other code ...}
           }

EDIT: I am on cpp, using official https://github.com/memflow/memflow-cmake-example/ .

Bug when hard-wiring connector

error[E0277]: the trait bound QemuProcfs: DerefMut is not satisfied
  --> src/main.rs:29:43
   |
29 |     let kernel_info = KernelInfo::scanner(&mut connector).scan().unwrap();
   |                                           ^^^^^^^^^^^^^^ the trait DerefMut is not implemented for QemuProcfs
[23:11]

Cargo.toml:

memflow = { git = "https://github.com/memflow/memflow.git", branch = "dev" }
memflow-win32 = { git = "https://[email protected]/memflow/memflow.git", branch = "dev" }
memflow-qemu-procfs = "0.1"

Parse memory regions from qapi if available

In case the QAPI (QMP) is available it would be great to automatically connect to it and figure out the appropiate memory regions instead of just "guessing" them.

To achieve this we should also provide a nice qmp/qapi crate first.

Metadata provides wrong size

Metadata does not provide the proper ram size but only the size of the entire buffer after remapping.

It can be easily reproduced via:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.provider :libvirt do |v|
    v.memory = 4096
    v.cpus = 2
    v.qemuargs :value => "-qmp"
    v.qemuargs :value => "unix:/tmp/vagrant-qmp.sock,server,nowait"
  end

  config.vm.box = "generic/ubuntu2010"
end

It fails with and without qmp to provide the proper metadata.

QMP could be enabled in the wrong cases

Since we now use memflow-native to do the underlying syscalls to read from qemu we need to check special cases for accessing QMP through localhost.

QMP should only be accessed if we use the access qemu on the current OS through memflow-native.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.