Dynamic Instrumentation with Frida
Frida is a dynamic instrumentation toolkit: a JavaScript runtime injected into a live process, the agent script it executes, and a channel back to the analyst. A debugger halts the program to let you inspect it; a hook leaves it running and reports the arguments, return values and memory passing through functions you name. What follows is the deployment model, the timing that decides whether a hook fires, the API an analyst uses, and what the result can prove.
Injection Is Code Execution With the Target's Privileges
frida-server
running as root on a rooted device, an app holding its own permissions. Work only against a device, emulator
or binary you own, or one you have written authorisation to test.
What the Tool Actually Is
Frida has two halves. A shared library the host loads into the target carries a JavaScript runtime and the machinery to call native functions, read memory and patch code. Your agent — one script, executed inside the process — registers hooks and posts results back over USB, a TCP socket or a local pipe. Everything happens in the target's address space, so you see the modules actually mapped, not the ones an import table claims. Three shapes get the library in:
| Shape | What is deployed | When it applies |
|---|---|---|
| Device server | A frida-server binary started as root on a rooted or jailbroken device. |
Every process, no repackaging; needs root on a device you control. |
| Embedded gadget | A frida-gadget library bundled into the app or an emulator image, with a config file. |
One app, where root is unavailable or the app must keep its signature. |
| Local spawn | Nothing: the host launches the process and injects at startup. | Desktop binaries you can run under your own account. |
The differences show up when they fail: a server needs a root path the device may lack, a gadget changes a signature the app may check.
Attach, Spawn, and Why Timing Decides
Attaching starts the agent whenever you arrive, so everything before that is invisible: a key derived during library initialisation, an integrity check run from a constructor, an archive decrypted before the first window. You see the results but not the work, and the usual symptom is a hook that installs cleanly, logs nothing and looks correct — the function never runs again.
Spawning suspends the new process, injects before the program's own initialisation, and resumes when you say so. Libraries are already mapped, but constructors have not run, so a hook installed there catches the first call. Spawn when you do not know when the code runs; attach when you do, since a process already in the right state is cheaper to instrument than to reproduce.
# List processes on the attached device, then attach to one by name
frida-ps -U
frida -U -n com.example.app -l agent.js
# Or start the program yourself; it waits at the entry point until you
# resume, so the agent runs before initialisation
frida -U -f com.example.app -l agent.js
%resume
# A process already running on this machine
frida -p 4812 -l agent.jsThe API an Analyst Uses
The surface is small.
Process.enumerateModules()
lists what is mapped now, with name, base, size and path; the difference from what should load is worth a
look.
Module.findExportByName(null, 'EVP_DigestUpdate')
resolves an export to a pointer or null, and a null module searches everything loaded.
Interceptor.attach
takes onEnter,
which receives the arguments, and
onLeave,
which receives the return value, where
retval.replace()
would change it. The helper names have changed between releases (the current version is not stated here).
In onEnter,
args[0] is a
pointer, not a number:
Memory.readUtf8String
and toInt32()
make it legible.
this.context
holds the registers at entry, beside
this.returnAddress
and this.depth.
Fields written to this
in onEnter
survive into onLeave
for that invocation, which is how an argument is carried forward, with
this.depth
keeping recursive frames apart.
// agent.js - resolve one export, log both directions, modify nothing
const mods = Process.enumerateModules();
const lib = mods.find(m => m.name.indexOf('libcrypto') === 0);
console.log(mods.length + ' modules, libcrypto: ' + (lib ? lib.base : 'absent'));
const target = Module.findExportByName(lib ? lib.name : null, 'EVP_DigestUpdate');
if (target === null) {
console.log('export not found: module absent, or the call is inlined');
} else {
Interceptor.attach(target, {
onEnter(args) {
this.len = args[2].toInt32(); // size_t cnt, low 32 bits for the log
this.data = Memory.readUtf8String(args[1], this.len); // UTF-8; readByteArray for binary
console.log('digest update, len ' + this.len);
console.log(' caller ' + this.returnAddress);
console.log(' data ' + this.data);
},
onLeave(retval) {
console.log(' returned ' + retval); // observe: nothing is replaced
}
});
}Hooking a Function With No Export
Most functions worth watching in a stripped binary have no export. Take the module base and add the
disassembler's offset:
Module.findBaseAddress('libtarget.so').add(0x1a2c).
The base is null until the module is mapped, so check it, and confirm the prologue bytes match the
disassembler's — otherwise the base or the build is wrong and you are observing another function.
That offset belongs to this exact build. Optimisation flags, a dependency bump or a store update all move it. Here dynamic work becomes a false statement: a hook that works today is not evidence about any other version — not that the function is absent, not that a defect was fixed. Each build needs its own offset from its own file, and a finding carries that file's hash.
Inlining is the other direction: with link-time optimisation there may be no separate body to hook.
The Java and ObjC Bridges
The language bridges are not core: each attaches to a runtime already present in the process.
Java.available
is false without ART or Dalvik, and
Java.perform
never runs its callback; a Java hook against a native-only process fails for that reason, not because the
class is missing.
ObjC.available
behaves the same without an Objective-C runtime. On Android the bridge arrives when the VM does, which can be
after native initialisation did the work.
When present, the bridge gives classes and methods rather than addresses:
Java.use('com.example.License'),
a method's implementation
property, ObjC.classes.NSString.
A name survives recompilation where an offset does not. It also changes what "the same code" means between
builds: obfuscators rename methods, overloads need their signature spelled out, and a JNI-backed method is
often a wrapper over native code the bridge cannot see.
Detection and Its Limits
A detector inspects the process it runs in. It can walk its own mapped modules for a library that does not belong to the app — the injected runtime, or a gadget — read thread names, look for a listener on a local port or socket, check for an attached tracer, hash its own code, or scan memory for runtime structures.
The values behind those checks — port, thread name, library filename, process name — belong to a build. Documented defaults have changed across releases and are easy to change, so a detector written against one build's literals fingerprints that build, not the technique (the current version is not stated here).
A detector can be beaten by hooking it, replacing the check's return value or its code. That is a race, not a state you hold. The check may run before your agent loads, from a constructor, a second process or a child at startup, and may be called from several sites. Passing on this launch says nothing about the next.
Observation Versus Modification
A hook is either an observer or a modifier, and that decides what the result is worth. An observer reads
arguments, memory and return values and writes nothing; on the same build the same input yields the same
log, so another analyst can reproduce it. A modifier changes the program —
retval.replace(),
Interceptor.replace,
a memory write, a branch forced the other way — and from then the program observed is not the one in the
field.
A string recovered by patching the check that would have rejected it is not a string the application produces in a real run, and a log taken after a modification describes your modified program: right about the modification, possibly wrong about the sample.
Keep the phases apart: observe first on an unmodified run, name the hook class, the address and the build in the finding.
Evidence for an Instrumentation Finding
An observation is reproducible only with the context that produced it:
Target build: SHA-256 of the exact file or package installed on the device, plus the architecture and OS build.
Hook class: Observer or modifier, named explicitly, with the API used (Interceptor.attach or Interceptor.replace).
Address: Export name, or module name and relative offset. An offset without the build hash identifies nothing.
Timing: Whether the agent attached to a running process or spawned it, and where the resume point fell.
Agent and input: The agent source, and the interaction or input that drove execution through the hooked function.
Output: Agent console log or message stream captured verbatim, with the analysis host clock for correlation.
False Positive Identification
A hook that produced nothing may be a finding or a mistake; these separate the two:
- No output at all: The module was not mapped or the function never ran after installation — check the module list before concluding the call is absent.
- Offset from another build: The trampoline landed on unrelated code, so the log describes a different function. Re-derive the offset from the installed file.
- Internal or inlined call: The target reached the logic through a locally bound copy or an inlined body, so the export hook was never on the path.
- A check that passed: A detector patched by a hook reports success for that launch only; it says nothing about whether the unmodified build would have failed.
Related Topics
Dynamic Analysis
The debugger workflow this replaces where a breakpoint would stop the program you need to keep running.
Anti-Analysis Techniques
The detector categories an instrumented process has to survive, from timing checks to integrity tests.
Managed Code
The Java and .NET runtimes whose objects the language bridges expose, and what obfuscation does to their names.