This is a set of bash scripts and Magisk module for patching system JAR
files and SO
files of Android.
Magisk module does following things:
- substitutes JARs in
/system/framework
dir - remounts folders in
/apex/
to be writeable (why? readAPEX
section) - substitutes JARs in
/apex/
(temporarily or permanently)
Sometimes tools like Frida or XPosed/LSPosed got detected by app under research, so we need to use some uncommon techniques to bypass detection. One of such technique is patching system libraries (.jar
files, .so
files) to execute code from them as soon as app load them into memory.
- Download latest apktool.jar and put it in this folder
- Choose JAR file you want to modify on your Android from
/system/framework
- Download JAR from device (via ADB) and put it in this folder under any name (for example
my.jar
) - Modify variables in
*.sh
files according to comments on top - Run
./jar_to_smali.sh my.jar
, after run you will getmy.jar.smali
folder - Modify SMALI files in
my.jar.smali
as you wish - Run
smali_to_jar.sh my.jar
to build JAR from updated SMALI files (updated JAR will be located inmagisk_module/system/framework/my.jar
) - Build Magisk module for replacing original JARs:
./build_magisk_module.sh
- Push magisk module (
jarpatcher.zip
) to device via ADB, install magisk module and reboot device to apply changes:adb push jarpatcher.zip /sdcard/ && adb shell su -c "magisk --install-module /sdcard/jarpatcher.zip" && adb reboot
- if you modified JAR from APEX(
/apex/
), seeHow to replace JAR in APEX?
section below
- if you modified JAR from APEX(
- Enjoy modified JAR! Or not if you caught a boot loop, then here is how to easily fix it
This repo contains most useful patches (patches/
) for reverse-engineering which you can apply to your device:
sslunpinning
- makes global unpinning in system for all apps on Java level which using Conscrypt library (almost 80% of apps).set_webview_debuggable
- enables DevTools for WebViews systemwide so you can debug JavaScript in them from Desktop viachrome://inspect/#devices
hide_debug_mode
- hides USB Debugging enabled in Developer options, so apps will not be able to detect it
- Do steps 1-5 from How to use? section for the JAR library which you want to patch (each file contains library name and file name).
- Just run the patch you interested in! For example:
bash ./patches/ssl_unpinning.sh
It is very easy to write your own patches! Just copy&paste any file from patches/
and see its code, the code is completely self-expanatory. "Patch-engine" is 100% shell-based, the only dependency needed is PERL (preinstalled almost in all Linux-based systems including MacOS).
Each patch consist of following parts:
PATCH_IDENTITY
- any unique name of patch to locate it in SMALI code (currently used MD5 hashes, but can be any human-readable name)LIB
- JAR library name which patch will be applied to (after using same name in./jar_to_smali.sh
script)FILE
- class path to patched file in JAR libraryREGEX
- PERL-based regex to locate place in SMALI code which must be patched. To escape SMALI code, you can use this online toolPATCH
- SMALI code which will be added
Well, most of interesting JARs since Android 10 are distributed in APEX format, which contains .img
with .jar
and .so
libraries. Best explanation of APEX format you may find here. As far as we want to be able to modify all system libraries, we have to be able to modify libraries provided by APEXes as well.
For example, default Java classes like java.lang.String
or java.net.URL
provided to all Android apps via /apex/com.android.art/javalib/core-oj.jar
library.
Native utility for interaction with APEX - apexd
We can interact with APEXes manually via apexd
binary available on every modern Android:
stop
(stop zygote and all apps which are using files from/apex/*
)apexd --unmount-all
(unmount all apex filders)apexd --otachroot-bootstrap
(mounts all apexes back)start
(start zygote with user space)
By default, Magisk module from this project contains service.sh
which remounts some of most interesting APEXes after file system got initialized. This lets you to do substitute JAR and SO libraries in runtime like this:
stop
(stop zygote and all apps which are using files from/apex/*
)cp /sdcard/patched-core-oj.jar /apex/com.android.art/javalib/core-oj.jar
(patch)start
(start zygote with user space)
After system boots, patched version of core-oj.jar
will be loaded in all apps.
If you need to modify some other APEXes, edit service.sh
(see REMOUNTING APEXES
comment).
IMPORTANT! Patching like this will not make permanent changes for APEXes, after reboot you will have to repeat this process again. If you want changes to be permanent after each system reboot, see PERMANENT CHANGES IN APEXES
comment in service.sh
and rebuild Magisk module.
- Enable Magisk Hide ("magisk modifications are reverted for processes on hidelist") and install PlayIntegrityFix module to stay stealth.
- Add researchable app to Magisk Hide list
- Flash TWRP into recovery partition
- Boot TWRP
- In TWRP go to
Advanced -> File Manager
- Remove Magisk module folder
/data/adb/modules/jarpatcher
- Reboot to system