Example #1
0
def executeExperiment(args):
    """
    Executes the analysis for an application.
    The analysis depends of the scenario the user as defined in configuration.
    """
    logger = Logger.getLogger(__name__)
    (listOfAPKs, iEmulator, mainConfiguration, analysisConfiguration,
     reportingConfiguration) = args
    reporter = Reporter(reportingConfiguration)

    logger.warning("Execute experiment with scenario: [{0}]".format(', '.join(
        analysisConfiguration.scenario)))

    # When you play with emulators, you can modify this variable to sleep a bit longer.
    # Default has to be >1
    SLEEP_FACTOR = 1
    if mainConfiguration.typeOfDevice == 'emulated':
        SLEEP_FACTOR = 5
    if SLEEP_FACTOR < 1:
        raise Exception("SLEEP_FACTOR variable cannot be less than 1.")

    while True:
        # Pick one APK to analyse
        try:
            apkToAnalyze = listOfAPKs.get()  # 0xFFFF
            logger.info("APK to analyze : {0}".format(apkToAnalyze))

            # Build the identifier experiment
            idXp = Analysis.generateIdXp([apkToAnalyze])

            # Build the emulator name
            if mainConfiguration.typeOfDevice == 'emulated':
                emulatorName = "Emulator_{0}".format(iEmulator)
            else:
                emulatorName = mainConfiguration.deviceId

            # Create a new report for this analysis
            logger.debug("Create report.")
            Analysis.createReport(reporter, idXp, emulatorName, "unknown",
                                  apkToAnalyze, "automatic",
                                  mainConfiguration.name)

            # Execute static analysis
            logger.debug("Executing the Static Analysis")
            staticAnalysis = StaticAnalysis(apkToAnalyze, mainConfiguration,
                                            reporter, idXp)
            staticAnalysis.execute()
            logger.info(staticAnalysis)

            # Create the emulator
            if mainConfiguration.typeOfDevice == 'emulated':
                device = Analysis.createEmulator(iEmulator,
                                                 emulatorName,
                                                 mainConfiguration,
                                                 analysisType="automatic")
            else:
                device = Analysis.createDevice(
                    iEmulator,
                    mainConfiguration.deviceId,
                    mainConfiguration,
                    analysisConfiguration.backupDirectory,
                    analysisType="automatic")

            if device is None:
                raise Exception(
                    "Something has prevented the creation of an device.")

            # Starts the device
            Analysis.reportEvent(reporter, idXp, emulatorName, "Start device")
            device.start()

            # Install et execute preparation applications
            if mainConfiguration.typeOfDevice == 'emulated':
                for prepareAPK in analysisConfiguration.prepareAPKs:
                    Analysis.reportEvent(reporter, idXp, emulatorName,
                                         "Install preparation APK", prepareAPK)
                    device.installAPK(prepareAPK)

                # Execute preparation applications
                for prepareAPK in analysisConfiguration.prepareAPKs:
                    Analysis.reportEvent(reporter, idXp, emulatorName,
                                         "Start activity",
                                         os.path.basename(prepareAPK)[:-4])
                    device.startActivity(os.path.basename(prepareAPK)[:-4])

            # Writes the experiment configuration on the device
            Analysis.reportEvent(reporter, idXp, emulatorName,
                                 "Write configuration file")
            Analysis.writeConfigurationOnEmulator(device, idXp,
                                                  reportingConfiguration)

            if mainConfiguration.typeOfDevice == 'emulated':
                sleepDuration = 30
                logger.debug(
                    "Waiting {0} seconds for the device to prepare...".format(
                        sleepDuration))
                time.sleep(sleepDuration)

            # Install the targeted application
            Analysis.reportEvent(reporter, idXp, emulatorName,
                                 "Install target APK", apkToAnalyze)
            device.installAPK(apkToAnalyze)
            time.sleep(5 * SLEEP_FACTOR / 5)

            # We then follow the scenario user has filled in configuration
            for order in analysisConfiguration.scenario:
                if "execute" == order:
                    Analysis.reportEvent(reporter, idXp, emulatorName,
                                         "Launching main activity",
                                         staticAnalysis.mainActivity)
                    logger.info("Starting main activity: {0}".format(
                        staticAnalysis.mainActivity))
                    device.startActivityFromPackage(
                        staticAnalysis.packageName,
                        staticAnalysis.mainActivity)
                    time.sleep(5 * SLEEP_FACTOR)
                elif "stimulate" == order:
                    Analysis.reportEvent(reporter, idXp, emulatorName,
                                         "Stimulating package with monkey",
                                         staticAnalysis.packageName)
                    logger.info("Stimulating with monkey: {0}".format(
                        staticAnalysis.packageName))
                    device.stimulateWithMonkey(staticAnalysis.packageName)
                    time.sleep(10 * SLEEP_FACTOR)
                elif "externalStimulation" == order:
                    Analysis.reportEvent(
                        reporter, idXp, emulatorName,
                        "Stimulating phone with external conditions")
                    logger.info(
                        "Stimulating phone with external conditions...")
                    externalStimulation = TelnetEmulation(
                        reporter, idXp, device)
                    externalStimulation.start()
                    time.sleep(10 * SLEEP_FACTOR)
                elif "reboot" == order:
                    Analysis.reportEvent(reporter, idXp, emulatorName,
                                         "Rebooting device")
                    logger.info("Rebooting device.")
                    device.reboot()
                    time.sleep(5 * SLEEP_FACTOR)

            Analysis.reportEvent(reporter, idXp, emulatorName,
                                 "Analysis has finished", apkToAnalyze)

            logger.info("Analysis of APK {0} has been finished.")
            Analysis.reportEvent(reporter, idXp, emulatorName,
                                 "Emulator closed")
            device.stop()

        except KeyboardInterrupt:
            logger.debug("Keyboard interrupt caught\n")
            # Try to stop device if necessary
            if device is not None:
                device.stop()
            break
        except Exception, e:
            logger.error(
                "Exception while executing an experiment : {0}".format(e))
            tb = traceback.format_exc()
            logger.error(tb)
            try:
                device.stop()
            except Exception:
                logger.error("Cannot stop the AVD, quitting experience.")
                break
