2023-11-20 07:42:21 +08:00
|
|
|
#include <android/log.h>
|
|
|
|
#include <sys/system_properties.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2023-11-22 07:32:52 +08:00
|
|
|
#include <map>
|
2023-11-20 07:42:21 +08:00
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
#include "zygisk.hpp"
|
2023-11-22 01:25:33 +08:00
|
|
|
#include "dobby.h"
|
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 07:32:52 +08:00
|
|
|
#define PROP_FILE_PATH "/data/adb/modules/playintegrityfix/pif.prop"
|
|
|
|
|
|
|
|
#define MAX_LINE_LENGTH 256
|
2023-11-20 07:42:21 +08:00
|
|
|
|
|
|
|
typedef void (*T_Callback)(void *, const char *, const char *, uint32_t);
|
|
|
|
|
2023-11-22 07:32:52 +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-22 07:32:52 +08:00
|
|
|
if (cookie == nullptr || name == nullptr || value == nullptr ||
|
|
|
|
!callbacks.contains(cookie))
|
|
|
|
return;
|
2023-11-20 07:42:21 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-11-22 07:32:52 +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-22 07:32:52 +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-22 07:32:52 +08:00
|
|
|
static void parsePropsFile(const char *filename) {
|
|
|
|
LOGD("Proceed to parse '%s' file", filename);
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
FILE *file = fopen(filename, "r");
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
char line[MAX_LINE_LENGTH];
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
while (fgets(line, sizeof(line), file) != nullptr) {
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
std::string key, value;
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
char *data = strtok(line, "=");
|
|
|
|
|
|
|
|
while (data) {
|
|
|
|
if (key.empty()) {
|
|
|
|
key = data;
|
|
|
|
} else {
|
|
|
|
value = data;
|
|
|
|
}
|
|
|
|
data = strtok(nullptr, "=");
|
|
|
|
}
|
|
|
|
|
|
|
|
key.erase(std::remove_if(key.begin(), key.end(),
|
|
|
|
[](unsigned char x) { return std::isspace(x); }), key.end());
|
|
|
|
value.erase(std::remove_if(value.begin(), value.end(),
|
|
|
|
[](unsigned char x) { return std::isspace(x); }), value.end());
|
|
|
|
|
|
|
|
if (key == "SECURITY_PATCH") {
|
|
|
|
SECURITY_PATCH = value;
|
|
|
|
LOGD("Set SECURITY_PATCH to '%s'", value.c_str());
|
|
|
|
} else if (key == "API_LEVEL") {
|
|
|
|
API_LEVEL = value;
|
|
|
|
LOGD("Set API_LEVEL to '%s'", value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
key.clear();
|
|
|
|
value.clear();
|
|
|
|
key.shrink_to_fit();
|
|
|
|
key.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doHook(const std::string &str) {
|
|
|
|
parsePropsFile(str.c_str());
|
2023-11-22 01:25:33 +08:00
|
|
|
|
|
|
|
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 07:32:52 +08:00
|
|
|
callbacks.clear();
|
|
|
|
API_LEVEL.clear();
|
|
|
|
SECURITY_PATCH.clear();
|
|
|
|
API_LEVEL.shrink_to_fit();
|
|
|
|
SECURITY_PATCH.shrink_to_fit();
|
|
|
|
|
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);
|
2023-11-22 07:32:52 +08:00
|
|
|
propsFile = rawDir;
|
2023-11-20 07:42:21 +08:00
|
|
|
env->ReleaseStringUTFChars(args->app_data_dir, rawDir);
|
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
propsFile = propsFile + "/pif.prop";
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
int strSize = static_cast<int>(propsFile.size());
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
write(fd, &strSize, sizeof(strSize));
|
|
|
|
write(fd, propsFile.data(), strSize);
|
2023-11-20 07:42:21 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
doHook(propsFile);
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-20 07:42:21 +08:00
|
|
|
if (!moduleDex.empty()) injectDex();
|
2023-11-22 07:32:52 +08:00
|
|
|
|
|
|
|
LOGD("clean");
|
|
|
|
propsFile.clear();
|
|
|
|
propsFile.shrink_to_fit();
|
|
|
|
moduleDex.clear();
|
|
|
|
moduleDex.shrink_to_fit();
|
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;
|
|
|
|
bool isGmsUnstable = false;
|
2023-11-22 07:32:52 +08:00
|
|
|
std::string propsFile;
|
2023-11-20 07:42:21 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void companion(int fd) {
|
|
|
|
int strSize;
|
|
|
|
read(fd, &strSize, sizeof(strSize));
|
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
std::string propsFile;
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
propsFile.resize(strSize);
|
|
|
|
read(fd, propsFile.data(), strSize);
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
std::filesystem::copy_file(PROP_FILE_PATH, propsFile,
|
2023-11-20 07:42:21 +08:00
|
|
|
std::filesystem::copy_options::overwrite_existing);
|
2023-11-22 07:32:52 +08:00
|
|
|
std::filesystem::permissions(propsFile, std::filesystem::perms::others_all);
|
|
|
|
|
|
|
|
propsFile.clear();
|
|
|
|
propsFile.shrink_to_fit();
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
FILE *dex = fopen(DEX_FILE_PATH, "rb");
|
2023-11-20 07:42:21 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
fseek(dex, 0, SEEK_END);
|
|
|
|
long size = ftell(dex);
|
|
|
|
fseek(dex, 0, SEEK_SET);
|
2023-11-20 07:42:21 +08:00
|
|
|
|
|
|
|
char buffer[size];
|
2023-11-22 07:32:52 +08:00
|
|
|
fread(buffer, 1, size, dex);
|
|
|
|
|
|
|
|
fclose(dex);
|
2023-11-22 01:25:33 +08:00
|
|
|
|
2023-11-22 07:32:52 +08:00
|
|
|
buffer[size] = '\0';
|
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)
|