基於Allatori的Java代碼混淆 maven插件工具
項目地址:全球最大同性交友網站
Java代碼很容易進行反編譯,如果有人想要研究你的項目,隨便反編譯一下就看得清清楚楚了,特別像我們公司的測試人員,經常會反編譯我們的項目,然後找缺陷(這操作沒誰了),另外就是公司的項目對外發布時希望能夠進行加密,所以這時候就需要對源碼進行混淆,增加閱讀成本,當然只要花時間,總還是能看懂的,這個是java底層限制了,不可能完全加密。
不過一般人看到這種代碼,應該會放棄了!
接下來就具體說說這個插件吧!
這個插件是基於Allatori來實現的。
Allatori本身是支持在maven中集成使用的,但是它只能指定哪些文件不混淆,然後會對剩餘文件全部混淆,在實際業務中有時候可能只是想對部分涉密文件進行混淆,所以封裝了這個工具。
另外剛開始接觸這個Allatori,可能對它的配置文件比較陌生,也容易踩坑,所以我在這個插件中直接定義了一些通用配置,無需你再改配置,只需要在項目根目錄下創建一個classNames.properties文件,在其中指定需要混淆的類全類名即可。
使用方式:
- mvn install 此項目到本地倉庫,然後在想要混淆的項目的pom文件中引入此插件
- 在需要混淆的項目的pom文件中導入依賴即可使用。build -> plugins中導入依賴即可使用
- 注意事項:如果需要在正式環境中使用,請將此maven插件項目放到私服遠程maven倉庫中
- 在對項目進行打包時此插件便會運行
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();
}
}
}