Rizin
unix-like reverse engineering framework and cli tools
io_rzk_windows.c File Reference
#include "io_rzk_windows.h"

Go to the source code of this file.

Functions

static BOOL InstallService (const char *rutaDriver, LPCTSTR lpServiceName, LPCTSTR lpDisplayName)
 
static BOOL RemoveService (LPCTSTR lpServiceName)
 
BOOL StartStopService (LPCTSTR lpServiceName, BOOL bStop)
 
static BOOL InitDriver (VOID)
 
static const char * GetFileName (const char *path)
 
int GetSystemModules (RzIO *io)
 
int ReadKernelMemory (ut64 address, ut8 *buf, int len)
 
int WriteKernelMemory (ut64 address, const ut8 *buf, int len)
 
int Init (const char *driverPath)
 

Variables

HANDLE gHandleDriver = NULL
 

Function Documentation

◆ GetFileName()

static const char* GetFileName ( const char *  path)
static

Definition at line 83 of file io_rzk_windows.c.

83  {
84  const char *pfile = path + strlen(path);
85  for (; pfile > path; pfile--) {
86  if ((*pfile == '\\') || (*pfile == '/')) {
87  pfile++;
88  break;
89  }
90  }
91  return pfile;
92 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35

References path.

Referenced by GetSystemModules().

◆ GetSystemModules()

int GetSystemModules ( RzIO io)

Definition at line 94 of file io_rzk_windows.c.

94  {
95  DWORD bRead = 0;
96  int i;
97  LPVOID lpBufMods = NULL;
98  int bufmodsize = 1024 * 1024;
99  if (gHandleDriver) {
100  if (!(lpBufMods = malloc(bufmodsize))) {
101  eprintf("[rzk] GetSystemModules: Error can't allocate %i bytes of memory.\n", bufmodsize);
102  return -1;
103  }
104  if (DeviceIoControl(gHandleDriver, IOCTL_GET_SYSTEM_MODULES, lpBufMods, bufmodsize, lpBufMods, bufmodsize, &bRead, NULL)) {
107  for (i = 0; i < pm->NumberOfModules; i++) {
108  const char *fileName = GetFileName((const char *)pMod[i].FullPathName);
109  io->cb_printf("f nt.%s 0x%x @ 0x%p\n", fileName, pMod[i].ImageSize, pMod[i].ImageBase);
110  }
111  }
112  } else {
113  eprintf("Driver not initialized.\n");
114  }
115  return 1;
116 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
HANDLE gHandleDriver
Definition: io_rzk_windows.c:8
static const char * GetFileName(const char *path)
struct _RTL_PROCESS_MODULES * PRTL_PROCESS_MODULES
#define IOCTL_GET_SYSTEM_MODULES
void * malloc(size_t size)
Definition: malloc.c:123
#define eprintf(x, y...)
Definition: rlcc.c:7
RTL_PROCESS_MODULE_INFORMATION Modules[1]
PrintfCallback cb_printf
Definition: rz_io.h:91
DWORD

References rz_io_t::cb_printf, DWORD, eprintf, test-lz4-speed::fileName, GetFileName(), gHandleDriver, i, IOCTL_GET_SYSTEM_MODULES, malloc(), _RTL_PROCESS_MODULES::Modules, NULL, and _RTL_PROCESS_MODULES::NumberOfModules.

Referenced by rzk__system().

◆ Init()

int Init ( const char *  driverPath)

Definition at line 175 of file io_rzk_windows.c.

175  {
176  BOOL ret = FALSE;
177  if (InitDriver() == FALSE) {
178  if (strlen(driverPath)) {
179  StartStopService(TEXT("rzk"), TRUE);
180  RemoveService(TEXT("rzk"));
181  eprintf("Installing driver: %s\n", driverPath);
182  if (InstallService(driverPath, TEXT("rzk"), TEXT("rzk"))) {
183  StartStopService(TEXT("rzk"), FALSE);
184  ret = InitDriver();
185  }
186  } else {
187  eprintf("Error initalizating driver, try rzk://pathtodriver\nEx: rizin.exe rzk://c:\\rzk.sys");
188  }
189  } else {
190  eprintf("Driver present [OK]\n");
191  ret = TRUE;
192  }
193  return ret;
194 }
static BOOL InstallService(const char *rutaDriver, LPCTSTR lpServiceName, LPCTSTR lpDisplayName)
static BOOL InitDriver(VOID)
static BOOL RemoveService(LPCTSTR lpServiceName)
BOOL StartStopService(LPCTSTR lpServiceName, BOOL bStop)
#define TRUE
Definition: mybfd.h:103
#define FALSE
Definition: mybfd.h:102

References eprintf, FALSE, InitDriver(), InstallService(), RemoveService(), StartStopService(), and TRUE.

Referenced by rzk__open().

◆ InitDriver()

static BOOL InitDriver ( VOID  )
static

Definition at line 75 of file io_rzk_windows.c.

75  {
76  const int genericFlags = GENERIC_READ | GENERIC_WRITE;
77  const int shareFlags = FILE_SHARE_READ | FILE_SHARE_WRITE;
78  gHandleDriver = CreateFile(TEXT(RZK_DEVICE), genericFlags, shareFlags,
79  NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_DIRECTORY, 0);
81 }
#define RZK_DEVICE
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21

References gHandleDriver, INVALID_HANDLE_VALUE, NULL, and RZK_DEVICE.

Referenced by Init().

◆ InstallService()

static BOOL InstallService ( const char *  rutaDriver,
LPCTSTR  lpServiceName,
LPCTSTR  lpDisplayName 
)
static

Definition at line 10 of file io_rzk_windows.c.

10  {
11  HANDLE hService;
12  BOOL ret = FALSE;
13  HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
14  if (hSCManager) {
15  LPTSTR rutaDriver_ = rz_sys_conv_utf8_to_win(rutaDriver);
16  hService = CreateService(hSCManager, lpServiceName, lpDisplayName, SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, rutaDriver_, NULL, NULL, NULL, NULL, NULL);
17  if (hService) {
18  CloseServiceHandle(hService);
19  ret = TRUE;
20  }
21  free(rutaDriver_);
22  CloseServiceHandle(hSCManager);
23  }
24  return ret;
25 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
DWORD * HANDLE

References FALSE, free(), HANDLE, NULL, and TRUE.

Referenced by Init().

◆ ReadKernelMemory()

int ReadKernelMemory ( ut64  address,
ut8 buf,
int  len 
)

Definition at line 118 of file io_rzk_windows.c.

118  {
119  DWORD ret = -1, bRead = 0;
120  LPVOID lpBuffer = NULL;
121  int bufsize;
122  PPA p;
123  memset(buf, '\xff', len);
124  if (gHandleDriver) {
125  bufsize = sizeof(PA) + len;
126  if (!(lpBuffer = malloc(bufsize))) {
127  eprintf("[rzk] ReadKernelMemory: Error can't allocate %i bytes of memory.\n", bufsize);
128  return -1;
129  }
130  p = (PPA)lpBuffer;
131  p->address.QuadPart = address;
132  p->len = len;
133  if (DeviceIoControl(gHandleDriver, IOCTL_READ_KERNEL_MEM, lpBuffer, bufsize, lpBuffer, bufsize, &bRead, NULL)) {
134  memcpy(buf, lpBuffer, len);
135  ret = len;
136  } else {
137  ret = -1;
138  // eprintf("[rzk] ReadKernelMemory: Error IOCTL_READ_KERNEL_MEM.\n");
139  }
140  free(lpBuffer);
141  } else {
142  eprintf("Driver not initialized.\n");
143  }
144  return ret;
145 }
size_t len
Definition: 6502dis.c:15
struct _PPA PA
struct _PPA * PPA
#define IOCTL_READ_KERNEL_MEM
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References DWORD, eprintf, free(), gHandleDriver, IOCTL_READ_KERNEL_MEM, len, malloc(), memcpy(), memset(), NULL, and p.

Referenced by rzk__read().

◆ RemoveService()

static BOOL RemoveService ( LPCTSTR  lpServiceName)
static

Definition at line 27 of file io_rzk_windows.c.

27  {
28  HANDLE hService;
29  BOOL ret = FALSE;
30  HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
31  if (hSCManager) {
32  hService = OpenService(hSCManager, lpServiceName, SERVICE_START | DELETE | SERVICE_STOP);
33  if (hService) {
34  DeleteService(hService);
35  CloseServiceHandle(hService);
36  ret = TRUE;
37  }
38  CloseServiceHandle(hSCManager);
39  }
40  return ret;
41 }

References FALSE, HANDLE, NULL, and TRUE.

Referenced by Init().

◆ StartStopService()

BOOL StartStopService ( LPCTSTR  lpServiceName,
BOOL  bStop 
)

Definition at line 43 of file io_rzk_windows.c.

43  {
44  HANDLE hSCManager;
45  HANDLE hService;
46  SERVICE_STATUS ssStatus;
47  BOOL ret = FALSE;
48  hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
49  if (hSCManager) {
50  hService = OpenService(hSCManager, lpServiceName, SERVICE_START | DELETE | SERVICE_STOP);
51  if (hService) {
52  if (!bStop) {
53  if (StartService(hService, 0, NULL)) {
54  eprintf("Service started [OK]\n");
55  ret = TRUE;
56  } else {
57  eprintf("Service started [FAIL]\n");
58  }
59  } else {
60  if (ControlService(hService, SERVICE_CONTROL_STOP, &ssStatus)) {
61  eprintf("Service Stopped [OK]\n");
62  ret = TRUE;
63  } else {
64  eprintf("Service Stopped [FAIL]\n");
65  }
66  }
67  CloseServiceHandle(hService);
68  DeleteService(hService);
69  }
70  CloseServiceHandle(hSCManager);
71  }
72  return ret;
73 }

References eprintf, FALSE, HANDLE, NULL, and TRUE.

Referenced by Init(), and rzk__close().

◆ WriteKernelMemory()

int WriteKernelMemory ( ut64  address,
const ut8 buf,
int  len 
)

Definition at line 147 of file io_rzk_windows.c.

147  {
148  DWORD ret = -1, bRead = 0;
149  LPVOID lpBuffer = NULL;
150  int bufsize;
151  PPA p;
152  if (gHandleDriver) {
153  bufsize = sizeof(PA) + len;
154  if (!(lpBuffer = malloc(bufsize))) {
155  eprintf("[rzk] WriteKernelMemory: Error can't allocate %i bytes of memory.\n", bufsize);
156  return -1;
157  }
158  p = (PPA)lpBuffer;
159  p->address.QuadPart = address;
160  p->len = len;
161  memcpy(&p->buffer, buf, len);
162  if (DeviceIoControl(gHandleDriver, IOCTL_WRITE_KERNEL_MEM, lpBuffer, bufsize, lpBuffer, bufsize, &bRead, NULL)) {
163  ret = len;
164  } else {
165  eprintf("[rzk] WriteKernelMemory: Error IOCTL_WRITE_KERNEL_MEM.\n");
166  ret = -1;
167  }
168  free(lpBuffer);
169  } else {
170  eprintf("Driver not initialized.\n");
171  }
172  return ret;
173 }
#define IOCTL_WRITE_KERNEL_MEM

References DWORD, eprintf, free(), gHandleDriver, IOCTL_WRITE_KERNEL_MEM, len, malloc(), memcpy(), NULL, and p.

Referenced by rzk__write().

Variable Documentation

◆ gHandleDriver

HANDLE gHandleDriver = NULL