/
sid.go
216 lines (188 loc) · 4.85 KB
/
sid.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
package windowssecurity
import (
"crypto/sha1"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"unicode/utf16"
"unsafe"
"github.com/lkarlslund/adalanche/modules/dedup"
"github.com/lkarlslund/adalanche/modules/ui"
)
var ErrorOnlySIDVersion1Supported = errors.New("only SID version 1 supported")
type SID string
// Windows representation
// 0 = revision (always 1)
// 1 = subauthority count
// 2-7 = authority
// 8-11+ = chunks of 4 with subauthorities
// Our representation
// 0-5 = authority
// 6-9+ = chunks of 4 with subauthorities
func BytesToSID(data []byte) (SID, []byte, error) {
if len(data) == 0 {
return "", data, errors.New("No data supplied")
}
if data[0] != 0x01 {
if len(data) > 32 {
data = data[:32]
}
return "", data, fmt.Errorf("SID revision must be 1 (dump %x ...)", data)
}
subauthoritycount := int(data[1])
if subauthoritycount > 15 {
return "", data, errors.New("SID subauthority count is more than 15")
}
sidend := 8 + 4*subauthoritycount
return SID(dedup.D.BS(data[2:sidend])), data[sidend:], nil
}
func ParseStringSID(input string) (SID, error) {
if len(input) < 5 {
return "", errors.New("SID string is too short to be a SID")
}
subauthoritycount := strings.Count(input, "-") - 2
if subauthoritycount < 0 {
return "", errors.New("Less than one subauthority found")
}
if input[0] != 'S' {
return "", errors.New("SID must start with S")
}
var sid = make([]byte, 6+4*subauthoritycount)
strnums := strings.Split(input, "-")
version, err := strconv.ParseUint(strnums[1], 10, 8)
if err != nil {
return "", err
}
if version != 1 {
return "", ErrorOnlySIDVersion1Supported
}
authority, err := strconv.ParseInt(strnums[2], 10, 48)
if err != nil {
return "", err
}
authslice := make([]byte, 8)
binary.BigEndian.PutUint64(authslice, uint64(authority)<<16) // dirty tricks
copy(sid[0:], authslice[0:6])
for i := 0; i < subauthoritycount; i++ {
subauthority, err := strconv.ParseUint(strnums[3+i], 10, 32)
if err != nil {
return "", err
}
binary.LittleEndian.PutUint32(sid[6+4*i:], uint32(subauthority))
}
return SID(dedup.D.S(string(sid))), nil
}
func MustParseStringSID(input string) SID {
sid, err := ParseStringSID(input)
if err != nil {
panic(err)
}
return sid
}
func (sid SID) IsNull() bool {
return sid == ""
}
func (sid SID) String() string {
if sid == "" {
return "NULL SID"
}
var authority uint64
for i := 0; i <= 5; i++ {
authority = authority<<8 | uint64(sid[i])
}
s := fmt.Sprintf("S-1-%d", authority)
// Subauthorities
for i := 6; i < len(sid); i += 4 {
subauthority := binary.LittleEndian.Uint32([]byte(sid[i:]))
s += fmt.Sprintf("-%d", subauthority)
}
return s
}
func (sid SID) MarshalJSON() ([]byte, error) {
return json.Marshal(sid.String())
}
func (sid *SID) UnmarshalJSON(data []byte) error {
var sidstring string
err := json.Unmarshal(data, &sidstring)
if err != nil {
return err
}
newsid, err := ParseStringSID(sidstring)
*sid = newsid
return err
}
func (sid SID) Components() int {
return (len(sid) + 2) / 4
}
func (sid SID) Component(n int) uint64 {
switch n {
case 0:
if len(sid) == 0 {
return 0 // FAIL
}
return 1 // always version 1
case 1:
if len(sid) < 8 {
return 0 // FAIL
}
var authority uint64
for i := 0; i <= 5; i++ {
authority = authority<<8 | uint64(sid[i])
}
return authority
default:
offset := n*4 - 2
if len(sid) < offset+3 {
return 0 // FAIL
}
return uint64(binary.LittleEndian.Uint32([]byte(sid[offset:])))
}
}
func (sid SID) StripRID() SID {
if len(sid) < 10 {
ui.Error().Msgf("SID %s is too short to strip RID", sid)
return ""
}
return sid[:len(sid)-4]
}
func (sid SID) RID() uint32 {
if len(sid) <= 6 {
return 0
}
l := len(sid) - 4
return binary.LittleEndian.Uint32([]byte(sid[l:]))
}
func (sid SID) IsBlank() bool {
return sid == ""
}
func (sid SID) AddComponent(component uint32) SID {
newsid := make([]byte, len(sid)+4)
copy(newsid, sid)
binary.LittleEndian.PutUint32(newsid[len(sid):], component)
newsid[1] = byte(len(newsid)/4) - 2 // Adjust internal length
return SID(newsid)
}
func SIDFromPtr(data uintptr) (SID, error) {
bytes := (*[1024]byte)(unsafe.Pointer(data))
if bytes[0] != 0x01 {
return "", fmt.Errorf("SID revision must be 1 (dump %x ...)", bytes[0:32])
}
subauthoritycount := int(bytes[1])
var sid = make([]byte, 6+4*subauthoritycount)
copy(sid, bytes[2:len(sid)])
return SID(sid), nil
}
// Calculate a Windows service SID by converting servicename to uppercase, converting to Unicode 16, running through SHA1, and then converting to SID
func ServiceNameToServiceSID(servicename string) SID {
use := utf16.Encode([]rune(strings.ToUpper(servicename)))
rawbytes := (*[16384]byte)(unsafe.Pointer(&use[0]))[:len(use)*2]
huse := sha1.Sum(rawbytes)
var sidbytes [30]byte
sidbytes[5] = 5
sidbytes[6] = 80
copy(sidbytes[10:], huse[:])
return SID(sidbytes[:])
}