From 532b909044188dc4c559da9bdd60396e2f9af787 Mon Sep 17 00:00:00 2001 From: "Remi GASCOU (Podalirius)" <79218792+p0dalirius@users.noreply.github.com> Date: Mon, 3 Jun 2024 10:15:20 +0200 Subject: [PATCH] Added 'bat' command with syntax highlighting --- smbclientng/core/CommandCompleter.py | 2 +- smbclientng/core/InteractiveShell.py | 30 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/smbclientng/core/CommandCompleter.py b/smbclientng/core/CommandCompleter.py index b040c61..90f4bf8 100644 --- a/smbclientng/core/CommandCompleter.py +++ b/smbclientng/core/CommandCompleter.py @@ -285,7 +285,7 @@ def complete(self, text, state): if s.lower().startswith(remainder.lower()) ] - elif command in ["get", "rm"]: + elif command in ["bat", "cat", "get", "rm"]: # Choose local files and directories path = "" if '\\' in remainder.strip() or '/' in remainder.strip(): diff --git a/smbclientng/core/InteractiveShell.py b/smbclientng/core/InteractiveShell.py index 3a9b117..392374c 100644 --- a/smbclientng/core/InteractiveShell.py +++ b/smbclientng/core/InteractiveShell.py @@ -17,6 +17,7 @@ import traceback from rich.console import Console from rich.table import Table +from rich.syntax import Syntax from smbclientng.core.CommandCompleter import CommandCompleter from smbclientng.core.utils import b_filesize, unix_permissions, windows_ls_entry, local_tree @@ -227,6 +228,35 @@ def process_command(self, command, arguments=[]): # Commands ================================================================ + @command_arguments_required + @active_smb_connection_needed + @smb_share_is_set + def command_bat(self, arguments, command): + # Command arguments required : Yes + # Active SMB connection needed : Yes + # SMB share needed : Yes + + path = ' '.join(arguments) + try: + rawcontents = self.smbSession.read_file(path=path) + if rawcontents is not None: + encoding = charset_normalizer.detect(rawcontents)["encoding"] + if encoding is not None: + filecontent = rawcontents.decode(encoding).rstrip() + lexer = Syntax.guess_lexer(path=ntpath.basename(path), code=filecontent) + # Some trickery for the files undetected by the lexer + if lexer == "default": + if '' in filecontent: + lexer = "html" + syntax = Syntax(code=filecontent, line_numbers=True, lexer=lexer) + Console().print(syntax) + else: + print("[!] Could not detect charset of '%s'." % path) + except impacket.smbconnection.SessionError as e: + print("[!] SMB Error: %s" % e) + @command_arguments_required @active_smb_connection_needed @smb_share_is_set