-
Notifications
You must be signed in to change notification settings - Fork 71
Add Rapid7 Velociraptor artifacts plugin #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6f3dbaf
Add initial Rapid7 Velociraptor artifacts plugin
bc5f1e4
Add comment
828705c
Remove comment
eb618f2
Open file-object without `with`
3c6f4b9
Map results
685850e
Add test
a030e0b
Merge branch 'main' into feature/velociraptor_plugin
68ffa6e
Add test
21d4fd2
Merge branch 'main' into feature/velociraptor_plugin
1a3f43d
Merge branch 'main' into feature/velociraptor_plugin
c0705d6
Merge branch 'feature/velociraptor_plugin' of github.com:Zawadidone/d…
0efa557
Merge remote-tracking branch 'upstream/main' into feature/velocirapto…
90e9590
Fix loader and implement suggestions coder review
ae769a0
Fix test
c1d63db
Merge remote-tracking branch 'upstream/main' into feature/velocirapto…
84aada4
Fix docstring
f0e6f11
Linting
0553e0b
Fix loader and tests
908291a
Linting
67cf363
Move Acquire to `apps.edr.acquire`
da87727
Fix nested records
96c8485
Fix typo
e5c03a9
Remove comment
984d598
Remove OS name from record name
e83fc3a
Fix record name
118cf13
Improve log message
41747fa
Apply suggestions code review
a80999a
Merge branch 'main' into feature/velociraptor_plugin
9ec0fd1
Small changes
Schamper 88e7e70
Linting changes
Schamper 588934c
Apply suggestions code review
ca3cb2b
Merge branch 'main' into feature/velociraptor_plugin
f83233f
Merge branch 'feature/velociraptor_plugin' of github.com:Zawadidone/d…
c74c753
Update tests/plugins/apps/edr/test_velociraptor.py
Schamper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Empty file.
This file contains hidden or 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,85 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import csv | ||
| import gzip | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from dissect.target.exceptions import UnsupportedPluginError | ||
| from dissect.target.helpers.record import TargetRecordDescriptor | ||
| from dissect.target.plugin import Plugin, export | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
|
|
||
| from dissect.target.target import Target | ||
|
|
||
| AcquireOpenHandlesRecord = TargetRecordDescriptor( | ||
| "filesystem/acquire_open_handles", | ||
| [ | ||
| ("path", "name"), | ||
| ("string", "handle_type"), | ||
| ("string", "object"), | ||
| ("varint", "unique_process_id"), | ||
| ("varint", "handle_value"), | ||
| ("varint", "granted_access"), | ||
| ("varint", "creator_back_trace_index"), | ||
| ("varint", "object_type_index"), | ||
| ("varint", "handle_attributes"), | ||
| ("varint", "reserved"), | ||
| ], | ||
| ) | ||
|
|
||
| AcquireHashRecord = TargetRecordDescriptor( | ||
| "filesystem/acquire_hash", | ||
| [ | ||
| ("path", "path"), | ||
| ("filesize", "filesize"), | ||
| ("digest", "digest"), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| class AcquirePlugin(Plugin): | ||
| """Returns records from data collected by Acquire.""" | ||
|
|
||
| __namespace__ = "acquire" | ||
|
|
||
| def __init__(self, target: Target): | ||
| super().__init__(target) | ||
| self.hash_file = target.fs.path("$metadata$/file-hashes.csv.gz") | ||
| self.open_handles_file = target.fs.path("$metadata$/open_handles.csv.gz") | ||
|
|
||
| def check_compatible(self) -> None: | ||
| if not self.hash_file.exists() and not self.open_handles_file.exists(): | ||
| raise UnsupportedPluginError("No hash file or open handles found") | ||
|
|
||
| @export(record=AcquireHashRecord) | ||
| def hashes(self) -> Iterator[AcquireHashRecord]: | ||
| """Return file hashes collected by Acquire. | ||
|
|
||
| An Acquire file container contains a file hashes csv when the hashes module was used. The content of this csv | ||
| file is returned. | ||
| """ | ||
| if self.hash_file.exists(): | ||
| with self.hash_file.open() as fh, gzip.open(fh, "rt") as gz_fh: | ||
| for row in csv.DictReader(gz_fh): | ||
| yield AcquireHashRecord( | ||
| path=self.target.fs.path(row["path"]), | ||
| filesize=row["file-size"], | ||
| digest=(row["md5"] or None, row["sha1"] or None, row["sha256"] or None), | ||
| _target=self.target, | ||
| ) | ||
|
|
||
| @export(record=AcquireOpenHandlesRecord) | ||
| def handles(self) -> Iterator[AcquireOpenHandlesRecord]: | ||
| """Return open handles collected by Acquire. | ||
|
|
||
| An Acquire file container contains an open handles csv when the handles module was used. The content of this csv | ||
| file is returned. | ||
| """ | ||
| if self.open_handles_file.exists(): | ||
| with self.open_handles_file.open() as fh, gzip.open(fh, "rt") as gz_fh: | ||
| for row in csv.DictReader(gz_fh): | ||
| if name := row.get("name"): | ||
| row.update({"name": self.target.fs.path(name)}) | ||
| yield AcquireOpenHandlesRecord(**row, _target=self.target) | ||
This file contains hidden or 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,111 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import re | ||
| import urllib.parse | ||
| from functools import lru_cache | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from dissect.target.exceptions import UnsupportedPluginError | ||
| from dissect.target.helpers.record import DynamicDescriptor, TargetRecordDescriptor | ||
| from dissect.target.plugin import Plugin, export | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
|
|
||
| from flow.record import Record | ||
|
|
||
| from dissect.target.target import Target | ||
|
|
||
| VELOCIRAPTOR_RESULTS = "/$velociraptor_results$" | ||
| ISO_8601_PATTERN = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?" | ||
|
|
||
|
|
||
| class VelociraptorRecordBuilder: | ||
| def __init__(self, artifact_name: str): | ||
| self._create_event_descriptor = lru_cache(4096)(self._create_event_descriptor) | ||
| self.record_name = f"velociraptor/{artifact_name}" | ||
|
|
||
| def build(self, object: dict, target: Target) -> TargetRecordDescriptor: | ||
| """Builds a Velociraptor record.""" | ||
| record_values = {} | ||
| record_fields = [] | ||
|
|
||
| record_values["_target"] = target | ||
|
|
||
| for key, value in object.items(): | ||
| # Reserved by flow.record | ||
| if key.startswith("_"): | ||
| continue | ||
|
|
||
| key = key.lower().replace("(", "_").replace(")", "_") | ||
|
|
||
| if re.match(ISO_8601_PATTERN, str(value)): | ||
| record_type = "datetime" | ||
| elif isinstance(value, list): | ||
| record_type = "string[]" | ||
| elif isinstance(value, int): | ||
| record_type = "varint" | ||
| elif key == "hash": | ||
| record_type = "digest" | ||
| value = (value.get("MD5"), value.get("SHA1"), value.get("SHA256")) | ||
| elif isinstance(value, str): | ||
| record_type = "string" | ||
| elif isinstance(value, dict): | ||
| record_type = "record" | ||
| value = self.build(value, target) | ||
| else: | ||
| record_type = "dynamic" | ||
|
|
||
| record_fields.append((record_type, key)) | ||
| record_values[key] = value | ||
|
|
||
| # tuple conversion here is needed for lru_cache | ||
| desc = self._create_event_descriptor(tuple(record_fields)) | ||
| return desc(**record_values) | ||
|
|
||
| def _create_event_descriptor(self, record_fields: list[tuple[str, str]]) -> TargetRecordDescriptor: | ||
| return TargetRecordDescriptor(self.record_name, record_fields) | ||
|
|
||
|
|
||
| class VelociraptorPlugin(Plugin): | ||
| """Returns records from Velociraptor artifacts.""" | ||
|
|
||
| __namespace__ = "velociraptor" | ||
|
|
||
| def __init__(self, target: Target): | ||
| super().__init__(target) | ||
| self.results_dir = target.fs.path(VELOCIRAPTOR_RESULTS) | ||
|
|
||
| def check_compatible(self) -> None: | ||
| if not self.results_dir.exists(): | ||
| raise UnsupportedPluginError("No Velociraptor artifacts found") | ||
|
|
||
| @export(record=DynamicDescriptor(["datetime"])) | ||
| def results(self) -> Iterator[Record]: | ||
| """Return Rapid7 Velociraptor artifacts. | ||
|
|
||
| References: | ||
| - https://docs.velociraptor.app/docs/vql/artifacts/ | ||
| """ | ||
| for artifact in self.results_dir.glob("*.json"): | ||
| # "Windows.KapeFiles.Targets%2FAll\ File\ Metadata.json" becomes "windows_kapefiles_targets" | ||
| artifact_name = ( | ||
| urllib.parse.unquote(artifact.name.removesuffix(".json")).split("/")[0].lower().replace(".", "_") | ||
| ) | ||
| record_builder = VelociraptorRecordBuilder(artifact_name) | ||
|
|
||
| for line in artifact.open("rt"): | ||
| if not (line := line.strip()): | ||
| continue | ||
|
|
||
| try: | ||
| object = json.loads(line) | ||
| yield record_builder.build(object, self.target) | ||
| except json.decoder.JSONDecodeError: | ||
| self.target.log.warning( | ||
| "Could not decode Velociraptor JSON log line in file %s: %s", | ||
| artifact, | ||
| line, | ||
| ) | ||
| continue | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
tests/_data/plugins/apps/edr/velociraptor/Windows.Memory.ProcessInfo.json
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/_data/plugins/apps/edr/velociraptor/windows-uploads.json
Git LFS file not shown
This file contains hidden or 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
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.