Beispiel #1
0
def test_temp_file_with_name():
    ''' ensures we can get a temp file with a name '''
    testPath = None
    with temporaryFilePath(fileName='myfile') as t:
        testPath = t
        assert not os.path.exists(t)
        assert t.endswith('myfile')
        with open(t, 'w') as f:
            f.write("Hey")

    assert not os.path.exists(testPath)
Beispiel #2
0
def test_temp_file_path_with_a_file():
    ''' ensures temporaryFilePaths() can be created and delete appropriately with a file '''
    testPath = None
    with temporaryFilePath() as t:
        testPath = str(t)
        assert not os.path.exists(t)
        with open(t, 'w') as f:
            f.write('bleh')
        assert os.path.isfile(t)

    assert not os.path.exists(testPath)
Beispiel #3
0
    def getAnalysis(self, applicationName, rowUid, useCache=True):
        ''' internal function called to get the Analysis object for the given rowUid '''
        if useCache:
            dataBlob = self.getApplicationCell(applicationName, rowUid,
                                               'CrashDumpAnalysis')
            if dataBlob:
                logger.debug("Returning from cache, analysis: %s / %s" %
                             (applicationName, rowUid))
                try:
                    return pickle.loads(dataBlob)
                except Exception as ex:
                    logger.error("Failed to de-serialize pickle data: %s" %
                                 str(ex))

        logger.info("Attempting to generate Analysis for: %s / %s" %
                    (applicationName, rowUid))

        crashDumpBinary = self.getApplicationCell(applicationName, rowUid,
                                                  'CrashDumpFile')
        if not crashDumpBinary:
            logger.error(
                "Crash dump file was not available with the given uid")
            flask.abort(404)

        operatingSystem = self.getApplicationCell(applicationName, rowUid,
                                                  'OperatingSystem')
        if not operatingSystem:
            logger.error(
                "Operating system was not available with the given uid... this should not be possible!"
            )
            flask.abort(404)

        with temporaryFilePath() as crashDumpBinaryFilePath:
            with open(crashDumpBinaryFilePath, 'wb') as f:
                f.write(crashDumpBinary)

            if operatingSystem == SupportedOperatingSystems.WINDOWS.value:
                debugger = WinDbg(crashDumpBinaryFilePath,
                                  WINDOWS_SYMBOL_STORE)
            else:
                logger.error("Unsupported OS is somehow in the database")
                logger.abort(500)

            analysis = debugger.getAnalysis()
        analysisAsPickledData = pickle.dumps(analysis)
        if not self.setApplicationCell(applicationName, rowUid,
                                       'CrashDumpAnalysis',
                                       analysisAsPickledData):
            logger.warning(
                "Failed to save off crash dump analysis pickle data.. uid=%s" %
                rowUid)
            flask.abort(500)

        return analysis
Beispiel #4
0
def test_temp_file_path_with_a_directory():
    ''' ensures temporaryFilePaths() can be created and delete appropriately with a folder '''
    testPath = None
    with temporaryFilePath() as t:
        testPath = str(t)
        assert not os.path.exists(t)
        os.mkdir(t)
        with open(os.path.join(t, 'myfile'), 'w') as f:
            f.write('bleh')
        assert os.path.isdir(t)

    assert not os.path.exists(testPath)
Beispiel #5
0
def test_zip_directory_to_bytes_io():
    ''' ensures we can zip a directory to io.BytesIO '''
    with temporaryFilePath() as tempFile:
        # make a directory
        os.mkdir(tempFile)

        # make a file in there
        with open(os.path.join(tempFile, 'tmp'), 'w') as f:
            f.write('bleh')

        binaryData = zipDirectoryToBytesIo(tempFile).read()
        assert len(binaryData) > 10
        assert binaryData[0] == 0x50  # all zip files start with 0x50
Beispiel #6
0
def test_temp_file_with_name_delete_off():
    ''' ensures we can get a temp file with a name and delete being False '''
    testPath = None
    with temporaryFilePath(fileName='myfile', delete=False) as t:
        testPath = t
        assert not os.path.exists(t)
        assert t.endswith('myfile')
        with open(t, 'w') as f:
            f.write("Hey")

    assert os.path.isfile(testPath)

    os.remove(testPath)
    os.rmdir(os.path.dirname(testPath))
Beispiel #7
0
    def addFromAddRequest(self, request):
        ''' called by the flask app to add something for the given request to addHandler
        Note that this will return the status that will be returned by addHandler() '''
        def failAnd401(msg):
            ''' helper to at this moment and log to the logger with the given message '''
            logger.error(msg)
            flask.abort(flask.Response(msg, 401))

        # verify valid request 1st.

        # need to have the operating system set
        operatingSystem = request.form.get("OperatingSystem")
        if not operatingSystem:
            failAnd401("request was missing required field: OperatingSystem")

        if not SupportedOperatingSystems.isValidValue(operatingSystem):
            failAnd401(
                "request's OperatingSystem is not supported: %s. Valid options: %s"
                %
                (operatingSystem, str(SupportedOperatingSystems.getValues())))

        # need to have the application set
        application = request.form.get("Application")
        if not application:
            failAnd401("request was missing required field: Application")

        # need at least one of: SymbolsFile, ExecutableFile, CrashDumpFile
        symbolsFile = request.files.get("SymbolsFile")
        executableFile = request.files.get("ExecutableFile")
        crashDumpFile = request.files.get("CrashDumpFile")

        if not (symbolsFile or executableFile or crashDumpFile):
            failAnd401(
                "request needed to include at least one of the following: SymbolsFile, ExecutableFile, CrashDumpFile"
            )

        # if we made it here, the request is valid

        # ensure we have a table for this application
        if not self.applicationExists(application):
            if not self.applicationAdd(application):
                failAnd401("unable to add application with name: %s" %
                           application)

        applicationTableName = self.getApplicationTableName(application)

        # get binary data
        symbolsFileBinary = symbolsFile.read() if symbolsFile else None
        executableFileBinary = executableFile.read(
        ) if executableFile else None
        crashDumpFileBinary = crashDumpFile.read() if crashDumpFile else None

        # get binary file names
        symbolsFileName = symbolsFile.filename if symbolsFile else None
        executableFileName = executableFile.filename if executableFile else None
        crashDumpFileName = crashDumpFile.filename if crashDumpFile else None

        # add objects to symbol store
        if operatingSystem == SupportedOperatingSystems.WINDOWS.value:
            if symbolsFileBinary:
                # additions must have original name (to work in symbol store)
                with temporaryFilePath(fileName=symbolsFileName) as temp:
                    with open(temp, 'wb') as f:
                        f.write(symbolsFileBinary)

                    try:
                        self.windowsSymbolStore.add(temp, compressed=True)
                    except Exception as ex:
                        failAnd401("Failed to add symbols file to store: %s" %
                                   str(ex))

            if executableFileBinary:
                # additions must have original name (to work in symbol store)
                with temporaryFilePath(fileName=executableFileName) as temp:
                    with open(temp, 'wb') as f:
                        f.write(executableFileBinary)

                    try:
                        self.windowsSymbolStore.add(temp, compressed=True)
                    except Exception as ex:
                        failAnd401(
                            "Failed to add executable file to store: %s" %
                            str(ex))

        # add to database
        uid = getUniqueId()
        if not self.database.addRow(
                applicationTableName,
            {
                'UID': uid,
                'Timestamp': str(datetime.datetime.now()),
                'UploaderIP': request.remote_addr,
                'OperatingSystem': operatingSystem,
                'Tag': request.form.get('Tag'),
                'ApplicationVersion': request.form.get('ApplicationVersion'),
                'SymbolsFile': symbolsFileBinary,
                'SymbolsFileName': symbolsFileName,
                'ExecutableFile': executableFileBinary,
                'ExecutableFileName': executableFileName,
                'CrashDumpFile': crashDumpFileBinary,
                'CrashDumpFileName': crashDumpFileName,
                # CrashDumpAnalysis is not given here, it can be generated later.
            }):
            failAnd401("Unable to add to database")

        # success!
        return "Successfully added! UID: %s" % uid