Skip to content

Commit

Permalink
fix: rewrite security functions
Browse files Browse the repository at this point in the history
  • Loading branch information
buptczq committed Nov 20, 2020
1 parent 73810bd commit f9de056
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 66 deletions.
69 changes: 7 additions & 62 deletions utils/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,10 @@ package utils
import (
"golang.org/x/sys/windows"
"syscall"
"unsafe"
)

var (
modadvapi32 = windows.NewLazySystemDLL("advapi32.dll")
procGetSecurityInfo = modadvapi32.NewProc("GetSecurityInfo")
)

const (
seKernelObject = 6
ownerSecurityInformation = 1
)

func getSecurityInfo(
handle windows.Handle,
objectType uint32,
securityInformation uint32,
ppsidOwner **windows.SID,
ppsidGroup **windows.SID,
ppDacl uintptr,
ppSacl uintptr,
ppSecurityDescriptor *windows.Handle,
) (err error) {
r1, _, e1 := syscall.Syscall9(
procGetSecurityInfo.Addr(),
8,
uintptr(handle),
uintptr(objectType),
uintptr(securityInformation),
uintptr(unsafe.Pointer(ppsidOwner)),
uintptr(unsafe.Pointer(ppsidGroup)),
uintptr(unsafe.Pointer(ppDacl)),
uintptr(unsafe.Pointer(ppSacl)),
uintptr(unsafe.Pointer(ppSecurityDescriptor)),
0,
)
if r1 != 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

func GetUserSID() (*windows.SID, error) {
token, err := windows.OpenCurrentProcessToken()
if err != nil {
return nil, err
}
defer token.Close()
token := windows.GetCurrentProcessToken()
user, err := token.GetTokenUser()
if err != nil {
return nil, err
Expand All @@ -63,26 +15,19 @@ func GetUserSID() (*windows.SID, error) {
}

func GetHandleSID(h windows.Handle) (*windows.SID, error) {
var sid, gid *windows.SID
var psd windows.Handle
err := getSecurityInfo(h, seKernelObject, ownerSecurityInformation, &sid, &gid, 0, 0, &psd)
defer func() {
if psd != 0 {
windows.LocalFree(psd)
}
}()
sd, err := windows.GetSecurityInfo(h, windows.SE_KERNEL_OBJECT, windows.OWNER_SECURITY_INFORMATION)
if err != nil {
return nil, err
}
sid, _, err := sd.Owner()
if err != nil {
return nil, err
}
return sid, nil
}

func GetDefaultSID() (*windows.SID, error) {
proc, err := windows.GetCurrentProcess()
if err != nil {
return nil, err
}
defer windows.CloseHandle(proc)
proc := windows.CurrentProcess()
return GetHandleSID(proc)
}

Expand Down
8 changes: 4 additions & 4 deletions versioninfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"FileVersion": {
"Major": 1,
"Minor": 1,
"Patch": 0,
"Patch": 1,
"Build": 0
},
"ProductVersion": {
"Major": 1,
"Minor": 1,
"Patch": 0,
"Patch": 1,
"Build": 0
},
"FileFlagsMask": "3f",
Expand All @@ -24,12 +24,12 @@
"FileDescription": "WinCrypt SSH Agent",
"FileVersion": "",
"InternalName": "",
"LegalCopyright": "Copyright © 2019-2020 BUPTCZQ.",
"LegalCopyright": "Copyright © 2019-2021 BUPTCZQ.",
"LegalTrademarks": "",
"OriginalFilename": "WinCryptSSHAgent.exe",
"PrivateBuild": "",
"ProductName": "WinCrypt SSH Agent",
"ProductVersion": "v1.1.0",
"ProductVersion": "v1.1.1",
"SpecialBuild": ""
},
"VarFileInfo": {
Expand Down

0 comments on commit f9de056

Please sign in to comment.