The principle of Androge repair

Source: Internet
Author: User

Han Mengfei sha Han Yafei [email protected] yue31313 Han_meng_fei_sha

#热修复技术

The app sends out the package early, if the client problem arises, it is anxious, spilt. So the online repair program is imminent.

# # #概述

Based on the idea in the xposed, by modifying the C-Layer method instance description, the behavior of the corresponding Java methods can be changed to achieve the purpose of repair.

# # #Xposed

Born in the XDA Forum, similar to an application platform, the difference is that it offers many system-level applications. Many magical functions can be achieved. Xposed need to jailbreak as a prerequisite, like the Cydia in iOS.

Xposed can modify any Java method (root) of any program, provided on GitHub with Xposedinstaller, is an Android app. Provides a number of framework layers, application-level programs. Developers can develop some system or application-related plugins, customize the Android system, and can even do dynamic rights Management (Xposedmods).

# # #Android系统启动与应用启动

The zygote process is a permanent process called ' fertilized eggs ' after the Android phone system is launched.

    • Zygote startup implementation script in the/init.rc file
    • Binary files executed during startup are/system/bin/app_process

When any application starts, it will fork out a new process from the zygote process. and load some of the necessary class,invoke some initialization methods. This includes such things as:

    • Activitythread
    • Servicethread
    • Applicationpackagemanager

The necessary classes are triggered, such as: Handlebindapplication, the initialization method that binds this process to the corresponding application, and a copy of the Dalvik virtual machine instance in the zygote process, Therefore, each application process has its own instance of the Dalvik virtual machine, which loads the existing Java runtime into the process, and registers some of the JNI methods of the Android core class into the virtual machine, supporting the boot process from C to Java.

# # #Xposed做了手脚

Xposed in this process rewrote app_process (source code in XPOSED:A modified app_process binary), replacing/system/bin/app_process this binary file. Then two things were done:

    1. Through the xposed hook technique, in the above process, the methods of loading classes mentioned above are hook.
    2. Load Xposedbridge.jar

The necessary method of hook is to facilitate developers to develop plug-ins for it, loading Xposedbridge.jar is to provide the basis for dynamic hooks. Loading it at this time means that all programs can load this jar (because of the fork process mentioned above) at startup. Combined with hook technology, it achieves all the methods of controlling all programs.

To obtain read and write access to the/system/bin/directory, root must be the prerequisite.

# # #Xposed的hook思想

So how does xposed hook the Java method? To look from Xposedbridge, focus on the Xposedbridge.hookmethod (the member object of the original method, the Xc_methodhook object containing the new method);

private native synchronized static void hookMethodNative(Member method, Class<?> declaringClass, int slot, Object additionalInfo);

This native method, through this method, allows the hook method to turn to a C method of the native layer. How do you do that?

When a transmit from java to native occurs, dvm sets up a native stack.In dvmCallJNIMethod(), dvmPlatformInvoke is used to call the native method(signature in Method.insns).

In the middle world of JNI, type data is communicated by the JNI table to the world of Java and C, and the method is operated by a C + + pointer in conjunction with the API method of the dvm* system (such as DVMSLOTTOMETHOD,DVMDECODEINDIRECTREF, etc.) to manipulate the virtual machine, Thus realizing the world of Java Methods and C methods.

Then the hook process is like this: first through the dexclassload to load the method to hook, analysis class, into the C layer, see Code xposedbridge_hookmethodnative method, get to Hook methods class, The method* pointer is then obtained through the Dvmslottomethod method,

Method* method = dvmSlotToMethod(declaredClass, slot);

Declaredclass is the class in which the hook method is located, corresponding to the jobject. The slot is the method class, which describes the index of this Java object in the VM, and by doing so, we get the C-layer methods pointer, by

SET_METHOD_FLAG(method, ACC_NATIVE);

Mark the method as a native method, and then pass the

method->nativeFunc = &hookedMethodCallback;

The directional C-layer method is hookedmethodcallback so that when the hook's Java method executes, it is transferred to the C-layer Hookedmethodcallback method.

By Meth->nativefunc redirecting Methodcallbridge to hookedmethodcallback this method, controlling this C + + pointer is ignoring Java's private.

In addition, in the method structure there are

method->insns = (const u2*) hookInfo;

Use Insns to point to the replacement method so that Hookedmethodcallback can get the Java method that is really expected to be executed.

Now all the hooks are pointing to the Hookedmethodcallbackc method, and then implementing the call substitution into the Java method in this method.

# # #从Xposed提炼精髓

Review xposed, with Root as a prerequisite, in App_ The process loads the Xposedbidge.jar, thus realizing the ability to hook all the methods of all applications, and the subsequent dynamic hook application method, in fact, only load the Zypote process copied from the runtime of this xposedbidge.jar, and then hook it. Therefore, if the hook,root in an application scope is not necessary, but simply the implementation of the load hook, you can modify the method of the application.

In the industry there is also the "modification of the Basedexclassloader in the PathList, to dynamically load dex" way to achieve thermal repair. " The latter is pure Java implementation, but requires the hack class of optimization process, will hit the class_ispreverified tag class, remove this label to solve the class and class reference is not in a Dex exception problem. This will discard the Dex optimize optimizations for startup run speed. In principle, this is more noticeable for applications where the number of methods is not as large as the need for Multidex. The former does not offend the original optimization process, only point to kill the method that needs to hook, more pure, effective.

The principle of Androge repair

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.