def create_profile(self, install_prefix, current_dir, configure=True): mtz = os.path.join(current_dir, '%s.mtz' % self.name) print('Creating profile %s...' % mtz) mtz = MtzDistribution(mtz, 'w') self.install(install_prefix, mtz, configure) mtz.close() print( """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SUCCESS! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Successfully created %s. You may now import this file into Maltego. INSTRUCTIONS: ------------- 1. Open Maltego. 2. Click on the home button (Maltego icon, top-left corner). 3. Click on 'Import'. 4. Click on 'Import Configuration'. 5. Follow prompts. 6. Enjoy! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SUCCESS! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ % mtz.filename)
def _install_entities(self, distribution): try: src = self.entities_file if not os.path.lexists(src): return mtz = MtzDistribution(src) for entity_file in mtz.entity_files: distribution.add_entity(mtz.read_file(entity_file)) for icon_category in mtz.icon_categories: icon_category_dir = distribution.get_icon_category_dir(icon_category) for icon in mtz.get_icons_by_category(icon_category): data = mtz.read_file(icon) p = distribution.path_join(icon_category_dir, os.path.basename(icon)) print 'Installing custom icon %s to %s...' % (icon, p) distribution.write_file(p, data) except ImportError: pass
def create_profile(self, install_prefix, current_dir, configure=True): mtz = os.path.join(current_dir, '%s.mtz' % self.name) print('Creating profile %s...' % mtz) mtz = MtzDistribution(mtz, 'w') self.install(install_prefix, mtz, configure) mtz.close() print(""" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SUCCESS! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Successfully created %s. You may now import this file into Maltego. INSTRUCTIONS: ------------- 1. Open Maltego. 2. Click on the home button (Maltego icon, top-left corner). 3. Click on 'Import'. 4. Click on 'Import Configuration'. 5. Follow prompts. 6. Enjoy! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SUCCESS! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% """ % mtz.filename)
def generate_entities(args): opts = parse_args(args) if os.path.exists(opts.outfile) and not opts.append and not \ parse_bool('%s already exists. Are you sure you want to overwrite it?' % repr(opts.outfile), default='n'): exit(-1) distribution = None if not opts.mtz_file: distribution = MaltegoDistribution() if distribution.version >= '3.4.0': print(""" =========================== ERROR: NOT SUPPORTED =========================== Starting from Maltego v3.4.0 the 'canari generate-entities' command can no longer generate entity definition files from the Maltego configuration directory. Entities can only be generated from export files (*.mtz). To export entities navigate to the 'Manage' tab in Maltego, then click on the 'Export Entities' button and follow the prompts. Once the entities have been exported, run the following command: shell> canari generate-entities -m myentities.mtz =========================== ERROR: NOT SUPPORTED =========================== """) exit(-1) else: distribution = MtzDistribution(opts.mtz_file) namespaces = dict() excluded_entities = [] if opts.append: existing_entities = get_existing_entities(opts.outfile) # excluded_entities.extend([e._type_ for e in existing_entities]) for entity_class in existing_entities: excluded_entities.extend(entity_class._type_) if entity_class._type_.endswith('Entity'): namespaces[entity_class._namespace_] = entity_class.__name__ print 'Generating %s...' % repr(opts.outfile) outfile = open(opts.outfile, 'ab' if opts.append else 'wb') if opts.append: outfile.write('\n\n') else: outfile.write( '#!/usr/bin/env python\n\nfrom canari.maltego.entities import EntityField, Entity\n\n\n' ) for entity_file in distribution.entity_files: entity = MaltegoEntity.parse(distribution.read_file(entity_file)) if (opts.entity and entity.id not in opts.entity) or entity.id in excluded_entities: continue namespace_entity = entity.id.split('.') base_classname = None namespace = '.'.join(namespace_entity[:-1]) name = namespace_entity[-1] classname = name if (opts.namespace and namespace not in opts.namespace) or namespace in opts.exclude_namespace: continue print 'Generating entity definition for %s...' % entity_file if namespace not in namespaces: base_classname = '%sEntity' % (''.join( [n.title() for n in namespace_entity[:-1]])) namespaces[namespace] = base_classname outfile.write('class %s(Entity):\n _namespace_ = %s\n\n' % (base_classname, repr(namespace))) else: base_classname = namespaces[namespace] for field in entity.properties.fields.itervalues(): fields = [ 'name=%s' % repr(field.name), 'propname=%s' % repr(normalize_fn(field.name)), 'displayname=%s' % repr(field.displayname) ] outfile.write('@EntityField(%s)\n' % ', '.join(fields)) outfile.write('class %s(%s):\n pass\n\n\n' % (classname, base_classname)) outfile.close() print 'done.'
def generate_entities(project, output_path, mtz_file, exclude_namespace, namespace, maltego_entities, append, entity): if not output_path: if project.is_valid: output_path = project.common_dir else: output_path = os.getcwd() entities_py = os.path.join(output_path, 'entities.py') if os.path.exists(entities_py) and not append: click.confirm( '{!r} already exists. Are you sure you want to overwrite it?'. format(entities_py), default=False, abort=True) if maltego_entities: namespace.extend(exclude_namespace) exclude_namespace = [] mtz = MtzDistribution(mtz_file) target = output_path variables = project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in exclude_namespace: continue elif not namespace or namespace in namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if append: module = project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v 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)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update( { 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }, ) configurator = Configurator('canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() click.echo('Generating entities for %r...' % variables['project.name'], err=True) configurator.render() click.echo('done!', err=True)
def generate_entities(args): opts = parse_args(args) mtz = MtzDistribution(opts.mtz_file) target = opts.out_path variables = opts.project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^\.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in opts.exclude_namespace: continue elif not opts.namespace or namespace in opts.namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if opts.append: module = opts.project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v 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)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update({ 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }) configurator = Configurator( 'canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables ) configurator.ask_questions() print('Generating entities for %r...' % variables['project.name']) configurator.render() print('done!')
def generate_entities(args): opts = parse_args(args) mtz = MtzDistribution(opts.mtz_file) target = opts.out_path variables = opts.project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in opts.exclude_namespace: continue elif not opts.namespace or namespace in opts.namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if opts.append: module = opts.project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v 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)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update( { 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }, ) configurator = Configurator('canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() print('Generating entities for %r...' % variables['project.name'], file=sys.stderr) configurator.render() print('done!', file=sys.stderr)