-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
424 lines (379 loc) · 12.3 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
git clone github.com/abakum/sites
go get github.com/Trisia/gosysproxy
go get github.com/xlab/closer
go install github.com/tc-hib/go-winres@latest
go-winres init
go get github.com/abakum/embed-encrypt
*/
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
"github.com/Trisia/gosysproxy"
"github.com/abakum/embed-encrypt/encryptedfs"
"github.com/xlab/closer"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
//go:generate go run github.com/abakum/embed-encrypt
//go:generate go run github.com/abakum/version
const (
ROOT = "SetDefaultBrowser"
SDB = ROOT + ".exe"
)
//encrypted:embed SetDefaultBrowser
var Bin encryptedfs.FS
var (
fns map[string]string
DefaultConnectionSettings []byte
DcsLen = 109
)
func main() {
var (
err error
wg sync.WaitGroup
ProxySettings,
report,
exeDir,
_ string
// ProxyMode = ""
key registry.Key
)
defer closer.Close()
ctx, ca := context.WithCancel(context.Background())
closer.Bind(func() {
ca()
if err != nil {
let.Println(err)
defer os.Exit(1)
}
// key, err = registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\ControlSet001\Services\iphlpsvc`, registry.SET_VALUE)
// if err == nil {
// PrintOk("Start=2", key.SetDWordValue("Start", 2))
// key.Close()
// }
// PrintOk("iphlpsvc", iphlpsvc("start"))
// key, err = registry.OpenKey(registry.CURRENT_USER,
// `SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel`, registry.SET_VALUE)
// if err == nil {
// PrintOk("Autoconfig=1", key.SetDWordValue("Autoconfig", 1))
// key.Close()
// }
PrintOk("gosysproxy", proxy(ProxySettings))
// key, err = registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Policies\YandexBrowser`, registry.SET_VALUE)
// if err == nil {
// PrintOk("ProxyMode="+ProxyMode, key.SetStringValue("ProxyMode", ProxyMode))
// key.Close()
// }
// PrintOk("SetDefaultBrowser", SetDefaultBrowser())
wg.Wait()
pressEnter()
})
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree\Microsoft\Windows\GroupPolicy\
// администратор через планировщик устанавливает ХОРОШИЕ значения в реестре
exeDir, err = os.Executable()
if err != nil {
panic(1)
}
exeDir = filepath.Dir(exeDir)
fns, report, err = encryptedfs.Xcopy(Bin, ROOT, exeDir, "")
if report != "" {
fmt.Println(report)
}
key, err = registry.OpenKey(registry.CURRENT_USER,
`SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections`, registry.QUERY_VALUE|registry.SET_VALUE)
if err == nil {
DefaultConnectionSettings, _, err = key.GetBinaryValue("DefaultConnectionSettings")
PrintOk(fmt.Sprintf("DefaultConnectionSettings[8]=%d len=%d", DefaultConnectionSettings[8], len(DefaultConnectionSettings)), err)
if len(DefaultConnectionSettings) == DcsLen && DefaultConnectionSettings[8] != 1 {
ConnectionSettings := bytes.Clone(DefaultConnectionSettings)
ConnectionSettings[8] = 1
PrintOk("ConnectionSettings[8]=1", key.SetBinaryValue("DefaultConnectionSettings", ConnectionSettings))
}
key.Close()
}
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`SOFTWARE\Policies\Microsoft\Internet Explorer\Control Panel`, "Autoconfig", 0, func() { PrintOk("gosysproxy", proxy("")) })
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`Software\Microsoft\Windows\CurrentVersion\Internet Settings`, "ProxyEnable", 0, func() { PrintOk("gosysproxy", proxy("")) })
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`Software\Microsoft\Windows\CurrentVersion\Internet Settings`, "AutoConfigURL", "", func() { PrintOk("gosysproxy", proxy("")) })
// go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
// `SYSTEM\ControlSet001\Services\iphlpsvc`, "Start", 4, func() { PrintOk("iphlpsvc", iphlpsvc("stop")) })
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings`, "ProxySettingsPerUser", 1, nil)
key, err = registry.OpenKey(registry.CURRENT_USER, `SOFTWARE\Policies\YandexBrowser`, registry.QUERY_VALUE)
if err == nil {
// ProxyMode, _, err = key.GetStringValue("ProxyMode")
// PrintOk("ProxyMode="+ProxyMode, err)
ProxySettings, _, err = key.GetStringValue("ProxySettings")
PrintOk("ProxySettings="+ProxySettings, err)
key.Close()
}
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`SOFTWARE\Policies\YandexBrowser`, "ProxyPacUrl", ProxySettings, nil)
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`SOFTWARE\Policies\YandexBrowser`, "ProxyMode", "pac_script", nil) //direct
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`SOFTWARE\Policies\Microsoft\Windows\Control Panel\Desktop`, "ScreenSaveActive", "0", nil)
// go anyWatch(ctx, &wg, registry.CURRENT_USER,
// `SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System`, "DisableLockWorkstation", 1, nil)
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System`, "DisableLockWorkstation", 1, nil)
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Microsoft\Windows\Personalization`, "NoLockScreen", 1, nil)
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Microsoft\Power\PowerSettings\3C0BC021-C8A8-4E07-A973-6B14CBCB2B7E`, "ACSettingIndex", 0, nil)
go anyWatch(ctx, &wg, registry.CURRENT_USER,
`SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice`, "ProgId", "ChromeHTML", func() { PrintOk("SetDefaultBrowser", SetDefaultBrowser()) })
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Microsoft\Windows Defender`, "DisableAntiSpyware", 0, nil)
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Microsoft\Windows Defender\Real-Time Protection`, "DisableRealtimeMonitoring", 0, nil)
go anyWatch(ctx, &wg, registry.LOCAL_MACHINE,
`SOFTWARE\Policies\Mozilla\Firefox`, "", nil, nil)
// go anyWatch(ctx, &wg, registry.CURRENT_USER,
// `SOFTWARE\Classes\VncViewer.Config\DefaultIcon`, "", `C:\Program Files\uvnc bvba\UltraVNC\vncviewer.exe,0`, nil)
// go func() {
// time.Sleep(time.Second * 3) // test notify done
// ca() // test try done
// }()
closer.Hold()
}
// run SetDefaultBrowser without parameters to list all Browsers on your system http://kolbi.cz/blog/?p=396
func SetDefaultBrowser() (err error) {
// cwd, err := os.Getwd()
// if err != nil {
// err = srcError(err)
// return
// }
// exe := filepath.Join(cwd, "SetDefaultBrowser", "SetDefaultBrowser.exe")
sdb := exec.Command(fns[SDB],
"chrome",
// "delay=1000",
)
// sdb.Dir = filepath.Dir(sdb.Path)
sdb.Stdout = os.Stdout
sdb.Stderr = os.Stderr
lt.Println(cmd("Run", sdb))
err = sdb.Run()
return
}
func cmd(s string, c *exec.Cmd) string {
if c == nil {
return ""
}
return fmt.Sprintf(`%s "%s" %s`, s, c.Args[0], strings.Join(c.Args[1:], " "))
}
// watch and restore reg key
// следит за восстанавлением реестра
func anyWatch(ctx context.Context, wg *sync.WaitGroup, root registry.Key,
path, key string, val any, after func()) {
const (
tryAfter = time.Second
)
var (
fn func(k registry.Key) (bool, any, error)
)
switch value := val.(type) {
case int:
fn = func(k registry.Key) (bool, any, error) {
old, _, err := k.GetIntegerValue(key)
if uint32(old) != uint32(value) || err != nil {
err = k.SetDWordValue(key, uint32(value))
if err != nil {
return false, nil, srcError(err)
}
return true, old, nil
}
return false, old, nil
}
case uint32:
fn = func(k registry.Key) (bool, any, error) {
old, _, err := k.GetIntegerValue(key)
if uint32(old) != value || err != nil {
err = k.SetDWordValue(key, value)
if err != nil {
return false, nil, srcError(err)
}
return true, old, nil
}
return false, old, nil
}
case uint64:
fn = func(k registry.Key) (bool, any, error) {
old, _, err := k.GetIntegerValue(key)
if uint64(old) != value || err != nil {
err = k.SetQWordValue(key, value)
if err != nil {
return false, nil, srcError(err)
}
return true, old, nil
}
return false, old, nil
}
case string:
fn = func(k registry.Key) (bool, any, error) {
old, _, err := k.GetStringValue(key)
if old != value { // || err != nil
err = k.SetStringValue(key, value)
if err != nil {
return false, nil, srcError(err)
}
return true, old, nil
}
return false, old, nil
}
case nil:
fn = func(k registry.Key) (bool, any, error) {
err := deleteRegistryKey(k)
if err != nil {
return false, nil, srcError(err)
}
return true, nil, nil
}
default:
letf.Println("wrong type of val", value)
return
}
wg.Add(1)
defer wg.Done()
ltf.Println(path, key)
try := make(chan struct{})
notify := make(chan error)
for {
go func() {
// wait until the cause of the error disappears or until the detected changes in the registry run out
// ждём пока причина ошибки уйдёт или закончатся обнаруженные изменения в реестре
time.Sleep(tryAfter)
try <- struct{}{}
}()
select {
case <-ctx.Done():
ltf.Println(key, "try done")
return // done
case <-try:
if val == nil && key != "" {
path += `\` + key
key = ""
}
// query, compare and set
k, err := registry.OpenKey(root, path, registry.QUERY_VALUE|registry.SET_VALUE|syscall.KEY_NOTIFY|registry.ENUMERATE_SUB_KEYS)
if err != nil {
if !strings.Contains(err.Error(), "The system cannot find the file specified.") {
letf.Println(root, path, err)
}
continue // next try after tryAfter
}
ok, old, err := fn(k)
if err != nil {
let.Println(err)
k.Close()
continue // next try after tryAfter
}
if ok {
if val == nil {
ltf.Printf(`deleted %s\`, path)
} else {
ltf.Printf(`%s\%s %v->%v`, path, key, old, val)
}
if after != nil {
go after()
}
}
// wait for change
go func() {
// https://learn.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regnotifychangekeyvalue
// If the specified key is closed, the event is signaled
// чтоб прервать regNotifyChangeKeyValue надо закрыть k
notify <- srcError(windows.RegNotifyChangeKeyValue(windows.Handle(k), false, windows.REG_NOTIFY_CHANGE_LAST_SET, 0, false)) //windows.REG_NOTIFY_CHANGE_NAME|
}()
select {
case <-ctx.Done():
ltf.Println(key, "notify done")
k.Close() // cancel regNotifyChangeKeyValue
return // done
case err = <-notify:
k.Close()
if err != nil {
if !strings.Contains(err.Error(), "Illegal operation attempted on a registry key that has been marked for deletion.") {
let.Println(err)
}
}
}
}
}
}
func proxy(PAC string) error {
gosysproxy.SetPAC(PAC)
gosysproxy.Off()
key, err := registry.OpenKey(registry.CURRENT_USER,
`SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections`, registry.SET_VALUE)
if err == nil {
if len(DefaultConnectionSettings) == DcsLen {
ConnectionSettings := DefaultConnectionSettings[:]
if PAC == "" {
ConnectionSettings[8] = 1
}
PrintOk(fmt.Sprintf("ConnectionSettings[8]=%d len=%d", ConnectionSettings[8], len(ConnectionSettings)), key.SetBinaryValue("DefaultConnectionSettings", ConnectionSettings))
}
key.Close()
}
cmd := exec.Command("netsh",
"winhttp",
"import",
"proxy",
"ie",
)
return cmd.Run()
}
func iphlpsvc(s string) error {
cmd := exec.Command("net",
s,
"iphlpsvc",
)
return cmd.Run()
}
func deleteRegistryKey(key registry.Key) error {
const c = 1
// Find all the value names inside the key and delete them.
valueNames, err := key.ReadValueNames(c)
if err != nil && err != io.EOF {
return err
}
for _, valueName := range valueNames {
ltf.Println("DeleteValue", valueName)
err = key.DeleteValue(valueName)
if err != nil && err != io.EOF {
return err
}
}
// Find the subkeys and delete those recursively.
subkeyNames, err := key.ReadSubKeyNames(c)
if err != nil && err != io.EOF {
return err
}
for _, subkeyName := range subkeyNames {
ltf.Println("deleteRegistryKey", subkeyName)
subKey, err := registry.OpenKey(key, subkeyName, registry.ALL_ACCESS)
if err != nil {
return err
}
err = deleteRegistryKey(subKey)
if err != nil {
return err
}
}
// Then delete itself.
return registry.DeleteKey(key, "")
}