-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_attributes_windows.go
281 lines (220 loc) · 7.74 KB
/
file_attributes_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package attributes
import (
"fmt"
"runtime"
"syscall"
"unicode/utf16"
"unsafe"
)
var (
advapiLib, _ = syscall.LoadLibrary("advapi32.dll")
setFileSecurity, _ = syscall.GetProcAddress(advapiLib, "SetFileSecurityW")
getNamedSecurityInfo, _ = syscall.GetProcAddress(advapiLib, "GetNamedSecurityInfoW")
convertSidToStringSid, _ = syscall.GetProcAddress(advapiLib, "ConvertSidToStringSidW")
convertSecurityDescriptorToStringSecurityDescriptor, _ = syscall.GetProcAddress(advapiLib, "ConvertSecurityDescriptorToStringSecurityDescriptorW")
convertStringSecurityDescriptorToSecurityDescriptor, _ = syscall.GetProcAddress(advapiLib, "ConvertStringSecurityDescriptorToSecurityDescriptorW")
kernel32Lib, _ = syscall.LoadLibrary("Kernel32.dll")
localFree, _ = syscall.GetProcAddress(kernel32Lib, "LocalFree")
)
const (
fileSecurityInfo uint32 = 1
ownerSecurityInformation uint32 = 1
groupSecurityInformation uint32 = 2
daclSecurityInformation uint32 = 4
attributeSecurityInformation uint32 = 32
desiredSecurityInformation = ownerSecurityInformation | groupSecurityInformation | daclSecurityInformation | attributeSecurityInformation
sddlRevision uint32 = 1
securityDescriptorTag = "securityDescriptor"
ownerTag = "owner"
groupTag = "group"
daclTag = "dacl"
)
type WidowsFileAttributes struct {
windowsFileName []uint16
}
func NewWidowsFileAttributes(filePath string) (*WidowsFileAttributes, error) {
if runtime.GOOS != "windows" {
return nil, fmt.Errorf("WidowsFileAttributes only works on Windows")
}
windowsFileName, err := syscall.UTF16FromString(filePath)
if err != nil {
return nil, fmt.Errorf("failed to convert %s to Windows equivalent", filePath)
}
return &WidowsFileAttributes{windowsFileName: windowsFileName}, nil
}
func (attributes *WidowsFileAttributes) GetSecurityDescriptor() (map[string]string, error) {
securityInfoMap, err := attributes.securityInfo()
if err != nil {
return nil, err
}
securityDescriptor := securityInfoMap[securityDescriptorTag]
if securityDescriptor == nil {
return nil, fmt.Errorf("failed to get a security descriptor from a call to GetNamedSecurityInfoW")
}
defer freeWindowsSecurityDescriptor(securityDescriptor)
securityDescriptorString, err := securityDescriptorString(securityDescriptor)
if err != nil {
return nil, err
}
ownerSid := securityInfoMap[ownerTag]
if ownerSid == nil {
return nil, fmt.Errorf("failed to get owner sid from a call to GetNamedSecurityInfoW")
}
ownerSidString, err := stringFromSid(ownerSid)
if err != nil {
return nil, err
}
groupSid := securityInfoMap[groupTag]
if groupSid == nil {
return nil, fmt.Errorf("failed to get group sid from a call to GetNamedSecurityInfoW")
}
groupSidString, err := stringFromSid(groupSid)
if err != nil {
return nil, err
}
result := make(map[string]string)
result[daclTag] = securityDescriptorString
result[ownerTag] = ownerSidString
result[groupTag] = groupSidString
return result, nil
}
func (attributes *WidowsFileAttributes) securityInfo() (map[string]*byte, error) {
if len(attributes.windowsFileName) == 0 {
return nil, fmt.Errorf("file name is not set")
}
var numArgs uintptr = 8
var securityDescriptor *byte = nil
var ownerSid *byte = nil
var groupSid *byte = nil
returnVal, _, callErr := syscall.Syscall9(
uintptr(getNamedSecurityInfo),
numArgs,
uintptr(unsafe.Pointer(&attributes.windowsFileName[0])),
uintptr(fileSecurityInfo),
uintptr(desiredSecurityInformation),
uintptr(unsafe.Pointer(&ownerSid)),
uintptr(unsafe.Pointer(&groupSid)),
uintptr(0),
uintptr(0),
uintptr(unsafe.Pointer(&securityDescriptor)),
0,
)
if returnVal != 0 {
return nil, callErr
}
result := make(map[string]*byte)
result[securityDescriptorTag] = securityDescriptor
result[ownerTag] = ownerSid
result[groupTag] = groupSid
return result, nil
}
func freeWindowsSecurityDescriptor(securityDescriptor *byte) uint32 {
var numArgs = 1
returnVal, _, _ := syscall.Syscall(
uintptr(localFree),
uintptr(numArgs),
uintptr(unsafe.Pointer(securityDescriptor)),
0,
0,
)
return uint32(returnVal)
}
func securityDescriptorString(securityDescriptor *byte) (string, error) {
var numArgs uintptr = 5
var securityDescriptorUnicodeText *uint16 = nil
var securityDescriptorUnicodeTextLen uint32 = 0
// Turn the security descriptor into a string we can save in an NFI-compatible way
returnVal, _, callErr := syscall.Syscall6(
uintptr(convertSecurityDescriptorToStringSecurityDescriptor),
numArgs,
uintptr(unsafe.Pointer(securityDescriptor)),
uintptr(sddlRevision),
uintptr(desiredSecurityInformation),
uintptr(unsafe.Pointer(&securityDescriptorUnicodeText)),
uintptr(unsafe.Pointer(&securityDescriptorUnicodeTextLen)),
0,
)
if returnVal == 0 {
return "", fmt.Errorf("call to ConvertSecurityDescriptorToStringSecurityDescriptor failed with error: %v", callErr)
}
defer freeWindowsUnicodeText(securityDescriptorUnicodeText)
return unicodeStringToString(securityDescriptorUnicodeText), nil
}
func freeWindowsUnicodeText(allocatedMemory *uint16) uint32 {
var numArgs = 1
returnVal, _, _ := syscall.Syscall(
uintptr(localFree),
uintptr(numArgs),
uintptr(unsafe.Pointer(allocatedMemory)),
0,
0,
)
return uint32(returnVal)
}
func unicodeStringToString(unicodeString *uint16) string {
if unicodeString != nil {
unicodeStringSlice := make([]uint16, 0)
for unicodeStringCursor := uintptr(unsafe.Pointer(unicodeString)); ; unicodeStringCursor += 2 {
unicodeCharacter := *(*uint16)(unsafe.Pointer(unicodeStringCursor))
if unicodeCharacter == 0 {
return string(utf16.Decode(unicodeStringSlice))
}
unicodeStringSlice = append(unicodeStringSlice, unicodeCharacter)
}
}
return ""
}
func stringFromSid(sid *byte) (string, error) {
var numArgs uintptr = 2
var sidUnicodeText *uint16 = nil
returnVal, _, callErr := syscall.Syscall(
uintptr(convertSidToStringSid),
numArgs,
uintptr(unsafe.Pointer(sid)),
uintptr(unsafe.Pointer(&sidUnicodeText)),
0,
)
if returnVal == 0 {
return "", callErr
}
defer freeWindowsUnicodeText(sidUnicodeText)
return unicodeStringToString(sidUnicodeText), nil
}
func (attributes *WidowsFileAttributes) SetSecurityDescriptor(securityDescriptorString string) error {
windowsSecurityDescriptorText, err := syscall.UTF16FromString(securityDescriptorString)
if err != nil {
return fmt.Errorf("failed to convert %s to Windows equivalent", securityDescriptorString)
}
var numArgs uintptr = 4
var securityDescriptor *byte = nil
var securityDescriptorSize uint32 = 0
returnVal, _, callErr := syscall.Syscall6(
uintptr(convertStringSecurityDescriptorToSecurityDescriptor),
numArgs,
uintptr(unsafe.Pointer(&windowsSecurityDescriptorText[0])),
uintptr(sddlRevision),
uintptr(unsafe.Pointer(&securityDescriptor)),
uintptr(unsafe.Pointer(&securityDescriptorSize)),
0,
0,
)
if returnVal == 0 {
return fmt.Errorf("call to ConvertStringSecurityDescriptorToSecurityDescriptor failed with error: %v", callErr)
}
defer freeWindowsSecurityDescriptor(securityDescriptor)
numArgs = 3
if len(attributes.windowsFileName) == 0 {
return fmt.Errorf("file name is not set")
}
returnVal, _, callErr = syscall.Syscall(
uintptr(setFileSecurity),
numArgs,
uintptr(unsafe.Pointer(&attributes.windowsFileName[0])),
uintptr(desiredSecurityInformation),
uintptr(unsafe.Pointer(securityDescriptor)),
)
if returnVal == 0 {
return fmt.Errorf("call to SetFileSecurity failed with error: %v", callErr)
}
return nil
}