From a8274b9b30a513ff0edf8e44bf81bd012b1ef9fd Mon Sep 17 00:00:00 2001 From: allfro Date: Sun, 10 Feb 2013 02:41:03 -0500 Subject: [PATCH] Fixed transform runners to work with new pysudo --- src/canari/commands/common.py | 15 +++++++++ src/canari/commands/debug_transform.py | 16 ++++++--- src/canari/commands/run_transform.py | 25 +++++++------- src/scripts/pysudo | 45 ++++++++++++-------------- 4 files changed, 58 insertions(+), 43 deletions(-) diff --git a/src/canari/commands/common.py b/src/canari/commands/common.py index e85a9ae..f97b0a8 100644 --- a/src/canari/commands/common.py +++ b/src/canari/commands/common.py @@ -1,7 +1,10 @@ #!/usr/bin/env python import os +import subprocess import sys +import unicodedata +from canari.utils.fs import fmutex from pkg_resources import resource_filename from distutils.dist import Distribution @@ -12,6 +15,8 @@ from string import Template from canari.config import CanariConfigParser import threading + + def synchronized(func): func.__lock__ = threading.RLock() @@ -77,6 +82,10 @@ def _detect_settings_dir(d): print('Could not automatically find Maltego\'s settings directory. Use the -w parameter to specify its location, instead.') +def to_utf8(s): + return unicodedata.normalize('NFKD', unicode(s)).encode('ascii', 'ignore') + + def detect_settings_dir(): d = None if sys.platform.startswith('linux'): @@ -90,6 +99,12 @@ def detect_settings_dir(): return d +def sudo(args): + p = subprocess.Popen([os.path.join(get_bin_dir(), 'pysudo')] + args, stdin=subprocess.PIPE) + p.communicate() + return p.returncode + + def read_template(name, values): t = Template(file(resource_filename('canari.resources.template', '%s.plate' % name)).read()) return t.substitute(**values) diff --git a/src/canari/commands/debug_transform.py b/src/canari/commands/debug_transform.py index 10fd698..db6aa7e 100644 --- a/src/canari/commands/debug_transform.py +++ b/src/canari/commands/debug_transform.py @@ -6,8 +6,8 @@ import sys from argparse import ArgumentParser from traceback import format_exc -from canari.maltego.message import MaltegoException, MaltegoTransformResponseMessage -from common import croak, import_transform, cmd_name, console_message, fix_binpath +from canari.maltego.message import MaltegoException, MaltegoTransformResponseMessage, UIMessage +from common import croak, import_transform, cmd_name, console_message, fix_binpath, sudo from canari.maltego.utils import onterminate, parseargs from canari.config import config @@ -17,7 +17,7 @@ __copyright__ = 'Copyright 2012, Canari Project' __credits__ = [] __license__ = 'GPL' -__version__ = '0.2' +__version__ = '0.3' __maintainer__ = 'Nadeem Douba' __email__ = 'ndouba@gmail.com' __status__ = 'Development' @@ -71,8 +71,14 @@ def run(args): m = import_transform(transform) if os.name == 'posix' and hasattr(m.dotransform, 'privileged') and os.geteuid(): - os.execvp('sudo', ['sudo'] + list(sys.argv)) - exit(-1) + rc = sudo(sys.argv) + if rc == 1: + console_message(MaltegoTransformResponseMessage() + UIMessage('User cancelled transform.')) + elif rc == 2: + console_message(MaltegoTransformResponseMessage() + UIMessage('Too many incorrect password attempts.')) + elif rc: + console_message(MaltegoTransformResponseMessage() + UIMessage('Unknown error occurred.')) + exit(rc) if hasattr(m, 'onterminate'): onterminate(m.onterminate) diff --git a/src/canari/commands/run_transform.py b/src/canari/commands/run_transform.py index 68b0055..8696311 100644 --- a/src/canari/commands/run_transform.py +++ b/src/canari/commands/run_transform.py @@ -6,8 +6,8 @@ import sys from argparse import ArgumentParser from traceback import format_exc -from canari.maltego.message import MaltegoException, MaltegoTransformResponseMessage -from common import cmd_name, import_transform, fix_binpath, get_bin_dir +from canari.maltego.message import MaltegoException, MaltegoTransformResponseMessage, UIMessage +from common import cmd_name, import_transform, fix_binpath, get_bin_dir, sudo from canari.maltego.utils import onterminate, parseargs, croak, message from canari.config import config @@ -17,7 +17,7 @@ __copyright__ = 'Copyright 2012, Canari Project' __credits__ = [] __license__ = 'GPL' -__version__ = '0.2' +__version__ = '0.3' __maintainer__ = 'Nadeem Douba' __email__ = 'ndouba@gmail.com' __status__ = 'Development' @@ -73,17 +73,14 @@ def run(args): m = import_transform(transform) if os.name == 'posix' and hasattr(m.dotransform, 'privileged') and os.geteuid(): -# Keep it for another day -# if platform == 'darwin': -# execvp( -# 'osascript', -# ['osascript', '-e', 'do shell script "%s" with administrator privileges' % ' '.join(sys.argv)] -# ) -# if sys.platform.startswith('linux') and path.exists("/usr/bin/gksudo"): -# execvp('/usr/bin/gksudo', ['/usr/bin/gksudo'] + list(sys.argv)) -# else: - os.execvp(pysudo, [pysudo] + list(sys.argv)) - exit(-1) + rc = sudo(sys.argv) + if rc == 1: + message(MaltegoTransformResponseMessage() + UIMessage('User cancelled transform.')) + elif rc == 2: + message(MaltegoTransformResponseMessage() + UIMessage('Too many incorrect password attempts.')) + elif rc: + message(MaltegoTransformResponseMessage() + UIMessage('Unknown error occurred.')) + exit(0) if hasattr(m, 'onterminate'): onterminate(m.onterminate) diff --git a/src/scripts/pysudo b/src/scripts/pysudo index e0b48a9..abb4bd3 100755 --- a/src/scripts/pysudo +++ b/src/scripts/pysudo @@ -1,19 +1,17 @@ #!/usr/bin/env python -from canari.easygui import passwordbox -from canari.utils.fs import fmutex - -from pexpect import spawn, EOF -from sys import argv, stderr -from os import execvp +import sys +import subprocess +from canari.utils.fs import fmutex +from canari.easygui import passwordbox __author__ = 'Nadeem Douba' __copyright__ = 'Copyright 2012, Canari Project' __credits__ = [] __license__ = 'GPL' -__version__ = '0.1' +__version__ = '0.2' __maintainer__ = 'Nadeem Douba' __email__ = 'ndouba@gmail.com' __status__ = 'Development' @@ -21,28 +19,27 @@ __status__ = 'Development' def main(): - if len(argv) == 1: - print 'usage: %s ' % argv[0] + if not sys.argv[1:]: + print 'usage: %s ' % sys.argv[0] exit(-1) - child = None + l = fmutex('pysudo.lock') - try: - l = fmutex('pysudo.lock') - child = spawn('sudo -S echo start') - while not child.expect(['Password:', 'start']): - password = passwordbox('Please enter your password.', 'sudo', '') - child.sendline(password) - del l - execvp('sudo', ['sudo'] + argv[1:]) + p = subprocess.Popen(['sudo', '-S'] + sys.argv[1:], stdin=subprocess.PIPE, stderr=subprocess.PIPE) + p.communicate() - except EOF: - stderr.write(child.before) - except TypeError: - stderr.write('Terminated.') - except Exception, e: - stderr.write(str(e)) + if not p.returncode: + exit(0) + for i in range(0, 3): + password = passwordbox('Please enter your password.', 'sudo', '') + if password is None: + exit(1) + p = subprocess.Popen(['sudo', '-S'] + sys.argv[1:], stdin=subprocess.PIPE) + p.communicate(input='%s\n' % password) + if not p.returncode: + exit(0) + exit(2) if __name__ == '__main__': main() -- 2.45.1