您的位置 首页 java

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

Allatori 是什么

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

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

使用案例

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

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

 <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中加入编译时需要用到的插件

 <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(混淆后)文件。

通过反编译工具查看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;


@ Rest Controller
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@@IC05")).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错误。

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

参考

allatori官网

文章来源:智云一二三科技

文章标题:Java代码混淆工具入门——Allatori

文章地址:https://www.zhihuclub.com/190402.shtml

关于作者: 智云科技

热门文章

网站地图