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 save(message, xmlMessage, namespace_project, namespace): """Generate the XML representation of a Network message""" xmlMessage.set("{http://www.w3.org/2001/XMLSchema-instance}type", L4NetworkMessageFactory.XML_SCHEMA_TYPE) # Add network message properties L2NetworkMessageFactory.addL2PropertiesToElement(xmlMessage, message, namespace) L3NetworkMessageFactory.addL3PropertiesToElement(xmlMessage, message, namespace) L4NetworkMessageFactory.addL4PropertiesToElement(xmlMessage, message, namespace)
def save(message, xmlMessage, namespace_project, namespace): """Generate the XML representation of a Network message""" xmlMessage.set("{http://www.w3.org/2001/XMLSchema-instance}type", L4NetworkMessageFactory.XML_SCHEMA_TYPE) # Add network message properties L2NetworkMessageFactory.addL2PropertiesToElement( xmlMessage, message, namespace) L3NetworkMessageFactory.addL3PropertiesToElement( xmlMessage, message, namespace) L4NetworkMessageFactory.addL4PropertiesToElement( xmlMessage, message, namespace)
def loadFromXML(rootElement, namespace, version, id, timestamp, data): """Function which parses an XML and extract from it the definition of a network message @param rootElement: XML root of the network message @return an instance of a NetworkMessage @raise NameError if XML invalid""" if rootElement.get( "{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") != L4NetworkMessageFactory.XML_SCHEMA_TYPE: raise NameError( "The parsed xml doesn't represent a Network message.") # Retrieve layer 2 properties (l2Protocol, l2SourceAddress, l2DestinationAddress) = L2NetworkMessageFactory.loadL2Properties( rootElement, namespace) # Retrieve layer 3 properties (l3Protocol, l3SourceAddress, l3DestinationAddress) = L3NetworkMessageFactory.loadL3Properties( rootElement, namespace) # Retrieve layer 4 properties (l4Protocol, l4SourcePort, l4DestinationPort) = L4NetworkMessageFactory.loadL4Properties( rootElement, namespace) # IMPORTANT : Avoid circular import from netzob.Common.Models.L4NetworkMessage import L4NetworkMessage message = L4NetworkMessage(id, timestamp, data, l2Protocol, l2SourceAddress, l2DestinationAddress, l3Protocol, l3SourceAddress, l3DestinationAddress, l4Protocol, l4SourcePort, l4DestinationPort) return message
def loadFromXML(rootElement, namespace, version, id, timestamp, data): """Function which parses an XML and extract from it the definition of a network message @param rootElement: XML root of the network message @return an instance of a NetworkMessage @raise NameError if XML invalid""" if rootElement.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") != L4NetworkMessageFactory.XML_SCHEMA_TYPE: raise NameError("The parsed xml doesn't represent a Network message.") # Retrieve layer 2 properties (l2Protocol, l2SourceAddress, l2DestinationAddress) = L2NetworkMessageFactory.loadL2Properties(rootElement, namespace) # Retrieve layer 3 properties (l3Protocol, l3SourceAddress, l3DestinationAddress) = L3NetworkMessageFactory.loadL3Properties(rootElement, namespace) # Retrieve layer 4 properties (l4Protocol, l4SourcePort, l4DestinationPort) = L4NetworkMessageFactory.loadL4Properties(rootElement, namespace) # IMPORTANT : Avoid circular import from netzob.Common.Models.L4NetworkMessage import L4NetworkMessage message = L4NetworkMessage(id, timestamp, data, l2Protocol, l2SourceAddress, l2DestinationAddress, l3Protocol, l3SourceAddress, l3DestinationAddress, l4Protocol, l4SourcePort, l4DestinationPort) return message
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