コード例 #1
0
ファイル: VariableView.py プロジェクト: KurSh/netzob
    def addAlternate(self, event, rootVariable, rootEntry):
        # Display the form for the creation of a word variable
        dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK, None)
        dialog.set_markup(_("Definition of the Alternative"))

        # Create the ID of the new variable
        variableID = str(uuid.uuid4())

        mainTable = gtk.Table(rows=3, columns=2, homogeneous=False)
        # parent id of the variable
        variablePIDLabel = gtk.Label(_("Parent ID:"))
        variablePIDLabel.show()
        variablePIDValueLabel = gtk.Label(str(rootVariable.getID()))
        variablePIDValueLabel.set_sensitive(False)
        variablePIDValueLabel.show()
        mainTable.attach(variablePIDLabel, 0, 1, 0, 1, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)
        mainTable.attach(variablePIDValueLabel, 1, 2, 0, 1, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)

        # id of the variable
        variableIDLabel = gtk.Label(_("ID:"))
        variableIDLabel.show()
        variableIDValueLabel = gtk.Label(variableID)
        variableIDValueLabel.set_sensitive(False)
        variableIDValueLabel.show()
        mainTable.attach(variableIDLabel, 0, 1, 1, 2, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)
        mainTable.attach(variableIDValueLabel, 1, 2, 1, 2, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)

        # name of the variable
        variableValueLabel = gtk.Label(_("Name:"))
        variableValueLabel.show()
        variableValueEntry = gtk.Entry()
        variableValueEntry.show()
        mainTable.attach(variableValueLabel, 0, 1, 2, 3, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)
        mainTable.attach(variableValueEntry, 1, 2, 2, 3, xoptions=gtk.FILL, yoptions=0, xpadding=5, ypadding=5)

        dialog.vbox.pack_end(mainTable, True, True, 0)
        dialog.show_all()
        result = dialog.run()

        if result != gtk.RESPONSE_OK:
            dialog.destroy()
            return

        # We retrieve the name of the variable
        varName = variableValueEntry.get_text()

        # Creation of the aggregate id, name, mutable, value):
        alternateVariable = AlternateVariable(variableID, varName, None)
        rootVariable.addChild(alternateVariable)

        self.datas[str(alternateVariable.getID())] = alternateVariable

        self.treestore.append(rootEntry, [str(alternateVariable.getID()), _("Alternate")])

        # We close the current dialog
        dialog.destroy()
