Beispiel #1
0
 def getRoot(self):
     # We create an aggregate of all the fields
     rootSymbol = AggregateVariable(self.getID(), self.getName(), False,
                                    False, None)
     for field in self.getExtendedFields():
         variable = field.getVariable()
         rootSymbol.addChild(variable)
     return rootSymbol
Beispiel #2
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()))
Beispiel #3
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()