Exemple #1
0
def result():
    encode = re.search(r"(?<=\+)(.*?)(?=\;)",
                       request.headers["Content-type"]).group(
                           1)  # message encoding
    corr_id = request.headers["X-Request-ID"]  # correlation ID
    status = request.headers['Status']

    profile, device_socket = request.headers["From"].rsplit("@", 1)
    # profile used, device IP:port

    data = safe_json({
        "headers": dict(request.headers),
        "content": safe_json(request.data.decode('utf-8'))
    })
    print(
        f"Received {status} response from {profile}@{device_socket} - {data}")
    print("Writing to buffer.")
    producer = Producer()
    producer.publish(
        message=decode_msg(request.data, encode),  # message being decoded
        headers={
            "socket": device_socket,
            "correlationID": corr_id,
            "profile": profile,
            "encoding": encode,
            "transport": "https"
        },
        exchange="orchestrator",
        routing_key="response")

    return make_response(
        # Body
        encode_msg({
            "status": 200,
            "status_text": "received"
        }, encode),
        # Status Code
        200,
        # Headers
        {
            "Content-type": f"application/openc2-rsp+{encode};version=1.0",
            "Status":
            200,  # Numeric status code supplied by Actuator's OpenC2-Response
            "X-Request-ID": corr_id,
            "Date":
            f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}",  # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
            # "From": f"{profile}@{device_socket}",
            # "Host": f"{orc_id}@{orc_socket}",
        })
Exemple #2
0
def process_message(body, message):
    """
    Callback when we receive a message from internal buffer to publish to waiting flask.
    :param body: Contains the message to be sent.
    :param message: Contains data about the message as well as headers
    """
    body = body if isinstance(body, dict) else safe_json(body)
    rcv_headers = message.headers

    # Device Info
    profile = rcv_headers["profile"]  # profile used

    rcv_time = f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}"
    print(f"Received command from {profile}@{rcv_time} - {body}")
    state[rcv_headers["correlationID"]] = dict(received=rcv_time,
                                               headers=rcv_headers,
                                               body=body)
    def _read_item(self, item, queue, callback: Callable):
        """
        Callback function which is called when an item is read
        """
        # Setup another read of this queue
        self._set_queue_read(queue, callback)

        (channel, method_frame, header_frame, body) = item
        headers = ObjectDict(header_frame.headers)
        body = safe_json(body)

        self._log(
            f"{method_frame.exchange} ({method_frame.routing_key}): {body}",
            system=self.name)
        d = defer.maybeDeferred(callback, headers, body)
        d.addCallbacks(
            lambda _: channel.basic_ack(delivery_tag=method_frame.delivery_tag
                                        ), lambda _: channel.
            basic_nack(delivery_tag=method_frame.delivery_tag))
def process_message(body, message):
    """
    Callback when we receive a message from internal buffer to publish to waiting flask.
    :param body: Contains the message to be sent.
    :param message: Contains data about the message as well as headers
    """
    producer = Producer()

    body = body if isinstance(body, dict) else safe_json(body)
    rcv_headers = message.headers

    orc_socket = rcv_headers["source"]["transport"]["socket"]  # orch IP:port
    orc_id = rcv_headers["source"]["orchestratorID"]  # orchestrator ID
    corr_id = rcv_headers["source"]["correlationID"]  # correlation ID

    for device in rcv_headers["destination"]:
        device_socket = device["socket"]  # device IP:port
        encoding = device["encoding"]  # message encoding

        if device_socket and encoding and orc_socket:
            for profile in device["profile"]:
                print(f"Sending command to {profile}@{device_socket}")
                rtn_headers = {
                    "socket": device_socket,
                    "correlationID": corr_id,
                    "profile": profile,
                    "encoding": encoding,
                    "transport": "https"
                }

                try:
                    rslt = requests.post(
                        url=f"http://{device_socket}",
                        headers={
                            "Content-type": f"application/openc2-cmd+{encoding};version=1.0",
                            # Numeric status code supplied by Actuator's OpenC2-Response
                            # "Status": ...,
                            "X-Request-ID": corr_id,
                            # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
                            "Date": f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}",
                            "From": f"{orc_id}@{orc_socket}",
                            # "Host": f"{profile}@{device_socket}"
                        },
                        data=encode_msg(body, encoding)  # command being encoded
                    )

                    data = {
                        "headers": dict(rslt.headers),
                        "content": decode_msg(rslt.content.decode('utf-8'), encoding)
                    }
                    print(f"Response from request: {rslt.status_code} - {data}")
                    # TODO: UPDATE HEADERS WITH RESPONSE INFO
                    response = safe_json(data['content']) if isinstance(data['content'], dict) else data['content']
                except requests.exceptions.ConnectionError as err:
                    response = str(getattr(err, "message", err))
                    rtn_headers["error"] = True
                    print(f"Connection error: {err}")
                except json.decoder.JSONDecodeError as err:
                    response = str(getattr(err, "message", err))
                    rtn_headers["error"] = True
                    print(f"Message error: {err}")
                except Exception as err:
                    response = str(getattr(err, "message", err))
                    rtn_headers["error"] = True
                    print(f"HTTP error: {err}")

                producer.publish(
                    message=response,
                    headers=rtn_headers,
                    exchange="orchestrator",
                    routing_key="response"
                )
        else:
            response = "Destination/Encoding/Orchestrator Socket of command not specified"
            rcv_headers["error"] = True
            print(response)
            producer.publish(
                message=str(response),
                headers=rcv_headers,
                exchange="orchestrator",
                routing_key="response"
            )
