def load_plume_package(opts): with PushDir(opts.plume_dir): if not os.path.exists('canari.conf'): print('Plume does not appear to be installed in %s.' % opts.plume_dir, file=sys.stderr) print("Please run 'canari install-plume' and try again.", file=sys.stderr) exit(-1) transform_package = None try: transform_package = TransformDistribution(opts.package) except ValueError as e: print('An error occurred', e, file=sys.stderr) exit(-1) if transform_package.has_remote_transforms: try: transform_package.configure(opts.plume_dir, remote=True, defaults=opts.accept_defaults) except ImportError as e: print('An error occurred while trying to import %r from %s: %s' % ( transform_package.name, opts.plume_dir, e ), file=sys.stderr) print('Please make sure that %r is importable from %s' % (transform_package.name, opts.plume_dir), file=sys.stderr) exit(-1) print('Please restart plume for changes to take effect.', file=sys.stderr) exit(0) print('Error: no remote transforms found. ' 'Please make sure that at least one transform has remote=True set before retrying.', file=sys.stderr) exit(-1)
def load_plume_package(opts): if not os.path.exists(os.path.join(opts.plume_dir, 'plume.py')): print('Plume does not appear to be installed in %s.' % opts.plume_dir) ans = parse_bool("Would you like to install it [Y/n]: ") if not ans: print 'Installation cancelled. Quitting...' exit(-1) os.system('canari install-plume --install-dir %s' % opts.plume_dir) opts.plume_dir = os.path.join(opts.plume_dir, 'plume') transform_package = None try: transform_package = TransformDistribution(opts.package) except ValueError: exit(-1) if transform_package.has_remote_transforms: try: transform_package.configure(opts.plume_dir, remote=True) except ImportError: pass print('Please restart plume (./plume.sh restart) for changes to take effect.') exit(0) print ('Error: no remote transforms found. ' 'Please make sure that at least one transform has remote=True in @configure before retrying.') exit(-1)
def load_plume_package(opts): with PushDir(opts.plume_dir): if not os.path.exists('canari.conf'): print('Plume does not appear to be installed in %s.' % opts.plume_dir) print("Please run 'canari install-plume' and try again.") exit(-1) transform_package = None try: transform_package = TransformDistribution(opts.package) except ValueError, e: print 'An error occurred', e exit(-1) if transform_package.has_remote_transforms: try: transform_package.configure(opts.plume_dir, remote=True, defaults=opts.accept_defaults) except ImportError, e: print 'An error occurred while trying to import %r from %s: %s' % ( transform_package.name, opts.plume_dir, e ) print 'Please make sure that %r is importable from %s' % (transform_package.name, opts.plume_dir) exit(-1) print('Please restart plume for changes to take effect.') exit(0)
def load_plume_package(opts): if not os.path.exists(os.path.join(opts.plume_dir, 'plume.py')): print('Plume does not appear to be installed in %s.' % opts.plume_dir) ans = parse_bool("Would you like to install it [Y/n]: ") if not ans: print 'Installation cancelled. Quitting...' exit(-1) os.system('canari install-plume --install-dir %s' % opts.plume_dir) opts.plume_dir = os.path.join(opts.plume_dir, 'plume') transform_package = None try: transform_package = TransformDistribution(opts.package) except ValueError: exit(-1) if transform_package.has_remote_transforms: try: transform_package.configure(opts.plume_dir, remote=True) except ImportError: pass print( 'Please restart plume (./plume.sh restart) for changes to take effect.' ) exit(0) print( 'Error: no remote transforms found. ' 'Please make sure that at least one transform has remote=True in @configure before retrying.' ) exit(-1)
def uninstall_package(args): opts = parse_args(args) try: transform_package = TransformDistribution(opts.package) transform_package.uninstall(opts.working_dir, opts.settings_dir) except ValueError, e: print str(e) exit(-1)
def install_package(args): opts = parse_args(args) try: with pushd(opts.working_dir or os.getcwd()): transform_package = TransformDistribution(opts.package) transform_package.install(opts.working_dir, opts.settings_dir) except ValueError, e: print str(e) exit(-1)
def create_profile(args): opts = parse_args(args) current_dir = os.getcwd() try: with pushd(opts.working_dir or current_dir): transform_package = TransformDistribution(opts.package) transform_package.create_profile(opts.working_dir, current_dir) except ValueError, e: print(str(e)) exit(-1)
def create_profile(args): opts = parse_args(args) current_dir = os.getcwd() try: with pushd(opts.working_dir or current_dir): transform_package = TransformDistribution(opts.package) transform_package.create_profile(opts.working_dir, current_dir) except ValueError, e: print(str(e)) exit(-1)
def create_profile(args): set_canari_mode(CanariMode.Local) opts = parse_args(args) current_dir = os.getcwd() try: with PushDir(opts.working_dir or current_dir): transform_package = TransformDistribution(opts.package) transform_package.create_profile(opts.working_dir, current_dir) except ValueError as e: print(str(e), file=sys.stderr) exit(-1)
def create_profile(args): set_canari_mode(CanariMode.Local) opts = parse_args(args) current_dir = os.getcwd() try: with PushDir(opts.working_dir or current_dir): transform_package = TransformDistribution(opts.package) transform_package.create_profile(opts.working_dir, current_dir) except ValueError, e: print(str(e)) exit(-1)
def _initialize(self): packages = None # Read packages that are to be loaded at runtime try: config = load_config() packages = config['canari.remote.packages'] except NoSectionError: print( 'Exiting... You did not specify a [canari.remote] section and a "packages" ' 'option in your canari.conf file!', file=sys.stderr) exit(-1) # Is packages not blank if not packages: print( 'Exiting... You did not specify any transform packages to load in your canari.conf file!', file=sys.stderr) exit(-1) elif isinstance(packages, string_types): packages = [packages] # Create the static directory for static file loading if not os.path.exists('static'): os.mkdir('static', 0o755) # Iterate through the list of packages to load for p in packages: # Copy all the image resource files in case they are used as entity icons distribution = TransformDistribution(p) print('Loading transform package %s' % repr(p), file=sys.stderr) for transform in distribution.remote_transforms: transform_name = transform().name print('Loading transform %s at /%s...' % (repr(transform_name), transform_name), file=sys.stderr) if os.name == 'posix' and transform.superuser and os.geteuid( ) and __name__.startswith('_mod_wsgi_'): print( 'WARNING: mod_wsgi does not allow applications to run with root privileges. ' 'Transform %s ignored...' % repr(transform_name), file=sys.stderr) continue self.transforms[transform_name] = transform self._copy_images(p) if not self.transforms: print( 'Exiting... Your transform packages have no remote transforms.', file=sys.stderr) exit(-1) self.transforms['canari.Version'] = Version
def load_object(classpath): package, cls = re.search(r'^(.*)\.([^.]+)$', classpath).groups() module = import_module(package) if cls in module.__dict__: return module.__dict__[cls] for t in TransformDistribution(package).transforms: if t.name == classpath: return t raise MaltegoException("Transform {!r} not found".format(classpath))
def generate_entities_doc(args): args = parse_args(args) transform_package = TransformDistribution(args.package) jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) def get_property_type(v): if isinstance(v, IntegerEntityField): return 'int' elif isinstance(v, FloatEntityField): return 'float' elif isinstance(v, BooleanEntityField): return 'bool' elif isinstance(v, TimeSpanEntityField): return 'timedelta' elif isinstance(v, DateTimeEntityField): return 'datetime' elif isinstance(v, DateEntityField): return 'date' elif isinstance(v, LongEntityField): return 'long' else: return 'str' jinja2_env.filters['get_property_type'] = get_property_type entity_module = 'canari.maltego.entities' if transform_package.name == 'canari' \ else '%s.transforms.common.entities' % transform_package.name variables = { 'transform.module': entity_module, 'transform.entities': transform_package.entities, 'transform.author': '%s <%s>' % (transform_package.author, transform_package.author_email) } configurator = Configurator( 'canari.resources.templates:generate_entities_doc', args.out_path, {'non_interactive': True}, variables=variables) configurator.ask_questions() print('Creating entities.rst documentation for %r...' % args.package) configurator.render() print('done!')
def unload_plume_package(opts): if not os.path.exists(os.path.join(opts.plume_dir, 'plume.py')): print('Plume does not appear to be installed in %s.' % opts.plume_dir) exit(-1) try: TransformDistribution(opts.package).configure(opts.plume_dir, load=False, remote=True) except ImportError: pass exit(0)
def convert(self, value, param, ctx): if not value: if not ctx or not ctx.obj: self.fail(self.failure_message) if not ctx.obj.project.is_valid: self.fail(self.failure_message) value = ctx.obj.project.name try: return TransformDistribution(value) except ImportError as e: self.fail(str(e))
def list_transforms(args): # TODO: project_tree may raise an exception if either project_root can't be # determined or if we can't find the package as an installed package. # Atleast the create-transform command calls this function without handling # the possible exception. What is the best sollution? # TODO: create-transform takes an argument --transform-dir which can be used # to control where to place the transform template. This breaks the new # assumption of the 'transforms' folder always being inside the 'pkg' # folder. However this is an assumption all over the place, so this # parameter doesn't really make much sense? # TODO: There are most likely many commands with similar problems # (above). and perhaps they should be updated to use the below template and # have their argument updated to -d/--dir instead with CWD as the default # value. # TODO: Perhaps we should introduce a 'create' command that will just make # an empty canari root dir (project). Inside this we can then call # create-package a number of times to generate all the desired # packages. This could even be automated for N-times during the call to # 'create'. 'create-package' can even still default to call 'create' if not # inside a canari root directory, to preserve backwards compatability. # TODO: Handle hyphening of package names. When creating them and when # trying to access them. This goes for project_tree, it should change '-' # with '_' in the package name. try: with pushd(args.dir): opts = parse_args(args) with pushd(args.ptree['src']): transform_package = TransformDistribution(opts.package) for t in transform_package.transforms: print('`- %s: %s' % (highlight( t.__name__, 'green', True), t.dotransform.description)) print(highlight(' `- Maltego identifiers:', 'black', True)) for uuid, (input_set, input_type) in zip(t.dotransform.uuids, t.dotransform.inputs): print ' `- %s applies to %s in set %s' % ( highlight(uuid, 'red', False), highlight(input_type._type_, 'red', False), highlight(input_set, 'red', False)) print '' except ValueError, e: print str(e) exit(-1)
def shell(opts): set_canari_mode(CanariMode.LocalShellDebug) if not opts.package.endswith('transforms'): opts.package = '%s.transforms' % opts.package try: transform_package = TransformDistribution(opts.package) with PushDir(opts.working_dir or transform_package.default_prefix): mtg_console = MtgConsole(transform_package.transforms, auto_sudo=opts.sudo) mtg_console.interact(highlight('Welcome to Canari %s.' % canari.__version__, 'green', True)) except ValueError, e: print str(e) exit(-1)
def unload_plume_package(opts): with PushDir(opts.plume_dir): if not os.path.exists('canari.conf'): print('Plume does not appear to be installed in %s.' % opts.plume_dir) print("Please run 'canari install-plume' and try again.") exit(-1) try: TransformDistribution(opts.package).configure(opts.plume_dir, load=False, remote=True) except ImportError: pass exit(0)
def shell(opts): fix_binpath(config['default/path']) fix_pypath() if not opts.package.endswith('transforms'): opts.package = '%s.transforms' % opts.package try: t = TransformDistribution(opts.package) with pushd(opts.working_dir or t.default_prefix): mtg_console = MtgConsole(opts.package) mtg_console.interact( highlight('Welcome to Canari %s.' % canari.__version__, 'green', True)) except ValueError, e: print str(e) exit(-1)
def list_transforms(opts): try: with PushDir(opts.working_dir or CanariProject().src_dir): transform_package = TransformDistribution(opts.package) for transform_class in transform_package.transforms: transform = transform_class() print('`- %s: %s' % (highlight(transform.name, 'green', True), transform.description)) print(highlight(' `- Maltego identifiers:', 'black', True)) print(' `- %s applies to %s in set %s' % ( highlight(transform.name, 'red', False), highlight(transform.input_type._type_, 'red', False), highlight(transform.transform_set, 'red', False) )) print('') except ValueError, e: print(e) exit(-1)
def run_server(args): fix_binpath(config['default/path']) opts = parse_args(args) if opts.port == -1: opts.port = 443 if not opts.disable_ssl else 80 if os.name == 'posix' and os.geteuid() and (opts.port <= 1024 or opts.enable_privileged): print ('You must run this server as root to continue...') os.execvp('sudo', ['sudo'] + sys.argv) transforms = {} print ('Loading transform packages...') try: for pkg_name in opts.packages: t = TransformDistribution(pkg_name) print ('Loading transform package %s' % pkg_name) for transform_module in t.remote_transforms: transform_name = transform_module.__name__ if os.name == 'posix' and hasattr(transform_module.dotransform, 'privileged') and \ (os.geteuid() or not opts.enable_privileged): continue if hasattr(transform_module.dotransform, 'remote') and transform_module.dotransform.remote: print ('Loading %s at /%s...' % (transform_name, transform_name)) inputs = [] if hasattr(transform_module.dotransform, 'inputs'): for category, entity_type in transform_module.dotransform.inputs: inputs.append(entity_type._type_) inputs.append(entity_type._v2type_) transforms['/%s' % transform_name] = (transform_module.dotransform, inputs) except Exception, e: print (str(e)) print ('Failed to load transforms... exiting') exit(-1)
def list_transforms(args): opts = parse_args(args) try: with pushd(opts.working_dir or os.getcwd()): transform_package = TransformDistribution(opts.package) for t in transform_package.transforms: print('`- %s: %s' % (highlight( t.__name__, 'green', True), t.dotransform.description)) print(highlight(' `- Maltego identifiers:', 'black', True)) for uuid, (input_set, input_type) in zip(t.dotransform.uuids, t.dotransform.inputs): print ' `- %s applies to %s in set %s' % ( highlight(uuid, 'red', False), highlight(input_type._type_, 'red', False), highlight(input_set, 'red', False)) print '' except ValueError, e: print str(e) exit(-1)
def _initialize(self): # Flask application container reload hack. reload(_config) packages = None # Read packages that are to be loaded at runtime try: packages = _config.config['remote/packages'] except NoSectionError: sys.stderr.write( 'Exiting... You did not specify a [remote] section and a "packages" ' 'option in your canari.conf file!\n') exit(-1) # Is packages not blank if not packages: sys.stderr.write( 'Exiting... You did not specify any transform packages to load in your canari.conf file!\n' ) exit(-1) elif isinstance(packages, basestring): packages = [packages] # Create the static directory for static file loading if not os.path.exists('static'): os.mkdir('static', 0755) # Iterate through the list of packages to load for p in packages: # Copy all the image resource files in case they are used as entity icons self._copy_images(p) distribution = TransformDistribution(p) sys.stderr.write('Loading transform package %s\n' % repr(p)) for transform in distribution.remote_transforms: transform_name = transform.__name__ sys.stderr.write('Loading transform %s at /%s...\n' % (repr(transform_name), transform_name)) # Should the transform be publicly available? # Does it conform to V2 of the Canari transform signature standard? if get_transform_version(transform.dotransform) == 2: sys.stderr.write( 'ERROR: Plume does not support V2 Canari transforms (%s). Please update to V3.' ' See http://www.canariproject.com/4-3-transform-development-quick-start/ for' ' more details.\n' % repr(transform_name)) exit(-1) # Does the transform need to be executed as root? If so, is this running in mod_wsgi? Yes = Bad! elif os.name == 'posix' and hasattr(transform.dotransform, 'privileged') and \ os.geteuid() and __name__.startswith('_mod_wsgi_'): sys.stderr.write( 'WARNING: mod_wsgi does not allow applications to run with root privileges. ' 'Transform %s ignored...\n' % repr(transform_name)) continue # So everything is good, let's register our transform with the global transform registry. inputs = {} if hasattr(transform.dotransform, 'inputs'): for category, entity_type in transform.dotransform.inputs: inputs[entity_type._type_] = entity_type inputs[entity_type._v2type_] = entity_type self.transforms[transform_name] = (transform.dotransform, inputs)