-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.hpp
346 lines (282 loc) · 10.8 KB
/
commands.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#pragma once
#include <memory>
#include "commands_id.hpp"
class CCommand
{
private:
HANDLE hDriver = INVALID_HANDLE_VALUE;
bool bInitialized = false;
NTSTATUS LastStatus = STATUS_SUCCESS;
bool SendCmd( std::uint32_t dwIOCTL, std::uint64_t* Buffer, std::uint32_t Lenght )
{
IO_STATUS_BLOCK io{ };
LastStatus = NtDeviceIoControlFile(
hDriver, nullptr, nullptr, nullptr, &io,
dwIOCTL, Buffer, Lenght, Buffer, Lenght );
return NT_SUCCESS( LastStatus );
}
public:
CCommand()
{
UNICODE_STRING us{ };
RtlInitUnicodeString( &us, ( L"\\DosDevices\\Global\\KlhkCtrl" ) );
OBJECT_ATTRIBUTES oa{ };
InitializeObjectAttributes( &oa, &us, OBJ_CASE_INSENSITIVE, NULL, NULL );
IO_STATUS_BLOCK io{ };
const auto res = NtOpenFile( &hDriver, FILE_GENERIC_READ | FILE_GENERIC_WRITE, &oa, &io, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE );
if ( !NT_SUCCESS( res ) )
{
printf( "NtOpenFile failed 0x%X\n", res );
getchar();
return;
}
if ( hDriver == INVALID_HANDLE_VALUE || !hDriver )
{
MessageBoxA( GetActiveWindow(), "Driver not found or loaded!", NULL, MB_ICONSTOP );
}
else
bInitialized = true;
}
~CCommand()
{
if ( bInitialized )
CloseHandle( hDriver );
}
bool Status() const
{
return bInitialized;
}
NTSTATUS GetLastNtError() const
{
return LastStatus;
}
bool AttachProcess( std::uint64_t ProcessId, std::uint64_t ThreadId, std::uint64_t* hProcess )
{
ATTACH_PROCESS cmd{ };
cmd.ProcessId = ProcessId;
cmd.ThreadId = ThreadId;
const auto res = SendCmd( IOCTL_ATTACH_TO_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
if ( res )
{
if ( hProcess )
*hProcess = cmd.OutProcessHandle;
}
return res;
}
bool DetachProcess( std::uint64_t ProcessHandle )
{
DETACH_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
return SendCmd( IOCTL_DETACH_FROM_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool SuspendProcess( std::uint64_t ProcessHandle )
{
PROCESS_MISC cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.Suspend = TRUE;
return SendCmd( IOCTL_SUSPEND_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool ResumeProcess( std::uint64_t ProcessHandle )
{
PROCESS_MISC cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.Suspend = FALSE;
return SendCmd( IOCTL_RESUME_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool FlushInstructionCache( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint32_t Length )
{
FLUSHCACHE_MEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Length;
return SendCmd( IOCTL_FLUSH_INSTRUCTION_CACHE, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool SetInformationProcess( std::uint64_t ProcessHandle, std::uint32_t InfoClass, std::uint64_t* ProcessInfo, std::uint32_t ProcessInfoLenght )
{
SETINFO_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.InformationClass = InfoClass;
cmd.Buffer = ProcessInfo;
cmd.Lenght = ProcessInfoLenght;
return SendCmd( IOCTL_SET_INFORMATION_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool ReadProcessMemory( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t* Buffer, std::uint64_t Lenght, std::uint64_t* BytesReaded )
{
READMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Buffer = Buffer;
cmd.Lenght = Lenght;
cmd.BytesRead = BytesReaded;
return SendCmd( IOCTL_READ_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool WriteProcessMemory( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t* Buffer, std::uint64_t Lenght, std::uint64_t* BytesWritten )
{
WRITEMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Buffer = Buffer;
cmd.Lenght = Lenght;
cmd.BytesWritten = BytesWritten;
return SendCmd( IOCTL_WRITE_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
std::uint64_t VirtualAllocEx( std::uint64_t ProcessHandle, std::uint64_t BaseAddress, std::uint64_t Lenght, std::uint32_t Type, std::uint32_t Protect )
{
ALLOCMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.Type = Type;
cmd.Protect = Protect;
if ( SendCmd( IOCTL_ALLOCATE_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) ) )
return cmd.BaseAddress;
return NULL;
}
bool FlushMemory( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t Lenght, PIO_STATUS_BLOCK IoStatus )
{
FLUSHVIRTUAL_MEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.IoStatus = IoStatus;
return SendCmd( IOCTL_FLUSH_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool VirtualLockEx( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t Lenght, std::uint32_t LockOption )
{
LOCKMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.Option = LockOption;
return SendCmd( IOCTL_LOCK_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool VirtualUnlockEx( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t Lenght, std::uint32_t LockOption )
{
UNLOCKMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.Option = LockOption;
return SendCmd( IOCTL_UNLOCK_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool VirtualFreeEx( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t Lenght, std::uint32_t Type )
{
FREEMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.Type = Type;
return SendCmd( IOCTL_FREE_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool VirtualProtectEx( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint64_t Lenght, std::uint32_t NewAccess, std::uint32_t* OldAccess )
{
PROTECTMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.Lenght = Lenght;
cmd.NewAccess = NewAccess;
cmd.OldAccess = OldAccess;
return SendCmd( IOCTL_PROTECT_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool VirtualQueryEx( std::uint64_t ProcessHandle, std::uint64_t* BaseAddress, std::uint32_t InformationClass, std::uint64_t* Buffer, std::uint64_t Length, std::uint32_t* ResultLength )
{
QUERYMEMORY_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.BaseAddress = BaseAddress;
cmd.InformationClass = InformationClass;
cmd.Buffer = Buffer;
cmd.Lenght = Length;
cmd.ResultLength = ResultLength;
return SendCmd( IOCTL_QUERY_MEMORY_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool QueryInformationProcess( std::uint64_t ProcessHandle, std::uint32_t InformationClass, std::uint64_t* Buffer, std::uint32_t Length, std::uint32_t* ResultLength )
{
QUERYINFO_PROCESS cmd{ };
cmd.ProcessHandle = ProcessHandle;
cmd.InformationClass = InformationClass;
cmd.Buffer = Buffer;
cmd.Lenght = Length;
cmd.ResultLength = ResultLength;
return SendCmd( IOCTL_QUERY_INFO_PROCESS, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool QuerySystemInformationEx( std::uint32_t InformationClass, std::uint64_t* InputBuffer, std::uint32_t InputBufferLenght, std::uint64_t* SystemInfo, std::uint32_t SystemInfoLenght, std::uint32_t* ResultLength )
{
QUERY_SYSTEMINFOEX cmd{ };
cmd.InformationClass = InformationClass;
cmd.InputBuffer = InputBuffer;
cmd.InputBufferLenght = InputBufferLenght;
cmd.SystemInfo = SystemInfo;
cmd.SystemInfoLenght = SystemInfoLenght;
cmd.ResultLength = ResultLength;
return SendCmd( IOCTL_QUERY_SYSTEM_INFO_EX, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool OpenThread( std::uint64_t ThreadId, std::uint64_t ProcessId, std::uint64_t* hThreadHandle )
{
OPEN_THREAD_PROCESS cmd{ };
cmd.ProcessId = ProcessId;
cmd.ThreadId = ThreadId;
const auto res = SendCmd( IOCTL_OPEN_THREAD, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
if ( res )
{
if ( hThreadHandle )
*hThreadHandle = cmd.OutThreadHandle;
}
return res;
}
bool GetThreadContext( std::uint64_t ThreadHandle, PCONTEXT pCtx )
{
GET_CONTEXT_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.Context = pCtx;
return SendCmd( IOCTL_GET_CONTEXT_THREAD, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool SetThreadContext( std::uint64_t ThreadHandle, PCONTEXT pCtx )
{
SET_CONTEXT_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.Context = pCtx;
return SendCmd( IOCTL_SET_CONTEXT_THREAD, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool ResumeThread( std::uint64_t ThreadHandle, std::uint32_t* ResumeCount )
{
RESUME_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.Count = ResumeCount;
return SendCmd( IOCTL_RESUME_THREAD, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool SuspendThread( std::uint64_t ThreadHandle, std::uint32_t* SuspendCount )
{
SUSPEND_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.Count = SuspendCount;
return SendCmd( IOCTL_SUSPEND_THREAD, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool QueryThreadInformation( std::uint64_t ThreadHandle, std::uint32_t InfoClass, std::uint64_t* ThreadInfo, std::uint32_t ThreadInfoLenght, std::uint32_t* ResultLenght )
{
QUERYINFO_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.ThreadInfo = ThreadInfo;
cmd.ThreadInfoLenght = ThreadInfoLenght;
cmd.ResultLenght = ResultLenght;
cmd.InformationClass = InfoClass;
return SendCmd( IOCTL_QUERY_THREAD_INFO, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
bool SetThreadInformation( std::uint64_t ThreadHandle, std::uint32_t InfoClass, std::uint64_t* ThreadInfo, std::uint32_t ThreadInfoLenght )
{
SETINFO_THREAD_PROCESS cmd{ };
cmd.ThreadHandleValue = ThreadHandle;
cmd.ThreadInfo = ThreadInfo;
cmd.ThreadInfoLenght = ThreadInfoLenght;
cmd.InformationClass = InfoClass;
return SendCmd( IOCTL_SET_THREAD_INFO, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
void WaitForSingleObject( std::uint64_t ObjectId, BOOL Alertable, PLARGE_INTEGER Timeout )
{
WAIT_OBJECT_PROCESS cmd{ };
cmd.ObjectValue = ObjectId;
cmd.Alertable = Alertable;
cmd.Timeout = Timeout;
SendCmd( IOCTL_WAIT_FOR_OBJECT, reinterpret_cast< std::uint64_t* >( &cmd ), sizeof( cmd ) );
}
};
extern std::unique_ptr< CCommand > g_cmdDriver;