Exemple #5
0
def result():
    encode = re.search(r"(?<=\+)(.*?)(?=\;)", request.headers["Content-type"]).group(1)  # message encoding
    corr_id = request.headers["X-Request-ID"]  # correlation ID
    # data = decode_msg(request.data, encode)  # message being decoded

    # profile, device_socket = request.headers["Host"].rsplit("@", 1)
    # profile used, device IP:port
    orc_id, orc_socket = request.headers["From"].rsplit("@", 1)
    # orchestrator ID, orchestrator IP:port
    message = request.data
    msg_json = decode_msg(message, encode)

    data = safe_json({
        "headers": dict(request.headers),
        "content": safe_json(message.decode('utf-8'))
    })

    rsp = {
        "status": 200,
        "status_text": "received",
        # command id??
    }

    # Basic verify against language schema??

    # get destination actuator
    actuators = list(msg_json.get('actuator', {}).keys())

    print(f"Received command from {orc_id}@{orc_socket} - {data}")
    if msg_json['action'] == "query" and "command" in msg_json['target']:
        print("QUERY COMMAND")
        cmd_id = msg_json['target']['command']
        prev_cmd = state.get(cmd_id)
        if prev_cmd:
            rsp = {
                "status_text": "previous command found",
                "response": {
                    "command": prev_cmd[0]
                }
            }

    else:
        print("Writing to buffer")
        producer = Producer()
        queue_msg = {
            "message": message,
            "headers": {
                "socket": orc_socket,
                # "device": device_socket,
                "correlationID": corr_id,
                # "profile": profile,
                "encoding": encode,
                "orchestratorID": orc_id,
                "transport": "http"
            }
        }
        if len(actuators) == 0:
            print('No NSIDs specified, Send to all')
            try:
                producer.publish(
                    **queue_msg,
                    exchange="actuator_all",
                    exchange_type="fanout",
                    routing_key="actuator_all"
                )
            except Exception as e:
                print(f'Publish Error: {e}')
        else:
            print(f'NSIDs specified - {actuators}')
            for act in actuators:
                producer.publish(
                    **queue_msg,
                    exchange="actuator",
                    routing_key=act
                )

        print(f"Corr_id: {corr_id}")
        for wait in range(0, MAX_WAIT):
            print(f"Checking for response... {MAX_WAIT} - {wait}")
            rsp_cmd = state.get(corr_id)
            if rsp_cmd:
                rsp = rsp_cmd[0]['body']
                break
            time.sleep(1)

    return make_response(
        # Body
        encode_msg(rsp, encode),
        # Status Code
        200,
        # Headers
        {
            "Content-type": f"application/openc2-rsp+{encode};version=1.0",
            "Status": 200,  # Numeric status code supplied by Actuator's OpenC2-Response
            "X-Request-ID": corr_id,
            "Date": f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}",  # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
            # "From": f"{profile}@{device_socket}",
            # "Host": f"{orc_id}@{orc_socket}",
        }
    )
