예제 #1
0
    def getEvents(self):
        """
        Get Events off the DXL fabric
        """

        ## Create Web API request queue
        SERVICE_TYPE = "/opendxl/webapi"
        REQUEST_TOPIC = SERVICE_TYPE + "/requests"

        class MyRequestCallback(RequestCallback):
            def on_request(self, request):
                # Extract
                print("Service recieved request payload: " +
                      request.payload.decode())

        # Create the client
        with DxlClient(config) as client:
            # Connect to the fabric
            client.connect()

            ## Register with ePO and add the request topic
            info = ServiceRegistrationInfo(client, SERVICE_TYPE)
            client.register_service_sync(info, 10)
            info.add_topic(REQUEST_TOPIC, MyRequestCallback())

            ## Get list of vendorIDs and subscribe to each topic
            #vendorList = getVendorList()
            #for vendor in vendorList:
            #    client.add_event_callback(vendorsDict[vendor]['topic'], ChgRepCallback())

            client.disconnect()
            ## Listent to Events
            print("Listening for Events")
            while not thread_stop_event.isSet():
                time.sleep(self.delay)
예제 #2
0
 def __register_service(self):
     registration = ServiceRegistrationInfo(self.dxl_conn,
                                            self.__config.type)
     for name, constructor in SERVICE_ENDPOINTS:
         topic = '{}/{}'.format(self.__config.type, name)
         registration.add_topic(
             topic,
             constructor(
                 self.__monitoring_ctx.register(
                     ServiceEndpointMonitor('endpoint.' + topic)), self))
     self.dxl_conn.register_service_sync(registration, 2)
예제 #3
0
    def initialize(self):
        self.dxl.connect()

        service_reg = ServiceRegistrationInfo(self.dxl, self.__config.type)
        # Register the endpoints that the service provides
        for endpoint in self.__config.endpoints:
            topic = '{}/{}'.format(self.__config.type, endpoint.name)
            monitor = self.__monitoring_ctx.register(
                ServiceEndpointMonitor('endpoints.{}'.format(topic)))
            service_reg.add_topic(topic, Dispatcher(endpoint, monitor, self))

        self.dxl.register_service_sync(service_reg, 2)
예제 #4
0
 def __register_service(self):
     """
     Create dispatchers for each endpoint provided by the GRR API and register the service with the OpenDXL service
     fabric
     :return: None
     """
     svc = ServiceRegistrationInfo(self.dxlc, self.__config.service_type)
     for endpoint in self.__endpoints:
         topic = self.__config.service_type + '/' + endpoint.name
         svc.add_topic(topic,
                       Dispatcher(endpoint, self,
                                  self.__register_monitor(ServiceEndpointMonitor('endpoints.' + topic))))
     self.dxlc.register_service_sync(svc, 5)
 def run_sample_with_service(self, sample_file, sample_args, storage_dir):
     config = DxlClientConfig.create_dxl_config_from_file(
         self._CONFIG_FILE)
     with DxlClient(config) as dxl_client:
         dxl_client.connect()
         info = ServiceRegistrationInfo(
             dxl_client, "test-file-transfer-service")
         info.add_topic(FileTransferClient._DEFAULT_FILE_SEND_TOPIC,
                        TestFileStoreRequestCallback(dxl_client,
                                                     storage_dir))
         dxl_client.register_service_sync(
             info, self._SERVICE_REG_INFO_TIMEOUT)
         mock_print = self.run_sample(sample_file, sample_args)
         dxl_client.unregister_service_sync(
             info, self._SERVICE_REG_INFO_TIMEOUT)
     return mock_print
예제 #6
0
    def on_register_services(self):
        """
        Invoked when services should be registered with the application
        """
        # Register service
        service = ServiceRegistrationInfo(self.client, self.DXL_SERVICE_TYPE)
        for request_topic in self._epo_by_topic:
            service.add_topic(
                str(request_topic),
                _EpoRequestCallback(self.client, self._epo_by_topic))

        logger.info("Registering service ...")
        self.client.register_service_sync(
            service, self.DXL_SERVICE_REGISTRATION_TIMEOUT)
        logger.info("Service registration succeeded.")

        self._dxl_service = service
