-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriverInterop.cpp
More file actions
82 lines (64 loc) · 2.27 KB
/
DriverInterop.cpp
File metadata and controls
82 lines (64 loc) · 2.27 KB
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
#ifdef MEMORY_INTERFACE_DRIVER
#include "DriverInterop.h"
#include <Windows.h>
#include <string>
#include "Utils.h"
#define PRIMITIVES_ONLY 1
#include "Global.hpp"
#define SIOCTL_TYPE 40000
#define IO_READ_REQUEST CTL_CODE(SIOCTL_TYPE, 0x903, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IO_WRITE_REQUEST CTL_CODE(SIOCTL_TYPE, 0x904, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IO_GET_MODULE_BASE_REQUEST CTL_CODE(SIOCTL_TYPE, 0x906, METHOD_BUFFERED, FILE_ANY_ACCESS)
struct IOReadRequest {
unsigned long ProcessId;
uintptr_t Address;
SIZE_T Size;
unsigned char* Value;
};
struct IOWriteRequest {
unsigned long ProcessId;
uintptr_t Address;
SIZE_T Size;
unsigned char* Value;
};
bool DriverInterop::ReadRaw(uintptr_t address, void* data, unsigned long size) {
if (address < MINIMUM_ADDRESS_SIZE) {
return false;
}
IOReadRequest request{ this->pid, address, size, (unsigned char*)data };
return DeviceIoControl(this->handle, IO_READ_REQUEST, &request, sizeof(IOReadRequest), &request, sizeof(IOReadRequest), nullptr, nullptr);
}
bool DriverInterop::WriteRaw(uintptr_t address, void* data, unsigned long size) {
if (address < MINIMUM_ADDRESS_SIZE) {
return false;
}
IOWriteRequest request{ this->pid, address, size, (unsigned char*)data};
return DeviceIoControl(this->handle, IO_WRITE_REQUEST, &request, sizeof(IOWriteRequest), &request, sizeof(IOWriteRequest), nullptr, nullptr);
}
bool DriverInterop::UpdateProcessId(const wchar_t* processName) {
if (this->pid) {
return true;
}
this->pid = Utils::GetProcessIdByName(processName);
return this->pid != 0;
}
uintptr_t DriverInterop::GetModuleBase() {
uintptr_t request = 0;
return DeviceIoControl(this->handle,
IO_GET_MODULE_BASE_REQUEST,
&request, sizeof(uintptr_t),
&request, sizeof(uintptr_t),
nullptr, nullptr
) ? request : 0;
}
DriverInterop::DriverInterop() : pid(0) {
this->handle = CreateFileW(L"\\\\.\\TestDriver", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (GetLastError()) {
MessageBox(0, L"Failed to open driver. Is it running?", L"TCDR", MB_OK);
exit(0);
}
}
DriverInterop::~DriverInterop() {
CloseHandle(this->handle);
}
#endif