关于java:Java代码混淆工具入门Allatori~

29次阅读

共计 4027 个字符,预计需要花费 11 分钟才能阅读完成。

作者:threedayman

起源:恒生 LIGHT 云社区

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;


@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 <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 官网

正文完
 0