Java代码混淆工具入门——Allatori~

作者:threedayman

来源:恒生LIGHT云社区

Allatori 是什么

Allatori是第二代java代码混淆工具,为你的产品知识产权提供全方位的保护。通过代码混淆,让代码逆向工程几乎变得不可能。

除了代码混淆作用,Allatori还可以最小化应用代码大小,提高应用启动速度。

使用案例

创建一个mixup的maven工程如下图

Java代码混淆工具入门——Allatori~_第1张图片

如上图 在根目录下创建allatori文件夹,放入配置文件allatori.xml,创建lib文件夹,在其下面放入allatori.jar和allatori-annotations.jar。


    
         
         
    
  
    
          
                
              
            
        
    
    
        
        
    

allatori.xml配置详细可见注释。

pom.xml中加入编译时需要用到的插件


    org.apache.maven.plugins
    maven-resources-plugin
    2.6
    
        
            copy-and-filter-allatori-config
            package
            
                copy-resources
            
            
                ${basedir}/target
                
                    
                        allatori
                        
                            allatori.xml
                        
                        true
                    
                
            
        
    


    org.codehaus.mojo
    exec-maven-plugin
    1.2.1
    
        
            run-allatori
            package
            
                exec
            
        
    
    
        java
        
            -Xms128m
            -Xmx512m
            -jar
            allatori/lib/allatori.jar
            ${basedir}/target/allatori.xml
        
    

通过maven package命令进行打包,在taget目录下生成了mixup-0.0.1-SNAPSHOT.jar和mixup-0.0.1-SNAPSHOT-obfuscated.jar(混淆后)文件。

Java代码混淆工具入门——Allatori~_第2张图片

通过反编译工具查看mixup-0.0.1-SNAPSHOT-obfuscated.jar。

混淆前代码

package com.example.mixup.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

    private Person person;

    @RequestMapping("/hello")
    public String sayHello(String name){
        return "hello "+ name;
    }
}

混淆后通过反编译工具查看

package BOOT-INF.classes.com.example.mixup.controller;

import com.example.mixup.controller.Person;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
  private Person h;
  
  @RequestMapping({"/hello"})
  public String sayHello(String name) {
    return (new StringBuilder()).insert(0, a("D@@IC\005")).append(name).toString();
  }
  
  public static String a(Object s) {
    // Byte code:
    //   0: iconst_4
    //   1: iconst_3
    //   2: ishl
    //   3: iconst_5
    //   4: ixor
    //   5: iconst_4
    //   6: iconst_3
    //   7: ishl
    //   8: iconst_5
    //   9: iconst_3
    //   10: ishl
    //   11: iconst_4
    //   12: ixor
    //   13: aload_0
    //   14: checkcast java/lang/String
    //   17: dup
    //   18: astore_0
    //   19: invokevirtual length : ()I
    //   22: dup
    //   23: newarray char
    //   25: iconst_1
    //   26: dup
    //   27: pop2
    //   28: swap
    //   29: iconst_1
    //   30: isub
    //   31: dup_x2
    //   32: istore_3
    //   33: astore_1
    //   34: istore #4
    //   36: dup_x2
    //   37: pop2
    //   38: istore_2
    //   39: iflt -> 79
    //   42: aload_1
    //   43: aload_0
    //   44: iload_3
    //   45: dup_x1
    //   46: invokevirtual charAt : (I)C
    //   49: iinc #3, -1
    //   52: iload_2
    //   53: ixor
    //   54: i2c
    //   55: castore
    //   56: iload_3
    //   57: iflt -> 79
    //   60: aload_1
    //   61: aload_0
    //   62: iload_3
    //   63: iinc #3, -1
    //   66: dup_x1
    //   67: invokevirtual charAt : (I)C
    //   70: iload #4
    //   72: ixor
    //   73: i2c
    //   74: castore
    //   75: iload_3
    //   76: goto -> 39
    //   79: new java/lang/String
    //   82: dup
    //   83: aload_1
    //   84: invokespecial  : ([C)V
    //   87: areturn
    // Local variable table:
    //   start    length    slot    name    descriptor
    //   0    88    0    s    Ljava/lang/Object;
  }
}

混淆后代码难以阅读,通过java -jar mixup-0.0.1-SNAPSHOT-obfuscated.jar命令,混淆后的代码能够正常运行。

PS:allatori.xml中要加入一下代码,不要混淆spring框架中的代码,不然会影响springboot项目启动,出现ClassNotFoundException错误。


    
    

参考

allatori官网

你可能感兴趣的:(java)