Pull requests are welcomed.
- Try to run the given injection techniques code.
- Understand how each technique works
- Understand the attack vector and the different parts (stages) of the chain
(i.e the bridgehead shellcode, injection to process memory,LPE, when to create a new process etc.)
- Describe the need for a custom statically PIC compiled elf (Shared object library) loader shellcode.
- Injection vs patching at runtime?
- Implement / improve it by yourself.
Modern Android Notes
This repository is a research index, not a copy-paste implementation guide. The
useful question for modern Android is no longer only "how do I call dlopen in
another process?". The harder questions are:
- Which process lifecycle point do you control?
- Which SELinux/domain and ptrace constraints apply?
- Who maps the library: Android's linker or a custom loader?
- How deterministic does the mapped address need to be?
- How will native code cross into ART/JNI/DEX code once it is resident?
The notes below assume an authorized post-exploitation research setting. When
this document says "RCE" in the Android target-entry discussion, read that as
shorthand for a completed chain: native code execution already exists, privilege
escalation to a useful system/root-equivalent context has already happened, and
SELinux/domain restrictions have either been bypassed, changed by policy, or are
otherwise not the blocker being studied. This repository is about what the
loader/injector does after that point.
Short Conclusions
- For a modern zygote or
system_server lifecycle model, study
ReZygisk before old BinderJack-style
examples. Its interesting part is not the module API; it is the early zygote
tracer, custom loader, and hooks around app/system-server specialization.
- For late injection into an already-running, attachable process, study
AndKittyInjector. It is a more
complete modern ptrace-based injector than the older examples listed below.
- For linker namespace and symbol-resolution work after code is already inside
the target process, study xDL.
- For linker-managed placement, do not skip
android_dlextinfo. The
ANDROID_DLEXT_RESERVED_ADDRESS, ANDROID_DLEXT_RESERVED_ADDRESS_HINT, and
ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE flags are the public Android
interface for asking the linker to use caller-provided address space.
- For true custom ELF loading, compare against Chromium/NDK Crazy Linker and
AOSP bionic's linker. For in-memory linker-mediated loading, compare against
LibcoreSyscall's
DlExtLibraryLoader.
- For native or ART hooks after residency, study
ShadowHook,
ByteHook, and
LSPlant. These are not process injectors
by themselves.
- Treat injectvm-binderjack
and simple ptrace examples as historical references. They are useful for
understanding the idea, but they do not model modern Android linker namespaces,
zygote timing, ART changes, or current policy boundaries well enough.
Mental Model
Think about process injection as a chain of separate problems:
- Target entry: how execution first crosses into the target process.
Common families are late ptrace attach, early zygote tracing, preload/module
frameworks, or already-running in-process code.
- Remote memory: how scratch memory, file bytes, and final executable
mappings appear in the target address space.
- ELF loading: whether Android's linker performs
dlopen /
android_dlopen_ext, or whether a custom loader maps PT_LOAD segments and
applies relocations itself.
- Symbol discovery: how addresses are located in the target, usually from
/proc/<pid>/maps, ELF parsing, local-to-remote offsets for the same mapped
object, linker metadata, or ART/runtime exports.
- Execution handoff: whether the injected library starts through an
exported function,
JNI_OnLoad, a zygote module callback, a JNI native-method
replacement, or an ART hook.
- Managed-code bridge: native residency is not enough for DEX execution.
DEX bytes still need an ART-visible loading path, a recoverable
JavaVM *,
a current-thread JNIEnv *, a class loader context, and compatibility with
hidden API and process lifecycle restrictions.
Memory Mapping and Placement
The address where an injected shared library lands is determined by the loading
path, not just by the fact that a remote call happened.
System linker path
When the target process executes dlopen or android_dlopen_ext, Android's
linker owns the final mapping choices, relocation processing, dependency
resolution, and constructor calls. A memfd-backed load changes the backing object
and avoids a normal filesystem path, but it does not by itself make the final
base address deterministic. ASLR, existing VMAs, linker namespace rules, and
architecture-specific loader behavior still matter.
This is the path used by many late injectors. For example, AndKittyInjector
resolves remote dlopen / __loader_dlopen and can use memfd plus
ANDROID_DLEXT_USE_LIBRARY_FD through android_dlopen_ext.
android_dlextinfo reserved-address path
android_dlopen_ext accepts Android-specific loading options through
android_dlextinfo. This is the important middle ground between "let the linker
choose everything" and "write a full custom ELF loader".
The placement-relevant fields are reserved_addr and reserved_size; they only
matter when paired with the reserved-address flags below.
This means a remote mmap call can matter to final library placement only if
the reserved region it creates is then handed to the target process linker via
android_dlopen_ext and android_dlextinfo. A scratch mmap used only for
strings, temporary buffers, or RPC arguments does not constrain where dlopen
maps the final shared object.
Injector process versus target process
Actions in the injector process do not directly reserve memory, change linker
state, or choose load addresses in the target process. Android processes have
separate virtual address spaces and separate dynamic-linker state. A local
dlopen / android_dlopen_ext experiment can help identify ABI, API-level
behavior, ELF size, dependency closure, SONAME, relocation style, and symbol
offsets for the same on-disk library, but it does not make the target process
pick the same base address.
The useful relationship is indirect:
- Static metadata can be learned locally. Segment spans, alignment,
DT_NEEDED dependencies, exported symbol offsets, and RELRO size are
properties of the ELF files. Those values can help estimate how large a
target-side reservation would need to be.
- Absolute addresses are process-local. A
dlsym result in the injector is
not a target address. At most, if the exact same library build is mapped in
both processes, the symbol offset relative to that library's load bias can be
reused after finding the target's own mapping.
- Post-fork local loads do not propagate. Loading a library inside an app
process after it forked from zygote affects that process only.
- Pre-fork zygote state can propagate. Mappings and reserved holes created
in zygote before fork can be inherited by app processes and
system_server.
WebView's reserved-address/RELRO design relies on this kind of controlled
lifecycle, not on a random injector process influencing another process after
the fact.
- Shared RELRO is explicit. A RELRO file created in one process is useful to
another only when the library and relevant dependencies are loaded at the same
addresses and the target load uses the matching
android_dlextinfo flags.
So the rule is: local actions can inform a target-side loading plan, and
pre-fork zygote actions can shape descendants, but target layout is affected
only by target-side mappings, inherited mappings, and the android_dlopen_ext
arguments actually used by the target process.
Other android_dlextinfo flags solve adjacent problems. Read them as linker
policy inputs, not as a generic remote-memory API:
| Flag |
What it changes |
Important caveat |
ANDROID_DLEXT_RESERVED_ADDRESS |
Requires the linker to load into the caller-supplied reserved range. |
The range must already exist in the target process and must be large enough, or loading fails. |
ANDROID_DLEXT_RESERVED_ADDRESS_HINT |
Treats the caller-supplied range as preferred placement. |
If the range is unsuitable, the linker can ignore the hint and choose another address. |
ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE |
Extends reserved-address and RELRO behavior to newly loaded dependencies. |
reserved_size must cover the main library plus new dependencies. Existing already-loaded dependencies are not included, so reproducibility depends on the target's prior load state. |
ANDROID_DLEXT_USE_LIBRARY_FD |
Reads the library from library_fd instead of opening it by path. |
The filename argument still identifies the library to the linker. |
ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET |
Starts reading from library_fd_offset. |
Only valid together with ANDROID_DLEXT_USE_LIBRARY_FD; useful for embedded libraries such as uncompressed ZIP entries. |
ANDROID_DLEXT_FORCE_LOAD |
Avoids the normal already-loaded check based on stat(2). |
It can load another file with the same filename, but dependency resolution can still prefer the first library when DT_SONAME overlaps. It is not a placement control. |
ANDROID_DLEXT_USE_NAMESPACE |
Requests loading in library_namespace. |
Marked internal in the public docs because the NDK does not expose a namespace API. |
ANDROID_DLEXT_WRITE_RELRO |
Writes relocated GNU RELRO pages to relro_fd. |
Useful only when another process can later map the same library at the same address. Implies ANDROID_DLEXT_USE_RELRO. |
ANDROID_DLEXT_USE_RELRO |
Reuses identical relocated RELRO pages from relro_fd. |
Mostly useful for WebView-style shared-library layouts where address placement and dependency state are controlled. |
The recursive reserved-address mode is the closest public linker feature to
deterministic multi-library placement: newly loaded dependencies are placed
consecutively from reserved_addr in DT_NEEDED order. That determinism is
conditional. It depends on the reserved region being large enough and on the set
of dependencies that were not already loaded being stable.
RELRO sharing is not placement
ANDROID_DLEXT_WRITE_RELRO and ANDROID_DLEXT_USE_RELRO are often seen next to
reserved-address loading, but they solve a different problem. WRITE_RELRO
serializes the already-relocated GNU RELRO pages to relro_fd. USE_RELRO
later compares the target process's already-relocated RELRO pages with that file
and maps file-backed pages over the matching page ranges.
That means RELRO sharing depends on layout sameness; it does not create layout
sameness. If the library or its newly loaded dependencies land at different base
addresses, many RELRO pages can differ because they contain relocated absolute
pointers. The linker can then skip the non-matching pages and still complete the
load. So USE_RELRO by itself is not a strict deterministic-placement check and
not a way to force the linker to choose the original address.
The deterministic WebView-style pattern is the combination:
- reserve a sufficiently large address range at a controlled lifecycle point;
- load once with
ANDROID_DLEXT_RESERVED_ADDRESS,
ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE, and ANDROID_DLEXT_WRITE_RELRO;
- load later with the same reserved range and
ANDROID_DLEXT_USE_RELRO;
- keep the set and order of newly loaded dependencies consistent.
In that pattern, reserved-address loading is the placement mechanism and RELRO is
the memory-sharing/result-reuse mechanism. RELRO can reveal partial sameness
because identical pages are remapped from the file, but it should not be treated
as the thing that makes the layout deterministic.
Choosing a reserved range
There is no universal "close to 100%" fixed start address for a late arbitrary
target. The reliable primitive is not guessing a magic hole; it is creating the
reservation in the target address space and then giving the exact returned range
to the target linker.
For one already-running target process, the most reliable model is:
- create a target-side anonymous
PROT_NONE reservation large enough for the
intended linker-managed load;
- let the target kernel choose the address rather than hard-coding one;
- keep that reservation alive until
android_dlopen_ext consumes it through
reserved_addr / reserved_size;
- treat the returned address as valid only for that target process and that
moment in time.
That can make the range "available now" in the target, but it does not make the
address deterministic across different processes or launches. If the requirement
is the same address across a family of processes, the stronger design is
pre-fork reservation in zygote or another controlled lifecycle point. That is
why WebView reserves in zygote instead of trying to discover a perfect hole
after every app process already exists.
Picking a fixed address by reading /proc/<pid>/maps