コード例 #2
0
ファイル: AbstractVariable.py プロジェクト: lindi2/netzob
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Load a variable from an XML definition.
                Calls its proper heir function for it to create the variable.

                @type xmlRoot: lxml.etree.Element
                @param xmlRoot: the root of the XML definition.
                @type namespace: string
                @param namespace: the namespace (precision) associated to the variable in the XML definition.
                @type version: string
                @param version: the load version.
                @rtype: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable
                @type symbol: netzob.Common.Symbol
                @param symbol: the symbol in which the current variable is placed, used by relation variable.
                @return: a variable constructed from this XML definition.
        """
        if version == "0.1":
            logging.debug("[ AbstractVariable: loadFromXML:")
            # Data Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:DataVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.DataVariable import DataVariable
                return DataVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Aggregate Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:AggregateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AggregateVariable import AggregateVariable
                return AggregateVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Alternate Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:AlternateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AlternateVariable import AlternateVariable
                return AlternateVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Repeat Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:RepeatVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.RepeatVariable import RepeatVariable
                return RepeatVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Direct Relation Variable
            #elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:DirectRelationVariable":
            #    from netzob.Common.MMSTD.Dictionary.Variables.DirectRelationVariable import DirectRelationVariable
            #    return DirectRelationVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Computed Relation Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:ComputedRelationVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.ComputedRelationVariable import ComputedRelationVariable
                return ComputedRelationVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            else:
                logging.debug("xmlRoot.get(...) returns {0} which does not correspond to a true variable class.").format(xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract"))
            logging.debug("AbstractVariable: loadFromXML ]")
        else:
            logging.debug("Version != 0.1")
コード例 #3
0
ファイル: Variable.py プロジェクト: KurSh/netzob
    def loadFromXML(xmlRoot, namespace, version):
        if version == "0.1":
            # IPv4 Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:IPv4Variable":
                from netzob.Common.MMSTD.Dictionary.Variables.IPv4Variable import IPv4Variable
                return IPv4Variable.loadFromXML(xmlRoot, namespace, version)

            # Word Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:WordVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.WordVariable import WordVariable
                return WordVariable.loadFromXML(xmlRoot, namespace, version)

            # DecimalWord Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:DecimalWordVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.DecimalWordVariable import DecimalWordVariable
                return DecimalWordVariable.loadFromXML(xmlRoot, namespace, version)

            # Binary Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:BinaryVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.BinaryVariable import BinaryVariable
                return BinaryVariable.loadFromXML(xmlRoot, namespace, version)

            # Hex Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:HexVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.HexVariable import HexVariable
                return HexVariable.loadFromXML(xmlRoot, namespace, version)

            # Aggregate Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:AggregateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AggregateVariable import AggregateVariable
                return AggregateVariable.loadFromXML(xmlRoot, namespace, version)

            # Alternate Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:AlternateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AlternateVariable import AlternateVariable
                return AlternateVariable.loadFromXML(xmlRoot, namespace, version)

            # Referenced Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:ReferencedVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.ReferencedVariable import ReferencedVariable
                return ReferencedVariable.loadFromXML(xmlRoot, namespace, version)
コード例 #4
0
    def loadFromXML(xmlRoot, namespace, version, symbol):
        """loadFromXML:
                Load a variable from an XML definition.
                Calls its proper heir function for it to create the variable.

                @type xmlRoot: lxml.etree.Element
                @param xmlRoot: the root of the XML definition.
                @type namespace: string
                @param namespace: the namespace (precision) associated to the variable in the XML definition.
                @type version: string
                @param version: the load version.
                @rtype: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable
                @type symbol: netzob.Common.Symbol
                @param symbol: the symbol in which the current variable is placed, used by relation variable.
                @return: a variable constructed from this XML definition.
        """
        if version == "0.1":
            logging.debug("[ AbstractVariable: loadFromXML:")
            # Data Variable
            if xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                           "abstract") == "netzob:DataVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.DataVariable import DataVariable
                return DataVariable.loadFromXML(xmlRoot, namespace, version,
                                                symbol)

            # Aggregate Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == "netzob:AggregateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AggregateVariable import AggregateVariable
                return AggregateVariable.loadFromXML(xmlRoot, namespace,
                                                     version, symbol)

            # Alternate Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == "netzob:AlternateVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.AlternateVariable import AlternateVariable
                return AlternateVariable.loadFromXML(xmlRoot, namespace,
                                                     version, symbol)

            # Repeat Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == "netzob:RepeatVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.RepeatVariable import RepeatVariable
                return RepeatVariable.loadFromXML(xmlRoot, namespace, version,
                                                  symbol)

            # Direct Relation Variable
            #elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type", "abstract") == "netzob:DirectRelationVariable":
            #    from netzob.Common.MMSTD.Dictionary.Variables.DirectRelationVariable import DirectRelationVariable
            #    return DirectRelationVariable.loadFromXML(xmlRoot, namespace, version, symbol)

            # Computed Relation Variable
            elif xmlRoot.get("{http://www.w3.org/2001/XMLSchema-instance}type",
                             "abstract") == "netzob:ComputedRelationVariable":
                from netzob.Common.MMSTD.Dictionary.Variables.ComputedRelationVariable import ComputedRelationVariable
                return ComputedRelationVariable.loadFromXML(
                    xmlRoot, namespace, version, symbol)

            else:
                logging.debug(
                    "xmlRoot.get(...) returns {0} which does not correspond to a true variable class."
                ).format(
                    xmlRoot.get(
                        "{http://www.w3.org/2001/XMLSchema-instance}type",
                        "abstract"))
            logging.debug("AbstractVariable: loadFromXML ]")
        else:
            logging.debug("Version != 0.1")
コード例 #5
0
    def validateChanges(self, widget):
        """validateChanges:
                Validate the changes that a user has done on a variable.
                Called by a click on the apply button.

                @type widget: Gtk.widget
                @param widget: the widget which calls this function.
        """

        dialog = self.view.getWidg("dialog")
        if self.editOverCreate:
            anid = self.variable.getID()
        else:
            anid = str(uuid.uuid4())

        name = self.view.getWidg("nameEntry").get_text()
        mutable = self.view.getWidg("mutableCheck").get_active()
        learnable = self.view.getWidg("learnableCheck").get_active()
        varTypeIndex = self.view.getWidg("variableTypeCombo").get_active()

        variable = None
        # Aggregate variable
        if varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AggregateVariable.TYPE):
            variable = AggregateVariable(anid, name, mutable, learnable)

        # Alternate Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AlternateVariable.TYPE):
            variable = AlternateVariable(anid, name, mutable, learnable)

        # Repeat Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(RepeatVariable.TYPE):
            minIterations = int(self.view.getWidg("minSpin").get_text())
            maxIterations = int(self.view.getWidg("maxSpin").get_text())
            variable = RepeatVariable(anid, name, mutable, learnable, None, minIterations, maxIterations)

        # Data Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(DataVariable.TYPE):
            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractType.makeType(VariableTreeController.TYPE_INDEX_LIST[self.view.getWidg("typeCombo").get_active()], sized, minChars, maxChars, delimiter)
            originalValue = vtype.str2bin(self.view.getWidg("valueEntry").get_text())
            variable = DataVariable(anid, name, mutable, learnable, vtype, originalValue)

#===============================================================================
#        # Direct Relation Variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            # We find the variable by its ID.
#            pointedID = str(self.view.getWidg("IDEntry").get_text())
#
#            variable = DirectRelationVariable(anid, name, mutable, learnable, pointedID, self.treeController.symbol)
#===============================================================================

        # Computed Relation Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(ComputedRelationVariable.TYPE):

            # We find the variable by its ID.
            pointedID = str(self.view.getWidg("IDEntry").get_text())

            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()

            factor = float(self.view.getWidg("factorEntry").get_text())
            offset = float(self.view.getWidg("offsetEntry").get_text())
            vtype = AbstractRelationType.makeType(VariableTreeController.RELATION_TYPE_INDEX_LIST[self.view.getWidg("relationTypeCombo").get_active()], sized, minChars, maxChars, delimiter, factor, offset)
            variable = ComputedRelationVariable(anid, name, mutable, learnable, vtype, pointedID, self.treeController.symbol)

        if variable is not None:
            # This part is for saving and transfering children when transforming a node variable into an other kind of node variable.
            if self.editOverCreate:
                father = None
                if len(self.variable.getFathers()) > 0:
                    father = self.variable.getFathers()[0]
                # We transform a node variable into a node variable.
                if (self.variable.getVariableType() == AggregateVariable.TYPE or self.variable.getVariableType() == AlternateVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    children = self.variable.getChildren()
                    self.variable = variable
                    for child in children:
                        self.variable.addChild(child)

                # We transform a repeat variable into a node variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We transform a repeat variable into a repeat variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == RepeatVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.setChild(child)

                # We do not manage/save children.
                else:
                    self.variable = variable
                if father is not None:
                    self.variable.addFather(father)
                self.treeController.editVariable(self.variable)

            else:
                if self.variable.getVariableType() == RepeatVariable.TYPE:
                    self.variable.setChild(variable)
                else:
                    self.variable.addChild(variable)
                self.treeController.registerVariable(self.rootEntry, variable)
        dialog.destroy()