Skip to content

Instantly share code, notes, and snippets.

@micahyoung
Last active March 19, 2024 20:47
Show Gist options
  • Save micahyoung/4163bbe0195a18850706e7f34cef3c01 to your computer and use it in GitHub Desktop.
Save micahyoung/4163bbe0195a18850706e7f34cef3c01 to your computer and use it in GitHub Desktop.
Example of os.Chown-equivalent for Golang Windows
module go-windows-chown
go 1.15
require golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
//+build windows
package main
import (
"flag"
"fmt"
"golang.org/x/sys/windows"
"log"
)
func main() {
outputFilePathPtr := flag.String("output-file", "", "output file")
outputSIDPtr := flag.String("output-sid", "", "output SID")
flag.Parse()
if err := run(*outputFilePathPtr, *outputSIDPtr); err != nil {
log.Fatal(err)
}
}
func run(outputFilePath, outputSIDStr string) error {
// convert SID string to struct
outputSecurityIdentifier, err := windows.StringToSid(outputSIDStr)
if err != nil {
return err
}
// write a owner SIDs to the file's security descriptor
err = windows.SetNamedSecurityInfo(
outputFilePath,
windows.SE_FILE_OBJECT,
windows.OWNER_SECURITY_INFORMATION,
outputSecurityIdentifier,
nil,
nil,
nil,
)
if err != nil {
return err
}
// read security descriptor (owner SIDs only)
readSecurityDescriptor, err := windows.GetNamedSecurityInfo(
outputFilePath,
windows.SE_FILE_OBJECT,
windows.OWNER_SECURITY_INFORMATION)
if err != nil {
return err
}
fmt.Printf("file: %s\n", outputFilePath)
fmt.Printf(" SDDL: %s\n", readSecurityDescriptor.String())
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment