Introducing a new way to review your PRs.

Read the announcement

mobsf / mobile-security-framework-mobsf

Mobile Security Framework (MobSF) is an automated, all-in-one mobile application (Android/iOS/Windows) pen-testing, malware analysis and security assessment framework capable of performing static and dynamic analysis.

View on GitHub

13 documents · Updated Nov 16, 2026

docs / android-dynamic-analysis

Android Dynamic Analysis

Overview

The Android dynamic-analysis flow starts at mobsf/…/dynamic_analyzer.py. android_dynamic_analysis() lists previously scanned APKs from StaticAnalyzerAndroid, enumerates ADB devices, stores the selected device in request.session['android_device_identifier'], and uses Environment from mobsf/…/environment.py to show the selected runtime, Android version/API level, proxy endpoint, and optionally the installed third-party package inventory. The device inventory is loaded lazily through android_device_apps(), persisted by save_device_packages() in mobsf/…/operations.py, and used later by get_package_name() when an app was pulled from a device instead of found in the static-analysis database.

dynamic_analyzer() in mobsf/…/dynamic_analyzer.py prepares a session for one APK checksum. It validates AndroidDynamicAnalysisForm from mobsf/…/forms.py, resolves the package through get_package_name(), reads static-analysis activities/deeplinks from StaticAnalyzerAndroid, connects to the selected ADB target through Environment.connect(), and builds a DeviceProfile with runtime, SDK, ABI, supported ABIs, root status, SELinux state, and /data/local/tmp writability. The setup path chooses one of three modes:

  • legacy for supported rooted emulator/VM flows. Environment.connect_n_mount() restarts adbd as root, remounts /system, checks the API ceiling (ANDROID_API_SUPPORTED = 30), and prepares MobSF agents when the image is not already MobSFyed.
  • rooted for physical devices with non-interactive root. Environment.probe_root() chooses direct root ADB, su -c, or su 0; mobsfy_init() installs Frida server under /data/local/tmp/mobsf/fd_server and writes a marker under /data/local/tmp/mobsf/.mobsf-f rather than modifying /system.
  • gadget for stock physical devices, unsupported VM APIs, or failed Frida-server preparation. The APK is patched and re-signed by GadgetPatcher in mobsf/…/apk_patcher.py, then installed as the analysis target.

Before a new session is started, dynamic_analyzer() calls supersede_android_analyses() from mobsf/…/operations.py. That closes in-process Frida sessions, reads proxy-state scan-state files, force-stops previous packages, clears device proxy state, stops physical-device Frida servers, removes MobSF-owned Frida forwards, and clears old analysis state. The new session then force-stops the package, closes any matching Frida session, removes Frida forwarding, clears screenshots through Environment.dz_cleanup(), and writes the active device binding with write_analysis_state(checksum, {'identifier': identifier}) after proxy setup begins.

Device and APK setup is centralized in mobsf/…/environment.py. Environment.adb_command() wraps adb -s <identifier> and serializes traffic per device with _device_adb_lock(). Environment.get_environment() classifies Genymotion, Corellium, Android Studio emulator, other VMs, and physical devices from getprop. Package operations include exact installation checks, uninstall after validation, single/split APK install, APK path discovery with pm path --user, installed APK hashing, device APK pull, and application-data archiving. UI-side device actions in mobsf/…/operations.py use this same object for launching apps, screenshots, screen streaming, touch/text/key events, MobSF CA install/remove, and global proxy set/unset.

Proxy capture is started during setup by Environment.configure_proxy(). It installs the mitmproxy CA into /system/etc/security/cacerts for non-physical/non-Gadget modes, skips CA installation for physical devices, and in Gadget mode relies on the patched APK embedding the MobSF CA. It then calls stop_httptools() and start_proxy() from mobsf/…/webproxy.py. start_proxy() launches mitmdump on settings.PROXY_PORT, saves binary flows to ~/.httptools/flows/<package>.flow, and uses mobsf/…/mitm_capture.py to write a readable ~/…/<package>.flow.txt. For Android 5+, dynamic_analyzer() also ensures adb reverse tcp:<PROXY_PORT> tcp:<PROXY_PORT>, writes the selected device into scan state, sets the global Android proxy to the MobSF proxy address, and verifies settings get global http_proxy for Android 4.4+.

Gadget mode is implemented by GadgetPatcher in mobsf/…/apk_patcher.py. It validates that inputs live under the upload directory, reads retained split-APK metadata from scan state, selects a device-compatible ABI, rejects unknown existing Gadget injections unless explicitly confirmed, and uses cached MobSF patch metadata when possible. The patcher decodes the APK with apktool, injects a primary-process-only System.loadLibrary("frida-gadget") loader into an existing or generated Application/launcher class, adds INTERNET when resources are rebuilt, embeds the MobSF CA into res/raw, patches network-security XML to trust that CA with overridePins="true", downloads a matching Frida Gadget library, writes a local-only Gadget config listening on 127.0.0.1:27042 with on_load: wait, rebuilds and signs the APK with a MobSF-generated PKCS12 key, signs required splits, and returns the install set to dynamic_analyzer(). Split APK patching preserves raw resources and therefore does not embed the Root CA; root_ca_embedded is returned in the page context so the UI can explain interception limitations.

