Ejemplo n.º 1
0
def ask_user(defaults):

    print('Welcome to the Canari transform package wizard.')

    if not parse_bool('Would you like to specify authorship information? [Y/n]: '):
        return

    defaults['description'] = raw_input('Project description [%s]: ' % defaults['description']) or defaults['description']
    defaults['example'] = parse_bool('Generate an example transform? [Y/n]: ')
    defaults['author'] = raw_input('Author name [%s]: ' % defaults['author']) or defaults['author']
    defaults['email'] = raw_input('Author email []: ') or ''
    defaults['maintainer'] = raw_input('Maintainer name [%s]: ' % defaults['author']) or defaults['author']

    if not parse_bool('Are you satisfied with this information? [Y/n]: '):
        return ask_user(defaults)
Ejemplo n.º 2
0
def ask_user(defaults):
    print('Welcome to the Canari transform package wizard.')

    while True:
        if not parse_bool('Would you like to specify authorship information?', defaults['create_authorship']):
            return

        defaults['description'] = parse_str('Project description', defaults['description'])
        defaults['create_example'] = parse_bool('Generate an example transform?', defaults['create_example'])
        defaults['author'] = parse_str('Author name', defaults['author'])
        defaults['email'] = parse_str('Author email', defaults['email'])
        defaults['maintainer'] = parse_str('Maintainer name', defaults['author'])

        if parse_bool('Are you satisfied with this information?'):
            return
Ejemplo n.º 3
0
def ask_user(defaults):
    print('Welcome to the Canari transform package wizard.')

    if not parse_bool('Would you like to specify authorship information? [Y/n]: '):
        return

    defaults['description'] = raw_input('Project description [%s]: ' % defaults['description']) or defaults[
        'description']
    defaults['example'] = parse_bool('Generate an example transform? [Y/n]: ')
    defaults['author'] = raw_input('Author name [%s]: ' % defaults['author']) or defaults['author']
    defaults['email'] = raw_input('Author email []: ') or ''
    defaults['maintainer'] = raw_input('Maintainer name [%s]: ' % defaults['author']) or defaults['author']

    if not parse_bool('Are you satisfied with this information? [Y/n]: '):
        return ask_user(defaults)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def run(args):

    opts = parse_args(args)

    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')


    if opts.package.endswith('.transforms'):
        opts.package = opts.package.replace('.transforms', '')


    print ('Looking for transforms in %s.transforms' % opts.package)
    transform_package = None
    try:
        transform_package = import_package('%s.transforms' % opts.package)
    except ImportError, e:
        print ("Does not appear to be a valid canari package. "
               "Couldn't import the '%s.transforms' package in '%s'. Error message: %s" %
               (opts.package, opts.package, e))
        exit(-1)
Ejemplo n.º 6
0
def ask_user(defaults):
    print("Welcome to the Canari transform package wizard.")

    if not parse_bool("Would you like to specify authorship information? [Y/n]: "):
        return

    defaults["description"] = (
        raw_input("Project description [%s]: " % defaults["description"]) or defaults["description"]
    )
    defaults["example"] = parse_bool("Generate an example transform? [Y/n]: ")
    defaults["author"] = raw_input("Author name [%s]: " % defaults["author"]) or defaults["author"]
    defaults["email"] = raw_input("Author email []: ") or ""
    defaults["maintainer"] = raw_input("Maintainer name [%s]: " % defaults["author"]) or defaults["author"]

    if not parse_bool("Are you satisfied with this information? [Y/n]: "):
        return ask_user(defaults)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
def run(args):

    opts = parse_args(args)

    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')

    if opts.package.endswith('.transforms'):
        opts.package = opts.package.replace('.transforms', '')

    print('Looking for transforms in %s.transforms' % opts.package)
    transform_package = None
    try:
        transform_package = import_package('%s.transforms' % opts.package)
    except ImportError, e:
        print(
            "Does not appear to be a valid canari package. "
            "Couldn't import the '%s.transforms' package in '%s'. Error message: %s"
            % (opts.package, opts.package, e))
        exit(-1)
Ejemplo n.º 9
0
def ask_user(defaults):
    print('Welcome to the Canari transform package wizard.')

    while True:
        if not parse_bool('Would you like to specify authorship information?',
                          defaults['create_authorship']):
            return

        defaults['description'] = parse_str('Project description',
                                            defaults['description'])
        defaults['create_example'] = parse_bool(
            'Generate an example transform?', defaults['create_example'])
        defaults['author'] = parse_str('Author name', defaults['author'])
        defaults['email'] = parse_str('Author email', defaults['email'])
        defaults['maintainer'] = parse_str('Maintainer name',
                                           defaults['author'])

        if parse_bool('Are you satisfied with this information?'):
            return
