def extract_apk_pkg_name(self, apk_path, apk=None):

        # Androguard analyze if not provided already
        if apk is None:
            apk = APK(apk_path)

        return apk.get_package()
    def extract_apk_icon(self, apk_path, apk=None):

        b64 = None

        # Androguard analyze if not provided already
        if apk is None:
            apk = APK(apk_path)

        # Extract icon filename
        icon_path = None
        try:
            icon_path = apk.get_app_icon()
        except ResParserError as e:
            pass

        if icon_path:
            # Extract icon to byte array
            zipi = zipfile.ZipFile(apk_path)

            try:
                zipi.extract(icon_path)
            except KeyError:
                pass

            icon_file = open(icon_path, 'rb')
            data = icon_file.read()
            icon_file.close()

            # Encode into b64 str
            b64 = base64.b64encode(data)

            shutil.rmtree(icon_path.split(os.path.sep)[0])

        return b64
Exemple #3
0
def analysis(apkfile):
    app = APK(apkfile)

    if not app.is_valid_APK():
        print('{} is not a valid apk')
        sys.exit(0)

    pprint(app.filename)
    pprint(app.androidversion)
    pprint(app.files)
    def add_apk_db(self, apk_path, sha256=None, is_malware = None):

        apk = APK(apk_path)
    
        # Insert into database
        data = {'sha256': sha256, 'filename': os.path.basename(apk_path), 'malware': is_malware, 'icon' : self.extract_apk_icon(apk_path), 'pkg_name': self.extract_apk_pkg_name(apk_path)}
        self.dbclient.execute('''INSERT OR IGNORE INTO apks(sha256, filename, malware, icon, downloaded, package_name) VALUES(:sha256, :filename, :malware, :icon, 1, :pkg_name)''', data)  
def dump_permissions(apkfile):
    return [perm
            for perm in APK(apkfile).get_AndroidManifest().toxml().splitlines()
            if 'android.permission' in perm.lower()]
def GetFromXML(ApkDirectoryPath, ApkFile):
    '''
    Get requested permission etc. for an ApkFile from Manifest files.
    :param String ApkDirectoryPath
    :param String ApkFile
    :return RequestedPermissionSet
    :rtype Set([String])
    :return ActivitySet
    :rtype Set([String])
    :return ServiceSet
    :rtype Set([String])
    :return ContentProviderSet
    :rtype Set([String])
    :return BroadcastReceiverSet
    :rtype Set([String])
    :return HardwareComponentsSet
    :rtype Set([String])
    :return IntentFilterSet
    :rtype Set([String])
    '''
    ApkDirectoryPath = os.path.abspath(ApkDirectoryPath)
    xml_tmp_dir = "/tmp/drod_xml_files"
    if not os.path.exists(xml_tmp_dir):
        os.mkdir(xml_tmp_dir)

    ApkName = os.path.splitext(os.path.basename(ApkFile))[0]

    RequestedPermissionList = []
    ActivityList = []
    ServiceList = []
    ContentProviderList = []
    BroadcastReceiverList = []
    HardwareComponentsList = []
    IntentFilterList = []
    try:
        ApkFile = os.path.abspath(ApkFile)
        a = APK(ApkFile)
        f = open(os.path.join(xml_tmp_dir, ApkName + ".xml"), "w")
        xmlstring = etree.tostring(a.xml["AndroidManifest.xml"], pretty_print=True, encoding="utf-8")
        f.write(xmlstring)
        f.close()
    except Exception as e:
        print(str(e))
        print("Executing Androlyze on " + ApkFile + " to get AndroidManifest.xml Failed.")
        return

    try:
        f = open(os.path.join(xml_tmp_dir, ApkName + ".xml"), "r")
        Dom = minidom.parse(f)
        DomCollection = Dom.documentElement

        DomPermission = DomCollection.getElementsByTagName("uses-permission")
        for Permission in DomPermission:
            if Permission.hasAttribute("android:name"):
                RequestedPermissionList.append(Permission.getAttribute("android:name"))

        DomActivity = DomCollection.getElementsByTagName("activity")
        for Activity in DomActivity:
            if Activity.hasAttribute("android:name"):
                ActivityList.append(Activity.getAttribute("android:name"))

        DomService = DomCollection.getElementsByTagName("service")
        for Service in DomService:
            if Service.hasAttribute("android:name"):
                ServiceList.append(Service.getAttribute("android:name"))

        DomContentProvider = DomCollection.getElementsByTagName("provider")
        for Provider in DomContentProvider:
            if Provider.hasAttribute("android:name"):
                ContentProviderList.append(Provider.getAttribute("android:name"))

        DomBroadcastReceiver = DomCollection.getElementsByTagName("receiver")
        for Receiver in DomBroadcastReceiver:
            if Receiver.hasAttribute("android:name"):
                BroadcastReceiverList.append(Receiver.getAttribute("android:name"))

        DomHardwareComponent = DomCollection.getElementsByTagName("uses-feature")
        for HardwareComponent in DomHardwareComponent:
            if HardwareComponent.hasAttribute("android:name"):
                HardwareComponentsList.append(HardwareComponent.getAttribute("android:name"))

        DomIntentFilter = DomCollection.getElementsByTagName("intent-filter")
        DomIntentFilterAction = DomCollection.getElementsByTagName("action")
        for Action in DomIntentFilterAction:
            if Action.hasAttribute("android:name"):
                IntentFilterList.append(Action.getAttribute("android:name"))

    except Exception as e:
        print(str(e))
        print("Cannot resolve " + ApkFile + "'s AndroidManifest.xml File!")
        return RequestedPermissionList, ActivityList, ServiceList, ContentProviderList, BroadcastReceiverList, HardwareComponentsList, IntentFilterList
    finally:
        f.close()
        return RequestedPermissionList, ActivityList, ServiceList, ContentProviderList, BroadcastReceiverList, HardwareComponentsList, IntentFilterList
Exemple #7
0
 def __init__(self, path):
     APK.__init__(self, path)
     self.path = str(path)
     self.get_sha256_hash()