class MockEpoServer(object):
    def __init__(self,
                 client,
                 id_number=0,
                 use_commands_service=True,
                 user_authorized=True):
        self._client = client
        self.id_number = id_number
        self.use_commands_service = use_commands_service
        self.user_authorized = user_authorized

        # Create DXL Service Registration object
        self._service_registration_info = ServiceRegistrationInfo(
            self._client,
            "/mcafee/service/epo/commands"
            if self.use_commands_service else "/mcafee/service/epo/remote"
        )
        self._service_registration_info._ttl_lower_limit = 5
        self._service_registration_info.ttl = 5
        if self.use_commands_service:
            self._service_registration_info.metadata = \
                {"epoGuid": LOCAL_TEST_SERVER_NAME + str(id_number)}

    def __enter__(self):
        mock_callback = FakeEpoServerCallback(self._client,
                                              self.id_number,
                                              self.use_commands_service,
                                              self.user_authorized)

        self._service_registration_info.add_topic(
            mock_callback.epo_request_topic,
            mock_callback
        )

        self._client.register_service_sync(self._service_registration_info, 10)

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._client.unregister_service_sync(self._service_registration_info,
                                             10)
    def _dxl_connect(self):
        """
        Attempts to connect to the DXL fabric and register the ePO DXL service
        """

        # Connect to fabric
        config = DxlClientConfig.create_dxl_config_from_file(
            self._dxlclient_config_path)
        config.incoming_message_thread_pool_size = self._incoming_thread_count
        config.incoming_message_queue_size = self._incoming_queue_size
        logger.info(
            "Incoming message configuration: queueSize={0}, threadCount={1}".
            format(config.incoming_message_queue_size,
                   config.incoming_message_thread_pool_size))

        client = DxlClient(config)
        logger.info("Attempting to connect to DXL fabric ...")
        client.connect()
        logger.info("Connected to DXL fabric.")

        try:
            # Register service
            service = ServiceRegistrationInfo(client, self.DXL_SERVICE_TYPE)
            for request_topic in self._epo_by_topic:
                service.add_topic(
                    str(request_topic),
                    _EpoRequestCallback(client, self._epo_by_topic))

            logger.info("Registering service ...")
            client.register_service_sync(service,
                                         self.DXL_SERVICE_REGISTRATION_TIMEOUT)
            logger.info("Service registration succeeded.")
        except:
            client.destroy()
            raise

        self._dxl_client = client
        self._dxl_service = service
class MockEpoServer(object):
    def __init__(self, client, id_number=0):
        self._client = client
        self.id_number = id_number

        # Create DXL Service Registration object
        self._service_registration_info = ServiceRegistrationInfo(
            self._client,
            "/mcafee/service/epo/remote",
        )
        self._service_registration_info._ttl_lower_limit = 5
        self._service_registration_info.ttl = 5

    def __enter__(self):
        mock_callback = FakeEpoServerCallback(self._client, self.id_number)

        self._service_registration_info.add_topic(
            mock_callback.epo_request_topic, mock_callback)

        self._client.register_service_sync(self._service_registration_info, 10)

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._client.unregister_service_sync(self._service_registration_info,
                                             10)
예제 #10
0
                # Create the response message
                response = Response(request)
                # Populate the response payload
                response.payload = weather_response.encode(encoding="UTF-8")
                # Send the response
                client.send_response(response)

            except Exception as ex:
                print str(ex)
                # Send error response
                client.send_response(
                    ErrorResponse(
                        request,
                        error_message=str(ex).encode(encoding="UTF-8")))

    # Create service registration object
    info = ServiceRegistrationInfo(client, SERVICE_NAME)

    # Add a topic for the service to respond to
    info.add_topic(SERVICE_CURRENT_WEATHER_TOPIC, CurrentWeatherCallback())

    # Register the service with the fabric (wait up to 10 seconds for registration to complete)
    client.register_service_sync(info, 10)

    logger.info("Weather service is running...")

    # Wait forever
    while True:
        time.sleep(60)
예제 #11
0
with DxlClient(config) as client:

    client.connect()

    class DxlService(RequestCallback):
        def on_request(self, request):
            try:
                query = request.payload.decode()
                logger.info("Service received request payload: " + query)
                response = Response(request)
                response.payload = str(
                    wrapper.action(messagefilter.action(query))).encode()
                client.send_response(response)
                print response

            except Exception as ex:
                print str(ex)
                client.send_response(
                    ErrorResponse(request, error_message=str(ex).encode()))

    info = ServiceRegistrationInfo(client, SERVICE_INPUT)
    info.add_topic(TOPIC_INPUT, DxlService())
    # Register the service with the fabric (wait up to 10 seconds for registration to complete)
    client.register_service_sync(info, 10)
    logger.info("Service is running on topic: " + TOPIC_INPUT)

    # Wait forever
    while True:
        time.sleep(60)
