A parameter statistics method and device based on hook
By hooking the calling portal of the method in the native layer of the application and replacing the execution address, the problem of the inability to accurately count the application method call parameters in the prior art is solved, and high-accuracy parameter statistics in the Java layer are achieved.
Patent Information
- Application Number
- CN202110780229.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-09
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2041-07-09
AI Technical Summary
The prior art is difficult to accurately count the calling parameters of a method in an application, especially when the application is composed of multiple modules and the source code is complex, resulting in the inability to implement dynamic statistics of method calling parameters.
When the target method is called in the Java layer of the application, the call portal of the method is hooked in the native layer and replaced the execution address of the target method with the execution address of the hook method before the method is executed. After the hook method is executed, the target method is selected. Perform address restoration to realize parameter statistics when the target method is called.
Without affecting the normal execution of the target method, accurate statistics of parameters when the method is called is achieved, and the accuracy of statistics is improved.
Smart Images

Figure CN113485903B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a parameter statistics method and device based on hook. Background Art
[0002] An application usually integrates multiple methods. In some specific application scenarios, the calling parameters of a method in the application are counted. For example, during the development or use of an application, the number of calls to privacy methods or other specific methods in the application is usually counted to determine whether the application complies with privacy compliance or whether there are security risks.
[0003] Currently, when counting the calling parameters of application methods, most of them are analyzed based on source code. However, applications are usually composed of multiple modules, and it is often difficult to accurately obtain the calling parameters of methods by analyzing the source code, resulting in the inability to count the calling parameters of methods. Summary of the invention
[0004] The embodiments of the present application provide a hook-based parameter statistics method and device, which are used to solve the problem that it is currently impossible to accurately count the call parameters of methods in applications.
[0005] To solve the above technical problems, the embodiments of the present application are implemented as follows:
[0006] First, a parameter statistics method based on hook is proposed, including:
[0007] When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application;
[0008] The execution address of the target method is replaced with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called;
[0009] Calling and executing the hook method in the native layer to obtain the target parameters;
[0010] The execution address of the target method is restored and the target method is executed in the native layer.
[0011] In the second aspect, a parameter statistics device based on hook is proposed, comprising:
[0012] A processing unit, which hooks a calling entry of the target method in the native layer of the application when the Java layer of the application calls the target method in the application;
[0013] an address replacement unit, which replaces the execution address of the target method with the execution address of a predefined hook method, wherein the hook method is used to obtain the target parameters when the target method is called;
[0014] A first execution unit calls and executes the hook method in the native layer to obtain the target parameter;
[0015] The second execution unit restores the execution address of the target method and executes the target method in the native layer.
[0016] In a third aspect, an electronic device is provided, the electronic device comprising:
[0017] Processor; and
[0018] A memory arranged to store computer executable instructions which, when executed, cause the processor to:
[0019] When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application;
[0020] The execution address of the target method is replaced with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called;
[0021] Calling and executing the hook method in the native layer to obtain the target parameters;
[0022] The execution address of the target method is restored and the target method is executed in the native layer.
[0023] In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores one or more programs, and when the one or more programs are executed by an electronic device including a plurality of application programs, the electronic device performs the following method:
[0024] When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application;
[0025] The execution address of the target method is replaced with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called;
[0026] Calling and executing the hook method in the native layer to obtain the target parameters;
[0027] The execution address of the target method is restored and the target method is executed in the native layer.
[0028] At least one of the above technical solutions adopted in the embodiments of the present application can achieve the following beneficial effects:
[0029] When the target method is called in the Java layer of the application, the calling entry of the target method can be hooked in the native layer of the application, and the execution address of the target method can be replaced with the execution address of the predefined hook method, and the statistical logic in the hook method can be called in the native layer to obtain the target parameters when the target method is called. After the hook method is executed and the target parameters are obtained, the execution address of the target method can be restored and the target method can be executed. In this way, when the target method is called in the Java layer, by hooking the calling entry of the method in the native layer, replacing the execution address of the target method with the execution address of the hook method before the method is executed, and restoring the execution address of the target method after the hook method is executed, the parameter statistics when the target method is called can be realized without affecting the normal execution of the target method. In addition, since the hook method can be executed to count the calling parameters each time the target method is called, the parameters obtained by statistics are highly accurate. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0031] Figure 1 It is a flowchart of a parameter statistics method based on hook in one embodiment of the present application;
[0032] Figure 2 It is a flowchart of a parameter statistics method based on hook in one embodiment of the present application;
[0033] Figure 3 is a schematic diagram of a parameter statistics method based on hook in one embodiment of the present application;
[0034] Figure 4 is a schematic diagram of the structure of an electronic device according to an embodiment of the present application;
[0035] Figure 5 It is a structural diagram of a parameter statistics device based on hook in one embodiment of the present application. DETAILED DESCRIPTION
[0036] In order to enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.
[0037] At present, in order to protect the privacy and security of users, when the number of calls to the privacy methods in the application (used to obtain the user's private data or other sensitive data) is too many, the application will be prohibited from further access, or the application will be prohibited from further access after being mistaken for a virus, etc., resulting in the application being unable to operate normally. To avoid this situation, before the application is put on the shelves or during the user's use of the application after the application is put on the shelves, it is necessary to monitor and count parameters such as the number of calls to the privacy methods or other specific methods in the application to determine whether the application meets privacy compliance or whether there are security risks.
[0038] In the prior art, when counting the calling parameters of the methods in the application, the source code of the application is usually analyzed. However, this method can only achieve static statistics, and cannot achieve dynamic statistics during the operation of the application. In addition, since the application is usually composed of multiple third-party SDKs, the source code of the application is relatively complex, and the source code of some third-party SDKs is not easy to obtain, which results in the inability to accurately obtain the parameters when the method is called through the source code analysis. It can be seen that there is currently a problem of being unable to accurately count the calling parameters of the methods in the application.
[0039] In order to solve the above technical problems, an embodiment of the present application provides a hook-based parameter statistics method and device. When calling a target method in the Java layer of an application, the calling entry of the method is hooked in the native layer of the application, and the execution address of the target method is replaced with the execution address of the hook method before the method is executed. After the hook method is executed, the execution address of the target method is restored. This can achieve parameter statistics when the target method is called without affecting the normal execution of the target method. In addition, since the hook method can be executed to count the calling parameters each time the target method is called, the parameters obtained by statistics are highly accurate.
[0040] It should be noted that the technical solution provided in the embodiment of the present application can not only realize parameter statistics when privacy methods and certain specific methods are called, but also realize parameter statistics when other methods in the application are called. These methods can be methods in the Java layer of the application or methods in the native layer of the application, which are not specifically limited in the embodiment of the present application. In addition, when performing parameter statistics, parameter statistics can be performed when the application is tested before the application is put on the shelves, or parameter statistics can be performed when the user uses the application after the application is put on the shelves, which is not specifically limited here.
[0041] The technical solutions provided by various embodiments of the present application are described in detail below in conjunction with the accompanying drawings.
[0042] Figure 1 FIG. 1 is a flow chart of a parameter statistics method based on hook according to an embodiment of the present application. The method is described as follows.
[0043] S102: When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application.
[0044] An application can be understood as an application based on the Android operating system. Under the Android operating system, the application may include a Java layer and a native layer, and the application may include multiple methods, which may be methods in the Java layer or methods in the native layer. Specifically, they may be privacy methods (used to obtain privacy data) or other methods.
[0045] When the Java layer in the application calls a target method in the application, the Java layer and the native layer in the application will interact. In this embodiment, when the Java layer and the native layer in the application interact, the calling entry of the called target method will be hooked in the native layer, and the calling entry can be understood as the memory address of the target method.
[0046] Optionally, in order to facilitate the Java layer to hook the target method's calling entry in the native layer when calling the target method, when initializing the Java layer, the Java layer can be made to call the communication interface JNI (Java native interface) provided by the native layer to establish communication between the Java layer and the native layer. In this way, when the Java layer calls the target method, the Java layer can interact with the native through the JNI interface called during initialization, so that the target method's calling entry can be hooked in the native layer. Initializing the Java layer can be understood as initializing the application process, that is, when the application process is started, the Java layer in the application is initialized.
[0047] S104: replacing the execution address of the target method with the execution address of the predefined hook method, where the hook method is used to obtain the target parameters when the target method is called.
[0048] When the native layer has hooked the calling entry of the target method, the execution address of the target method can be replaced with the execution address of the predefined hook method in the native layer. The hook method is used to obtain the target parameters when the target method is called.
[0049] The above hook method can be predefined in the Java layer. Specifically, before the Java layer calls the target method, the target method information and the statistical logic of the hook method can be predefined in the Java layer. The target method information can specifically be the class name, method name and parameters of the target method. The statistical logic of the hook method can be understood as how to obtain the target parameters by statistics.
[0050] The target parameters used for statistics by the hook method can be the number of calls to the target method, the duration of the call, the time when the call is made, etc., and examples will not be given here one by one. Among them, if the target parameter is the number of calls, the number of calls can specifically be the total number of times the target method is called within the set time, and the corresponding statistical logic of the hook method can be that within the set time, each time the Java layer calls the target method, the number of calls to the target method is accumulated and added by one, so that the total number of calls to the target method within the set time can be counted. The call duration can be a single word call duration, or it can be the cumulative duration of multiple calls, and the time when it is called is the specific time when the target method is called.
[0051] In this embodiment, the target method can be mapped to the ArtMethod structure at the native layer, and the ArtMethod can store information such as the class, access rights, and execution address of the target method. In this way, by mapping the target method to the ArtMethod structure, it is convenient to replace the execution address of the target method with the execution address of the hook method later.
[0052] The above ArtMethod also has an attribute entry_point_from_quick_compiled_code_, wherein when the execution address of the target method is replaced with the execution address of the hook method, specifically, the memory address of the attribute entry_point_from_quick_compiled_code_ of ArtMethod can be replaced with the execution address of the hook method.
[0053] In a more specific implementation, considering that the target method is executed on the CPU, when executing the target method on the CPU, the method needs to be converted into machine language and then into assembly language, and finally obtain instructions that can be executed on the CPU. Therefore, when replacing the execution address of the target method with the execution address of the predefined hook method, the replacement of the execution address can be achieved based on the method of replacing the instruction set, that is, when replacing the execution address, the corresponding instruction set can be replaced. Among them, considering that the CPU has different architectures, different CPU architectures are adapted to different instruction sets, that is, different CPU architectures correspond to different processing instruction sets. Therefore, the adapted instruction set can be selected based on the CPU currently running the application to replace the execution address.
[0054] S106: Call and execute the hook method in the native layer to obtain target parameters.
[0055] In the native layer, after replacing the execution address of the target method with the execution address of the predefined hook method, the hook method can be called and executed in the native layer. After the hook method is executed, the target parameters to be counted can be obtained.
[0056] It should be noted that if the target parameter to be counted is the number of calls within a set time, after the hook method is executed, the statistical result is the total number of calls to the target method within the set time, that is, each time the target method is called within the set time, the statistical result can be accumulated and added by one.
[0057] S108: Restore the execution address of the target method and execute the target method in the native layer.
[0058] After executing the hook method and obtaining the target parameters, the execution address of the target method can be restored, that is, the execution address of the target method can be restored from the execution address of the hook method to the execution address of the target method itself. After restoring the execution address of the target method, the target method can be executed in the native layer to ensure the normal execution of the target method.
[0059] It can be seen that when the target method is called at the Java layer, the target method's call entry is hooked at the native layer, and the target method's execution address is replaced with the execution address of the hook method for parameter statistics before the target method is executed. After the hook method is executed, the target method's execution address is restored, so that the target method is executed and the hook method also performs statistics and monitoring. The technical solution provided by the embodiment of the present application is less invasive to the application. It only needs to define the method for parameter statistics and the target method to be monitored at the Java layer. Since the hook is implemented by replacing the execution address at the native layer, there is almost no negative impact on the performance of the application.
[0060] Optionally, after obtaining the target parameters, the target parameters may be returned to the Java layer so that the Java layer outputs the target parameters obtained by statistics.
[0061] Optionally, after obtaining the target parameters, it is also possible to determine whether the application complies with privacy compliance or whether there are security risks based on the target parameters. If it does not comply with privacy compliance or there are security risks, the application can be prohibited from running or repaired.
[0062] With respect to the technical solution provided in the embodiments of the present application, in a possible application scenario, the Java layer may be a target method called in response to a specified operation of a user or a terminal operating system on an application. Specifically, during the running of the application, the user or the operating system of the terminal on which the application is installed will perform a specified operation on the application, and the specified operation can be used to obtain a specified resource in the application, and the specified resource may be a resource that has been pre-stored in the native layer of the application. After the application receives the specified operation, based on the specified operation, the Java layer in the application will call the target method in the application, and the target method corresponds to the specified operation, and can be used to call the specified resource corresponding to the specified operation from the native layer of the application. Among them, the correspondence between the target method and the specified operation can be understood as that the specified resource to be obtained by the specified operation can be obtained from the native layer by calling and executing the target method.
[0063] In a more specific implementation, the specified resource may be sensitive data in the native layer, the specified operation may be an operation for obtaining sensitive data, and accordingly, the target method may be a privacy method, specifically used to call sensitive data from the native layer of the application. The sensitive data may be the user's personal data or other private data in the application.
[0064] In this way, when the Java layer in the application calls the privacy method to obtain sensitive data, the calling entry of the privacy method is hooked in the native layer, and the execution address of the privacy method is replaced with the execution address of the hook method. After the statistical logic of the hook method is executed, the execution address of the privacy method is restored and the privacy method is executed. In this way, the call of the privacy method can be monitored, and the calling parameters when the privacy method is called can be obtained by statistics. After that, it is also possible to judge whether the application complies with privacy compliance or whether there are security risks based on the statistical calling parameters of the privacy method. For example, if the number of times the privacy method is called within the set time is not greater than the set threshold, it can be considered that the application complies with privacy compliance and there is no security risk. Conversely, if the number of times the privacy method is called within the set time is greater than the set threshold, it can be considered that the application does not comply with privacy compliance and there is a security risk.
[0065] Optionally, in other implementations, the specified resource obtained by the above-mentioned specified operation may also be other data, for example, application data generated during the operation of the application, etc. Correspondingly, the target method may be a method for calling other data from the native layer. In this way, when the Java layer in the application calls the target method to obtain other data in the application, the parameter statistics method provided in the embodiment of the present application can be used to monitor the call of the target method and obtain the call parameters when the target method is called, thereby knowing the call and use of other data in the application during the operation of the application, so as to understand the operation details of the application.
[0066] Optionally, in actual applications, the implementation logic of the hook-based parameter statistics method provided in the embodiment of the present application in the native layer can also be packaged into a so file, and the so file can be regarded as an intermediate file for communication between the native layer and the Java layer. In this way, when the Java layer calls the method in the application, the so file can be loaded by calling the JNI interface between the native layer, so that the parameter statistics when the method is called can be implemented through the technical solution provided in the embodiment of the present application.
[0067] To facilitate understanding of the technical solutions provided in the embodiments of the present application, please refer to Figure 2 . Figure 2A flow chart of a parameter statistics method based on hook according to an embodiment of the present application is shown below. Figure 2 The illustrated embodiment specifically includes the following steps.
[0068] S201: pre-define information of a target method for which parameter statistics need to be performed and statistical logic of a hook method for performing parameter statistics in the Java layer of the application.
[0069] S202: When initializing the Java layer, the Java layer calls the interface JNI provided by the native layer.
[0070] S203: Receive a specified operation on the application from the user or the operating system of the terminal.
[0071] S204: In response to the specified operation, a target method corresponding to the specified operation is called at the Java layer.
[0072] S205: Hook the calling entry of the target method in the native layer of the application.
[0073] S206: Map the target method to the structure of ArtMethod, and replace the memory address of the attribute entry_point_from_quick_compiled_code_ of ArtMethod with the execution address of the hook method based on the method of replacing the instruction set.
[0074] S207: calling and executing the hook method in the native layer to obtain the target parameters when the target method is called.
[0075] S208: Restore the execution address of the target method and execute the target method in the native layer.
[0076] The specific implementation of the above methods S201 to S208 can be found in Figure 1 The specific implementation of the corresponding steps in the illustrated embodiment will not be repeated here.
[0077] Figure 2 In the embodiment shown, when calling the target method in the Java layer, the calling entry of the method is hooked in the native layer, and the execution address of the target method is replaced with the execution address of the hook method before the method is executed, and the execution address of the target method is restored after the hook method is executed. This can achieve parameter statistics when the target method is called without affecting the normal execution of the target method. In addition, since the hook method can be executed to count the calling parameters each time the target method is called, the parameters obtained by statistics are highly accurate.
[0078] The following will take the target method as the system method Log.e and the target parameter as the number of calls as an example to describe in detail the specific implementation of the technical solution provided in the embodiments of the present application.
[0079] Step 1: Define the statistical logic of Log.e information and hook methods in the Java layer.
[0080] The specific code can be as follows:
[0081]
[0082] In the above code, className is the class name Log, methodName is the method e, methodSig is the parameter, and the logic in the hookTest method is the statistical logic. This hookTest method is called before each call to the Log.e method. In this method, the cumulative number of calls to the Log.e method is recorded.
[0083] Step 2: When the Java layer is initialized, the interface JNI provided by the native layer is called. When the Java layer calls the method Log.e, the calling entry of the method Log.e can be hooked in the native layer.
[0084] The code looks like this:
[0085]
[0086] Step 3: Set a button event in the application's UI homepage. When the user clicks the button, the Log.e method is called 200 times in a loop.
[0087] Step 4: Log.e's corresponding ArtMethod property in the native layer is hooked before, so the entry point is replaced with the entry address of the previous hookTest method, and then the hookTest method is called. After the call is completed, the backup method is used to return to the original system method Log.e.
[0088] The entire process can be found in Figure 3 . Figure 3 The application includes a Java layer and a native layer. The Java layer defines a target method Log.e that needs to be counted and a hook method for parameter statistics. When the method Log.e is called in the Java layer, based on the information of the method Log.e defined in the Java layer, the calling entry of the method Log.e can be hooked in the native layer. After that, the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod of the method Log.e can be replaced by replacing the instruction set. Figure 3 The two gray blocks at the bottom of the eight blocks represented by the method Log.e shown in the figure are replaced with the execution address of the hook method ( Figure 3 The two gray blocks at the bottom of the eight blocks represented by the hook method shown), and then the statistical logic in the hook method is executed.
[0089] After executing the hook method, restore the execution address of the method Log.e. Figure 3 As shown, the execution address of the method Log.e can be restored based on the backup method. After restoring the execution address of the method Log.e, the method Log.e can be executed.
[0090] In general, when Log.e is called, the system will combine the parameters of Log.e, use the entry_point_from_quick_compiled_code_ address as the entry point, and execute the Log.e method in the native layer. However, this application replaces the method entry address with a trampoline in the native layer, and jumps and restores on this instruction. The restored backup pointer points to the entry point of Log.e, which will return to the original method execution.
[0091] Step 5: The Java layer calls the Log.e method. After the hook, it first calls the HookTest method in the Hook_Log_e class and then calls the original Log.e method. Finally, it can be counted that the Log.e method is called two hundred times when the application is running.
[0092] The technical solution provided in the embodiment of the present application can replace the ArtMethod method entry based on the native layer inline hook technology to implement the Java layer full method hook, so as to achieve the purpose of monitoring and counting any Java layer method call; hook from the native layer to realize the address replacement and restoration of the method call, which has higher performance, less invasiveness, greater security and efficiency than the Java layer proxy hook; at the Java layer, you only need to pay attention to the methods and statistical logic that need to be counted and do not need to pay attention to the hook details, and support the Java layer full method hook, which is more friendly to developers.
[0093] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0094] Figure 4 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. Figure 4 At the hardware level, the electronic device includes a processor, and optionally also includes an internal bus, a network interface, and a memory. The memory may include a memory, such as a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage. Of course, the electronic device may also include hardware required for other services.
[0095] The processor, network interface and memory can be interconnected through an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0096] The memory is used to store the program. Specifically, the program may include a program code, and the program code includes a computer operation instruction. The memory may include a memory and a non-volatile memory, and provides instructions and data to the processor.
[0097] The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it, forming a hook-based parameter statistics device at the logical level. The processor executes the program stored in the memory and is specifically used to perform the following operations:
[0098] When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application;
[0099] The execution address of the target method is replaced with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called;
[0100] Calling and executing the hook method in the native layer to obtain the target parameters;
[0101] The execution address of the target method is restored and the target method is executed in the native layer.
[0102] The above application Figure 4 The method performed by the hook-based parameter statistics device disclosed in the illustrated embodiment can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor or an instruction in the form of software. The above processor may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in conjunction with the embodiments of the present application can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module can be located in a storage medium mature in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.
[0103] The electronic device may also perform Figure 1 and Figure 2 Method, and implement the parameter statistics device based on hook Figure 1 and Figure 2 The functions in the illustrated embodiment will not be described in detail in the embodiments of the present application.
[0104] Of course, in addition to software implementation methods, the electronic device of the present application does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0105] The embodiment of the present application also provides a computer-readable storage medium, which stores one or more programs, wherein the one or more programs include instructions, which, when executed by a portable electronic device including a plurality of application programs, enable the portable electronic device to execute Figure 1 and Figure 2 The method of the embodiment shown is specifically used to perform the following operations:
[0106] When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application;
[0107] The execution address of the target method is replaced with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called;
[0108] Calling and executing the hook method in the native layer to obtain the target parameters;
[0109] The execution address of the target method is restored and the target method is executed in the native layer.
[0110] Figure 5 This is a schematic diagram of the structure of a parameter statistics device 50 based on hook in one embodiment of the present application. Figure 5 In a software implementation, the hook-based parameter statistics device 50 may include: a processing unit 51, an address replacement unit 52, a first execution unit 53 and a second execution unit 54, wherein:
[0111] The processing unit 51 hooks a calling entry of the target method in the native layer of the application when the Java layer of the application calls the target method in the application;
[0112] An address replacement unit 52 replaces the execution address of the target method with the execution address of a predefined hook method, wherein the hook method is used to obtain the target parameters when the target method is called;
[0113] A first execution unit 53 calls and executes the hook method in the native layer to obtain the target parameter;
[0114] The second execution unit 54 restores the execution address of the target method and executes the target method in the native layer.
[0115] Optionally, before the Java layer calls the target method, information of the target method and statistical logic of the hook method are predefined in the Java layer, and the information of the target method includes the class name, method name and parameters of the target method.
[0116] Optionally, the target method is mapped to a structure of ArtMethod;
[0117] The address replacement unit 52 replaces the execution address of the target method with the execution address of the predefined hook method, including:
[0118] Replace the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod with the execution address of the hook method.
[0119] Optionally, when initializing the Java layer, the Java layer calls the interface JNI provided by the native layer, so that the processing unit 51 hooks the calling entry of the target method in the native layer when the Java layer calls the target method.
[0120] Optionally, the address replacement unit 52 replaces the execution address of the target method with the execution address of a predefined hook method, including:
[0121] Based on the method of replacing the instruction set, the execution address of the target method is replaced with the execution address of the predefined hook method. Different CPU architectures are adapted to different instruction sets.
[0122] Optionally, the processing unit 51 calls a target method in the application at a Java layer of the application, including:
[0123] receiving a specified operation of the application by a user or an operating system of a terminal, wherein the specified operation is used to obtain a specified resource in the native layer;
[0124] In response to the specified operation, the target method is called in the Java layer of the application, the target method corresponds to the specified operation, and the target method is used to call the specified resource from the native layer.
[0125] Optionally, the designated resource includes sensitive data in the native layer, the designated operation includes an operation for obtaining the sensitive data, the target method includes a privacy method, and the target method is used to call the sensitive data from the native layer.
[0126] The parameter statistics device 50 based on hook provided in the embodiment of the present application can also execute Figure 1 and Figure 2 Method, and implement the parameter statistics device based on hook Figure 1 and Figure 2 The functions of the illustrated embodiment will not be described in detail in the embodiments of the present application.
[0127] In short, the above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
[0128] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0129] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0130] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0131] Each embodiment in this application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
Claims
1. A parameter statistics method based on hook, characterized in that: include: When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application; Replace the execution address of the target method with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called, and the target parameters include the number of times the target method is called; Calling and executing the hook method in the native layer to obtain the target parameter, so as to determine whether the application complies with privacy compliance according to the number of calls within a set time; Restoring the execution address of the target method and executing the target method in the native layer; Calling a target method in the application at the Java layer of the application includes: receiving a specified operation of the application by a user or an operating system of a terminal, wherein the specified operation is used to obtain a specified resource in the native layer; In response to the specified operation, calling the target method in the Java layer of the application, the target method corresponding to the specified operation, and the target method is used to call the specified resource from the native layer; The designated resource includes sensitive data in the native layer, the designated operation includes an operation for obtaining the sensitive data, the target method includes a privacy method and the target method is used to call the sensitive data from the native layer; The target method is mapped to the structure of ArtMethod; Wherein, replacing the execution address of the target method with the execution address of the predefined hook method includes: Replace the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod with the execution address of the hook method.
2. The parameter statistics method according to claim 1, characterized in that: Before the Java layer calls the target method, the target method information and the statistical logic of the hook method are predefined in the Java layer, and the target method information includes the class name, method name and parameters of the target method.
3. The parameter statistics method according to claim 1, characterized in that: When the Java layer is initialized, the Java layer calls the interface JNI provided by the native layer, so that when the Java layer calls the target method, the native layer hooks the calling entry of the target method.
4. The parameter statistics method according to claim 1, characterized in that: Replacing the execution address of the target method with the execution address of the predefined hook method includes: Based on the method of replacing the instruction set, the execution address of the target method is replaced with the execution address of the predefined hook method. Different CPU architectures are adapted to different instruction sets.
5. A parameter statistics device based on hook, characterized in that: include: A processing unit, which hooks a calling entry of the target method in the native layer of the application when the Java layer of the application calls the target method in the application; an address replacement unit, which replaces the execution address of the target method with the execution address of a predefined hook method, wherein the hook method is used to obtain a target parameter when the target method is called, and the target parameter includes the number of times the target method is called; A first execution unit, calling and executing the hook method in the native layer to obtain the target parameter, so as to determine whether the application complies with privacy compliance according to the number of calls within a set time; a second execution unit, restoring the execution address of the target method and executing the target method in the native layer; The processing unit calls a target method in the application at the Java layer of the application, including: receiving a specified operation of the application by a user or an operating system of a terminal, wherein the specified operation is used to obtain a specified resource in the native layer; In response to the specified operation, calling the target method in the Java layer of the application, the target method corresponding to the specified operation, and the target method is used to call the specified resource from the native layer; The designated resource includes sensitive data in the native layer, the designated operation includes an operation for obtaining the sensitive data, the target method includes a privacy method and the target method is used to call the sensitive data from the native layer; The target method is mapped to the structure of ArtMethod; Wherein, in the address replacement unit, replacing the execution address of the target method with the execution address of the predefined hook method includes: Replace the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod with the execution address of the hook method.
6. An electronic device, characterized in that: include: processor; as well as A memory arranged to store computer executable instructions which, when executed, cause the processor to: When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application; Replace the execution address of the target method with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called, and the target parameters include the number of times the target method is called; Calling and executing the hook method in the native layer to obtain the target parameter, so as to determine whether the application complies with privacy compliance according to the number of calls within a set time; Restoring the execution address of the target method and executing the target method in the native layer; Calling a target method in the application at the Java layer of the application includes: receiving a specified operation of the application by a user or an operating system of a terminal, wherein the specified operation is used to obtain a specified resource in the native layer; In response to the specified operation, calling the target method in the Java layer of the application, the target method corresponding to the specified operation, and the target method is used to call the specified resource from the native layer; The designated resource includes sensitive data in the native layer, the designated operation includes an operation for obtaining the sensitive data, the target method includes a privacy method and the target method is used to call the sensitive data from the native layer; The target method is mapped to the structure of ArtMethod; Wherein, replacing the execution address of the target method with the execution address of the predefined hook method includes: Replace the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod with the execution address of the hook method.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores one or more programs, which, when executed by an electronic device including a plurality of application programs, cause the electronic device to perform the following method: When the Java layer of the application calls the target method in the application, the calling entry of the target method is hooked in the native layer of the application; Replace the execution address of the target method with the execution address of a predefined hook method, where the hook method is used to obtain the target parameters when the target method is called, and the target parameters include the number of times the target method is called; Calling and executing the hook method in the native layer to obtain the target parameter, so as to determine whether the application complies with privacy compliance according to the number of calls within a set time; Restoring the execution address of the target method and executing the target method in the native layer; Calling a target method in the application at the Java layer of the application includes: receiving a specified operation of the application by a user or an operating system of a terminal, wherein the specified operation is used to obtain a specified resource in the native layer; In response to the specified operation, calling the target method in the Java layer of the application, the target method corresponding to the specified operation, and the target method is used to call the specified resource from the native layer; The designated resource includes sensitive data in the native layer, the designated operation includes an operation for obtaining the sensitive data, the target method includes a privacy method and the target method is used to call the sensitive data from the native layer; The target method is mapped to the structure of ArtMethod; Wherein, replacing the execution address of the target method with the execution address of the predefined hook method includes: Replace the memory address of the attribute entry_point_from_quick_compiled_code_ of the ArtMethod with the execution address of the hook method.
