Rizin
unix-like reverse engineering framework and cli tools
cs_driver.c
Go to the documentation of this file.
1 /* Capstone Driver */
2 /* By Satoshi Tanda <tanda.sat@gmail.com>, 2016-2019 */
3 
4 // Firstly, compile capstone_static_winkernel and
5 // generate capstone_static_winkernel.lib. It can be done by adding the
6 // capstone_static_winkernel project to your solution and compiling it first.
7 //
8 // Then, configure your driver project (cs_driver in this example) to locate to
9 // capstone.h and capstone_static_winkernel.lib. To do it, open project
10 // properties of the project and set Configuration to "All Configurations" and
11 // Platform to "All Platforms". Then, add the following entries:
12 // - C/C++ > General > Additional Include Directories
13 // - $(SolutionDir)capstone\include
14 // - C/C++ > Preprocessor > Preprocessor Definitions
15 // - _NO_CRT_STDIO_INLINE
16 // - Linker > Input > Additional Dependencies
17 // - $(OutDir)capstone_static_winkernel.lib
18 // - ntstrsafe.lib
19 //
20 // Note that ntstrsafe.lib is required to resolve __fltused indirectly used in
21 // Capstone.
22 
23 #include <ntddk.h>
24 #include <capstone/capstone.h>
25 
26 // 'conversion' : from function pointer 'type1' to data pointer 'type2'
27 #pragma warning(disable : 4054)
28 
29 
30 DRIVER_INITIALIZE DriverEntry;
31 static NTSTATUS cs_driver_hello();
32 
33 
34 // Driver entry point
35 EXTERN_C NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject,
36  PUNICODE_STRING RegistryPath) {
37  printf("Entering DriverEntry()\n");
38 
40 
41  printf("Leaving DriverEntry()\n");
42  return STATUS_CANCELLED;
43 }
44 
45 // Hello, Capstone!
47  csh handle;
48  cs_insn *insn;
49  size_t count;
50  KFLOATING_SAVE float_save;
52 
53  // Any of Capstone APIs cannot be called at IRQL higher than DISPATCH_LEVEL
54  // since our malloc implementation based on ExAllocatePoolWithTag() is not able
55  // to allocate memory at higher IRQL than the DISPATCH_LEVEL level.
56  NT_ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
57 
58  // On a 32bit driver, KeSaveFloatingPointState() is required before using any
59  // Capstone function because Capstone can access to the MMX/x87 registers and
60  // 32bit Windows requires drivers to use KeSaveFloatingPointState() before and
61  // KeRestoreFloatingPointState() after accessing them. See "Using Floating
62  // Point or MMX in a WDM Driver" on MSDN for more details.
63  status = KeSaveFloatingPointState(&float_save);
64  if (!NT_SUCCESS(status)) {
65  return status;
66  }
67 
68  // Do stuff just like user-mode. All functionalities are supported.
69  if (cs_open(CS_ARCH_X86, (sizeof(void *) == 4) ? CS_MODE_32 : CS_MODE_64,
70  &handle) != CS_ERR_OK) {
71  goto exit;
72  }
73 
75  (uint64_t)&cs_driver_hello, 0, &insn);
76  if (count > 0) {
77  printf("cs_driver!cs_driver_hello:\n");
78  for (size_t j = 0; j < count; j++) {
79  printf("0x%p\t%s\t\t%s\n", (void *)(uintptr_t)insn[j].address,
80  insn[j].mnemonic, insn[j].op_str);
81  }
82  cs_free(insn, count);
83  }
84  cs_close(&handle);
85 
86 exit:;
87  // Restores the nonvolatile floating-point context.
88  KeRestoreFloatingPointState(&float_save);
89  return status;
90 }
91 
92 // printf()
93 _Use_decl_annotations_ int __cdecl printf(const char * const _Format, ...) {
95  va_list args;
96 
97  va_start(args, _Format);
98  status = vDbgPrintEx(DPFLTR_DEFAULT_ID, DPFLTR_ERROR_LEVEL, _Format, args);
99  va_end(args);
100  return NT_SUCCESS(status);
101 }
static mcore_handle handle
Definition: asm_mcore.c:8
@ CS_ARCH_X86
X86 architecture (including x86 & x86-64)
Definition: capstone.h:78
@ CS_MODE_64
64-bit mode (X86, PPC)
Definition: capstone.h:107
@ CS_MODE_32
32-bit mode (X86)
Definition: capstone.h:106
size_t csh
Definition: capstone.h:71
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
DRIVER_INITIALIZE DriverEntry
Definition: cs_driver.c:30
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static NTSTATUS cs_driver_hello()
Definition: cs_driver.c:46
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
KeRestoreFloatingPointState
Definition: kernel.h:142
KeSaveFloatingPointState
Definition: kernel.h:145
KeGetCurrentIrql
Definition: kernel.h:106
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
int args
Definition: mipsasm.c:18
int CS_ERR_OK
Definition: __init__.py:235
unsigned long uint64_t
Definition: sftypes.h:28
unsigned char uint8_t
Definition: sftypes.h:31
_W64 unsigned int uintptr_t
LONG NTSTATUS
Definition: win.h:198
#define STATUS_UNSUCCESSFUL
Definition: winapi.h:680
#define NT_SUCCESS(status)
Definition: winapi.h:52
#define STATUS_CANCELLED
Definition: winapi.h:1824
mnemonic
Definition: z80asm.h:48