Example #1
0
    def execute(self, inputParams, outputFile):
        inputData = inputParams.data

        crcSize = inputParams.getCRCSize()
        if crcSize is None:
            _LOGGER.error(
                "Unable to determine CRC size: pass poly or crcsize as cmd argument"
            )
            return

        if inputData.crcSize != crcSize:
            ## deduced CRC size differs from input crc
            raise ValueError(
                "inconsistent crc size [%s] with input data crc[%s]" %
                (crcSize, inputData.crcSize))

        inputMasks = InputMaskList(inputData)
        if inputMasks.empty():
            _LOGGER.error("invalid case -- no data")
            return False

        _LOGGER.info("crc size: %s" % crcSize)

        revOrd = inputParams.isReverseOrder()
        if revOrd:
            inputMasks.reverseOrder()
        refBits = inputParams.isReflectBits()
        if refBits:
            inputMasks.reflectBits()

        ## List[ (NumberMask, NumberMask) ]
        inputList = inputMasks.getInputMasks()

        polyListStart, polyListStop = inputParams.getPolySearchRange()
        polyListSize = polyListStop - polyListStart + 1

        initListStart, initListStop = inputParams.getInitRegSearchRange()
        initListSize = initListStop - initListStart + 1

        xorListStart, xorListStop = inputParams.getXorValSearchRange()
        xorListSize = xorListStop - xorListStart + 1

        subSpaceSize = initListSize * xorListSize
        spaceSize = polyListSize * subSpaceSize
        _LOGGER.info("search space size: %s %s %s %s", spaceSize, polyListSize,
                     initListSize, xorListSize)

        _LOGGER.info("poly search range: %s %s" %
                     (polyListStart, polyListStop))
        _LOGGER.info("init search range: %s %s" %
                     (initListStart, initListStop))
        _LOGGER.info(" xor search range: %s %s" % (xorListStart, xorListStop))

        spaceCounter = 0

        crc_forward = self.procFactory.createForwardProcessor(crcSize)
        crc_operator = crc_forward.createDataOperator(crcSize, inputList)

        polyMask = NumberMask(0, crcSize)

        results = Counter()

        for polyNum in xrange(polyListStart, polyListStop + 1):
            polyMask.setNumber(polyNum)

            spaceCounter += subSpaceSize
            if self.progress:
                value = spaceCounter * 100.0 / spaceSize
                flush_percent(value, 4)

            crc_found = crc_operator.verifyRange(polyMask, initListStart,
                                                 initListStop, xorListStart,
                                                 xorListStop)

            for item in crc_found:
                initReg = item[0]
                xorVal = item[1]
                #                     flush_string( "Found CRC - poly: 0x{:X} initVal: 0x{:X} xorVal: 0x{:X}\n".format( polyMask.dataNum, initReg, xorVal ) )
                key = CRCKey(polyMask.dataNum,
                             initReg,
                             xorVal,
                             0,
                             inputData.dataSize,
                             revOrd=revOrd,
                             refBits=refBits)
                results[key] += 1

        _LOGGER.info("\n\nFound total results: %s", len(results))
        if self.progress:
            print_results(results, 1)

        if outputFile is not None:
            write_results(results, 1, outputFile)

        return results
Example #2
0
    def execute( self, inputParams, outputFile ):
        inputData = inputParams.data        # InputData

        crcSize = inputParams.getCRCSize()
        if crcSize is None:
            print "\nUnable to determine CRC size: pass poly or crcsize as cmd argument"
            return None

        if inputData.crcSize != crcSize:
            ## deduced CRC size differs from input crc
            raise ValueError( "inconsistent crc size [%s] with input data crc[%s]" % ( crcSize, inputData.crcSize ) )

        if crcSize < 1:
            raise ValueError( "invalid crc size [%s]" % ( crcSize ) )

        inputMasks = InputMaskList( inputData )
        if inputMasks.empty():
            print "invalid case -- no data"
            return None

        _LOGGER.info( "crc size: %s" % crcSize )

#         revOrd = inputParams.isReverseOrder()
#         if revOrd:
#             inputMasks.reverseOrder()
#         refBits = inputParams.isReflectBits()
#         if refBits:
#             inputMasks.reflectBits()

        inputList = inputMasks.getInputMasks()          # List[ (NumberMask, NumberMask) ]
        numbersLen = len(inputList)

        polyListStart, polyListStop = inputParams.getPolySearchRange()
        polyListSize = polyListStop - polyListStart + 1

        xorListStart, xorListStop = inputParams.getXorValSearchRange()
        xorListSize  = xorListStop - xorListStart + 1

        subSpaceSize = xorListSize
        spaceSize    = numbersLen * polyListSize * subSpaceSize
        _LOGGER.info( "search space size: %s %s %s %s", spaceSize, numbersLen, polyListSize, xorListSize )

        _LOGGER.info( "poly search range: %s %s" % ( polyListStart, polyListStop ) )
        _LOGGER.info( " xor search range: %s %s" % ( xorListStart, xorListStop ) )

        spaceCounter = 0

        crc_forward  = self.procFactory.createForwardProcessor( crcSize )      # CRCProcessor
        crc_backward = self.procFactory.createInvertProcessor( crcSize )       # CRCInvertProcessor

        crc_operator = None
        if numbersLen > 1:
            subInputList = inputList[1:]
            ## CRCDataOperator
            crc_operator = crc_forward.createDataOperator( crcSize, subInputList )

        results = Counter()

        dataSize = inputData.dataSize

        firstDataItem  = inputList[0]
        firstDataMask = firstDataItem[0]
        firstCrcMask  = firstDataItem[1]
        firstCrc      = firstCrcMask.dataNum

