Code obfuscation method and device, electronic equipment and storage medium

By adjusting the degree of obfuscation according to the security level of the source code file, determining the objective function and generating obfuscated functions by redirecting the function call, the problem of poor security of code obfuscating in the existing technology is solved, and the protection effect of the code is improved.

CN119272244APending Publication Date: 2025-01-07CHINA FINANCIAL CERTIFICATION AUTHORITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411194113.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-28
Publication Date
2025-01-07

AI Technical Summary

Technical Problem

The existing code obfuscation methods are poor in security, are easily restored by reverse tools, and are obfuscated in a fixed way.

Method used

According to the security level of the source code file, different degree of obfuscation are used for code obfuscation. By determining the objective function and calling the corresponding number of transit functions, the obfuscated function is generated, increasing the difficulty of reverse analysis.

Benefits of technology

It improves the security of code obfuscation methods, increases the difficulty of reverse analysis, and enhances the protection effect of code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119272244A_ABST
    Figure CN119272244A_ABST
Patent Text Reader

Abstract

The invention provides a code obfuscation method and device, electronic equipment and a storage medium, and relates to the technical field of code security, the method comprises the following steps: determining a target function in a to-be-obfuscated code, the target function being a to-be-obfuscated function; an obfuscated function corresponding to the target function is obtained, when the obfuscated function is called, a first number of target transfer functions are called firstly, then the target function is called, and the first number is determined according to the target security level corresponding to the to-be-obfuscated code; and updating the target function into the obfuscated function to obtain an obfuscated code. According to the method, code obfuscation is performed by adopting different obfuscation degrees according to the security level of the source code file, so that the security of the code obfuscation method is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of code security technology, and in particular to a code obfuscation method, device, electronic device and storage medium. Background Art

[0002] Code obfuscation is a common technical means, widely used to protect the intellectual property rights and security of the code and reduce the code size. Its common methods include: renaming variables and functions, deleting blanks and comments, string encryption or encoding, control flow flattening and useless code insertion.

[0003] Existing code obfuscation methods usually use a fixed method to perform code obfuscation. There are already many reverse engineering tools on the market, and the obfuscated code is also easier to restore, so the security is poor. Summary of the invention

[0004] The present invention provides a code obfuscation method, device, electronic device and storage medium to solve the defect of poor security of the code obfuscation method in the prior art. Different obfuscation degrees are used for code obfuscation according to the security level of the source code file, thereby improving the security of the code obfuscation method.

[0005] The present invention provides a code obfuscation method, comprising the following steps: Determine a target function in the code to be obfuscated, wherein the target function is a function to be obfuscated; Obtaining an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, wherein the first number is determined according to a target security level corresponding to the code to be obfuscated; The target function is updated to the obfuscated function to obtain the obfuscated code.

[0006] According to a code obfuscation method provided by the present invention, the step of obtaining an obfuscated function corresponding to the target function includes: Determine a target obfuscation number corresponding to the target function, where the target obfuscation number is used to indicate the number of times the target transfer function is called, and the higher the security level corresponding to the code to be obfuscated, the greater the target obfuscation number corresponding to the target function; Determine a target confusion array corresponding to the target confusion number, wherein the target confusion array is used to call the first number of target transfer functions and the target function; The obfuscated function is determined according to the target obfuscation array.

[0007] According to a code obfuscation method provided by the present invention, determining a target obfuscation number corresponding to the target function includes: Obtaining a target security level corresponding to the code to be obfuscated; According to the target security level and the correspondence between the preset security level and the obfuscation times, the target obfuscation times corresponding to the target security level are determined, and the target obfuscation times corresponding to the target security level are the target obfuscation times corresponding to the objective function.

[0008] According to a code obfuscation method provided by the present invention, the step of determining a target obfuscation array corresponding to the target obfuscation times includes: Acquire a second number of initial arrays, each of the initial arrays includes the same multiple initial values ​​and the multiple initial values ​​are arranged in a different order, the multiple initial values ​​correspond to multiple transfer functions respectively, the multiple transfer functions include the target function, and the second number is equal to the sum of the target confusion times and one; Select an initial value from each of the initial arrays to obtain a second number of initial values; The target confusion array is determined according to the second number of initial values, where the target confusion array corresponds to the first number of target transfer functions and the target function.

[0009] According to a code obfuscation method provided by the present invention, the code to be obfuscated includes multiple functions to be obfuscated, the multiple functions to be obfuscated are the multiple transfer functions, the multiple initial values ​​are integers increasing from zero, the target function is any one of the multiple functions to be obfuscated, the target function corresponds to the target initial value in the second number of initial values, and selecting an initial value from each of the initial arrays to obtain the second number of initial values ​​includes: Obtain a first initial value whose subscript is the target initial value in the first initial array of the second number of initial arrays, then obtain a second initial value whose subscript is the first initial value in the next initial array, then obtain a third initial value whose subscript is the second initial value in the next initial array, and traverse the second number of initial arrays in this way to obtain a second number of initial values; The step of determining the target obfuscation array according to the second number of initial values ​​includes: According to the acquisition order of the second number of initial values, the second number of initial values ​​are arranged in reverse order starting from the last acquired initial value to obtain the target confusion array.

