forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug in uploading sparse files. (Velocidex#519)
* Fixed bug in uploading sparse files. This affected files fetched with NTFS which spanned multiple runs. * Added test.
- Loading branch information
Showing
13 changed files
with
319 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package responder | ||
|
||
import crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto" | ||
|
||
func TestResponder() *Responder { | ||
return &Responder{ | ||
output: make(chan *crypto_proto.GrrMessage, 100), | ||
request: &crypto_proto.GrrMessage{}, | ||
} | ||
} | ||
|
||
func GetTestResponses(self *Responder) []*crypto_proto.GrrMessage { | ||
close(self.output) | ||
result := []*crypto_proto.GrrMessage{} | ||
for item := range self.output { | ||
result = append(result, item) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package uploads | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert" | ||
"github.com/sebdah/goldie" | ||
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto" | ||
"www.velocidex.com/golang/velociraptor/json" | ||
"www.velocidex.com/golang/velociraptor/responder" | ||
vql_subsystem "www.velocidex.com/golang/velociraptor/vql" | ||
) | ||
|
||
type TestRangeReader struct { | ||
*bytes.Reader | ||
ranges []Range | ||
} | ||
|
||
func (self *TestRangeReader) Ranges() []Range { | ||
return self.ranges | ||
} | ||
|
||
// Combine the output of all fragments into a strings | ||
func CombineOutput(name string, responses []*crypto_proto.GrrMessage) string { | ||
result := []byte{} | ||
|
||
for _, item := range responses { | ||
if item.FileBuffer.Pathspec.Path == name { | ||
result = append(result, item.FileBuffer.Data...) | ||
} | ||
} | ||
|
||
return string(result) | ||
} | ||
|
||
func TestClientUploaderSparse(t *testing.T) { | ||
resp := responder.TestResponder() | ||
uploader := &VelociraptorUploader{ | ||
Responder: resp, | ||
} | ||
|
||
BUFF_SIZE = 10000 | ||
|
||
reader := &TestRangeReader{ | ||
Reader: bytes.NewReader([]byte( | ||
"Hello world hello world")), | ||
ranges: []Range{ | ||
{Offset: 0, Length: 6, IsSparse: false}, | ||
{Offset: 6, Length: 6, IsSparse: true}, | ||
{Offset: 12, Length: 6, IsSparse: false}, | ||
}, | ||
} | ||
range_reader, ok := interface{}(reader).(RangeReader) | ||
assert.Equal(t, ok, true) | ||
ctx := context.Background() | ||
scope := vql_subsystem.MakeScope() | ||
uploader.maybeUploadSparse(ctx, scope, | ||
"foo", "ntfs", "", 1000, range_reader) | ||
responses := responder.GetTestResponses(resp) | ||
|
||
// Expected size is the combined sum of all ranges with data | ||
// in them | ||
assert.Equal(t, responses[0].FileBuffer.Size, uint64(12)) | ||
|
||
assert.Equal(t, CombineOutput("foo", responses), | ||
"Hello hello ") | ||
goldie.Assert(t, "ClientUploaderSparse", | ||
json.MustMarshalIndent(responses)) | ||
assert.NotEqual(t, CombineOutput("foo.idx", responses), "") | ||
} | ||
|
||
func TestClientUploaderSparseMultiBuffer(t *testing.T) { | ||
resp := responder.TestResponder() | ||
uploader := &VelociraptorUploader{ | ||
Responder: resp, | ||
} | ||
|
||
// 2 bytes per message | ||
BUFF_SIZE = 2 | ||
reader := &TestRangeReader{ | ||
Reader: bytes.NewReader([]byte( | ||
"Hello world hello world")), | ||
ranges: []Range{ | ||
{Offset: 0, Length: 6, IsSparse: false}, | ||
{Offset: 6, Length: 6, IsSparse: true}, | ||
{Offset: 12, Length: 6, IsSparse: false}, | ||
}, | ||
} | ||
range_reader, ok := interface{}(reader).(RangeReader) | ||
assert.Equal(t, ok, true) | ||
ctx := context.Background() | ||
scope := vql_subsystem.MakeScope() | ||
uploader.maybeUploadSparse(ctx, scope, | ||
"foo", "ntfs", "", 1000, range_reader) | ||
responses := responder.GetTestResponses(resp) | ||
assert.Equal(t, CombineOutput("foo", responses), "Hello hello ") | ||
goldie.Assert(t, "ClientUploaderSparseMultiBuffer", | ||
json.MustMarshalIndent(responses)) | ||
assert.NotEqual(t, CombineOutput("foo.idx", responses), "") | ||
} | ||
|
||
func TestClientUploaderNoIndexIfNotSparse(t *testing.T) { | ||
resp := responder.TestResponder() | ||
uploader := &VelociraptorUploader{ | ||
Responder: resp, | ||
} | ||
|
||
// 2 bytes per message | ||
BUFF_SIZE = 2 | ||
reader := &TestRangeReader{ | ||
Reader: bytes.NewReader([]byte( | ||
"Hello world hello world")), | ||
ranges: []Range{ | ||
{Offset: 0, Length: 6, IsSparse: false}, | ||
{Offset: 12, Length: 6, IsSparse: false}, | ||
}, | ||
} | ||
range_reader, ok := interface{}(reader).(RangeReader) | ||
assert.Equal(t, ok, true) | ||
ctx := context.Background() | ||
scope := vql_subsystem.MakeScope() | ||
uploader.maybeUploadSparse(ctx, scope, | ||
"foo", "ntfs", "", 1000, range_reader) | ||
responses := responder.GetTestResponses(resp) | ||
assert.Equal(t, CombineOutput("foo", responses), "Hello hello ") | ||
|
||
// No idx written when there are no sparse ranges. | ||
assert.Equal(t, CombineOutput("foo.idx", responses), "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[ | ||
{ | ||
"request_id": 5, | ||
"FileBuffer": { | ||
"pathspec": { | ||
"path": "foo", | ||
"accessor": "ntfs" | ||
}, | ||
"size": 12, | ||
"data": "SGVsbG8g" | ||
} | ||
}, | ||
{ | ||
"request_id": 5, | ||
"response_id": 1, | ||
"FileBuffer": { | ||
"pathspec": { | ||
"path": "foo", | ||
"accessor": "ntfs" | ||
}, | ||
"offset": 6, | ||
"size": 12, | ||
"data": "aGVsbG8g", | ||
"eof": true | ||
} | ||
}, | ||
{ | ||
"request_id": 5, | ||
"response_id": 2, | ||
"FileBuffer": { | ||
"pathspec": { | ||
"path": "foo.idx", | ||
"accessor": "ntfs" | ||
}, | ||
"size": 196, | ||
"data": "eyJmaWxlX29mZnNldCI6MCwib3JpZ2luYWxfb2Zmc2V0IjowLCJmaWxlX2xlbmd0aCI6NiwibGVuZ3RoIjo2fQp7ImZpbGVfb2Zmc2V0Ijo2LCJvcmlnaW5hbF9vZmZzZXQiOjYsImZpbGVfbGVuZ3RoIjowLCJsZW5ndGgiOjZ9CnsiZmlsZV9vZmZzZXQiOjYsIm9yaWdpbmFsX29mZnNldCI6MTIsImZpbGVfbGVuZ3RoIjo2LCJsZW5ndGgiOjZ9Cg==", | ||
"eof": true | ||
} | ||
} | ||
] |
Oops, something went wrong.