Android Static Analysis
Overview
The Android static-analysis flow is entered through mobsf/…/static_analyzer.py. static_analyzer() validates the scan hash against RecentScansDB, checks the uploaded extension against settings.ANDROID_EXTS, builds the shared app_dic state (md5, original file name, upload directory, tools directory, icon placeholder), normalizes container formats, and dispatches by scan type. XAPK, split-APK (.apks), and AAB inputs are converted to a single base APK first by mobsf/…/xapk.py; APKs then go to apk_analysis(), JAR/AAR/DEX to common_analysis() in mobsf/…/jar_aar.py, shared objects to so_analysis() in mobsf/…/so.py, and zipped Android/iOS source to src_analysis() in mobsf/…/apk.py.
APK analysis is orchestrated by apk_analysis_task() in mobsf/…/apk.py. It computes size and hashes, unzips the APK into settings.UPLD_DIR/<md5>/, parses APK metadata with Androguard via mobsf/…/app.py, extracts aapt/aapt2 badging/files/strings through mobsf/…/aapt.py, records hardcoded cert/key files, extracts and analyzes the manifest, resolves app name and Play Store/AppMonsta details, runs malware-permission checks, copies the resolved icon to settings.DWD_DIR, runs ELF/native library analysis, reads signing certificate data, runs APKID and tracker detection, decompiles to Java with JADX, optionally emits smali with baksmali, runs code/SBOM/API/permission/behavior/NIAP analysis, extracts resource/code/native strings and secrets, performs Firebase and malware-domain checks, then persists and returns a render/API context through save_get_ctx().
Manifest handling is split between mobsf/…/manifest_utils.py and mobsf/…/manifest_analysis.py. get_parsed_manifest() locates AndroidManifest.xml from apktool output for APKs, the AAR root for AARs, or the expected source tree path for Eclipse/Studio/Flutter source. It falls back from apktool XML to Androguard XML and then BeautifulSoup XML parsing. extract_manifest_data() populates package name, app version, SDK levels, main activity, components, libraries, launcher icon references, and permission metadata using mobsf/…/dvm_permissions.py. manifest_analysis() converts manifest patterns into findings using descriptions from mobsf/…/android_manifest_desc.py, including insecure SDK levels, cleartext/debuggable/backup/test-only flags, network-security-config presence, exported/implicitly-exported components, browsable activities and app-link assetlinks checks, broad provider grants, secret dialer codes, high-priority intents/actions, permission summaries, exported counts, and network-security-config results.
Network-security-config analysis lives in mobsf/…/network_security.py. When the manifest references android:networkSecurityConfig, it reads the matching XML from app/src/main/res/xml for Studio/Flutter source or apktool_out/res/xml for APKs, with a fallback for files whose stem contains network_security. It reports cleartext allowances, system/user/bundled trust anchors, pinning expiry/absence, overridePins bypasses, and debuggable-only debug overrides into network_findings and a severity network_summary.
Certificate and signing analysis is implemented by mobsf/…/cert_analysis.py. get_hardcoded_cert_keystore() scans archive file lists for embedded certificate/key/keystore extensions and stores those findings in app_dic['file_analysis']. cert_info() prefers Androguard certificate extraction when app_dic['androguard_apk'] is available, otherwise falls back to apksigtool/apksigcopier; apksigner.jar is used where possible to normalize v1-v4 signature scheme booleans. Findings cover missing signatures, Janus-sensitive v1 signing by API level, debug certificates, and weak SHA1/MD5 hash algorithms, returning certificate_info, certificate_findings, and certificate_summary.
Code analysis is centralized in mobsf/…/code_analysis.py. It resolves the Java/Kotlin source root with get_android_src_dir(), reads source files once through SastEngine, and applies YAML rule packs under mobsf/StaticAnalyzer/views/android/rules/: android_rules.yaml for security findings, android_apis.yaml for API usage, android_permissions.yaml for dynamic permission-to-code mapping when PERM_MAPPING_ENABLED is on, and android_niap.yaml through ChoiceEngine when NIAP_ENABLED is on. The same pass triggers behavior analysis, extracts URLs/emails from .java/.kt files, and adds JavaScript/hybrid/Hermes results from the common JS analyzer. mobsf/…/sbom_analysis.py contributes sbom_versioned from standard dependency/build manifests and sbom_packages from Java/Kotlin package declarations.
String, secret, and URL metadata is assembled by mobsf/…/strings.py. APK resource strings come from Androguard resources or aapt2 dump strings, native strings come from ELF analysis, and code strings/secrets come from Java/Kotlin entropy scanning. The helper merges resource/native/code strings into code_dic['strings'], extends urls, emails, and urls_list, records Firebase resource credentials such as google_api_key/google_app_id, and deduplicates URLs.
Container and conversion paths preserve the same final context shape. mobsf/…/xapk.py extracts XAPK manifests, keeps the base split as <md5>.apk, writes retained sibling split metadata to scan state, extracts sibling split contents under a bounded uncompressed-size budget for split APK native-library visibility, and converts AABs to universal APKs using bundletool. mobsf/…/converter.py runs JADX into java_source/, falls back from whole-APK decompilation to top-level DEX files, optionally runs baksmali into smali_source/, and runs apktool into apktool_out/ for manifest/resource decoding. mobsf/…/jar_aar.py reuses most APK analyzers for JAR/AAR/DEX, synthesizing empty manifest/certificate data for formats without a manifest, while AARs parse their manifest normally. mobsf/…/so.py builds a manifestless context for .so files from ELF analysis, exported symbols, strings, Firebase/domain/tracker checks, and no appsec CVSS dashboard.
All persisted Android static-analysis output uses StaticAnalyzerAndroid through mobsf/…/db_interaction.py. save_or_update() maps app_dic, manifest data, manifest analysis, code analysis, certificates, binary/file analysis, APKID, trackers, Play Store details, network security, secrets, SBOM, and JS analysis into model fields, then updates RecentScansDB with app name, package, and version. get_context_from_db_entry() rebuilds template/API context from stored fields when a scan is not a rescan, applying manifest/code/JS suppression and secret sorting; get_context_from_analysis() builds the same context directly from fresh analysis. Binary APK/JAR/AAR/DEX contexts render static_analysis/android_binary_analysis.html, source ZIP contexts render static_analysis/android_source_analysis.html, and both add dashboard/appsec and average-CVSS summaries before rendering/API return.
Source-browsing views are under mobsf/StaticAnalyzer/views/android/views/. source_tree.py renders Java/smali/JS tree shells and serves paged jsTree nodes without following symlinks; JS trees are built from cached common JS source-file lists. view_source.py validates request forms, chooses Java/Kotlin, smali, JS, or XML roots, enforces safe relative paths, and returns the file body or a paged JS source window. manifest_view.py reuses get_manifest_file() to display the manifest for APK/AAR/source scans. find.py searches filename or content across Java/Kotlin/smali or cached JS files using bounded parallel chunked reads.
Coupling
- If any analyzer changes keys in
app_dic,man_data_dic,man_an_dic,code_an_dic, certificate dictionaries, or tracker/APKID outputs, also update mobsf/…/db_interaction.py; both DB persistence and cached-context reconstruction assume exact keys such asnetwork_security,malware_permissions,sbom,js_analysis,strings,secrets, andfile_analysis. - If mobsf/…/manifest_utils.py changes source-type labels or manifest locations, verify mobsf/…/apk.py, mobsf/…/jar_aar.py, mobsf/…/manifest_view.py, and mobsf/…/view_source.py; these paths all branch on
apk,aar,eclipse,studio, andflutterwhen locating manifests/XML roots. - If APK/AAB/XAPK/split-APK normalization changes in mobsf/…/xapk.py or mobsf/…/static_analyzer.py, verify mobsf/…/apk.py and the source views still expect the final binary at
settings.UPLD_DIR/<md5>/<md5>.apkand decoded resources underapktool_out/. - If JADX, apktool, baksmali, bundletool, apksigner, aapt/aapt2, or vd2svg paths/timeouts change in mobsf/…/converter.py, mobsf/…/xapk.py, mobsf/…/cert_analysis.py, mobsf/…/aapt.py, or mobsf/…/icon_analysis.py, verify the corresponding settings (
JADX_BINARY,JADX_TIMEOUT,APKTOOL_BINARY,BACKSMALI_BINARY,BUNDLE_TOOL,BINARY_ANALYSIS_TIMEOUT,AAPT_BINARY,AAPT2_BINARY,VD2SVG_BINARY) and template links that assumejava_source/,smali_source/, andapktool_out/outputs exist. - If rule IDs, metadata fields, or match semantics change in mobsf/…/android_rules.yaml, android_apis.yaml, android_permissions.yaml, or android_niap.yaml, verify mobsf/…/code_analysis.py and the dashboard/average-CVSS consumers; code findings are passed through suppression and CVSS aggregation, while permission mappings are reduced to a permission-to-files map.
- If manifest finding keys are added or renamed in mobsf/…/manifest_analysis.py, update mobsf/…/android_manifest_desc.py; missing templates are only logged and the finding is dropped from
manifest_anal. - If icon extraction changes in mobsf/…/icon_analysis.py, verify mobsf/…/manifest_utils.py; manifest data imports
_ordered_manifest_icon_nodes()to collect icon references, so launcher/app/component priority must stay consistent between manifest data and final copied icon. - If JS/hybrid analysis result shape changes in common JS analysis, update mobsf/…/code_analysis.py, mobsf/…/db_interaction.py, mobsf/…/source_tree.py, mobsf/…/view_source.py, and mobsf/…/find.py; Android static analysis stores JS findings/secrets and uses cached JS source lists for browsing/search.
Risks
- mobsf/…/icon_analysis.py is path- and format-fragile: recent history shows a sequence of fixes for XAPK root icons, activity-level icons, density ordering, adaptive/vector XML resolution, corrupted PNG/WebP validation, and symlink/path-containment hardening. If candidate ordering or path validation regresses, user-visible icons can be missing or an extracted archive path could escape the scan directory.
- mobsf/…/source_tree.py, mobsf/…/view_source.py, and mobsf/…/find.py recently changed for lazy loading, JS paging, cached JS tree keys, and removal of ripgrep. If source-root selection or paging limits drift, large scans can time out or show/search the wrong source tree, especially for JS/hybrid and Flutter scans.
- mobsf/…/xapk.py handles untrusted nested archives and split APK contents. It relies on
is_safe_path(),_regular_file(), split metadata, and the sibling split uncompressed-size budget; if these checks are bypassed, split extraction can miss native libraries or reintroduce archive traversal/symlink/resource-exhaustion issues. - mobsf/…/manifest_analysis.py performs outbound assetlinks checks from manifest-controlled hosts. The code caps total hosts/time and uses
valid_host()/safe_request()with URL path/port validation; changing that logic can make scans hang on DNS/network calls or weaken SSRF protections.