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

213 lines
6.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 <string>
#include <vector>
#include <filesystem>
2023-11-22 01:25:33 +08:00
#include <fstream>
2023-11-20 07:42:21 +08:00
#include "zygisk.hpp"
2023-11-22 01:25:33 +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__)
2023-11-22 01:25:33 +08:00
static std::string API_LEVEL;
static std::string SECURITY_PATCH;
2023-11-20 07:42:21 +08:00
#define DEX_FILE_PATH "/data/adb/modules/playintegrityfix/classes.dex"
2023-11-22 01:25:33 +08:00
#define JSON_FILE_PATH "/data/adb/modules/playintegrityfix/pif.json"
2023-11-20 07:42:21 +08:00
typedef void (*T_Callback)(void *, const char *, const char *, uint32_t);
static volatile T_Callback propCallback = nullptr;
static void modify_callback(void *cookie, const char *name, const char *value, uint32_t serial) {
if (cookie == nullptr || name == nullptr || value == nullptr || propCallback == nullptr) return;
std::string_view prop(name);
2023-11-22 01:25:33 +08:00
if (prop.ends_with("api_level")) value = API_LEVEL.c_str();
else if (prop.ends_with("security_patch")) value = SECURITY_PATCH.c_str();
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
return propCallback(cookie, name, value, serial);
}
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);
}
propCallback = callback;
return o_system_property_read_callback(pi, modify_callback, cookie);
}
static void doHook() {
2023-11-22 01:25:33 +08:00
std::ifstream ifs("/data/data/com.google.android.gms/pif.json");
auto json = nlohmann::json::parse(ifs, nullptr, false);
ifs.close();
LOGD("Loaded %d keys from pif.json", static_cast<int>(json.size()));
API_LEVEL = json["API_LEVEL"].get<std::string>();
SECURITY_PATCH = json["SECURITY_PATCH"].get<std::string>();
json.clear();
void *handle = DobbySymbolResolver("libc.so", "__system_property_read_callback");
2023-11-20 07:42:21 +08:00
if (handle == nullptr) {
LOGD("Couldn't find '__system_property_read_callback' handle. Report to @chiteroman");
return;
}
LOGD("Found '__system_property_read_callback' handle at %p", handle);
2023-11-22 01:25:33 +08:00
DobbyHook(handle, (void *) my_system_property_read_callback,
(void **) &o_system_property_read_callback);
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 {
auto rawProcess = env->GetStringUTFChars(args->nice_name, nullptr);
std::string_view process(rawProcess);
bool isGms = process.starts_with("com.google.android.gms");
isGmsUnstable = process.compare("com.google.android.gms.unstable") == 0;
env->ReleaseStringUTFChars(args->nice_name, rawProcess);
if (isGms) api->setOption(zygisk::FORCE_DENYLIST_UNMOUNT);
if (isGmsUnstable) {
2023-11-22 01:25:33 +08:00
int fd = api->connectCompanion();
2023-11-20 07:42:21 +08:00
auto rawDir = env->GetStringUTFChars(args->app_data_dir, nullptr);
std::string dir(rawDir);
env->ReleaseStringUTFChars(args->app_data_dir, rawDir);
int strSize = static_cast<int>(dir.size());
write(fd, &strSize, sizeof(strSize));
2023-11-22 01:25:33 +08:00
write(fd, dir.data(), strSize);
2023-11-20 07:42:21 +08:00
dir.clear();
dir.shrink_to_fit();
long size;
read(fd, &size, sizeof(size));
2023-11-22 01:25:33 +08:00
char buffer[size];
read(fd, buffer, size);
2023-11-20 07:42:21 +08:00
close(fd);
2023-11-22 01:25:33 +08:00
moduleDex.insert(moduleDex.end(), buffer, buffer + size);
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
return;
2023-11-20 07:42:21 +08:00
}
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
}
void postAppSpecialize(const zygisk::AppSpecializeArgs *args) override {
if (!isGmsUnstable) return;
2023-11-22 01:25:33 +08:00
doHook();
2023-11-20 07:42:21 +08:00
if (!moduleDex.empty()) injectDex();
}
void preServerSpecialize(zygisk::ServerSpecializeArgs *args) override {
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
}
private:
zygisk::Api *api = nullptr;
JNIEnv *env = nullptr;
bool isGmsUnstable = false;
std::vector<char> moduleDex;
void injectDex() {
2023-11-22 01:25:33 +08:00
LOGD("Preparing to inject %d bytes to the process", static_cast<int>(moduleDex.size()));
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 buffer");
auto buf = env->NewDirectByteBuffer(moduleDex.data(), static_cast<jlong>(moduleDex.size()));
LOGD("create class loader");
auto dexClClass = env->FindClass("dalvik/system/InMemoryDexClassLoader");
auto dexClInit = env->GetMethodID(dexClClass, "<init>",
"(Ljava/nio/ByteBuffer;Ljava/lang/ClassLoader;)V");
auto dexCl = env->NewObject(dexClClass, dexClInit, buf, systemClassLoader);
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);
LOGD("call init");
auto entryClass = (jclass) entryClassObj;
auto entryInit = env->GetStaticMethodID(entryClass, "init", "()V");
env->CallStaticVoidMethod(entryClass, entryInit);
2023-11-22 01:25:33 +08:00
LOGD("clean");
moduleDex.clear();
moduleDex.shrink_to_fit();
2023-11-20 07:42:21 +08:00
}
};
static void companion(int fd) {
int strSize;
read(fd, &strSize, sizeof(strSize));
2023-11-22 01:25:33 +08:00
std::string file;
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
file.resize(strSize);
read(fd, file.data(), strSize);
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
file = file + "/pif.json";
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
LOGD("Json path: %s", file.c_str());
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
std::filesystem::copy_file(JSON_FILE_PATH, file,
2023-11-20 07:42:21 +08:00
std::filesystem::copy_options::overwrite_existing);
2023-11-22 01:25:33 +08:00
std::filesystem::permissions(file, std::filesystem::perms::all);
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
std::ifstream dex(DEX_FILE_PATH, std::ifstream::binary | std::ifstream::ate);
2023-11-20 07:42:21 +08:00
2023-11-22 01:25:33 +08:00
long size = dex.tellg();
dex.seekg(std::ifstream::beg);
2023-11-20 07:42:21 +08:00
char buffer[size];
2023-11-22 01:25:33 +08:00
dex.read(buffer, size);
dex.close();
2023-11-20 07:42:21 +08:00
write(fd, &size, sizeof(size));
write(fd, buffer, size);
}
REGISTER_ZYGISK_MODULE(PlayIntegrityFix)
REGISTER_ZYGISK_COMPANION(companion)