fix encoding issues in interactive shell

This commit is contained in:
n1nj4sec 2016-04-28 17:46:02 +02:00
parent 1c651ad21c
commit 4780fbcedf
2 changed files with 12 additions and 5 deletions

View File

@ -54,11 +54,10 @@ class InteractiveShell(PupyModule):
program="/system/bin/sh" program="/system/bin/sh"
elif self.client.is_windows(): elif self.client.is_windows():
program="cmd.exe" program="cmd.exe"
encoding="cp437"
if args.program: if args.program:
program=args.program program=args.program
with redirected_stdio(self.client.conn): with redirected_stdio(self.client.conn):
self.client.conn.modules.interactive_shell.interactive_open(program=program, encoding=encoding) self.client.conn.modules.interactive_shell.interactive_open(program=program)
else: #handling tty else: #handling tty
self.client.load_package("ptyshell") self.client.load_package("ptyshell")
self.ps=self.client.conn.modules['ptyshell'].PtyShell() self.ps=self.client.conn.modules['ptyshell'].PtyShell()

View File

@ -7,6 +7,8 @@ from threading import Thread
from Queue import Queue, Empty from Queue import Queue, Empty
import time import time
import traceback import traceback
import locale
import re
import rpyc import rpyc
import os import os
@ -48,17 +50,23 @@ def flush_loop(queue, encoding):
def interactive_open(program=None, encoding=None): def interactive_open(program=None, encoding=None):
try: try:
if program is None: if program is None or program=="cmd.exe":
if "win" in sys.platform.lower(): if "win" in sys.platform.lower():
program="cmd.exe" program="cmd.exe"
encoding="cp437" try:
#couldn't find a better way, none of the following methods worked for me : kernel32.SetConsoleOutputCP(), locale.getpreferredencoding(), sys.stdout.encoding
encoding="cp"+str(re.findall(r".*:\s*([0-9]+)",subprocess.check_output("chcp", shell=True))[0])
except:
pass
else: else:
if "SHELL" in os.environ: if "SHELL" in os.environ:
program=os.environ["SHELL"] program=os.environ["SHELL"]
else: else:
program="/bin/sh" program="/bin/sh"
encoding=None encoding=None
print "Opening interactive %s ... (encoding : %s)"%(program,encoding) if encoding is None:
encoding=locale.getpreferredencoding()
print "Opening interactive %s (with encoding %s)..."%(program,encoding)
if sys.platform=="win32": if sys.platform=="win32":
startupinfo = subprocess.STARTUPINFO() startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW startupinfo.dwFlags = subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW