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

本文由 简悦 SimpRead 转码, 原文地址 segmentfault.com

作者:threedayman 来源:恒生 LIGHT 云社区 Allatori 是什么 Allatori 是第二代 java 代码混淆工具,为你的产品知识产权提供全方位的保护。

作者:threedayman

来源:恒生 LIGHT 云社区

Allatori 是什么

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

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

使用案例

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

https://segmentfault.com/img/remote/1460000040541701

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<config>
    <input>
         <!--混淆后直接覆盖原文件,out指向的路劲为混淆后的jar -->
        <jar in="mixup-0.0.1-SNAPSHOT.jar"  out="mixup-0.0.1-SNAPSHOT-obfuscated.jar" /> 
    </input>
  
    <keep-names>
        <!-- protected/public的都保留名称 -->  
        <class access="protected+">        
            <field access="protected+" />  
            <method access="protected+" />
        </class>
    </keep-names>
    <ignore-classes>
        <!-- 注意:spring的框架相关的文件需要排除,避免启动报错 -->
        <class template="class *springframework*"/>
    </ignore-classes>

</config>

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

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-and-filter-allatori-config</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target</outputDirectory>
                <resources>
                    <resource>
                        <directory>allatori</directory>
                        <includes>
                            <include>allatori.xml</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>run-allatori</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Xms128m</argument>
            <argument>-Xmx512m</argument>
            <argument>-jar</argument>
            <argument>allatori/lib/allatori.jar</argument>
            <argument>${basedir}/target/allatori.xml</argument>
        </arguments>
    </configuration>
</plugin>

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

https://segmentfault.com/img/remote/1460000040541702

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

混淆前代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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;
    }
}

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 <init> : ([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 错误。

1
2
3
4
<ignore-classes>
    <!-- 注意:spring的框架相关的文件需要排除,避免启动报错 -->
    <class template="class *springframework*"/>
</ignore-classes>

参考

allatori 官网