Skip to content

Commit

Permalink
Merge pull request #24 from p0dalirius/add-lcat-lbat-commands
Browse files Browse the repository at this point in the history
[enhancement] Add lcat lbat commands, fixing #22 #23
  • Loading branch information
p0dalirius authored Jun 7, 2024
2 parents 8136239 + 44a55e5 commit bd0b28b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
18 changes: 16 additions & 2 deletions smbclientng/core/CommandCompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class CommandCompleter(object):
commands = {
"bat": {
"description": [
"Pretty prints the contents of a file.",
"Pretty prints the contents of a remote file.",
"Syntax: 'bat <file>'"
],
"subcommands": []
},
"cat": {
"description": [
"Get the contents of a file.",
"Get the contents of a remote file.",
"Syntax: 'cat <file>'"
],
"subcommands": []
Expand Down Expand Up @@ -103,6 +103,20 @@ class CommandCompleter(object):
],
"subcommands": ["server", "share"]
},
"lbat": {
"description": [
"Pretty prints the contents of a local file.",
"Syntax: 'lbat <file>'"
],
"subcommands": []
},
"lcat": {
"description": [
"Get the contents of a local file.",
"Syntax: 'lcat <file>'"
],
"subcommands": []
},
"lcd": {
"description": [
"Changes the current local directory.",
Expand Down
62 changes: 62 additions & 0 deletions smbclientng/core/InteractiveShell.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ def process_command(self, command, arguments=[]):
elif command == "put":
self.command_put(arguments, command)

# Shows the content of a local file
elif command == "lcat":
self.command_lcat(arguments, command)

# Changes the current local directory
elif command == "lcd":
self.command_lcd(arguments, command)
Expand All @@ -182,6 +186,10 @@ def process_command(self, command, arguments=[]):
elif command == "lcp":
self.command_lcp(arguments, command)

# Pretty prints the content of a local file
elif command == "lbat":
self.command_lbat(arguments, command)

# Lists the contents of the current local directory
elif command == "lls":
self.command_lls(arguments, command)
Expand Down Expand Up @@ -383,6 +391,29 @@ def command_info(self, arguments, command):
except impacket.smbconnection.SessionError as e:
print("[!] SMB Error: %s" % e)

@command_arguments_required
def command_lcat(self, arguments, command):
# Command arguments required : Yes
# Active SMB connection needed : No
# SMB share needed : No

path = ' '.join(arguments)
try:
if os.path.exists(path=path):
f = open(path, 'rb')
rawcontents = f.read()
if rawcontents is not None:
encoding = charset_normalizer.detect(rawcontents)["encoding"]
if encoding is not None:
filecontent = rawcontents.decode(encoding).rstrip()
print(filecontent)
else:
print("[!] Could not detect charset of '%s'." % path)
else:
print("[!] Local file '%s' does not exist." % path)
except impacket.smbconnection.SessionError as e:
print("[!] SMB Error: %s" % e)

@command_arguments_required
def command_lcd(self, arguments, command):
# Command arguments required : Yes
Expand Down Expand Up @@ -417,6 +448,37 @@ def command_lcp(self, arguments, command):
else:
self.commandCompleterObject.print_help(command=command)

@command_arguments_required
def command_lbat(self, arguments, command):
# Command arguments required : Yes
# Active SMB connection needed : No
# SMB share needed : No

path = ' '.join(arguments)
try:
if os.path.exists(path=path):
f = open(path, 'rb')
rawcontents = f.read()
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 '<?xml' in filecontent:
lexer = "xml"
elif '<html>' 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)
else:
print("[!] Local file '%s' does not exist." % path)
except impacket.smbconnection.SessionError as e:
print("[!] SMB Error: %s" % e)

def command_lls(self, arguments, command):
# Command arguments required : No
# Active SMB connection needed : No
Expand Down

0 comments on commit bd0b28b

Please sign in to comment.