forked from GoogleCloudPlatform/guest-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows_accounts.go
309 lines (276 loc) · 7.98 KB
/
windows_accounts.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"hash"
"math/big"
"reflect"
"time"
"github.com/GoogleCloudPlatform/guest-logging-go/logger"
)
var (
accountRegKey = "PublicKeys"
credsWriter = &serialPort{"COM4"}
)
// newPwd will generate a random password that meets Windows complexity
// requirements: https://technet.microsoft.com/en-us/library/cc786468.
// Characters that are difficult for users to type on a command line (quotes,
// non english characters) are not used.
func newPwd(userPwLgth int) (string, error) {
var pwLgth int
minPwLgth := 15
maxPwLgth := 255
lower := []byte("abcdefghijklmnopqrstuvwxyz")
upper := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
numbers := []byte("0123456789")
special := []byte(`~!@#$%^&*_-+=|\(){}[]:;<>,.?/`)
chars := bytes.Join([][]byte{lower, upper, numbers, special}, nil)
pwLgth = minPwLgth
if userPwLgth > minPwLgth {
pwLgth = userPwLgth
}
if userPwLgth > maxPwLgth {
pwLgth = maxPwLgth
}
for {
b := make([]byte, pwLgth)
for i := range b {
ci, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
return "", err
}
b[i] = chars[ci.Int64()]
}
var l, u, n, s int
if bytes.ContainsAny(lower, string(b)) {
l = 1
}
if bytes.ContainsAny(upper, string(b)) {
u = 1
}
if bytes.ContainsAny(numbers, string(b)) {
n = 1
}
if bytes.ContainsAny(special, string(b)) {
s = 1
}
// If the password does not meet Windows complexity requirements, try again.
// https://technet.microsoft.com/en-us/library/cc786468
if l+u+n+s >= 3 {
return string(b), nil
}
}
}
type credsJSON struct {
ErrorMessage string `json:"errorMessage,omitempty"`
EncryptedPassword string `json:"encryptedPassword,omitempty"`
UserName string `json:"userName,omitempty"`
PasswordFound bool `json:"passwordFound,omitempty"`
Exponent string `json:"exponent,omitempty"`
Modulus string `json:"modulus,omitempty"`
HashFunction string `json:"hashFunction,omitempty"`
}
func printCreds(creds *credsJSON) error {
data, err := json.Marshal(creds)
if err != nil {
return err
}
_, err = credsWriter.Write(append(data, []byte("\n")...))
return err
}
var badExpire []string
func (k windowsKey) expired() bool {
t, err := time.Parse(time.RFC3339, k.ExpireOn)
if err != nil {
if !containsString(k.ExpireOn, badExpire) {
logger.Errorf("error parsing time: %s", err)
badExpire = append(badExpire, k.ExpireOn)
}
return true
}
return t.Before(time.Now())
}
func (k windowsKey) createOrResetPwd() (*credsJSON, error) {
pwd, err := newPwd(k.PasswordLength)
if err != nil {
return nil, fmt.Errorf("error creating password: %v", err)
}
if _, err := userExists(k.UserName); err == nil {
logger.Infof("Resetting password for user %s", k.UserName)
if err := resetPwd(k.UserName, pwd); err != nil {
return nil, fmt.Errorf("error running resetPwd: %v", err)
}
if k.AddToAdministrators != nil && *k.AddToAdministrators == true {
if err := addUserToGroup(k.UserName, "Administrators"); err != nil {
return nil, fmt.Errorf("error running addUserToGroup: %v", err)
}
}
} else {
logger.Infof("Creating user %s", k.UserName)
if err := createUser(k.UserName, pwd); err != nil {
return nil, fmt.Errorf("error running createUser: %v", err)
}
if k.AddToAdministrators == nil || *k.AddToAdministrators == true {
if err := addUserToGroup(k.UserName, "Administrators"); err != nil {
return nil, fmt.Errorf("error running addUserToGroup: %v", err)
}
}
}
return createcredsJSON(k, pwd)
}
func createcredsJSON(k windowsKey, pwd string) (*credsJSON, error) {
mod, err := base64.StdEncoding.DecodeString(k.Modulus)
if err != nil {
return nil, fmt.Errorf("error decoding modulus: %v", err)
}
exp, err := base64.StdEncoding.DecodeString(k.Exponent)
if err != nil {
return nil, fmt.Errorf("error decoding exponent: %v", err)
}
key := &rsa.PublicKey{
N: new(big.Int).SetBytes(mod),
E: int(new(big.Int).SetBytes(exp).Int64()),
}
if k.HashFunction == "" {
k.HashFunction = "sha1"
}
var hashFunc hash.Hash
switch k.HashFunction {
case "sha1":
hashFunc = sha1.New()
case "sha256":
hashFunc = sha256.New()
case "sha512":
hashFunc = sha512.New()
default:
return nil, fmt.Errorf("unknown hash function requested: %q", k.HashFunction)
}
encPwd, err := rsa.EncryptOAEP(hashFunc, rand.Reader, key, []byte(pwd), nil)
if err != nil {
return nil, fmt.Errorf("error encrypting password: %v", err)
}
return &credsJSON{
PasswordFound: true,
Exponent: k.Exponent,
Modulus: k.Modulus,
UserName: k.UserName,
HashFunction: k.HashFunction,
EncryptedPassword: base64.StdEncoding.EncodeToString(encPwd),
}, nil
}
type winAccountsMgr struct{}
func (a *winAccountsMgr) diff() bool {
return !reflect.DeepEqual(newMetadata.Instance.Attributes.WindowsKeys, oldMetadata.Instance.Attributes.WindowsKeys)
}
func (a *winAccountsMgr) timeout() bool {
return false
}
func (a *winAccountsMgr) disabled(os string) (disabled bool) {
if os != "windows" {
return true
}
disabled, err := config.Section("accountManager").Key("disable").Bool()
if err == nil {
return disabled
}
if newMetadata.Instance.Attributes.DisableAccountManager != nil {
return *newMetadata.Instance.Attributes.DisableAccountManager
}
if newMetadata.Project.Attributes.DisableAccountManager != nil {
return *newMetadata.Project.Attributes.DisableAccountManager
}
return false
}
var badKeys []string
func (a *winAccountsMgr) set() error {
newKeys := newMetadata.Instance.Attributes.WindowsKeys
regKeys, err := readRegMultiString(regKeyBase, accountRegKey)
if err != nil && err != errRegNotExist {
return err
}
toAdd := compareAccounts(newKeys, regKeys)
for _, key := range toAdd {
creds, err := key.createOrResetPwd()
if err == nil {
printCreds(creds)
continue
}
logger.Errorf("error setting password: %s", err)
creds = &credsJSON{
PasswordFound: false,
Exponent: key.Exponent,
Modulus: key.Modulus,
UserName: key.UserName,
ErrorMessage: err.Error(),
}
printCreds(creds)
}
var jsonKeys []string
for _, key := range newKeys {
jsn, err := json.Marshal(key)
if err != nil {
// This *should* never happen as each key was just Unmarshalled above.
logger.Errorf("Failed to marshal windows key to JSON: %s", err)
continue
}
jsonKeys = append(jsonKeys, string(jsn))
}
return writeRegMultiString(regKeyBase, accountRegKey, jsonKeys)
}
var badReg []string
func compareAccounts(newKeys windowsKeys, oldStrKeys []string) windowsKeys {
if len(newKeys) == 0 {
return nil
}
if len(oldStrKeys) == 0 {
return newKeys
}
var oldKeys windowsKeys
for _, s := range oldStrKeys {
var key windowsKey
if err := json.Unmarshal([]byte(s), &key); err != nil {
if !containsString(s, badReg) {
logger.Errorf("Bad windows key from registry: %s", err)
badReg = append(badReg, s)
}
continue
}
oldKeys = append(oldKeys, key)
}
var toAdd windowsKeys
for _, key := range newKeys {
if func(key windowsKey, oldKeys windowsKeys) bool {
for _, oldKey := range oldKeys {
if oldKey.UserName == key.UserName &&
oldKey.Modulus == key.Modulus &&
oldKey.ExpireOn == key.ExpireOn {
return false
}
}
return true
}(key, oldKeys) {
toAdd = append(toAdd, key)
}
}
return toAdd
}