Skip to content

Commit 384c649

Browse files
committed
Support extract dex from memory
1 parent 885e8e7 commit 384c649

17 files changed

+124
-58
lines changed

module/build.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ MODULE_LIB_NAME="$(echo "$PLUGIN_TYPE_NAME" | tr '[:upper:]' '[:lower:]')-module
1212
echo VERSION_NAME: $VERSION_NAME
1313
echo VERSION_CODE: $VERSION_CODE
1414
bash ./reset.sh
15-
echo "updateJson=\${updateJson}" >> $MODULE_TEMPLATE/template/magisk_module/module.prop
16-
perl -i -pe 's/(description: moduleDescription,)/$1 \nupdateJson: moduleUpdateJson,/g' $MODULE_TEMPLATE/module/build.gradle
17-
echo org.gradle.java.home="$(java_home || echo "$JAVA_HOME_15")" >> $MODULE_TEMPLATE/gradle.properties
18-
1915
cp -rfv ./src/cpp/* $MODULE_TEMPLATE/module/src/main/cpp/
2016
cp -rfv "$MODULE_GRALDE_FILE" $MODULE_TEMPLATE/module.gradle
2117
cp -rfv "./src/gradle/fingerprint.gradle" $MODULE_TEMPLATE/
@@ -35,9 +31,7 @@ perl -0777 -i -pe 's/(forkAndSpecializePost[\W\w]+?{[\W\w]*?)}/$1 fingerprin
3531
perl -0777 -i -pe 's/(specializeAppProcessPre[\W\w]+?{[\W\w]+?)}/$1 fingerprintPre(env, appDataDir, niceName);\n}/' $MODULE_TEMPLATE/module/src/main/cpp/main.cpp
3632
perl -0777 -i -pe 's/(specializeAppProcessPost[\W\w]+?{[\W\w]+?)}/$1 fingerprintPost(env, MAGISK_MODULE_TYPE_RIRU);\n}/' $MODULE_TEMPLATE/module/src/main/cpp/main.cpp
3733
perl -0777 -i -pe 's/^/#include "fingerprint.h"\n/' $MODULE_TEMPLATE/module/src/main/cpp/main.cpp
38-
perl -i -pe 's/(main\.cpp)/$1 fingerprint.cpp zygisk_main.cpp/g' $MODULE_TEMPLATE/module/src/main/cpp/CMakeLists.txt
39-
echo 'add_definitions(-DMODULE_NAME="${MODULE_NAME}")' >> $MODULE_TEMPLATE/module/src/main/cpp/CMakeLists.txt
40-
echo 'target_link_libraries(${MODULE_NAME})' >> $MODULE_TEMPLATE/module/src/main/cpp/CMakeLists.txt
34+
perl -i -pe 's/(main\.cpp)/$1 fingerprint.cpp zygisk_main.cpp resource_extractor.cpp/g' $MODULE_TEMPLATE/module/src/main/cpp/CMakeLists.txt
4135
$MODULE_TEMPLATE/gradlew -p $MODULE_TEMPLATE clean \
4236
-PVERSION_NAME=$VERSION_NAME \
4337
-PVERSION_CODE=$VERSION_CODE \

module/flash.sh

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
set -e
33
cd ${0%/*}
44

5-
bash ./build.sh :module:flashRelease ./src/gradle/qq.gradle Zygisk
6-
#bash ./build.sh :module:flashRelease ./src/gradle/qq.gradle Riru
7-
bash ./build.sh :module:flashRelease ./src/gradle/alipay.gradle Zygisk
8-
#bash ./build.sh :module:flashRelease ./src/gradle/alipay.gradle Riru
9-
bash ./build.sh :module:flashRelease ./src/gradle/taobao.gradle Zygisk
10-
#bash ./build.sh :module:flashRelease ./src/gradle/taobao.gradle Riru
11-
bash ./build.sh :module:flashRelease ./src/gradle/wechat.gradle Zygisk
12-
#bash ./build.sh :module:flashRelease ./src/gradle/wechat.gradle Riru
13-
bash ./build.sh :module:flashRelease ./src/gradle/unionpay.gradle Zygisk
14-
#bash ./build.sh :module:flashRelease ./src/gradle/unionpay.gradle Riru
15-
adb shell "rm -f /data/local/tmp/lib*.debug.dex"
5+
# bash ./build.sh :module:flashDebug ./src/gradle/qq.gradle Riru
6+
# bash ./build.sh :module:flashDebug ./src/gradle/alipay.gradle Riru
7+
# bash ./build.sh :module:flashDebug ./src/gradle/taobao.gradle Riru
8+
# bash ./build.sh :module:flashDebug ./src/gradle/wechat.gradle Riru
9+
# bash ./build.sh :module:flashDebug ./src/gradle/unionpay.gradle Riru
10+
bash ./build.sh :module:flashDebug ./src/gradle/qq.gradle Zygisk
11+
bash ./build.sh :module:flashDebug ./src/gradle/alipay.gradle Zygisk
12+
bash ./build.sh :module:flashDebug ./src/gradle/taobao.gradle Zygisk
13+
bash ./build.sh :module:flashDebug ./src/gradle/wechat.gradle Zygisk
14+
bash ./build.sh :module:flashDebug ./src/gradle/unionpay.gradle Zygisk

module/make.sh

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#!/bin/bash
2-
set -e
3-
cd ${0%/*}
2+
set -ex
3+
cd ${0%/*}/..
44

5-
bash ./build.sh :module:assembleRelease ./src/gradle/qq.gradle Zygisk
6-
bash ./build.sh :module:assembleRelease ./src/gradle/qq.gradle Riru
7-
bash ./build.sh :module:assembleRelease ./src/gradle/alipay.gradle Zygisk
8-
bash ./build.sh :module:assembleRelease ./src/gradle/alipay.gradle Riru
9-
bash ./build.sh :module:assembleRelease ./src/gradle/taobao.gradle Zygisk
10-
bash ./build.sh :module:assembleRelease ./src/gradle/taobao.gradle Riru
11-
bash ./build.sh :module:assembleRelease ./src/gradle/wechat.gradle Zygisk
12-
bash ./build.sh :module:assembleRelease ./src/gradle/wechat.gradle Riru
5+
./gradlew clean
6+
./gradlew :app:assembleRelease
7+
cd module
8+
bash ./build.sh :module:assembleRelease ./src/gradle/qq.gradle Zygisk
9+
bash ./build.sh :module:assembleRelease ./src/gradle/qq.gradle Riru
10+
bash ./build.sh :module:assembleRelease ./src/gradle/alipay.gradle Zygisk
11+
bash ./build.sh :module:assembleRelease ./src/gradle/alipay.gradle Riru
12+
bash ./build.sh :module:assembleRelease ./src/gradle/taobao.gradle Zygisk
13+
bash ./build.sh :module:assembleRelease ./src/gradle/taobao.gradle Riru
14+
bash ./build.sh :module:assembleRelease ./src/gradle/wechat.gradle Zygisk
15+
bash ./build.sh :module:assembleRelease ./src/gradle/wechat.gradle Riru
1316
bash ./build.sh :module:assembleRelease ./src/gradle/unionpay.gradle Zygisk
1417
bash ./build.sh :module:assembleRelease ./src/gradle/unionpay.gradle Riru

module/src/cpp/fingerprint.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
#include <riru.h>
1212

13-
#include "fingerprint.h"
1413
#include "log.h"
14+
#include "fingerprint.h"
15+
#include "resource_extractor.h"
1516

1617
static bool sHookEnable = false;
1718
static char *sAppDataDir = NULL;
@@ -138,16 +139,22 @@ void fingerprintPost(JNIEnv *env, const char *pluginTypeName) {
138139
char appCacheDir[PATH_MAX] = {0};
139140
snprintf(appCacheDir, PATH_MAX - 1, "%s/cache", sAppDataDir);
140141
if (access(appCacheDir, 0) != 0) mkdir(appCacheDir, 0755);
141-
142-
const char *dexPath = "/data/local/tmp/lib" MODULE_NAME ".debug.dex";
142+
char appFilesDir[PATH_MAX] = {0};
143+
snprintf(appFilesDir, PATH_MAX - 1, "%s/files", sAppDataDir);
144+
if (access(appFilesDir, 0) != 0) mkdir(appFilesDir, 0755);
145+
146+
char dexPath[PATH_MAX] = {0};
147+
snprintf(dexPath, PATH_MAX - 1, "%s/" MODULE_NAME ".dex", appFilesDir);
148+
#ifdef NDEBUG
149+
extractDexResource(dexPath);
150+
#else
151+
LOGD("=======================================================");
152+
LOGD("==================== DEBUG MODE ON ====================");
153+
LOGD("=======================================================");
143154
if (access(dexPath, 0) != 0) {
144-
dexPath = "/system/framework/lib" MODULE_NAME ".dex";
145-
}
146-
//https://github.com/eritpchy/FingerprintPay/issues/107
147-
if (access(dexPath, 0) != 0) {
148-
LOGE("Error: dexPath:%s is not accessible", dexPath);
149-
dexPath = "/data/local/tmp/lib" MODULE_NAME ".dex";
155+
extractDexResource(dexPath);
150156
}
157+
#endif
151158
if (access(dexPath, 0) != 0) {
152159
LOGE("Error: dexPath:%s is not accessible", dexPath);
153160
return;

module/src/cpp/log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <android/log.h>
66

77
#ifndef LOG_TAG
8-
#define LOG_TAG "XFINGERPRINT"
8+
#define LOG_TAG "FingerprintPayLoader"
99
#endif
1010

1111

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <sys/stat.h>
5+
#include <unistd.h>
6+
#include "log.h"
7+
8+
// 声明外部符号,这些符号是由objcopy工具生成的
9+
extern "C" {
10+
// DEX资源的开始、结束地址和大小
11+
extern const uint8_t _binary_classes_dex_start[];
12+
extern const uint8_t _binary_classes_dex_end[];
13+
extern const size_t _binary_classes_dex_size;
14+
}
15+
16+
/**
17+
* 将嵌入的DEX资源提取到指定路径
18+
*
19+
* @param outputPath DEX文件的输出路径
20+
* @return 是否成功提取
21+
*/
22+
extern "C" bool extractDexResource(const char* outputPath) {
23+
LOGI("Extracting DEX resource to: %s", outputPath);
24+
25+
// 计算资源大小
26+
size_t resourceSize = reinterpret_cast<size_t>(_binary_classes_dex_end) -
27+
reinterpret_cast<size_t>(_binary_classes_dex_start);
28+
if (access(outputPath, 0) == 0 && remove(outputPath) != 0) {
29+
LOGE("Error delete file %s reason: %s", outputPath, strerror(errno));
30+
return -1;
31+
}
32+
FILE* outFile = fopen(outputPath, "wb");
33+
if (!outFile) {
34+
LOGE("Failed to create output file: %s", outputPath);
35+
return false;
36+
}
37+
38+
// 写入DEX数据
39+
size_t written = fwrite(_binary_classes_dex_start, 1, resourceSize, outFile);
40+
41+
// 检查是否写入成功
42+
if (written != resourceSize) {
43+
LOGE("Failed to write DEX data. Written: %zu, Expected: %zu", written, resourceSize);
44+
fclose(outFile);
45+
return false;
46+
}
47+
48+
// 关闭文件
49+
fclose(outFile);
50+
51+
// 设置文件权限
52+
chmod(outputPath, S_IRUSR | S_IRGRP | S_IROTH);
53+
54+
LOGI("DEX resource extraction successful, size: %zu bytes", resourceSize);
55+
return true;
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef RESOURCE_EXTRACTOR_H
2+
#define RESOURCE_EXTRACTOR_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
/**
9+
* 将嵌入的DEX资源提取到指定路径
10+
*
11+
* @param outputPath DEX文件的输出路径
12+
* @return 是否成功提取,成功返回true,失败返回false
13+
*/
14+
bool extractDexResource(const char* outputPath);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif // RESOURCE_EXTRACTOR_H

module/src/gradle/alipay.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ ext {
2828
moduleVersion = "v${project.property("VERSION_NAME")}"
2929
moduleVersionCode = project.property("VERSION_CODE") as int
3030
moduleUpdateJson = "https://raw.githubusercontent.com/eritpchy/FingerprintPay/main/release/update_${pluginTypeName.toLowerCase()}_alipay.json"
31+
moduleResourcePath = fileTree(dir: rootProject.file('../../app/build/intermediates/dex'), include: '**/classes.dex').files.sort { it.lastModified() }.last().absolutePath
3132
}
3233
apply from: "$rootDir/fingerprint.gradle"

module/src/gradle/fingerprint.gradle

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ afterEvaluate {
22
android.libraryVariants.forEach { variant ->
33
def magiskDir = file("$rootDir/template/magisk_module")
44
variant.assembleProvider.get().doLast {
5-
exec {
6-
executable = "$rootDir/../../gradlew"
7-
args = ['-p', "$rootDir/../../", "clean", ":app:mergeDexRelease"]
8-
}
9-
file("$magiskDir/system/framework").mkdirs()
10-
copy {
11-
from file("$rootDir/../../app/build/intermediates/dex/release/mergeDexRelease/classes.dex")
12-
rename('classes.dex', "lib${moduleLibraryName}.dex")
13-
into file("$magiskDir/system/framework")
14-
}
155
}
166
}
177
}

0 commit comments

Comments
 (0)