Example #2
0
 def __init__(self, mainConfiguration, reportingConfiguration):
     self._logger = Logger.getLogger(__name__)
     self.mainConfiguration = mainConfiguration
     self.reportingConfiguration = reportingConfiguration
     self.reporter = Reporter(self.reportingConfiguration)
Example #3
0
def executeExperiment(args):
    """
    Executes the analysis for an application.
    The analysis depends of the scenario the user as defined in configuration.
    """
    logger = Logger.getLogger(__name__)
    (listOfAPKs, iEmulator, mainConfiguration, analysisConfiguration,
     reportingConfiguration) = args
    reporter = Reporter(reportingConfiguration)

    logger.warning("Execute experiment with scenario: [{0}]".format(', '.join(
        analysisConfiguration.scenario)))

    while True:
        # Pick one APK to analyse
        try:
            apkToAnalyze = listOfAPKs.get()  # 0xFFFF
            logger.info("APK to analyze : {0}".format(apkToAnalyze))

            # Build the identifier experiment
            idXp = Analysis.generateIdXp([apkToAnalyze])

            # Build the emulator name
            emulatorName = "Emulator_{0}".format(iEmulator)

            # Create a new report for this analysis
            logger.debug("Create report.")
            Analysis.createReport(reporter, idXp, emulatorName, "unknown",
                                  apkToAnalyze, "automatic", None)

            # Execute static analysis
            logger.debug("Executing the Static Analysis")
            staticAnalysis = StaticAnalysis(apkToAnalyze, mainConfiguration,
                                            reporter, idXp)
            staticAnalysis.execute()
            logger.info(staticAnalysis)

            # Create the emulator
            emulator = Analysis.createEmulator(iEmulator, emulatorName,
                                               mainConfiguration)

            if emulator is None:
                raise Exception(
                    "Something has prevented the creation of an emulator.")

            # Starts the emulator
            Analysis.reportEvent(reporter, idXp, "Emulator", "start")
            emulator.start()

            # Install et execute preparation applications
            for prepareAPK in analysisConfiguration.prepareAPKs:
                Analysis.reportEvent(reporter, idXp, "Emulator", "installAPK",
                                     prepareAPK)
                emulator.installAPK(prepareAPK)

            # Execute preparation applications
            for prepareAPK in analysisConfiguration.prepareAPKs:
                Analysis.reportEvent(reporter, idXp, "Emulator",
                                     "startActivity",
                                     os.path.basename(prepareAPK)[:-4])
                emulator.startActivity(os.path.basename(prepareAPK)[:-4])

            # Writes the experiment configuration on the emulator
            Analysis.reportEvent(reporter, idXp, "Emulator",
                                 "writeConfiguration")
            Analysis.writeConfigurationOnEmulator(emulator, idXp,
                                                  reportingConfiguration)

            sleepDuration = 30
            logger.debug(
                "Waiting {0} seconds for the emulator to prepare...".format(
                    sleepDuration))
            time.sleep(sleepDuration)

            # Install the targeted application
            Analysis.reportEvent(reporter, idXp, "Emulator", "installAPK",
                                 apkToAnalyze)
            emulator.installAPK(apkToAnalyze)
            time.sleep(5)

            # We then follow the scenario user has filled in configuration
            for order in analysisConfiguration.scenario:
                if "execute" == order:
                    Analysis.reportEvent(reporter, idXp, "Emulator",
                                         "Launching main activity",
                                         staticAnalysis.mainActivity)
                    logger.info("Starting main activity: {0}".format(
                        staticAnalysis.mainActivity))
                    emulator.startActivityFromPackage(
                        staticAnalysis.packageName,
                        staticAnalysis.mainActivity)
                    time.sleep(60)
                elif "stimulate" == order:
                    Analysis.reportEvent(reporter, idXp, "Emulator",
                                         "Stimulating package with monkey",
                                         staticAnalysis.packageName)
                    logger.info("Stimulating with monkey: {0}".format(
                        staticAnalysis.packageName))
                    emulator.stimulateWithMonkey(staticAnalysis.packageName)
                    time.sleep(80)
                elif "externalStimulation" == order:
                    Analysis.reportEvent(
                        reporter, idXp, "Emulator",
                        "Stimulating phone with external conditions")
                    logger.info(
                        "Stimulating phone with external conditions...")
                    externalStimulation = TelnetEmulation(
                        reporter, idXp, emulator, iEmulator)
                    externalStimulation.start()
                    time.sleep(30)
                elif "reboot" == order:
                    Analysis.reportEvent(reporter, idXp, "Emulator",
                                         "Rebooting emulator")
                    logger.info("Rebooting emulator.")
                    emulator.rebootAVD()
                    time.sleep(100)

            Analysis.reportEvent(reporter, idXp, "Emulator",
                                 "Finished analysis", apkToAnalyze)

            logger.info("Analysis of APK {0} has been finished.")
            Analysis.reportEvent(reporter, idXp, "Emulator", "closed")
            emulator.stopAVD()

        except KeyboardInterrupt:
            pass
        except Exception, e:
            logger.error(
                "Exception while executing an experiment : {0}".format(e))
            tb = traceback.format_exc()
            logger.error(tb)
            try:
                emulator.stopAVD()
            except Exception:
                logger.error("Cannot stop the AVD.")