Git Product home page Git Product logo

cpp-veh-dbi's Introduction

cpp-veh-dbi

C++ Implementation of VEH based windows dynamic binary instrumentation

  • Copyright (c) 2019 YoungJoong Kim, cpp-veh-dbi is licensed under the MIT license.
  • This repository aims to write simple and fast programmable DBI tool.

Environments

Tested environments

  • Windows 10
  • Visual Studio 2019
  • CMake 3.14.0

Usage

To build library and samples,

.\script\setup
.\script\build

Then executables can be found in the bin directory.

ls bin

To run sample branch tracer for notepad,

.\bin\dllinjector C:\Windows\notepad.exe .\bin\branchtracer.dll

Then console will log the Windows API call.

+00007FF66FF9AC85,00007FFCEF55CD30,KERNEL32.DLL,GetStartupInfoW
+00007FF66FF9AD18,00007FFCF038FC10,ntdll.dll,RtlRetrieveNtUserPfn
+00007FF66FF9AD18,00007FFCF038FC10,ntdll.dll,RtlRetrieveNtUserPfn
+00007FF66FF9AD5A,00007FFCEEE6A6A0,msvcrt.dll,initterm
+00007FF66FF9AE4A,00007FFCEEE513B0,msvcrt.dll,ismbblead
+00007FF66FF9AE4A,00007FFCEEE513B0,msvcrt.dll,ismbblead

Structure

Struct VehDBI has three ways to instrument binary.

  1. Handler : Trigger on specified address.
  2. Tracer : Tracing instruction with debugging event.
  3. BTCallback : Callback on every instruction in text section.

1. Handler

Sample handler which triggered on entrypoint.

struct EntrypointHandler : Handler {
    void Handle(PCONTEXT context) override {
        std::ofstream("CONOUT$") << "trigged on entrypoint" << std::endl;
    }
};

// create dbi
VehDBI dbi;
// handler sample
size_t entrypoint = Utils::GetEntryPointAddress();
dbi.AddHandler(entrypoint, std::make_unique<EntrypointHandler>());

VehDBI::AddHandler get two arguments, address(size_t) and handler(std::unique_ptr<Handler>).

Handler require only one method, which will be invoked in specified address.

// Interface for debug event handler.
struct Handler {
    // Default virtual destructor.
    virtual ~Handler() = default;
    // Handle debug event.
    virtual void Handle(PCONTEXT context) = 0;
};

2. Tracer

Sample tracer which tracing branch instruction, branch_tracer.

Tracer require two methods.

  • HandleSingleStep: Handle single step exception.
  • HandleBreakpoint: Handle breakpoint exception.

If tracer set software bp, HandleBreakpoint should recover the opcode.

// Interface for code trace handler.
struct Tracer {
    // Default virtual destructor.
    virtual ~Tracer() = default;
    // Handle single step exception.
    virtual void HandleSingleStep(PCONTEXT context, Utils::SoftwareBP& bp) = 0;
    // Handle software breakpoint exception.
    virtual void HandleBreakpoint(PCONTEXT context, Utils::SoftwareBP& bp) = 0;
};

VehDBI::AddTracer get three arguments, tracer start point(size_t), end point(size_t) and tracer(std::unique_ptr<Tracer>).

  • If start point is 0, dbi automatically start tracer on entrypoint.
  • If end point is 0, tracer doesn't stop until process termination.
dbi.AddTracer(0, 0, std::make_unique<BranchTracer>());

3. BTCallback

Indeed, BTCallback is callback for branch tracer, which call BTCallback::run at every instruction.

VehDBI basically run branch tracer on text section. And VehDBI::AddBTCallback add given callback to the default branch tracer. Then added callback will be invoked on every instruction in text section.

Sample BTCallback which log branch instruction, logger.

// btcallback sample
auto logger = std::make_unique<Logger>("CONOUT$");
// tracer sample
dbi.AddTracer(0, 0, std::make_unique<BranchTracer>(std::move(logger)));

Which is same with VehDBI::AddBTCallback, if tracer (start, end) point is (0, 0).

dbi.AddBTCallback(std::make_unique<Logger>("CONOUT$"));

BTCallback require only one method, which will be invoked on every instruction.

// Callback for branch tracer.
struct BTCallback {
    // Default destructor.
    virtual ~BTCallback() = default;
    // Callback.
    virtual void run(BTInfo const& info, PCONTEXT context) = 0;
};

cpp-veh-dbi's People

Contributors

revsic avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

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.