def handle(self, *args, **options):
        if options['all']:
            try:
                reports = Report.objects.order_by('-creation_date')
            except Report.DoesNotExist:
                raise CommandError('No reports found')
        else:
            try:
                reports = Report.objects.filter(pk__in=options['report_id'])
            except Report.DoesNotExist:
                raise CommandError('No reports found')

        count = 1
        for report in reports:
            self.stdout.write(
                self.style.SUCCESS('Start updating report "%s" - %s/%s' %
                                   (report.id, count, len(reports))))
            count += 1
            with tempfile.TemporaryDirectory() as tmp_dir:
                icon_name = '%s_%s.png' % (report.bucket,
                                           report.application.handle)
                apk_name = report.apk_file
                apk_tmp = os.path.join(tmp_dir, apk_name)

                storage_helper = RemoteStorageHelper(report.bucket)

                # Refresh signature
                try:
                    storage_helper.get_file(apk_name, apk_tmp)
                except ResponseError:
                    raise CommandError('Unable to get APK')
                static_analysis = StaticAnalysis(apk_path=apk_tmp)
                source = report.application.source
                icon_phash = static_analysis.get_icon_and_phash(
                    storage_helper, icon_name, source)
                if icon_phash:
                    report.application.icon_path = icon_name
                    report.application.save()
                    self.stdout.write(
                        self.style.SUCCESS('Successfully updated icon'))
                try:
                    report.application.app_uid = static_analysis.get_application_universal_id(
                    )
                except Exception as e:
                    self.style.WARNING(e)
                report.application.icon_phash = icon_phash
                report.application.save()
                if report.application.apk.certificate_set.count() == 0:
                    certificates = static_analysis.get_certificates()
                    for certificate in certificates:
                        c = Certificate(apk=report.application.apk)
                        c.issuer = certificate.issuer
                        c.fingerprint = certificate.fingerprint
                        c.subject = certificate.subject
                        c.serial_number = certificate.serial
                        c.save(force_insert=True)