Exemple #6
0
def process_message(body, message):
    """
    Callback when we receive a message from internal buffer to publish to waiting flask.
    :param body: Contains the message to be sent.
    :param message: Contains data about the message as well as headers
    """
    http = urllib3.PoolManager(cert_reqs="CERT_NONE")
    producer = Producer()

    body = body if isinstance(body, dict) else safe_json(body)
    rcv_headers = message.headers

    orc_socket = rcv_headers["source"]["transport"]["socket"]  # orch IP:port
    orc_id = rcv_headers["source"]["orchestratorID"]  # orchestrator ID
    corr_id = rcv_headers["source"]["correlationID"]  # correlation ID

    for device in rcv_headers["destination"]:
        device_socket = device["socket"]  # device IP:port
        encoding = device["encoding"]  # message encoding

        if device_socket and encoding and orc_socket:
            for profile in device["profile"]:
                print(f"Sending command to {profile}@{device_socket}")

                try:
                    rsp = http.request(
                        method="POST",
                        url=f"https://{device_socket}",
                        body=encode_msg(body, encoding),  # command being encoded
                        headers={
                            "Content-type": f"application/openc2-cmd+{encoding};version=1.0",
                            # "Status": ...,  # Numeric status code supplied by Actuator's OpenC2-Response
                            "X-Request-ID": corr_id,
                            "Date": f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}",  # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
                            "From": f"{orc_id}@{orc_socket}",
                            "Host": f"{profile}@{device_socket}",
                        }
                    )

                    rsp_headers = dict(rsp.headers)
                    if "Content-type" in rsp_headers:
                        rsp_enc = re.sub(r"^application/openc2-(cmd|rsp)\+", "", rsp_headers["Content-type"])
                        rsp_enc = re.sub(r"(;version=\d+\.\d+)?$", "", rsp_enc)
                    else:
                        rsp_enc = "json"

                    rsp_headers = {
                        "socket": device_socket,
                        "correlationID": corr_id,
                        "profile": profile,
                        "encoding": rsp_enc,
                        "transport": "https"
                    }

                    data = {
                        "headers": rsp_headers,
                        "content": decode_msg(rsp.data.decode("utf-8"), rsp_enc)
                    }

                    print(f"Response from request: {rsp.status} - {safe_json(data)}")
                    producer.publish(message=data["content"], headers=rsp_headers, exchange="orchestrator", routing_key="response")
                except Exception as err:
                    err = str(getattr(err, "message", err))
                    rcv_headers["error"] = True
                    producer.publish(message=err, headers=rcv_headers, exchange="orchestrator", routing_key="response")
                    print(f"HTTPS error: {err}")

        else:
            response = "Destination/Encoding/Orchestrator Socket of command not specified"
            rcv_headers["error"] = True
            producer.publish(message=str(response), headers=rcv_headers, exchange="orchestrator", routing_key="response")
            print(response)
def process_message(body: Union[dict, str], message: kombu.Message) -> None:
    """
    Callback when we receive a message from internal buffer to publish to waiting flask.
    :param body: Contains the message to be sent.
    :param message: Contains data about the message as well as headers
    """
    producer = Producer()

    body = body if isinstance(body, dict) else safe_json(body)
    rcv_headers = message.headers

    orc_socket = rcv_headers["source"]["transport"]["socket"]  # orch IP:port
    orc_id = rcv_headers["source"]["orchestratorID"]  # orchestrator ID
    corr_id = uuid.UUID(
        rcv_headers["source"]["correlationID"])  # correlation ID

    for device in rcv_headers["destination"]:
        transport = transport_cache.cache.get(device["transport"])
        device_socket = f"{transport['host']}:{transport['port']}"  # device IP:port

        encoding = device["encoding"]  # message encoding
        path = f"/{transport['path']}" if "path" in transport else ""

        if device_socket and encoding and orc_socket:
            with Auth(transport) as auth:
                for profile in device["profile"]:
                    print(f"Sending command to {profile}@{device_socket}")
                    rtn_headers = {
                        "socket": device_socket,
                        "correlationID": str(corr_id),
                        "profile": profile,
                        "encoding": encoding,
                        "transport": "https"
                    }
                    request = Message(
                        recipients=[f"{profile}@{device_socket}"],
                        origin=f"{orc_id}@{orc_socket}",
                        # created=... auto generated
                        msg_type=MessageType.Request,
                        request_id=corr_id,
                        content_type=SerialFormats.from_value(encoding),
                        content=body)
                    prod_kwargs = {
                        "cert": (auth.clientCert, auth.clientKey)
                        if auth.clientCert and auth.clientKey else None,
                        "verify":
                        auth.caCert if auth.caCert else False
                    }

                    try:
                        rslt = requests.post(
                            url=
                            f"http{'s' if transport['prod'] else ''}://{device_socket}{path}",
                            headers={
                                "Content-Type":
                                f"application/openc2-cmd+{encoding};version=1.0",
                                # Numeric status code supplied by Actuator's OpenC2-Response
                                # "Status": ...,
                                "X-Request-ID": str(request.request_id),
                                # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
                                "Date":
                                f"{request.created:%a, %d %b %Y %H:%M:%S GMT}",
                                "From": request.origin,
                                # "Host": f"{profile}@{device_socket}"
                            },
                            data=request.serialize(),  # command being encoded
                            **(prod_kwargs if transport["prod"] else {}))

                        response = Message.oc2_loads(rslt.content, encoding)
                        print(
                            f"Response from request: {rslt.status_code} - H:{dict(rslt.headers)} - C:{response}"
                        )
                        # TODO: UPDATE HEADERS WITH RESPONSE INFO
                        response = response.content
                    except requests.exceptions.ConnectionError as err:
                        response = str(getattr(err, "message", err))
                        rtn_headers["error"] = True
                        print(f"Connection error: {err}")
                    except json.decoder.JSONDecodeError as err:
                        response = str(getattr(err, "message", err))
                        rtn_headers["error"] = True
                        print(f"Message error: {err} - `{rslt.content}`")
                    except Exception as err:
                        response = str(getattr(err, "message", err))
                        rtn_headers["error"] = True
                        print(f"HTTPS error: {err}")

                    producer.publish(message=response,
                                     headers=rtn_headers,
                                     exchange="orchestrator",
                                     routing_key="response")
        else:
            response = "Destination/Encoding/Orchestrator Socket of command not specified"
            rcv_headers["error"] = True
            print(response)
            producer.publish(message=str(response),
                             headers=rcv_headers,
                             exchange="orchestrator",
                             routing_key="response")
