def save(message, root, namespace_project, namespace_common):
        """Generate an XML representation of a message"""

        # Create the initial xml tree
        xmlMessage = etree.SubElement(root, "{" + namespace_common + "}message")
        xmlMessage.set("id", str(message.getID()))
        xmlMessage.set("timestamp", str(message.getTimestamp()))

        # Add the data
        subData = etree.SubElement(xmlMessage, "{" + namespace_common + "}data")
        subData.text = str(message.getData())

        # Depending of the type, we add its proper meta-datas
        if message.getType() == "File":
            FileMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "L2Network":
            L2NetworkMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "L3Network":
            L3NetworkMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "L4Network":
            L4NetworkMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "IPC":
            IPCMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "IRP":
            IRPMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "IRPDeviceIoControl":
            IRPDeviceIoControlMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        elif message.getType() == "RAW":
            RawMessageFactory.save(message, xmlMessage, namespace_project, namespace_common)
        else:
            raise NameError('''There is no factory which would support
            the generation of an xml representation of the message : ''' + str(message))

        return etree.tostring(xmlMessage)
Beispiel #2
0
    def save(message, xmlMessage, namespace_project, namespace):

        xmlMessage.set("{http://www.w3.org/2001/XMLSchema-instance}type", IRPDeviceIoControlMessageFactory.XML_SCHEMA_TYPE)

        IRPMessageFactory.addPropertiesToElement(xmlMessage, message, namespace)

        # ioctl
        subIoctl = etree.SubElement(xmlMessage, "{" + namespace + "}ioctl")
        subIoctl.text = str(message.getIOCTL())
    def save(message, root, namespace_project, namespace_common):
        """Generate an XML representation of a message"""

        # Create the initial xml tree
        xmlMessage = etree.SubElement(root,
                                      "{" + namespace_common + "}message")
        xmlMessage.set("id", str(message.getID()))
        xmlMessage.set("timestamp", str(message.getTimestamp()))

        # Add the data
        subData = etree.SubElement(xmlMessage,
                                   "{" + namespace_common + "}data")
        subData.text = str(message.getData())

        # Depending of the type, we add its proper meta-datas
        if message.getType() == "File":
            FileMessageFactory.save(message, xmlMessage, namespace_project,
                                    namespace_common)
        elif message.getType() == "L2Network":
            L2NetworkMessageFactory.save(message, xmlMessage,
                                         namespace_project, namespace_common)
        elif message.getType() == "L3Network":
            L3NetworkMessageFactory.save(message, xmlMessage,
                                         namespace_project, namespace_common)
        elif message.getType() == "L4Network":
            L4NetworkMessageFactory.save(message, xmlMessage,
                                         namespace_project, namespace_common)
        elif message.getType() == "IPC":
            IPCMessageFactory.save(message, xmlMessage, namespace_project,
                                   namespace_common)
        elif message.getType() == "IRP":
            IRPMessageFactory.save(message, xmlMessage, namespace_project,
                                   namespace_common)
        elif message.getType() == "IRPDeviceIoControl":
            IRPDeviceIoControlMessageFactory.save(message, xmlMessage,
                                                  namespace_project,
                                                  namespace_common)
        elif message.getType() == "RAW":
            RawMessageFactory.save(message, xmlMessage, namespace_project,
                                   namespace_common)
        else:
            raise NameError('''There is no factory which would support
            the generation of an xml representation of the message : ''' +
                            str(message))

        return etree.tostring(xmlMessage)
    def loadFromXML(rootElement, namespace, version):
        """loadFromXML:
           Function which parses an XML and extract from it
           the definition of a file message
           @param rootElement: XML root of the file message
           @return an instance of a message
           @throw NameError if XML invalid"""

        # Computes which type is it
        if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "abstract":
            raise NameError("The parsed xml doesn't represent a valid type message.")

        # Parse common attributes (id, timestamp, data)
        # Parse the data field and transform it into a byte array
        if rootElement.find("{" + namespace + "}data") is None or not rootElement.find("{" + namespace + "}data").text:
            raise NameError("The parsed message has no data specified")
        data = bytearray(rootElement.find("{" + namespace + "}data").text)
        id = str(rootElement.get("id"))
        timestamp = float(rootElement.get("timestamp"))

        if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == FileMessageFactory.XML_SCHEMA_TYPE:
            return FileMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

