( بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيمِ )
CAUTION#FreePalastine
Its all about the hook :”)
TL;DR - I Don’t Have Time for ThisA 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:
| Check | What it looks at | Why a normal hook fails it |
|---|---|---|
| CRC or hash of the code | The library’s bytes in memory | The bytes changed |
Reading memory another way (/proc/self/mem) | The same bytes | Still changed |
/proc/self/smaps | Private_Dirty for each mapping | Writing to a code page makes a private copy, so it turns dirty |
/proc/self/maps | Permissions of each mapping | Hooking 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 codeSo:
- 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>/mapsstay 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 pageIMPORTANTThe 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:
| Function | Offset in the library | Role |
|---|---|---|
get_secret_value(x) | 0x6D000 | Returns x * 2. So secretValue(100) is 200. The target. |
calculate_crc32 and friends | 0x6C000 and up | Checks 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:
- Get the baseline CRC32 of the library’s code (more on this in a second).
- Compute the CRC32 of the same code right now.
- Compare the two: equal means PASS.
- Call
get_secret_value(100)and show the result. - 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 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:
- Find its own code. It reads
/proc/self/mapsand picks thelibcrcdemo.soline with execute permission. On my phone that was 950272 bytes. - 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.
- Take the current CRC, every time. Each press of Verify computes the CRC32 over the same memory again.
- 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 compare | Read from | When |
|---|---|---|
| Baseline | The library’s code in memory | Once, at the first check |
| Current | The same memory | Every 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.sofile 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: 0xC95B66FAThe app’s baseline is 0x4EFD1654. Clean.
WARNINGThe 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 // c0035fd6The 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 VerifyWhat the app showed:
| Before the patch | Shadow patch live | After --release | |
|---|---|---|---|
| Baseline CRC32 | 0x4EFD1654 | 0x4EFD1654 | 0x4EFD1654 |
| CRC32 right now | 0x4EFD1654 | 0x4EFD1654 | 0x4EFD1654 |
| Result | PASS | PASS | PASS |
secretValue(100) | 200 | 9 | 200 |
8 bytes at the target, read by root through /proc/<pid>/mem | ff4300d1e00f00b9 | ff4300d1e00f00b9 | ff4300d1e00f00b9 |
Private_Dirty of the code mapping | 0 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.

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=714026e000Line by line:
pfn 8d86e1->938399: the two physical pages.8d86e1is the original,938399is 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:
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 write | wxshadow | |
|---|---|---|
| Patched code runs | Yes, returns 9 | Yes, returns 9 |
| CRC32 right now | 0xC95B66FA, FAIL | 0x4EFD1654, PASS |
Bytes read through /proc/<pid>/mem | 200180d2c0035fd6 (the patch) | ff4300d1e00f00b9 (the original) |
Private_Dirty | 4 kB | 0 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 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/adrpto 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.
CAUTIONThe module’s
prctlinterface 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
| Tool | What it did |
|---|---|
| wxshadow (KPM) | Keeps the two pages and swaps the PTE on every fault. The star of the show. |
| wxshadow_client | Sends the --patch and --release commands. |
| KernelSU Next + KPatch-Next | Root, and the loader for the .kpm module. |
| crcdemo | The demo app: a target to patch and a guard that watches it. |
adb + dmesg | Ran 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
- kkkbbb/mkpms: wxshadow source, client and demo app
- The author’s write-up on Kanxue (Chinese)
- The compat branch that works on kernel 6.1
- Upstream issue #7, which pointed to the compat branch
- KernelSU-Next/KPatch-Next
This was the end. May your hooks stay hidden and your pages stay clean :“D
