Beispiel #1
0
 def makeType(typeString, sized, minSize=0, maxSize=0, delimiter=None):
     _type = None
     from netzob.Common.MMSTD.Dictionary.DataTypes.BinaryType import BinaryType
     from netzob.Common.MMSTD.Dictionary.DataTypes.DecimalWordType import DecimalWordType
     from netzob.Common.MMSTD.Dictionary.DataTypes.HexWordType import HexWordType
     from netzob.Common.MMSTD.Dictionary.DataTypes.IPv4WordType import IPv4WordType
     from netzob.Common.MMSTD.Dictionary.DataTypes.MACWordType import MACWordType
     from netzob.Common.MMSTD.Dictionary.DataTypes.IntegerType import IntegerType
     from netzob.Common.MMSTD.Dictionary.DataTypes.WordType import WordType
     if typeString == BinaryType.TYPE:
         _type = BinaryType(sized, minSize, maxSize, delimiter)
     elif typeString == DecimalWordType.TYPE:
         _type = DecimalWordType(sized, minSize, maxSize, delimiter)
     elif typeString == HexWordType.TYPE:
         _type = HexWordType(sized, minSize, maxSize, delimiter)
     elif typeString == IntegerType.TYPE:
         _type = IntegerType(sized, minSize, maxSize, delimiter)
     elif typeString == IPv4WordType.TYPE:
         _type = IPv4WordType(sized, minSize, maxSize, delimiter)
     elif typeString == MACWordType.TYPE:
         _type = MACWordType(sized, minSize, maxSize, delimiter)
     elif typeString == WordType.TYPE:
         _type = WordType(sized, minSize, maxSize, delimiter)
     else:
         logging.error("Wrong type specified for this variable.")
     return _type
Beispiel #2
0
 def setType(self, _type):
     if type is not None:
         self.type = _type
     else:
         # Default type is Binary.
         self.log.info("Variable {0} (Data): type undefined.".format(
             self.getName()))
         self.type = BinaryType()
Beispiel #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()))
Beispiel #4
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()))