Giter Club home page Giter Club logo

cs_driver's Introduction

cs_driver

cs_driver is a sample project for using Capstone from a driver in Visual Studio 2015. This project aims to provide a minimal, clean procedure to compile and link the Capstone disassembly framework in your Windows driver project. For more information about Capstone, see its project page.

Sample Output

sample

Motivation

Capstone is a reliable, supported disassemler developed by an active community. It is designed for multi-platform support in first place, and supports usage by OS kernel code. In fact, one of Capstone documents refers to a sample project for learning how to embed Capstone into an Windows kernel driver.

However, this project lacks information about how to actually configure your driver project and compile Capstone. Also, it refers to a two-year-old branch and cannot be compiled with Visual Studio 2015 with described instructions. Not only that, the repository consists of 50000+, 1.3GB of files in order to demonstrate make use of C++ Standard Template Library and Boost Libraries from drivers while those libraries are neither relevant to Capstone nor always demanded. For those reasons, the sample project is not overly helpful for those who want to learn how to use Capstone in Windows drivers.

cs_driver, on the other hand, explains how to configure your and Capstone project and includes only minimum amount of code with detailed comments for learning a procedure to apply Capstone to your project quickly. Also, cs_driver is able to run all existing Capstone test code so that a developer can confirm that Capstone on the kernel mode is properly functioning.

How to use Capstone from your WDK project

In general, what you need to embed Capstone to a driver are: capstone.lib complied from the modified source code (contents of modification are explained later), ntstrsafe.lib to resolve __fltused, and cs_driver.h and some runtime initialization and safe guard code explained in cs_driver.c. In order to make use of Capstone from a new driver project, follow the below steps.

  1. Add a new project "Kernel Mode Driver, Empty (KMDF)" to the cs_driver solution new_project

  2. Add a source file to the new project

    source_file

  3. Open a project properties of the cs_driver project and set Configuration to "All Configurations" and Platform to "All Platforms"

    • C/C++ > General > Additional Include Directories
      • $(SolutionDir)capstone\include
    • Linker > Input > Additional Dependencies
      • $(OutDir)..$(ConfigurationName)_WDK\capstone.lib;ntstrsafe.lib properties
  4. Set dependency as below from [Project] > [Project Dependencies] dependency

  5. Include cs_driver.h from the source file. It can be done by referencing existing one or creating a copy under the project

  6. In source code, call KeSaveFloatingPointState() before using any of Capstone APIs on a 32bit system, and also call cs_driver_init() in order to setup dynamic memory management of Capstone. For more details, refer to comments in cs_driver.c

After this, you are free to use Capstone API from a driver.

Those steps are just example and not a hard-rule. Developpers are also free to have separate solutions for Capstone and your driver as long as the driver can link capstone.lib and run equivalent code to what cs_driver.h provides.

How the cloned Capstone was modified for WDK projects

As of time cs_driver was created, source code of Capstone needs to be modified in order to compile, link and run all tests as part of a driver successfully. This sections explains what changes were made and why as a reference. Beware that you not need apply those changes when Capstone in this repository is used.

Added CAPSTONE_API to all Capstone APIs

This change is to specifie calling convention for Calstone APIs.

The default setting of calling convention is different between the capstone_static project and a WDK project. capstone_static compiles code with __cdecl calling converntion, while a WDK project compiles code as __stdcall, leading to link or runtime errors

Replacesd snprintf() with cs_snprintf()

This change is to avoid making use of snprintf(), which is not available for drivers.

This change could lead to a runtime issue when user-defined vsnprintf() does not return the same value as what genuine vsnprintf() does. In order to assess this impact, a developer is able to use the cs_driver_vsnprintf_test() function to test if their vsnprintf() conforms behaviour of that of the C/C++ standard.

Avoided compile errors with regard to string literals

This change is to avoid that strings comprise of PRI* macros is being threated as string literals and cause compile errors when compiled as C++11 and later. Details of this issue is explained under the "String literals followed by macros" section in the "Breaking Changes in Visual C++ 2015" page on MSDN.

This change was made because cs_driver_test.cpp attemped to compile all test code as C++ code for ease of excersising all regression test.

Added and made use of CS_OPT_NONE and CS_OPT_OFF

This change is to avoid compile errors with regard to conversion errors from integer to enum (cs_opt_type and cs_opt_value) when test_skipdata.c is compiled as C++ source as part of cs_driver_test.cpp.

Renamed a variable "i" to "ins" to avoid a warning

This change is to avoid compiler warning C4456 with regard to shadowed variables and required because warnings are treated as errors in a WDK project by default.

Added *_WDK configurations in the capstone_static project

This change is to add new build configurations for drivers.

First of all, the project file was upgraded for Visual Studio 2015. Then, *_WDK configurations were made from existing configurations and following changes were made to the *_WDK configurations:

  • C/C++ > General > Debug Information Format
    • OLD: Program Database for Edit And Continue (/ZI)
    • NEW: Program Database (/Zi)
  • C/C++ > Preprocessor > Preprocessor Definitions
    • NEW: Deleted CAPSTONE_USE_SYS_DYN_MEM
  • C/C++ > Code Generation > Basic Runtime Checks
    • OLD: Both (/RTC1, equiv. to /RTCsu) (/RTC1)
    • NEW: Default
  • C/C++ > Code Generation > Runtime Library
    • OLD: Multi-threaded Debug (/MTd)
    • NEW: (empty)
  • C/C++ > All Options > Additional Options
    • OLD: (empty)
    • NEW: /kernel

Replaced stdint.h with myinttypes.h

This change is to avoid compile errors due to make use of stdint.h, which is not available for drivers.

Added _KERNEL_MODE support

This change is to let myinttype.h and platform.h use the non-stanadard headers (stdint.h and stdbool.h), which are not available for drivers.

Note that _KERNEL_MODE is defined when a program is compiled with the /kernel option as explained in the "/kernel (Create Kernel Mode Binary)" page on MSDN.

Supported Platforms

  • x86 and x64 Windows 7, 8.1 and 10

License

This software is released under the MIT License, see LICENSE.

cs_driver's People

Contributors

tandasat avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.