예제 #12
0
                logger.info("Ignoring cancel request " + cam.transaction_id +
                            " for application " + cam.requestor_id)

            # Send request acknowledgement message with the transaction
            # id that was cancelled
            res = Response(request)
            ram = RequestAcknowledgementMessage()
            ram.transaction_id = cam.transaction_id
            res.payload = (ram.to_json()).encode()
            client.send_response(res)

    # Prepare service registration information
    info = ServiceRegistrationInfo(client, "/scap/manager")

    # Have manager provide assessment request, cancel assessment, and query services
    info.add_topic(SERVICE_INITIATE_ASSESSMENT_TOPIC,
                   InitiateAssessmentCallback())
    info.add_topic(SERVICE_CANCEL_ASSESSMENT_TOPIC, CancelAssessmentCallback())

    # Connect to the message fabric and register the service
    client.connect()
    client.register_service_sync(info, 10)

    # Wait forever
    while True:
        # Process all initiate assessment requests that were received
        while assessment_requests:
            ar = assessment_requests.pop(0)

            if ar.message_type == MessageType.CANCEL_ASSESSMENT.value:

                iam = InitiateAssessmentMessage()
예제 #13
0
class MockVtService(object):

    mockvt_basic_test_topic = FakeVTServiceCallback.vt_basic_test_topic

    def __init__(self, client):
        self._client = client

        # Create DXL Service Registration object
        self._service_registration_info = ServiceRegistrationInfo(
            self._client,
            VirusTotalApiClient._SERVICE_TYPE,
        )
        self._service_registration_info._ttl_lower_limit = 5
        self._service_registration_info.ttl = 5

    def __enter__(self):
        mock_callback = FakeVTServiceCallback(self._client)

        self._service_registration_info.add_topic(
            MockVtService.mockvt_basic_test_topic, mock_callback)

        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_DOMAIN_REPORT, mock_callback)
        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_FILE_REPORT, mock_callback)
        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_FILE_RESCAN, mock_callback)
        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_IP_ADDRESS_REPORT, mock_callback)
        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_URL_REPORT, mock_callback)
        self._service_registration_info.add_topic(
            VirusTotalApiClient._REQ_TOPIC_URL_SCAN, mock_callback)

        self._client.register_service_sync(self._service_registration_info, 10)

        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._client.unregister_service_sync(self._service_registration_info,
                                             10)
예제 #14
0
        elif instructions == "remaining_request":
            assessment_results = "remaining " + OS + " results (" + PCE_ID + ")"
        else:
            assessment_results = "unknown results"

        return assessment_results

    # Process incoming collection requests from the manager
    class PCERequestCallback(RequestCallback):
        def on_request(self, request):
            logger.info("PCE received payload: %s", request.payload.decode())
            collection_requests.append(request)

    # Prepare service registration information
    info = ServiceRegistrationInfo(client, "/scap/pce" + PCE_ID)
    info.add_topic(SERVICE_PCE_REQUEST_TOPIC, PCERequestCallback())

    # Connect to the message fabric and add a listener for registration events
    client.connect()
    client.register_service_sync(info, 10)

    # Register PCE by sending registration event to the collector/PCX
    event = Event(EVENT_PCE_REGISTRATION_TOPIC)
    rm = RegistrationMessage(PCE_ID, "", "", ASSET, MAKE, MODEL, "", "", "",
                             "", "", SUPPORTED_CHECK_TYPES)
    event.payload = (rm.to_json()).encode()
    logger.info("Sending registration event: %s", rm.to_s())
    client.send_event(event)

    # Wait forever
    while True:
예제 #15
0
class MockTieServer(object):
    def __init__(self, client):
        self._client = client

        # Create DXL Service Registration object
        self._service_registration_info = ServiceRegistrationInfo(
            self._client, "/opendxl/mocktieserver")

    def __enter__(self):
        mock_callback = FakeTieServerCallback(self._client)

        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_GET_FILE_REPUTATION_TOPIC, mock_callback)
        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_GET_CERT_REPUTATION_TOPIC, mock_callback)
        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_SET_FILE_REPUTATION_TOPIC, mock_callback)
        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_SET_CERT_REPUTATION_TOPIC, mock_callback)
        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_GET_FILE_FIRST_REFS, mock_callback)
        self._service_registration_info.add_topic(
            dxltieclient.client.TIE_GET_CERT_FIRST_REFS, mock_callback)

        self._client.register_service_sync(self._service_registration_info, 10)

        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._client.unregister_service_sync(self._service_registration_info,
                                             10)
