No need for signature prefix

This commit is contained in:
LoveSy 2022-02-03 12:39:51 +08:00 committed by LoveSy
parent f5422786c0
commit 45f0962d59
9 changed files with 17 additions and 31 deletions

View File

@ -128,7 +128,7 @@ namespace lspd {
RegisterResourcesHook(env); RegisterResourcesHook(env);
RegisterArtClassLinker(env); RegisterArtClassLinker(env);
RegisterYahfa(env, obfuscated_signature_); RegisterYahfa(env);
RegisterPendingHooks(env); RegisterPendingHooks(env);
RegisterNativeAPI(env); RegisterNativeAPI(env);
} }
@ -198,14 +198,12 @@ namespace lspd {
// Call application_binder directly if application binder is available, // Call application_binder directly if application binder is available,
// or we proxy the request from system server binder // or we proxy the request from system server binder
auto dex = instance->RequestLSPDex(env, application_binder ? application_binder : system_server_binder); auto [dex_fd, size]= instance->RequestLSPDex(env, application_binder ? application_binder : system_server_binder);
auto dex_fd = std::get<0>(dex); LoadDex(env, dex_fd, size);
LoadDex(env, dex_fd, std::get<1>(dex));
close(dex_fd); close(dex_fd);
instance->HookBridge(*this, env); instance->HookBridge(*this, env);
if (application_binder) { if (application_binder) {
obfuscated_signature_ = std::move(std::get<2>(dex));
InstallInlineHooks(); InstallInlineHooks();
Init(env); Init(env);
FindAndCall(env, "forkSystemServerPost", "(Landroid/os/IBinder;)V", application_binder); FindAndCall(env, "forkSystemServerPost", "(Landroid/os/IBinder;)V", application_binder);
@ -265,11 +263,9 @@ namespace lspd {
: instance->RequestBinder(env, nice_name); : instance->RequestBinder(env, nice_name);
if (binder) { if (binder) {
InstallInlineHooks(); InstallInlineHooks();
auto dex = instance->RequestLSPDex(env, binder); auto [dex_fd, size] = instance->RequestLSPDex(env, binder);
auto dex_fd = std::get<0>(dex); LoadDex(env, dex_fd, size);
LoadDex(env, dex_fd, std::get<1>(dex));
close(dex_fd); close(dex_fd);
obfuscated_signature_ = std::move(std::get<2>(dex));
Init(env); Init(env);
LOGD("Done prepare"); LOGD("Done prepare");
FindAndCall(env, "forkAndSpecializePost", FindAndCall(env, "forkAndSpecializePost",

View File

@ -69,7 +69,6 @@ namespace lspd {
jclass class_linker_class_ = nullptr; jclass class_linker_class_ = nullptr;
jmethodID post_fixup_static_mid_ = nullptr; jmethodID post_fixup_static_mid_ = nullptr;
bool skip_ = false; bool skip_ = false;
std::string obfuscated_signature_;
struct PreloadedDex { struct PreloadedDex {

View File

@ -38,8 +38,6 @@ namespace lspd {
std::vector<std::pair<void *, void *>> jit_movements_; std::vector<std::pair<void *, void *>> jit_movements_;
std::shared_mutex jit_movements_lock_; std::shared_mutex jit_movements_lock_;
std::string obfuscated_signature_;
} }
bool isHooked(void *art_method) { bool isHooked(void *art_method) {
@ -96,7 +94,7 @@ namespace lspd {
} }
LSP_DEF_NATIVE_METHOD(jclass, Yahfa, buildHooker, jobject app_class_loader, jchar return_class, LSP_DEF_NATIVE_METHOD(jclass, Yahfa, buildHooker, jobject app_class_loader, jchar return_class,
jcharArray classes, jstring method_name) { jcharArray classes, jstring method_name, jstring hooker_name) {
static auto *kInMemoryClassloader = JNI_NewGlobalRef(env, JNI_FindClass(env, static auto *kInMemoryClassloader = JNI_NewGlobalRef(env, JNI_FindClass(env,
"dalvik/system/InMemoryDexClassLoader")); "dalvik/system/InMemoryDexClassLoader"));
static jmethodID kInitMid = JNI_GetMethodID(env, kInMemoryClassloader, "<init>", static jmethodID kInitMid = JNI_GetMethodID(env, kInMemoryClassloader, "<init>",
@ -121,7 +119,7 @@ namespace lspd {
cbuilder.set_source_file("LSP"); cbuilder.set_source_file("LSP");
auto hooker_type = auto hooker_type =
TypeDescriptor::FromClassname(obfuscated_signature_.c_str()); TypeDescriptor::FromClassname(JUTFString(env, hooker_name).get());
auto *hooker_field = cbuilder.CreateField("hooker", hooker_type) auto *hooker_field = cbuilder.CreateField("hooker", hooker_type)
.access_flags(dex::kAccStatic) .access_flags(dex::kAccStatic)
@ -212,13 +210,10 @@ namespace lspd {
"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;Z)Z"), "(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;Z)Z"),
LSP_NATIVE_METHOD(Yahfa, isHooked, "(Ljava/lang/reflect/Executable;)Z"), LSP_NATIVE_METHOD(Yahfa, isHooked, "(Ljava/lang/reflect/Executable;)Z"),
LSP_NATIVE_METHOD(Yahfa, buildHooker, LSP_NATIVE_METHOD(Yahfa, buildHooker,
"(Ljava/lang/ClassLoader;C[CLjava/lang/String;)Ljava/lang/Class;"), "(Ljava/lang/ClassLoader;C[CLjava/lang/String;Ljava/lang/String;)Ljava/lang/Class;"),
}; };
void RegisterYahfa(JNIEnv *env, std::string obfuscated_signature) { void RegisterYahfa(JNIEnv *env) {
std::replace(obfuscated_signature.begin(), obfuscated_signature.end(), '/', '.');
obfuscated_signature_ = obfuscated_signature.substr(1) + ".LspHooker";
LOGD("RegisterYahfa: obfuscated_signature_=%s", obfuscated_signature_.c_str());
REGISTER_LSP_NATIVE_METHODS(Yahfa); REGISTER_LSP_NATIVE_METHODS(Yahfa);
} }

View File

@ -30,6 +30,6 @@ namespace lspd {
std::vector<std::pair<void*, void*>> getJitMovements(); std::vector<std::pair<void*, void*>> getJitMovements();
void RegisterYahfa(JNIEnv *, std::string obfuscated_signature); void RegisterYahfa(JNIEnv *);
} // namespace lspd } // namespace lspd

View File

@ -309,7 +309,7 @@ namespace lspd {
return app_binder; return app_binder;
} }
std::tuple<int, size_t, std::string> Service::RequestLSPDex(JNIEnv *env, const ScopedLocalRef<jobject> &binder) { std::tuple<int, size_t> Service::RequestLSPDex(JNIEnv *env, const ScopedLocalRef<jobject> &binder) {
auto data = JNI_CallStaticObjectMethod(env, parcel_class_, obtain_method_); auto data = JNI_CallStaticObjectMethod(env, parcel_class_, obtain_method_);
auto reply = JNI_CallStaticObjectMethod(env, parcel_class_, obtain_method_); auto reply = JNI_CallStaticObjectMethod(env, parcel_class_, obtain_method_);
auto res = JNI_CallBooleanMethod(env, binder, transact_method_, auto res = JNI_CallBooleanMethod(env, binder, transact_method_,
@ -318,18 +318,15 @@ namespace lspd {
reply, 0); reply, 0);
if (!res) { if (!res) {
LOGE("Service::RequestLSPDex: transaction failed?"); LOGE("Service::RequestLSPDex: transaction failed?");
return {-1, 0, ""}; return {-1, 0};
} }
auto parcel_fd = JNI_CallObjectMethod(env, reply, read_file_descriptor_method_); auto parcel_fd = JNI_CallObjectMethod(env, reply, read_file_descriptor_method_);
int fd = JNI_CallIntMethod(env, parcel_fd, detach_fd_method_); int fd = JNI_CallIntMethod(env, parcel_fd, detach_fd_method_);
auto size = JNI_CallLongMethod(env, reply, read_long_method_); auto size = JNI_CallLongMethod(env, reply, read_long_method_);
auto signature = JNI_CallObjectMethod(env, reply, read_string_method_);
JNI_CallVoidMethod(env, data, recycleMethod_); JNI_CallVoidMethod(env, data, recycleMethod_);
JNI_CallVoidMethod(env, reply, recycleMethod_); JNI_CallVoidMethod(env, reply, recycleMethod_);
JUTFString sign(env, static_cast<jstring>(signature.get())); LOGD("Service::RequestLSPDex fd=%d, size=%zu", fd, size);
return {fd, size};
LOGD("Service::RequestLSPDex fd=%d, size=%zu, sign=%s", fd, size, sign.get());
return {fd, size, sign.get()};
} }
} // namespace lspd } // namespace lspd

View File

@ -57,7 +57,7 @@ namespace lspd {
ScopedLocalRef<jobject> RequestApplicationBinderFromSystemServer(JNIEnv *env, const ScopedLocalRef<jobject> &system_server_binder); ScopedLocalRef<jobject> RequestApplicationBinderFromSystemServer(JNIEnv *env, const ScopedLocalRef<jobject> &system_server_binder);
std::tuple<int, size_t, std::string> RequestLSPDex(JNIEnv *env, const ScopedLocalRef<jobject> &binder); std::tuple<int, size_t> RequestLSPDex(JNIEnv *env, const ScopedLocalRef<jobject> &binder);
private: private:
inline static std::unique_ptr<Service> instance_ = std::make_unique<Service>(); inline static std::unique_ptr<Service> instance_ = std::make_unique<Service>();

View File

@ -34,5 +34,5 @@ public class Yahfa {
public static native boolean isHooked(Executable member); public static native boolean isHooked(Executable member);
public static native Class<?> buildHooker(ClassLoader appClassLoader, char returnType, char[] params, String methodName); public static native Class<?> buildHooker(ClassLoader appClassLoader, char returnType, char[] params, String methodName, String hookerName);
} }

View File

@ -105,7 +105,7 @@ public class HookerDexMaker {
} }
private void doMake(String methodName) throws Exception { private void doMake(String methodName) throws Exception {
Class<?> hookClass = Yahfa.buildHooker(LspHooker.class.getClassLoader(), getDescriptor(mReturnType), getDescriptors(mActualParameterTypes), methodName); Class<?> hookClass = Yahfa.buildHooker(LspHooker.class.getClassLoader(), getDescriptor(mReturnType), getDescriptors(mActualParameterTypes), methodName, LspHooker.class.getCanonicalName());
if (hookClass == null) throw new IllegalStateException("Failed to hook " + methodName); if (hookClass == null) throw new IllegalStateException("Failed to hook " + methodName);
// Execute our newly-generated code in-process. // Execute our newly-generated code in-process.
Method backupMethod = hookClass.getMethod(METHOD_NAME_BACKUP, mActualParameterTypes); Method backupMethod = hookClass.getMethod(METHOD_NAME_BACKUP, mActualParameterTypes);

View File

@ -54,7 +54,6 @@ public class LSPApplicationService extends ILSPApplicationService.Stub {
ParcelFileDescriptor pfd = ParcelFileDescriptor.fromFd(ObfuscationManager.preloadDex()); ParcelFileDescriptor pfd = ParcelFileDescriptor.fromFd(ObfuscationManager.preloadDex());
reply.writeFileDescriptor(pfd.getFileDescriptor()); reply.writeFileDescriptor(pfd.getFileDescriptor());
reply.writeLong(ObfuscationManager.getPreloadedDexSize()); reply.writeLong(ObfuscationManager.getPreloadedDexSize());
reply.writeString(ObfuscationManager.getObfuscatedSignature());
} catch (IOException ignored) { } catch (IOException ignored) {
Log.e(TAG, "LSPApplicationService.onTransact: ParcelFileDescriptor.fromFd failed"); Log.e(TAG, "LSPApplicationService.onTransact: ParcelFileDescriptor.fromFd failed");
return false; return false;