예제 #1
0
    def _get_namespace_def(self, additional_ns_dict=None):
        # copy necessary namespaces

        namespaces = self._get_namespaces()

        if additional_ns_dict:
            for ns, prefix in additional_ns_dict.iteritems():
                namespaces.update([Namespace(ns, prefix)])

        # TODO: For now, always add the ID namespace. Later we can figure out
        # how to intelligently do it only when necessary
        namespaces.update([cybox.utils.idgen._get_generator().namespace])

        # if there are any other namepaces, include xsi for "schemaLocation"
        if namespaces:
            namespaces.update([META.lookup_prefix('xsi')])

        if not namespaces:
            return ""

        namespaces = sorted(namespaces, key=str)

        return ('\n\t' + get_xmlns_string(namespaces) +
                '\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
                '"')
예제 #2
0
    def _get_namespaces(self, recurse=True):
        nsset = set()

        # Get all _namespaces for parent classes
        namespaces = [x._namespace for x in self.__class__.__mro__
                      if hasattr(x, '_namespace')]

        nsset.update([maecMETA.lookup_namespace(ns) for ns in namespaces])

        #In case of recursive relationships, don't process this item twice
        self.touched = True
        if recurse:
            for x in self._get_children():
                if not hasattr(x, 'touched'):
                    nsset.update(x._get_namespaces())
        del self.touched

        # Add any additional namespaces that may be included in the entity
        entity_dict = self.__dict__
        input_ns = entity_dict.get("__input_namespaces__", {})
        for namespace, alias in input_ns.items():
            maec_ns = maecMETA.lookup_namespace(namespace)
            cybox_ns = META.lookup_namespace(namespace)
            if not maec_ns and not cybox_ns:
                nsset.add(Namespace(namespace, alias))

        return nsset
예제 #3
0
    def _get_namespace_def(self, additional_ns_dict=None):
        # copy necessary namespaces

        namespaces = self._get_namespaces()

        if additional_ns_dict:
            for ns, prefix in additional_ns_dict.iteritems():
                namespaces.update([Namespace(ns, prefix)])

        # TODO: For now, always add the ID namespace. Later we can figure out
        # how to intelligently do it only when necessary
        namespaces.update([cybox.utils.idgen._get_generator().namespace])

        # if there are any other namepaces, include xsi for "schemaLocation"
        if namespaces:
            namespaces.update([META.lookup_prefix('xsi')])

        if not namespaces:
            return ""

        namespaces = sorted(namespaces, key=str)

        return ('\n\t' + get_xmlns_string(namespaces) +
                '\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
                '"')
예제 #4
0
 def process_bundle_namespace(self, bundle):
     if bundle.get_Malware_Instance_Object_Attributes() is not None:
         self.get_namespace_from_object(bundle.get_Malware_Instance_Object_Attributes())
     if bundle.get_Process_Tree() is not None:
         self.add_object_namespace(META.lookup_object('ProcessObjectType'))
     if bundle.get_Behaviors() is not None:
         for behavior in bundle.get_Behaviors().get_Behavior():
             self.process_behavior_namespace(behavior)
     if bundle.get_Actions() is not None:
         for action in bundle.get_Actions().get_Action():
             self.process_action_namespace(action)
     if bundle.get_Objects() is not None:
         for object in bundle.get_Objects().get_Object():
             self.get_namespace_from_object(object)
     if bundle.get_Collections() is not None:
         collections = bundle.get_Collections()
         if collections.get_Behavior_Collections() is not None:
             for behavior_collection in collections.get_Behavior_Collections().get_Behavior_Collection():
                 if behavior_collection.get_Behavior_List() is not None:
                     for behavior in behavior_collection.get_Behavior_List().get_Behavior():
                         self.process_behavior_namespace(behavior)
         if collections.get_Action_Collections() is not None:
             for action_collection in collections.get_Action_Collections().get_Action_Collection():
                 if action_collection.get_Action_List() is not None:
                     for action in action_collection.get_Action_List().get_Action():
                         self.process_action_namespace(action)
         if collections.get_Object_Collections() is not None:
             for object_collection in collections.get_Object_Collections().get_Object_Collection():
                 if object_collection.get_Object_List() is not None:
                     for object in object_collection.get_Object_List().get_Object():
                         self.get_namespace_from_object(object)
예제 #5
0
    def _get_namespaces(self, recurse=True):
        ns = set()

        # If this raises an AttributeError, it's because the object doesn't
        # have a "_namespace" element. All subclasses should define this.
        ns.update([META.lookup_namespace(self._namespace)])

        #In case of recursive relationships, don't process this item twice
        self.touched = True
        if recurse:
            for x in self._get_children():
                if not hasattr(x, 'touched'):
                    ns.update(x._get_namespaces())
        del self.touched

        return ns
예제 #6
0
    def _get_namespaces(self, recurse=True):
        nsset = set()

        # Get all _namespaces for parent classes
        namespaces = [x._namespace for x in self.__class__.__mro__ if hasattr(x, "_namespace")]

        nsset.update([META.lookup_namespace(ns) for ns in namespaces])

        # In case of recursive relationships, don't process this item twice
        self.touched = True
        if recurse:
            for x in self._get_children():
                if not hasattr(x, "touched"):
                    nsset.update(x._get_namespaces())
        del self.touched

        return nsset
