forked from can1357/ThePerfectInjector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriverLoader.h
178 lines (140 loc) · 4.68 KB
/
DriverLoader.h
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
#pragma once
#include <Windows.h>
#include <string>
#include <Shlwapi.h>
#include <iostream>
#include "NtDefines.h"
#pragma comment(lib, "Shlwapi.lib")
static NTSTATUS Dl_RemoveDriverFromRegistry( const wchar_t* DriverName )
{
NTSTATUS Status = STATUS_SUCCESS;
std::wstring RegistryPath = std::wstring( L"System\\CurrentControlSet\\Services\\" ) + DriverName;
Status = RegDeleteKeyW( HKEY_LOCAL_MACHINE,
RegistryPath.c_str() );
if ( !Status || Status == ERROR_FILE_NOT_FOUND )
return STATUS_SUCCESS;
Status = SHDeleteKeyW( HKEY_LOCAL_MACHINE,
RegistryPath.c_str() );
if ( !Status || Status == ERROR_FILE_NOT_FOUND )
return STATUS_SUCCESS;
Status = RegDeleteKeyW( HKEY_LOCAL_MACHINE,
RegistryPath.c_str() );
if ( !Status || Status == ERROR_FILE_NOT_FOUND )
return STATUS_SUCCESS;
return Status;
}
static NTSTATUS Dl_TryOpenServiceKey( const wchar_t* DriverName )
{
std::wstring RegistryPath = std::wstring( L"System\\CurrentControlSet\\Services\\" ) + DriverName;
HKEY Key;
NTSTATUS Result = RegOpenKeyExW( HKEY_LOCAL_MACHINE,
RegistryPath.c_str(),
0,
KEY_ALL_ACCESS,
&Key );
RegCloseKey( Key );
return Result;
}
static NTSTATUS Dl_AddServiceToRegistery( const wchar_t* DriverName )
{
NTSTATUS Status = STATUS_SUCCESS;
std::wstring RegistryPath = std::wstring( L"System\\CurrentControlSet\\Services\\" ) + DriverName;
Dl_RemoveDriverFromRegistry( DriverName );
HKEY Key;
Status = RegCreateKeyExW( HKEY_LOCAL_MACHINE,
RegistryPath.c_str(),
0,
NULL,
0,
KEY_ALL_ACCESS,
NULL,
&Key,
0 );
if ( Status )
return Status;
const auto RegWriteString = [ = ] ( const wchar_t* Name, const std::wstring& Data ) -> NTSTATUS
{
return RegSetValueExW( Key,
Name,
0,
REG_EXPAND_SZ,
( PBYTE ) Data.c_str(),
Data.size() * sizeof( wchar_t ) );
};
const auto RegWriteDWORD = [ = ] ( const wchar_t* Name, DWORD Data ) -> NTSTATUS
{
return RegSetValueExW( Key,
Name,
0,
REG_DWORD,
( PBYTE ) &Data,
sizeof( DWORD ) );
};
Status |= RegWriteString( L"ImagePath", std::wstring( L"\\SystemRoot\\System32\\drivers\\" ) + DriverName + L".sys" );
Status |= RegWriteDWORD( L"Type", 1 );
Status |= RegWriteDWORD( L"ErrorControl", 1 );
Status |= RegWriteDWORD( L"Start", 3 );
if ( Status )
{
RegCloseKey( Key );
Dl_RemoveDriverFromRegistry( DriverName );
return Status;
}
RegCloseKey( Key );
return STATUS_SUCCESS;
}
static NTSTATUS Dl_UnloadDriver( const wchar_t* DriverName )
{
if ( !AcquirePrivilege( SeLoadDriverPrivilege, AdjustCurrentProcess ) )
return 1;
if ( Dl_TryOpenServiceKey( DriverName ) == 2 )
Dl_AddServiceToRegistery( DriverName );
std::wstring SourceRegistry = std::wstring( L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\" ) + DriverName;
UNICODE_STRING SourceRegistryUnicode = { 0 };
SourceRegistryUnicode.Buffer = ( wchar_t* ) SourceRegistry.c_str();
SourceRegistryUnicode.Length = ( SourceRegistry.size() ) * 2;
SourceRegistryUnicode.MaximumLength = ( SourceRegistry.size() + 1 ) * 2;
NTSTATUS Status = NtUnloadDriver( &SourceRegistryUnicode );
printf( "[+] NtUnloadDriver(%ls) returned %08x\n", SourceRegistry.c_str(), Status );
Dl_RemoveDriverFromRegistry( DriverName );
return Status;
}
static NTSTATUS Dl_LoadDriver( const wchar_t* DriverName )
{
if ( !AcquirePrivilege( SeLoadDriverPrivilege, AdjustCurrentProcess ) )
return 1;
if ( Dl_AddServiceToRegistery( DriverName ) )
return 2;
std::wstring SourceRegistry = std::wstring( L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\" ) + DriverName;
UNICODE_STRING SourceRegistryUnicode = { 0 };
SourceRegistryUnicode.Buffer = ( wchar_t* ) SourceRegistry.c_str();
SourceRegistryUnicode.Length = ( SourceRegistry.size() ) * 2;
SourceRegistryUnicode.MaximumLength = ( SourceRegistry.size() + 1 ) * 2;
NTSTATUS Status = NtLoadDriver( &SourceRegistryUnicode );
printf( "[+] NtLoadDriver(%ls) returned %08x\n", SourceRegistry.c_str(), Status );
if ( Status )
{
Dl_UnloadDriver( DriverName );
Dl_RemoveDriverFromRegistry( DriverName );
}
return Status;
}
static HANDLE Dl_OpenDevice( std::string DriverName )
{
char CompleteDeviceName[ 128 ];
sprintf_s( CompleteDeviceName, "\\\\.\\%s", DriverName.data() );
HANDLE DeviceHandle = CreateFileA
(
CompleteDeviceName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if ( DeviceHandle == INVALID_HANDLE_VALUE )
DeviceHandle = 0;
printf( "[+] CreateFileA(%s) returned %08x\n", CompleteDeviceName, DeviceHandle );
return DeviceHandle;
}