Frida instrumentation is coordinated by mobsf/…/frida_core.py and invoked by mobsf/…/tests_frida.py. The instrument() view validates hook selections, custom code, PID attachment arguments, and package names, then creates a Frida object. Frida.get_script() combines custom code, selected default scripts from mobsf/DynamicAnalyzer/tools/frida_scripts/android/default, all RPC exports from android/rpc, and auxiliary scripts from android/auxiliary via mobsf/…/frida_scripts.py; Frida 17 bridge support is injected through the shared bridge loader when needed. For server mode, Frida.instrument() starts fd_server, gets a Frida device, spawns or attaches to a process, loads the combined script, resumes spawned processes, and records the active session in _ACTIVE_SESSIONS. For Gadget mode, it stops any Frida server, force-stops the app, frees port 27042, launches the Gadget-patched app, forwards a local ADB port to device tcp:27042, finds the Gadget-host process, and attaches before resuming.

Frida session ownership is guarded by a per-checksum frida-owner scan-state file in mobsf/…/frida_core.py, plus in-memory locks keyed by (checksum, identifier). Script messages are routed by prefix: MobSF-API-Monitor: is appended to mobsf_api_monitor.txt, mobsf-android-clipboard: to mobsf_app_clipboard.txt, runtime dependencies to mobsf_app_deps.txt and mobsf_frida_out.txt, auxiliary output to mobsf_frida_out.txt, and RPC results to mobsf_rpc_classes.txt, mobsf_rpc_methods.txt, or mobsf_rpc_impl.txt. get_runtime_dependencies() reuses an existing session when possible or temporarily instruments with dependency collection hooks.

TLS testing is available only when MobSF can transition the device Root CA, so tls_tests() in mobsf/…/tests_common.py rejects Gadget and physical-device sessions. run_tls_tests() in mobsf/…/tests_tls.py runs three capture windows: no Root CA/no bypass for TLS misconfiguration, Root CA/no bypass for missing pinning or certificate transparency, and Root CA plus Frida SSL-pinning bypass hooks. Each window force-stops/restarts the package, captures mitmproxy traffic under a temporary tls_tests project, classifies HTTP/HTTPS requests, and writes mobsf_tls_tests.json under the APK upload directory.

Runtime collection and reporting are split between mobsf/…/tests_common.py, mobsf/…/analysis.py, and mobsf/…/report.py. collect_logs() downloads logcat.txt and dump.txt, pulls Xposed logs into x_logcat.txt for Android <5, then stops mitmproxy, force-stops the app, clears the device proxy, closes Frida, stops physical Frida server, removes Frida forwarding, and clears analysis state. download_data() archives /data/data/<package> to <package>.tar when root or root ADB permits it. view_report() requires logcat.txt, parses Xposed Droidmon logs, Frida API-monitor logs and Base64 strings, runtime dependencies, clipboard data, traffic-derived URLs/domains/emails, application files, TLS results, screenshots, and tracker matches, then calls generate_download() to copy logs, Frida output, web traffic, screenshots, dumpsys, and app data into the downloads directory.

The web traffic viewer uses httptools_start() in mobsf/…/dynamic_analyzer.py and helpers from mobsf/…/webproxy.py. It lists valid project stems from ~/.httptools/flows, groups loaded mitmproxy HTTPFlow entries by scheme://host, returns request/response details as JSON for one flow ID, and can replay a captured project through an analyst-provided upstream fuzzer proxy by running mitmdump --no-server --client-replay with mitm_capture.py routing replayed requests through the normalized proxy.

Progress streaming is opt-in through the X-MobSF-Progress: 1 request header. dynamic_analyzer() and mobsfy() wrap their work in progress_response() from mobsf/…/progress.py, which runs setup on a thread, attaches a logging handler to mobsf.DynamicAnalyzer, emits de-duplicated INFO+ log messages as newline-delimited JSON, and finishes with either {"done": ...} or {"error": ...}. This is how long-running device setup, APK patching, proxy startup, and installation steps reach the browser without waiting for the final HTML render.

Device app metadata and icons are ancillary to the analysis lifecycle. android_device_details() and android_device_apps() use Environment.get_device_profile() and get_device_packages() to show connected-device capabilities and installed apps. android_device_app_icon() calls device_package_icon() in mobsf/…/device_icon.py, which reads selected ZIP entries from installed APKs over adb exec-out instead of pulling full APKs, resolves launcher icons through Androguard resource tables and splits, converts bounded Android vectors through mobsf/…/device_vector.py, and caches sanitized icon/label artifacts under settings.MOBSF_HOME/device_icons.

