-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Driver.cpp
273 lines (220 loc) · 6.72 KB
/
Driver.cpp
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
#include "Driver.h"
#define SystemModuleInformation 11
typedef struct _RTL_PROCESS_MODULE_INFORMATION
{
HANDLE Section;
PVOID MappedBase;
DWORD ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
typedef struct _RTL_PROCESS_MODULES
{
ULONG NumberOfModules;
RTL_PROCESS_MODULE_INFORMATION Modules[1];
} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
Driver::Driver(
const wstring& drvName,
const wstring& alternativeDrvName,
const IoctlNamesMap* ioctlNames
)
: driverName(drvName), lastError(ERROR_SUCCESS), ioctlNamesMap(ioctlNames)
{
shared_ptr<HANDLE> drvHandle(new HANDLE(
CreateFileW(
wstring(wstring(L"\\\\.\\") + driverName).c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL
)),
[](const HANDLE *ptr) {
if( *ptr != (HANDLE)INVALID_HANDLE_VALUE && ptr != nullptr) {
//wcout << L"[.] Closing driver's handle." << endl;
CloseHandle(*ptr);
}
delete ptr;
});
if( *drvHandle == static_cast<HANDLE>(INVALID_HANDLE_VALUE)) {
lastError = GetLastError();
wcerr << L"[!] Could not open \\Device\\" << driverName << L" driver's handle. Error: 0x"
<< lastError << endl;
return;
}
driverHandle = move(drvHandle);
wstring moduleName = (alternativeDrvName.size() > 0)? alternativeDrvName : drvName;
driverImageBase = Driver::GetModuleImageBase(moduleName);
if(!driverImageBase) {
lastError = GetLastError();
}
}
Driver::~Driver() {
}
DWORD
Driver::GetModuleImageBase(
const wstring &imageNameParam
) {
static const wstring ending(L".sys");
wstring imageName(imageNameParam);
transform(imageName.begin(), imageName.end(), imageName.begin(), ::tolower);
if(!equal(ending.rbegin(), ending.rend(), imageName.rbegin())) {
imageName += ending;
}
shared_ptr<RTL_PROCESS_MODULES> modulesInfo(
new RTL_PROCESS_MODULES[Max_Number_Of_Modules]
);
if(!modulesInfo.get()) {
wcerr << L"[!] Could not allocate memory for modulesInfo!" << endl;
return 0;
}
NTSTATUS status = NtQuerySystemInformation(
(SYSTEM_INFORMATION_CLASS)SystemModuleInformation,
modulesInfo.get(),
sizeof(RTL_PROCESS_MODULES) * Max_Number_Of_Modules,
NULL
);
if(!NT_SUCCESS(status)) {
wcerr << L"[!] Could not obtain list of loaded driver modules! Status: 0x"
<< hex << setfill(L'0') << setw(8) << status << endl;
return 0;
}
for(size_t i = 0; i < modulesInfo->NumberOfModules; i++ ) {
const DWORD imageBase = modulesInfo->Modules[i].ImageBase;
const char *moduleImageNamePointer = reinterpret_cast<const char*>(
modulesInfo->Modules[i].FullPathName
+ modulesInfo->Modules[i].OffsetToFileName);
const string moduleImageName(moduleImageNamePointer);
wstring moduleImageNameWide(moduleImageName.begin(), moduleImageName.end());
transform(
moduleImageNameWide.begin(),
moduleImageNameWide.end(),
moduleImageNameWide.begin(),
::tolower);
if(imageName == moduleImageNameWide) {
wcout << L"[+] Found " << imageName << L" driver's base: 0x"
<< hex << setfill(L'0') << setw(8) << imageBase << endl;
return imageBase;
}
}
wcerr << L"[!] Could not find the " << imageName
<< L" module among the loaded modules." << endl;
return 0;
}
tuple<ULONG, DWORD, wstring>
Driver::GetKernelModuleInfos() {
shared_ptr<RTL_PROCESS_MODULES> modulesInfo(
new RTL_PROCESS_MODULES[Max_Number_Of_Modules]
);
if(!modulesInfo.get()) {
wcerr << L"[!] Could not allocate memory for modulesInfo!" << endl;
return make_tuple(0, 0, L"");
}
NTSTATUS status = NtQuerySystemInformation(
(SYSTEM_INFORMATION_CLASS)SystemModuleInformation,
modulesInfo.get(),
sizeof(RTL_PROCESS_MODULES) * Max_Number_Of_Modules,
NULL
);
if(!NT_SUCCESS(status)) {
wcerr << L"[!] Could not obtain list of loaded driver modules! Status: 0x"
<< hex << setfill(L'0') << setw(8) << status << endl;
return make_tuple(0, 0, L"");
}
const char *moduleImageNamePointer = reinterpret_cast<const char*>(
modulesInfo->Modules[0].FullPathName
+ modulesInfo->Modules[0].OffsetToFileName);
const string moduleImageName(moduleImageNamePointer);
wstring moduleImageNameWide(moduleImageName.begin(), moduleImageName.end());
transform(
moduleImageNameWide.begin(),
moduleImageNameWide.end(),
moduleImageNameWide.begin(),
::tolower);
return make_tuple(
modulesInfo->Modules[0].ImageBase,
modulesInfo->Modules[0].ImageSize,
moduleImageNameWide
);
}
bool
Driver::SendIOCTL(
DWORD ioctlCode,
LPVOID inputBuffer,
DWORD inputSize,
LPVOID outputBuffer,
DWORD outSize,
LPDWORD writtenBytes,
BOOL quiet
) {
if(!quiet) {
auto ioctlCodeString = [=]() -> const wchar_t* {
wstringstream wss;
wss << L"0x" << hex << setw(8) << setfill(L'0') << ioctlCode;
return wss.str().c_str();
};
if (ioctlNamesMap) {
auto ioctl = ioctlNamesMap->find(ioctlCode);
if ( ioctl != ioctlNamesMap->end()) {
wcout << endl << L"[+] Issuing IOCTL: " << ioctl->second << endl;
} else {
wcout << endl << L"[+] Issuing IOCTL: " << ioctlCodeString() << endl;
}
} else {
wcout << endl << L"[+] Issuing IOCTL: " << ioctlCodeString() << endl;
}
}
DWORD written = 0;
bool ownAllocation = false;
LPDWORD writtenPtr = (writtenBytes != nullptr)? writtenBytes : &written;
if (inputBuffer == nullptr) {
ownAllocation = true;
inputBuffer = VirtualAlloc ((LPVOID)0,
inputSize,
MEM_COMMIT | MEM_RESERVE,
PAGE_EXECUTE_READWRITE
);
if (inputBuffer == nullptr) {
wcout << L"[!] Could not allocate memory for " << inputSize << L" bytes! Error: "
<< GetLastError() << endl;
throw runtime_error("allocation error");
}
}
if(!quiet) {
wcout << hex << setw(8) << setfill(L'0')
<< L"[.] Input buffer: 0x" << inputBuffer
<< L", size: " << dec << inputSize << L" bytes. Output: 0x"
<< hex << outputBuffer << endl;
}
SetLastError(0);
BOOL ret = DeviceIoControl(*driverHandle,
ioctlCode,
inputBuffer,
inputSize,
outputBuffer,
outSize,
writtenPtr,
NULL
);
const DWORD lastError = GetLastError();
if(!quiet) {
wcout << hex << setw(8) << setfill(L'0')
<< L"[>] After sending IOCTL. ret: " << ((ret)? L"TRUE" : L"FALSE")
<< L", last error: " << dec << lastError << L", written bytes: "
<< dec << *writtenPtr << endl;
if (lastError != ERROR_SUCCESS) {
wcout << L"\tError message: " << getErrorString(lastError) << endl;
}
wcout << endl;
}
if (ownAllocation) {
VirtualFree(inputBuffer, inputSize, MEM_DECOMMIT);
}
return ret;
}