Merge pull request #570 from snovvcrash/codec

Add -codec execution option
main
mpgn 2022-06-17 22:12:54 +02:00 committed by GitHub
commit 055eb25c71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 24 deletions

View File

@ -191,6 +191,11 @@ class smb(connection):
cgroup = smb_parser.add_argument_group("Command Execution", "Options for executing commands")
cgroup.add_argument('--exec-method', choices={"wmiexec", "mmcexec", "smbexec", "atexec"}, default=None, help="method to execute the command. Ignored if in MSSQL mode (default: wmiexec)")
cgroup.add_argument('--codec', default='utf-8', help='Set encoding used (codec) from the target\'s output (default '
'"utf-8"). If errors are detected, run chcp.com at the target, '
'map the result with '
'https://docs.python.org/3/library/codecs.html#standard-encodings and then execute '
'again with --codec and the corresponding codec')
cgroup.add_argument('--force-ps32', action='store_true', help='force the PowerShell command to run in a 32-bit process')
cgroup.add_argument('--no-output', action='store_true', help='do not retrieve command output')
cegroup = cgroup.add_mutually_exclusive_group()
@ -580,7 +585,16 @@ class smb(connection):
if hasattr(self, 'server'): self.server.track_host(self.host)
output = u'{}'.format(exec_method.execute(payload, get_output).strip())
output = exec_method.execute(payload, get_output)
try:
if not isinstance(output, str):
output = output.decode(self.args.codec)
except UnicodeDecodeError:
logging.debug('Decoding error detected, consider running chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings')
output = output.decode('cp437')
output = u'{}'.format(output.strip())
if self.args.execute or self.args.ps_execute:
self.logger.success('Executed command {}'.format('via {}'.format(self.args.exec_method) if self.args.exec_method else ''))

View File

@ -42,13 +42,7 @@ class TSCH_EXEC:
def execute(self, command, output=False):
self.__retOutput = output
self.execute_handler(command)
try:
if isinstance(self.__outputBuffer, str):
return self.__outputBuffer
return self.__outputBuffer.decode()
except UnicodeDecodeError:
logging.debug('Decoding error detected, consider running chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings')
return self.__outputBuffer.decode('cp437')
return self.__outputBuffer
def output_callback(self, data):
self.__outputBuffer = data

View File

@ -52,7 +52,7 @@ class MMCEXEC:
self.__nthash = ''
self.__share_name = share_name
self.__output = None
self.__outputBuffer = ''
self.__outputBuffer = b''
self.__shell = 'c:\\windows\\system32\\cmd.exe'
self.__pwd = 'C:\\'
self.__quit = None
@ -174,7 +174,7 @@ class MMCEXEC:
self.get_output_fileless()
def output_callback(self, data):
self.__outputBuffer += data.decode("utf-8")
self.__outputBuffer += data
def get_output_fileless(self):
if not self.__retOutput: return

View File

@ -76,13 +76,7 @@ class SMBEXEC:
else:
self.execute_remote(command)
self.finish()
try:
if isinstance(self.__outputBuffer, str):
return self.__outputBuffer
return self.__outputBuffer.decode()
except UnicodeDecodeError:
logging.debug('Decoding error detected, consider running chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings')
return self.__outputBuffer.decode('cp437')
return self.__outputBuffer
def output_callback(self, data):

View File

@ -55,13 +55,7 @@ class WMIEXEC:
else:
self.execute_handler(command)
self.__dcom.disconnect()
try:
if isinstance(self.__outputBuffer, str):
return self.__outputBuffer
return self.__outputBuffer.decode()
except UnicodeDecodeError:
logging.debug('Decoding error detected, consider running chcp.com at the target, map the result with https://docs.python.org/3/library/codecs.html#standard-encodings')
return self.__outputBuffer.decode('cp437')
return self.__outputBuffer
def cd(self, s):
self.execute_remote('cd ' + s)