#         initSum = 0

        polyMask = NumberMask( 0, crcSize )
        for polyNum in xrange(polyListStart, polyListStop + 1):
            if polyNum == 0x0:
                ## value does not make sense and it's heavily computable
                spaceCounter += numbersLen * subSpaceSize
                continue

            polyMask.setNumber( polyNum )

            spaceCounter += numbersLen * subSpaceSize
            if self.progress:
                value = spaceCounter * 100.0 / spaceSize
                flush_percent( value, 7 )

            ## xorDict: List[ (xor, List[init]) ]
            xorDict = crc_backward.calculateInitRegRange( firstDataMask, firstCrc, polyMask, xorListStart, xorListStop )

            if crc_operator is None:
                ## there is only single data row
                for xorOutPair in xorDict:
                    xorOut      = xorOutPair[0]
                    init_found  = xorOutPair[1]
                    for init_reg in init_found:
                        key = CRCKey( polyNum, init_reg, xorOut, 0, dataSize, revOrd=False, refBits=False )
                        results[ key ] += 1
                continue

#             callsCounter = 0

            for xorOutPair in xorDict:
                xorOut      = xorOutPair[0]
                init_found  = xorOutPair[1]
                crc_forward.setXorOutValue( xorOut )

#                 callsCounter += len( init_found )

                for init_reg in init_found:
                    crc_forward.setInitValue( init_reg )

                    valid = crc_operator.verify( polyMask )
                    if valid:
                        key = CRCKey( polyNum, init_reg, xorOut, 0, dataSize, revOrd=False, refBits=False )
                        results[ key ] += 1

#             print "verify call count:", callsCounter

        _LOGGER.info( "\n\nFound total results: %s", len(results) )

        if self.progress:
            print ""
            print_results( results, 1, True )

        if outputFile is not None:
            write_results( results, 1, outputFile, True )

#         print "inits per item:", initSum, float(initSum) / (polyListSize*xorListSize*numbersLen)

        return results
Example #3
0
    def bruteForce(self, inputParams, searchRange=0):
        inputData = inputParams.data  # InputData

        crcSize = inputParams.getCRCSize()
        if crcSize is None:
            print "\nUnable to determine CRC size: pass poly or crcsize as cmd argument"
            return []

        if inputData.crcSize != crcSize:
            ## deduced CRC size differs from input crc
            raise ValueError(
                "inconsistent crc size [%s] with input data crc[%s]" %
                (crcSize, inputData.crcSize))

        inputMasks = InputMaskList(inputData)
        if inputMasks.empty():
            print "invalid case -- no data"
            return []

        _LOGGER.info("crc size: %s" % crcSize)

        if inputParams.isReverseOrder():
            inputMasks.reverseOrder()
        if inputParams.isReflectBits():
            inputMasks.reflectBits()

        ## List[ (NumberMask, NumberMask) ]
        inputList = inputMasks.getInputMasks()

        polyListStart, polyListStop = inputParams.getPolySearchRange()
        polyListSize = polyListStop - polyListStart + 1

        initListStart, initListStop = inputParams.getInitRegSearchRange()
        initListSize = initListStop - initListStart + 1

        xorListStart, xorListStop = inputParams.getXorValSearchRange()
        xorListSize = xorListStop - xorListStart + 1

        subSpaceSize = initListSize * xorListSize
        spaceSize = polyListSize * subSpaceSize
        _LOGGER.info("search space size: %s %s %s %s", spaceSize, polyListSize,
                     initListSize, xorListSize)

        _LOGGER.info("poly search range: %s %s" %
                     (polyListStart, polyListStop))
        _LOGGER.info("init search range: %s %s" %
                     (initListStart, initListStop))
        _LOGGER.info(" xor search range: %s %s" % (xorListStart, xorListStop))

        crc_forward = self.procFactory.createForwardProcessor(crcSize)
        crc_operator = crc_forward.createDataOperator(crcSize, inputList)

        retList = Counter()

        spaceCounter = 0
        polyMask = NumberMask(0, crcSize)

        for polyNum in xrange(polyListStart, polyListStop + 1):
            polyMask.setNumber(polyNum)

            spaceCounter += subSpaceSize
            if self.progress:
                value = spaceCounter * 100.0 / spaceSize
                flush_percent(value, 4)

            # Counter
            crc_found = crc_operator.calculateRange(polyMask, initListStart,
                                                    initListStop, xorListStart,
                                                    xorListStop)

            for item in crc_found:
                initReg = item[0]
                xorVal = item[1]
                counted = crc_found[item]
                #                 flush_string( "Found CRC - poly: 0x{:X} initVal: 0x{:X} xorVal: 0x{:X}\n".format( polyMask.dataNum, initReg, xorVal ) )
                key = CRCKey(polyMask.dataNum,
                             initReg,
                             xorVal,
                             0,
                             inputData.dataSize,
                             rev=False)
                retList[key] += counted

        return retList