Skip to content

Commit

Permalink
Merge pull request #248 from patilsanty/com_initsecurity
Browse files Browse the repository at this point in the history
refs added code to com InitializeSecurity
  • Loading branch information
jacobsantos authored Aug 4, 2023
2 parents 5ed15d5 + 8406bfd commit de26f2b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions com.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var (
procCoInitialize = modole32.NewProc("CoInitialize")
procCoInitializeEx = modole32.NewProc("CoInitializeEx")
procCoInitializeSecurity = modole32.NewProc("CoInitializeSecurity")
procCoUninitialize = modole32.NewProc("CoUninitialize")
procCoCreateInstance = modole32.NewProc("CoCreateInstance")
procCoTaskMemFree = modole32.NewProc("CoTaskMemFree")
Expand All @@ -37,6 +38,9 @@ var (
procDispatchMessageW = moduser32.NewProc("DispatchMessageW")
)

// This is to enable calling COM Security initialization multiple times
var bSecurityInit bool = false

// coInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
Expand Down Expand Up @@ -68,6 +72,35 @@ func coInitializeEx(coinit uint32) (err error) {
return
}

// coInitializeSecurity: Registers security and sets the default security values
// for the process.
func coInitializeSecurity(cAuthSvc int32,
dwAuthnLevel uint32,
dwImpLevel uint32,
dwCapabilities uint32) (err error) {
// Check COM Security initialization has done previously
if !bSecurityInit {
// https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializesecurity
hr, _, _ := procCoInitializeSecurity.Call(
uintptr(0), // Allow *all* VSS writers to communicate back!
uintptr(cAuthSvc), // Default COM authentication service
uintptr(0), // Default COM authorization service
uintptr(0), // Reserved parameter
uintptr(dwAuthnLevel), // Strongest COM authentication level
uintptr(dwImpLevel), // Minimal impersonation abilities
uintptr(0), // Default COM authentication settings
uintptr(dwCapabilities), // Cloaking
uintptr(0)) // eserved parameter
if hr != 0 {
err = NewError(hr)
} else {
// COM Security initialization done make global flag true.
bSecurityInit = true
}
}
return
}

// CoInitialize initializes COM library on current thread.
//
// MSDN documentation suggests that this function should not be called. Call
Expand Down Expand Up @@ -96,6 +129,15 @@ func CoUninitialize() {
procCoUninitialize.Call()
}

// CoInitializeSecurity: Registers security and sets the default security values
// for the process.
func CoInitializeSecurity(cAuthSvc int32,
dwAuthnLevel uint32,
dwImpLevel uint32,
dwCapabilities uint32) (err error) {
return coInitializeSecurity(cAuthSvc, dwAuthnLevel, dwImpLevel, dwCapabilities)
}

// CoTaskMemFree frees memory pointer.
func CoTaskMemFree(memptr uintptr) {
procCoTaskMemFree.Call(memptr)
Expand Down

0 comments on commit de26f2b

Please sign in to comment.