Exemple #8
0
def result():
    encode = re.search(r"(?<=\+)(.*?)(?=\;)",
                       request.headers["Content-type"]).group(
                           1)  # message encoding
    corr_id = request.headers["X-Request-ID"]  # correlation ID
    # data = decode_msg(request.data, encode)  # message being decoded

    profile, device_socket = request.headers["Host"].rsplit("@", 1)
    # profile used, device IP:port
    orc_id, orc_socket = request.headers["From"].rsplit("@", 1)
    # orchestrator ID, orchestrator IP:port
    message = request.data
    msg_json = decode_msg(message, encode)

    data = safe_json({
        "headers": dict(request.headers),
        "content": safe_json(message.decode('utf-8'))
    })

    rsp = {
        "status": 200,
        "status_text": "received",
        # command id??
    }

    print(f"Received command from {orc_id}@{orc_socket} - {data}")
    if msg_json['action'] == "query" and "command" in msg_json['target']:
        print("QUERY COMMAND")
        cmd_id = msg_json['target']['command']
        prev_cmd = state.get(cmd_id)
        if prev_cmd:
            rsp = {
                "status_text": "previous command found",
                "response": {
                    "command": prev_cmd
                }
            }

    else:
        print("Writing to buffer")
        producer = Producer()
        producer.publish(message=message,
                         headers={
                             "socket": orc_socket,
                             "device": device_socket,
                             "correlationID": corr_id,
                             "profile": profile,
                             "encoding": encode,
                             "orchestratorID": orc_id,
                             "transport": "https"
                         },
                         exchange="actuator",
                         routing_key=profile)

        print(f"Corr_id: {corr_id}")
        for wait in range(0, MAX_WAIT):
            print(f"Checking for response... {MAX_WAIT} - {wait}")
            rsp_cmd = state.get(corr_id)
            if rsp_cmd:
                rsp = rsp_cmd['body']
                break
            time.sleep(1)

    return make_response(
        # Body
        encode_msg(rsp, encode),
        # Status Code
        200,
        # Headers
        {
            "Content-type": f"application/openc2-rsp+{encode};version=1.0",
            "Status":
            200,  # Numeric status code supplied by Actuator's OpenC2-Response
            "X-Request-ID": corr_id,
            "Date":
            f"{datetime.utcnow():%a, %d %b %Y %H:%M:%S GMT}",  # RFC7231-7.1.1.1 -> Sun, 06 Nov 1994 08:49:37 GMT
            # "From": f"{profile}@{device_socket}",
            # "Host": f"{orc_id}@{orc_socket}",
        })