Skip to content

Commit

Permalink
Update + fix compile
Browse files Browse the repository at this point in the history
Get scripts from PIFork by @osm0sis

Co-authored-by: Chris Renshaw <[email protected]>
  • Loading branch information
chiteroman and osm0sis committed Sep 4, 2024
1 parent d3c6e36 commit bf00dbf
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 132 deletions.
9 changes: 3 additions & 6 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ android {
resources {
excludes += "**"
}
jniLibs {
excludes += "**/libdobby.so"
}
}

defaultConfig {
applicationId = "es.chiteroman.playintegrityfix"
minSdk = 26
targetSdk = 35
versionCode = 17300
versionName = "v17.3"
versionName = "v17.4-TEST"
multiDexEnabled = false

externalNativeBuild {
Expand All @@ -43,13 +40,13 @@ android {
)

cFlags(
"-std=c23",
"-std=gnu23",
"-fvisibility=hidden",
"-fvisibility-inlines-hidden"
)

cppFlags(
"-std=c++23",
"-std=gnu++26",
"-fno-exceptions",
"-fno-rtti",
"-fvisibility=hidden",
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ add_library(${CMAKE_PROJECT_NAME} SHARED

target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
cJSON
shadowhook/shadowhook/src/main/cpp
shadowhook/.
shadowhook/arch/${ARCH}
shadowhook/include
shadowhook/common
Expand Down
133 changes: 77 additions & 56 deletions app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <android/log.h>
#include <sys/system_properties.h>
#include <unistd.h>
#include <dirent.h>
#include <string>
#include <vector>
#include <filesystem>
#include "zygisk.hpp"
#include "dobby.h"
#include "shadowhook.h"
#include "cJSON.h"

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "PIF", __VA_ARGS__)
Expand All @@ -18,30 +19,35 @@

#define TS_PATH "/data/adb/modules/tricky_store"

static ssize_t xread(int fd, void *buffer, size_t count) {
ssize_t total = 0;
char *buf = (char *) buffer;
while (count > 0) {
ssize_t ret = read(fd, buf, count);
if (ret < 0) return -1;
buf += ret;
total += ret;
count -= ret;
static size_t xread(int fd, uint8_t *data, size_t size) {
size_t remaining = size;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(read(fd, data, remaining));
if (n <= 0) {
return size - remaining;
}
data += n;
remaining -= n;
}
return total;
return size;
}

static ssize_t xwrite(int fd, void *buffer, size_t count) {
ssize_t total = 0;
char *buf = (char *) buffer;
while (count > 0) {
ssize_t ret = write(fd, buf, count);
if (ret < 0) return -1;
buf += ret;
total += ret;
count -= ret;
static size_t xwrite(int fd, uint8_t *data, size_t size) {
size_t remaining = size;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(write(fd, data, remaining));
if (n < 0) {
LOGE("write failed: %s", strerror(errno));
return size - remaining;
}
data += n;
remaining -= n;
}
return total;
if (TEMP_FAILURE_RETRY(fsync(fd)) == -1) {
LOGE("fsync failed: %s", strerror(errno));
return -1;
}
return size;
}

static std::string DEVICE_INITIAL_SDK_INT;
Expand Down Expand Up @@ -93,18 +99,28 @@ my_system_property_read_callback(const prop_info *pi, T_Callback callback, void
return o_system_property_read_callback(pi, modify_callback, cookie);
}

static void doHook() {
void *handle = DobbySymbolResolver(nullptr, "__system_property_read_callback");
if (!handle) {
LOGE("error resolving __system_property_read_callback symbol!");
return;
}
if (DobbyHook(handle, (void *) my_system_property_read_callback,
(void **) &o_system_property_read_callback)) {
LOGE("hook __system_property_read_callback failed!");
return;
static bool doHook() {
shadowhook_init(SHADOWHOOK_MODE_UNIQUE, false);
{
auto libc_handle = shadowhook_dlopen("libc.so");
if (!libc_handle) {
LOGE("error loading libc.so library!");
goto exit;
}
auto handle = shadowhook_dlsym(libc_handle, "__system_property_read_callback");
if (!handle) {
LOGE("error resolving __system_property_read_callback symbol!");
goto exit;
}
if (shadowhook_hook_sym_addr(handle, (void *) my_system_property_read_callback,
(void **) &o_system_property_read_callback)) {
LOGD("hook __system_property_read_callback success at %p", handle);
return true;
}
}
LOGD("hook __system_property_read_callback success at %p", handle);
exit:
LOGE("hook __system_property_read_callback failed!");
return false;
}

class PlayIntegrityFix : public zygisk::ModuleBase {
Expand Down Expand Up @@ -160,8 +176,8 @@ class PlayIntegrityFix : public zygisk::ModuleBase {
int dexSize = 0, jsonSize = 0;
std::vector<uint8_t> jsonVector;

xread(fd, &dexSize, sizeof(int));
xread(fd, &jsonSize, sizeof(int));
xread(fd, (uint8_t *) &dexSize, sizeof(int));
xread(fd, (uint8_t *) &jsonSize, sizeof(int));

if (dexSize > 0) {
dexVector.resize(dexSize);
Expand All @@ -176,7 +192,7 @@ class PlayIntegrityFix : public zygisk::ModuleBase {
}

bool trickyStore = false;
xread(fd, &trickyStore, sizeof(trickyStore));
xread(fd, (uint8_t *) &trickyStore, sizeof(trickyStore));

close(fd);

Expand All @@ -186,7 +202,7 @@ class PlayIntegrityFix : public zygisk::ModuleBase {
parseJSON();

if (trickyStore) {
LOGD("TrickyStore module installed and enabled, disabling spoofProps and spoofProvider");
LOGD("TrickyStore module installed, disabling spoofProps and spoofProvider");
spoofProps = false;
spoofProvider = false;
}
Expand All @@ -197,8 +213,9 @@ class PlayIntegrityFix : public zygisk::ModuleBase {

UpdateBuildFields();

if (spoofProps) doHook();
else api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);
if (spoofProps)
if (!doHook())
api->setOption(zygisk::DLCLOSE_MODULE_LIBRARY);

if (spoofProvider || spoofSignature) injectDex();
else
Expand All @@ -218,8 +235,8 @@ class PlayIntegrityFix : public zygisk::ModuleBase {
JNIEnv *env = nullptr;
std::vector<uint8_t> dexVector;
cJSON *json = nullptr;
bool spoofProps = true;
bool spoofProvider = true;
bool spoofProps = false;
bool spoofProvider = false;
bool spoofSignature = false;

void parseJSON() {
Expand Down Expand Up @@ -305,7 +322,7 @@ class PlayIntegrityFix : public zygisk::ModuleBase {
jclass buildClass = env->FindClass("android/os/Build");
jclass versionClass = env->FindClass("android/os/Build$VERSION");

cJSON *currentElement = nullptr;
cJSON *currentElement;
cJSON_ArrayForEach(currentElement, json) {
const char *key = currentElement->string;

Expand Down Expand Up @@ -345,7 +362,9 @@ static std::vector<uint8_t> readFile(const char *path) {

if (!file) return {};

auto size = std::filesystem::file_size(path);
fseek(file, 0, SEEK_END);
auto size = ftell(file);
fseek(file, 0, SEEK_SET);

std::vector<uint8_t> vector(size);

Expand All @@ -360,21 +379,16 @@ static void companion(int fd) {

std::vector<uint8_t> dex, json;

if (std::filesystem::exists(DEX_PATH)) {
dex = readFile(DEX_PATH);
}
dex = readFile(DEX_PATH);

if (std::filesystem::exists(PIF_JSON)) {
json = readFile(PIF_JSON);
} else if (std::filesystem::exists(PIF_JSON_DEFAULT)) {
json = readFile(PIF_JSON_DEFAULT);
}
json = readFile(PIF_JSON);
if (json.empty()) json = readFile(PIF_JSON_DEFAULT);

int dexSize = static_cast<int>(dex.size());
int jsonSize = static_cast<int>(json.size());

xwrite(fd, &dexSize, sizeof(int));
xwrite(fd, &jsonSize, sizeof(int));
xwrite(fd, (uint8_t *) &dexSize, sizeof(int));
xwrite(fd, (uint8_t *) &jsonSize, sizeof(int));

if (dexSize > 0) {
xwrite(fd, dex.data(), dexSize * sizeof(uint8_t));
Expand All @@ -384,9 +398,16 @@ static void companion(int fd) {
xwrite(fd, json.data(), jsonSize * sizeof(uint8_t));
}

bool trickyStore = std::filesystem::exists(TS_PATH) &&
!std::filesystem::exists(std::string(TS_PATH) + "/disable");
xwrite(fd, &trickyStore, sizeof(trickyStore));
bool trickyStore = false;

DIR *dir = opendir(TS_PATH);

if (dir) {
trickyStore = true;
closedir(dir);
}

xwrite(fd, (uint8_t *) &trickyStore, sizeof(trickyStore));
}

REGISTER_ZYGISK_MODULE(PlayIntegrityFix)
Expand Down
53 changes: 53 additions & 0 deletions module/common_func.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
RESETPROP="resetprop -n"
[ -n "$MAGISK_VER_CODE" ] && [ "$MAGISK_VER_CODE" -lt "27003" ] && RESETPROP=resetprop_hexpatch

# resetprop_hexpatch [-f|--force] <prop name> <new value>
resetprop_hexpatch() {
case "$1" in
-f|--force) local FORCE=1; shift;;
esac

local NAME="$1"
local NEWVALUE="$2"
local CURVALUE="$(resetprop "$NAME")"

[ ! "$NEWVALUE" -o ! "$CURVALUE" ] && return 1
[ "$NEWVALUE" = "$CURVALUE" -a ! "$FORCE" ] && return 2

local NEWLEN=${#NEWVALUE}
if [ -f /dev/__properties__ ]; then
local PROPFILE=/dev/__properties__
else
local PROPFILE="/dev/__properties__/$(resetprop -Z "$NAME")"
fi
[ ! -f "$PROPFILE" ] && return 3
local NAMEOFFSET=$(echo $(strings -t d "$PROPFILE" | grep "$NAME") | cut -d ' ' -f 1)

#<hex 2-byte change counter><flags byte><hex length of prop value><prop value + nul padding to 92 bytes><prop name>
local NEWHEX="$(printf '%02x' "$NEWLEN")$(printf "$NEWVALUE" | od -A n -t x1 -v | tr -d ' \n')$(printf "%$((92-NEWLEN))s" | sed 's/ /00/g')"

printf "Patch '$NAME' to '$NEWVALUE' in '$PROPFILE' @ 0x%08x -> \n[0000??$NEWHEX]\n" $((NAMEOFFSET-96))

echo -ne "\x00\x00" \
| dd obs=1 count=2 seek=$((NAMEOFFSET-96)) conv=notrunc of="$PROPFILE"
echo -ne "$(printf "$NEWHEX" | sed -e 's/.\{2\}/&\\x/g' -e 's/^/\\x/' -e 's/\\x$//')" \
| dd obs=1 count=93 seek=$((NAMEOFFSET-93)) conv=notrunc of="$PROPFILE"
}

# resetprop_if_diff <prop name> <expected value>
resetprop_if_diff() {
local NAME="$1"
local EXPECTED="$2"
local CURRENT="$(resetprop "$NAME")"

[ -z "$CURRENT" ] || [ "$CURRENT" = "$EXPECTED" ] || $RESETPROP "$NAME" "$EXPECTED"
}

# resetprop_if_match <prop name> <value match string> <new value>
resetprop_if_match() {
local NAME="$1"
local CONTAINS="$2"
local VALUE="$3"

[[ "$(resetprop "$NAME")" = *"$CONTAINS"* ]] && $RESETPROP "$NAME" "$VALUE"
}
15 changes: 6 additions & 9 deletions module/customize.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Module requires Zygisk to work
if [ "$ZYGISK_ENABLED" != "1" ] && [ ! -d "/data/adb/modules/zygisksu" ]; then
abort "! Zygisk is not enabled. Please, enable Zygisk in Magisk Settings or install the ZygiskNext or ReZygisk module."
fi

# Error on < Android 8
if [ "$API" -lt 26 ]; then
abort "- !!! You can't use this module on Android < 8.0"
abort "! You can't use this module on Android < 8.0"
fi

# safetynet-fix module is obsolete and it's incompatible with PIF
Expand All @@ -20,16 +25,8 @@ if [ -d "/data/adb/modules/MagiskHidePropsConf" ]; then
ui_print "! WARNING, MagiskHidePropsConf module may cause issues with PIF."
fi

# If TrickyStore module is installed, PIF won't spoof Provider
if [ -d "/data/adb/modules/tricky_store" ]; then
ui_print "- TrickyStore module detected!"
ui_print "- PIF will disable Provider spoofing."
fi

# Check custom fingerprint
if [ -f "/data/adb/pif.json" ]; then
mv -f "/data/adb/pif.json" "/data/adb/pif.json.old"
ui_print "- Backup custom pif.json"
fi

rm -rf "$MODPATH"/system
Loading

0 comments on commit bf00dbf

Please sign in to comment.