Example #1
0
    def download_pulses(self):
        """Retrieves all the pulses information, both new and deleted
        Args:
            None
        Returns:
            result(dict): number of new pulses downloaded, updated and deleted pulses.
        """
        try:
            p_update, p_delete = self.get_pulse_updates()
            p_new = self.get_new_pulses()
            self.pulse_correlation_db.sync()
        except Exception:
            raise

        db_set_config("open_threat_exchange_latest_update", datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))

        return {'new_pulses': p_new, 'updated_pulses': p_update, 'deleted_pulses': p_delete}
Example #2
0
    def download_pulses(self):
        """Retrieves all the pulses information, both new and deleted
        Args:
            None
        Returns:
            result(dict): number of new pulses downloaded, updated and deleted pulses.
        """
        try:
            p_update, p_delete = self.get_pulse_updates()
            p_new = self.get_new_pulses()
            self.pulse_correlation_db.sync()
        except Exception:
            raise

        db_set_config("open_threat_exchange_latest_update",
                      datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))

        return {
            'new_pulses': p_new,
            'updated_pulses': p_update,
            'deleted_pulses': p_delete
        }
Example #3
0
def apimethod_stop_contributing_otx():
    """Disable the config flag to start contributing to OTX

    Returns:
        success (bool): True if successful, False elsewhere
        result(string): Error message if there was an error or empty string otherwise.
    """
    success, info = db_set_config("open_threat_exchange", "no")

    if not success:
        api_log.error("[apimethod_stop_contributing_otx] %s" % str(info))
        return False, str(info)

    return True, ""
Example #4
0
def apimethod_stop_contributing_otx():
    """Disable the config flag to start contributing to OTX

    Returns:
        success (bool): True if successful, False elsewhere
        result(string): Error message if there was an error or empty string otherwise.
    """
    success, info = db_set_config("open_threat_exchange", "no")

    if not success:
        api_log.error("[apimethod_stop_contributing_otx] %s" % str(info))
        return False, str(info)

    return True, ""
Example #5
0
def apimethod_get_open_threat_exchange_config():
    """Retrieves the OTX configuration from the database

    Returns:
        success (bool): True if successful, False elsewhere
        result(dict)  : A python dic containing all the OTX configuration.
    """
    result = {}

    keys = {
        "token": "open_threat_exchange_key",
        "username": "******",
        "user_id": "open_threat_exchange_user_id",
        "latest_update": "open_threat_exchange_latest_update",
        "latest_contribution": "open_threat_exchange_last",
        "contributing": "open_threat_exchange",
        "key_version": "open_threat_exchange_key_version"
    }

    for result_key, db_key in keys.iteritems():
        success, value = db_get_config(db_key)
        if not success:
            api_log.error("[apimethod_get_open_threat_exchange_config] %s" %
                          str(value))
            return False, str(value)
        else:
            if result_key == "contributing":
                result[result_key] = True if value == "yes" else "no"
            else:
                result[result_key] = value

    #Check problem with OTX keys that are not updated.
    if result["token"] and result["key_version"] < "2":
        try:
            otxapi = OTXv2(key=result["token"])
            user_data = otxapi.check_token()
            username = user_data.get('username')
            user_id = user_data.get('user_id')

            if username != 'user_needs_profile':
                db_set_config("open_threat_exchange_key_version", 2)
                db_set_config("open_threat_exchange_username", username)
                db_set_config("open_threat_exchange_user_id", user_id)
                result["username"] = username
                result["user_id"] = user_id
                result["key_version"] = "2"

        except Exception as err:
            api_log.error("Cannot check if the OTX Key is valid: %s" %
                          str(err))

    return True, result
Example #6
0
def apimethod_get_open_threat_exchange_config():
    """Retrieves the OTX configuration from the database

    Returns:
        success (bool): True if successful, False elsewhere
        result(dict)  : A python dic containing all the OTX configuration.
    """
    result = {}

    keys = {"token": "open_threat_exchange_key",
            "username": "******",
            "user_id": "open_threat_exchange_user_id",
            "latest_update": "open_threat_exchange_latest_update",
            "latest_contribution": "open_threat_exchange_last",
            "contributing": "open_threat_exchange",
            "key_version": "open_threat_exchange_key_version"}

    for result_key, db_key in keys.iteritems():
        success, value = db_get_config(db_key)
        if not success:
            api_log.error("[apimethod_get_open_threat_exchange_config] %s" % str(value))
            return False, str(value)
        else:
            if result_key == "contributing":
                result[result_key] = True if value == "yes" else "no"
            else:
                result[result_key] = value

    #Check problem with OTX keys that are not updated.
    if result["token"] and result["key_version"] < "2":
        try:
            otxapi = OTXv2(key=result["token"])
            user_data = otxapi.check_token()
            username = user_data.get('username')
            user_id = user_data.get('user_id')

            if username != 'user_needs_profile':
                db_set_config("open_threat_exchange_key_version", 2)
                db_set_config("open_threat_exchange_username", username)
                db_set_config("open_threat_exchange_user_id", user_id)
                result["username"] = username
                result["user_id"] = user_id
                result["key_version"] = "2"

        except Exception as err:
            api_log.error("Cannot check if the OTX Key is valid: %s" % str(err))

    return True, result
