forked from zouxianyu/KernelHiddenExecute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlservices.cpp
More file actions
190 lines (145 loc) · 6.01 KB
/
dlservices.cpp
File metadata and controls
190 lines (145 loc) · 6.01 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
/*
This file is part of driver-loader
Copyright (C) 2017 @maldevel
driver-loader - Load a Windows Kernel Driver.
https://github.com/maldevel/driver-loader
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For more see the file 'LICENSE' for copying permission.
*/
#include "dlservices.h"
static SC_HANDLE scManager;
bool Services::init(void) {
scManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
if (scManager == NULL) {
return false;
}
return true;
}
SC_HANDLE Services::Open(QString service) {
if (service == NULL || scManager == NULL || service.trimmed().isEmpty() ||
service.trimmed().length() > 256) return NULL;
return OpenServiceA(scManager, service.toStdString().c_str(), SERVICE_ALL_ACCESS);
}
void Services::uninit(void) {
if (scManager == NULL) return;
CloseServiceHandle(scManager);
}
unsigned long Services::Register(QString driver, QString serviceName, QString displayName,
QString startTypeStr, QString error) {
if (driver == NULL || serviceName == NULL || scManager == NULL ||
displayName == NULL || startTypeStr == NULL || error == NULL ||
driver.trimmed().isEmpty() || serviceName.trimmed().isEmpty() ||
displayName.trimmed().isEmpty() || startTypeStr.trimmed().isEmpty() ||
error.trimmed().isEmpty() || serviceName.trimmed().length() > 256 ||
displayName.trimmed().length() > 256) return 1;
SC_HANDLE scService;
unsigned long startType = SERVICE_DEMAND_START;
unsigned long errorControl = SERVICE_ERROR_NORMAL;
//"Automatic" "Boot" "Demand" "Disabled" "System"
if (startTypeStr.trimmed().compare("Automatic", Qt::CaseSensitive) == 0) {
startType = SERVICE_AUTO_START;
} else if (startTypeStr.trimmed().compare("Boot", Qt::CaseSensitive) == 0) {
startType = SERVICE_BOOT_START;
} else if (startTypeStr.trimmed().compare("Demand", Qt::CaseSensitive) == 0) {
startType = SERVICE_DEMAND_START;
} else if (startTypeStr.trimmed().compare("Disabled", Qt::CaseSensitive) == 0) {
startType = SERVICE_DISABLED;
} else if (startTypeStr.trimmed().compare("System", Qt::CaseSensitive) == 0) {
startType = SERVICE_SYSTEM_START;
}
//"Critical" "Ignore" "Normal" "Severe"
if (error.trimmed().compare("Critical", Qt::CaseSensitive) == 0) {
errorControl = SERVICE_ERROR_CRITICAL;
} else if (error.trimmed().compare("Ignore", Qt::CaseSensitive) == 0) {
errorControl = SERVICE_ERROR_IGNORE;
} else if (error.trimmed().compare("Normal", Qt::CaseSensitive) == 0) {
errorControl = SERVICE_ERROR_NORMAL;
} else if (error.trimmed().compare("Severe", Qt::CaseSensitive) == 0) {
errorControl = SERVICE_ERROR_SEVERE;
}
if ((scService = CreateServiceA(scManager, serviceName.trimmed().toStdString().c_str(),
displayName.trimmed().toStdString().c_str(),
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
startType, errorControl,
driver.trimmed().toStdString().c_str(),
NULL, NULL, NULL, NULL, NULL)) == NULL) {
return GetLastError();
}
CloseServiceHandle(scService);
return 0;
}
unsigned long Services::Unregister(QString service) {
if (service == NULL || scManager == NULL || service.trimmed().isEmpty() ||
service.trimmed().length() > 256) return 1;
SC_HANDLE srvHandle;
unsigned long error = 0;
if ((srvHandle = Open(service)) == NULL) {
return GetLastError();
}
if ((error = Stop(srvHandle)) != 0) {
if (error != ERROR_SERVICE_NOT_ACTIVE) {
CloseServiceHandle(srvHandle);
return error;
}
}
if (DeleteService(srvHandle) == 0) {
CloseServiceHandle(srvHandle);
return GetLastError();
}
CloseServiceHandle(srvHandle);
return 0;
}
unsigned long Services::Start(SC_HANDLE service) {
if (service == NULL || scManager == NULL) return 1;
if (StartService(service, 0, NULL) == 0) {
return GetLastError();
}
return 0;
}
unsigned long Services::Start(QString service) {
if (service == NULL || scManager == NULL || service.trimmed().isEmpty() ||
service.trimmed().length() > 256) return 1;
unsigned long error = 0;
SC_HANDLE srvHandle;
if ((srvHandle = Open(service)) == NULL) {
return GetLastError();
}
if ((error = Start(srvHandle)) != 0) {
CloseServiceHandle(srvHandle);
return error;
}
CloseServiceHandle(srvHandle);
return 0;
}
unsigned long Services::Stop(SC_HANDLE service) {
if (service == NULL || scManager == NULL) return 1;
SERVICE_STATUS serviceStatus;
if (ControlService(service, SERVICE_CONTROL_STOP, &serviceStatus) == 0) {
return GetLastError();
}
return 0;
}
unsigned long Services::Stop(QString service) {
if (service == NULL || scManager == NULL || service.trimmed().isEmpty() ||
service.trimmed().length() > 256) return 1;
SC_HANDLE srvHandle;
unsigned long error = 0;
if ((srvHandle = Open(service)) == NULL) {
return GetLastError();
}
if ((error = Stop(srvHandle)) != 0) {
CloseServiceHandle(srvHandle);
return error;
}
CloseServiceHandle(srvHandle);
return 0;
}