Recently, due to the company's project needs, learned a lot about the class loading knowledge, to the project has brought some thermal deployment breakthroughs. Due to the recent lack of busy work and the curiosity and thirst for a lower level of knowledge, I decided to learn a class file structure, and through a week of unremitting efforts, already mastered the class file structure, and Java implementation of a simple anti-compiler: Read the class file, decompile into pure Java code. Here's a look at the specific implementation ideas and code analysis.
1. class file is a platform-independent binary file, through the IO stream can read into byte[], the byte array into 16 (string), the data structure of class is at a glance, the class The parsing of the file becomes the segmentation and parsing of the entire hexadecimal string .
2. So how to split it? In fact, class files store data in the form of a "pseudo-struct" that has only two data types: unsigned numbers and tables (the data in the table is also unsigned). The concept of a table we all know, what is an unsigned number? As we all know, the most basic unit of data in a computer is bytes, 1 bytes (byte) = 8 bits (bit), which is 8-length binary, and 4-length binary can represent 1-length hexadecimal, so two hexadecimal represents a byte, which is identified with an unsigned number:
-
-
- The U1 represents a byte that represents a 2-length hexadecimal (such as 0x01);
- The U2 represents two bytes, representing 4-length hexadecimal (such as 0x0001);
- U4 represents 4 bytes, representing 8-length hexadecimal (e.g. 0x00000001)
3. The entire class file is a table in which the fields are: Magic number, minor version of the virtual machine, major version, Chang size, Chang, Access ID, current class, parent class, number of interfaces implemented, interface collection, Number of field tables, Field table collection, method table number, Method table collection, property sheet number, property sheet collection. Among them, magic number, primary and secondary version, Chang size, access identifier, current class, parent class, number of table collection, and so on are unsigned numbers. The constant pool, the Field table collection, the Method table collection, the property sheet collection, and so on are all table structures, and the fields in the table structure are nested in other table structures. The specific unsigned number size and table structure are not unfolded here, in a nutshell:the data structure of a class file is a nesting of table structures .
4. The above is a brief introduction to the data structure of the class file, and now we begin to discuss how to parse and store the class file. We can create the corresponding bean according to the various table structures in the class file, for example, for the entire class file, i.e., Class_info, we can create the following bean:
Public classClass_info {PrivateString Magic;//Magic number PrivateString minor_version;//Virtual Machine minor version PrivateString major_version;//Virtual Machine Major version Private intCp_count;//Constant Pool Size PrivateMap<integer, constant_x_info> Constant_pool_map;//Constant Pool PrivateString Access_flag;//Access Identity Private intThis_class_index;//Current class Index Private intSuper_class_index;//Parent Class Index Private intInterfaces_count;//Number of interfaces PrivateList<integer> interfaceslist;//Interface Collection Private intFields_count;//Number of field tables PrivateList<fields_info> fields_info_list;//Field table Collection Private intMethods_count;//number of method tables PrivateList<methods_info> methods_info_list;//Method Table Collection Private intAttributes_count;//number of attribute tables PrivateList<attribute_info> attributes;//Property Sheet Collection PublicString getmagic () {returnMagic; } Public voidsetmagic (String magic) { This. Magic =Magic; }
..... Omit other Get Set
}
Once all the table structures are set up, we can begin to cut the hexadecimal string that the class file reads into and populate our bean with the cut data. Here, we provide a way to cut the string: Create a static pointer to the start position of the cut string, and after each cut length, initialize the pointer, that is, start = start + length. If you want to cut the data, you just need to call cutstring (int len). The code is as follows:
Private Static int start_pointer = 0; Private Static // Hex String Private Static String cutstring (int len) { = hexstring.substring (start_pointer, Start_pointer + len) ; // Initializing pointers Start_pointer = Start_pointer + len; return cutstr; }
5. Please note that although the above is simple to say, but the cutting data can not be mistaken for any length of a byte, if the wrong length of any one byte, then the data behind is completely misplaced , must be torn down again! After a series of efforts, finally all the data are cut and filled into the bean, the following is the use of data, the Java source code is assembled. The most important part of this is the assembly of the method body, in the process of compiling, the compiler has compiled the Java statements in the method body into bytecode instructions, is completely memory stack operation, with our previous Java code than completely changed form and syntax. So, how to derive the Java source code according to the bytecode instruction? Summing up all of the Java syntax is nothing more than:
-
- New Object
- Method Invocation (static method, constructor method, member method, interface method)
- Parameter passing
- Calculate, judge, assign value
- Other statements (if for while try, etc.)
We need to be quite skilled in reading bytecode instructions and need to reach 1. Looking at the Java code, the compiled bytecode instruction 2. Look at the bytecode and reverse the Java code. On this basis, a large number of regular summary, which is the most difficult to decompile, the core of the place. Because the content is more complex, this does not repeat, you can view the author's project source code.
6. The simple anti-compiler implemented by the author has been open source to Github:https://github.com/malcolmff/decompiler, of which the most important two classes are: Com.xuanjie.app.App.java ( The main method is the class, parsing the class file to store the data in the Bean) and Com.xuanjie.core.SrcCreator.java (for Java source Code assembly).
Readers are welcome to carry out surely and make suggestions to maintain perfect together.
Java implementation of the class file anti-compiler