forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_uploader.go
389 lines (332 loc) · 9 KB
/
client_uploader.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
package uploads
import (
"context"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"os"
"time"
"www.velocidex.com/golang/velociraptor/accessors"
actions_proto "www.velocidex.com/golang/velociraptor/actions/proto"
"www.velocidex.com/golang/velociraptor/constants"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/responder"
"www.velocidex.com/golang/vfilter"
)
var (
BUFF_SIZE = int64(1024 * 1024)
UPLOAD_CTX = "__uploads"
)
// An uploader delivering files from client to server.
type VelociraptorUploader struct {
Responder responder.Responder
Count int
}
func (self *VelociraptorUploader) Upload(
ctx context.Context,
scope vfilter.Scope,
filename *accessors.OSPath,
accessor string,
store_as_name *accessors.OSPath,
expected_size int64,
mtime time.Time,
atime time.Time,
ctime time.Time,
btime time.Time,
mode os.FileMode,
reader io.Reader) (
*UploadResponse, error) {
if mode.IsDir() {
return nil, fmt.Errorf("%w: Directories not supported",
fs.ErrInvalid)
}
if accessor == "" {
accessor = "auto"
}
if store_as_name == nil {
store_as_name = filename
}
cached, pres, closer := DeduplicateUploads(scope, store_as_name)
defer closer()
if pres {
return cached, nil
}
upload_id := self.Responder.NextUploadId()
// Try to collect sparse files if possible
result, err := self.maybeUploadSparse(
ctx, scope, filename, accessor, store_as_name,
expected_size, mtime, upload_id, reader)
if err == nil {
CacheUploadResult(scope, store_as_name, result)
return result, nil
}
result = &UploadResponse{
StoredName: store_as_name.String(),
Accessor: accessor,
Components: store_as_name.Components[:],
}
if accessor != "data" {
result.Path = filename.String()
}
offset := uint64(0)
self.Count += 1
md5_sum := md5.New()
sha_sum := sha256.New()
for {
// Ensure there is a fresh allocation for every
// iteration to prevent overwriting in flight buffers.
buffer := make([]byte, BUFF_SIZE)
read_bytes, err := reader.Read(buffer)
if err != nil && err != io.EOF {
return nil, err
}
data := buffer[:read_bytes]
_, err = sha_sum.Write(data)
if err != nil {
return nil, err
}
_, err = md5_sum.Write(data)
if err != nil {
return nil, err
}
packet := &actions_proto.FileBuffer{
Pathspec: &actions_proto.PathSpec{
Path: store_as_name.String(),
Components: store_as_name.Components,
Accessor: accessor,
},
Offset: offset,
Size: uint64(expected_size),
StoredSize: offset + uint64(len(data)),
Mtime: mtime.UnixNano(),
Atime: atime.UnixNano(),
Ctime: ctime.UnixNano(),
Btime: btime.UnixNano(),
Data: data,
DataLength: uint64(len(data)),
// The number of the upload within the flow.
UploadNumber: upload_id,
Eof: read_bytes == 0,
}
select {
case <-ctx.Done():
return nil, errors.New("Cancelled!")
default:
// Send the packet to the server.
self.Responder.AddResponse(&crypto_proto.VeloMessage{
RequestId: constants.TransferWellKnownFlowId,
FileBuffer: packet})
}
offset += uint64(read_bytes)
if err != nil && err != io.EOF {
return nil, err
}
// On the last packet send back the hashes into the query.
if read_bytes == 0 {
result.Size = offset
result.StoredSize = offset
result.Sha256 = hex.EncodeToString(sha_sum.Sum(nil))
result.Md5 = hex.EncodeToString(md5_sum.Sum(nil))
CacheUploadResult(scope, store_as_name, result)
return result, nil
}
}
}
func (self *VelociraptorUploader) maybeUploadSparse(
ctx context.Context,
scope vfilter.Scope,
filename *accessors.OSPath,
accessor string,
store_as_name *accessors.OSPath,
ignored_expected_size int64,
mtime time.Time,
upload_id int64,
reader io.Reader) (
*UploadResponse, error) {
// Can the reader produce ranges?
range_reader, ok := reader.(RangeReader)
if !ok {
return nil, errors.New("Not supported")
}
index := &actions_proto.Index{}
if store_as_name == nil {
store_as_name = filename
}
// This is the response that will be passed into the VQL
// engine.
result := &UploadResponse{
StoredName: store_as_name.String(),
Components: store_as_name.Components,
Accessor: accessor,
}
if accessor != "data" {
result.Path = filename.String()
}
self.Count += 1
md5_sum := md5.New()
sha_sum := sha256.New()
// Does the index contain any sparse runs?
is_sparse := false
// Read from the sparse file with read_offset and write to the
// output file at write_offset. All ranges are written back to
// back skipping sparse ranges. The index file will allow
// users to reconstruct the sparse file if needed.
read_offset := int64(0)
write_offset := int64(0)
// Adjust the expected size properly to the sum of all
// non-sparse ranges and build the index file.
ranges := range_reader.Ranges()
// Inspect the ranges and prepare an index.
expected_size := int64(0)
real_size := int64(0)
for _, rng := range ranges {
file_length := rng.Length
if rng.IsSparse {
file_length = 0
}
index.Ranges = append(index.Ranges,
&actions_proto.Range{
FileOffset: expected_size,
OriginalOffset: rng.Offset,
FileLength: file_length,
Length: rng.Length,
})
if !rng.IsSparse {
expected_size += rng.Length
} else {
is_sparse = true
}
if real_size < rng.Offset+rng.Length {
real_size = rng.Offset + rng.Length
}
}
// No ranges - just send a placeholder.
if expected_size == 0 {
if !is_sparse {
index = nil
}
self.Responder.AddResponse(&crypto_proto.VeloMessage{
RequestId: constants.TransferWellKnownFlowId,
FileBuffer: &actions_proto.FileBuffer{
Pathspec: &actions_proto.PathSpec{
Path: store_as_name.String(),
Components: store_as_name.Components,
Accessor: accessor,
},
Size: uint64(real_size),
StoredSize: uint64(expected_size),
IsSparse: is_sparse,
Index: index,
Mtime: mtime.UnixNano(),
Eof: true,
UploadNumber: upload_id,
},
})
result.Size = uint64(real_size)
result.Sha256 = hex.EncodeToString(sha_sum.Sum(nil))
result.Md5 = hex.EncodeToString(md5_sum.Sum(nil))
return result, nil
}
// Send each range separately
for _, rng := range ranges {
// Ignore sparse ranges
if rng.IsSparse {
continue
}
// Range is not sparse - send it one buffer at the time.
to_read := rng.Length
read_offset = rng.Offset
_, err := range_reader.Seek(read_offset, io.SeekStart)
if err != nil {
return nil, err
}
for to_read > 0 {
to_read_buf := to_read
// Ensure there is a fresh allocation for every
// iteration to prevent overwriting in-flight buffers.
if to_read_buf > BUFF_SIZE {
to_read_buf = BUFF_SIZE
}
buffer := make([]byte, to_read_buf)
read_bytes, err := range_reader.Read(buffer)
// Hard read error - give up.
if err != nil && err != io.EOF {
return nil, err
}
// End of range - go to the next range
if read_bytes == 0 || err == io.EOF {
to_read = 0
continue
}
data := buffer[:read_bytes]
_, err = sha_sum.Write(data)
if err != nil {
return nil, err
}
_, err = md5_sum.Write(data)
if err != nil {
return nil, err
}
packet := &actions_proto.FileBuffer{
Pathspec: &actions_proto.PathSpec{
Path: store_as_name.String(),
Components: store_as_name.Components,
Accessor: accessor,
},
Offset: uint64(write_offset),
Size: uint64(real_size),
StoredSize: uint64(expected_size),
IsSparse: is_sparse,
Mtime: mtime.UnixNano(),
Data: data,
DataLength: uint64(len(data)),
UploadNumber: upload_id,
}
select {
case <-ctx.Done():
return nil, errors.New("Cancelled!")
default:
// Send the packet to the server.
self.Responder.AddResponse(&crypto_proto.VeloMessage{
RequestId: constants.TransferWellKnownFlowId,
FileBuffer: packet})
}
to_read -= int64(read_bytes)
write_offset += int64(read_bytes)
read_offset += int64(read_bytes)
}
}
// We did a sparse file, upload the index as well.
if !is_sparse {
index = nil
}
// Send an EOF as the last packet with no data. If the file
// was sparse, also include the index in this packet. NOTE:
// There should be only one EOF packet.
self.Responder.AddResponse(&crypto_proto.VeloMessage{
RequestId: constants.TransferWellKnownFlowId,
FileBuffer: &actions_proto.FileBuffer{
Pathspec: &actions_proto.PathSpec{
Path: store_as_name.String(),
Components: store_as_name.Components,
Accessor: accessor,
},
Size: uint64(real_size),
StoredSize: uint64(write_offset),
IsSparse: is_sparse,
Offset: uint64(write_offset),
Index: index,
Eof: true,
UploadNumber: upload_id,
},
})
result.Size = uint64(real_size)
result.StoredSize = uint64(write_offset)
result.Sha256 = hex.EncodeToString(sha_sum.Sum(nil))
result.Md5 = hex.EncodeToString(md5_sum.Sum(nil))
return result, nil
}