Skip to content

Commit 2294418

Browse files
authored
Add individual hash commands to target-shell (#1517)
1 parent f787f5a commit 2294418

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

dissect/target/tools/shell.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,36 @@ def cmd_hash(self, args: argparse.Namespace, stdout: TextIO) -> bool:
11651165
print(f"MD5:\t{md5}\nSHA1:\t{sha1}\nSHA256:\t{sha256}", file=stdout)
11661166
return False
11671167

1168+
@arg("path")
1169+
def cmd_md5sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
1170+
"""print the MD5 checksum of a file provided by a path"""
1171+
if not (path := self.check_file(args.path)):
1172+
return False
1173+
1174+
(md5,) = path.get().hash(["md5"])
1175+
print(f"{md5} {path!s}", file=stdout)
1176+
return False
1177+
1178+
@arg("path")
1179+
def cmd_sha1sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
1180+
"""print the SHA1 checksum of a file provided by a path"""
1181+
if not (path := self.check_file(args.path)):
1182+
return False
1183+
1184+
(sha1,) = path.get().hash(["sha1"])
1185+
print(f"{sha1} {path!s}", file=stdout)
1186+
return False
1187+
1188+
@arg("path")
1189+
def cmd_sha256sum(self, args: argparse.Namespace, stdout: TextIO) -> bool:
1190+
"""print the SHA256 checksum of a file provided by a path"""
1191+
if not (path := self.check_file(args.path)):
1192+
return False
1193+
1194+
(sha256,) = path.get().hash(["sha256"])
1195+
print(f"{sha256} {path!s}", file=stdout)
1196+
return False
1197+
11681198
@arg("path")
11691199
@alias("head")
11701200
@alias("more")
@@ -1482,7 +1512,6 @@ def build_pipe(pipe_parts: list[str], pipe_stdout: int = subprocess.PIPE) -> Ite
14821512
On context exit the generator will close the input stream and wait for
14831513
the subprocessess to finish.
14841514
"""
1485-
14861515
if not pipe_parts:
14871516
raise ValueError("No pipe components provided")
14881517

tests/tools/test_shell.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ def test_redirect_simple_ls(tmp_path: Path, target_win: Target, monkeypatch: pyt
257257
assert "sysvol" in content
258258

259259

260+
def test_target_cli_hash_checksums(target_unix: Target, capsys: pytest.CaptureFixture) -> None:
261+
target_unix.fs.map_file_fh("/test-file", BytesIO(b"Hello world!"))
262+
263+
cli = TargetCli(target_unix)
264+
265+
cli.onecmd("md5sum /test-file")
266+
assert "86fb269d190d2c85f6e0468ceca42a20 /test-file" in capsys.readouterr().out
267+
268+
cli.onecmd("sha1sum /test-file")
269+
assert "d3486ae9136e7856bc42212385ea797094475802 /test-file" in capsys.readouterr().out
270+
271+
cli.onecmd("sha256sum /test-file")
272+
assert "c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a /test-file" in capsys.readouterr().out
273+
274+
260275
@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific test")
261276
def test_redirect_pipe(tmp_path: Path, target_win: Target, monkeypatch: pytest.MonkeyPatch) -> None:
262277
monkeypatch.setattr(fs, "LS_COLORS", {"di": "\033[34m", "fi": "\033[0m"})

0 commit comments

Comments
 (0)