-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check if credentials files are protectd on windows
we have already been checking if permissions for credentials files are appropreate or not on linux/macos, but haven't for windows. windows doesn't support 0600 style permissions but rather it has ACL mechanism which is slightly complicated. now we are checking ACL for credential files and able to determine if the files are protected properly.
- Loading branch information
Takashi Oguma
committed
Apr 27, 2017
1 parent
132fdf7
commit 622dea3
Showing
8 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build !windows | ||
|
||
package lib | ||
|
||
import "os" | ||
|
||
func IsFilePermissionTooOpen(path string) (bool, error) { | ||
s, err := os.Stat(path) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if s.Mode()&077 != 0 { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// +build windows | ||
|
||
package lib | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bearmini/go-acl/api" | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
func IsFilePermissionTooOpen(path string) (bool, error) { | ||
var ( | ||
ownerSID *windows.SID | ||
dacl *api.ACL | ||
secDesc windows.Handle | ||
) | ||
err := api.GetNamedSecurityInfo( | ||
path, | ||
api.SE_FILE_OBJECT, | ||
api.OWNER_SECURITY_INFORMATION|api.DACL_SECURITY_INFORMATION, | ||
&ownerSID, | ||
nil, | ||
&dacl, | ||
nil, | ||
&secDesc, | ||
) | ||
defer windows.LocalFree(secDesc) | ||
if err != nil { | ||
// This `err` always contains "The operation completed successfully" | ||
// So we create a new error instance | ||
return false, fmt.Errorf("unable to get security info for the file: %s", path) | ||
} | ||
|
||
currProcSID, err := GetCurrentProcessSID() | ||
if err != nil { | ||
return false, err | ||
} | ||
//fmt.Println(sidToString(currProcSID)) | ||
|
||
//fmt.Printf("dacl == %+v\n", dacl) | ||
aces := dacl.GetACEList() | ||
//fmt.Printf("ACEs == %+v\n", aces) | ||
for _, ace := range aces { | ||
switch ace.(type) { | ||
case *api.AccessAllowedACE: | ||
// ok to have this if it's sid == mine | ||
default: | ||
return true, nil | ||
} | ||
//fmt.Println(sidToString(ace.GetSID())) | ||
if !windows.EqualSid(ace.GetSID(), currProcSID) { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
func GetCurrentProcessSID() (*windows.SID, error) { | ||
token, err := windows.OpenCurrentProcessToken() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer token.Close() | ||
|
||
tu, err := token.GetTokenUser() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tu.User.Sid, nil | ||
} | ||
|
||
func sidToString(sid *windows.SID) string { | ||
str, err := sid.String() | ||
if err != nil { | ||
return "<err: " + err.Error() | ||
} | ||
return str | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters