-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdriver.cpp
More file actions
199 lines (160 loc) · 4.79 KB
/
Copy pathdriver.cpp
File metadata and controls
199 lines (160 loc) · 4.79 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
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
#include "driver.h"
#include <filesystem>
#include <bcrypt.h>
#include <fstream>
#include <cwctype>
#include <sstream>
#include <iomanip>
#include <chrono>
#include <random>
#define PAGE_SIZE 0x1000
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "bcrypt.lib")
std::wstring generate_hex_wstr()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<unsigned int> dis(0x10000, 0xFFFFF); // 限制在 5 位十六进制范围
std::wstringstream wss;
wss << std::hex << std::setw(5) << std::setfill(L'0') << dis(gen);
return wss.str();
}
//必须要随机化,不然有bug
std::wstring svc_name = L"WpnUserService_" + generate_hex_wstr();
BOOL ReadPhysicalMemory(HANDLE hDevice, LARGE_INTEGER physicalAddress, DWORD sizeToRead, std::vector<BYTE>& outBuffer) {
if (hDevice == INVALID_HANDLE_VALUE || sizeToRead == 0) return FALSE;
DWORD pageOffset = physicalAddress.QuadPart % PAGE_SIZE;
LARGE_INTEGER alignedAddress; alignedAddress.QuadPart = physicalAddress.QuadPart - pageOffset;
DWORD totalRequiredLength = pageOffset + sizeToRead;
DWORD alignedSize = ((totalRequiredLength + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
BYTE* tempBuffer = (BYTE*)VirtualAlloc(NULL, alignedSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!tempBuffer) return FALSE;
OVERLAPPED ol = {0};
ol.Offset = alignedAddress.LowPart;
ol.OffsetHigh = alignedAddress.HighPart;
DWORD bytesRead = 0;
BOOL success = ReadFile(hDevice, tempBuffer, alignedSize, &bytesRead, &ol);
if (success && bytesRead >= totalRequiredLength) {
outBuffer.resize(sizeToRead);
std::memcpy(outBuffer.data(), tempBuffer + pageOffset, sizeToRead);
}else{
success = FALSE;
}
VirtualFree(tempBuffer, 0, MEM_RELEASE);
return success;
}
Bytes phys_read(HANDLE hDevice, uint64_t addr, uint64_t size) {
LARGE_INTEGER testAddress;
testAddress.QuadPart = addr;
std::vector<BYTE> resultData;
if (!ReadPhysicalMemory(hDevice, testAddress, size, resultData)) {
printf("Read_PhysicalMemory fail \n");
}
return (resultData.size() >= size) ? resultData : Bytes{};
}
//this is have a bug,if run too many time if wail fail, better it to random to gen svc_name
bool load_driver(std::string filepath = "")
{
auto path =
std::filesystem::absolute(
filepath.empty() ? "Magnet.sys" : filepath
).wstring();
//constexpr wchar_t svc_name[] =
// L"AMD Crash DriverServie";
SC_HANDLE h_scm =
OpenSCManagerW(
nullptr,
nullptr,
SC_MANAGER_ALL_ACCESS);
if (!h_scm)
{
printf("OpenSCManager failed: %lu\n",
GetLastError());
return false;
}
SC_HANDLE h_svc =
CreateServiceW(
h_scm,
svc_name.c_str(),
svc_name.c_str(),
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
path.c_str(),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr);
if (!h_svc)
{
DWORD err = GetLastError();
printf("CreateServiceW failed: %lu\n", err);
if (err == ERROR_SERVICE_EXISTS ||
err == ERROR_SERVICE_MARKED_FOR_DELETE)
{
h_svc =
OpenServiceW(
h_scm,
svc_name.c_str(),
SERVICE_ALL_ACCESS);
if (!h_svc)
{
printf("OpenServiceW failed: %lu\n",
GetLastError());
CloseServiceHandle(h_scm);
return false;
}
}
else
{
CloseServiceHandle(h_scm);
return false;
}
}
BOOL ok =
StartServiceW(
h_svc,
0,
nullptr);
if (!ok)
{
DWORD err = GetLastError();
printf("StartServiceW failed: %lu\n", err);
if (err == ERROR_SERVICE_ALREADY_RUNNING)
{
ok = TRUE;
}
}
CloseServiceHandle(h_svc);
CloseServiceHandle(h_scm);
return ok == TRUE;
}
//this is have a bug,i donot know why it sometime will fail
bool stop_driver()
{
SC_HANDLE h_scm =
OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
if (!h_scm)
return false;
SC_HANDLE h_svc =
OpenServiceW(
h_scm,
svc_name.c_str(),
SERVICE_ALL_ACCESS);
if (!h_svc)
{
CloseServiceHandle(h_scm);
return false;
}
SERVICE_STATUS status;
ControlService(
h_svc,
SERVICE_CONTROL_STOP,
&status);
DeleteService(h_svc);
CloseServiceHandle(h_svc);
CloseServiceHandle(h_scm);
return true;
}