-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdriver.cpp
257 lines (208 loc) · 6.83 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
/* =========================================================
* Driver.cpp
* *********************************************************
*
* Class: CDriver_Loader
*
* Author: Dan Revah
*
* Date: 03/12/2011 (DD/MM/YYYY)
* Version: 1.00
*
* (-!-) Kernel-mode driver loader
* - Injecting into windows kernel
* ======================================================== */
#include "driver.h"
/* ===========================================================
* CDriver_Loader::CDriver_Loader()
*
* (*) Default Constructor
*/
CDriver_Loader::CDriver_Loader():
init(false), loaded(false), started(false), mFilePath(NULL), mServiceName(NULL),
mDisplayName(NULL), mStartType(0), mService(NULL)
{
}
/* ==========================================================================================================
* CDriver_Loader::CDriver_Loader(LPTSTR filePath, LPTSTR serviceName, LPTSTR displayName, DWORD startType)
*
* (*) Initializing constructor
*/
CDriver_Loader::CDriver_Loader(LPTSTR filePath, LPTSTR serviceName, LPTSTR displayName, DWORD startType):
init(true), loaded(false), started(false), mFilePath(filePath), mServiceName(serviceName),
mDisplayName(displayName), mStartType(startType), mService(NULL)
{
}
/* ====================================
* CDriver_Loader::~CDriver_Loader()
*
* (*) Destructor
*/
CDriver_Loader::~CDriver_Loader()
{
UnloadSvc();
mFilePath = NULL;
mServiceName = NULL;
mDisplayName = NULL;
mStartType = 0;
mService = NULL;
init = false;
loaded = false;
started = false;
}
/* ===========================================================
* DWORD CDriver_Loader::CreateSvc()
*
* - Creating the driver service
* Return Value
* If the function succeeds, the return value is SVC_OK
* If the function failed it will throw a exception
*/
DWORD CDriver_Loader::CreateSvc()
{
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
throw std::exception("OpenSCManager Failed with error code: "+GetLastError());
mService = CreateService(hSCManager, mServiceName, mDisplayName, SC_MANAGER_ALL_ACCESS,
SERVICE_KERNEL_DRIVER, mStartType, SERVICE_ERROR_NORMAL, mFilePath, NULL, NULL, NULL, NULL, NULL );
if (mService == NULL)
{
mService = OpenService(hSCManager, mServiceName, SERVICE_ALL_ACCESS);
if (mService == NULL)
{
CloseServiceHandle(hSCManager);
throw std::exception("CreateService Failed with error code: "+GetLastError());
}
}
loaded = true;
CloseServiceHandle(hSCManager);
return SVC_OK;
}
/* ============================================================================================================
* DWORD CDriver_Loader::InitSvc(LPTSTR filePath, LPTSTR serviceName, LPTSTR displayName, DWORD startType)
*
* - Initilazing the service parameters
*
* Parameters:
* filePath - The fully-qualified path to the service binary file
* serviceName - The service name
* displayName - The dos-service name
* startType - The service start options
*
* Return Value
* If the function succeeds or already initialzed, the return value is SV_OK
*/
DWORD CDriver_Loader::InitSvc(LPTSTR filePath, LPTSTR serviceName, LPTSTR displayName, DWORD startType)
{
if (isInit())
return SVC_OK;
mFilePath = filePath;
mServiceName = serviceName;
mDisplayName = displayName;
mStartType = startType;
mService = NULL;
init = true;
loaded = false;
started = false;
return SVC_OK;
}
/* ==============================================================
* SVC_Result CDriver_Loader::StartSvc()
*
* - Initilazing the service parameters
*
* Return Value
* If the function succeeds, the return value is SVC_OK
* If the function failed it will throw a exception
*/
DWORD CDriver_Loader::StartSvc()
{
if (!isLoaded())
throw std::exception("Service is not loaded");
if (isStarted())
return SVC_OK;
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
throw std::exception("OpenSCManager Failed with error code: "+GetLastError());
mService = OpenService(hSCManager, mServiceName, SERVICE_ALL_ACCESS);
if (mService == NULL)
{
CloseServiceHandle(hSCManager);
throw std::exception("OpenService Failed with error code: "+GetLastError());
}
if (StartService(mService,0,NULL)== NULL)
{
CloseServiceHandle(hSCManager);
CloseServiceHandle(mService);
throw std::exception("StartService Failed with error code: "+GetLastError());
}
CloseServiceHandle(hSCManager);
started = true;
return SVC_OK;
}
/* ==============================================================
* DWORD CDriver_Loader::StopSvc()
*
* - Initilazing the service parameters
*
* Return Value
* If the function succeeds, the return value is SVC_OK
* If the function failed it will throw a exception
*/
DWORD CDriver_Loader::StopSvc()
{
SERVICE_STATUS ss;
if (!isStarted())
return SVC_OK;
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
throw std::exception("OpenSCManager Failed with error code: "+GetLastError());
mService = OpenService(hSCManager, mServiceName, SERVICE_ALL_ACCESS);
if (mService == NULL)
{
CloseServiceHandle(hSCManager);
throw std::exception("OpenService Failed with error code: "+GetLastError());
}
if (ControlService(mService,SERVICE_CONTROL_STOP,&ss)== NULL)
{
CloseServiceHandle(hSCManager);
CloseServiceHandle(mService);
throw std::exception("ControlService Failed with error code: "+GetLastError());
}
CloseServiceHandle(hSCManager);
CloseServiceHandle(mService);
started = false;
return SVC_OK;
}
/* ==============================================================
* DWORD CDriver_Loader::UnloadSvc()
*
* - Unloading the service
*
* Return Value
* If the function succeeds, the return value is SVC_OK
* If the function failed it will throw a exception
*/
DWORD CDriver_Loader::UnloadSvc()
{
if (!isLoaded())
return SVC_OK;
if (isStarted())
{
if (StopSvc() != SVC_OK)
throw std::exception("Unloading driver Failed with error code: "+GetLastError());
}
SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
throw std::exception("OpenSCManager Failed with error code: "+GetLastError());
mService = OpenService(hSCManager, mServiceName, SERVICE_ALL_ACCESS);
if (mService == NULL)
{
CloseServiceHandle(hSCManager);
throw std::exception("OpenService Failed with error code: "+GetLastError());
}
DeleteService(mService);
CloseServiceHandle(hSCManager);
loaded = false;
return SVC_OK;
}