Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion dissect/target/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,36 @@ def cmd_hash(self, args: argparse.Namespace, stdout: TextIO) -> bool:
print(f"MD5:\t{md5}\nSHA1:\t{sha1}\nSHA256:\t{sha256}", file=stdout)
return False

@arg("path")
def cmd_md5sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
"""print the MD5 checksum of a file provided by a path"""
if not (path := self.check_file(args.path)):
return False

(md5,) = path.get().hash(["md5"])
print(f"{md5} {path!s}", file=stdout)
return False

@arg("path")
def cmd_sha1sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
"""print the SHA1 checksum of a file provided by a path"""
if not (path := self.check_file(args.path)):
return False

(sha1,) = path.get().hash(["sha1"])
print(f"{sha1} {path!s}", file=stdout)
return False

@arg("path")
def cmd_sha256sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
"""print the SHA256 checksum of a file provided by a path"""
if not (path := self.check_file(args.path)):
return False

(sha256,) = path.get().hash(["sha256"])
print(f"{sha256} {path!s}", file=stdout)
return False

@arg("path")
@alias("head")
@alias("more")
Expand Down Expand Up @@ -1482,7 +1512,6 @@ def build_pipe(pipe_parts: list[str], pipe_stdout: int = subprocess.PIPE) -> Ite
On context exit the generator will close the input stream and wait for
the subprocessess to finish.
"""

if not pipe_parts:
raise ValueError("No pipe components provided")

Expand Down
15 changes: 15 additions & 0 deletions tests/tools/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ def test_redirect_simple_ls(tmp_path: Path, target_win: Target, monkeypatch: pyt
assert "sysvol" in content


def test_target_cli_hash_checksums(target_unix: Target, capsys: pytest.CaptureFixture) -> None:
target_unix.fs.map_file_fh("/test-file", BytesIO(b"Hello world!"))

cli = TargetCli(target_unix)

cli.onecmd("md5sum /test-file")
assert "86fb269d190d2c85f6e0468ceca42a20 /test-file" in capsys.readouterr().out

cli.onecmd("sha1sum /test-file")
assert "d3486ae9136e7856bc42212385ea797094475802 /test-file" in capsys.readouterr().out

cli.onecmd("sha256sum /test-file")
assert "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a /test-file" in capsys.readouterr().out


@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific test")
def test_redirect_pipe(tmp_path: Path, target_win: Target, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(fs, "LS_COLORS", {"di": "\033[34m", "fi": "\033[0m"})
Expand Down
Loading