[0010] According to a code obfuscation method provided by the present invention, determining the obfuscated function according to the target obfuscation array includes: Generate a target number of sub-obfuscation functions, each of which includes an initial value of a currently called function, the number of remaining function calls, and parameters corresponding to the target function; The target number of sub-confusion functions are determined as the post-confusion functions.

[0011] The present invention also provides a code obfuscation device, comprising the following modules: A function determination module, used to determine a target function in the code to be obfuscated, wherein the target function is a function to be obfuscated; a function acquisition module, configured to acquire an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, wherein the first number is determined according to a target security level corresponding to the code to be obfuscated; The function updating module is used to update the target function to the obfuscated function to obtain the obfuscated code.

[0012] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any of the code obfuscation methods described above is implemented.

[0013] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the code obfuscation method described above is implemented.

[0014] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the code obfuscation method described above is implemented.

[0015] The code obfuscation method, device, electronic device and storage medium provided by the present invention use different obfuscation degrees to perform code obfuscation according to the security level of the source code file, thereby improving the security of the code obfuscation method. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0017] Figure 1 It is a flowchart of the code obfuscation method provided by the present invention.

[0018] Figure 2 It is a flowchart of the method for obtaining an obfuscated function provided by the present invention.

[0019] Figure 3 It is a flow chart of the method for determining the target transfer function provided by the present invention.

[0020] Figure 4 It is a schematic diagram of the effect of the code obfuscation method provided by the present invention.

[0021] Figure 5 A schematic diagram of the structure of the code obfuscation device provided by the present invention.

[0022] Figure 6 It is a schematic diagram of the physical structure of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0024] Code obfuscation is a common technical means, widely used to protect the intellectual property rights and security of the code and reduce the code size. Common methods include: renaming variables and functions, deleting blanks and comments, string encryption or encoding, control flow flattening and useless code insertion. Although the above obfuscation methods cannot eliminate reverse engineering, they can increase its cost and difficulty. Code obfuscation can include TypeScript code obfuscation, etc.

[0025] Existing code obfuscation methods usually use a fixed method to perform code obfuscation. There are already many reverse engineering tools on the market, and the obfuscated code is also easier to restore, so the security is poor.

[0026] In view of this, an embodiment of the present invention provides a code obfuscation method, by determining a target function in the code to be obfuscated, the target function is the function to be obfuscated; obtaining an obfuscated function corresponding to the target function, when the obfuscated function is called, first calling a first number of target transit functions and then calling the target function, the first number is determined according to the target security level corresponding to the code to be obfuscated; updating the target function to the obfuscated function, and obtaining the obfuscated code. This method can use different obfuscation degrees to perform code obfuscation according to the security level of the code file, thereby improving the security of the code obfuscation method.

[0027] The technical solutions in the embodiments of the present invention will be described below in conjunction with the accompanying drawings in the embodiments of the present invention.

[0028] Figure 1 : is a flow chart of the code obfuscation method provided by the present invention. The code obfuscation method can be applied to electronic devices, which can be various types of devices with information processing capabilities during implementation. For example, the electronic device can include a personal computer, a laptop, a PDA or a server, etc.; the electronic device can also be a mobile terminal, for example, the mobile terminal can include a mobile phone, a car computer, a tablet computer or a projector, etc. Figure 1 As shown, the method may include the following steps 101 to 103: Step 101: Determine a target function in the code to be obfuscated, where the target function is a function to be obfuscated.

[0029] It should be noted that there are many methods for determining the target function in the code to be obfuscated, where the target function is the function to be obfuscated. For example, the target function can be determined according to a preset function name, or a mapping table of the target function can be determined by a preset program screening, etc. The present invention does not limit the method for determining the target function in the code to be obfuscated, where the target function is the function to be obfuscated.

[0030] Exemplarily, the features of all functions except the constructor in each source file to be obfuscated can be parsed in sequence by the code parsing program / module, including the function name, the number of parameters, and a mapping relationship can be established. The JSON object description is as follows: {"FileA":["FunctionA1":4,"FunctionA2":3],"FileB":["FunctionB1":1,"FunctionB2":2]}. Among them, the functions except the constructor can be definition functions, which are used to indicate the functions that define the object action, and can include callback functions, anonymous functions, high-order functions, etc.

