Esempio n. 1
0
    def learn(self, child, readingToken):
        """learn:
                The aggregate variable learns the given value: it tries to add ONE mutable child before child in order to comply with the given value.
                We do not manage the learning of several children because we think that it is not the usecase and it inserts many errors.
                This learning can only extend the variable, not remove some children of it. If you want to potentially not take care of some variables, you can include them in 0-1 repeat variable.

                @type child: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable
                @param child: the child we expected to find while reading the given value.
                @type readingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableReadingToken.VariableReadingToken
                @param readingToken: a token which contains all critical information on this access.
        """
        self.log.debug("- [ {0}: learn.".format(self.toString()))
        savedIndex = readingToken.getIndex()

        childPosition = self.indexOfChild(child)
        repeatVariable = RepeatVariable(str(uuid.uuid4()), "Learned Option Variable", False, True, self, 0, 1)
        # We will insert the new child under a 0-1 repeat variable to potentially not take care of it, just before the position of the problematic child.
        self.insertChild(childPosition, repeatVariable)
        valueToBeRead = readingToken.getValue()[readingToken.getIndex():]
        for index in len(valueToBeRead):
            tmpValue = valueToBeRead[:index]
            tmpChild = DataVariable(str(uuid.uuid4()), "Learned Inserted Variable", True, True, BinaryType(True, len(tmpValue), len(tmpValue)), tmpValue)
            repeatVariable.add(tmpChild)

            # We read this new variable in a learning context.
            self.setLearning(True)
            self.read(readingToken)
            # If this read access works, we learn the proper variable.
            if readingToken.isOk():
                break
            else:
                # We remove the just added child.
                repeatVariable.removeChild(tmpChild)

        # We did not found, so we restore and give up.
        if not readingToken.isOk():
            self.removeChild(repeatVariable)
            readingToken.setIndex(savedIndex)
        else:
            # The value of the variable is simply the value we 'ate'.
            self.currentValue = readingToken.getValue()[savedIndex:readingToken.getIndex()]

        self.log.debug("Variable {0}: {1}. ] -".format(self.getName(), readingToken.toString()))
Esempio n. 2
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")
Esempio n. 3
0
    def learn(self, child, readingToken):
        """learn:
                The aggregate variable learns the given value: it tries to add ONE mutable child before child in order to comply with the given value.
                We do not manage the learning of several children because we think that it is not the usecase and it inserts many errors.
                This learning can only extend the variable, not remove some children of it. If you want to potentially not take care of some variables, you can include them in 0-1 repeat variable.

                @type child: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable
                @param child: the child we expected to find while reading the given value.
                @type readingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableReadingToken.VariableReadingToken
                @param readingToken: a token which contains all critical information on this access.
        """
        self.log.debug("- [ {0}: learn.".format(self.toString()))
        savedIndex = readingToken.getIndex()

        childPosition = self.indexOfChild(child)
        repeatVariable = RepeatVariable(str(uuid.uuid4()),
                                        "Learned Option Variable", False, True,
                                        self, 0, 1)
        # We will insert the new child under a 0-1 repeat variable to potentially not take care of it, just before the position of the problematic child.
        self.insertChild(childPosition, repeatVariable)
        valueToBeRead = readingToken.getValue()[readingToken.getIndex():]
        for index in len(valueToBeRead):
            tmpValue = valueToBeRead[:index]
            tmpChild = DataVariable(
                str(uuid.uuid4()), "Learned Inserted Variable", True, True,
                BinaryType(True, len(tmpValue), len(tmpValue)), tmpValue)
            repeatVariable.add(tmpChild)

            # We read this new variable in a learning context.
            self.setLearning(True)
            self.read(readingToken)
            # If this read access works, we learn the proper variable.
            if readingToken.isOk():
                break
            else:
                # We remove the just added child.
                repeatVariable.removeChild(tmpChild)

        # We did not found, so we restore and give up.
        if not readingToken.isOk():
            self.removeChild(repeatVariable)
            readingToken.setIndex(savedIndex)
        else:
            # The value of the variable is simply the value we 'ate'.
            self.currentValue = readingToken.getValue(
            )[savedIndex:readingToken.getIndex()]

        self.log.debug("Variable {0}: {1}. ] -".format(
            self.getName(), readingToken.toString()))
Esempio n. 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")
Esempio n. 5
0
    def learn(self, child, readingToken):
        """learn:
                The alternate variable learns the given value and adds it at the end of its children.

                @type child: netzob.Common.MMSTD.Dictionary.Variable.AbstractVariable.AbstractVariable
                @param child: the child we expected to find while reading the given value.
                @type readingToken: netzob.Common.MMSTD.Dictionary.VariableProcessingToken.VariableReadingToken.VariableReadingToken
                @param readingToken: a token which contains all critical information on this access.
        """
        self.log.debug("- [ {0}: learn.".format(self.toString()))

        dictOfValues = dict()
        savedIndex = readingToken.getIndex()
        savedFather = self.getFathers()[0]  # TODO: not accurate. But yet  we can only have one father
        selfPosition = savedFather.indexOfChild(self)

        # We create a fake father for this alternate.
        if self.getFathers()[0].getType() == AggregateVariable.TYPE:
            fakeFather = AggregateVariable(str(uuid.uuid4()), "Fake father", False, False)
            # We add this element and its right brother as child of the fake father in order to pursue the read access from where we are.
            fakeFather.addChild(self)
            for rightBrother in self.getFathers()[0].getChildren()[selfPosition:]:
                fakeFather.addChild(rightBrother)
        elif self.getFathers()[0].getType() == RepeatVariable.TYPE:
            (minIterations, maxIterations) = self.getFathers()[0].getNumberIterations()
            # Some iterations of this treatment could have been made before. The fake father should not make more iterations than it remains for the real father.
            minIterations = max(0, minIterations - self.getFathers()[0].getCurrentIteration())
            maxIterations = max(0, maxIterations - self.getFathers()[0].getCurrentIteration())
            fakeFather = RepeatVariable(str(uuid.uuid4()), "Fake father", False, False, self, minIterations, maxIterations)
        else:
            self.log.error("The father is neither an aggregate nor a repeat variable.")

        # We execute the treatment on the fake father.
        valueToBeRead = readingToken.getValue()[readingToken.getIndex():]
        for index in len(valueToBeRead):
            # We search if, by shifting the position of actual variable, we could read the given value.
            tmpValue = valueToBeRead[:index]
            tmpChild = DataVariable(str(uuid.uuid4()), "Learned Inserted Variable", True, True, BinaryType(True, len(tmpValue), len(tmpValue)), tmpValue)
            # We add the new variable at the end, in order to minimize its impact.
            self.add(tmpChild)

            # We read this new variable from the father in a learning context.
            self.setLearning(True)
            fakeFather.read(readingToken)
            # If this read access works, we learn the variable.
            if readingToken.isOk():
                break
            else:
                # We remove the just added child.
                self.removeChild(tmpChild)

        self.removefather(fakeFather)
        # We restore the action induced by the fake father.
        readingToken.setIndex(savedIndex)
        vocabulary = readingToken.getVocabulary()
        for key, val in dictOfValues.iteritems():
            child = vocabulary.getVariableByID(key)
            # We restore the current values.
            child.setCurrentValue(val)
            # We restore the cached values.
            child.restore(readingToken)

        if readingToken.isOk():
            # We continue the treatment. The real father's treatment will pursue.
            self.read(readingToken)

        self.log.debug("Variable {0}: {1}. ] -".format(self.getName(), readingToken.toString()))
Esempio n. 6
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()