-
Notifications
You must be signed in to change notification settings - Fork 6
/
service_windows.go
446 lines (396 loc) · 10.9 KB
/
service_windows.go
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package service
import (
"syscall"
"unicode/utf16"
"fmt"
"unsafe"
)
func newService(name, displayName, description string) (*windowsService, error) {
return &windowsService{
name: name,
displayName: displayName,
description: description,
}, nil
}
type windowsService struct {
name, displayName, description string
}
func (ws *windowsService) Install() error {
pathToBinary, err := getModuleFileName()
if err != nil {
return err
}
scmManagerHandle, err := openSCManager()
if err != nil {
return err
}
defer closeServiceHandle(scmManagerHandle)
serviceHandle, err := createService(scmManagerHandle, ws.name, ws.displayName, pathToBinary)
if err != nil {
return err
}
defer closeServiceHandle(serviceHandle)
err = changeServiceDescription(serviceHandle, ws.description)
if err != nil {
return err
}
regKey, err := regCreateKey(`SYSTEM\CurrentControlSet\Services\Eventlog\Application\` + ws.name)
if err != nil {
return err
}
defer regCloseKey(regKey)
regSetKeyValue(regKey, "EventMessageFile", `%SystemRoot%\System32\EventCreate.exe`)
if err != nil {
return err
}
regSetKeyValue(regKey, "CustomSource", uint32(1))
if err != nil {
return err
}
regSetKeyValue(regKey, "TypesSupported", uint32(7))
if err != nil {
return err
}
return nil
}
func (ws *windowsService) Remove() error {
scmManagerHandle, err := openSCManager()
if err != nil {
return err
}
defer closeServiceHandle(scmManagerHandle)
serviceHandle, err := openService(scmManagerHandle, ws.name)
if err != nil {
return err
}
defer closeServiceHandle(serviceHandle)
err = deleteService(serviceHandle)
if err != nil {
return err
}
err = regDeleteKey(`SYSTEM\CurrentControlSet\Services\Eventlog\Application\` + ws.name)
if err != nil {
return err
}
return nil
}
func (ws *windowsService) Run(onStart, onStop func() error) error {
return runService(ws.name, onStart, onStop)
}
func (ws *windowsService) LogError(format string, a ...interface{}) error {
return writeToEventLog(ws.name, fmt.Sprintf(format, a ...), levelError)
}
func (ws *windowsService) LogWarning(format string, a ...interface{}) error {
return writeToEventLog(ws.name, fmt.Sprintf(format, a ...), levelWarning)
}
func (ws *windowsService) LogInfo(format string, a ...interface{}) error {
return writeToEventLog(ws.name, fmt.Sprintf(format, a ...), levelInfo)
}
func getExePath() (exePath string, err error) {
return getModuleFileName()
}
var (
advapi = syscall.MustLoadDLL("advapi32.dll")
kernel = syscall.MustLoadDLL("kernel32.dll")
//advapi32.dll
createServiceProc = advapi.MustFindProc("CreateServiceW")
openServiceProc = advapi.MustFindProc("OpenServiceW")
deleteServiceProc = advapi.MustFindProc("DeleteService")
closeServiceHandleProc = advapi.MustFindProc("CloseServiceHandle")
openEventLogProc = advapi.MustFindProc("OpenEventLogW")
registerEventSourceProc = advapi.MustFindProc("RegisterEventSourceW")
deregisterEventSourceProc = advapi.MustFindProc("DeregisterEventSource")
reportEventProc = advapi.MustFindProc("ReportEventW")
openSCManagerProc = advapi.MustFindProc("OpenSCManagerW")
changeServiceConfig2Proc = advapi.MustFindProc("ChangeServiceConfig2W")
// Registry
regCloseKeyProc = advapi.MustFindProc("RegCloseKey")
regSetKeyValueExProc = advapi.MustFindProc("RegSetValueExW")
regCreateKeyExProc = advapi.MustFindProc("RegCreateKeyExW")
regDeleteKeyProc = advapi.MustFindProc("RegDeleteKeyW")
// kernel32.dll
getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW")
)
const (
_STANDARD_RIGHTS_REQUIRED = 0x000F0000
_SERVICE_WIN32_OWN_PROCESS = 0x00000010
_SERVICE_DEMAND_START = 0x00000003
_SERVICE_ERROR_NORMAL = 0x00000001
)
const (
_SC_MANAGER_CONNECT = 0x0001
_SC_MANAGER_CREATE_SERVICE = 0x0002
_SC_MANAGER_ENUMERATE_SERVICE = 0x0004
_SC_MANAGER_LOCK = 0x0008
_SC_MANAGER_QUERY_LOCK_STATUS = 0x0010
_SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020
_SC_MANAGER_ALL_ACCESS = (_STANDARD_RIGHTS_REQUIRED |
_SC_MANAGER_CONNECT |
_SC_MANAGER_CREATE_SERVICE |
_SC_MANAGER_ENUMERATE_SERVICE |
_SC_MANAGER_LOCK |
_SC_MANAGER_QUERY_LOCK_STATUS |
_SC_MANAGER_MODIFY_BOOT_CONFIG)
)
const (
_SERVICE_QUERY_CONFIG = 0x0001
_SERVICE_CHANGE_CONFIG = 0x0002
_SERVICE_QUERY_STATUS = 0x0004
_SERVICE_ENUMERATE_DEPENDENTS = 0x0008
_SERVICE_START = 0x0010
_SERVICE_STOP = 0x0020
_SERVICE_PAUSE_CONTINUE = 0x0040
_SERVICE_INTERROGATE = 0x0080
_SERVICE_USER_DEFINED_CONTROL = 0x0100
_SERVICE_ALL_ACCESS = (_STANDARD_RIGHTS_REQUIRED |
_SERVICE_QUERY_CONFIG |
_SERVICE_CHANGE_CONFIG |
_SERVICE_QUERY_STATUS |
_SERVICE_ENUMERATE_DEPENDENTS |
_SERVICE_START |
_SERVICE_STOP |
_SERVICE_PAUSE_CONTINUE |
_SERVICE_INTERROGATE |
_SERVICE_USER_DEFINED_CONTROL)
)
type eventLevel uint32
const (
levelError eventLevel = 0x0001
levelWarning eventLevel = 0x0002
levelInfo eventLevel = 0x0004
)
func writeToEventLog(title, text string, level eventLevel) error {
eventSource, err := registerEventSource(title)
if err != nil {
return err
}
err = reportEvent(eventSource, title, text, level)
if err != nil {
return err
}
err = deregisterEventSource(eventSource)
if err != nil {
return err
}
return nil
}
func registerEventSource(title string) (syscall.Handle, error) {
r0, _, e1 := registerEventSourceProc.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))))
if r0 == 0 {
return syscall.Handle(0), e1
}
return syscall.Handle(r0), nil
}
func deregisterEventSource(eventSrouce syscall.Handle) error {
r0, _, e1 := deregisterEventSourceProc.Call(uintptr(eventSrouce))
if r0 == 0 {
return e1
}
return nil
}
/*
const (
_EVENTLOG_ERROR_TYPE = 0x0001
_EVENTLOG_WARNING_TYPE = 0x0002
_EVENTLOG_INFORMATION_TYPE = 0x0004
)
*/
func reportEvent(eventSource syscall.Handle, title, text string, level eventLevel) error {
msg := [...]uintptr{
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))}
r0, _, e1 := reportEventProc.Call(
uintptr(eventSource),
uintptr(level), //type
uintptr(0), //category
uintptr(1), //eventID
uintptr(0),
uintptr(2),
uintptr(0),
uintptr(unsafe.Pointer(&msg[0])),
0)
if r0 == 0 {
return e1
}
return nil
}
func closeServiceHandle(service syscall.Handle) error {
r0, _, e1 := closeServiceHandleProc.Call(uintptr(service))
if r0 == 0 {
return e1
}
return nil
}
func createService(scManager syscall.Handle, serviceName, serviceDisplayName, pathToBinary string) (syscall.Handle, error) {
r0, _, e1 := createServiceProc.Call(
uintptr(scManager),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(serviceName))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(serviceDisplayName))),
uintptr(uint32(_SERVICE_ALL_ACCESS)),
uintptr(uint32(_SERVICE_WIN32_OWN_PROCESS)),
uintptr(uint32(_SERVICE_DEMAND_START)),
uintptr(uint32(_SERVICE_ERROR_NORMAL)),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(pathToBinary))),
0, 0, 0, 0, 0) //+4 optional params not here
if r0 == 0 {
return syscall.Handle(0), e1
}
return syscall.Handle(r0), nil
}
func deleteService(serviceHandle syscall.Handle) error {
r0, _, e1 := deleteServiceProc.Call(uintptr(serviceHandle))
if r0 == 0 {
return e1
}
return nil
}
func openService(scManager syscall.Handle, serviceName string) (syscall.Handle, error) {
r0, _, e1 := openServiceProc.Call(uintptr(scManager), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(serviceName))), uintptr(uint32(_SC_MANAGER_ALL_ACCESS)))
if r0 == 0 {
return syscall.Handle(0), e1
}
return syscall.Handle(r0), nil
}
func getModuleFileName() (string, error) {
var n uint32
b := make([]uint16, syscall.MAX_PATH)
size := uint32(len(b))
r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
n = uint32(r0)
if n == 0 {
return "", e1
}
return string(utf16.Decode(b[0:n])), nil
}
func openSCManager() (syscall.Handle, error) {
r0, _, e1 := openSCManagerProc.Call(uintptr(0), uintptr(0), uintptr(uint32(_SC_MANAGER_ALL_ACCESS)))
if r0 == 0 {
return syscall.Handle(0), e1
}
return syscall.Handle(r0), nil
}
const (
_SERVICE_CONFIG_DESCRIPTION = 1
)
func changeServiceDescription(h syscall.Handle, desc string) error {
msg := &struct {
desc uintptr
}{
desc: uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(desc))),
}
r0, _, e1 := changeServiceConfig2Proc.Call(
uintptr(h),
uintptr(_SERVICE_CONFIG_DESCRIPTION),
uintptr(unsafe.Pointer(msg)),
)
if r0 == 0 {
return e1
}
return nil
}
// registry functions
/*
LONG WINAPI RegCloseKey(
__in HKEY hKey
);
LONG WINAPI RegSetValueExW(
__in HKEY hKey,
__in_opt LPCTSTR lpValueName,
__reserved DWORD Reserved,
__in DWORD dwType,
__in const BYTE *lpData,
__in DWORD cbData
);
LONG WINAPI RegCreateKeyExW(
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__reserved DWORD Reserved,
__in_opt LPTSTR lpClass,
__in DWORD dwOptions,
__in REGSAM samDesired,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__out PHKEY phkResult,
__out_opt LPDWORD lpdwDisposition
);
LONG WINAPI RegDeleteKeyExW(
__in HKEY hKey,
__in LPCTSTR lpSubKey,
__in REGSAM samDesired,
__reserved DWORD Reserved
);
*/
func StringToUTF16PtrLen(s string) (ptr uintptr, l uintptr) {
u := syscall.StringToUTF16(s)
l = uintptr(len(u) * 2) //size in uint8, length of uint16
ptr = uintptr(unsafe.Pointer(&u[0]))
return
}
const (
_HKEY_LOCAL_MACHINE = 0x80000002
_REG_SZ = 1
_REG_DWORD = 4
_KEY_ALL_ACCESS = 0xF003F
)
func regCloseKey(h syscall.Handle) error {
r0, _, e1 := regCloseKeyProc.Call(
uintptr(h),
)
if r0 != 0 {
return e1
}
return nil
}
func regSetKeyValue(h syscall.Handle, keyName string, data interface{}) error {
var dataPtr, dataLen, dataType uintptr
switch v := data.(type) {
case uint32:
dataPtr, dataLen = uintptr(unsafe.Pointer(&v)), 4
dataType = _REG_DWORD
case string:
// The comment on MSDN regarding escaping back-slashes, are c-lang specific.
// The API just takes a normal NUL terminated string, no special escaping required.
dataPtr, dataLen = StringToUTF16PtrLen(v)
dataType = _REG_SZ
}
r0, _, e1 := regSetKeyValueExProc.Call(
uintptr(h),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(keyName))),
0,
dataType,
dataPtr,
dataLen,
)
if r0 != 0 {
return e1
}
return nil
}
func regCreateKey(keyName string) (h syscall.Handle, err error) {
r0, _, e1 := regCreateKeyExProc.Call(
uintptr(_HKEY_LOCAL_MACHINE),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(keyName))),
0,
0, //class
0, //no options
uintptr(_KEY_ALL_ACCESS),
0, //no security
uintptr(unsafe.Pointer(&h)),
0, //can return if created or opened
)
if r0 != 0 {
err = e1
return
}
return
}
func regDeleteKey(keyName string) error {
r0, _, e1 := regDeleteKeyProc.Call(
uintptr(_HKEY_LOCAL_MACHINE),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(keyName))),
)
if r0 != 0 {
return e1
}
return nil
}