Skip to content

Commit 111b910

Browse files
author
ben
committed
add some SID functions, refactor
1 parent d94a2d7 commit 111b910

5 files changed

+518
-25
lines changed

advapi32.go

+73-24
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,27 @@ var (
1515
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
1616

1717
// procRegSetKeyValue = modadvapi32.NewProc("RegSetKeyValueW")
18-
procRegSetValueEx = modadvapi32.NewProc("RegSetValueExW")
19-
procRegDeleteKeyValue = modadvapi32.NewProc("RegDeleteKeyValueW")
20-
procRegDeleteValue = modadvapi32.NewProc("RegDeleteValueW")
21-
procRegDeleteTree = modadvapi32.NewProc("RegDeleteTreeW")
22-
procOpenEventLog = modadvapi32.NewProc("OpenEventLogW")
23-
procReadEventLog = modadvapi32.NewProc("ReadEventLogW")
24-
procCloseEventLog = modadvapi32.NewProc("CloseEventLog")
25-
procCloseServiceHandle = modadvapi32.NewProc("CloseServiceHandle")
26-
procControlService = modadvapi32.NewProc("ControlService")
27-
procControlTrace = modadvapi32.NewProc("ControlTraceW")
28-
procOpenEventLog = modadvapi32.NewProc("OpenEventLogW")
29-
procOpenSCManager = modadvapi32.NewProc("OpenSCManagerW")
30-
procOpenService = modadvapi32.NewProc("OpenServiceW")
31-
procReadEventLog = modadvapi32.NewProc("ReadEventLogW")
32-
procRegCloseKey = modadvapi32.NewProc("RegCloseKey")
33-
procRegCreateKeyEx = modadvapi32.NewProc("RegCreateKeyExW")
34-
procRegEnumKeyEx = modadvapi32.NewProc("RegEnumKeyExW")
35-
procRegGetValue = modadvapi32.NewProc("RegGetValueW")
36-
procRegOpenKeyEx = modadvapi32.NewProc("RegOpenKeyExW")
37-
procRegSetValueEx = modadvapi32.NewProc("RegSetValueExW")
38-
procStartService = modadvapi32.NewProc("StartServiceW")
39-
procStartTrace = modadvapi32.NewProc("StartTraceW")
18+
procRegSetValueEx = modadvapi32.NewProc("RegSetValueExW")
19+
procRegDeleteKeyValue = modadvapi32.NewProc("RegDeleteKeyValueW")
20+
procRegDeleteValue = modadvapi32.NewProc("RegDeleteValueW")
21+
procRegDeleteTree = modadvapi32.NewProc("RegDeleteTreeW")
22+
procOpenEventLog = modadvapi32.NewProc("OpenEventLogW")
23+
procReadEventLog = modadvapi32.NewProc("ReadEventLogW")
24+
procCloseEventLog = modadvapi32.NewProc("CloseEventLog")
25+
procCloseServiceHandle = modadvapi32.NewProc("CloseServiceHandle")
26+
procControlService = modadvapi32.NewProc("ControlService")
27+
procControlTrace = modadvapi32.NewProc("ControlTraceW")
28+
procOpenSCManager = modadvapi32.NewProc("OpenSCManagerW")
29+
procOpenService = modadvapi32.NewProc("OpenServiceW")
30+
procRegCloseKey = modadvapi32.NewProc("RegCloseKey")
31+
procRegCreateKeyEx = modadvapi32.NewProc("RegCreateKeyExW")
32+
procRegEnumKeyEx = modadvapi32.NewProc("RegEnumKeyExW")
33+
procRegGetValue = modadvapi32.NewProc("RegGetValueW")
34+
procRegOpenKeyEx = modadvapi32.NewProc("RegOpenKeyExW")
35+
procStartService = modadvapi32.NewProc("StartServiceW")
36+
procStartTrace = modadvapi32.NewProc("StartTraceW")
37+
procInitializeSecurityDescriptor = modadvapi32.NewProc("InitializeSecurityDescriptor")
38+
procSetSecurityDescriptorDacl = modadvapi32.NewProc("SetSecurityDescriptorDacl")
4039
)
4140

4241
var (
@@ -396,6 +395,7 @@ func ControlService(hService HANDLE, dwControl uint32, lpServiceStatus *SERVICE_
396395
}
397396

398397
func ControlTrace(hTrace TRACEHANDLE, lpSessionName string, props *EVENT_TRACE_PROPERTIES, dwControl uint32) (success bool, e error) {
398+
399399
ret, _, _ := procControlTrace.Call(
400400
uintptr(unsafe.Pointer(hTrace)),
401401
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpSessionName))),
@@ -405,11 +405,12 @@ func ControlTrace(hTrace TRACEHANDLE, lpSessionName string, props *EVENT_TRACE_P
405405
if ret == ERROR_SUCCESS {
406406
return true, nil
407407
}
408-
e = errors.New(fmt.Sprintf("ControlTrace: Error: 0x%x", ret))
408+
e = errors.New(fmt.Sprintf("error: 0x%x", ret))
409409
return
410410
}
411411

412412
func StartTrace(lpSessionName string, props *EVENT_TRACE_PROPERTIES) (hTrace TRACEHANDLE, e error) {
413+
413414
ret, _, _ := procStartTrace.Call(
414415
uintptr(unsafe.Pointer(&hTrace)),
415416
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpSessionName))),
@@ -418,6 +419,54 @@ func StartTrace(lpSessionName string, props *EVENT_TRACE_PROPERTIES) (hTrace TRA
418419
if ret == ERROR_SUCCESS {
419420
return
420421
}
421-
e = errors.New(fmt.Sprintf("StartTrace: Error: 0x%x", ret))
422+
e = errors.New(fmt.Sprintf("error: 0x%x", ret))
423+
return
424+
}
425+
426+
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa378863(v=vs.85).aspx
427+
func InitializeSecurityDescriptor(rev uint16) (pSecurityDescriptor *SECURITY_DESCRIPTOR, e error) {
428+
429+
pSecurityDescriptor = &SECURITY_DESCRIPTOR{}
430+
431+
ret, _, _ := procInitializeSecurityDescriptor.Call(
432+
uintptr(unsafe.Pointer(pSecurityDescriptor)),
433+
uintptr(rev),
434+
)
435+
436+
if ret != 0 {
437+
return
438+
}
439+
e = syscall.GetLastError()
440+
return
441+
}
442+
443+
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa379583(v=vs.85).aspx
444+
func SetSecurityDescriptorDacl(pSecurityDescriptor *SECURITY_DESCRIPTOR, pDacl *ACL) (e error) {
445+
446+
if pSecurityDescriptor == nil {
447+
return errors.New("null descriptor")
448+
}
449+
450+
var ret uintptr
451+
if pDacl == nil {
452+
ret, _, _ = procSetSecurityDescriptorDacl.Call(
453+
uintptr(unsafe.Pointer(pSecurityDescriptor)),
454+
uintptr(0), // DaclPresent
455+
uintptr(0), // pDacl
456+
uintptr(1), // DaclDefaulted
457+
)
458+
} else {
459+
ret, _, _ = procSetSecurityDescriptorDacl.Call(
460+
uintptr(unsafe.Pointer(pSecurityDescriptor)),
461+
uintptr(1), // DaclPresent
462+
uintptr(unsafe.Pointer(pDacl)),
463+
uintptr(0), //DaclDefaulted
464+
)
465+
}
466+
467+
if ret != 0 {
468+
return
469+
}
470+
e = syscall.GetLastError()
422471
return
423472
}

0 commit comments

Comments
 (0)