Exemple #2
0
def recompute_all_reports():
    ev = EventGroup()
    ev.info('Starting the update of all reports', initiator=__name__)

    try:
        reports = Report.objects.order_by('-creation_date')
    except Report.DoesNotExist:
        ev.error('No reports found', initiator=__name__)
        return

    count = 1
    for report in reports:
        ev.info('Start updating report "%s" - %s/%s' %
                (report.id, count, len(reports)),
                initiator=__name__)
        try:
            handle = report.application.handle
        except Exception as e:
            ev.error(e, initiator=__name__)
            continue

        count += 1
        with tempfile.TemporaryDirectory() as tmpdir:
            storage_helper = RemoteStorageHelper(report.bucket)

            # Refresh trackers
            # Download class list file
            static_analysis = StaticAnalysis(None)
            clist_tmp = os.path.join(tmpdir, report.class_list_file)
            try:
                storage_helper.get_file(report.class_list_file, clist_tmp)
            except Exception:
                ev.error('Unable to get clist file', initiator=__name__)
                continue

            trackers = static_analysis.detect_trackers(clist_tmp)
            if report.found_trackers.count() != len(trackers):
                ev.warning('previous: %s - new: %s trackers' %
                           (report.found_trackers.count(), len(trackers)),
                           initiator=__name__)
            report.found_trackers.set(trackers)
            report.save()
            ev.info(
                'Successfully updated trackers list of "{}"'.format(handle),
                initiator=__name__)
    def handle(self, *args, **options):
        if options['all']:
            try:
                reports = Report.objects.order_by('-creation_date')
            except Report.DoesNotExist:
                raise CommandError('No reports found')
        elif options['report_id']:
            try:
                reports = Report.objects.filter(pk__in=options['report_id'])
            except Report.DoesNotExist:
                raise CommandError('No reports found')
        else:
            raise CommandError('Please specify a report id or --all option')

        trackers_changed = 0
        count = 1
        for report in reports:
            self.stdout.write('Start updating report "{}" - {}/{}'.format(
                report.id, count, len(reports)))

            # report.application could fail with malformed reports
            try:
                handle = report.application.handle
            except Exception as e:
                self.stdout.write(self.style.WARNING(str(e)))
                continue

            count += 1
            with tempfile.TemporaryDirectory() as tmpdir:
                storage_helper = RemoteStorageHelper(report.bucket)

                if options['clist'] or options['icons']:
                    apk_name = report.apk_file
                    apk_tmp = os.path.join(tmpdir, apk_name)
                    try:
                        storage_helper.get_file(apk_name, apk_tmp)
                    except ResponseError:
                        raise CommandError('Unable to get APK')
                    static_analysis = StaticAnalysis(apk_path=apk_tmp)

                    if options['clist']:
                        with tempfile.NamedTemporaryFile(delete=True) as fp:
                            static_analysis.save_embedded_classes_in_file(
                                fp.name)
                            storage_helper.put_file(fp.name,
                                                    report.class_list_file)
                        self.stdout.write(
                            self.style.SUCCESS(
                                'Successfully updated classes list of "{}"'.
                                format(handle)))

                    if options['icons']:
                        icon_name = '{}_{}.png'.format(report.bucket, handle)
                        icon_phash = static_analysis.get_icon_and_phash(
                            storage_helper, icon_name)
                        if icon_phash:
                            report.application.icon_path = icon_name
                            report.application.save()
                            self.stdout.write(
                                self.style.SUCCESS(
                                    'Successfully updated icon of "{}"'.format(
                                        handle)))

                if options['trackers']:
                    # Download class list file
                    static_analysis = StaticAnalysis(None)
                    clist_tmp = os.path.join(tmpdir, report.class_list_file)
                    try:
                        storage_helper.get_file(report.class_list_file,
                                                clist_tmp)
                    except (ResponseError, NoSuchKey):
                        raise CommandError('Unable to get clist file')

                    trackers = static_analysis.detect_trackers(clist_tmp)
                    if report.found_trackers.count() != len(trackers):
                        trackers_changed += 1
                        self.stdout.write(
                            self.style.WARNING(
                                'Previous: {} - New: {} trackers'.format(
                                    report.found_trackers.count(),
                                    len(trackers))))
                    report.found_trackers.set(trackers)
                    report.save()
                    self.stdout.write(
                        self.style.SUCCESS(
                            'Successfully updated trackers list of "{}"'.
                            format(handle)))

            self.stdout.write('=====')

        self.stdout.write(self.style.SUCCESS('Update complete !'))
        if options['trackers']:
            self.stdout.write(
                'Reports updated (trackers): {}'.format(trackers_changed))
    def handle(self, *args, **options):
        if options['all']:
            try:
                reports = Report.objects.order_by('-creation_date')
            except Report.DoesNotExist:
                raise CommandError('No reports found')
        elif options['report_id']:
            try:
                reports = Report.objects.filter(pk__in=options['report_id'])
            except Report.DoesNotExist:
                raise CommandError('No reports found')
        else:
            raise CommandError('Please specify a report id or --all option')

        count = 1
        for report in reports:
            self.stdout.write('Start updating report "%s" - %s/%s' %
                              (report.id, count, len(reports)))

            # report.application could fail with malformed reports
            try:
                handle = report.application.handle
            except Exception as e:
                self.stdout.write(self.style.WARNING(str(e)))
                continue

            count += 1
            with tempfile.TemporaryDirectory() as tmpdir:
                icon_name = '%s_%s.png' % (report.bucket, handle)
                apk_name = report.apk_file
                apk_tmp = os.path.join(tmpdir, apk_name)

                storage_helper = RemoteStorageHelper(report.bucket)

                # Refresh clist
                if options['clist']:
                    try:
                        storage_helper.get_file(apk_name, apk_tmp)
                    except ResponseError:
                        raise CommandError('Unable to get APK')
                    static_analysis = StaticAnalysis(apk_path=apk_tmp)
                    with tempfile.NamedTemporaryFile(delete=True) as fp:
                        static_analysis.save_embedded_classes_in_file(fp.name)
                        storage_helper.put_file(fp.name,
                                                report.class_list_file)
                    self.stdout.write(
                        self.style.SUCCESS(
                            'Successfully updated classes list of "%s"' %
                            handle))

                # Refresh trackers
                if options['trackers']:
                    # Download class list file
                    static_analysis = StaticAnalysis(None)
                    clist_tmp = os.path.join(tmpdir, report.class_list_file)
                    try:
                        storage_helper.get_file(report.class_list_file,
                                                clist_tmp)
                    except ResponseError:
                        raise CommandError('Unable to clist file')
                    trackers = static_analysis.detect_trackers(clist_tmp)
                    self.stdout.write(
                        self.style.WARNING(
                            'previous: %s - new: %s trackers' %
                            (report.found_trackers.count(), len(trackers))))
                    report.found_trackers = trackers
                    report.save()
                    self.stdout.write(
                        self.style.SUCCESS(
                            'Successfully updated trackers list of "%s"' %
                            handle))

                # Refresh icon
                if options['icons']:
                    try:
                        storage_helper.get_file(apk_name, apk_tmp)
                    except ResponseError:
                        raise CommandError('Unable to get APK')
                    static_analysis = StaticAnalysis(apk_path=apk_tmp)
                    icon_path, _ = static_analysis.get_icon_and_phash(
                        storage_helper, icon_name)
                    if icon_path != '':
                        report.application.icon_path = icon_path
                        report.application.save()
                        self.stdout.write(
                            self.style.SUCCESS(
                                'Successfully updated icon of "%s"' %
                                report.application.handle))