A method, device and computer equipment for modifying variable memory layout
By statically analyzing the memory address of the target variable of the dynamic library, the problem in the existing technology of relying on jailbreaking or private interfaces to modify the memory layout of the dynamic library is solved, realizing convenient modification without dependence and avoiding the risk of private API access.
Patent Information
- Application Number
- CN201910860114.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-09-11
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2040-07-29
AI Technical Summary
The existing technology needs to rely on jailbreak or private interfaces to modify the memory layout of static/global variables of dynamic libraries, which poses the risk of private interface access and inconvenient jailbreak dependency issues.
By statically analyzing the memory address of the target variable of the dynamic library, using the offset information and random offset in the executable file, the memory address of the target variable is determined and modified, thereby modifying the memory layout of the dynamic library variable and avoiding reliance on jailbreak or private interfaces. .
It realizes the convenient modification of the memory layout of dynamic library variables without jailbreaking or private interfaces, avoids the risk of private API access, and improves the convenience and reliability of operation.
Smart Images

Figure CN111177026B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and in particular, relates to a method, device and computer equipment for modifying variable memory layout. Background Art
[0002] In a terminal application of a computer device, based on some specific requirements, it may be necessary to modify the memory layout of one or more uninitialized static / global variables of a dynamic library introduced by the application.
[0003] Currently, when modifying the data in the memory of static / global variables of a dynamic library, the variable is generally obtained and modified through a function call. Since the data segments of most dynamic libraries are private, the memory modification of the required variable can usually only be achieved by calling a private interface (API, Application Programming Interface). In addition, the memory modification of static / global variables can also be achieved through jailbreaking. That is, the existing technology needs to rely on private interfaces or jailbreaking to modify the memory layout of static / global variables of the dynamic library introduced by the application.
[0004] However, the above-mentioned variable memory modification method based on private interface is prone to some private interface access risks. For example, the private interface may face abandonment due to review, detection, reporting or system upgrade. Once it is unavailable, it will lead to the inability to complete the memory modification of the required variables. Some variables may not even provide any private interface to read and write memory. The jailbreak method depends on the installation and deployment of jailbreak software, which is not convenient enough. Summary of the invention
[0005] In view of this, the purpose of the present application is to provide a method, apparatus and computer device for modifying the memory layout of variables, which are used to modify the memory layout of dynamic library variables by statically analyzing the memory addresses of target variables of the dynamic library, thereby breaking the limitation of the prior art that it is necessary to rely on jailbreaking or private interfaces to modify the memory of dynamic library variables.
[0006] To achieve the above objectives, on the one hand, the present application provides a method for modifying a variable memory layout, the method comprising:
[0007] Load the application's executable file and dependent dynamic libraries into system memory;
[0008] Determine the offset information corresponding to the target variable in the dynamic library in the executable file to obtain a first offset;
[0009] Determining a random offset corresponding to when the executable file is loaded;
[0010] Determine a memory address corresponding to the target variable based on the first offset and the random offset;
[0011] Based on the memory address, modify the memory layout of the target variable. :
[0012] On the other hand, the present application also provides a device for modifying a variable memory layout, the device comprising:
[0013] A loading unit, used to load the application's executable file and dependent dynamic libraries into the system memory;
[0014] A first determining unit, configured to determine offset information corresponding to a target variable in the dynamic library in the executable file, to obtain a first offset;
[0015] A second determining unit, configured to determine a random offset corresponding to when the executable file is loaded;
[0016] A third determining unit, configured to determine a memory address corresponding to the target variable based on the first offset and the random offset;
[0017] A memory modification unit is used to modify the memory layout of the target variable based on the memory address.
[0018] In another aspect, the present application further provides a computer device, the computer device comprising:
[0019] A memory for storing computer executable instructions;
[0020] A processor is used to call and execute the computer executable instructions, wherein the computer executable instructions are at least used to execute the method for modifying the variable memory layout as described above.
[0021] It can be seen from the above scheme that the method, device and computer equipment for modifying the memory layout of variables provided by the present application, after loading the executable file of the application and the dynamic library it depends on into the system memory, further statically analyzes the memory address of the target variable in the dynamic library on which the application depends, and specifically determines the memory address of the target variable based on the first offset corresponding to the target variable in the executable file and the random offset corresponding when the executable file is loaded. On this basis, the memory layout of the target variable is finally modified based on the determined memory address. It can be seen that by applying the scheme of the present application, the purpose of modifying the memory layout of the dynamic library variables can be achieved by statically analyzing the memory address of the variables of the dynamic library, without relying on jailbreaking or private interfaces, and correspondingly avoiding various drawbacks of the prior art, such as the risk of no private API access, etc., and the memory layout of the variables in the dynamic library can be conveniently modified. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] 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 embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0023] Figure 1 It is a schematic diagram of a composition structure of a computer device to which the method for modifying the variable memory layout in the embodiment of the present application is applicable;
[0024] Figure 2 It is a flow chart of a method for modifying variable memory layout in an embodiment of the present application;
[0025] Figure 3 This is a diagram of the file structure of the Mach-O file of iOS;
[0026] Figure 4 It is a schematic diagram of the memory layout corresponding to the operating system process after the executable file Mach-O is loaded into the memory;
[0027] Figure 5 It is a schematic diagram of a flow chart for determining the memory address of a target variable in an embodiment of the present application;
[0028] Figure 6 is another flow chart of determining the memory address of the target variable in an embodiment of the present application;
[0029] Figure 7 is another flow chart of the method for modifying the variable memory layout in an embodiment of the present application;
[0030] Figure 8 It is a structural schematic diagram of a device for modifying variable memory layout in an embodiment of the present application;
[0031] Fig. 9 It is another structural schematic diagram of the device for modifying the variable memory layout in the embodiment of the present application. DETAILED DESCRIPTION
[0032] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0033] As one aspect of an embodiment of the present application, the present application provides a method for modifying the memory layout of variables. The method can modify the memory layout of dynamic library variables by statically analyzing the memory addresses of uninitialized variables of the dynamic library, so as to break the limitation of the prior art that it is necessary to rely on jailbreaking or private interfaces to modify the memory of dynamic library variables.
[0034] Optionally, the above method for modifying the memory layout of variables can be applied in computer devices, such as, but not limited to, terminal devices such as smart phones, tablet computers, personal data assistants, laptops, notebooks, all-in-one computers or desktop PCs (personal computers). As a typical application scenario, it can be applied to applications supporting browser engines, i.e., browser applications, of mobile terminals such as smart phones and tablet computers, for example, specifically applied to iOS applications supporting the browser engine WebKit, and in the application, the memory layout of the required variables can be modified by statically analyzing the memory addresses of uninitialized variables of the dynamic library without relying on jailbreak or private interfaces.
[0035] See also Figure 1 , which shows a schematic diagram of a composition structure of a computer device to which the method for modifying the variable memory layout of the embodiment of the present application is applicable. The computer device is also the computer device disclosed as another aspect of the embodiment of the present application. Figure 1 As shown, the computer device may include: a processor 101 and a memory 102. Of course, it may also include a communication interface 103, an input unit 104, a display 105 and a communication bus 106.
[0036] The processor 101 , the memory 102 , the communication interface 103 , the input unit 104 and the display 105 all communicate with each other via the communication bus 106 .
[0037] In the embodiment of the present application, the processor 101 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, etc.
[0038] The processor 101 may call a program stored in the memory 102 .
[0039] The memory 102 is used to store one or more programs. The program may include program code, and the program code includes computer operation instructions. In the embodiment of the present application, the memory 102 at least stores a program for implementing the following functions:
[0040] Load the application's executable file and dependent dynamic libraries into system memory;
[0041] Determine the offset information corresponding to the target variable in the dynamic library in the executable file to obtain a first offset;
[0042] Determining a random offset corresponding to when the executable file is loaded;
[0043] Determine a memory address corresponding to the target variable based on the first offset and the random offset;
[0044] Based on the memory address, a memory layout of the target variable is modified.
[0045] In one possible implementation, the memory 102 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and application programs required for at least one function (such as a sound playback function, an image playback function, etc.); the data storage area may store data created during the use of the computer, such as user data, user access data, audio data, etc.
[0046] In addition, the memory 102 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0047] The communication interface 103 may be an interface of a communication module, such as an interface of a GSM module.
[0048] The input unit 104 may be a touch sensing unit, a keyboard, etc. The display 105 may include a display panel, such as a touch display panel, etc.
[0049] certainly, Figure 1 The computer device structure shown does not constitute a limitation on the computer device in the embodiment of the present application. In actual applications, the computer device may include Figure 1 More or fewer components than shown, or combinations of certain components.
[0050] The following will further describe the embodiments of the present application in detail based on the above common aspects involved in the embodiments of the present application. Figure 2 FIG. 1 is a flow chart of a method for modifying a variable memory layout provided in an embodiment of the present application, the method comprising:
[0051] Step S201: Load the executable file of the application and the dependent dynamic libraries into the system memory.
[0052] The application may be, but is not limited to, an application in a terminal device such as a smartphone, tablet computer, personal data assistant, laptop, notebook, all-in-one or desktop PC as described above. This application will mainly take the application as an application on a terminal device (such as a mobile phone) with IOS as an example to illustrate the method of this application. More specifically, the application may be a browser application of IOS.
[0053] In terminal applications of computer devices, it may be necessary to modify the memory layout of one or more uninitialized static / global variables of the dynamic library introduced by the application based on certain specific requirements. When this requirement occurs, the executable file of the application and the dependent dynamic library must first be loaded into the system memory.
[0054] Take iOS application as an example. Specifically, the executable file of iOS is a Mach-O file. The file structure of Mach-O file can be found in Figure 3 As shown in the figure, it includes the file header, command area (commands) and data area (data). When dyld (the dynamic link editor, Apple's dynamic linker) creates a process file, it will find the corresponding code segment and main function from the Mach-O file to find the program entry, and will also load the dynamic library. When the application is started and the process is loaded, the system will load the dependent dynamic libraries in sequence. When dyld loads the dynamic library, it will map its data segment to the program's memory address space, and the default permissions are readable and writable.
[0055] The memory layout of the operating system process corresponding to the Mach-O executable file after it is loaded into memory can be found in Figure 4 Among them, uninitialized static variables / global variables are stored in the _bss section corresponding to the DATA segment of Mach-O.
[0056] Step S202: Determine the offset information corresponding to the target variable in the dynamic library in the executable file to obtain a first offset.
[0057] In this embodiment, the target variable may be, but is not limited to, one or more uninitialized static variables / global variables in the dynamic library that the application depends on.
[0058] Since uninitialized static variables / global variables are stored in the _bsssection corresponding to the DATA segment of Mach-O, therefore, for a static variable / global variable to be modified, i.e., the target variable in this application, its memory address is in the memory area pointed to by the _bss section, and as long as the offset information corresponding to the target variable in the memory area pointed to by the _bss section in the Mach-O file is determined, i.e., the first offset, the memory address corresponding to the target variable in the memory can be further determined based on the first offset.
[0059] The first offset corresponding to the target variable in the executable file may be a second offset of the memory address corresponding to the target variable compared to the starting memory address of the data block to which the target variable belongs, or may be a third offset of the target variable relative to the starting virtual address specified by the executable file.
[0060] Step S203: Determine the random offset corresponding to when the executable file is loaded.
[0061] After the Mach-O file is loaded into the process address space by dyld, a random offset will be added to the virtual address of the original file, and the sum of the values obtained after adding the random offset will be used as its address in the memory. Then, the contents of each section of the _DATA segment corresponding to the Mach-O file will be fully mapped to the continuous process memory space, that is, the continuous uninitialized static variables are stored in the _bss section area in the BSS (Block Started By Symbol) in its memory layout structure. Therefore, when determining the memory address after the target variable is loaded into the memory, in addition to determining the offset information corresponding to the uninitialized target variable in the Mach-O file, that is, the first offset, it is also necessary to determine the random offset corresponding to the executable file when it is loaded.
[0062] Step S204: Determine a memory address corresponding to the target variable based on the first offset and the random offset.
[0063] Based on determining the first offset corresponding to the target variable in the executable file and the random offset corresponding to when the executable file is loaded, the memory address of the uninitialized target variable can be obtained by performing corresponding operations on the offset of the first offset and the value of the random offset. This part will be described in detail in the following embodiments.
[0064] Step S205: Based on the memory address, modify the memory layout of the target variable.
[0065] The data mapped to the _bss section of the process memory space has readable and writable permissions. Therefore, for the above-mentioned target variables mapped to the _bss section of the process memory space, the permissions to access their memory addresses and modify their memory contents can be obtained. Therefore, the memory layout can be modified directly based on the determined memory addresses.
[0066] Specifically, by reverse engineering the memory layout of the target variable, the required variable data can be filled into the memory address corresponding to the target variable according to the memory layout of the target variable, thereby achieving the purpose of changing certain behaviors of the application.
[0067] Among them, the memory address corresponding to the target variable in the memory can be written to the memory during the startup initialization phase of the application, but this is only a preferred embodiment of the present application and does not constitute a limitation to the present application. In actual implementation, this processing can also be performed in other links (rather than the startup initialization phase of the application), but it should at least be ensured that the target variable is written to the memory before the application is used to perform related processing.
[0068] The following examples illustrate this.
[0069] In the browser proxy scenario of the browser application of IOS, it is necessary to modify the memory of the variable Hashset in WebKit (browser engine, which is a dynamic library), and realize the proxy registration to the proxy layer by writing the http / https protocol into the variable memory. For this scenario, the prior art realizes writing the protocol information such as http / https into the memory of the above-mentioned Hashset by calling a private API provided in the browser engine source code of the IOS platform. Based on the scheme of this application, it is no longer necessary to rely on the above-mentioned private API provided by the IOS browser engine source code, but it can determine the memory address of the Hashset variable after being loaded based on the above-mentioned static analysis variable memory address method provided by this application, and then fill the required memory data into the Hashset variable by reversely studying the memory layout method of the Hashset variable (specifically a memory layout based on a structure), such as filling the required http / https protocol information, etc., so as to realize the registration of the filled http / https protocol information to the proxy layer, and the whole process does not involve any symbol call and modification, and there is no risk of private API access.
[0070] It can be seen from the above scheme that the method for modifying the memory layout of variables provided in the embodiment of the present application, after loading the executable file of the application and the dependent dynamic library into the system memory, further determines the memory address of the target variable based on the first offset corresponding to the target variable in the executable file and the random offset corresponding when the executable file is loaded, and on this basis, finally modifies the memory layout of the target variable based on the determined memory address. It can be seen that by applying the scheme of the present application, the purpose of modifying the memory layout of the dynamic library variables can be achieved by statically analyzing the memory address of the variables of the dynamic library, without relying on jailbreaking or private interfaces, and correspondingly avoiding various drawbacks of the prior art, such as the risk of no private API access, etc., and the memory layout of the variables in the dynamic library can be conveniently modified. Furthermore, the purpose of hooking or dynamically modifying the behavior of certain applications or APIs can be achieved, and support can also be provided for some derivative behaviors.
[0071] In this application, how to statically analyze the memory address corresponding to the target variable in the dynamic library, that is, a certain uninitialized static variable / global variable when the dynamic library is loaded is the core of this case. The following will further describe in detail the implementation process of determining the memory address corresponding to the target variable in the dynamic library. As an optional implementation method, refer to Figure 5 , the memory address corresponding to the target variable can be determined through the following processing:
[0072] Step S501: Determine a second offset of a memory address corresponding to the target variable compared to a start memory address of a data block to which the target variable belongs.
[0073] Still taking the iOS application as an example, the second offset refers to the offset of the uninitialized target variable within the block in the _bsssection of the Mach-O file.
[0074] Step S502: Determine the loading base address of the data block to which the target variable belongs.
[0075] Step S503: Determine the random offset corresponding to when the executable file is loaded.
[0076] Step S504: Determine a memory address corresponding to the target variable based on the second offset, a loading base address of a data block to which the target variable belongs, and the random offset.
[0077] In this embodiment, it is assumed that the block offset (ie, the second offset) of the uninitialized target variable in the _bss section of the Mach-O file is offx, and the loading base address of the _bss section block is base.
[0078] In addition, further assuming that the random offset corresponding to the Mach-O file after it is loaded into memory is slide, the memory address y of the uninitialized target variable is as follows:
[0079] y = offx + base + slide;
[0080] Therefore, as long as the values of the three parameters of the second offset offx, the loading base address base, and the random offset slide are determined, the memory address of the target variable can be obtained.
[0081] Under normal circumstances, standard Mach-O files are stored in the _bss section of the _DATA segment. What is special is that the browser engine's static variable HashSet is stored in the _bss section corresponding to the _DATA_DIRTYsegment of Mach-O.
[0082] The second offset offx among the three parameters is the block offset of the target variable relative to the data block _bsssection to which it belongs. As long as we find where y2 is accessed (i.e. the virtual address of the static variable in the compiled Mach-O product), we can indirectly deduce offx. The calculation formula is as follows:
[0083] y2=offx+base2;
[0084] Among them, base2 is the virtual address of the data block _bss section to which the above target variable belongs relative to Mach-O. Taking the target variable as the HashSet as an example, the starting address of the _bss section block in the _DATA_DIRTY segment can be directly found by traversing the section of Mach-O, and the starting address is used as the value of base2.
[0085] According to the characteristics of Mach-O files, y2 can only be found in the code segment, that is, wherever y2 is accessed, the value of y2 can be found there. As mentioned above, when Mach-O files are loaded into memory, they are mapped to the above Figure 4 In the _TEXT code segment in the MahcO file, y2 is specifically accessed in the function of the _TEXT code segment in the MahcO file. Therefore, it is only necessary to find this function in the _TEXT code segment and determine y2 based on the assembly code that finally accesses y2.
[0086] The following is an example of the assembly code that finally accesses y2:
[0087] 0x1f196b748<+0>:subsp,sp,#0x50;=0x50
[0088] 0x1f196b74c<+4>:stp x22,x21,[sp,#0x20]
[0089] 0x1f196b750<+8>:stp x20,x19,[sp,#0x30]
[0090] 0x1f196b754<+12>:stp x29,x30,[sp,#0x40]
[0091] 0x1f196b758<+16>:add x29,sp,#0x40;=0x40
[0092] 0x1f196b75c<+20>:mov x19,x0
[0093] 0x1f196b760<+24>:ldr x8,[x19]
[0094] 0x1f196b764<+28>:cbz x8,0x1f196b7c0;<+120>
[0095] 0x1f196b768<+32>:adrp x8,169719
[0096] 0x1f196b76c<+36>:ldrb w9,[x8,#0x948]
[0097] 0x1f196b770<+40>:tbz w9,#0x0,0x1f196b7d4;<+140>
[0098] 0x1f196b774<+44>:adrp x0,169719
[0099] 0x1f196b778<+48>:add x0,x0,#0x930;=0x930
[0100] 0x1f196b77c<+52>:add x8,sp,#0x8;=0x8
[0101] 0x1f196b780<+56>:mov x1,x19
[0102] 0x1f196b784<+60>:bl0x1f16f168c;WTF::HashTable<WTF::String,
[0103] WTF::String,WTF::IdentityExtractor,WTF::ASCIICaseInsensitiveHash,
[0104] WTF::HashTraits <wtf::string>,
[0105] WTF::HashTraits <wtf::string>>::add(WTF::String const&)
[0106] From the above assembly code, we can know that at the process address 0x1f196b784, the add method of the target variable (the above assembly code is specifically for the HashSet variable) is executed, where the x0 register is the value of y2, and the value of y2 can be calculated according to the above instructions. At this time:
[0107] y2=PC&(2^12)+169719*(2^12)+0x930
[0108] Among them, PC refers to the value of the PC register (pointing to the current instruction address), and the current PC = 0x1f196b774.
[0109] On the basis of determining y2, further combining the above calculation formula y2=offx+base2, the value of offx can be determined.
[0110] For the loading base address base among the above three parameters, you can directly call the dyld interface function to obtain the process loading address of a certain framework (dynamic library) or a certain segment of the current binary; by calling the dyldMach-O related interface, traverse the segment_command of the framework and find the corresponding _bsssection.
[0111] For the random offset slide in the above three parameters, according to the dynamic library identifier of the dynamic library where the target variable is located, such as the WebKit dynamic library name, all loaded dynamic libraries can be traversed to obtain the starting address and random offset loaded into the memory (that is, the address space configuration random loading aslr offset).
[0112] On this basis, the runtime memory address of the above static variable (HashSet) can be finally calculated based on the above calculation formula y=offx+base+slide.
[0113] Based on the scheme of this embodiment, the offset of the uninitialized target static / global variable of the dynamic library in the segment of the Mach-o file can be found through static analysis to obtain the memory address of the uninitialized target static / global variable at runtime, and then the memory area corresponding to the memory address can be modified. The whole process does not involve any symbol call and modification, does not need to rely on jailbreak and private API, and accordingly can avoid various disadvantages of relying on jailbreak and private API for variable memory modification, such as avoiding the risk of private API access.
[0114] In other embodiments of the present application, as another optional implementation, refer to Figure 6 , the memory address corresponding to the target variable can also be determined through the following processing process:
[0115] Step S601: Obtain the starting virtual address specified by the executable file.
[0116] The starting virtual address specified by the executable file is also the loading base address of the executable file.
[0117] This embodiment also takes the IOS application as an example to describe in detail the implementation process of determining the memory address of the target variable.
[0118] Therefore, in this step, the starting virtual address specified by the executable file is the starting virtual address specified (declared) by the MachO file.
[0119] Step S602: Determine a third offset of the target variable relative to the starting virtual address specified by the executable file.
[0120] Correspondingly, the third offset of the target variable relative to the starting virtual address specified by the executable file is the offset of the target variable relative to the starting virtual address specified by the MachO file.
[0121] Step S603: Determine the random offset corresponding to when the executable file is loaded.
[0122] Step S604: Determine a memory address corresponding to the target variable based on the starting virtual address specified by the executable file, the third offset, and the random offset.
[0123] Assuming that the starting virtual address specified by the current MachO file is va_base, the address offset (ie, the third offset) of the target variable (ie, an uninitialized static variable / global variable) relative to the starting virtual address va_base specified by the current MachO file is va_offset.
[0124] At the same time, assuming that the random offset after the Mach-O file is loaded is slide, the memory address y after the target variable is loaded can be expressed as:
[0125] y=va_base+va_offset+slide.
[0126] Therefore, as long as the values of the three parameters of the starting virtual address va_base, the third offset va_offset and the random offset slide specified by the above-mentioned MachO file are determined, the memory address of the target variable can be obtained.
[0127] Among them, the value of the starting virtual address va_base specified by the MachO file can be determined according to the first load command in the MachO load command (Load Commands). For example, the first load command of the dynamic library is generally the VM Address of LC_SEGMENT_64 (_TEXT), so the value of va_base can be determined based on the first load command.
[0128] va_offset is a product determined after compilation. You need to find the corresponding code segment reference of va_offset and find va_offset through static analysis. Generally speaking, a code segment reference to a variable will trigger page memory access, which is generally as follows:
[0129] 0x1f196b768<+32>:adrp x8,169719; load page memory
[0130] 0x1f196b76c<+36>:ldrb w9,[x8,#0x948]; The page is accessed at an internal offset of 0x948, where the uninitialized global variable is obtained.
[0131] Based on the above code (or code similar to the above code), it can be determined that va=PC&(4*1024)+169719*(4*1024)+0x948 of the target variable to be accessed (ie, ldrb in the above code) can be determined.
[0132] Among them, va refers to the address of the target variable in mcah-O, that is, the first offset mentioned above; here PC refers to the value of the program counter. In the above code, the value of PC is 0x1f196b768.
[0133] Subtract va_base from va to get the above va_offset.
[0134] For the random offset slide in the above three parameters, according to the dynamic library identifier of the dynamic library where the target variable is located, such as the WebKit dynamic library name, all loaded dynamic libraries can be traversed to obtain the starting address and random offset loaded into the memory (that is, the address space configuration random loading aslr offset).
[0135] On this basis, the runtime memory address of the above static variable (HashSet) can be finally calculated based on the above calculation formula y=va_base+va_offset+slide.
[0136] It should be noted that the above-mentioned method of determining the values of each parameter provided in this embodiment is only an illustrative and non-restrictive description of the present application. In specific implementation, other feasible methods can also be used to determine the values of each parameter, such as using other methods to determine the value of the va_offset, etc. As for the above-mentioned method of determining va_offset by finding the corresponding code segment reference of va_offset, since the obtained va is essentially the first offset, the va can also be directly added with a random offset slide to obtain the memory address of the target variable, and the present application does not limit this.
[0137] Based on the scheme of this embodiment, the reference information of the uninitialized target static / global variables of the dynamic library in the corresponding code segment can be found through static analysis to obtain the memory address of the uninitialized target static / global variables at runtime, and then the memory area corresponding to the memory address can be modified. The whole process does not involve any symbol calls and modifications, does not need to rely on jailbreaking and private APIs, and accordingly avoids various disadvantages of relying on jailbreaking and private APIs for variable memory modification, such as avoiding the risks of private API access.
[0138] In an optional embodiment of the present application, Figure 7 As shown, the method for modifying the variable memory layout of the present application may also include the following processing procedures:
[0139] Step S206: modify the value of the guard variable at the memory address corresponding to the guard variable of the target variable in the memory, so as to indicate that the target variable has been initialized based on the modified guard variable value.
[0140] For uninitialized static variables / global variables, if they are C++ static variables, there will be a concept of guard variables. Among them, the guard variable is used to ensure that the corresponding static variables / global variables are initialized only once, such as the HashSet described above. Therefore, after modifying the memory layout of the target variable, it is necessary to modify the value of the corresponding guard variable at the same time to inform the process that the target variable has been initialized and does not need to be reinitialized to fill the memory.
[0141] Among them, the guard variable is located at the next 8 bytes at the end of the corresponding static variable / global variable memory. Therefore, the guard variable can be set at the next 8 bytes at the end of the target variable memory. For example, the guard variable can be set to 1. In specific implementation, the value can be set according to the actual design and does not have to be limited to 1.
[0142] This embodiment can promptly inform the process that the target variable has been initialized and does not need to re-initialize and fill the memory by modifying the value of the corresponding guard variable when modifying the memory layout of the target variable.
[0143] On the other hand, the present application also provides a device for modifying the memory layout of a variable.
[0144] See also Figure 8 , which shows a structural schematic diagram of the device for modifying the variable memory layout in the present application. The device can be specifically applied to a computer device. The component structure of the computer device to which the device of the present application is applicable can be found in the relevant introduction above and will not be repeated here.
[0145] like Figure 8 As shown, the browser proxy implementation device of this embodiment may include:
[0146] The loading unit 801 is used to load the executable file of the application and the dependent dynamic library into the system memory;
[0147] A first determining unit 802 is used to determine the offset information corresponding to the target variable in the dynamic library in the executable file to obtain a first offset;
[0148] A second determining unit 803 is used to determine a random offset corresponding to when the executable file is loaded;
[0149] A third determining unit 804 is used to determine a memory address corresponding to the target variable based on the first offset and the random offset;
[0150] The memory modification unit 805 is used to modify the memory layout of the target variable based on the memory address.
[0151] In an optional implementation manner of the first embodiment of the present application, the first determining unit 802 is specifically configured to:
[0152] Determine a second offset of a memory address corresponding to the target variable compared to a starting memory address of a data block to which the target variable belongs; or determine a third offset of the target variable relative to a starting virtual address specified by the executable file;
[0153] The third determination unit 804 is specifically used to: determine the memory address corresponding to the target variable based on the second offset, the random offset and the loading base address of the data block to which the target variable belongs; or determine the memory address corresponding to the target variable based on the third offset, the random offset and the loading base address of the executable file.
[0154] In an optional implementation manner of the first embodiment of the present application, the first determining unit 802 is specifically configured to:
[0155] Determining the virtual address of the target variable in the compilation result of the executable file;
[0156] Determine a virtual address of a data block to which the target variable belongs relative to the executable file;
[0157] The second offset is determined based on the virtual address of the target variable in the compilation result of the executable file and the virtual address of the data block to which the target variable belongs relative to the executable file.
[0158] The second determining unit 803 is specifically configured to:
[0159] According to the dynamic library identifier of the dynamic library that the executable file depends on, traverse each dynamic library loaded in the system memory to obtain a random offset when the dynamic library that the executable file depends on is loaded, and the random offset is used as the random offset corresponding to when the executable file is loaded.
[0160] In an optional implementation of the first embodiment of the present application, the memory modification unit 805 is specifically used to:
[0161] During the startup initialization phase of the application, the required variable data is written to the memory address corresponding to the target variable based on the memory layout of the target variable.
[0162] In an optional implementation of the first embodiment of the present application, see Fig. 9 , the device for modifying the variable memory layout may also include:
[0163] The guard variable modification unit 806 is used to write the required variable data to the memory address corresponding to the target variable based on the memory layout of the target variable during the startup initialization phase of the application, so as to indicate that the target variable has been initialized based on the modified guard variable value.
[0164] On the other hand, the present application also provides a storage medium, which stores a computer program. When the computer program is loaded and executed by a processor, the method for modifying the variable memory layout as described in any of the above embodiments is implemented.
[0165] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0166] For the convenience of description, the above system or device is described by dividing it into various modules or units according to its functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0167] It can be known from the description of the above implementation methods that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0168] Finally, it should be noted that, in this article, relational terms such as first, second, third and fourth are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article 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, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
[0169] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.< / wtf::string> < / wtf::string>
Claims
1. A method for modifying the memory layout of a variable, It is characterized in that include: Load the application's executable file and dependent dynamic libraries into system memory; Determine the offset information corresponding to the target variable in the dynamic library in the executable file to obtain a first offset; Determining a random offset corresponding to when the executable file is loaded; Determine a memory address corresponding to the target variable based on the first offset and the random offset; Based on the memory address, modify the memory layout of the target variable; The determining the offset information corresponding to the target variable in the dynamic library in the executable file to obtain the first offset includes: Determine a second offset of a memory address corresponding to the target variable compared to a starting memory address of a data block to which the target variable belongs; Alternatively, a third offset of the target variable relative to a starting virtual address specified by the executable file is determined.
2. The method according to claim 1, It is characterized in that The determining, based on the first offset and the random offset, a memory address corresponding to the target variable includes: Based on the second offset, the random offset and the loading base address of the data block to which the target variable belongs, the memory address corresponding to the target variable is determined; or, based on the third offset, the random offset and the loading base address of the executable file, the memory address corresponding to the target variable is determined.
3. The method according to claim 2, It is characterized in that The determining a second offset of the memory address corresponding to the target variable compared to the starting memory address of the data block to which the target variable belongs includes: Determining the virtual address of the target variable in the compilation result of the executable file; Determine a virtual address of a data block to which the target variable belongs relative to the executable file; The second offset is determined based on the virtual address of the target variable in the compilation result of the executable file and the virtual address of the data block to which the target variable belongs relative to the executable file.
4. The method according to claim 1, It is characterized in that The determining the random offset corresponding to when the executable file is loaded includes: According to the dynamic library identifier of the dynamic library that the executable file depends on, traverse each dynamic library loaded in the system memory to obtain a random offset when the dynamic library that the executable file depends on is loaded, and the random offset is used as the random offset corresponding to when the executable file is loaded.
5. The method according to claim 1, It is characterized in that The modifying the memory layout of the target variable based on the memory address includes: During the startup initialization phase of the application, the required variable data is written to the memory address corresponding to the target variable based on the memory layout of the target variable.
6. The method according to claim 1, It is characterized in that When modifying the memory layout of the target variable, the method further includes: At a memory address corresponding to a guard variable of the target variable in the memory, the value of the guard variable is modified to indicate that the target variable has been initialized based on the modified guard variable value.
7. A device for modifying the memory layout of a variable, It is characterized in that include: A loading unit, used to load the application's executable file and dependent dynamic libraries into the system memory; A first determining unit, configured to determine offset information corresponding to a target variable in the dynamic library in the executable file, to obtain a first offset; A second determining unit, configured to determine a random offset corresponding to when the executable file is loaded; A third determining unit, configured to determine a memory address corresponding to the target variable based on the first offset and the random offset; A memory modification unit, used to modify the memory layout of the target variable based on the memory address; The first determining unit is specifically configured to: Determine a second offset of a memory address corresponding to the target variable compared to a starting memory address of a data block to which the target variable belongs; or determine a third offset of the target variable relative to a starting virtual address specified by the executable file.
8. The device according to claim 7, Features: The third determination unit is specifically used to: determine the memory address corresponding to the target variable based on the second offset, the random offset and the loading base address of the data block to which the target variable belongs; or determine the memory address corresponding to the target variable based on the third offset, the random offset and the loading base address of the executable file.
9. The device according to claim 8, Features: The first determining unit is specifically configured to: Determining the virtual address of the target variable in the compilation result of the executable file; Determine a virtual address of a data block to which the target variable belongs relative to the executable file; Determine the second offset based on the virtual address of the target variable in the compilation result of the executable file and the virtual address of the data block to which the target variable belongs relative to the executable file; The second determining unit is specifically configured to: According to the dynamic library identifier of the dynamic library that the executable file depends on, traverse each dynamic library loaded in the system memory to obtain a random offset when the dynamic library that the executable file depends on is loaded, and the random offset is used as the random offset corresponding to when the executable file is loaded.
10. A computer device, It is characterized in that include: A memory for storing computer executable instructions; A processor, configured to call and execute the computer executable instructions, wherein the computer executable instructions are at least configured to execute the method for modifying the memory layout of a variable as described in any one of claims 1 to 5.