예제 #7
0
    def _get_namespace_def(self, additional_ns_dict=None):
        # copy necessary namespaces

        namespaces = self._get_namespaces()

        if additional_ns_dict:
            for ns, prefix in additional_ns_dict.iteritems():
                namespaces.update([Namespace(ns, prefix)])

        # if there are any other namepaces, include xsi for "schemaLocation"
        if namespaces:
            namespaces.update([META.lookup_prefix('xsi')])

        if not namespaces:
            return ""

        return ('\n\t' + get_xmlns_string(namespaces) +
                '\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
                '"')
예제 #8
0
    def get_namespace_schemalocation_str(self):
        '''Get the namespace/schemalocation declaration string'''
        output_string = '\n '
        schemalocs = []
        #Add the XSI, MAEC, and CybOX Core/Common namespaces and schemalocation
        output_string += 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n '
        if isinstance(self.maec_entity, package_binding.PackageType):
            output_string += 'xmlns:maecPackage="http://maec.mitre.org/XMLSchema/maec-package-2" \n '
            output_string += 'xmlns:maecBundle="http://maec.mitre.org/XMLSchema/maec-bundle-4" \n '
            output_string += 'xmlns:maecVocabs="http://maec.mitre.org/default_vocabularies-1" \n '
            output_string += 'xmlns:mmdef="http://xml/metadataSharing.xsd" \n '
            schemalocs.append('http://maec.mitre.org/default_vocabularies-1 http://maec.mitre.org/language/version4.0/maec_default_vocabularies.xsd')
            schemalocs.append(' http://maec.mitre.org/XMLSchema/maec-package-2 http://maec.mitre.org/language/version4.0/maec_package_schema.xsd')
        elif isinstance(self.maec_entity, bundle_binding.BundleType):
            output_string += 'xmlns:maecVocabs="http://maec.mitre.org/default_vocabularies-1" \n '
            output_string += 'xmlns:maecBundle="http://maec.mitre.org/XMLSchema/maec-bundle-4" \n '
            output_string += 'xmlns:mmdef="http://xml/metadataSharing.xsd" \n '
            schemalocs.append('http://maec.mitre.org/default_vocabularies-1 http://maec.mitre.org/language/version4.0/maec_default_vocabularies.xsd')
            schemalocs.append(' http://maec.mitre.org/XMLSchema/maec-bundle-4 http://maec.mitre.org/language/version4.0/maec_bundle_schema.xsd')
        output_string += 'xmlns:cybox="http://cybox.mitre.org/cybox-2" \n '
        output_string += 'xmlns:cyboxCommon="http://cybox.mitre.org/common-2" \n '
        output_string += 'xmlns:cyboxVocabs="http://cybox.mitre.org/default_vocabularies-2" \n '
        schemalocs.append(' http://cybox.mitre.org/cybox-2 http://cybox.mitre.org/XMLSchema/core/2.0/cybox_core.xsd')
        schemalocs.append(' http://maec.mitre.org/default_vocabularies-1 http://cybox.mitre.org/XMLSchema/default_vocabularies/2.0.0/cybox_default_vocabularies.xsd')
        
        for object_type in self.object_types:
            namespace = META.lookup_object(object_type).namespace
            namespace_prefix = META.lookup_namespace(namespace).prefix
            output_string += ('xmlns:' + namespace_prefix + '=' + '"' + namespace + '"' + ' \n ')

        for object_type_dependency in self.object_type_dependencies:
            if object_type_dependency not in self.object_types:
                namespace = META.lookup_object(object_type_dependency).namespace
                namespace_prefix = META.lookup_namespace(namespace).prefix
                output_string += ('xmlns:' + namespace_prefix + '=' + '"' + namespace + '"' + ' \n ')
        
        output_string += 'xsi:schemaLocation="'
        
        for object_type in self.object_types:
            namespace = META.lookup_object(object_type).namespace
            schemalocation = META.lookup_namespace(namespace).schema_location
            schemalocs.append(' ' + namespace + ' ' + schemalocation)

        for schemalocation_string in schemalocs:
            if schemalocs.index(schemalocation_string) == (len(schemalocs) - 1):
                output_string += (schemalocation_string + '"')
            else:
                output_string += (schemalocation_string + '\n')
        
        return output_string
예제 #9
0
    def _get_namespaces(self, recurse=True):
        nsset = set()

        # Get all _namespaces for parent classes
        namespaces = [
            x._namespace for x in self.__class__.__mro__
            if hasattr(x, '_namespace')
        ]

        nsset.update([META.lookup_namespace(ns) for ns in namespaces])

        #In case of recursive relationships, don't process this item twice
        self.touched = True
        if recurse:
            for x in self._get_children():
                if not hasattr(x, 'touched'):
                    nsset.update(x._get_namespaces())
        del self.touched

        return nsset