forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrogation_test.go
183 lines (154 loc) · 6.57 KB
/
interrogation_test.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
package interrogation
import (
"context"
"testing"
"time"
"github.com/Velocidex/ordereddict"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/datastore"
"www.velocidex.com/golang/velociraptor/file_store/test_utils"
flows_proto "www.velocidex.com/golang/velociraptor/flows/proto"
"www.velocidex.com/golang/velociraptor/paths"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/client_info"
"www.velocidex.com/golang/velociraptor/services/inventory"
"www.velocidex.com/golang/velociraptor/services/journal"
"www.velocidex.com/golang/velociraptor/services/labels"
"www.velocidex.com/golang/velociraptor/services/launcher"
"www.velocidex.com/golang/velociraptor/services/notifications"
"www.velocidex.com/golang/velociraptor/services/repository"
"www.velocidex.com/golang/velociraptor/vtesting"
)
type ServicesTestSuite struct {
suite.Suite
config_obj *config_proto.Config
client_id string
flow_id string
sm *services.Service
}
func (self *ServicesTestSuite) SetupTest() {
var err error
self.config_obj, err = new(config.Loader).WithFileLoader(
"../../http_comms/test_data/server.config.yaml").
WithRequiredFrontend().WithWriteback().
LoadAndValidate()
require.NoError(self.T(), err)
// Start essential services.
ctx, _ := context.WithTimeout(context.Background(), time.Second*60)
self.sm = services.NewServiceManager(ctx, self.config_obj)
require.NoError(self.T(), self.sm.Start(client_info.StartClientInfoService))
require.NoError(self.T(), self.sm.Start(journal.StartJournalService))
require.NoError(self.T(), self.sm.Start(notifications.StartNotificationService))
require.NoError(self.T(), self.sm.Start(inventory.StartInventoryService))
require.NoError(self.T(), self.sm.Start(repository.StartRepositoryManager))
require.NoError(self.T(), self.sm.Start(labels.StartLabelService))
require.NoError(self.T(), self.sm.Start(launcher.StartLauncherService))
require.NoError(self.T(), self.sm.Start(StartInterrogationService))
self.client_id = "C.12312"
self.flow_id = "F.1232"
}
func (self *ServicesTestSuite) TearDownTest() {
self.sm.Close()
test_utils.GetMemoryFileStore(self.T(), self.config_obj).Clear()
test_utils.GetMemoryDataStore(self.T(), self.config_obj).Clear()
}
func (self *ServicesTestSuite) EmulateCollection(
artifact string, rows []*ordereddict.Dict) string {
// Emulate a Generic.Client.Info collection: First write the
// result set, then write the collection context.
// Write a result set for this artifact.
journal, err := services.GetJournal()
assert.NoError(self.T(), err)
journal.PushRowsToArtifact(self.config_obj,
rows, artifact, self.client_id, self.flow_id)
// Emulate a flow completion message coming from the flow processor.
journal.PushRowsToArtifact(self.config_obj,
[]*ordereddict.Dict{ordereddict.NewDict().
Set("ClientId", self.client_id).
Set("FlowId", self.flow_id).
Set("Flow", &flows_proto.ArtifactCollectorContext{
ClientId: self.client_id,
SessionId: self.flow_id,
ArtifactsWithResults: []string{artifact}})},
"System.Flow.Completion", "server", "",
)
return self.flow_id
}
func (self *ServicesTestSuite) TestInterrogationService() {
hostname := "MyHost"
flow_id := self.EmulateCollection(
"Generic.Client.Info/BasicInformation", []*ordereddict.Dict{
ordereddict.NewDict().
Set("ClientId", self.client_id).
Set("Hostname", hostname).
Set("Labels", []string{"Foo"}),
})
// Wait here until the client is fully interrogated
db, err := datastore.GetDB(self.config_obj)
assert.NoError(self.T(), err)
client_path_manager := paths.NewClientPathManager(self.client_id)
client_info := &actions_proto.ClientInfo{}
vtesting.WaitUntil(2*time.Second, self.T(), func() bool {
db.GetSubject(self.config_obj, client_path_manager.Path(), client_info)
return client_info.Hostname == hostname
})
// Check that we record the last flow id.
assert.Equal(self.T(), client_info.LastInterrogateFlowId, flow_id)
// Make sure the labels are updated in the client info
assert.Equal(self.T(), client_info.Labels, []string{"Foo"})
// Check the label is set on the client.
labeler := services.GetLabeler()
assert.True(self.T(), labeler.IsLabelSet(self.config_obj, self.client_id, "Foo"))
assert.NoError(self.T(), err)
}
func (self *ServicesTestSuite) TestEnrollService() {
enroll_message := ordereddict.NewDict().Set("ClientId", self.client_id)
db, err := datastore.GetDB(self.config_obj)
assert.NoError(self.T(), err)
client_path_manager := paths.NewClientPathManager(self.client_id)
client_info := &actions_proto.ClientInfo{}
db.GetSubject(self.config_obj, client_path_manager.Path(), client_info)
assert.Equal(self.T(), client_info.ClientId, "")
// Push many enroll_messages to the internal queue - this will
// trigger the enrollment service to enrolling this client.
// When the system is loaded it may be that multiple
// enrollment messages are being written before the client is
// able to be enrolled. We should always generate only a
// single interrogate flow if the client is not known.
journal, err := services.GetJournal()
assert.NoError(self.T(), err)
err = journal.PushRowsToArtifact(self.config_obj,
[]*ordereddict.Dict{
enroll_message, enroll_message, enroll_message, enroll_message,
},
"Server.Internal.Enrollment",
"server", "")
assert.NoError(self.T(), err)
// Wait here until the client is enrolled
vtesting.WaitUntil(2*time.Second, self.T(), func() bool {
db.GetSubject(self.config_obj, client_path_manager.Path(), client_info)
return client_info.ClientId == self.client_id
})
// Check that a collection is scheduled.
flow_path_manager := paths.NewFlowPathManager(self.client_id,
client_info.LastInterrogateFlowId)
collection_context := &flows_proto.ArtifactCollectorContext{}
err = db.GetSubject(self.config_obj, flow_path_manager.Path(), collection_context)
assert.Equal(self.T(), collection_context.Request.Artifacts,
[]string{"Generic.Client.Info"})
// Make sure only one flow is generated
children, err := db.ListChildren(
self.config_obj, flow_path_manager.ContainerPath(), 0, 100)
assert.NoError(self.T(), err)
assert.Equal(self.T(), children,
[]string{flow_path_manager.ContainerPath() +
"/" + client_info.LastInterrogateFlowId})
}
func TestInterrogationService(t *testing.T) {
suite.Run(t, &ServicesTestSuite{})
}