type
status
date
slug
summary
tags
category
icon
password
 
 

一、源码

Magisk
topjohnwuUpdated Jun 30, 2024
本文对 V26.1版本 进行编译
编译环境:Ubuntu 22.04 (WSL) (建议使用Linux环境进行编译,Windows环境坑太多)
 

二、Magisk编译

 

1. 准备编译中所需要的工具

  • Python3.8+
  • Git
⚠️
注意需要开启GitEnable symbolic links 选项,因为在编译过程中需要使用到符号链接,否则会失败。 在Windows Git安装过程中有一步可以选择开启符号链接。 在Linux下可以使用git config --global core.symlinks true 进行开启
 

2. 下载Android Studio设置环境变量

因为构建APP需要JDK17支持,所以为了方便官方推荐下载Android Studio,然后设置系统环境变量ANDROID_STUDIO为安装路径可进行编译,否则无法构建APP。
notion image
然后需要设置ANDROID_SDK_ROOT环境变量,该目录位置可以在AndroidStudio的设置中SDK中找到。
 

3. 拉取Magisk

git clone --recurse-submodules https://github.com/topjohnwu/Magisk.git
⚠️
这里注意必须要使用--recurse-submodules 选项拉取Magisk编译过程中需要的子模块,否则编译无法进行。 在下载过程中可能会遇到网络问题导致拉取不完整,建议使用代理进行下载或使用流量进行下载。如果拉取不完整,需要手动进行下载并且将对应子模块放置于native/src/external目录下对应子模块中并执行git submodule update --init --recursive
 

4. 下载NDK

在拉取下来的Magisk目录存在build.py 文件,该文件为编译脚本,使用python3 build.py ndk 即可下载官方NDK工具链,只不过这个和常规NDK不一样,Magisk使用的工具链为ONDK
ondk
topjohnwuUpdated Jun 26, 2024
,即Oxidized NDK (ONDK) is an unofficial repackaged Android NDK that includes a Rust toolchain. 下载完成后无需进行其他操作,但是前提是前置环境变量等设置正确。
 

5. 开始编译

在这里可以使用python build.py all直接进行编译,将会编译三大部分。编译需要较长时间,耐心等待即可。
  • 第一部分为Native工具,包括 magisk magiskinit busybox magiskboot
  • 第二部分编译APK,分为两个APK,一个是app-debug(release).apk,其实就是Magisk本体安装程序,将其后缀改为zip即可通过REC刷入。另一个是stub-debug(release).apk 即Magisk代理程序,即在隐藏Magisk后会创建的空壳APP,其作用就是拿到完整Magisk APK并将其加载到内存中进行运行以防止检测。
    • When hiding the Magisk app, it will install a “stub” APK that has nothing in it. The only functionality this stub app has is downloading the full Magisk app APK into its internal storage and dynamically loading it. Due to the fact that the APK is literally empty, it does not contain the image resource for the app icon.
💡
如果想通过AndroidStudio进行代码分析可以参考Building and Development | Magisk (topjohnwu.github.io)中的IDE Support一节
 

6. 编译完成进行安装

可以直接安装编译输出的app-debug(release).apk ,然后在内部选择打补丁(前提是已经解锁),打完补丁后fastboot刷入补丁后的boot.img。也可以直接REC刷入ZIP包,前提是第三方REC,否则刷不进去。如果是debug版本,安装完成就是D版本,调试信息会很详细。
notion image
 
Magisk V26.1源码分析XCTF-Final Flappy-Bird-Cheat题目复现
  • Twikoo