[0031] Step 102: Obtain an obfuscated function corresponding to the target function. When the obfuscated function is called, a first number of target transit functions are first called and then the target function is called. The first number is determined according to a target security level corresponding to the code to be obfuscated.

[0032] It should be noted that the function obfuscation program / module can determine the degree of obfuscation, that is, the number of target transit functions called, according to the security level of the code to be obfuscated, and generate an obfuscated function, that is, an obfuscated source file.

[0033] There are many ways to obtain the obfuscated function corresponding to the target function, such as obtaining it through a preset program, obtaining it through remote processing, etc. The present invention does not limit the way to obtain the obfuscated function corresponding to the target function.

[0034] Step 103: updating the target function to the obfuscated function to obtain the obfuscated code.

[0035] It should be noted that the target function can be updated to the obfuscated function by calling a function to update the program / module, and the present invention does not limit the manner of updating the target function to the obfuscated function.

[0036] Exemplarily, according to the mapping relationship generated by the code parsing module and the corresponding obfuscated function, each function call is replaced with a call to the obfuscated function one by one.

[0037] The present invention proposes a code obfuscation method for obfuscating the execution order of functions. The method can determine different obfuscation degrees according to the security level of a source code file, and call the target function after calling a first number of target transit functions according to the obfuscation degree. The obfuscated code increases the difficulty of reverse analysis and improves the security of the source code.

[0038] It is understandable that this code obfuscation method can be extended to code obfuscation scenarios in JavaScript, Hongmeng ETS and other languages ​​that are supersets of JavaScript.

[0039] Figure 2 FIG. 1 is a flow chart of the method for obtaining an obfuscated function provided by the present invention. Figure 2 As shown, obtaining the obfuscated function corresponding to the target function may include: Step 201: Determine a target obfuscation count corresponding to the target function, where the target obfuscation count is used to indicate the number of times the target transfer function is called. The higher the security level corresponding to the code to be obfuscated, the greater the target obfuscation count corresponding to the target function.

[0040] It should be noted that there are many ways to determine the target obfuscation times. For example, the target obfuscation times can be determined according to the correspondence between the preset security level and the obfuscation times, or the target obfuscation times can be determined according to the preset obfuscation times, etc. The number of times the target function is called is 1. The present invention does not limit the method for determining the target obfuscation times and the target obfuscation times.

[0041] It can be understood that the target obfuscation times are the times that the target transfer function needs to be called, and the number of target transfer functions required can be determined based on the number of times the function needs to be called.

[0042] Step 202: Determine a target confusion array corresponding to the target confusion times, wherein the target confusion array is used to call the first number of target transfer functions and the target function.

[0043] Exemplarily, determining the target confusion array corresponding to the target confusion number can be achieved by setting a certain number of target confusion arrays, and the target confusion number and the number of target confusion array groups can be equal or different. For example, if the target confusion number is 4 times, the number of target confusion array groups can be 5, that is, including 4 arrays for 4 calls to the target transfer function and 1 array for 1 call to the target function. The number of target confusion array groups can also be 1, which can achieve 4 calls to the target transfer function and 1 call to the target function. The present invention does not limit the method of determining the target confusion array corresponding to the target confusion number.

[0044] Step 203: Determine the obfuscated function according to the target obfuscation array.

[0045] It should be noted that there are many ways to determine the obfuscated function according to the target obfuscation array, for example, the function can be set by a preset program, or it can be determined by remote setting, etc. The present invention does not limit the way to determine the obfuscated function according to the target obfuscation array.

[0046] It can be understood that the method of determining the target obfuscation times, determining the target obfuscation array, and then generating the obfuscated function according to the target obfuscation array can conveniently determine the obfuscated function and improve the efficiency of code obfuscation.

[0047] Further, the step 201 determines the target obfuscation times corresponding to the target function, which may include: obtaining a target security level corresponding to the code to be obfuscated; determining the target obfuscation times corresponding to the target security level according to the target security level and a correspondence between a preset security level and the obfuscation times, the target obfuscation times corresponding to the target security level being the target obfuscation times corresponding to the target function.

[0048] It can be understood that according to the correspondence between the preset security level and the obfuscation times, the target obfuscation times corresponding to the target security level are determined, and the target obfuscation times corresponding to the target security level are the target obfuscation times corresponding to the objective function. This method can conveniently determine the target obfuscation times and improve the efficiency of code obfuscation.

[0049] Figure 3 FIG. 1 is a flow chart of a method for determining a target transfer function provided by the present invention. Figure 3 As shown, the step 202 determines a target confusion array corresponding to the target confusion times, and the target confusion array is used to call the first number of target transfer functions and the target function, which may include: Step 301: Obtain a second number of initial arrays, each of the initial arrays includes the same multiple initial values ​​and the arrangement order of the multiple initial values ​​is different, the multiple initial values ​​correspond to multiple transfer functions respectively, the multiple transfer functions include the target function, and the second number is equal to the sum of the target confusion times and one.

