forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector_test.go
347 lines (278 loc) · 9.44 KB
/
collector_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
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
package main
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/Velocidex/yaml/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/constants"
"www.velocidex.com/golang/velociraptor/file_store"
"www.velocidex.com/golang/velociraptor/paths"
)
type CollectorTestSuite struct {
suite.Suite
binary string
extension string
tmpdir string
config_file string
config_obj *config_proto.Config
test_server *httptest.Server
}
func (self *CollectorTestSuite) SetupTest() {
if runtime.GOOS == "windows" {
self.extension = ".exe"
}
// Search for a valid binary to run.
binaries, err := filepath.Glob(
"../output/velociraptor*" + constants.VERSION + "-" + runtime.GOOS +
"-" + runtime.GOARCH + self.extension)
assert.NoError(self.T(), err)
if len(binaries) == 0 {
binaries, _ = filepath.Glob("../output/velociraptor*" +
self.extension)
}
self.binary, _ = filepath.Abs(binaries[0])
fmt.Printf("Found binary %v\n", self.binary)
self.tmpdir, err = ioutil.TempDir("", "tmp")
assert.NoError(self.T(), err)
self.config_file = filepath.Join(self.tmpdir, "server.config.yaml")
fd, err := os.OpenFile(
self.config_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
assert.NoError(self.T(), err)
self.config_obj, err = new(config.Loader).
WithFileLoader("../http_comms/test_data/server.config.yaml").
LoadAndValidate()
assert.NoError(self.T(), err)
self.config_obj.Datastore.Implementation = "FileBaseDataStore"
self.config_obj.Datastore.Location = self.tmpdir
self.config_obj.Datastore.FilestoreDirectory = self.tmpdir
self.config_obj.Frontend.DoNotCompressArtifacts = true
// Start a web server that serves the filesystem
self.test_server = httptest.NewServer(
http.FileServer(http.Dir(filepath.Dir(self.binary))))
// Set the server URL correctly.
self.config_obj.Client.ServerUrls = []string{
self.test_server.URL + "/",
}
serialized, err := yaml.Marshal(self.config_obj)
assert.NoError(self.T(), err)
fd.Write(serialized)
fd.Close()
}
func (self *CollectorTestSuite) TearDownTest() {
os.RemoveAll(self.tmpdir)
self.test_server.Close()
}
func (self *CollectorTestSuite) TestCollector() {
OS_TYPE := "Linux"
if runtime.GOOS == "windows" {
OS_TYPE = "Windows"
} else if runtime.GOOS == "darwin" {
OS_TYPE = "Darwin"
}
// Change into the tmpdir
old_dir, _ := os.Getwd()
defer os.Chdir(old_dir)
os.Chdir(self.tmpdir)
// Create a new artifact..
file_store_factory := file_store.GetFileStore(self.config_obj)
fd, err := file_store_factory.WriteFile(paths.GetArtifactDefintionPath(
"Custom.TestArtifactDependent"))
assert.NoError(self.T(), err)
fd.Truncate()
fd.Write([]byte(`name: Custom.TestArtifactDependent
tools:
- name: MyTool
sources:
- query: |
LET binary <= SELECT FullPath, Name
FROM Artifact.Generic.Utils.FetchBinary(
ToolName="MyTool", SleepDuration='0')
SELECT "Foobar", Stdout, binary[0].Name
FROM execve(argv=[binary[0].FullPath, "artifacts", "list"])
`))
fd.Close()
fd, err = file_store_factory.WriteFile(
paths.GetArtifactDefintionPath("Custom.TestArtifact"))
assert.NoError(self.T(), err)
fd.Truncate()
fd.Write([]byte(`name: Custom.TestArtifact
parameters:
- name: MyParameter
default: DefaultMyParameter
- name: MyDefaultParameter
default: DefaultMyDefaultParameter
sources:
- query: |
SELECT *, MyParameter, MyDefaultParameter
FROM Artifact.Custom.TestArtifactDependent()
reports:
- type: HTML
template: |
<html><body><h1>This is the html report template</h1> {{ .main \
}} </body></html>
- type: CLIENT
template: |
# This is the report.
{{ Query "SELECT * FROM source()" | Table }}
{{ $foundit := Query "SELECT * FROM source()" | Expand }}
{{ if $foundit }}
## Found a Scheduled Task
{{ else }}
## Did not find a Scheduled Task!
{{ end }}
`))
fd.Close()
cmd := exec.Command(self.binary, "--config", self.config_file,
"artifacts", "show", "Custom.TestArtifact")
out, err := cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
var os_name string
for _, os_name = range []string{"Windows", "Windows_x86", "Linux", "Darwin"} {
cmd = exec.Command(self.binary, "--config", self.config_file,
"tools", "upload", "--name", "Velociraptor"+os_name,
self.config_file,
"--serve_remote")
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
}
switch runtime.GOOS {
case "windows":
os_name = "Windows"
case "linux":
os_name = "Linux"
case "darwin":
os_name = "Darwin"
}
cmd = exec.Command(self.binary, "--config", self.config_file,
"tools", "upload", "--name", "Velociraptor"+os_name,
self.test_server.URL+"/"+filepath.Base(self.binary),
"--serve_remote")
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
// Make sure the binary is proprly added.
assert.Regexp(self.T(), "name: Velociraptor", string(out))
// Not served locally - download on demand should have no hash
// and serve_locally should be false.
assert.NotRegexp(self.T(), "serve_locally", string(out))
assert.NotRegexp(self.T(), "hash: .+", string(out))
cmd = exec.Command(self.binary, "--config", self.config_file,
"tools", "upload", "--name", "MyTool",
self.test_server.URL+"/"+filepath.Base(self.binary),
"--serve_remote")
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
output_zip := filepath.Join(self.tmpdir, "output.zip")
// Now we want to create a stand alone collector. We do this
// by collecting the Server.Utils.CreateCollector artifact
cmdline := []string{"--config", self.config_file, "-v",
"artifacts", "collect", "Server.Utils.CreateCollector",
"--args", "OS=" + OS_TYPE,
"--args", "artifacts=[\"Custom.TestArtifact\"]",
"--args", "parameters={\"Custom.TestArtifact\":{\"MyParameter\": \"MyValue\"}}",
"--args", "target=ZIP",
"--args", "opt_admin=N",
"--args", "opt_prompt=N",
"--args", "template=Custom.TestArtifact",
"--output", output_zip,
}
cmd = exec.Command(self.binary, cmdline...)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
r, err := zip.OpenReader(output_zip)
assert.NoError(self.T(), err)
defer r.Close()
// Iterate through the files in the archive,
// printing some of their contents.
output_executable := filepath.Join(self.tmpdir, "collector"+self.extension)
for _, f := range r.File {
fmt.Printf("Contents of collector: %s (%v bytes)\n",
f.Name, f.UncompressedSize)
if strings.HasPrefix(f.Name, "Collector") {
fmt.Printf("Extracting %v to %v\n", f.Name, output_executable)
rc, err := f.Open()
assert.NoError(self.T(), err)
out_fd, err := os.OpenFile(
output_executable, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
assert.NoError(self.T(), err)
n, err := io.Copy(out_fd, rc)
assert.NoError(self.T(), err)
rc.Close()
out_fd.Close()
fmt.Printf("Copied %v bytes\n", n)
}
}
// Now just run the executable.
fmt.Printf("Config show\n")
cmd = exec.Command(output_executable, "config", "show")
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
// Now just run the executable.
cmd = exec.Command(output_executable)
out, err = cmd.CombinedOutput()
fmt.Println(string(out))
require.NoError(self.T(), err)
// There should be a collection now.
zip_files, err := filepath.Glob("Collection-*.zip")
assert.NoError(self.T(), err)
assert.Equal(self.T(), 1, len(zip_files))
// Inspect the collection zip file - there should be a single
// artifact output from our custom artifact, and the data it
// produces should have the string Foobar in it.
r, err = zip.OpenReader(zip_files[0])
assert.NoError(self.T(), err)
assert.True(self.T(), len(r.File) > 0)
for _, f := range r.File {
fmt.Printf("Contents of %s:\n", f.Name)
assert.Equal(self.T(), f.Name, "Custom.TestArtifact.json")
rc, err := f.Open()
assert.NoError(self.T(), err)
data, err := ioutil.ReadAll(rc)
assert.NoError(self.T(), err)
assert.Contains(self.T(), string(data), "Foobar")
}
// Inspect the produced HTML report.
html_files, err := filepath.Glob("Collection-*.html")
assert.NoError(self.T(), err)
assert.Equal(self.T(), 1, len(html_files))
html_fd, err := os.Open(html_files[0])
assert.NoError(self.T(), err)
data, err := ioutil.ReadAll(html_fd)
assert.NoError(self.T(), err)
// Ensure the report contains the data that was passed.
assert.Contains(self.T(), string(data), "MyValue")
// And the default parameter is still there.
assert.Contains(self.T(), string(data), "DefaultMyDefaultParameter")
assert.Contains(self.T(), string(data), "Foobar")
assert.Contains(self.T(), string(data), "This is the report")
// Make sure we found the artifact in the report
assert.Contains(self.T(), string(data), "Windows.System.TaskScheduler")
assert.Contains(self.T(), string(data), "Found a Scheduled Task")
// Check that we used the default template from the
// Reporting.Default artifact:
assert.Contains(self.T(), string(data), "<html>")
assert.Contains(self.T(), string(data), "This is the html report template")
// fmt.Println(string(data))
}
func TestCollector(t *testing.T) {
suite.Run(t, &CollectorTestSuite{})
}