Currently, Java program encryption methods are similar to Java fuzzy processing (obfuscator) and classloader encryption methods, but most of them are extensions and variations ). No matter how difficult the two methods cause to Java anti-compiler, there are still traces to find and opportunities to multiply. The method described in this article is an improvement in classloader encryption to ensure the security of traditional binary program code.
Chapter 1 Introduction to popular encryption methods
Java program encryption methods have always been dominated by Java fuzzy processing (obfuscator. There are also a lot of research results in this area, including fuzzy machine (such as jode ), there are also "bombs" for the anti-compiler (such as the "bombs" crema and hosemocha for The Decompilation tool Mocha ). Fuzzy machine, literally, we can know that it uses fuzzy processing of Java code. Specifically, it is to replace the variable name and function name, even the method such as the class name makes the decompiled code ununderstandable. For example.
Compile the following source code into a class file.
public class test{int sortway;void sort(Vector a){……}void setSortWay(int way){……}void sort(Vector a, int way){……}} |
After fuzzy processing through Jode, The decompiled code may become the following code.
public class OoOoooOo0Oo0O{int OoOo0oOo0Oo0O;void OoO0ooOo0Oo0O (Vector OoOoo0Oo0OoOO){……}void OoOo00oOoOo0O (int Oo0oooOo0Oo0O){……}void OoO0ooOo0Oo0O (Vector OoOoo0Oo0OoOO, int Oo0oooOo0Oo0O)
{OoOo00oOoOo0O (Oo0oooOo0Oo0O);OoO0ooOo0Oo0O (OoOoo0Oo0OoOO);}} |
In fact, this only achieves visual processing, but its business logic remains unchanged. Patience can still be broken. If it is used for user authentication and other purposes, the authentication algorithm can be found to break through the restrictions.
The so-called "bomb" is a defect in the decompilation tool itself. This method is very effective for a specific decompilation tool. However, up to now, there has not been a full solution, it is effective for each decompilation tool, and its limitations are obvious!
Another method is classloader encryption. The Java virtual machine uses an object called classloader to load the bytecode of class files, and the classloader can be customized by the Java program itself. How does classloader load classes? Classloader finds the class file in the jar package based on the class name, reads the file, and converts it into a class object. The principle of this method is to encrypt the class files to be encrypted (such as Pgp, RSA, and MD5). After reading the files, after decryption, convert to a class object.
The detailed introduction of classloader's working methods is not described here. I have discussed this topic in the previous article.
Have you found that this method does not solve the security of classloader itself? Obviously, as long as the classloader class is decompiled, you can find other classes. It can be seen that the classloader itself "plaintext" method still causes certain security risks. However, if this method solves the security of classloader itself, it is a better security solution.
Chapter 2 classloader Encryption Method Improvement
The Java program is started through java.exe/javaw.exe. to perform encryption on classloader, you can only start with java.exe/javaw.exe.
Let's take a look at the release path of JDK. We found that every version of JDK provides SRC. Jar. Open it with WinZip and you can see the program code of java.exe/javaw.exe included in a launcherfolder. Haha, now we can do what we like. :-) Open java. C. There is a section in it, as shown below:
jstring mainClassName = GetMainClassName(env, jarfile);if ((*env)->ExceptionOccurred(env)) { (*env)->ExceptionDescribe(env); goto leave;}if (mainClassName == NULL) { fprintf(stderr, "Failed to load Main-Class manifest attribute " "from/n%s/n", jarfile); goto leave;}classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);if (classname == NULL) { (*env)->ExceptionDescribe(env); goto leave;}mainClass = LoadClass(env, classname);(*env)->ReleaseStringUTFChars(env, mainClassName, classname); } else {mainClass = LoadClass(env, classname); } if (mainClass == NULL) { (*env)->ExceptionDescribe(env); status = 4;goto leave; } |
For the loadclass function, see:
static jclassLoadClass(JNIEnv *env, char *name){ char *buf = MemAlloc(strlen(name) + 1); char *s = buf, *t = name, c; jclass cls; jlong start, end; if (debug)start = CounterGet(); do { c = *t++;*s++ = (c == '.') ? '/' : c; } while (c != '/0'); cls = (*env)->FindClass(env, buf); free(buf); if (debug) {end = CounterGet();printf("%ld micro seconds to load main class/n", (jint)Counter2Micros(end-start));printf("----_JAVA_LAUNCHER_DEBUG----/n"); } return cls;} |
After analyzing the above program, we can see that the findclass function in env directly obtains the mainclass object based on the class name. If we want to load the encrypted Java program, it is obviously not possible to directly call the findclass function. Is there any way we can read the file ourselves and convert it into a mainclass object?
Let's see what else is in jnienv? Open the JDK path/include/JNI. h and find the following definitions:
#ifdef __cplusplustypedef JNIEnv_ JNIEnv;#elsetypedef const struct JNINativeInterface_ *JNIEnv;#endif |
In the definition of jninativeinterface:
struct JNINativeInterface_ { …… jclass (JNICALL *DefineClass) (JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len);……} |
By the way, defineclass is what we are looking for. It can convert a buffer zone (class bytecode) into a class instance! The following is how to load the encryption class:
Static jclassloadclass (jnienv * ENV, char * Name) {file * In; long length, I; char * cc; int X; char javaloader [MAXPATHLEN], javapath [MAXPATHLEN]; char * Buf = memalloc (strlen (name) + 1); char * s = Buf, * t = Name, C; jclass CLS; jlong start, end; If (Debug) start = counterget (); do {c = * t ++; * s ++ = (C = '. ')? '/': C;} while (C! = '/0');/* If the loaded class is myloader */If (strcmp (BUF, "myloader") = 0) {If (getapplicationhome (javapath, sizeof (javapath) {sprintf (javaloader, "% S // myloader. class ", javapath);} If (in = fopen (javaloader," rb ") = NULL) {fprintf (stderr," cannot open input file. /n "); Return (jclass) 0x0f;}/* read the encrypted class file */fseek (in, 0l, seek_end); length = ftell (in ); fseek (in, 0, seek_set); CC = memalloc (length); fread (void *) CC, length, 1, In); fclose (in);/* decryption algorithm */...... /* Convert the decrypted class bytecode to class */CLS = (* env)-> defineclass (ENV, Buf, 0, CC, length-1 ); free (CC);} elsecls = (* env)-> findclass (ENV, Buf); free (BUF); If (Debug) {end = counterget (); printf ("% LD micro seconds to load main class/N", (jint) counter2micros (end-Start); printf ("---- _ java_launcher_debug ----/N ");} return CLS ;} |
Chapter 3 Application Examples
In the example application, we recommend that the new startup program follow the parameter call format of java.exe, that is, Java [-options] class [ARGs...], in this way, on the one hand, the calling method of the program during development (non-encrypted) and release (encrypted) is the same, which is easy for others to understand, on the other hand, it is much easier to start the program. You only need to modify the java. the loadclass method in C.
The following are common applications:
If the calling method is as follows: class1 calls class2, and class2 calls class3, class2 has its own classloader (classloader used by non-class3 ), in this case, an interface should be added between class2 and class3. The interface calls class3's corresponding classloader to load class3. the interface itself cannot be encrypted. A typical application in this form is a Web application on Tomcat. When Tomcat loads Servlet classes, it uses its own classloader to load them. If you encrypt the servlet, tomcat will not be loaded successfully when loading the servlet. You must use the interface method! The following are their applications:
Chapter 4 Application Scope
Since decryption takes a certain amount of time, if encryption is performed without distinction, the speed and response of the program will inevitably be affected. Therefore, encryption should be performed only when encryption is needed. For example, user password verification, patented algorithms, or database passwords will not cause system performance degradation.
To achieve the above purpose, classloader must judge the class. non-encrypted classes call the loadclass function of the JVM system classloader, and encryption is used to decrypt the class. Suggestion: classloader is best configurable!