[0050] For example, if the target obfuscation times is 2, three initial arrays can be set, each of which includes five initial values, and the five initial values ​​in each initial array are the same but the arrangement order of the five initial values ​​is different. For example, the three initial arrays can be (1, 3, 2, 4, 5), (2, 4, 5, 1, 3) and (5, 1, 3, 2, 4). Among them, 1, 2, 3, 4, and 5 correspond to different transfer functions respectively.

[0051] Step 302: Select an initial value from each of the initial arrays to obtain a second number of initial values, wherein the initial value selected from the first initial array corresponds to the objective function.

[0052] Exemplarily, the initial value selected by the first initial array corresponds to the objective function, for example, the position of the selected initial value may correspond to the ranking of the objective function, etc. An initial value may also be selected from each initial array according to a preset rule, for example, the first array selects an initial value corresponding to the objective function, for example, the initial value of the first position, and the first initial value is determined to be 2. The second and third arrays may select the third initial value of each initial array according to the preset rule, and the initial arrays 5 and 3 may be obtained, and finally three initial values ​​2, 5, and 3 are obtained.

[0053] Step 303: Determine the target confusion array according to the second number of initial values, where the target confusion array corresponds to the first number of target transfer functions and the target function.

[0054] It should be noted that, since each initial value corresponds to a transfer function, three transfer functions corresponding to 2, 5, and 3 can be determined, among which the transfer function corresponding to 2 is the target function.

[0055] It can be understood that by setting multiple initial arrays, each initial array including multiple random initial values, and by screening to determine the obfuscation method of the target number of transfer functions, the difficulty of code reverse analysis is increased and the security of the code is improved.

[0056] Further, the code to be obfuscated includes multiple functions to be obfuscated, the multiple functions to be obfuscated are the multiple transit functions, the multiple initial values ​​are integers increasing from zero, the target function is any one of the multiple functions to be obfuscated, the target function corresponds to the target initial value in the second number of initial values, and the step 302 selects an initial value from each of the initial arrays to obtain a second number of initial values, which may include: obtaining a first initial value with a subscript of the target initial value in the first initial array of the second number of initial arrays, and then obtaining a second initial value with a subscript of the first initial value in the next initial array, and then obtaining a third initial value with a subscript of the second initial value in the next initial array, and traversing the second number of initial arrays in this way to obtain the second number of initial values; Determining the target confusion array according to the second number of initial values ​​may include: arranging the second number of initial values ​​in reverse order starting from the last obtained initial value according to the order in which the second number of initial values ​​are obtained, to obtain the target confusion array, wherein the target confusion array corresponds to the target transit function and the target function.

[0057] Exemplarily, there are five target functions included in the code to be obfuscated, namely sum, sub, divide, multiply and square. The five functions are also transit functions. The initial values ​​corresponding to the five functions are set to 0, 1, 2, 3, 4. The number of obfuscations is 2 times plus 1 self-call, a total of 3 calls, and the three initial arrays obtained are (1, 4, 3, 2, 0), (0, 2, 1, 3, 4), and (2, 1, 4, 3, 0). The target function is sub, and the initial value corresponding to sub is 1. Then, the initial value 4 with the array subscript 1 is obtained from the first initial array, and the initial value 4 with the array subscript 4 is obtained from the second initial array, and the initial value 0 with the subscript 4 is obtained from the third initial array, and the three initial values ​​(4, 4, 0) are obtained, which correspond to the three transit functions of square, square, and sum. Then, according to the order in which the three initial values ​​are obtained, the three initial values ​​(0, 4, 4) are arranged in reverse order starting from the last initial value obtained, corresponding to the three target transfer functions sum, square, and square.

[0058] It can be understood that the method of obtaining the second number of initial values ​​and obtaining the target obfuscation array in the above manner increases the difficulty of code reverse analysis and improves the security of the code.

[0059] In some embodiments, the step 203 determines the obfuscated function according to the target obfuscation array, which may include: generating a target number of sub-obfuscation functions, each of which includes an initial value of a currently called function, the number of remaining function calls, and a parameter corresponding to the target function; and determining the target number of sub-obfuscation functions as the obfuscated function.

[0060] Exemplarily, after determining the target obfuscation array (0, 4, 4) corresponding to sub, three sub-obfuscation functions are generated. For example, if the target function is calculator.sum(2, 3), the first sub-obfuscation function generated is calculaotr.funcs[0](2, [2,3]), where 0 represents the need to call the function with subscript 4 in the third initial array, the first 2 represents that there are 2 calls left, and (2, 3) represents the parameters to be called. Similarly, the second sub-obfuscation function is calculaotr.funcs[4](1,[2,3]), where 4 represents the need to call the function with subscript 2 in the second initial array, 1 represents that there is 1 call left, and (2,3) represents the parameters to be called. The third sub-obfuscation function is calculaotr.funcs[4](0, [2,3]). In this case, the second parameter in the sub-obfuscation function is the original parameter converted into an array type.

