-
Notifications
You must be signed in to change notification settings - Fork 21
/
kmd_install.cpp
146 lines (126 loc) · 3.48 KB
/
kmd_install.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
#include <stdio.h>
#include "windows.h"
#include "winsvc.h"
/*
Get a handle to the SCM database and then register the service
*/
SC_HANDLE installdriver(LPCTSTR drivername, LPCTSTR binarypath)
{
SC_HANDLE scmdbhandle = NULL;
SC_HANDLE svchandle = NULL;
//open a handle to the SCM
scmdbhandle = OpenSCManager
(
NULL, //MAchine name, NULL = local machine
NULL, //database name, NULL = SERVICES_ACTIVE_DATABASE
SC_MANAGER_ALL_ACCESS //desired access
);
//Check if were able to open a handle to the SCM
if(scmdbhandle == NULL)
{
cout << "installdriver, could not open handle to SCM manager" << endl;
return NULL;
}
//create a service which the SCM will run
svchandle = CreateService
(
scmdbhandle, //handle to the SC manager
drivername, //service name
drivername, //display name
SERVICE_ALL_ACCESS, //desired access
SERVICE_KERNEL_DRIVER, //service type
SERVICE_DEMAND_START, //start type, service will start when we call start service
SERVICE_ERROR_NORMAL, //error control
binarypath, //binary path name
NULL, //load order group
NULL, //tag id
NULL, //Dependancies
NULL, //service start name (account name)
NULL //password for the account
);
//check if we were able to create a new service
if(svchandle == NULL)
{
//check if the service already exists
if(GetLastError() == ERROR_SERVICE_EXISTS)
{
cout << "Error service exists" <<endl;
svchandle = OpenService(scmdbhandle, drivername, SERVICE_ALL_ACCESS);
//check if we were able to open the service
if(svchandle == NULL)
{
cout << "Error, could not open handle to the driver" << endl;
//close the handle to the service
CloseServiceHandle(scmdbhandle);
return NULL;
}
//close the handle to the service
CloseServiceHandle(scmdbhandle);
return NULL;
}
cout << "Error, could not open handle to the driver, and the service doesn't exist" << endl;
//close the handle to the service
CloseServiceHandle(scmdbhandle);
return NULL;
}
cout << "Success, the service was created" << endl;
//close the handle to the service
CloseServiceHandle(scmdbhandle);
return svchandle;
}
/*
This function will start running the
newly created service we just made
*/
bool loaddriver(SC_HANDLE svchandle)
{
//load the driver into the kernel, check if we were able
if(StartService(svchandle,0,NULL) == 0)
{
//check if the service is already running
if(GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
{
cout << "Error, the driver is already running" << endl;
return true;
}
//the driver couldn't be loaded into the kernel
else
{
cout << "Error, the driver could not be loaded into the kernel" <<endl;
return false;
}
}
cout << "driver successfully loaded"<<endl;
}
/*
This function will stop the driver from running
*/
bool stopdriver(SC_HANDLE svchandle)
{
SERVICE_STATUS status;
//call the function to stop the driver from running
if(ControlService(svchandle, SERVICE_CONTROL_STOP, &status) == 0)
{
cout << "stop driver, failed to unload driver" << endl;
return false;
}
cout << "stop driver, driver successfully unloaded"<<endl;
return true;
}
/*
This function will remove the driver from memory
*/
bool deletedriver(SC_HANDLE svchandle)
{
//call the function to remove the driver from memory
if(DeleteService(svchandle) == 0)
{
cout << "delete driver, failed to delete driver" << endl;
return false;
}
cout << "delete driver, driver successfully deleted"<<endl;
return true;
}
int main(int argc, char *argv[])
{
}