一款上手即用的Java代码混淆工具

基于Allatori的Java代码混淆 maven插件工具

项目地址:全球最大同性交友网站

Java代码很容易进行反编译,如果有人想要研究你的项目,随便反编译一下就看得清清楚楚了,特别像我们公司的测试人员,经常会反编译我们的项目,然后找缺陷(这操作没谁了),另外就是公司的项目对外发布时希望能够进行加密,所以这时候就需要对源码进行混淆,增加阅读成本,当然只要花时间,总还是能看懂的,这个是java底层限制了,不可能完全加密。

不过一般人看到这种代码,应该会放弃了!
在这里插入图片描述

在这里插入图片描述

接下来就具体说说这个插件吧!

这个插件是基于Allatori来实现的。

Allatori本身是支持在maven中集成使用的,但是它只能指定哪些文件不混淆,然后会对剩余文件全部混淆,在实际业务中有时候可能只是想对部分涉密文件进行混淆,所以封装了这个工具。

另外刚开始接触这个Allatori,可能对它的配置文件比较陌生,也容易踩坑,所以我在这个插件中直接定义了一些通用配置,无需你再改配置,只需要在项目根目录下创建一个classNames.properties文件,在其中指定需要混淆的类全类名即可。

使用方式:

  1. mvn install 此项目到本地仓库,然后在想要混淆的项目的pom文件中引入此插件
  2. 在需要混淆的项目的pom文件中导入依赖即可使用。build -> plugins中导入依赖即可使用
  3. 注意事项:如果需要在正式环境中使用,请将此maven插件项目放到私服远程maven仓库中
  4. 在对项目进行打包时此插件便会运行

pom文件中引入:

</build>
 </plugins>
   <plugin>
                <groupId>com.dwp</groupId>
                <artifactId>obfuscation</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>obfuscation</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <basePath>${basedir}</basePath>
                    <classPath>${basedir}/target/classes</classPath>
                </configuration>
            </plugin>
        </plugins>
    </build>

classNames.properties示例(将需要混淆的类全类名写到此文件中即可):

com.dwp.a
com.dwp.b
com.dwp.c

支持Windows、Linux、MacOS系统下使用。

插件主类代码如下欢迎各位大佬去github下载
(要是能帮我点点star,更是不胜感激了),水平有限,欢迎指教:

package com.dwp;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.net.URL;

/**
 * 混淆插件主类
 *
 * @author denngweiping
 * 2020-04-01
 */
@Mojo(name = "obfuscation", defaultPhase = LifecyclePhase.PACKAGE)
public class Obfuscation extends AbstractMojo {
    /**
     * 项目根目录
     */
    @Parameter
    private String basePath="D:/sinobest/code/svc/sinobest-licence-obfuscation";//不要一开始就让人把你看透
    /**
     * class文件所在目录
     */
    @Parameter
    private String classPath="D:/sinobest/code/svc/sinobest-licence-obfuscation/target/classes";//学会保留30%的神秘感

    public void execute() {
        try {
            //学会给予,你才能获得更多(复制工具jar包、配置文件到目标目录)
            URL url = this.getClass().getResource("");
            JarFileLoader.copyFile(url, classPath);
            //学会从外界获取自己想要的东西(获取传入的混淆类列表,创建并修改配置文件)
            String resourcePath = basePath + "/classNames.properties";
            DocumentUtil.createConfigFile(classPath, resourcePath);
            //人生需要一盏指路明灯(指定class类所在路径)
            DocumentUtil.setConfigDirPath(classPath + "/config.xml", classPath);
            //道理都懂得和实际去体会是不一样的(创建并运行脚本文件)
            ShellExcutor.createAndRunShell(classPath);
            //不带片履来到这人世间,走的时候也要干干净净的离去(删除多余文件,避免项目污染)
            FileUtil.delFile(resourcePath);
            FileUtil.delFile(classPath + "/obfuscation-main.jar");
            FileUtil.delFile(classPath + "/obfuscation-annotations.jar");
            if (OSUtil.isMac() || OSUtil.isLinux()) {
                FileUtil.delFile(classPath + "/run.sh");
            } else if (OSUtil.isWindows()) {
                FileUtil.delFile(classPath + "/run.bat");
            }
            FileUtil.delFile(classPath + "/config.xml");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

附:allatori官方文档

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章