[0061] The present invention provides a code obfuscation method for obfuscating the execution order of functions. The purpose of obfuscating the execution order of functions is achieved by introducing an obfuscated array, and different obfuscation levels can be defined by adjusting the number of the array. When a method is called again after obfuscation, all target functions in the file where the method is located may need to be executed before the real method code can be run. By increasing the calling depth, the difficulty of code debugging and analysis is greatly increased, and the security of the code is improved.

[0062] The following will describe an exemplary application of the embodiment of the present invention in a practical application scenario. The calculator class is used as an example for specific description. The code file of the calculator class includes: export class calculator {

[0063] sum(x: number, y: number) {

[0064] console.log("x + y = ", x + y); }

[0065] sub(x: number, y: number) {

[0066] console.log("x - y = ", x - y); }

[0067] divide(x: number, y: number) {

[0068] console.log("x / y = ", x / y); }

[0069] multiply(x: number, y: number) {

[0070] console.log("x * y = ", x * y); }

[0071] square(x: number) {

[0072] console.log("x ^ 2 = ", x * x); }

[0073] The steps to obfuscate the code file of the calculator class include: (1) Parse the file and find five functions: sum, sub, divide, multiply, and square. The square function has one parameter, and the remaining four functions require two parameters. The mapping relationship is as follows: {"calculator":["sum":2,"sub":2,"divide":2,"multiply":2,"square":1]}.

[0074] (2) Determine the degree of obfuscation. The degree of obfuscation indicates how many steps a function needs to jump to complete the execution of the target function, that is, the total number of steps including the execution of the transfer function and the target function. If the value is selected as 3, then an array with a length of "number of functions * step" should be established. The array length of the calculator class is 3 * 5 = 15. The array should be assigned values ​​from 0 to "number of functions - 1". The array can be named tags, then: tags = [0,1,2,3,4,0,1,2,3,4,0,1,2,3,4]. The tags are grouped according to the number of functions and shuffled. If the obfuscation results of the first group of tags are 1, 4, 3, 2, 0, the obfuscation results of the second group of tags are 0, 2, 1, 3, 4, and the obfuscation results of the third group of tags are 2, 1, 4, 3, 0, then the obfuscated tags array is: tags = [1, 4, 3, 2, 0, 0, 2, 1, 3, 4, 2, 1, 4, 3, 0].

[0075] (3) Generate a confusion function for each function, such as the sum function: sum(x: number, y: number) {

[0076] console.log("x + y = ", x + y); }

[0077] After obfuscation: confSum(tag: number, args: Array <any>) {

[0078] if (tag<0) {

[0079] this.sum(args[0], args[1]); } else {

[0080] for(let i = tag * this.funcs.length; i <tag * this.funcs.length +this.funcs.length; i++) {

[0081] if (this.tages[i] == 0) {

[0082] this.funcs[i % this.funcs.length](--tag, args); break; }

[0083] }

[0084] }

[0085] }

[0086] According to the above code, the steps to generate the obfuscation function are as follows: a. Determine the name of the obfuscated function. The name can be defined arbitrarily. In the above example, it is defined as confSum. b. The obfuscated function parameters are fixed to tag and args; c. Reference template code: if (tag<0) {

[0087] / / todo1 calls the unobfuscated function

[0088] } else {

[0089] for(let i = tag * this.funcs.length; i <tag * this.funcs.length +this.funcs.length; i++) {

[0090] if (this.tages[i] == the order of the function in the first step mapping relationship) { / / todo2

[0091] this.funcs[i % this.funcs.length](--tag, args); break; }

[0092] }

[0093] }

[0094] The principle of the above template code is as follows: 1. The tag passed in the parameter indicates the number of steps that need to be jumped at present, and whether the tag is less than 0 is determined; 2. If it is less than 0, it means that the target function has been jumped to, so the function is executed; 3. If it is greater than or equal to 0, it means that the current function is not the target function and the jump logic needs to be executed. How to jump needs to be determined according to the array and the tag passed in the parameter: (1) Define the variable i=tag*this.funcs.length, then i represents the starting index of the tag group in the array, and read the tag value in the array starting from index i in a loop. The number of reads is this.funcs.length, that is, the data of the tag group is read. (2) In the process of looping to read the tag value, determine whether the read tag is the order of the current function in the first step mapping relationship. If it is, it means that the function to jump to next is found, and the function is called at this time and the loop is jumped out. Note that when calling the function to jump to next, the first parameter tag needs to be reduced by 1, indicating that it is one step closer to the current function. 4. After executing the jump logic in step 3, enter the new function. This function also inserts template code, so steps 1-3 will also be executed. The process will exit when tag is less than 0.

[0095] (4) Replace todo1 and todo2 in the template code with real methods or variables.

[0096] (5) The obfuscated function needs to be written into the this.funcs array, this.funcs = [(tag, args) =>this.confSum(tag, args), (tag, args) =>this.confSub(tag, args), (tag, args)=>this.confDivide (tag, args), (tag, args) =>this.confMultiply(tag, args),(tag, args) =>this.confSquare(tag, args)].

[0097] (6) After generating the obfuscated file, you need to update the function call according to the mapping relationship and array generated in the first step.

[0098] The mapping relationship of the calculor class is: {"calculator":["sum":2,"sub":2,"divide":2,"multiply":2,"square":1]}

[0099] The array is: [1, 4, 3, 2, 0, 0, 2, 1, 3, 4, 2, 1, 4, 3, 0]

[0100] The order of the sum function in the mapping array is 0. The value of the first group in the array is 0, the value of the second group is 1, the value of the third group is 2, and the value of the third group is 2. Therefore, all calls to the sum function need to be replaced with calls to the function with subscript 4 in the funcs array in the calculator. For example: calculator.sum(2, 3)

[0101] Replace with: calculaotr.funcs[4](2, [2,3])

[0102] The first parameter 2 of the replaced function is the obfuscation level used by the calculator class minus 1, and the second parameter is the original parameter converted into an array type.

[0103] After the above steps, the calculator class is obfuscated as follows: export class calculator {

[0104] public funcs:Array <function>; private tages:Array <number>; constructor() {

[0105] this.tages = [1, 4, 3, 2, 0, 0, 2, 1, 3, 4, 2, 1, 4, 3, 0]; this.funcs = [(tag, args) =>this.confDivide(tag, args), (tag, args) =>this.confSub(tag, args),

[0106] (tag, args) =>this.confMultiply(tag, args), (tag, args) =>this.confSum(tag, args),

[0107] (tag, args) =>this.confSquare(tag, args)]; }

[0108] confSum(tag: number, args: Array <any>) {

[0109] if (tag<0) {

[0110] this.sum(args[0], args[1]); } else {

[0111] for(let i = tag * this.funcs.length; i<tag * this.funcs.length +this.funcs.length; i++) {

[0112] if (this.tages[i] == 0) {

[0113] this.funcs[i % this.funcs.length](--tag, args); break; }

[0114] }

[0115] }

[0116] }

[0117] sum(x: number, y: number) {

[0118] console.log("x + y = ", x + y); }

[0119] confSub(tag: number, args: Array <any>) {

[0120] if (tag<0) {

[0121] this.sub(args[0], args[1]); } else {

[0122] for(let i = tag * this.funcs.length; i<tag * this.funcs.length +this.funcs.length; i++) {

[0123] if (this.tages[i] == 1) {

[0124] this.funcs[i % this.funcs.length](--tag, args); break; }

[0125] }

[0126] }

[0127] }

[0128] sub(x: number, y: number) {

[0129] console.log("x - y = ", x - y); }

[0130] confDivide(tag: number, args: Array <any>) {

[0131] if (tag<0) {

[0132] this.divide(args[0], args[1]); } else {

[0133] for(let i = tag * this.funcs.length; i<tag * this.funcs.length +this.funcs.length; i++) {

[0134] if (this.tages[i] == 2) {

[0135] this.funcs[i % this.funcs.length](--tag, args); break; }

[0136] }

[0137] }

[0138] }

[0139] divide(x: number, y: number) {

[0140] console.log("x / y = ", x / y); }

[0141] confMultiply(tag: number, args: Array <any>) {

[0142] if (tag<0) {

[0143] this.multiply(args[0], args[1]); } else {

[0144] for(let i = tag * this.funcs.length; i<tag * this.funcs.length +this.funcs.length; i++) {

[0145] if (this.tages[i] == 3) {

[0146] this.funcs[i % this.funcs.length](--tag, args); break; }

[0147] }

[0148] }

[0149] }

[0150] multiply(x: number, y: number) {

[0151] console.log("x * y = ", x * y); }

[0152] confSquare(tag: number, args: Array <any>) {

[0153] if (tag<0) {

[0154] this.square(args[0]); } else {

[0155] for(let i = tag * this.funcs.length; i <tag * this.funcs.length +this.funcs.length; i++) {

[0156] if (this.tages[i] == 4) {

[0157] this.funcs[i % this.funcs.length](--tag, args); break; }

[0158] }

[0159] }

[0160] }

[0161] square(x: number) {

[0162] console.log("x ^ 2 = ", x * x); }

[0163] }

[0164] Figure 4 Schematic diagram of the effect of the code obfuscation method provided by the present invention. Figure 4 As shown, after the obfuscation method provided by the present invention, the process of calling FuncA changes from a direct call to a disordered multi-step call.

[0165] Based on the foregoing embodiments, an embodiment of the present invention provides a code obfuscation device. Each module included in the device and each unit included in each module can be implemented by a processor; of course, it can also be implemented by a specific logic circuit; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA), etc.

