PlayIntegrityFix/app/src/main/cpp/main.cpp

223 lines
7.7 KiB
C++
Raw Normal View History

2023-11-20 07:42:21 +08:00
#include <android/log.h>
#include <sys/system_properties.h>
#include <unistd.h>
#include "zygisk.hpp"
2023-11-25 03:34:31 +08:00
#include "dobby.h"
#include "json.hpp"
2023-11-20 07:42:21 +08:00
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "PIF/Native", __VA_ARGS__)
#define DEX_FILE_PATH "/data/adb/modules/playintegrityfix/classes.dex"
2023-11-25 03:34:31 +08:00
#define PROP_FILE_PATH "/data/adb/modules/playintegrityfix/pif.json"
2023-11-22 07:32:52 +08:00
2023-11-25 03:34:31 +08:00
#define DEF_FIRST_API_LEVEL "23"
#define DEF_SECURITY_PATCH "2017-08-05"
static std::string SECURITY_PATCH, FIRST_API_LEVEL;
2023-11-24 03:14:58 +08:00
2023-11-20 07:42:21 +08:00
typedef void (*T_Callback)(void *, const char *, const char *, uint32_t);
2023-11-24 18:52:57 +08:00
static std::map<void *, T_Callback> callbacks;
2023-11-20 07:42:21 +08:00
static void modify_callback(void *cookie, const char *name, const char *value, uint32_t serial) {
2023-11-24 18:52:57 +08:00
if (cookie == nullptr || name == nullptr || value == nullptr ||
!callbacks.contains(cookie))
return;
2023-11-20 07:42:21 +08:00
2023-11-24 18:52:57 +08:00
std::string_view prop(name);
2023-11-20 07:42:21 +08:00
2023-11-23 01:08:47 +08:00
if (prop.ends_with("api_level")) {
2023-11-25 03:34:31 +08:00
if (FIRST_API_LEVEL.empty()) {
value = nullptr;
} else {
value = FIRST_API_LEVEL.c_str();
}
2023-11-23 01:08:47 +08:00
} else if (prop.ends_with("security_patch")) {
2023-11-25 03:34:31 +08:00
if (SECURITY_PATCH.empty()) {
value = nullptr;
} else {
value = SECURITY_PATCH.c_str();
}
2023-11-23 01:08:47 +08:00
}
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
if (!prop.starts_with("cache") && !prop.starts_with("debug")) LOGD("[%s] -> %s", name, value);
2023-11-20 07:42:21 +08:00
2023-11-24 18:52:57 +08:00
return callbacks[cookie](cookie, name, value, serial);
2023-11-20 07:42:21 +08:00
}
static void (*o_system_property_read_callback)(const prop_info *, T_Callback, void *);
static void
my_system_property_read_callback(const prop_info *pi, T_Callback callback, void *cookie) {
if (pi == nullptr || callback == nullptr || cookie == nullptr) {
return o_system_property_read_callback(pi, callback, cookie);
}
2023-11-24 18:52:57 +08:00
callbacks[cookie] = callback;
2023-11-20 07:42:21 +08:00
return o_system_property_read_callback(pi, modify_callback, cookie);
}
2023-11-23 07:07:43 +08:00
static void doHook() {
2023-11-25 03:34:31 +08:00
void *handle = DobbySymbolResolver("libc.so", "__system_property_read_callback");
2023-11-23 07:07:43 +08:00
if (handle == nullptr) {
LOGD("Couldn't find '__system_property_read_callback' handle. Report to @chiteroman");
return;
2023-11-22 07:32:52 +08:00
}
2023-11-23 07:07:43 +08:00
LOGD("Found '__system_property_read_callback' handle at %p", handle);
2023-11-25 03:34:31 +08:00
DobbyHook(handle, reinterpret_cast<void *>(my_system_property_read_callback),
reinterpret_cast<void **>(&o_system_property_read_callback));
2023-11-22 07:32:52 +08:00
}
2023-11-20 07:42:21 +08:00
class PlayIntegrityFix : public zygisk::ModuleBase {
public:
void onLoad(zygisk::Api *api, JNIEnv *env) override {
this->api = api;
this->env = env;
}
void preAppSpecialize(zygisk::AppSpecializeArgs *args) override {
2023-11-25 03:34:31 +08:00
bool isGms = false;
bool isGmsUnstable = false;
2023-11-20 07:42:21 +08:00
auto rawProcess = env->GetStringUTFChars(args->nice_name, nullptr);
2023-11-25 03:34:31 +08:00
if (rawProcess) {
std::string_view process(rawProcess);
isGms = process.starts_with("com.google.android.gms");
isGmsUnstable = process.compare("com.google.android.gms.unstable") == 0;
}
2023-11-20 07:42:21 +08:00
env->ReleaseStringUTFChars(args->nice_name, rawProcess);
2023-11-24 18:52:57 +08:00
if (!isGms) {
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
return;
}
2023-11-24 03:14:58 +08:00
2023-11-24 18:52:57 +08:00
api->setOption(zygisk::FORCE_DENYLIST_UNMOUNT);
2023-11-24 03:14:58 +08:00
2023-11-24 18:52:57 +08:00
if (!isGmsUnstable) {
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
return;
}
2023-11-20 07:42:21 +08:00
2023-11-25 03:34:31 +08:00
auto rawDir = env->GetStringUTFChars(args->app_data_dir, nullptr);
path = rawDir;
env->ReleaseStringUTFChars(args->app_data_dir, rawDir);
2023-11-24 18:52:57 +08:00
int fd = api->connectCompanion();
2023-11-20 07:42:21 +08:00
2023-11-25 03:34:31 +08:00
write(fd, path.data(), path.size());
2023-11-20 07:42:21 +08:00
2023-11-25 03:34:31 +08:00
read(fd, nullptr, 0);
2023-11-24 03:14:58 +08:00
2023-11-25 03:34:31 +08:00
close(fd);
2023-11-20 07:42:21 +08:00
}
void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override {
2023-11-25 03:34:31 +08:00
if (path.empty()) return;
std::string prop(path + "/cache/pif.json");
if (std::filesystem::exists(prop)) {
FILE *file = fopen(prop.c_str(), "r");
nlohmann::json json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
LOGD("Read %d props from pif.json", static_cast<int>(json.size()));
SECURITY_PATCH = json["SECURITY_PATCH"].get<std::string>();
FIRST_API_LEVEL = json["FIRST_API_LEVEL"].get<std::string>();
json.clear();
} else {
LOGD("File pif.json doesn't exist, using default values");
SECURITY_PATCH = DEF_SECURITY_PATCH;
FIRST_API_LEVEL = DEF_FIRST_API_LEVEL;
}
2023-11-22 01:25:33 +08:00
2023-11-25 03:34:31 +08:00
std::string dex(path + "/cache/classes.dex");
if (std::filesystem::exists(dex)) {
inject();
}
2023-11-22 07:32:52 +08:00
2023-11-24 18:52:57 +08:00
doHook();
2023-11-20 07:42:21 +08:00
}
void preServerSpecialize(zygisk::ServerSpecializeArgs *args) override {
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
}
private:
zygisk::Api *api = nullptr;
JNIEnv *env = nullptr;
2023-11-25 03:34:31 +08:00
std::string path;
2023-11-20 07:42:21 +08:00
2023-11-23 07:07:43 +08:00
void inject() {
2023-11-20 07:42:21 +08:00
LOGD("get system classloader");
auto clClass = env->FindClass("java/lang/ClassLoader");
auto getSystemClassLoader = env->GetStaticMethodID(clClass, "getSystemClassLoader",
"()Ljava/lang/ClassLoader;");
auto systemClassLoader = env->CallStaticObjectMethod(clClass, getSystemClassLoader);
LOGD("create class loader");
2023-11-25 03:34:31 +08:00
auto pathClClass = env->FindClass("dalvik/system/PathClassLoader");
auto pathClInit = env->GetMethodID(pathClClass, "<init>",
"(Ljava/lang/String;Ljava/lang/ClassLoader;)V");
std::string dexPath(path + "/cache/classes.dex");
auto moduleDex = env->NewStringUTF(dexPath.c_str());
auto dexCl = env->NewObject(pathClClass, pathClInit, moduleDex, systemClassLoader);
2023-11-20 07:42:21 +08:00
LOGD("load class");
auto loadClass = env->GetMethodID(clClass, "loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;");
auto entryClassName = env->NewStringUTF("es.chiteroman.playintegrityfix.EntryPoint");
auto entryClassObj = env->CallObjectMethod(dexCl, loadClass, entryClassName);
auto entryClass = (jclass) entryClassObj;
2023-11-23 07:07:43 +08:00
2023-11-25 03:34:31 +08:00
LOGD("read props");
auto readProps = env->GetStaticMethodID(entryClass, "readProps", "(Ljava/lang/String;)V");
std::string prop(path + "/cache/pif.json");
auto javaPath = env->NewStringUTF(prop.c_str());
env->CallStaticVoidMethod(entryClass, readProps, javaPath);
2023-11-23 07:07:43 +08:00
LOGD("call init");
2023-11-20 07:42:21 +08:00
auto entryInit = env->GetStaticMethodID(entryClass, "init", "()V");
env->CallStaticVoidMethod(entryClass, entryInit);
2023-11-24 18:52:57 +08:00
2023-11-25 03:34:31 +08:00
path.clear();
2023-11-20 07:42:21 +08:00
}
};
2023-11-24 03:14:58 +08:00
static void companion(int fd) {
2023-11-25 03:34:31 +08:00
char buffer[256];
long size = read(fd, buffer, sizeof(buffer));
2023-11-20 07:42:21 +08:00
2023-11-25 03:34:31 +08:00
std::string path(buffer, size);
LOGD("[ROOT] Got GMS dir: %s", path.c_str());
2023-11-23 07:07:43 +08:00
2023-11-25 03:34:31 +08:00
std::string dex(path + "/cache/classes.dex");
std::string prop(path + "/cache/pif.json");
2023-11-23 07:07:43 +08:00
2023-11-25 03:34:31 +08:00
if (std::filesystem::copy_file(DEX_FILE_PATH, dex,
std::filesystem::copy_options::overwrite_existing)) {
std::filesystem::permissions(dex, std::filesystem::perms::owner_read |
std::filesystem::perms::group_read |
std::filesystem::perms::others_read);
}
2023-11-23 07:07:43 +08:00
2023-11-25 03:58:30 +08:00
if (!std::filesystem::exists(PROP_FILE_PATH)) {
std::filesystem::remove(prop);
}
2023-11-25 03:34:31 +08:00
if (std::filesystem::copy_file(PROP_FILE_PATH, prop,
std::filesystem::copy_options::overwrite_existing)) {
std::filesystem::permissions(prop, std::filesystem::perms::owner_read |
std::filesystem::perms::group_read |
std::filesystem::perms::others_read);
}
2023-11-23 07:07:43 +08:00
2023-11-25 03:34:31 +08:00
write(fd, nullptr, 0);
2023-11-20 07:42:21 +08:00
}
REGISTER_ZYGISK_MODULE(PlayIntegrityFix)
REGISTER_ZYGISK_COMPANION(companion)