예제 #16
0
            message_type = j["message_type"]

            if message_type == MessageType.INITIATE_ASSESSMENT.value:
                iam = InitiateAssessmentMessage()
                iam.parse(event.payload.decode())
                store_request(iam)
            elif message_type == MessageType.REPORT_RESULTS.value:
                rrsm = ReportResultsMessage()
                rrsm.parse(event.payload.decode())
                store_results(rrsm)
            elif message_type == MessageType.REGISTRATION.value:
                rm = RegistrationMessage()
                rm.parse(event.payload.decode())
                store_assets(rm)
            else:
                store_arbitrary_data(event.payload.decode())

    # Prepare service registration information
    info = ServiceRegistrationInfo(client, "/scap/repository")

    # Connect to the message fabric and add listeners for query requests and
    # storage requests
    client.connect()
    client.add_event_callback(EVENT_STORE_DATA_TOPIC, StoreDataEventCallback())
    info.add_topic(SERVICE_REPOSITORY_QUERY_TOPIC, QueryRequestCallback())
    client.register_service_sync(info, 10)

    # Wait forever
    while True:
        time.sleep(1)
# Create DXL configuration from file
config = DxlClientConfig.create_dxl_config_from_file(CONFIG_FILE)

# Create the client
with DxlClient(config) as client:
    # Connect to the fabric
    client.connect()

    class MyRequestCallback(RequestCallback):
        def on_request(self, request):
            # Extract
            print "Service recieved request payload: " + request.payload.decode(
            )
            # Create the response message
            res = Response(request)
            res.payload = "pong".encode()
            # Send the response
            client.send_event_response(res)

    # Create service reg object
    info = ServiceRegistrationInfo(client, "ScottoService")
    # Add topic for the service to repond to
    info.add_topic(SERVICE_TOPIC, MyRequestCallback())
    # Register the service with the fabric ( wait up to 10 seconds for registration to complete)
    client.register_service_sync(info, 10)
    logger.info("Ping Service is running ... ")

    while True:
        time.sleep(60)
                # Encode string payload as UTF-8
                response.payload = "Sample Response Payload".encode()

                # Send the Response back
                logger.info(
                    "Service Provider - Sending Response to Request ID: %s on %s",
                    response.request_message_id, request.destination_topic)
                client.send_response(response)

        # Create DXL Service Registration object
        service_registration_info = ServiceRegistrationInfo(
            client, "/mycompany/myservice")

        # Add a topic for the service to respond to
        service_registration_info.add_topic(SERVICE_TOPIC, MyRequestCallback())

        # Register the service with the DXL fabric (with a wait up to 10 seconds for registration to complete)
        logger.info("Registering service.")
        client.register_service_sync(service_registration_info, 10)

        # Wait for DXL Requests
        while True:
            print "   Enter 9 to quit"
            input = raw_input("   Enter value: ")

            try:
                option = int(input)
            except:
                option = input
    sys.stdout.flush()


# Create the client
with DxlClient(config) as dxl_client:

    # Connect to the fabric
    dxl_client.connect()

    logger.info("Connected to DXL fabric.")

    # Create service registration object
    info = ServiceRegistrationInfo(dxl_client, "myService")

    # Add a topic for the service to respond to
    info.add_topic(SERVICE_TOPIC,
                   FileStoreRequestCallback(dxl_client, STORAGE_DIR))

    # Register the service with the fabric (wait up to 10 seconds for
    # registration to complete)
    dxl_client.register_service_sync(info, 10)

    # Create client wrapper
    file_transfer_client = FileTransferClient(dxl_client, SERVICE_TOPIC)

    start = time.time()

    # Invoke the send file request method to store the file on the server
    resp = file_transfer_client.send_file_request(
        STORE_FILE_NAME,
        file_name_on_server=os.path.join(STORE_FILE_DIR,
                                         os.path.basename(STORE_FILE_NAME)),
예제 #20
0
        <name>Angry User</name> \
        <email>[email protected]</email> \
        <subject>Testing API</subject> \
        <message type="text/plain"><![CDATA[%s]]></message> \
        </ticket>' % msg
    URL = "https://<YOURURIHERE>/api/tickets.xml"
    API_KEY = "<YOURAPIKEYHERE>"  #passed as X-API-Key
    headers = {'X-API-Key': API_KEY}
    r = requests.post(URL, xmlpayload, headers=headers)
    return r.text


with DxlClient(config) as client:
    # Connect to the fabric
    client.connect()
    print "Connected to DXL"

    class tickethandler(RequestCallback):
        def on_request(self, request):
            print "Received request %s" % request.payload
            res = Response(request)
            res.payload = openticket(request.payload)
            print "Received Response from Service: %s" % res.payload
            client.send_response(res)

    info = ServiceRegistrationInfo(client, SERVICE_TOPIC)
    info.add_topic(SERVICE_TOPIC, tickethandler())
    client.register_service_sync(info, 10)
    while True:
        time.sleep(60)