Example #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()))
Example #2
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()))