RSA_GetRawLog

Use this script to get RAW log.

python · NetWitness

Details

IDRSA_GetRawLog
Languagepython
From Version6.9.0
Docker Imagedemisto/python3:3.12.13.10116658
Tagsfield-change-triggered

README

Use this script to get RAW log.
Each RSA NetWitness log contains the eventsource meta that contains an IP address that can be requested using RSA NetWitness Packets and Logs.
This log is after set in the field rsarawlogslist.

Script Data


Name Description
Script Type python3
Tags field-change-triggered

Dependencies


This script uses the following commands and scripts.

  • netwitness-query
  • RSA NetWitness Packets and Logs
  • netwitness-packets

Used In


This script is used in the following playbooks and scripts.

  • rsaalerts

Inputs


There are no inputs for this script.

Outputs


There are no outputs for this script.

Script Examples

Example command


### Context Example

```json
 {
    "RSA Alerts": [
        {
            "created": "2023-07-03T11:04:16.408Z",
            "detail": null,
            "events": [
                {
                    "destination": {},
                    "eventSource": "1.1.1.1:56005",
                    "eventSourceId": "12123434",
                    "source": {}
                }
            ],
            "id": "123456789",
            "riskScore": "50",
            "source": "NetWitness Investigate",
            "title": "Incident name",
            "type": "Log",
        },
    ]
}
import json

import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401

""" STANDALONE FUNCTION """


class RSAError(Exception):
    # exception when an element is not found
    pass


def is_json(myjson):
    try:
        json_object = json.loads(myjson)
        return json_object
    except ValueError:
        return None


def merge_dict(first_dict: dict, second_dict: dict):
    res = first_dict.copy()
    for key, value in second_dict.items():
        if key in res:
            if isinstance(res[key], list):
                if isinstance(value, list):
                    res[key] = list(set(res[key] + value))
                elif value not in res[key]:
                    res[key].append(value)
            elif value != res[key]:
                if isinstance(value, list):
                    res[key] = list({res[key], *value})
                else:
                    res[key] = [res[key], value]
        else:
            res[key] = value
    return res


def get_raw_log(event_source_id: str, concentrator_ip: str, concentrator_port: str) -> list:
    if isCommandAvailable("netwitness-packets") is False:
        return [{"log": "Please add RSA Netwitness Packet & Logs to see more details"}]

    params = {
        "sessions": event_source_id,
        "concentratorIP": concentrator_ip,
        "concentratorPort": concentrator_port,
        "render": "application/json",
        "renderToContext": "true",
    }
    res = demisto.executeCommand("netwitness-packets", params)

    if type(res[0]["Contents"]) is str:
        raise RSAError(res[0]["Contents"])

    return res[0]["Contents"].get("logs")


def get_metas_log(event_source_id: str, concentrator_ip: str, concentrator_port: str) -> list:
    if isCommandAvailable("netwitness-packets") is False:
        return ["Please add/configure RSA Netwitness Packet & Logs to see more details"]

    params = {
        "query": f"select * where sessionid={event_source_id}",
        "concentratorIP": concentrator_ip,
        "concentratorPort": concentrator_port,
    }
    res = demisto.executeCommand("netwitness-query", params)

    if type(res[0]["Contents"]) is str:
        raise RSAError(res[0]["Contents"])

    return res[0]["EntryContext"]["NetWitness.Events"]


def create_id_set(list_metas):
    id_found = []
    for meta in list_metas:
        if "id" in meta:
            id_found.append(meta["id"])
    return id_found


""" MAIN FUNCTION """


def main():
    inc = demisto.incident()
    rsa_alerts = inc.get("CustomFields", {}).get("rsaalerts", [])
    rsa_rawlogs = inc.get("CustomFields", {}).get("rsarawlogslist", [])
    rsa_nb_meta = 1
    rsa_metas = inc.get("CustomFields", {}).get("rsametasevents", [])
    # check in case we forgot to set XSOAR parameter right
    if not rsa_alerts:
        return_results(CommandResults(readable_output="No alert/event was found in this incident."))
        return

    id_set_alerts = create_id_set(rsa_alerts)
    id_set_rawlogs = create_id_set(rsa_rawlogs)
    tmp_nb_meta = len(rsa_metas)

    # in order to get only the new raw log, we only get the nb of changed line
    if set(id_set_alerts) == set(id_set_rawlogs):
        return_results(CommandResults(readable_output="Nothing has changed !"))
        return

    change = 0
    for alert in rsa_alerts:
        # alert already registered and raw log extracted
        if alert["id"] in id_set_rawlogs:
            continue
        rsa_rawlogs.append({"date": alert["created"], "id": alert["id"], "name": alert["title"], "type": "Alert"})
        for event in alert["events"]:
            session = event.get("eventSourceId")
            res = event.get("eventSource").split(":")
            concentrator_ip = res[0]
            concentrator_port = f"5010{res[1][-1]}"
            try:
                raw_log = get_raw_log(session, concentrator_ip, concentrator_port)
                if raw_log and len(raw_log) >= 1:
                    rsa_rawlogs.append(
                        {"date": alert["created"], "id": alert["id"], "name": raw_log[0]["log"], "type": "Raw event"}
                    )
            except RSAError as e:
                return_error(f"Error: {e}")
            except ValueError as e:
                return_results(f"Warning: {e}")
        change += 1

        # get only x raw log
        if tmp_nb_meta < rsa_nb_meta:
            try:
                metas = get_metas_log(session, concentrator_ip, concentrator_port)
                tmp_nb_meta += 1
                # rsa_metas => grid = list
                if len(rsa_metas) >= 1:
                    rsa_metas = [merge_dict(rsa_metas[0], metas[0])]
                else:
                    rsa_metas = [merge_dict({}, metas[0])]
            except RSAError as e:
                return_error(f"Error: {e}")
            except ValueError as e:
                return_results(f"Warning: {e}")

    demisto.executeCommand("setIncident", {"customFields": {"rsarawlogslist": rsa_rawlogs, "rsametasevents": rsa_metas}})
    return_results(CommandResults(readable_output=f"{change} raw log inserts !"))


""" ENTRY POINT """


if __name__ in ("__main__", "__builtin__", "builtins"):
    main()