Ejemplo n.º 10
0
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.'
Ejemplo n.º 11
0
def run(args):
    opts = parse_args(args)

    if path.exists(opts.outfile) and not opts.append and not \
        parse_bool('%s already exists. Are you sure you want to overwrite it? [y/N]: ' % repr(opts.outfile),
                   default='n'):
        exit(-1)

    entity_source = None
    if opts.mtz_file is None:
        d = detect_settings_dir()
        if maltego_version(d) >= '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)
        entity_source = DirFile(
            path.join(d, 'config', 'Maltego', 'Entities')
        )
    else:
        entity_source = ZipFile(opts.mtz_file)

    entity_files = filter(lambda x: x.endswith('.entity'), entity_source.namelist())

    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 entity_files:
        xml = XML(entity_source.open(entity_file).read())
        id_ = xml.get('id')

        if (opts.entity and id_ not in opts.entity) or id_ in excluded_entities:
            continue

        namespace_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

        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 xml.findall('Properties/Fields/Field'):
            fields = [
                'name=%s' % repr(field.get('name')),
                'propname=%s' % repr(normalize_fn(field.get('name'))),
                'displayname=%s' % repr(field.get('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.'
Ejemplo n.º 12
0
 def check(self, value):
     try:
         parse_bool(value)
         return True
     except:
         return False
Ejemplo n.º 13
0
 def parse(self, value):
     try:
         return parse_bool(value)
     except:
         log.error("Incorrect Boolean %s", value)
Ejemplo n.º 14
0
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.'
Ejemplo n.º 15
0
def run(args):

    opts = parse_args(args)

    if path.exists(opts.outfile) and not opts.append and not \
       parse_bool('%s already exists. Are you sure you want to overwrite it? [y/N]: ' % repr(opts.outfile), default='n'):
        exit(-1)


    ar = DirFile(
        path.join(detect_settings_dir(), 'config', 'Maltego', 'Entities')
    ) if opts.mtz_file is None else ZipFile(opts.mtz_file)

    entities = filter(lambda x: x.endswith('.entity'), ar.namelist())

    nses = dict()

    el = []
    if opts.append:
        l = diff(opts.outfile)
        el.extend([i.type for i in l])
        for i in l:
            if i.type.endswith('Entity'):
                nses[i.namespace] = i.__class__.__name__

    print 'Generating %s...' % repr(opts.outfile)
    fd = open(opts.outfile, 'ab' if opts.append else 'wb')

    if opts.append:
        fd.write('\n\n')
    else:
        fd.write('#!/usr/bin/env python\n\nfrom canari.maltego.entities import EntityField, Entity\n\n\n')

    for e in entities:
        xml = XML(ar.open(e).read())
        id_ = xml.get('id')

        if (opts.entity and id_ not in opts.entity) or id_ in el:
            continue

        ens = id_.split('.')

        base_classname = None
        namespace = '.'.join(ens[:-1])
        name = ens[-1]
        classname = name

        if (opts.namespace and namespace not in opts.namespace) or namespace in opts.exclude_namespace:
            continue

        if namespace not in nses:
            base_classname = '%sEntity' % (''.join([ n.title() for n in ens[:-1] ]))
            nses[namespace] = base_classname

            fd.write('class %s(Entity):\n    namespace = %s\n\n' % (base_classname, repr(namespace)))
        else:
            base_classname = nses[namespace]


        for f in xml.findall('Properties/Fields/Field'):
            fields = [
                'name=%s' % repr(f.get('name')),
                'propname=%s' % repr(normalize_fn(f.get('name'))),
                'displayname=%s' % repr(f.get('displayName'))

            ]
            fd.write('@EntityField(%s)\n' % ', '.join(fields))

        fd.write('class %s(%s):\n    pass\n\n\n' % (classname, base_classname))

    fd.close()
    print 'done.'
Ejemplo n.º 16
0
def run(args):
    opts = parse_args(args)

    if path.exists(opts.outfile) and not opts.append and not \
        parse_bool('%s already exists. Are you sure you want to overwrite it? [y/N]: ' % repr(opts.outfile),
                   default='n'):
        exit(-1)

    entity_source = None
    if opts.mtz_file is None:
        d = detect_settings_dir()
        if maltego_version(d) >= '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)
        entity_source = DirFile(path.join(d, 'config', 'Maltego', 'Entities'))
    else:
        entity_source = ZipFile(opts.mtz_file)

    entity_files = filter(lambda x: x.endswith('.entity'),
                          entity_source.namelist())

    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 entity_files:
        xml = XML(entity_source.open(entity_file).read())
        id_ = xml.get('id')

        if (opts.entity
                and id_ not in opts.entity) or id_ in excluded_entities:
            continue

        namespace_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

        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 xml.findall('Properties/Fields/Field'):
            fields = [
                'name=%s' % repr(field.get('name')),
                'propname=%s' % repr(normalize_fn(field.get('name'))),
                'displayname=%s' % repr(field.get('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.'