Ejemplo n.º 1
0
def main(args=None):
    args = parser.parse_args(args=args)
    if args.verbose == 1:
        log.setLevel('WARNING')
    elif args.verbose == 2:
        log.setLevel('INFO')
    elif args.verbose >= 3:
        log.setLevel('DEBUG')
    else:
        log.setLevel('ERROR')
    if args.simulate:
        if (not args.config or not os.path.isfile(args.config)) and not args.otx:
            log.error("You must either give an existing config file or your OTX API key with '--dry-run'.")
            sys.exit(4)
    elif (not args.config or not os.path.isfile(args.config)) and not (args.otx and args.server and args.misp):
        log.error("You must either give an existing config file or your API keys and the MISP server URL.")
        sys.exit(2)
    try:
        config = Configuration(args)
    except Exception as ex:
        log.error(ex.message)
        sys.exit(5)

    try:
        pulses = get_pulses(config.otx, from_timestamp=config.timestamp.isoformat())
    except InvalidAPIKey:
        log.error("Wrong API key: '{}'".format(config.otx))
        sys.exit(11)
    except ValueError as ex:
        log.error("Cannot use last import timestamp '{}'".format(config.timestamp.isoformat()))
        sys.exit(12)
    except BadRequest:
        log.error("Bad request")
        sys.exit(13)
    kwargs = {}
    if not config.simulate:
        kwargs = {
            'server': config.server,
            'key': config.misp,
            'distribution': config.distribution,
            'threat_level': config.threat_level,
            'analysis': config.analysis,
            'tlp': config.tlp,
            'discover_tags': config.discover_tags,
            'to_ids': config.to_ids,
            'author_tag': config.author_tag,
            'bulk_tag': config.bulk_tag,
            'dedup_titles': config.dedup_titles
        }
        try:
            import pymisp
        except ImportError:
            log.error('PyMISP is not installed. Aborting.')
            sys.exit(20)
    try:
        create_events(pulses, author=config.author, **kwargs)
    except Exception as ex:
        log.error(ex.message)
        sys.exit(21)
    if config.write_config or config.update_timestamp:
        if args.config:
            with open(args.config, 'w') as f:
                config.write(f)
        else:
            config.write(sys.stdout)
Ejemplo n.º 2
0
def main(args=None):
    args = parser.parse_args(args=args)
    if args.verbose == 1:
        log.setLevel('WARNING')
    elif args.verbose == 2:
        log.setLevel('INFO')
    elif args.verbose >= 3:
        log.setLevel('DEBUG')
    else:
        log.setLevel('ERROR')
    if args.simulate:
        if (not args.config or not os.path.isfile(args.config)) and not args.otx:
            log.error("You must either give an existing config file or your OTX API key with '--dry-run'.")
            sys.exit(4)
    elif (not args.config or not os.path.isfile(args.config)) and not (args.otx and args.server and args.misp):
        log.error("You must either give an existing config file or your API keys and the MISP server URL.")
        sys.exit(2)
    try:
        config = Configuration(args)
    except Exception as ex:
        log.error(ex.message)
        sys.exit(5)

    try:
        pulses = get_pulses(config.otx, from_timestamp=config.timestamp.isoformat())
    except InvalidAPIKey:
        log.error("Wrong API key: '{}'".format(config.otx))
        sys.exit(11)
    except ValueError as ex:
        log.error("Cannot use last import timestamp '{}'".format(config.timestamp.isoformat()))
        sys.exit(12)
    except BadRequest:
        log.error("Bad request")
        sys.exit(13)
    kwargs = {}
    if not config.simulate:
        kwargs = {
            'server': config.server,
            'key': config.misp,
            'distribution': config.distribution,
            'threat_level': config.threat_level,
            'analysis': config.analysis,
            'tlp': config.tlp,
            'discover_tags': config.discover_tags,
            'to_ids': config.to_ids,
            'author_tag': config.author_tag,
            'bulk_tag': config.bulk_tag,
            'dedup_titles': config.dedup_titles,
            'stop_on_error': config.stop_on_error
        }
        try:
            import pymisp
        except ImportError:
            log.error('PyMISP is not installed. Aborting.')
            sys.exit(20)
    try:
        create_events(pulses, author=config.author, **kwargs)
    except Exception as ex:
        log.error("Error: {}".format(ex))
        sys.exit(21)
    if config.write_config or config.update_timestamp:
        if args.config:
            with open(args.config, 'w') as f:
                config.write(f)
        else:
            config.write(sys.stdout)