1819 words
9 minutes
Shadow Hooking: Hiding a Hook Inside the Kernel

( بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيمِ )

CAUTION

#FreePalastine

Its all about the hook :”)#

TL;DR - I Don’t Have Time for This

A hook changes code. Changed code can be read, hashed and caught. Shadow hooking puts two physical pages behind one virtual address: one the CPU executes (with your patch) and one everyone else reads (the clean original). The app runs your patch and its own integrity check still says PASS.

I tested this on my own Pixel 6a, with a small demo app built by the author of the technique. Everything in this post comes from that phone: real screenshots, real kernel logs, real numbers. Where I did not test something, I say so.


A hook is a change, and changes can be seen#

To hook a native function, you overwrite its first instructions so they jump to your code, or just return what you want. Easy. The problem is that the app can look at its own code, and the code is now different.

Protection code inside apps (RASP) checks for that in a few ways:

CheckWhat it looks atWhy a normal hook fails it
CRC or hash of the codeThe library’s bytes in memoryThe bytes changed
Reading memory another way (/proc/self/mem)The same bytesStill changed
/proc/self/smapsPrivate_Dirty for each mappingWriting to a code page makes a private copy, so it turns dirty
/proc/self/mapsPermissions of each mappingHooking tools love leaving rwx mappings around

So the question is not “can I patch the code”. You can. The question is: can the CPU run my patch while anyone who reads the code sees the original?


The trick: one address, two pages#

Some background first. Your app sees virtual addresses. The kernel maps each 4 KB virtual page to a physical page through a page table entry (PTE). The PTE also holds the permissions for that page.

On arm64 a page can be execute-only: the CPU can fetch instructions from it, but a normal data load from it faults. That one feature is the whole trick.

wxshadow, a kernel module by kkkbbb, uses it like this:

  • It makes a copy of the code page and puts your patch in the copy. This is the shadow page. It is mapped execute-only (--x).
  • The original page is never touched. When it is mapped, it is read-only (r--).
  • Only one of them sits in the PTE at a time. When the wrong kind of access hits, the CPU faults, and the kernel swaps the PTE to the other page.
                         +--> instruction fetch --> SHADOW page   (--x)  your patch
virtual address --> PTE -+
                         +--> data read ---------> ORIGINAL page (r--)  clean code

So:

  • The CPU runs the patched code.
  • Every read gets the original bytes: the app’s CRC, /proc/self/mem, even root reading the process from outside.
  • The original page is never written, so it never turns dirty.
  • Nobody called mprotect, so the permissions in /proc/<pid>/maps stay the same.

It is the kernel doing a magic trick where the card you look at is never the card in play.

How you drive it#

wxshadow is a KPM, a module for KernelPatch, which you load at run time on a rooted phone. You talk to it through prctl with private command numbers. The author ships a small client for that, wxshadow_client. The two commands I used:

wxshadow_client -p <pid> -a <address> --patch <hex bytes>   # put bytes in a shadow page
wxshadow_client -p <pid> -a <address> --release             # drop the shadow page
IMPORTANT

The official v1.1 build of the module did not work on my kernel (6.1). A compat branch in the same upstream repo did. Link at the end.


The demo app, before we break anything#

The author also ships a demo app, crcdemo. It is tiny, which is what makes it a good teacher. The original is in Chinese and its buttons sit under the navigation bar, so I rebuilt only the screen: English text, readable colours, buttons you can actually press. The native library inside is byte-for-byte the same, so nothing about the mechanism changed.

The app is one screen on top of one native library, libcrcdemo.so. That library has two jobs:

FunctionOffset in the libraryRole
get_secret_value(x)0x6D000Returns x * 2. So secretValue(100) is 200. The target.
calculate_crc32 and friends0x6C000 and upChecks the library’s own code. The guard.

Keep those two words in mind. We hook the target. We never touch the guard. The guard’s only job is to notice that the target changed.

What happens when you press Verify#

One routine does all the work. It runs once when the app starts and again every time you press Verify:

  1. Get the baseline CRC32 of the library’s code (more on this in a second).
  2. Compute the CRC32 of the same code right now.
  3. Compare the two: equal means PASS.
  4. Call get_secret_value(100) and show the result.
  5. Print everything on screen.

The other three buttons only copy an address to the clipboard. They are there so you know where to aim.

With no patch, it looks like this:

The demo app with no patch: CRC matches, secretValue(100) is 200


The CRC check, and what it really compares#

This part confused me at first, so let’s go slow.

The guard never reads the .so file on disk. Both sides of the compare come from memory:

  1. Find its own code. It reads /proc/self/maps and picks the libcrcdemo.so line with execute permission. On my phone that was 950272 bytes.
  2. Take a baseline, once. The first time the check runs (at app start), it computes a CRC32 over those 950272 bytes and stores it. A flag makes sure this only ever happens once.
  3. Take the current CRC, every time. Each press of Verify computes the CRC32 over the same memory again.
  4. Compare. PASS if current == baseline.

The CRC32 itself is the boring standard one. It reads the code one byte at a time with normal loads. Remember “normal loads”. That detail is what we are about to abuse.

End of the compareRead fromWhen
BaselineThe library’s code in memoryOnce, at the first check
CurrentThe same memoryEvery check

Is the baseline actually clean?#

Fair question. If the baseline came from memory, what if the memory was already patched?

In my run it was clean, and I can prove it two ways:

  • Order. The baseline was taken at app start, before the wxshadow module was even loaded.
  • It matches the file. I took the same 950272 bytes from the original libcrcdemo.so file on my PC and ran a plain CRC32 on them:
CRC32 of the file bytes, clean:           0x4EFD1654   <- the app's baseline
same bytes with my 8-byte patch put in:   0xC95B66FA

The app’s baseline is 0x4EFD1654. Clean.

WARNING

The app itself does not guarantee this. It trusts whatever is in memory the first time it looks. If someone patched the code with a plain write before the first check, that patch would go straight into the baseline and every later check would happily say PASS. I did not test that case. A stronger check would compare against the file on disk.


Breaking it, on a real phone#

Setup: Pixel 6a, Android 15, kernel 6.1.99-android14, rooted with KernelSU Next, wxshadow loaded with KPatch-Next.

The patch is 8 bytes that turn get_secret_value into “return 9”:

mov x0, #9     // 200180d2
ret            // c0035fd6

The run:

PID=$(adb shell pidof com.example.crcdemo)
BASE=0x7140201000                    # start of libcrcdemo.so code, from /proc/$PID/maps
A=$((BASE + 0x6D000))                # get_secret_value = 0x714026e000

wxshadow_client -p $PID -a $A --patch 200180d2c0035fd6
# press Verify
wxshadow_client -p $PID -a $A --release
# press Verify

What the app showed:

Before the patchShadow patch liveAfter --release
Baseline CRC320x4EFD16540x4EFD16540x4EFD1654
CRC32 right now0x4EFD16540x4EFD16540x4EFD1654
ResultPASSPASSPASS
secretValue(100)2009200
8 bytes at the target, read by root through /proc/<pid>/memff4300d1e00f00b9ff4300d1e00f00b9ff4300d1e00f00b9
Private_Dirty of the code mapping0 kB

The middle column is the whole post. The app ran mov x0, #9 ; ret and got 9. Its own CRC still matched. And root, reading the memory from outside, saw the original bytes.

The app with the shadow patch live: secretValue(100) is 9 and the check still passes

What the kernel did#

dmesg from the same run, while the patch was live:

wxshadow: [patch] new shadow 714026e000+0 (8 bytes) pfn 8d86e1->938399
wxshadow: read fault at 714026e000, switched to live original (r--)
wxshadow: exec fault at 714026e018, switched to shadow (--x)
wxshadow: [release] addr=714026e000

Line by line:

  • pfn 8d86e1->938399: the two physical pages. 8d86e1 is the original, 938399 is the new shadow page.
  • read fault: the guard’s CRC loop hit the patched page with a normal load. The page was execute-only, so the load faulted, and the kernel put the original page in the PTE. The CRC read clean bytes.
  • exec fault: then the app called the target. The page was now read-only, so the instruction fetch faulted, and the kernel put the shadow page back. The CPU ran the patch.

The order matches the Verify routine exactly: CRC first, secret value second. Here is the whole run in one picture:

The full flow: baseline at start, Verify with no patch, shadow patch, Verify with the patch live, and the plain-write control

Same patch, the normal way#

To make sure the guard is not just asleep, I released the shadow patch and wrote the exact same 8 bytes the boring way, straight into /proc/<pid>/mem:

Plain writewxshadow
Patched code runsYes, returns 9Yes, returns 9
CRC32 right now0xC95B66FA, FAIL0x4EFD1654, PASS
Bytes read through /proc/<pid>/mem200180d2c0035fd6 (the patch)ff4300d1e00f00b9 (the original)
Private_Dirty4 kB0 kB

The guard caught the plain write instantly, with the exact CRC I computed on my PC for “file plus patch”. So the guard works. It just never sees the shadow page.

The app after a plain write: the CRC changed and the check fails


The catch: a shadow page cannot read itself#

Nothing is free. One PTE is either --x or r--, never both at the same time. So code running from a shadow page must never load data from its own page. If it does, the load goes to the original page and gets the original bytes, not yours.

In practice that kills a few common habits:

  • No literal pools on the same page.
  • No adr/adrp to data on the same page.
  • No jump tables on the same page.

Every address has to be built from the instruction bits themselves (movz/movk). The upstream README lists the general case as a known defect: one page cannot be read and executed at the same time. I did not reproduce that case on my phone, but the rule shaped everything I built on top of this.

CAUTION

The module’s prctl interface has no permission check in the source. While it is loaded, nothing in the code stops one process from patching another. I did not test a process without root doing it, but I would not leave this loaded on a phone I care about.


Tools I used#

ToolWhat it did
wxshadow (KPM)Keeps the two pages and swaps the PTE on every fault. The star of the show.
wxshadow_clientSends the --patch and --release commands.
KernelSU Next + KPatch-NextRoot, and the loader for the .kpm module.
crcdemoThe demo app: a target to patch and a guard that watches it.
adb + dmesgRan everything on the phone and showed what the kernel did.

Credits#

All credit for wxshadow, its client and the demo app goes to kkkbbb. I only poked at it on my phone and wrote down what happened. Go read the original write-up, it is in Chinese and worth the translate button.

References#

  1. kkkbbb/mkpms: wxshadow source, client and demo app
  2. The author’s write-up on Kanxue (Chinese)
  3. The compat branch that works on kernel 6.1
  4. Upstream issue #7, which pointed to the compat branch
  5. KernelSU-Next/KPatch-Next

This was the end. May your hooks stay hidden and your pages stay clean :“D