Rizin
unix-like reverse engineering framework and cli tools
io_rzk_windows.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016 skuater <skuater@hotmail.com>
2 // SPDX-FileCopyrightText: 2016 Rakholiya Jenish
3 // SPDX-FileCopyrightText: 2017 Jose Diaz <josediazplay@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include "io_rzk_windows.h"
7 
9 
10 static BOOL InstallService(const char *rutaDriver, LPCTSTR lpServiceName, LPCTSTR lpDisplayName) {
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 }
26 
27 static BOOL RemoveService(LPCTSTR lpServiceName) {
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 }
42 
43 BOOL StartStopService(LPCTSTR lpServiceName, BOOL bStop) {
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 }
74 
75 static BOOL InitDriver(VOID) {
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 }
82 
83 static const char *GetFileName(const char *path) {
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 }
93 
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 }
117 
118 int ReadKernelMemory(ut64 address, ut8 *buf, int len) {
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 }
146 
147 int WriteKernelMemory(ut64 address, const ut8 *buf, int len) {
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 }
174 
175 int Init(const char *driverPath) {
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static BOOL InstallService(const char *rutaDriver, LPCTSTR lpServiceName, LPCTSTR lpDisplayName)
HANDLE gHandleDriver
Definition: io_rzk_windows.c:8
int GetSystemModules(RzIO *io)
static BOOL InitDriver(VOID)
static BOOL RemoveService(LPCTSTR lpServiceName)
int ReadKernelMemory(ut64 address, ut8 *buf, int len)
BOOL StartStopService(LPCTSTR lpServiceName, BOOL bStop)
static const char * GetFileName(const char *path)
int WriteKernelMemory(ut64 address, const ut8 *buf, int len)
int Init(const char *driverPath)
struct _PPA PA
#define IOCTL_WRITE_KERNEL_MEM
#define RZK_DEVICE
struct _RTL_PROCESS_MODULES * PRTL_PROCESS_MODULES
struct _PPA * PPA
#define IOCTL_READ_KERNEL_MEM
#define IOCTL_GET_SYSTEM_MODULES
voidpf void * buf
Definition: ioapi.h:138
#define INVALID_HANDLE_VALUE
Definition: iowin32.c:21
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
#define TRUE
Definition: mybfd.h:103
#define FALSE
Definition: mybfd.h:102
#define eprintf(x, y...)
Definition: rlcc.c:7
RTL_PROCESS_MODULE_INFORMATION Modules[1]
Definition: rz_io.h:59
PrintfCallback cb_printf
Definition: rz_io.h:91
DWORD * HANDLE
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
DWORD