Project page
You can find both scripts here: cloaked-hooker.js and cloaked-hooker-pro.js

Malware that cleans up after itself Link to heading

Android malware has learned a neat trick. The APK you install looks almost innocent: a bit of Java, nothing too alarming. But buried in lib/ there’s a native library, and when it runs, it drops a second payload to disk (a DEX, another .so, sometimes a whole APK), loads it, and then deletes it. By the time you go looking for it, the file is gone, and all that’s left is a process doing things you can’t explain.

Native code is also where developers go when they really don’t want you reading something. Decompiling Java is easy; reading stripped ARM assembly is not. So the interesting bits (string decryption, C2 addresses, anti-analysis checks) tend to live there.

You could open the library in Ghidra and start reversing it function by function. Or you could just watch it. That’s what the Cloaked Hooker does: it’s a Frida script that hooks everything a native library uses to talk to the outside world (JNI, libc, the network, crypto), logs it all in a readable tree, and, the “cloaked” part, quietly refuses to let it delete anything.

Aim before you shoot Link to heading

The first thing you’ll see at the top of the script is this:

const TARGET_LIBRARIES = [];

Fill it in with the name (or part of the name) of the library you care about, like ["libpayload"].

This matters more than it looks. Functions like strcmp, open or send are called thousands of times per second by everything in the process, from the Android framework to the WebView. Logging all of them would be useless. So every hook checks who’s calling: Frida gives you the return address of each call, and the script only logs it if that address falls inside one of the target libraries. Everything else is ignored, as early as possible, to keep the overhead down.

An empty list hooks nothing
Most of the hooks (JNI, libc, network, crypto) only fire for libraries in TARGET_LIBRARIES. If you leave it empty, you’ll only get the asset and Java hooks. Set it first.

Each log entry tells you which library made the call and the exact offset inside it, like [JNI @ [libpayload.so + 0x1a2b4]], so when something interesting shows up, you can jump straight to that address in Ghidra or radare2.

Catching the library at birth Link to heading

Hooking a library that’s already running is easy. The problem is that a lot of the interesting stuff happens the moment it loads, in its constructors (.init_array) and in JNI_OnLoad. If you hook after that, you’ve already missed it.

So the script hooks the Android dynamic linker itself (linker64), in two places:

  1. do_dlopen, which is called with the path of every library being loaded. If it matches one of the targets, the library is marked as “pending”.
  2. call_constructor, which runs right before the library’s constructors. At this point the library is already mapped in memory, so the script knows its base address and size, and installs all the hooks just in time.

The result: you see everything the library does from its very first instruction.

Reading the JNI conversation Link to heading

Native code can’t do much on Android by itself. To touch anything in the Java world (call a method, read a field, create a string) it has to go through JNI, using the function table inside JNIEnv. That makes JNI the perfect wiretap: whatever the native code wants from Java, it has to ask for it out loud.

The script grabs the JNIEnv table and hooks the functions that tell the story:

  • RegisterNatives: lists every Java native method the library registers, with its signature and the real address of the function that implements it. This alone saves hours, because it’s how obfuscated libraries hide which native function does what.
  • FindClass, GetMethodID, GetFieldID (and their static versions): which classes, methods and fields the native code is looking up.
  • Call*Method (all ~40 variants): which methods it actually calls.
  • NewStringUTF, GetStringUTFChars: every string going in and out. If the library decrypts its strings at runtime, this is where they show up in plaintext.
  • SetByteArrayRegion: byte arrays being filled from native memory, with a hex dump.

There’s a nice touch here: jmethodID and jfieldID are just opaque pointers, so a log full of CallObjectMethod(0x7a3c...) would tell you nothing. The script remembers which name and signature each ID came from when it was looked up, so when it’s used later, the log shows Resolved: getDeviceId()Ljava/lang/String; instead.

On the libc side, it watches open, mkdir, strcmp and strlen calls coming from the target library. strcmp in particular is a goldmine: it’s how native code checks for root, emulators, Frida, or a license key. Every comparison shows both strings and the result. To keep the noise down, strings that look like Java/Android class names are filtered out.

It also hooks the native AAssetManager API, so you can see which files from the APK’s assets/ folder the native code opens, with a hex preview of the first bytes. Encrypted payloads love hiding in assets.

The cloak: no deleting allowed Link to heading

Here’s the part that gives the script its name. Remember the malware that drops a payload and deletes it? The script replaces unlinkat (the libc function behind file deletion) with its own version:

Interceptor.replace(unlinkatPtr, new NativeCallback(function (dirfd, pathnamePtr, flags) {
    const path = pathnamePtr.isNull() ? "" : pathnamePtr.readUtf8String();
    const inTarget = inRange(this.returnAddress);

    const isSqliteFile = path.includes("/databases/") ||
                         path.endsWith("-journal") ||
                         path.endsWith("-wal") ||
                         path.endsWith("-shm") ||
                         path.endsWith(".bak");

    if (isSqliteFile || !inTarget) {
        return origUnlinkat(dirfd, pathnamePtr, flags);
    }

    Log.warn("CLOAK", `BLOCKED unlinkat! (FS-DROP) | dirfd: ${dirfd} | Path: ${path}`);
    return 0;
}, 'int', ['int', 'pointer', 'int']));

