forked from hardentools/hardentools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.go
441 lines (374 loc) · 14.4 KB
/
gui.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Hardentools
// Copyright (C) 2017-2021 Security Without Borders
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//go:build !cli
package main
import (
"errors"
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var messageBox, firstColumn, secondColumn, thirdColumn *fyne.Container
var eventsTextAreaProgressBar *widget.ProgressBarInfinite
var stateLabels map[string]*widget.Label
var inProgressLabel *widget.Label
func mainGUI() {
// Check if hardentools has been started with elevated rights. If not
// ask user if she wants to elevate.
elevationStatus := isElevated()
if elevationStatus == false {
// Main window must already be open for this dialog to work.
askElevationDialog()
}
// Show splash screen since loading takes some time (at least with admin
// privileges) due to sequential reading of all the settings.
showSplash()
// Show main screen.
createMainGUIContent(elevationStatus)
}
// showSplash shows an splash content during initialization.
func showSplash() {
splashContent := container.NewVBox(
widget.NewLabelWithStyle("Hardentools is starting up. Please wait...", fyne.TextAlignCenter, fyne.TextStyle{Monospace: true}),
widget.NewProgressBarInfinite())
mainWindow.SetContent(splashContent)
}
// createMainGUIContent shows the main GUI screen that allows to harden or
// restore the settings.
func createMainGUIContent(elevationStatus bool) {
// Init variables.
var labelText, buttonText, expertSettingsText string
var enableHardenAdditionalButton bool
var buttonFunc func()
var expertSettingsCheckBox *widget.Check
// Check if we are running with elevated rights.
if elevationStatus == false {
allHardenSubjects = hardenSubjectsForUnprivilegedUsers
} else {
allHardenSubjects = hardenSubjectsForPrivilegedUsers
}
// Check hardening status.
var status = checkStatus()
// Build up expert settings checkboxes and map.
expertConfig = make(map[string]bool)
expertCompWidgetArray := make([]*widget.Check, len(allHardenSubjects))
for i, hardenSubject := range allHardenSubjects {
var subjectIsHardened = hardenSubject.IsHardened()
var enableField bool
if status == false {
// All checkboxes checked by default, disabled only if subject is already hardened.
expertConfig[hardenSubject.Name()] = !subjectIsHardened && hardenSubject.HardenByDefault()
// Only enable, if not already hardened.
enableField = !subjectIsHardened
} else {
// Restore: only checkboxes checked which are hardened.
expertConfig[hardenSubject.Name()] = subjectIsHardened
// Disable all, since the user must restore all settings because otherwise
// consecutive execution of hardentools might fail (e.g. starting powershell
// or cmd commands) or might be ineffectiv (settings are already hardened) or
// hardened settings might get saved as "before" settings, so user
// can't revert to the state "before".
enableField = false
}
expertCompWidgetArray[i] = widget.NewCheck(hardenSubject.LongName(), checkBoxEventGenerator(hardenSubject.Name()))
expertCompWidgetArray[i].SetChecked(expertConfig[hardenSubject.Name()])
if !enableField {
expertCompWidgetArray[i].Disable()
}
}
// Set labels / text fields (harden or restore).
if status == false {
buttonText = "Harden!"
buttonFunc = hardenAll
labelText = "Ready to harden some features of your system?"
expertSettingsText = "Change only if you know what you are doing!\nDisabled settings are already hardened."
enableHardenAdditionalButton = false
} else {
buttonText = "Restore..."
buttonFunc = restoreAll
labelText = "We have already hardened some risky features.\nDo you want to restore them?"
expertSettingsText = "The following hardened features are going to be restored:"
enableHardenAdditionalButton = true
}
// Expert tab.
countExpertSettings := len(expertCompWidgetArray)
expertTab1 := container.NewVBox()
expertTab2 := container.NewVBox()
for i, compWidget := range expertCompWidgetArray {
if i < countExpertSettings/2 {
expertTab1.Add(compWidget)
} else {
expertTab2.Add(compWidget)
}
}
expertSettingsHBox := container.NewHBox(expertTab1, expertTab2)
expertTabWidget := widget.NewCard("", "Expert Settings",
container.NewVBox(widget.NewLabelWithStyle(expertSettingsText, fyne.TextAlignCenter, fyne.TextStyle{Italic: true}),
expertSettingsHBox))
// Build main GUI window's main tab.
hardenAgainButton := widget.NewButton("Harden again (all default settings)",
hardenDefaultsAgain)
hardenAgainButton.Hidden = !enableHardenAdditionalButton
hardenButton := widget.NewButton(buttonText, func() { buttonFunc() })
hardenButton.SetIcon(theme.ConfirmIcon())
introText := widget.NewLabelWithStyle("Hardentools is designed to disable a number of \"features\" exposed by Microsoft\n"+
"Windows and some widely used applications (Microsoft Office and Adobe PDF Reader,\n"+
"for now). These features, commonly thought for enterprise customers,\n"+
"are generally useless to regular users and rather pose as dangers as\n"+
"they are very commonly abused by attackers to execute malicious code\n"+
"on a victim's computer. The intent of this tool is to simply reduce\n"+
"the attack surface by disabling the low-hanging fruit. Hardentools is\n"+
"for individuals at risk, who might want an extra level of security intended\n"+
"at the price of some usability. It is not intended for corporate environments.\n",
fyne.TextAlignCenter, fyne.TextStyle{Italic: true})
mainTabContent := container.NewVBox(
widget.NewLabelWithStyle(labelText, fyne.TextAlignCenter, fyne.TextStyle{Bold: true}),
hardenButton,
hardenAgainButton,
)
mainTabWidget := widget.NewCard("", "", mainTabContent)
expertSettingsCheckBox = widget.NewCheck("Show Expert Settings", func(on bool) {
if on {
mainWindow.SetContent(container.NewVBox(expertTabWidget, mainTabWidget))
} else {
mainWindow.SetContent(container.NewVBox(widget.NewCard("", "Introduction", introText), mainTabWidget))
}
mainWindow.CenterOnScreen()
})
mainTabContent.Add(expertSettingsCheckBox)
mainWindow.SetContent(container.NewVBox(widget.NewCard("", "Introduction", introText), mainTabWidget))
mainWindow.CenterOnScreen()
}
// showErrorDialog shows an error message.
func showErrorDialog(errorMessage string) {
if mainWindow != nil {
ch := make(chan bool)
err := errors.New(errorMessage)
errorDialog := dialog.NewError(err, mainWindow)
errorDialog.SetOnClosed(func() {
ch <- true
})
errorDialog.Show()
<-ch
} else {
// no main windows - seem to be in command line mode.
Info.Println("Error: " + errorMessage)
}
}
// showInfoDialog shows an info message.
func showInfoDialog(infoMessage string) {
if mainWindow != nil {
ch := make(chan bool)
infoDialog := dialog.NewInformation("Information", infoMessage, mainWindow)
infoDialog.SetOnClosed(func() {
ch <- true
})
infoDialog.Show()
<-ch
} else {
// no main windows - seem to be in command line mode.
Info.Println("Information: " + infoMessage)
}
}
// showEndDialog shows the close button after hardening/restoring.
func showEndDialog(infoMessage string) {
ch := make(chan bool)
eventsTextAreaProgressBar.Hide()
inProgressLabel.Hide()
message := widget.NewLabelWithStyle(infoMessage, fyne.TextAlignCenter, fyne.TextStyle{Monospace: true})
messageBox.Add(container.NewVBox(message,
widget.NewButton("Close", func() {
ch <- true
})))
<-ch
}
// askElevationDialog asks the user if she wants to elevates her rights.
func askElevationDialog() {
ch := make(chan int)
dialogText := "You are currently running hardentools as normal user.\n" +
"You won't be able to harden all available settings!\n" +
"If you have admin rights available, please press \"Yes\", otherwise press \"No\".\n"
cnf := dialog.NewConfirm("Do you want to use admin privileges?", dialogText, func(response bool) {
if response == true {
restartWithElevatedPrivileges()
}
ch <- 42
}, mainWindow)
cnf.SetDismissText("No")
cnf.SetConfirmText("Yes")
cnf.Show()
<-ch
}
// checkBoxEventGenerator is a helper function that allows GUI checkbox elements
// to call this function as a callback method. checkBoxEventGenerator then saves
// the requested expert config setting for the checkbox in the corresponding map.
func checkBoxEventGenerator(hardenSubjName string) func(on bool) {
var hardenSubjectName = hardenSubjName
return func(on bool) {
expertConfig[hardenSubjectName] = on
}
}
// restartWithElevatedPrivileges tries to restart hardentools.exe with admin
// privileges.
func restartWithElevatedPrivileges() {
// Find out our program (exe) name.
progName := os.Args[0]
// Start us again, this time with elevated privileges.
if startWithElevatedPrivs(progName) {
// Exit this instance (the unprivileged one).
os.Exit(0)
} else {
// Something went wrong.
showErrorDialog("Error while trying to gain elevated privileges. Starting in unprivileged mode...")
}
}
// showEventsTextArea updates the UI to show the harden/restore progress and
// the final status of the hardened settings.
func showEventsTextArea() {
// init map that remembers stateIcons.
stateLabels = make(map[string]*widget.Label, len(hardenSubjectsForPrivilegedUsers))
firstColumn = container.NewVBox(widget.NewLabelWithStyle("Harden Item Name",
fyne.TextAlignLeading, fyne.TextStyle{Bold: true}))
secondColumn = container.NewVBox(widget.NewLabelWithStyle("Operation Result",
fyne.TextAlignLeading, fyne.TextStyle{Bold: true}))
thirdColumn = container.NewVBox(widget.NewLabelWithStyle("Verification Result",
fyne.TextAlignLeading, fyne.TextStyle{Bold: true}))
resultBox := container.NewHBox(
firstColumn,
secondColumn,
thirdColumn)
resultBoxContainer := container.NewVScroll(resultBox)
resultBoxContainer.SetMinSize(fyne.NewSize(500, 600))
resultBoxGroup := widget.NewCard("", "", resultBoxContainer)
messageBox = container.NewVBox()
inProgressLabel = widget.NewLabelWithStyle("Operation in progress...",
fyne.TextAlignCenter, fyne.TextStyle{})
messageBox.Add(inProgressLabel)
eventsTextAreaProgressBar = widget.NewProgressBarInfinite()
messageBox.Add(eventsTextAreaProgressBar)
eventsArea := container.NewVBox(messageBox, resultBoxGroup)
mainWindow.SetContent(eventsArea)
mainWindow.CenterOnScreen()
}
// ShowSuccess sets GUI status of name field to success
func ShowSuccess(name string) {
if mainWindow != nil {
stateLabels[name] = widget.NewLabel("...")
firstColumn.Add(container.NewHBox(widget.NewLabel(name)))
secondColumn.Add(container.NewHBox(widget.NewLabel("Success")))
thirdColumn.Add(container.NewHBox(stateLabels[name]))
} else {
Info.Println(name + ": Success")
}
}
// ShowFailure sets GUI status of name field to failureText
func ShowFailure(name, failureText string) {
if mainWindow != nil {
stateLabels[name] = widget.NewLabel("...")
firstColumn.Add(container.NewHBox(widget.NewLabel(name)))
secondColumn.Add(container.NewHBox(widget.NewLabelWithStyle("FAIL", fyne.TextAlignLeading, fyne.TextStyle{Bold: true})))
thirdColumn.Add(container.NewHBox(stateLabels[name]))
showErrorDialog(name + " failed with error:\n" + failureText)
} else {
Info.Println(name + " failed with error: " + failureText)
}
}
// ShowIsHardened sets GUI result for name to is hardened
func ShowIsHardened(name string) {
label := stateLabels[name]
if label != nil {
label.SetText("is hardened")
} else {
stateLabels[name] = widget.NewLabel("is hardened")
firstColumn.Add(container.NewHBox(widget.NewLabel(name)))
secondColumn.Add(container.NewHBox(widget.NewLabel("not selected")))
thirdColumn.Add(container.NewHBox(stateLabels[name]))
}
}
// ShowNotHardened sets GUI result for name to not hardened
func ShowNotHardened(name string) {
label := stateLabels[name]
if label != nil {
label.SetText("not hardened")
} else {
stateLabels[name] = widget.NewLabel("not hardened")
firstColumn.Add(container.NewHBox(widget.NewLabel(name)))
secondColumn.Add(container.NewHBox(widget.NewLabel("not selected")))
thirdColumn.Add(container.NewHBox(stateLabels[name]))
}
}
func cmdHarden() {
cmdHardenRestore(true)
Info.Println("Done! Risky features have been hardened!\nFor all changes to take effect please restart Windows.")
os.Exit(0)
}
func cmdRestore() {
cmdHardenRestore(false)
Info.Println("Done! Restored settings to their original state.\nFor all changes to take effect please restart Windows.")
os.Exit(0)
}
// hardenAll starts harden procedure.
func hardenAll() {
showEventsTextArea()
// Use goroutine to allow gui to update window.
go func() {
triggerAll(true)
markStatus(true)
showStatus()
showEndDialog("Done! Risky features have been hardened!\nFor all changes to take effect please restart Windows.")
os.Exit(0)
}()
}
// RestoreAll starts restore procedure.
func restoreAll() {
showEventsTextArea()
// Use goroutine to allow gui to update window.
go func() {
triggerAll(false)
restoreSavedRegistryKeys() // TODO: add error handling/visibility to user
markStatus(false)
showStatus()
showEndDialog("Done! Restored settings to their original state.\nFor all changes to take effect please restart Windows.")
os.Exit(0)
}()
}
// hardenDefaultsAgain restores the original settings and
// hardens using the default settings (no custom settings apply).
func hardenDefaultsAgain() {
showEventsTextArea()
// Use goroutine to allow gui to update window.
go func() {
// Restore hardened settings.
triggerAll(false)
restoreSavedRegistryKeys()
markStatus(false)
// Reset expertConfig (is set to currently already hardened settings
// in case of restore).
expertConfig = make(map[string]bool)
for _, hardenSubject := range allHardenSubjects {
expertConfig[hardenSubject.Name()] = hardenSubject.HardenByDefault()
}
// Harden all settings.
triggerAll(true)
markStatus(true)
showStatus()
showEndDialog("Done!\nRisky features have been hardened!\nFor all changes to take effect please restart Windows.")
os.Exit(0)
}()
}