How to Perform Dynamic Frida Hooking with frida-run.ps1: A PowerShell Automation Guide
The frida-run.ps1 script from the reverse-skill repository automates tool discovery, device connection, and argument assembly to streamline dynamic Frida instrumentation of Android processes through an intuitive PowerShell interface.
Dynamic instrumentation of mobile applications using Frida traditionally requires verbose command-line syntax and manual management of Python tool paths. The frida-run.ps1 script in zhaoxuya520/reverse-skill eliminates this complexity by providing a modular wrapper that handles USB and remote device connections, process spawning, and JavaScript payload injection. This guide explains the script's architecture and demonstrates how to leverage its automation for efficient dynamic Frida hooking.
Understanding the frida-run.ps1 Architecture
The script implements a clear, modular design that abstracts repetitive Frida CLI boilerplate into manageable PowerShell functions.
Parameter Handling and CLI Interface
The script accepts all essential Frida connection parameters through a structured param() block at the file start. Key parameters include -Package for target package names, -Process for process names, -ScriptPath for your Frida JavaScript payload, and -RemoteHost for network-connected devices. Boolean switches like -Usb, -Spawn, -Pause, -ListDevices, and -ListProcesses control connection modes and utility functions.
Automatic Tool Discovery and Bootstrapping
The Get-PythonScriptCandidates and Get-ToolPath functions (lines 30-90) locate required CLI binaries (frida, frida-ps, frida-ls-devices) by scanning the system PATH and common Python installation directories. If the tools are missing, the script automatically invokes skills/scripts/bootstrap-reverse.ps1 to install frida-tools, ensuring the workstation is ready for dynamic Frida hooking without manual setup.
Device Selection Logic
On line 11, the script sets the $deviceFlag variable based on the -Usb switch presence. This flag determines whether the generated Frida command uses -U for USB-connected devices or -H for remote Frida servers, abstracting the underlying transport protocol from the analyst.
Process Enumeration and Device Listing
The script provides two convenience inspection modes. When invoked with -ListDevices (lines 101-104), it executes frida-ls-devices to enumerate all reachable Frida-compatible devices. The -ListProcesses switch (lines 113-119) calls frida-ps to list running processes on the currently selected device, aiding target identification before hooking.
Command Construction and Execution
Between lines 125-139, the script assembles the final argument array, conditionally including spawn flags (--spawn), pause directives (--pause), and script paths (--load). Line 141 executes the resolved Frida binary using PowerShell splatting: & $frida @fridaArgs, which passes the constructed arguments to the native Frida CLI while preserving exit code propagation.
Practical Usage Examples for Dynamic Frida Hooking
The following examples demonstrate common operational patterns using the PowerShell wrapper.
Attach to a Running Application
Inject a script into an already-running Android app on a USB-connected device:
.\frida-run.ps1 -Package com.example.myapp -Usb -ScriptPath .\hooks\log_requests.js
Spawn and Hook with Pause
Start a fresh process instance, inject your instrumentation script, and maintain the process in a paused state until explicitly resumed:
.\frida-run.ps1 -Package com.example.myapp -Spawn -ScriptPath .\hooks\init_hook.js -Pause -Usb
Enumerate Available Devices
List all Frida-compatible devices, including USB-connected phones and remote servers, without targeting a specific process:
.\frida-run.ps1 -ListDevices
Inspect Remote Server Processes
Enumerate running processes on a Frida server listening at a specific network address:
.\frida-run.ps1 -RemoteHost '192.168.1.100:27042' -ListProcesses
Required Files and Dependencies
The dynamic hooking workflow relies on the following components from the reverse-skill repository:
skills/apk-reverse/scripts/frida-run.ps1— The main PowerShell orchestration script that manages the Frida workflow.skills/scripts/bootstrap-reverse.ps1— Helper script that auto-installsfrida-toolswhen the CLI binaries are not detected in system PATH.skills/apk-reverse/references/frida-cookbook.md— Collection of ready-to-use Frida scripts for common hooking patterns.
Ensure Python 3.x is installed on the host system, as the bootstrap process relies on pip to install the Frida toolchain.
Summary
frida-run.ps1abstracts complex Frida CLI syntax into PowerShell parameters, reducing command-line errors during dynamic analysis.- The script implements automatic tool discovery via
Get-ToolPathand self-healing installation throughbootstrap-reverse.ps1. - Device flexibility is handled through the
-Usband-RemoteHostparameters, with the$deviceFlagvariable (line 11) managing the underlying transport flags. - Utility modes (
-ListDevices,-ListProcesses) provide quick environment reconnaissance without requiring manualfrida-psorfrida-ls-devicesinvocation. - Execution occurs via argument splatting on line 141, ensuring robust pass-through of Frida-native options like
--spawnand--pause.
Frequently Asked Questions
How does frida-run.ps1 handle missing Frida installations?
When the Get-ToolPath function fails to locate frida-tools binaries in system PATH or Python directories, it triggers skills/scripts/bootstrap-reverse.ps1 to automatically install the required Python packages. This ensures first-time users can begin dynamic Frida hooking immediately without manual dependency resolution.
What is the difference between using -Spawn and the default attach mode?
The -Spawn switch instructs Frida to start a new process instance of the specified package before injection, while the default mode attaches to an already-running process. According to the source code in lines 125-139, -Spawn appends the --spawn flag to the underlying Frida command, whereas attach mode omits this flag and targets the running PID directly.
Can I use frida-run.ps1 with remote Frida servers instead of USB?
Yes. Omit the -Usb switch and provide the -RemoteHost parameter with the server address (e.g., 192.168.1.100:27042). The script sets the appropriate -H flag in the $deviceFlag variable on line 11, routing all instrumentation traffic through the network connection rather than the local USB bridge.
Where does the script assemble the final Frida command arguments?
Argument assembly occurs between lines 125-139 of frida-run.ps1, where the script conditionally builds an array including spawn directives, pause flags, script paths, and target identifiers. This array is then splatted to the Frida executable on line 141 using & $frida @fridaArgs, which preserves argument boundaries and exit codes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →