Example #1
0
def update_hids_agent_status(agent_id, sensor_id, agent_status):
    """ Update status of HIDS agent

    Raises:
        APICannotResolveSensorID
        APIInvalidHIDSAgentID
        APICannotUpdateHIDSAgent
    """

    if sensor_id is None:
        api_log.error(
            "[update_hids_agent_status]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    if agent_id is None:
        api_log.error(
            "[update_hids_agent_status]: Agent ID could not be empty")
        raise APIInvalidHIDSAgentID(agent_id)

    try:
        sensor_id_bin = get_bytes_from_uuid(sensor_id)
        status_integer = Hids_Agents.get_status_integer_from_string(
            agent_status)

        db.session.begin()
        db.session.query(Hids_Agents).filter(
            and_(Hids_Agents.agent_id == agent_id,
                 Hids_Agents.sensor_id == sensor_id_bin)).update(
                     {"agent_status": status_integer})
        db.session.commit()
    except Exception as msg:
        db.session.rollback()
        api_log.error("[update_hids_agent_status]: %s" % str(msg))
        raise APICannotUpdateHIDSAgent(agent_id, sensor_id)
Example #2
0
def update_hids_agent_status(agent_id, sensor_id, agent_status):
    """ Update status of HIDS agent

    Raises:
        APICannotResolveSensorID
        APIInvalidHIDSAgentID
        APICannotUpdateHIDSAgent
    """

    if sensor_id is None:
        api_log.error("[update_hids_agent_status]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    if agent_id is None:
        api_log.error("[update_hids_agent_status]: Agent ID could not be empty")
        raise APIInvalidHIDSAgentID(agent_id)

    try:
        sensor_id_bin = get_bytes_from_uuid(sensor_id)
        status_integer = Hids_Agents.get_status_integer_from_string(agent_status)

        db.session.query(Hids_Agents).filter(
            and_(Hids_Agents.agent_id == agent_id,
                 Hids_Agents.sensor_id == sensor_id_bin)).update({"agent_status": status_integer})
    except Exception as msg:
        api_log.error("[update_hids_agent_status]: %s" % str(msg))
        raise APICannotUpdateHIDSAgent(agent_id, sensor_id)
Example #3
0
def get_hids_agents_by_asset(asset_id, sensor_id=None):
    """ Get HIDS agents by asset
    Args:
        asset_id(str): Asset ID
        sensor_id(str): Sensor ID
    Returns:
        Dictionary with HIDS agents related to asset in the database

    Raises:
        APICannotGetHIDSAgentByAsset
        APICannotResolveAssetID
    """

    hids_agents = {}

    if asset_id is None:
        api_log.error(
            "[get_hids_agents_by_asset]: Asset ID could not be empty")
        raise APICannotResolveAssetID(asset_id)

    query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \
                "ha.agent_status, HEX(ha.host_id) AS host_id " \
                "FROM hids_agents ha WHERE ha.host_id = UNHEX('{0}')".format(get_hex_string_from_uuid(asset_id))

    if sensor_id is not None:
        query = query + " AND ha.sensor_id = UNHEX('{0}')".format(
            get_hex_string_from_uuid(sensor_id))

    try:
        ha_list = db.session.connection(mapper=Hids_Agents).execute(query)

        for hids_agent in ha_list:
            ha_id = hids_agent.agent_id
            ha_name = hids_agent.agent_name
            ha_ip = hids_agent.agent_ip
            ha_status = hids_agent.agent_status
            ha_sensor_id = hids_agent.sensor_id
            ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else ''

            ha_key = ha_sensor_id + '#' + ha_id

            hids_agents[ha_key] = {
                'id': ha_id,
                'name': ha_name,
                'ip_cidr': ha_ip,
                'status': {
                    'id': ha_status,
                    'descr':
                    Hids_Agents.get_status_string_from_integer(ha_status)
                },
                'sensor_id': ha_sensor_id,
                'host_id': ha_host_id
            }

    except Exception as msg:
        api_log.error("[get_hids_agents_by_asset]: %s" % str(msg))
        raise APICannotGetHIDSAgentByAsset(asset_id)

    return hids_agents
Example #4
0
def get_hids_agent_by_sensor(sensor_id, agent_id):
    """ Get HIDS agent by sensor
    Args:
        sensor_id(str): Sensor ID
        agent_id(str): Agent ID
    Returns:
        Dictionary with the HIDS agent of the sensor in the database

    Raises:
        APICannotResolveSensorID
        APIInvalidHIDSAgentID
        APICannotGetHIDSAgents
    """

    if sensor_id is None:
        api_log.error("[get_hids_agent_by_sensor]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    if agent_id is None:
        api_log.error("[get_hids_agent_by_sensor]: Agent ID could not be empty")
        raise APIInvalidHIDSAgentID(agent_id)

    hids_agent = {}

    try:
        sensor_id_hex = get_hex_string_from_uuid(sensor_id)

        query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \
                "ha.agent_status, HEX(ha.host_id) AS host_id " \
                "FROM hids_agents ha WHERE ha.sensor_id = UNHEX('{0}') AND ha.agent_id = '{1}' " \
                "LIMIT 1".format(sensor_id_hex, agent_id)

        ha_list = db.session.connection(mapper=Hids_Agents).execute(query).fetchall()

        if ha_list:
            ha_list = ha_list[0]

            ha_id = ha_list.agent_id
            ha_name = ha_list.agent_name
            ha_ip = ha_list.agent_ip
            ha_status = ha_list.agent_status
            ha_host_id = ha_list.host_id if ha_list.host_id is not None else ''

            hids_agent = {
                'id': ha_id,
                'name': ha_name,
                'ip_cidr': ha_ip,
                'status': {
                    'id': ha_status,
                    'descr': Hids_Agents.get_status_string_from_integer(ha_status)
                },
                'host_id': ha_host_id
            }
    except Exception as msg:
        api_log.error("[get_hids_agent_by_sensor]: %s" % str(msg))
        raise APICannotGetHIDSAgents(sensor_id)

    return hids_agent
Example #5
0
def add_hids_agent(agent_id,
                   sensor_id,
                   agent_name,
                   agent_ip,
                   agent_status,
                   host_id=None):
    """ Add a new HIDS agent

    Raises:
        APICannotResolveSensorID
        APIInvalidHIDSAgentID
        APICannotAddHIDSAgent
    """

    if sensor_id is None:
        api_log.error("[add_hids_agent]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    if agent_id is None:
        api_log.error("[add_hids_agent]: Agent ID could not be empty")
        raise APIInvalidHIDSAgentID(agent_id)

    try:
        db.session.begin()

        sensor_id_bin = get_bytes_from_uuid(sensor_id)

        if host_id:
            hex_id_bin = get_bytes_from_uuid(host_id)
        else:
            hex_id_bin = None

        status_integer = Hids_Agents.get_status_integer_from_string(
            agent_status)

        hids_agent = Hids_Agents()
        hids_agent.agent_id = agent_id
        hids_agent.sensor_id = sensor_id_bin
        hids_agent.agent_name = agent_name
        hids_agent.agent_ip = agent_ip
        hids_agent.agent_status = status_integer
        hids_agent.host_id = hex_id_bin

        db.session.merge(hids_agent)
        db.session.commit()
    except Exception as msg:
        db.session.rollback()
        api_log.error("[add_hids_agent]: %s" % str(msg))
        raise APICannotAddHIDSAgent(agent_id, sensor_id)
Example #6
0
def get_hids_agents_by_asset(asset_id, sensor_id=None):
    """ Get HIDS agents by asset
    Args:
        asset_id(str): Asset ID
        sensor_id(str): Sensor ID
    Returns:
        Dictionary with HIDS agents related to asset in the database

    Raises:
        APICannotGetHIDSAgentByAsset
        APICannotResolveAssetID
    """

    hids_agents = {}

    if asset_id is None:
        api_log.error("[get_hids_agents_by_asset]: Asset ID could not be empty")
        raise APICannotResolveAssetID(asset_id)

    query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \
                "ha.agent_status, HEX(ha.host_id) AS host_id " \
                "FROM hids_agents ha WHERE ha.host_id = UNHEX('{0}')".format(get_hex_string_from_uuid(asset_id))

    if sensor_id is not None:
        query = query + " AND ha.sensor_id = UNHEX('{0}')".format(get_hex_string_from_uuid(sensor_id))

    try:
        ha_list = db.session.connection(mapper=Hids_Agents).execute(query)

        for hids_agent in ha_list:
            ha_id = hids_agent.agent_id
            ha_name = hids_agent.agent_name
            ha_ip = hids_agent.agent_ip
            ha_status = hids_agent.agent_status
            ha_sensor_id = hids_agent.sensor_id
            ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else ''

            ha_key = ha_sensor_id + '#' + ha_id

            hids_agents[ha_key] = {
                'id': ha_id,
                'name': ha_name,
                'ip_cidr': ha_ip,
                'status': {
                    'id': ha_status,
                    'descr': Hids_Agents.get_status_string_from_integer(ha_status)
                },
                'sensor_id': ha_sensor_id,
                'host_id': ha_host_id
            }

    except Exception as msg:
        api_log.error("[get_hids_agents_by_asset]: %s" % str(msg))
        raise APICannotGetHIDSAgentByAsset(asset_id)

    return hids_agents
Example #7
0
def get_hids_agents_by_sensor(sensor_id):
    """ Get HIDS agents by sensor
    Args:
        sensor_id(str): Sensor ID
    Returns:
        Dictionary with HIDS agents of the sensor in the database

    Raises:
        APICannotResolveSensorID
        APICannotGetHIDSAgents
    """

    hids_agents = {}

    if sensor_id is None:
        api_log.error(
            "[get_hids_agents_by_sensor]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    try:
        sensor_id_hex = get_hex_string_from_uuid(sensor_id)
        query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \
                "ha.agent_status, HEX(ha.host_id) AS host_id " \
                "FROM hids_agents ha WHERE ha.sensor_id = UNHEX('{0}')".format(sensor_id_hex)
        ha_list = db.session.connection(mapper=Hids_Agents).execute(query)

        for hids_agent in ha_list:
            ha_id = hids_agent.agent_id
            ha_name = hids_agent.agent_name
            ha_ip = hids_agent.agent_ip
            ha_status = hids_agent.agent_status
            ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else ''

            hids_agents[ha_id] = {
                'id': ha_id,
                'name': ha_name,
                'ip_cidr': ha_ip,
                'status': {
                    'id': ha_status,
                    'descr':
                    Hids_Agents.get_status_string_from_integer(ha_status)
                },
                'host_id': ha_host_id
            }

    except Exception as msg:
        api_log.error("[get_hids_agents_by_sensor]: %s" % str(msg))
        raise APICannotGetHIDSAgents(sensor_id)

    return hids_agents
Example #8
0
def add_hids_agent(agent_id, sensor_id, agent_name, agent_ip, agent_status, host_id=None):
    """ Add a new HIDS agent

    Raises:
        APICannotResolveSensorID
        APIInvalidHIDSAgentID
        APICannotAddHIDSAgent
    """

    if sensor_id is None:
        api_log.error("[add_hids_agent]: Sensor ID could not be empty")
        raise APICannotResolveSensorID(sensor_id)

    if agent_id is None:
        api_log.error("[add_hids_agent]: Agent ID could not be empty")
        raise APIInvalidHIDSAgentID(agent_id)

    try:
        db.session.begin()

        sensor_id_bin = get_bytes_from_uuid(sensor_id)

        if host_id:
            hex_id_bin = get_bytes_from_uuid(host_id)
        else:
            hex_id_bin = None

        status_integer = Hids_Agents.get_status_integer_from_string(agent_status)

        hids_agent = Hids_Agents()
        hids_agent.agent_id = agent_id
        hids_agent.sensor_id = sensor_id_bin
        hids_agent.agent_name = agent_name
        hids_agent.agent_ip = agent_ip
        hids_agent.agent_status = status_integer
        hids_agent.host_id = hex_id_bin

        db.session.merge(hids_agent)
        db.session.commit()
    except Exception as msg:
        db.session.rollback()
        api_log.error("[add_hids_agent]: %s" % str(msg))
        raise APICannotAddHIDSAgent(agent_id, sensor_id)