Coupling

  • If dynamic_analyzer() changes the selected-mode rules in mobsf/…/dynamic_analyzer.py, also check android_package_status() in the same file. The status endpoint precomputes whether confirmation is needed for existing installs and third-party Gadget injections; diverging mode selection can let the UI skip a required uninstall or Gadget replacement confirmation.
  • If Environment.configure_proxy(), set_global_proxy(), or ADB reverse behavior changes in mobsf/…/environment.py, verify run_tls_tests() in mobsf/…/tests_tls.py, collect_logs() in mobsf/…/tests_common.py, and webproxy.start_proxy() in mobsf/…/webproxy.py. The setup path assumes the device global proxy, ADB reverse, mitmdump project name, and CA state all point at the same settings.PROXY_PORT capture.
  • If scan-state keys or active-device binding change, update every reader of write_analysis_state(checksum, {'identifier': identifier}): supersede_android_analyses() in mobsf/…/operations.py, _analysis_identifier() in mobsf/…/tests_frida.py, get_analysis_identifier() and collect_logs() in mobsf/…/tests_common.py, and Frida owner/forward state in mobsf/…/frida_core.py and environment.py.
  • If Gadget metadata in mobsf/…/apk_patcher.py changes (PATCH_SCHEMA, patch_revision, loader metadata, CA resource metadata, split digests, or signer digest), update both GadgetPatcher.inspect() and _cached_output(). android_package_status() and dynamic_analyzer() rely on the same metadata to distinguish MobSF-patched APKs from unknown Gadget injections and to decide whether cached patched artifacts are safe.
  • If Android Frida script names under mobsf/DynamicAnalyzer/tools/frida_scripts/android/default or android/auxiliary are renamed, update UI/form values and code paths that pass those stems to Frida.get_scripts() and Frida.get_auxiliary() in mobsf/…/frida_core.py. The loader matches script stems directly and silently omits a missing hook.
  • If Frida message prefixes change inside the JavaScript hooks, update Frida.frida_response() in mobsf/…/frida_core.py and the report parsers in mobsf/…/tests_frida.py and analysis.py. API-monitor, clipboard, dependency, and auxiliary output are routed solely by string prefixes, not by a structured schema.
  • If get_package_name() in mobsf/…/operations.py changes package resolution, verify pull-and-scan paths in mobsf/…/dynamic_analyzer.py, report generation in mobsf/…/report.py, and all Frida/TLS/test actions. Device-pulled apps depend on the saved package inventory JSON when StaticAnalyzerAndroid is missing or incomplete.
  • If mitm_capture.py changes flow output naming, update analysis.get_log_data(), analysis.generate_download(), webproxy.get_traffic(), and httptools_start(). Dynamic report URL/domain extraction and the web traffic viewer both assume <project>.flow and <project>.flow.txt live under ~/.httptools/flows.
  • If APK install behavior changes in Environment.install_apk() or install_apks(), verify GadgetPatcher.patch() output handling in dynamic_analyzer(). Gadget mode may install a signed base plus signed splits, while legacy/rooted modes install the original APK and may allow reinstall flags.

Risks

  • mobsf/…/webproxy.py stores the running mitmdump process in a module-level _PROXY_PROCESS, and Environment.configure_proxy() stops any existing proxy before starting a new capture. If two Android dynamic-analysis sessions run concurrently, the later session can terminate the earlier session's capture and overwrite proxy state, causing missing or cross-project traffic in analysis.py and httptools_start().
  • supersede_android_analyses() in mobsf/…/operations.py intentionally closes all active Frida sessions and clears all proxy-state scan-state files before a new analysis. If a caller starts dynamic analysis while another analyst is still collecting logs, collect_logs() in mobsf/…/tests_common.py may find the app already force-stopped or proxy/Frida state already removed.
  • Frida Gadget mode depends on exclusive use of TCP port 27042 inside the app process (mobsf/…/apk_patcher.py writes the Gadget config; Environment.free_frida_port() and Frida.instrument() in environment.py and frida_core.py try to free and forward that port). If another Gadget app or Frida server keeps the port and cannot be identified due to SELinux/proc restrictions, Gadget instrumentation fails or attaches to the wrong responder.
  • collect_logs() in mobsf/…/tests_common.py is the main cleanup point for global proxy, ADB reverse, Frida sessions, physical Frida server, and analysis state. If the browser/API caller never invokes it after setup succeeds, the selected device can keep the MobSF proxy and Frida forwarding until a later supersede_android_analyses() or manual cleanup.