Repository navigation
Expand file tree
/
Copy pathService.cpp
More file actions
95 lines (77 loc) · 2.97 KB
/
Copy pathService.cpp
File metadata and controls
95 lines (77 loc) · 2.97 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
#include "Service.h"
Service::Service(LPCSTR serviceName, LPCSTR driverPath)
{
hSCM = OpenSCManagerA(".", NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCM == nullptr) {
printf("[-] Failed to get handle to SCM. (Error Code: %i)\n", GetLastError());
exit(0);
}
this->hSCM = hSCM;
this->serviceName = serviceName == 0 ? "PROCEXP152" : serviceName;
this->driverPath = driverPath == 0 ? "C:\\temp\\PROCEXP152.sys" : driverPath;
this->hService = nullptr;
this->hService = OpenServiceA(hSCM, this->serviceName, SERVICE_START|SERVICE_CHANGE_CONFIG);
}
bool Service::create_service() {
if (this->hService != nullptr) {
printf("[!] Service allready exists\n");
return this->start_service();
}
this->hService = CreateServiceA(this->hSCM, this->serviceName, this->serviceName, SC_MANAGER_CREATE_SERVICE|SERVICE_START|SERVICE_CHANGE_CONFIG, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, this->driverPath, NULL, NULL, NULL, NULL, NULL);
DWORD errCode = GetLastError();
if (this->hService == nullptr) {
printf("[-] Failed To Create Service. (Error Code: %lu)\n", GetLastError());
}
else if (errCode == 1073) { // 1073 == ERROR_SERVICE_EXISTS
printf("[!] Service allready exists\n");
return this->start_service();
}
else if (errCode == 0) {
printf("[+] Service Created Successfully\n");
return this->start_service();
}
else if (errCode != 0) {
printf("[!] Hit last else if in create service. (Error Code: %lu)\n", GetLastError());
}
return FALSE;
}
bool Service::start_service() {
BOOL OK = StartServiceA(this->hService, 0, NULL);
if (!OK)
{
DWORD errCode = GetLastError();
// 183 == ERROR_ALLREADY_EXISTS, 1056 = ERROR_SERVICE_ALREADY_RUNNING
if (errCode == 183 || errCode == 1056) {
printf("[!] Service allready running\n");
return TRUE;
}
else if(errCode == 1058) { // 1058 == ERROR_SERVICE_DISABLED
printf("[!] Service is in disabled state\n");
if(this->enable_service()) return TRUE;
}
else {
printf("[-] Failed to start Service. (Error Code: %lu)\n", errCode);
return FALSE;
}
}
printf("[+] Service started successfully\n");
return TRUE;
}
// We only get here when the service marked for deletion, Windows disables the service
bool Service::enable_service() {
BOOL OK = ChangeServiceConfigA(this->hService, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
// When errCode == 1072 (ERROR_SERVICE_MARKED_FOR_DELETE) it will be start anyway, but we will need this hack everytime unless a new service will be created
if (!OK && GetLastError() != 1072) {
printf("[-] Failed to change service config. (Error Code: %lu)", GetLastError());
printf("[!] Try to install the service again with different name or use sc.exe to start the service\n");
return FALSE;
}
printf("[+] Service enabled once again\n");
return TRUE;
}
Service::~Service()
{
if (this->hService != nullptr) CloseServiceHandle(this->hService);
if (this->hSCM != nullptr) CloseServiceHandle(this->hSCM);
}