-
Notifications
You must be signed in to change notification settings - Fork 15
/
Main.cpp
209 lines (161 loc) · 6.02 KB
/
Main.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
#include "pch.h"
#include "ObEngine.h"
thread_local HANDLE tl_hMailslot;
thread_local HANDLE tl_hUdpSocket;
SERVICE_STATUS g_MailslotStatus;
SERVICE_STATUS g_UdpSocketStatus;
HANDLE g_hMailslotStopEvent;
HANDLE g_hUdpSocketStopEvent;
SERVICE_STATUS_HANDLE g_hService;
void SetMailslotStatus(DWORD status);
void WINAPI ObMailslotMain(DWORD dwNumServicesArgs, LPTSTR* args);
DWORD WINAPI ObMailslotHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
DWORD WINAPI StartMailslotThread(PVOID);
void SetUdpSocketStatus(DWORD status);
void WINAPI ObUdpSocketMain(DWORD dwNumServicesArgs, LPTSTR* args);
DWORD WINAPI ObUdpSocketHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
DWORD WINAPI StartUdpSocketThread(PVOID);
int main() {
/*
For SERVICE_WIN32_SHARE_PROCESS services, each entry must contain the
name of a service. This name is the service name that was specified
by the CreateService function when the service was installed. For
SERVICE_WIN32_OWN_PROCESS services, the service name in the table
entry is ignored.
Because we specify multiple services in the table entry, the service
when installed must be SERVICE_WIN32_SHARE_PROCESS, otherwise only
the first service in the table entry structure will execute,
regardless of the service name as it would be ignored.
[Install via sc.exe]
sc create ObMailslot start= auto type= share binPath= "C:\Windows\ObService.exe"
sc create ObUdpSocket start= auto type= share binPath= "C:\Windows\ObService.exe"
*/
WCHAR MailslotSvc[] = L"ObMailslot";
WCHAR UdpSocketSvc[] = L"ObUdpSocket";
const SERVICE_TABLE_ENTRY table[] = {
{ MailslotSvc, ObMailslotMain },
{ UdpSocketSvc, ObUdpSocketMain },
{nullptr, nullptr}
};
if (!::StartServiceCtrlDispatcher(table))
return 1;
return 0;
}
void SetMailslotStatus(DWORD status) {
g_MailslotStatus.dwCurrentState = status;
g_MailslotStatus.dwControlsAccepted =
status == SERVICE_RUNNING ? SERVICE_ACCEPT_STOP : 0;
::SetServiceStatus(g_hService, &g_MailslotStatus);
}
void WINAPI ObMailslotMain(DWORD dwNumServicesArgs, LPTSTR* args) {
g_MailslotStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_MailslotStatus.dwWaitHint = 500;
// Setup security for a mailslot to recv. messages
bool error = true;
do {
// Because this is a "share" type process, name must match the name
// in the service table entry.
g_hService = ::RegisterServiceCtrlHandlerEx(L"ObMailslot", ObMailslotHandler, nullptr);
if (!g_hService)
break;
g_hMailslotStopEvent = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (!g_hMailslotStopEvent)
break;
SetMailslotStatus(SERVICE_START_PENDING);
// Start mailslot server in a new thread
tl_hMailslot = ::CreateThread(nullptr, 0, StartMailslotThread, nullptr, 0, nullptr);
if (tl_hMailslot == NULL)
break;
// No errors detected
error = false;
} while (false);
// Check if errors were encountered above
if (error) {
SetMailslotStatus(SERVICE_STOPPED);
return;
}
SetMailslotStatus(SERVICE_RUNNING);
// Loop until a service stop request is received
while (::WaitForSingleObject(g_hMailslotStopEvent, 1000) == WAIT_TIMEOUT) {
// Verify handler(s) are still active
if (::WaitForSingleObject(tl_hMailslot, 500) != WAIT_TIMEOUT) {
break;
}
}
SetMailslotStatus(SERVICE_STOPPED);
::CloseHandle(g_hMailslotStopEvent);
::CloseHandle(tl_hMailslot);
}
DWORD WINAPI ObMailslotHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {
switch (dwControl) {
case SERVICE_CONTROL_STOP:
SetMailslotStatus(SERVICE_STOP_PENDING);
::SetEvent(g_hMailslotStopEvent);
break;
}
return 0;
}
DWORD WINAPI StartMailslotThread(PVOID) {
//Helpers::DbgPrint(L"[Info][ObMailslot] Starting Mailslot thread.");
ObEngine::StartMailslotServer(std::wstring(L"obligato"));
return 1;
}
void SetUdpSocketStatus(DWORD status) {
g_UdpSocketStatus.dwCurrentState = status;
g_UdpSocketStatus.dwControlsAccepted =
status == SERVICE_RUNNING ? SERVICE_ACCEPT_STOP : 0;
::SetServiceStatus(g_hService, &g_UdpSocketStatus);
}
void WINAPI ObUdpSocketMain(DWORD dwNumServicesArgs, LPTSTR* args) {
g_UdpSocketStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_UdpSocketStatus.dwWaitHint = 500;
// Setup UDP socket to recv. messages
bool error = true;
do {
// Because this is a "share" type process, name must match the name
// in the service table entry.
g_hService = ::RegisterServiceCtrlHandlerEx(L"ObUdpSocket", ObUdpSocketHandler, nullptr);
if (!g_hService)
break;
g_hUdpSocketStopEvent = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (!g_hUdpSocketStopEvent)
break;
SetUdpSocketStatus(SERVICE_START_PENDING);
// Start UDP socket server in a new thread
tl_hUdpSocket = ::CreateThread(nullptr, 0, StartUdpSocketThread, nullptr, 0, nullptr);
if (tl_hUdpSocket == NULL)
break;
// No errors detected
error = false;
} while (false);
// Check if errors were encountered above
if (error) {
SetUdpSocketStatus(SERVICE_STOPPED);
return;
}
SetUdpSocketStatus(SERVICE_RUNNING);
// Loop until a service stop request is received
while (::WaitForSingleObject(g_hUdpSocketStopEvent, 1000) == WAIT_TIMEOUT) {
// Verify handler(s) are still active
if (::WaitForSingleObject(tl_hUdpSocket, 500) != WAIT_TIMEOUT) {
break;
}
}
SetUdpSocketStatus(SERVICE_STOPPED);
::CloseHandle(g_hUdpSocketStopEvent);
::CloseHandle(tl_hUdpSocket);
}
DWORD WINAPI ObUdpSocketHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {
switch (dwControl) {
case SERVICE_CONTROL_STOP:
SetUdpSocketStatus(SERVICE_STOP_PENDING);
::SetEvent(g_hUdpSocketStopEvent);
break;
}
return 0;
}
DWORD WINAPI StartUdpSocketThread(PVOID) {
//Helpers::DbgPrint(L"[Info][ObUdpSocket] Starting UDP socket thread.");
ObEngine::StartUdpSocketServer(std::wstring(L"0.0.0.0"), 49153);
return 1;
}