#        # Preserve compatibility with former traces
#        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob-common:NetworkMessage":
#            return OldFormatNetworkMessageFactory.loadFromXML(rootElement, namespace, version)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == L2NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L2NetworkMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == L3NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L3NetworkMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == L4NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L4NetworkMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == IPCMessageFactory.XML_SCHEMA_TYPE:
            return IPCMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == IRPMessageFactory.XML_SCHEMA_TYPE:
            return IRPMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == IRPDeviceIoControlMessageFactory.XML_SCHEMA_TYPE:
            return IRPDeviceIoControlMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == RawMessageFactory.XML_SCHEMA_TYPE:
            return RawMessageFactory.loadFromXML(rootElement, namespace, version, id, timestamp, data)
        else:
            raise NameError("The parsed xml doesn't represent a valid type message.")
            return None
    def loadFromXML(rootElement, namespace, version):
        """loadFromXML:
           Function which parses an XML and extract from it
           the definition of a file message
           @param rootElement: XML root of the file message
           @return an instance of a message
           @throw NameError if XML invalid"""

        # Computes which type is it
        if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                           "abstract") == "abstract":
            raise NameError(
                "The parsed xml doesn't represent a valid type message.")

        # Parse common attributes (id, timestamp, data)
        # Parse the data field and transform it into a byte array
        if rootElement.find("{" + namespace +
                            "}data") is None or not rootElement.find(
                                "{" + namespace + "}data").text:
            raise NameError("The parsed message has no data specified")
        data = bytearray(rootElement.find("{" + namespace + "}data").text)
        id = str(rootElement.get("id"))
        timestamp = float(rootElement.get("timestamp"))

        if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                           "abstract") == FileMessageFactory.XML_SCHEMA_TYPE:
            return FileMessageFactory.loadFromXML(rootElement, namespace,
                                                  version, id, timestamp, data)

#        # Preserve compatibility with former traces
#        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob-common:NetworkMessage":
#            return OldFormatNetworkMessageFactory.loadFromXML(rootElement, namespace, version)

        elif rootElement.get(
                "{http://www.w3.org/2001/XMLSchema-instance}type",
                "abstract") == L2NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L2NetworkMessageFactory.loadFromXML(rootElement, namespace,
                                                       version, id, timestamp,
                                                       data)

        elif rootElement.get(
                "{http://www.w3.org/2001/XMLSchema-instance}type",
                "abstract") == L3NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L3NetworkMessageFactory.loadFromXML(rootElement, namespace,
                                                       version, id, timestamp,
                                                       data)

        elif rootElement.get(
                "{http://www.w3.org/2001/XMLSchema-instance}type",
                "abstract") == L4NetworkMessageFactory.XML_SCHEMA_TYPE:
            return L4NetworkMessageFactory.loadFromXML(rootElement, namespace,
                                                       version, id, timestamp,
                                                       data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == IPCMessageFactory.XML_SCHEMA_TYPE:
            return IPCMessageFactory.loadFromXML(rootElement, namespace,
                                                 version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == IRPMessageFactory.XML_SCHEMA_TYPE:
            return IRPMessageFactory.loadFromXML(rootElement, namespace,
                                                 version, id, timestamp, data)

        elif rootElement.get(
                "{http://www.w3.org/2001/XMLSchema-instance}type", "abstract"
        ) == IRPDeviceIoControlMessageFactory.XML_SCHEMA_TYPE:
            return IRPDeviceIoControlMessageFactory.loadFromXML(
                rootElement, namespace, version, id, timestamp, data)

        elif rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == RawMessageFactory.XML_SCHEMA_TYPE:
            return RawMessageFactory.loadFromXML(rootElement, namespace,
                                                 version, id, timestamp, data)
        else:
            raise NameError(
                "The parsed xml doesn't represent a valid type message.")
            return None
Beispiel #6
0
    def loadFromXML(rootElement, namespace, version, id, timestamp, data):

        # Then we verify its an IPC Message
        if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") != IRPDeviceIoControlMessageFactory.XML_SCHEMA_TYPE:
            raise NameError("The parsed xml doesn't represent a IRP IOCTL message.")

        # Retrieve properties from normal IRP
        (msg_direction, msg_major, msg_minor, msg_requestMode, msg_pid, msg_status, msg_information, msg_cancel, msg_sizeIn, msg_sizeOut) = IRPMessageFactory.loadProperties(rootElement, namespace)

        # Retrieves the ioctl
        msg_ioctl = int(rootElement.find("{" + namespace + "}ioctl").text)

        # TODO : verify this ! Circular imports in python !
        # WARNING : verify this ! Circular imports in python !
        from netzob.Common.Models.IRPDeviceIoControlMessage import IRPDeviceIoControlMessage

        result = IRPDeviceIoControlMessage(id, timestamp, data, msg_direction, msg_major, msg_minor, msg_requestMode, msg_pid, msg_status, msg_information, msg_cancel, msg_sizeIn, msg_sizeOut, msg_ioctl)

        return result