[0166] The code obfuscation device provided by the present invention is described below. The code obfuscation device described below and the code obfuscation method described above can be referred to each other.

[0167] Figure 5 This is a schematic diagram of the structure of the code obfuscation device provided by the present invention. Figure 5 As shown, the device 400 includes a function determination module 401, a function acquisition module 402 and a function update module 403, wherein: Function determination module 401, used to determine the target function in the code to be obfuscated, where the target function is the function to be obfuscated; A function acquisition module 402 is used to acquire an obfuscated function corresponding to the target function. When the obfuscated function is called, a first number of target transit functions are first called and then the target function is called. The first number is determined according to a target security level corresponding to the code to be obfuscated. The function updating module 403 is used to update the target function to the obfuscated function to obtain the obfuscated code.

[0168] In some embodiments, the function acquisition module 402 includes a number determination unit, a function determination unit and a function generation unit, wherein:

[0169] The number determination unit is used to determine the target obfuscation number corresponding to the target function, the target obfuscation number is used to indicate the number of times the target transfer function is called, and the higher the security level corresponding to the code to be obfuscated, the greater the target obfuscation number corresponding to the target function; The function determination unit is used to determine a target confusion array corresponding to the target confusion number, and the target confusion array is used to call the first number of target transfer functions and the target function; The function generating unit is used to determine the obfuscated function according to the target obfuscation array.

