A kind of method and apparatus protecting JAVA executable program
Technical field
The present invention relates to field of software protection, particularly a kind of method and apparatus protecting JAVA executable program.
Background technology
JAVA software is generally made up of some resource files and JAVA executable file (independent .class file or the .class file be packaged in .jar or .war bag).JAVA executable file is the binary file that can be performed by JAVA virtual machine, its code has nothing to do with platform, form follows JAVA virtual machine specification, is easy to be reduced to JAVA source code by some decompiling analysis tools, causes the logic of software and flow process to be revealed and steal.
In JAVA language, the method function of class object has two types: commonsense method and nation method.Commonsense method is write by JAVA language to be compiled into JAVA instruction code, performed during operation by JAVA virtual machine (JAVAVirtualMachine, JVM); Another kind of nation method (NativeMethod), it is called by JNI(JAVANativeInterface, JAVA this locality) the storehouse operating in native operating sys-tern of interface accessing outside.JNI achieves mutually calling between JAVA program and external libraries, is commonly used to the function for JAVA program provides non-JAVA language to realize.
Software is made up of one or more JAVA executable file (as .jar .war or .class) and one or more local dynamic link library file (as forms such as .dll or .so).Code in executable file in JAVA can call export interface in dynamic link library (interface), realizes specific function.Class (class) is the key concept of JAVA, and .jar file comprises multiple .class file (jar bag and war bag etc. are all made up of some .class files and resource file), is no longer described in detail it.
JAVA software protection means common are at present to the JVM(JAVAVirtualMachineJAVA in .class file; virtual machine) code carries out obscuring process; or by means of self-defining ClassLoader .class file encryption is stored, just decipher when being loaded into JVM.The former processes in the aspect of JVM instruction, is subject to the restriction of JVM specification, and the effect of automation algorithm desirable not enough (common automation algorithm comprises title and symbol is obscured, but not easily automatically realizes complicated flow process and class relation); And the latter is after JVM loads .class file, just have original .class code in internal memory, be easily subject to internal memory DUMP(namely, content when being run by JVM in proceeding internal memory is carried out " snapshot " and is saved in file) attack.
Summary of the invention
In order to prevent the code of JAVA software and logic by reverse and crack, the invention provides a kind of method and apparatus protecting JAVA executable program.First a local dynamic link library file is created; enumerate the .class file in protected JAVA software; analyze the JVM code in this .class file; randomly draw the partial code fragment in JVM code; replace with to native method (namely in the situ of extracted partial code fragment; nation method) call, in local dynamic link library, generate derivative function, the function of the partial code fragment be extracted described in realization.By method provided by the invention, the security intensity of software can be improved.
The present invention is not only applicable to windows platform, is applicable to the operating system that other use dynamic base mechanism yet.
Protect a method for JAVA executable program, concrete steps comprise:
1. create a local Dynamic link library library file;
2. enumerate the .class file in shielded JAVA software;
3. analyze the JVM code in this .class file, random selecting code snippet;
4. pumped from .class file by the code snippet chosen, original position replaces with calling native method;
5., in local dynamic link library in step 1, generate a derivative function, function name is corresponding with the native method in step 4, and the instruction one by one in simulation code fragment, realizes the function of the code snippet extracted in step 4.
6. get back to step 2, continue to enumerate, until terminate.
According to an aspect of the present invention, the local dynamic link library file title in described step 1 is random.
According to an aspect of the present invention, realize for ease of robotization in described step 3, extract code snippet and follow single-input single-output and storehouse balance principle.Described single-input single-output and storehouse balance principle, refer to that performing flow process can only enter this section of code from described code snippet beginning, cannot jump in the middle of described code snippet from described code snippet, and this section of code can only be left from described code snippet ending, cannot jump to outside code snippet from described code snippet; Fundamental operation in described code snippet is complete, does not relate to the temporary variable in JVM storehouse when passing in and out described code snippet.These two principles can be realized by static analysis JVM instruction.
According to an aspect of the present invention, in step 4, native method name is random.
According to an aspect of the present invention, the instruction in step 5 one by one in simulation code fragment comprise use JNI(JAVANativeInterfaceJAVA this locality to call to the instruction such as access classes, object) simulation; Arithmetic sum controls transfer and uses local code simulation.
Protect a device for JAVA executable program, specifically comprise:
Enumeration module, for enumerating the .class file in shielded JAVA software, and can extract .class file from .jar .war bag;
Analysis module, for analyzing the JVM code in .class file, chooses code snippet according to single-input single-output and storehouse balance principle;
Replacing code module, for being extracted away from .class file by the code snippet chosen, replacing with calling the native method that is named at random in the situ of the code snippet be extracted;
Generate local code module, for generating a local code derivative function, the function of the code snippet be extracted described in realization.
Use in the JAVA software after the present invention's protection; the logic of code is dispersed in local dynamic library file, can take precautions against the decompiling static analysis of JVM aspect preferably, and is replaced by local code due to partial logic; flow process is dispersed in inside and outside JVM, adds the difficulty analyzed and crack.In addition, the code in local dynamic base directly runs, and can not have the code before replacement, like this from the attack of DUMP in JVM.Therefore the present invention protects logic and the flow process of software preferably, and code when running and data security, and can provide the algorithm realization of robotization, improve the security of software.
Accompanying drawing explanation
Fig. 1 is according to a kind of process flow diagram protecting the embodiment 1 of the method and apparatus of JAVA executable program of the present invention.
Fig. 2 is according to a kind of overall flow schematic diagram protecting a preferred embodiment of the method and apparatus of JAVA executable program of the present invention.
Fig. 3 is according to a kind of structured flowchart protecting the method and apparatus of JAVA executable program of the present invention.
Embodiment
For making object of the present invention, technical scheme and advantage clearly understand, to develop simultaneously embodiment referring to accompanying drawing, the present invention is described in more detail.
According to one embodiment of present invention, as shown in Figure 2, provide a kind of method protecting JAVA executable program, concrete steps comprise:
1. create a local Dynamic link library library file
2. enumerate the .class file in shielded JAVA software
3. analyze the JVM code in this .class file, random selecting code snippet
4. pumped from .class file by the code snippet chosen, original position replaces with calling native method
5., in local dynamic link library in step 1, generate a derivative function, function name is corresponding with the native method in step 4, and the instruction one by one in simulation code fragment, realizes the function of the code snippet extracted in step 4.
6. get back to step 2, continue to enumerate, until terminate.
According to an aspect of the present invention, the local dynamic link library file title in described step 1 can be random.
According to an aspect of the present invention, realize for ease of robotization in described step 3, extract code snippet and follow single-input single-output and storehouse balance principle.
According to an aspect of the present invention, in step 4, native method name can be random.
According to an aspect of the present invention, the instruction in step 5 one by one in simulation code fragment comprise use JNI(JAVANativeInterfaceJAVA this locality to call to the instruction such as access classes, object) simulation; Arithmetic sum controls transfer and uses local code simulation.
Described single-input single-output and storehouse balance principle, namely performing flow process is can only enter this section of code from fragment beginning, cannot jump in the middle of fragment from fragment, can only leave this section of code, cannot jump to outside in fragment from fragment ending.And fundamental operation is complete in fragment, during turnover fragment, do not relate to the temporary variable in JVM storehouse.These two principles can be realized by static analysis JVM instruction.
According to one embodiment of present invention, as shown in Figure 3, a kind of equipment protecting JAVA executable program is provided, specifically comprises:
Enumeration module, for enumerating the .class file in shielded JAVA software, and can extract .class file from .jar .war bag.
Analysis module, for analyzing the JVM code in .class file, the principle according to single-input single-output and storehouse balance chooses code snippet.
Replace code module, for being extracted away from .class file by the code snippet chosen, original position replaces with calling the native method that is named at random.
Generating local code module, for generating a local code derivative function, realizing the function of the JAVA code snippet extracted.As preferably, the equipment of the protection executable program of the present embodiment also comprises encrypting module, for being encrypted the information such as function or supplemental characteristic.As shown in Figure 3, in Fig. 3, encrypting module is encrypted for local code generation module.According to one embodiment of present invention, concrete cipher mode includes but not limited to: Custom Encryption algorithm, or disclosed symmetry, rivest, shamir, adelman.According to one embodiment of present invention, decipher during running software after protection.Code and the relevant key etc. of deciphering are placed in local code derivative function.
The present invention creates a local dynamic link library file; enumerate the .class file in protected JAVA software; analyze the JVM code in this .class file; randomly draw code snippet; former extraction position replaces with calling native (nation method) method; in local dynamic link library, generate derivative function, realize the function extracting code snippet.According to one embodiment of present invention, create local dynamic link library file and automatically generate by software programming instrument, create local dynamic link library and belong to the state of the art, and non-invention emphasis, the application is not described in detail.
。Use in the JAVA software after the present invention's protection; the logic of code is dispersed in local dynamic library file, can take precautions against the decompiling static analysis of JVM aspect preferably, and is replaced by local code due to partial logic; flow process is dispersed in inside and outside JVM, adds the difficulty analyzed and crack.In addition, the code in dynamic base at the bottom of cup directly runs, and can not have the code before replacement, so, from the attack of DUMP in JVM.Therefore the present invention protects logic and the flow process of software preferably, and code when running and data security, and can provide the algorithm realization of robotization, improve the security of software.
Embodiment 1
According to one embodiment of present invention, as shown in Figure 1, Fig. 1 comprises the code signal fragment of each several part in the present embodiment.Instantiation is as follows: certain software write by JAVA, has a class MyClass in source code, there is defined three field a, b, c and a case method Mul, and the logic of the method is multiplied at the value of field b and c, and result is assigned to field a.
Java source code is when compiling, and can generate a .class file for each class, the structure of this .class file defines in JVM document, is the set of a series of attribute and value.By resolving the class title association attributes in .class file, can learn it comes from which class in source code; Analytic method Table Properties again, can obtain methodical list in class; Resolve the Code attribute of each method, the JVM bytecode of method can be obtained.
Such as, after compilation of source code shown in Fig. 1, corresponding MyClass.class file, has Mul method in the method table parsed, and bytecode (JVM instruction) is as follows:
(be only signal, actual JVM instruction can be more complicated)
LoadMyClass.a//a the field of existing object is loaded into JVM run storehouse
LoadMyClass.c//c the field of existing object is loaded into JVM run storehouse
Mul//two numbers in storehouse are ejected and are multiplied, by operation result pop down
SetFieldMyClass.b//by the number in storehouse ejects and is saved in the b field of existing object
According to JVM document, the jump instruction in bytecode is all only limited to method inside, namely can not to jump in the code of additive method (call instruction also can only transfer to additive method code initial), because the method is " singly entering "; Again because the target location of all jump instructions staticly (namely just to determine when compiling, there is no register and indirect branch, it is also static for comprising abnormality processing etc.), so each JVM instruction column in method can be become a table, whether whether analyzing and marking each instruction is redirect or call instruction, and be possible redirect destination.Consider the code snippet be made up of continuous print some JVM instructions, if they are not redirect or call instruction (or have redirect but destination in fragment) yet, neither redirect destination (or destination but all from interval), then this fragment is single-input single-output.
JVM is based on storehouse, and the impact of execution on storehouse of each instruction has regulation in a document.As loadint instruction can be pressed into a word in storehouse, and mulint can eject 2, then is pressed into 1, amounts to and is equivalent to minimizing word, can static determine when these also all compile.To JVM instruction list in method, and record the impact of every bar instruction execution on storehouse with a stack pointer.If some the JVM instructions continuously in certain single-input single-output interval, after performing, stack pointer is constant, then this sub-range is storehouse balance.Between the JVM instruction area illustrated above, be namely single-input single-output and storehouse balance.
After protecting by method of the present invention, the above JVM instruction in .class file is pumped, and replaces with calling nation method native_fun123.And be derived native_fun123 function in newly-increased local dynamic link library, the above JVM instruction of instruction simulation one by one.According to the definition in JVM document, to each JVM instruction, finish writing one section of local code all in advance to simulate its function.As access object field JNI simulates, multiplying then uses local cpu multiplying order to simulate.Local code fragment assembly corresponding for all instructions in JVM instruction fragment is got up, just becomes the code of nation method.
Can see not had the JVM instruction of Mul method in the software after protection, operationally the key logic of the method is also realized by local code and JNI.
This method is not only applicable to windows platform, is also applicable to the operating system that other use dynamic base mechanism.When other operating system uses, method step is substantially identical with windows platform step, repeats no more herein.
The foregoing is only preferred embodiment of the present invention, be not intended to limit protection scope of the present invention.Within the spirit and principles in the present invention all, any amendment done, equivalent replacement and improvement etc., all should be included within protection scope of the present invention.