When the target library tries to delete a file, the script says “sure, done!” (returns 0, success) and does absolutely nothing. The malware happily carries on, convinced it covered its tracks, while the payload sits right there on disk for you to adb pull.

The same goes for the Java side: File.delete() is hooked to return false without deleting anything.

Why spare the databases?
SQLite constantly creates and deletes its own temporary files (-journal, -wal, -shm). Blocking those would break every database in the app, and a crashing app tells you nothing. So those are always allowed through.

And while it’s at it, the script also hooks the Java class loaders (BaseDexClassLoader, InMemoryDexClassLoader and DexFile), so you get a log entry the moment any DEX is loaded at runtime, whether from a file or straight from memory.

Network and crypto Link to heading

Finally, the network and crypto hooks:

  • DNS: getaddrinfo and gethostbyname, so you see every hostname the library resolves.
  • Sockets: connect, send/sendto and recv/recvfrom, with the destination IP and port and an ASCII dump of the data. This catches traffic that a proxy can’t see, like raw TCP or custom protocols.
  • OpenSSL/BoringSSL: EVP_CipherInit_ex gives you the key and IV and whether it’s encrypting or decrypting, and the Update/Final calls show the data going in and coming out. In other words, you get the plaintext without having to break anything.
  • Java crypto: Cipher.doFinal, with the algorithm, input and output.

Going Pro Link to heading

The base script is meant to stay focused on the native library. Cloaked Hooker Pro builds on it and casts a much wider net:

File dumping Link to heading

The Pro version keeps track of every file the target library opens. When the file is closed, it copies the whole thing to /data/local/tmp/dump_frida/. On top of that, it looks at every buffer passing through read and write, and if it starts with the magic bytes of an ELF, DEX or ZIP, it saves that buffer too. So even if a payload is decrypted in memory and written straight to disk, you get a copy.

More JNI and libc Link to heading

  • DefineClass: native code defining a Java class straight from a byte buffer. That’s dynamic code loading, flagged with a big warning in the log.
  • GetByteArrayElements and GetDirectBufferAddress: native code reading Java buffers.
  • strncmp and strstr: more string checks.

Intents and IPC Link to heading

A big chunk of the Pro version is about how the app talks to the rest of the system:

  • startActivity, every flavour of sendBroadcast, and registerReceiver (including the actions and schemes being listened for).
  • Every Intent being built or modified (setAction, setData, setClassName, putExtra…) and every extra being read.
  • PendingIntent creation, a favourite of malware abusing notifications and accessibility.

Activity launches, broadcasts and PendingIntents also come with a Java backtrace by default, so you know exactly which class triggered them.

Reflection Link to heading

Class.forName, getMethod, getDeclaredMethod and Method.invoke are hooked, so you can see the app calling methods by name to hide them from static analysis. The Android and Java framework classes are filtered out, so you only see the app’s own calls.

Network detection Link to heading

Set ENABLE_NETWORK_HOOKS = true and the Pro version also logs every URL and connection the whole app makes, not just the target library: URL/URI, InetAddress, raw sockets, HttpURLConnection, OkHttp, WebView, gRPC and Cronet on the Java side, and DNS, connect, sendto and plain HTTP send on the native side, each with a Java or native backtrace.

That gets loud fast, so it comes with some filtering:

  • FILTER_LEVEL drops known noise, like Google and Facebook endpoints (level 1) and analytics (level 2).
  • FILTER_PROXIES hides the traffic going to your own interception proxy (like ZAP or HTTP Toolkit), so you don’t drown in your own tooling.
  • Identical URLs logged within 150ms of each other are deduplicated.

Using it Link to heading

You’ll need a rooted device (or emulator) running frida-server. Open the script, set your TARGET_LIBRARIES, and spawn the app with it:

# The base version
frida -U -f com.example.app -l cloaked-hooker.js

# Or the Pro version
frida -U -f com.example.app -l cloaked-hooker-pro.js

Spawning (-f) instead of attaching matters here: the whole point is to be there before the library loads.

When you’re done, grab whatever the Pro version dumped, along with any files the malware “deleted”:

adb pull /data/local/tmp/dump_frida/
You're running malware!
These scripts don’t stop the app from doing anything, except deleting files. Whatever it was going to do, it will do, including talking to its C2 server. Run it on an isolated device or emulator that you don’t care about, never on your own phone.

Conclusion Link to heading

Static analysis tells you what code could do. Hooking tells you what it actually does, with real arguments, real strings and real keys. And sometimes the most useful thing you can do to a piece of malware isn’t to break its encryption or reverse its obfuscation, but simply to lie to it: tell it the file is gone, and let it walk away thinking it’s safe.

Happy hunting!