[0170] In some embodiments, the number determination unit is specifically used to: obtain a target security level corresponding to the code to be obfuscated; determine the target obfuscation number corresponding to the target security level according to the target security level and the correspondence between the preset security level and the obfuscation number, and the target obfuscation number corresponding to the target security level is the target obfuscation number corresponding to the objective function.

[0171] In some embodiments, the function determination unit is specifically used to: obtain a second number of initial arrays, each of the initial arrays includes the same multiple initial numerical values ​​and the arrangement order of the multiple initial numerical values ​​is different, the multiple initial numerical values ​​correspond to multiple transfer functions respectively, the multiple transfer functions include the target function, and the second number is equal to the sum of the target confusion times and one; select an initial numerical value from each of the initial arrays to obtain a second number of initial numerical values, and the initial numerical value selected from the first initial array corresponds to the target function; determine the target confusion array based on the second number of initial numerical values, and the target confusion array corresponds to the first number of target transfer functions and the target function.

[0172] In some embodiments, the function determination unit is also specifically used to: obtain the first initial value with the subscript of the target initial value in the first initial array of the second number of initial arrays, then obtain the second initial value with the subscript of the first initial value in the next initial array, and then obtain the third initial value with the subscript of the second initial value in the next initial array, and traverse the second number of initial arrays in this way to obtain the second number of initial values; according to the order in which the second number of initial values ​​are obtained, the second number of initial values ​​are arranged in reverse order starting from the last obtained initial value to obtain the target confusion array.

[0173] In some embodiments, the function updating module 403 is specifically used to: generate a target number of sub-obfuscation functions, each of which includes an initial value of a currently called function, the remaining number of function calls, and parameters corresponding to the target function; and determine the target number of sub-obfuscation functions as the obfuscated function.

[0174] In the embodiment of the present invention, different obfuscation degrees can be used to perform code obfuscation according to the security level of the source code file, thereby improving the security of the code obfuscation method.

[0175] Figure 6 Schematic diagram of the physical structure of the electronic device provided by the present invention. Figure 6 As shown, the electronic device 500 may include: a processor 501, a communication interface 502, a memory 503 and a communication bus 504, wherein the processor 501, the communication interface 502 and the memory 503 communicate with each other through the communication bus 504. The processor 501 may call the logic instructions in the memory 503 to execute the code obfuscation method, which includes: determining the target function in the code to be obfuscated, the target function is the function to be obfuscated; obtaining the obfuscated function corresponding to the target function, when the obfuscated function is called, first calling a first number of target transfer functions and then calling the target function, the first number is determined according to the target security level corresponding to the code to be obfuscated; updating the target function to the obfuscated function to obtain the obfuscated code.

[0176] In addition, the logic instructions in the above-mentioned memory 503 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0177] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the code obfuscation method provided by the above methods, and the method includes: determining a target function in the code to be obfuscated, wherein the target function is the function to be obfuscated; obtaining an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, wherein the first number is determined according to the target security level corresponding to the code to be obfuscated; updating the target function to the obfuscated function to obtain the obfuscated code.

