This repository has been archived by the owner on Jun 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathkernelinterface.hpp
224 lines (176 loc) · 5 KB
/
kernelinterface.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#pragma warning( disable : 4101 4244)
#include "communications.hpp"
// The interface handler for IOCTL. Thanks Zeromem.
class KeInterface
{
public:
HANDLE hDriver; // Handle to driver
// Initializer
KeInterface(LPCSTR RegistryPath)
{
hDriver = CreateFileA(RegistryPath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
}
bool RVM(ULONG ProcessId, ULONG ReadAddress, BYTE &Buffer, long Size)
{
// allocate a buffer with specified type to allow our driver to write our wanted data inside this buffer
DWORD Return, Bytes;
KERNEL_READ_REQUEST ReadRequest;
ReadRequest.ProcessId = ProcessId;
ReadRequest.Address = ReadAddress;
//send the 'address' of the buffer so our driver can know where to write the data
ReadRequest.pBuff = &Buffer;
ReadRequest.Size = Size;
// send code to our driver with the arguments
if (DeviceIoControl(hDriver, IO_READ_REQUEST, &ReadRequest, sizeof(ReadRequest), &ReadRequest, sizeof(ReadRequest), 0, 0))
{
//return our buffer
return true;
}
return false;
}
template <typename type>
type ReadVirtualMemory(ULONG ProcessId, ULONG ReadAddress, SIZE_T Size)
{
// allocate a buffer with specified type to allow our driver to write our wanted data inside this buffer
type Buffer;
DWORD Return, Bytes;
KERNEL_READ_REQUEST ReadRequest;
ReadRequest.ProcessId = ProcessId;
ReadRequest.Address = ReadAddress;
//send the 'address' of the buffer so our driver can know where to write the data
ReadRequest.pBuff = &Buffer;
ReadRequest.Size = Size;
// send code to our driver with the arguments
if (DeviceIoControl(hDriver, IO_READ_REQUEST, &ReadRequest, sizeof(ReadRequest), &ReadRequest, sizeof(ReadRequest), 0, 0))
{
//return our buffer
return Buffer;
}
return Buffer;
}
template <typename type>
bool WriteVirtualMemory(ULONG ProcessId, ULONG WriteAddress, type WriteValue, SIZE_T WriteSize)
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
DWORD Bytes;
KERNEL_WRITE_REQUEST WriteRequest;
WriteRequest.ProcessId = ProcessId;
WriteRequest.Address = WriteAddress;
//send driver the 'address' of our specified type WriteValue so our driver can copy the data we want to write from this address to WriteAddress
WriteRequest.pBuff = &WriteValue;
WriteRequest.Size = WriteSize;
if (DeviceIoControl(hDriver, IO_WRITE_REQUEST, &WriteRequest, sizeof(WriteRequest), 0, 0, &Bytes, NULL))
{
return true;
}
return false;
}
bool ReadSpecial(ULONG ProcessId, DWORD dwAddress, LPVOID lpBuffer, DWORD dwSize)
{
SIZE_T Out = NULL;
KERNEL_READ_REQUEST ReadRequest;
ReadRequest.ProcessId = ProcessId;
ReadRequest.Address = dwAddress;
ReadRequest.pBuff = &lpBuffer;
ReadRequest.Size = dwSize;
return (DeviceIoControl(hDriver, IO_READ_REQUEST, &ReadRequest, sizeof(ReadRequest), &ReadRequest, sizeof(ReadRequest), 0, 0) == TRUE);
}
DWORD GetTargetPid()
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
ULONG Id;
DWORD Bytes;
if (DeviceIoControl(hDriver, IO_GET_ID_REQUEST, &Id, sizeof(Id),
&Id, sizeof(Id), &Bytes, NULL))
{
return Id;
}
return false;
}
DWORD GetClientModule()
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
ULONG Address;
DWORD Bytes;
if (DeviceIoControl(hDriver, IO_GET_MODULE_REQUEST, &Address, sizeof(Address),
&Address, sizeof(Address), &Bytes, NULL))
{
return Address;
}
return false;
}
DWORD GetClientModuleSize()
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
ULONG Address;
DWORD Bytes;
if (DeviceIoControl(hDriver, IO_GET_MODULE_REQUEST_LENGTH, &Address, sizeof(Address),
&Address, sizeof(Address), &Bytes, NULL))
{
return Address;
}
return false;
}
DWORD GetEngineModule()
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
ULONG Address;
DWORD Bytes;
if (DeviceIoControl(hDriver, IO_GET_ENGINE_MODULE_REQUEST, &Address, sizeof(Address),
&Address, sizeof(Address), &Bytes, NULL))
{
return Address;
}
return false;
}
DWORD GetEngineModuleSize()
{
if (hDriver == INVALID_HANDLE_VALUE)
return false;
ULONG Address;
DWORD Bytes;
if (DeviceIoControl(hDriver, IO_GET_ENGINE_MODULE_REQUEST_LENGTH, &Address, sizeof(Address),
&Address, sizeof(Address), &Bytes, NULL))
{
return Address;
}
return false;
}
BOOL DataCompare(const BYTE* pData, const BYTE* pMask, const char* pszMask)
{
for (; *pszMask; ++pszMask, ++pData, ++pMask)
{
if (*pszMask == 'x' && *pData != *pMask)
{
return FALSE;
}
}
return (*pszMask == 0);
}
DWORD Scan(ULONG ProcessId, DWORD dwStart, DWORD dwSize, BYTE* pSignature, const char* pMask)
{
BYTE* pRemote = new BYTE[dwSize];
if (!ReadSpecial(ProcessId, dwStart, pRemote, dwSize))
{
delete[] pRemote;
return NULL;
}
for (DWORD dwIndex = 0; dwIndex < dwSize; dwIndex++)
{
if (DataCompare((const BYTE*)(pRemote + dwIndex),pSignature, pMask))
{
delete[] pRemote;
return (dwStart + dwIndex);
}
}
delete[] pRemote;
return NULL;
}
};