Example #7
0
def apimethod_remove_otx_account():
    """Remove the OTX configuration from the database

    Returns:
        success (bool): True if successful, False elsewhere
        result(string): Error message if there was an error or empty string otherwise.
    """
    #Removing the OTX config vars
    keys = ["open_threat_exchange",
            "open_threat_exchange_key",
            "open_threat_exchange_username",
            "open_threat_exchange_user_id",
            "open_threat_exchange_last",
            "open_threat_exchange_latest_update",
            "open_threat_exchange_key_version"]

    for k in keys:
        success, info = db_set_config(k, "")
        if not success:
            api_log.error("[apimethod_remove_otx_account] %s" % str(info))
            return False, str(info)

    #Removing the pulse database
    try:
        pulse_db = PulseDB()
        pulse_correlation_db = PulseCorrelationDB()

        pulse_db.flush_db()
        pulse_correlation_db.purge_all()
        pulse_correlation_db.sync()

        del pulse_db
        del pulse_correlation_db
    except Exception as err:
        api_log.error("[apimethod_remove_otx_account] %s" % str(err))
        return False, "Error removing OTX Account: Pulse List Cannot Be removed at this time."

    return True, ""
Example #8
0
def apimethod_remove_otx_account():
    """Remove the OTX configuration from the database

    Returns:
        success (bool): True if successful, False elsewhere
        result(string): Error message if there was an error or empty string otherwise.
    """
    #Removing the OTX config vars
    keys = [
        "open_threat_exchange", "open_threat_exchange_key",
        "open_threat_exchange_username", "open_threat_exchange_user_id",
        "open_threat_exchange_last", "open_threat_exchange_latest_update",
        "open_threat_exchange_key_version"
    ]

    for k in keys:
        success, info = db_set_config(k, "")
        if not success:
            api_log.error("[apimethod_remove_otx_account] %s" % str(info))
            return False, str(info)

    #Removing the pulse database
    try:
        pulse_db = PulseDB()
        pulse_correlation_db = PulseCorrelationDB()

        pulse_db.flush_db()
        pulse_correlation_db.purge_all()
        pulse_correlation_db.sync()

        del pulse_db
        del pulse_correlation_db
    except Exception as err:
        api_log.error("[apimethod_remove_otx_account] %s" % str(err))
        return False, "Error removing OTX Account: Pulse List Cannot Be removed at this time."

    return True, ""
Example #9
0
    except BadRequest, err:
        api_log.error("[Apimethod apimethod_register_otx_token] ERROR_BAD_REQUEST: %s" % str(err))
        return False, "ERROR_BAD_REQUEST"
    except Exception, err:
        api_log.error("[Apimethod apimethod_register_otx_token] ERROR_CONNECTION: %s" % str(err))
        return False, "ERROR_CONNECTION"

    username = user_data.get('username')
    user_id = user_data.get('user_id')
    #if username is user_needs_profile, the otx key is not updated.
    key_version = 1 if username == 'user_needs_profile' else 2

    #First we remove everything related to OTX
    apimethod_remove_otx_account()

    db_set_config("open_threat_exchange", "yes")
    db_set_config("open_threat_exchange_key", token)
    db_set_config("open_threat_exchange_username", username)
    db_set_config("open_threat_exchange_user_id", user_id)
    db_set_config("open_threat_exchange_last", "1969-01-01 00:00:00")
    db_set_config("open_threat_exchange_key_version", key_version)

    #Downloading the pulses
    monitor_download_pulses.delay()

    #Formatting result response
    result = {"token": token,
              "username": username,
              "user_id": user_id,
              "contributing": True,
              "latest_update": "",
Example #10
0
        return False, "ERROR_BAD_REQUEST"
    except Exception, err:
        api_log.error(
            "[Apimethod apimethod_register_otx_token] ERROR_CONNECTION: %s" %
            str(err))
        return False, "ERROR_CONNECTION"

    username = user_data.get('username')
    user_id = user_data.get('user_id')
    #if username is user_needs_profile, the otx key is not updated.
    key_version = 1 if username == 'user_needs_profile' else 2

    #First we remove everything related to OTX
    apimethod_remove_otx_account()

    db_set_config("open_threat_exchange", "yes")
    db_set_config("open_threat_exchange_key", token)
    db_set_config("open_threat_exchange_username", username)
    db_set_config("open_threat_exchange_user_id", user_id)
    db_set_config("open_threat_exchange_last", "1969-01-01 00:00:00")
    db_set_config("open_threat_exchange_key_version", key_version)

    #Downloading the pulses
    monitor_download_pulses.delay()

    #Formatting result response
    result = {
        "token": token,
        "username": username,
        "user_id": user_id,
        "contributing": True,