Frida & Dynamic Binary Instrumentation
Function Hooking & Instrumentation · Reverse Engineering track
7 min read
In this module
Instrumentation Without the Pain
Writing inline hooks by hand is a slog. Instruction boundaries, relocations, thread safety, platform quirks — every detail wants to bite you. Dynamic Binary Instrumentation (DBI) frameworks paper over the whole mess, so you can hook and instrument programs from a scripting language.
DBI takes a different angle entirely. Rather than patching individual functions, it wraps a hand around the program's whole execution, giving you a window onto everything that happens at runtime.
What Is DBI?
A full DBI engine parks itself between the program and the CPU, intercepting and rewriting code as it runs. The program's original instructions never execute directly. The framework copies them into a code cache, splices in your analysis code, and runs the modified copy. DynamoRIO and Pin work this way, and so does Frida's Stalker engine; Frida's Interceptor, which you'll meet first, takes the lighter route of patching a jump into the start of each hooked function.
The two models side by side. Follow where the original instructions actually execute in each:
Interceptor touches a handful of bytes per hooked function and leaves everything else alone. The code-cache engines never let the original bytes run at all, which is why they cost more and why they see everything.
That model handles the awkward cases for free: self-modifying code, JIT output, dynamically loaded libraries, multi-threaded execution. Because the framework owns every cycle, it'll happily instrument code that didn't even exist at process start — say, JavaScript generated by V8, or a payload a packer decrypts mid-run.
Frida: The Swiss Army Knife
Frida is the most popular DBI tool for security research. It injects a JavaScript engine (V8 or QuickJS) into a running process, letting you write hooks in JavaScript.
A simple Frida script to hook the open system call:
Interceptor.attach(Module.getGlobalExportByName('open'), { onEnter: function(args) { this.path = args[0].readUtf8String(); console.log('open(' + this.path + ')'); }, onLeave: function(retval) { console.log(' -> fd = ' + retval); }});Every call to open gets logged: path on entry, returned fd on exit. No source, no recompile, no restart of the target — point it at any running process and you're in. (Frida 17 removed the older Module.getExportByName(null, 'open') form you'll still see in many blog posts.)
You can't inject Frida into the runner, but the shape of an Interceptor hook is easy to copy by hand. Here every call to open goes through a pointer, the "attach" swaps that pointer for a wrapper, and the wrapper runs onEnter, calls the original and runs onLeave. Predict the fd each call gets before you run it.
Real Interceptor does the redirect by overwriting the first instructions of open itself, so it catches callers that never went through a pointer. The callback flow is the same.
The interactive loop is what makes Frida feel like a different class of tool. Attach to a process, inject a script, watch the output, tweak the script, re-inject. The target keeps running the whole time. That tight loop turns reversing into something closer to a REPL session.
Frida injects a JavaScript runtime into a target process, allowing you to hook functions, read/write memory, trace calls, and modify behaviour - all using JavaScript. It works on Windows, macOS, Linux, iOS, and Android, making it the go-to tool for dynamic analysis and security research.
Frida Capabilities
Interceptor: Hook native functions with onEnter and onLeave callbacks. Read or rewrite arguments and return values on the fly. Most Frida scripts you'll ever read live entirely inside this API.
Stalker: A code-tracing engine that walks execution one instruction at a time, recording branches and calls. Stalker traces through JIT-compiled and runtime-generated code that other tracers can't follow.
Memory: Read, write, scan, allocate. Hunt for byte patterns, dump regions, patch data. Need to find an encryption key buried somewhere in a 4GB process? Scan for it without knowing the address.
ObjC / Java bridges: Hook Objective-C methods on iOS and Java methods on Android by name. Mobile app analysis goes from painful to almost trivial.
// Hooking an Android Java methodJava.perform(function() { var Activity = Java.use('com.example.app.LoginActivity'); Activity.verifyPassword.implementation = function(password) { console.log('Password attempt: ' + password); return true; // always return true (bypass check) };});Since Frida 17 the Java, ObjC and Swift bridges are no longer built into Frida's runtime. The frida REPL and frida-trace still bundle them, so a script like this one runs as-is there; an agent you load through the API has to import the bridge package (frida-java-bridge) and be bundled with frida-compile.
Bridging native code with managed runtimes like Java and Objective-C is the trick that makes Frida the standard tool for mobile security work. Nothing else does it half as well.
DynamoRIO
DynamoRIO is a process-level DBI framework with a C/C++ API. Where Frida injects into a running process, DynamoRIO prefers to launch the target under its control from the start.
Its headline tools are Dr. Memory (a Valgrind-style memory error detector) and drcov, a code coverage tool that fuzzing pipelines lean on heavily. Custom instrumentation lives in C/C++ "clients", giving you the full reach of the API.
DynamoRIO operates closer to the metal than Frida — every individual instruction is in scope. Pick it up when you need full instruction tracing, taint analysis, or anything that wants to inspect every memory access in the program.
Intel Pin
Intel Pin is Intel's own DBI framework, with a rich API for x86 and x86-64. Academia loves it: cache simulation, branch prediction studies, security analysis papers — Pin sits behind a fair chunk of that work.
Granularity is Pin's calling card. Stick a callback before or after every instruction, every basic block, every function, every memory access. If your research needs to see every electron of program execution, Pin is the tool that delivers it.
Performance Considerations
DBI is never free. Tracing with Frida's Stalker usually costs you a 2-10x slowdown, depending on the workload, while a few targeted Interceptor hooks cost almost nothing outside the hooked calls. DynamoRIO and Pin run 5-50x slower under full instruction-level instrumentation.
Hook a handful of functions and log their arguments, and the overhead vanishes into the noise. Trace every instruction or every memory access, and interactive analysis becomes a slideshow. Pick your tool to match:
- Frida: Quick interactive analysis, mobile research, function-level hooking. Low overhead for targeted hooks.
- DynamoRIO/Pin: Heavy instrumentation, instruction-level analysis, academic research. Higher overhead but more complete visibility.
Security Implications
DBI frameworks are powerful tools for both offence and defence:
Defensive use: Watching malware behave inside a sandbox, fuzzing applications for vulnerabilities, dissecting protocol implementations, building automated analysis pipelines.
Offensive use: Sidestepping license checks, neutering anti-cheat, lifting encryption keys out of live processes, reversing closed protocols.
DBI is now table stakes for serious binary analysis. Watching and rewriting a live program with no source in sight is among the heaviest weapons a reverse engineer carries, and frameworks like Frida hand you that weapon without the cost of writing trampolines by hand.