forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_test.go
299 lines (244 loc) · 8.34 KB
/
binary_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
// Tests for the binary.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"www.velocidex.com/golang/velociraptor/config"
"www.velocidex.com/golang/velociraptor/constants"
)
type MainTestSuite struct {
suite.Suite
binary string
extension string
}
func (self *MainTestSuite) 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 = binaries[0]
fmt.Printf("Found binary %v\n", self.binary)
}
func (self *MainTestSuite) TestHelp() {
cmd := exec.Command(self.binary, "--help")
out, err := cmd.CombinedOutput()
require.NoError(self.T(), err)
require.Contains(self.T(), string(out), "usage:")
}
const autoexec_file = `
autoexec:
argv:
- artifacts
- list
artifact_definitions:
- name: MySpecialArtifact
sources:
- query: SELECT * FROM info()
- name: Windows.Sys.Interfaces
description: Override a built in artifact in the config.
sources:
- query: SELECT "MySpecialInterface" FROM scope()
`
func (self *MainTestSuite) TestAutoexec() {
// Create a tempfile for the repacked binary.
exe, err := ioutil.TempFile("", "exe*"+self.extension)
assert.NoError(self.T(), err)
defer os.Remove(exe.Name())
exe.Close()
// A temp file for the config.
config_file, err := ioutil.TempFile("", "config")
assert.NoError(self.T(), err)
defer os.Remove(config_file.Name())
config_file.Write([]byte(autoexec_file))
config_file.Close()
// Repack the config in the binary.
cmd := exec.Command(self.binary, "config", "repack", config_file.Name(), exe.Name())
out, err := cmd.CombinedOutput()
require.NoError(self.T(), err)
// Run the repacked binary with no args - it should run the
// `artifacts list` command.
cmd = exec.Command(exe.Name())
out, err = cmd.CombinedOutput()
require.NoError(self.T(), err)
// The output should contain MySpecialArtifact as well as the
// standard artifacts.
require.Contains(self.T(), string(out), "MySpecialArtifact")
require.Contains(self.T(), string(out), "Windows.Sys.Users")
// If provided args it works normally.
cmd = exec.Command(exe.Name(),
"artifacts", "collect", "Windows.Sys.Interfaces", "--format", "jsonl")
out, err = cmd.Output()
require.NoError(self.T(), err)
// Config artifacts override built in artifacts.
require.Contains(self.T(), string(out), "MySpecialInterface")
}
func (self *MainTestSuite) TestBuildDeb() {
// A temp file for the generated config.
config_file, err := ioutil.TempFile("", "config")
assert.NoError(self.T(), err)
defer os.Remove(config_file.Name())
cmd := exec.Command(
self.binary, "config", "generate", "--merge",
`{"Client": {"nonce": "Foo", "writeback_linux": "some_location"}}`)
out, err := cmd.Output()
require.NoError(self.T(), err)
// Write the config to the tmp file
config_file.Write(out)
config_file.Close()
// Create a tempfile for the binary executable.
binary_file, err := ioutil.TempFile("", "binary")
assert.NoError(self.T(), err)
defer os.Remove(binary_file.Name())
binary_file.Write([]byte("\x7f\x45\x4c\x46XXXXXXXXXX"))
binary_file.Close()
output_file, err := ioutil.TempFile("", "output*.deb")
assert.NoError(self.T(), err)
output_file.Close()
defer os.Remove(output_file.Name())
cmd = exec.Command(
self.binary, "--config", config_file.Name(),
"debian", "client", "--binary", binary_file.Name(),
"--output", output_file.Name())
_, err = cmd.Output()
require.NoError(self.T(), err)
// Make sure the file is written
fd, err := os.Open(output_file.Name())
assert.NoError(self.T(), err)
stat, err := fd.Stat()
assert.NoError(self.T(), err)
assert.Greater(self.T(), stat.Size(), int64(0))
// Now the server deb
output_file, err = ioutil.TempFile("", "output*.deb")
assert.NoError(self.T(), err)
output_file.Close()
defer os.Remove(output_file.Name())
cmd = exec.Command(
self.binary, "--config", config_file.Name(),
"debian", "server", "--binary", binary_file.Name(),
"--output", output_file.Name())
_, err = cmd.Output()
require.NoError(self.T(), err)
// Make sure the file is written
fd, err = os.Open(output_file.Name())
assert.NoError(self.T(), err)
stat, err = fd.Stat()
assert.NoError(self.T(), err)
assert.Greater(self.T(), stat.Size(), int64(0))
}
func (self *MainTestSuite) TestGenerateConfigWithMerge() {
// A temp file for the generated config.
config_file, err := ioutil.TempFile("", "config")
assert.NoError(self.T(), err)
defer os.Remove(config_file.Name())
defer config_file.Close()
cmd := exec.Command(
self.binary, "config", "generate", "--merge",
`{"Client": {"nonce": "Foo", "writeback_linux": "some_location"}}`)
out, err := cmd.Output()
require.NoError(self.T(), err)
// Write the config to the tmp file
config_file_content := out
config_file.Write(out)
config_file.Close()
// Try to load it now.
config_obj, err := new(config.Loader).WithFileLoader(config_file.Name()).
WithRequiredFrontend().WithRequiredCA().WithRequiredClient().
LoadAndValidate()
require.NoError(self.T(), err)
require.Equal(self.T(), config_obj.Client.Nonce, "Foo")
require.Equal(self.T(), config_obj.Client.WritebackLinux, "some_location")
// Try to show a config without a flag.
cmd = exec.Command(self.binary, "config", "show")
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG=",
)
out, err = cmd.Output()
require.Error(self.T(), err)
// Specify the config on the commandline
cmd = exec.Command(self.binary, "config", "show", "--config", config_file.Name())
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG=",
)
out, err = cmd.Output()
require.NoError(self.T(), err)
require.Contains(self.T(), string(out), "Foo")
// Specify the config in the environment
cmd = exec.Command(self.binary, "config", "show")
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG="+config_file.Name(),
)
out, err = cmd.Output()
require.NoError(self.T(), err)
require.Contains(self.T(), string(out), "Foo")
// Specifying invalid config in the flag is a hard stop - even
// if there is a valid environ.
cmd = exec.Command(self.binary, "config", "show", "--config", "XXXX")
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG="+config_file.Name(),
)
out, err = cmd.Output()
require.Error(self.T(), err)
// Create a tempfile for the repacked binary.
exe, err := ioutil.TempFile("", "exe*"+self.extension)
assert.NoError(self.T(), err)
defer os.Remove(exe.Name())
exe.Close()
// Repack the config in the binary.
cmd = exec.Command(self.binary, "config", "repack", config_file.Name(), exe.Name())
out, err = cmd.CombinedOutput()
require.NoError(self.T(), err)
// Run the repacked binary with invalid environ - config
// should come from embedded.
cmd = exec.Command(exe.Name(), "config", "show")
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG=XXXX",
)
out, err = cmd.Output()
require.NoError(self.T(), err)
require.Contains(self.T(), string(out), "Foo")
// Make second copy of config file and store modified version
second_config_file, err := ioutil.TempFile("", "config")
assert.NoError(self.T(), err)
defer os.Remove(second_config_file.Name())
// Second file has no Foo in it
second_config_file_content := bytes.ReplaceAll(
config_file_content, []byte(`Foo`), []byte(`Bar`))
second_config_file.Write(second_config_file_content)
second_config_file.Close()
// Check that embedded binary with config will use its
// embedded version, even if an env var is specified.
cmd = exec.Command(exe.Name(), "config", "show")
cmd.Env = append(os.Environ(),
"VELOCIRAPTOR_CONFIG="+second_config_file.Name(),
)
out, err = cmd.Output()
require.NoError(self.T(), err)
require.Contains(self.T(), string(out), "Foo")
// If a config is explicitly specified, it will override even the
// embedded config.
cmd = exec.Command(exe.Name(), "config", "show", "--config", second_config_file.Name())
out, err = cmd.Output()
require.NoError(self.T(), err)
// loaded config contains Bar
require.Contains(self.T(), string(out), "Bar")
}
func TestMainProgram(t *testing.T) {
suite.Run(t, &MainTestSuite{})
}