-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add LUKS volume support #404
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c1d3694
Add LUKS volume support
Schamper f522b6f
Remove debug code
Schamper cc0cf63
Add support for opening LUKS with volume key
Schamper 8fa79ef
Fix unit test
Schamper 57c2335
Change keychain list and order for improved logs
Schamper a0e5d6a
Merge branch 'main' into luks-vs
Schamper 73826c7
Improve the error logging in volume.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 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,116 @@ | ||
import logging | ||
import pathlib | ||
from typing import BinaryIO, Iterator, Optional, Union | ||
|
||
from dissect.fve import luks | ||
from dissect.util.stream import AlignedStream | ||
|
||
from dissect.target.exceptions import VolumeSystemError | ||
from dissect.target.helpers.keychain import KeyType | ||
from dissect.target.volume import EncryptedVolumeSystem, Volume | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class LUKSVolumeSystemError(VolumeSystemError): | ||
pass | ||
|
||
|
||
class LUKSVolumeSystem(EncryptedVolumeSystem): | ||
PROVIDER = "luks" | ||
|
||
def __init__(self, fh: Union[BinaryIO, list[BinaryIO]], *args, **kwargs): | ||
super().__init__(fh, *args, **kwargs) | ||
self.luks = luks.LUKS(fh) | ||
|
||
@staticmethod | ||
def _detect(fh: BinaryIO) -> bool: | ||
return luks.is_luks_volume(fh) | ||
|
||
def _volumes(self) -> Iterator[Volume]: | ||
if isinstance(self.fh, Volume): | ||
volume_details = dict( | ||
number=self.fh.number, | ||
offset=self.fh.offset, | ||
vtype=self.fh.type, | ||
name=self.fh.name, | ||
) | ||
else: | ||
volume_details = dict( | ||
number=1, | ||
offset=0, | ||
vtype=None, | ||
name=None, | ||
) | ||
|
||
stream = self.unlock_volume() | ||
yield Volume( | ||
fh=stream, | ||
size=stream.size, | ||
raw=self.fh, | ||
vs=self, | ||
**volume_details, | ||
) | ||
|
||
def unlock_with_volume_encryption_key(self, key: bytes, keyslot: Optional[int] = None) -> None: | ||
try: | ||
if keyslot is None: | ||
for keyslot in self.luks.keyslots.keys(): | ||
try: | ||
self.luks.unlock(key, keyslot) | ||
break | ||
except ValueError: | ||
continue | ||
else: | ||
raise ValueError("Failed to find matching keyslot for provided volume encryption key") | ||
Schamper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
self.luks.unlock(key, keyslot) | ||
|
||
log.debug("Unlocked LUKS volume with provided volume encryption key") | ||
except ValueError: | ||
log.exception("Failed to unlock LUKS volume with provided volume encryption key") | ||
|
||
def unlock_with_passphrase(self, passphrase: str, keyslot: Optional[int] = None) -> None: | ||
try: | ||
self.luks.unlock_with_passphrase(passphrase, keyslot) | ||
log.debug("Unlocked LUKS volume with provided passphrase") | ||
except ValueError: | ||
log.exception("Failed to unlock LUKS volume with provided passphrase") | ||
|
||
def unlock_with_key_file(self, key_file: pathlib.Path, keyslot: Optional[int] = None) -> None: | ||
if not key_file.exists(): | ||
log.error("Provided key file does not exist: %s", key_file) | ||
return | ||
|
||
try: | ||
self.luks.unlock_with_key_file(key_file, keyslot=keyslot) | ||
log.debug("Unlocked LUKS volume with key file %s", key_file) | ||
except ValueError: | ||
log.exception("Failed to unlock LUKS volume with key file %s", key_file) | ||
|
||
def unlock_volume(self) -> AlignedStream: | ||
keyslots = list(map(str, self.luks.keyslots.keys())) | ||
keys = self.get_keys_for_identifiers(keyslots) + self.get_keys_without_identifier() | ||
|
||
for key in keys: | ||
try: | ||
keyslot = int(key.identifier) | ||
except Exception: | ||
keyslot = None | ||
|
||
if key.key_type == KeyType.RAW: | ||
self.unlock_with_volume_encryption_key(key.value, keyslot) | ||
if key.key_type == KeyType.PASSPHRASE: | ||
self.unlock_with_passphrase(key.value, keyslot) | ||
elif key.key_type == KeyType.FILE: | ||
key_file = pathlib.Path(key.value) | ||
self.unlock_with_key_file(key_file, keyslot) | ||
|
||
if self.luks.unlocked: | ||
log.info("Volume %s unlocked with %s (keyslot: %d)", self.fh, key, self.luks._active_keyslot_id) | ||
break | ||
|
||
if self.luks.unlocked: | ||
return self.luks.open() | ||
else: | ||
raise LUKSVolumeSystemError("Failed to unlock LUKS volume") |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not seem to work?
self.unlock_with_passphrase("luks", 0)
ValueError: No valid keyslot found.
Tested on latest Ubuntu. Some usage documentation might be handy.
Also, is there a mechanism to provide a key through an ENV var or option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me, are you using the correct passphrase and keyslot?
Also, the intended way is to use the keychain, for which you can either provide a csv file using
-K <path>
or a value using-Kv <value>
. For example:There's also documentation available here: https://docs.dissect.tools/en/latest/usage/disk-encryption.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't get it to work with the -Kv parameter, let's say I make an empty luks volume like this:
But opening it with:
target-shell /path/to/lucky_luks.img -Kv luks
gives an error (Error: Group descriptor block locations exceed last block), yet:
works as expected?
What do I miss?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For documentation purposes: this was picked up offline and turned out to be a two-fold problem:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed, seems to work now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a warning or error if the LUKS version is 1 because now I can still open it but I will only see an empty disk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I improved the error logging in volume.py to make the existing error more visible.