[0178] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.

[0179] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the code obfuscation method provided by the above-mentioned methods, the method comprising: determining a target function in the code to be obfuscated, the target function being the function to be obfuscated; obtaining an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, the first number being determined according to a target security level corresponding to the code to be obfuscated; updating the target function to the obfuscated function to obtain an obfuscated code.

[0180] The above-mentioned computer-readable storage medium can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, - but not limited to - an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM) or flash memory, an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it.

[0181] Computer-readable signal media may include a data signal propagated in baseband or as part of a carrier wave, which carries a computer-readable program code. Such propagated data signals may take a variety of forms, including, but not limited to, electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.

[0182] The program code embodied on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wire, optical cable, radio frequency (RF), etc., or any suitable combination of the foregoing.

[0183] Computer program code for performing the operations of the present specification may be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, C++, and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0184] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0185] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0186] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.< / any> < / any> < / any> < / any> < / any> < / number> < / function> < / any>

Claims

1. A code obfuscation method, characterized in that: include: Determine a target function in the code to be obfuscated, wherein the target function is a function to be obfuscated; Obtaining an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, wherein the first number is determined according to a target security level corresponding to the code to be obfuscated; The target function is updated to the obfuscated function to obtain the obfuscated code.

2. The code obfuscation method according to claim 1, characterized in that: The obtaining the obfuscated function corresponding to the target function includes: Determine a target obfuscation number corresponding to the target function, where the target obfuscation number is used to indicate the number of times the target transfer function is called, and the higher the security level corresponding to the code to be obfuscated, the greater the target obfuscation number corresponding to the target function; Determine a target confusion array corresponding to the target confusion number, wherein the target confusion array is used to call the first number of target transfer functions and the target function; The obfuscated function is determined according to the target obfuscation array.

3. The code obfuscation method according to claim 2, characterized in that: Determining the target confusion number corresponding to the target function includes: Obtaining a target security level corresponding to the code to be obfuscated; According to the target security level and the correspondence between the preset security level and the obfuscation times, the target obfuscation times corresponding to the target security level are determined, and the target obfuscation times corresponding to the target security level are the target obfuscation times corresponding to the objective function.

4. The code obfuscation method according to claim 2, characterized in that: The determining of a target confusion array corresponding to the target confusion times includes: Acquire a second number of initial arrays, each of the initial arrays includes the same multiple initial values ​​and the multiple initial values ​​are arranged in a different order, the multiple initial values ​​correspond to multiple transfer functions respectively, the multiple transfer functions include the target function, and the second number is equal to the sum of the target confusion times and one; Selecting an initial value from each of the initial arrays to obtain a second number of initial values, the initial value selected from the first initial array corresponding to the objective function; The target confusion array is determined according to the second number of initial values, where the target confusion array corresponds to the first number of target transfer functions and the target function.

5. The code obfuscation method according to claim 4, characterized in that: The code to be obfuscated includes a plurality of functions to be obfuscated, the plurality of functions to be obfuscated are the plurality of transfer functions, the plurality of initial values ​​are integers increasing from zero, the target function is any one of the plurality of functions to be obfuscated, the target function corresponds to a target initial value in the second number of initial values, and selecting an initial value from each of the initial arrays to obtain the second number of initial values ​​comprises: Obtain a first initial value whose subscript is the target initial value in the first initial array of the second number of initial arrays, then obtain a second initial value whose subscript is the first initial value in the next initial array, then obtain a third initial value whose subscript is the second initial value in the next initial array, and traverse the second number of initial arrays in this way to obtain the second number of initial values; The step of determining the target obfuscation array according to the second number of initial values ​​includes: According to the acquisition order of the second number of initial values, the second number of initial values ​​are arranged in reverse order starting from the last acquired initial value to obtain the target confusion array.

6. The code obfuscation method according to claim 2, characterized in that: The step of determining the obfuscated function according to the target obfuscation array comprises: Generate a target number of sub-obfuscation functions, each of which includes an initial value of a currently called function, the number of remaining function calls, and parameters corresponding to the target function; The target number of sub-confusion functions are determined as the post-confusion functions.

7. A code obfuscation device, characterized in that: include: A function determination module, used to determine a target function in the code to be obfuscated, wherein the target function is a function to be obfuscated; a function acquisition module, configured to acquire an obfuscated function corresponding to the target function, wherein when the obfuscated function is called, a first number of target transit functions are first called and then the target function is called, wherein the first number is determined according to a target security level corresponding to the code to be obfuscated; The function updating module is used to update the target function to the obfuscated function to obtain the obfuscated code.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the code obfuscation method according to any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the code obfuscation method according to any one of claims 1 to 6 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the code obfuscation method according to any one of claims 1 to 6 is implemented.