forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.go
289 lines (244 loc) · 8.35 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
package main
import (
"encoding/hex"
"fmt"
"os"
"path/filepath"
"github.com/Velocidex/yaml/v2"
errors "github.com/go-errors/errors"
proto "google.golang.org/protobuf/proto"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
logging "www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/users"
"www.velocidex.com/golang/velociraptor/services/writeback"
"www.velocidex.com/golang/velociraptor/startup"
)
var (
gui_command = app.Command(
"gui", "Bring up a lazy GUI.")
gui_command_datastore = gui_command.Flag(
"datastore", "Path to a datastore directory (defaults to temp)").
ExistingDir()
gui_command_no_browser = gui_command.Flag(
"nobrowser", "Do not bring up the browser").Bool()
gui_command_no_client = gui_command.Flag(
"noclient", "Do not bring up a client").Bool()
)
func doGUI() error {
// Start from a clean slate
os.Setenv("VELOCIRAPTOR_CONFIG", "")
datastore_directory := *gui_command_datastore
if datastore_directory == "" {
datastore_directory = filepath.Join(os.TempDir(), "gui_datastore")
// Ensure the directory exists
err := os.MkdirAll(datastore_directory, 0o777)
if err != nil {
return fmt.Errorf("Unable to create datastore directory: %w", err)
}
}
datastore_directory, err := filepath.Abs(datastore_directory)
if err != nil {
return fmt.Errorf("Unable find path: %w", err)
}
server_config_path := filepath.Join(datastore_directory, "server.config.yaml")
client_config_path := filepath.Join(datastore_directory, "client.config.yaml")
// Try to open the config file from there
config_obj, err := makeDefaultConfigLoader().
WithVerbose(true).
WithFileLoader(server_config_path).LoadAndValidate()
if err != nil || config_obj.Frontend == nil {
// Stop on hard errors but if the file does not exist we need
// to create it below..
hard_err, ok := err.(config.HardError)
if ok && !errors.Is(hard_err.Err, os.ErrNotExist) {
return err
}
// Need to generate a new config. This config is not
// really suitable for use in a proper deployment but
// it is used here just to bring up the GUI and a self
// client. It is useful for demonstration purposes and
// to just be able to use the notebook and build an
// offline collector.
logging.Prelog("No valid config found - " +
"will generare a new one at <green>" + server_config_path)
config_obj = config.GetDefaultConfig()
err := generateNewKeys(config_obj)
if err != nil {
return fmt.Errorf("Unable to create config: %w", err)
}
// GUI Configuration - hard coded username/password
// and no SSL are suitable for local deployment only!
config_obj.GUI.BindAddress = "127.0.0.1"
config_obj.GUI.BindPort = 8889
// Frontend only suitable for local client
config_obj.Frontend.BindAddress = "127.0.0.1"
config_obj.Frontend.BindPort = 8000
config_obj.Frontend.DoNotCompressArtifacts = true
// Client configuration.
config_obj.Client.ServerUrls = []string{"https://localhost:8000/"}
config_obj.Client.UseSelfSignedSsl = true
write_back := filepath.Join(datastore_directory, "Velociraptor.writeback.%NONCE%.yaml")
config_obj.Client.WritebackWindows = write_back
config_obj.Client.WritebackLinux = write_back
config_obj.Client.WritebackDarwin = write_back
// Do not use a local buffer file since there is no
// point - we are by definition directly connected.
config_obj.Client.LocalBuffer.DiskSize = 0
config_obj.Client.LocalBuffer.FilenameWindows = ""
config_obj.Client.LocalBuffer.FilenameLinux = ""
config_obj.Client.LocalBuffer.FilenameDarwin = ""
// Make the client use the datastore_directory for tempfiles as well.
tmpdir := filepath.Join(datastore_directory, "temp")
err = os.MkdirAll(tmpdir, 0700)
if err != nil {
return fmt.Errorf("Unable to create temp directory: %w", err)
}
config_obj.Client.TempdirLinux = tmpdir
config_obj.Client.TempdirWindows = tmpdir
config_obj.Client.TempdirDarwin = tmpdir
config_obj.Datastore.Location = datastore_directory
config_obj.Datastore.FilestoreDirectory = datastore_directory
// Make events run much faster in this configuration
config_obj.Defaults.EventMaxWait = 1
config_obj.Defaults.EventMaxWaitJitter = 1
config_obj.Defaults.EventChangeNotifyAllClients = true
// Load the "fs" accessor this time (It will be loaded
// automatically after restart).
err = initFilestoreAccessor(config_obj)
if err != nil {
return err
}
// Create a user with default password
user_record, err := users.NewUserRecord(config_obj, "admin")
if err != nil {
return fmt.Errorf("Unable to create admin user: %w", err)
}
users.SetPassword(user_record, "password")
config_obj.GUI.InitialUsers = append(config_obj.GUI.InitialUsers,
&config_proto.GUIUser{
Name: user_record.Name,
PasswordHash: hex.EncodeToString(user_record.PasswordHash),
PasswordSalt: hex.EncodeToString(user_record.PasswordSalt),
})
// For the GUI org create a separate org.
config_obj.GUI.InitialOrgs = append(config_obj.GUI.InitialOrgs,
&config_proto.InitialOrgRecord{
OrgId: "O123",
Name: "ACME Inc",
Nonce: "ACME",
})
// Write the config for next time
serialized, err := yaml.Marshal(config_obj)
if err != nil {
return err
}
fd, err := os.OpenFile(server_config_path,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Open file %s: %w", server_config_path, err)
}
_, err = fd.Write(serialized)
if err != nil {
return fmt.Errorf("Write file %s: %w", server_config_path, err)
}
fd.Close()
// Now also write a client config
client_config := getClientConfig(config_obj)
client_config.Logging = config_obj.Logging
serialized, err = yaml.Marshal(client_config)
if err != nil {
return err
}
fd, err = os.OpenFile(client_config_path,
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("Open file %s: %w", client_config_path, err)
}
_, err = fd.Write(serialized)
if err != nil {
return fmt.Errorf("Write file %s: %w", client_config_path, err)
}
fd.Close()
}
if config_obj.Services == nil {
config_obj.Services = services.AllServerServicesSpec()
}
// Now start the frontend
ctx, cancel := install_sig_handler()
defer cancel()
// Now start the frontend services
sm, err := startup.StartFrontendServices(ctx, config_obj)
if err != nil {
return fmt.Errorf("starting frontend: %w", err)
}
defer sm.Close()
// Just try to open the browser in the background.
if !*gui_command_no_browser {
go func() {
url := fmt.Sprintf("https://admin:password@%v:%v/",
config_obj.GUI.BindAddress,
config_obj.GUI.BindPort)
res := OpenBrowser(url)
if !res {
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
logger.Error(
"Failed to open browser... you can try to connect directly to %v",
url)
}
}()
}
if !*gui_command_no_client {
*verbose_flag = true
logger := logging.GetLogger(config_obj, &logging.FrontendComponent)
logger.Info("Running client from %v", client_config_path)
// Include the writeback in the client's configuration.
config_obj, err := makeDefaultConfigLoader().
WithRequiredClient().
WithRequiredLogging().
WithFileLoader(client_config_path).
WithWriteback().LoadAndValidate()
if err != nil {
return err
}
sm.Wg.Add(1)
go func() {
RunClient(ctx, config_obj)
sm.Wg.Done()
}()
org_manager, err := services.GetOrgManager()
if err != nil {
return err
}
// Try to start a client in our own org - it may not exist but
// this is not an error.
org_config_obj, err := org_manager.GetOrgConfig("O123")
if err == nil {
org_client_config := &config_proto.Config{
Version: proto.Clone(org_config_obj.Version).(*config_proto.Version),
Client: proto.Clone(org_config_obj.Client).(*config_proto.ClientConfig),
}
// Make sure the client writeback is initialized
writeback_service := writeback.GetWritebackService()
writeback_service.LoadWriteback(org_config_obj)
sm.Wg.Add(1)
go func() {
RunClient(ctx, org_client_config)
sm.Wg.Done()
}()
}
}
sm.Wg.Wait()
return nil
}
func init() {
command_handlers = append(command_handlers, func(command string) bool {
switch command {
case gui_command.FullCommand():
FatalIfError(gui_command, doGUI)
return true
}
return false
})
}