Spring Boot使用Allatori代碼混淆

Allatori混淆技術介紹

Allatori是一個Java 混淆器,它屬於第二代混淆器,所以它可以全方位地保護你的知識產權。 Allatori具備如下幾種保護方式:命名混淆,流混淆,調試信息混淆,字符串混淆,以及水印技術。對於教育和非商業項目來講這個混淆器是免費的。支持war和jar文件格式,而且容許對須要混淆代碼的應用程序添加有效日期。 有項目須要對代碼進行保護,比較初級的方案就是對代碼進行混淆,打包以後的文件進行反編譯後,就能夠看到效果。此外,使用Allatori打的包體積也會小一點。java

工程介紹

這裏寫圖片描述

一個很普通的maven工程,不一樣的是在根目錄下加入Allatori的jar包。git

下面咱們來看看pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lovnx</groupId>
	<artifactId>confusion</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<!-- Allatori plugin start -->
			<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>${basedir}/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>${basedir}/lib/allatori.jar</argument>
						<argument>${basedir}/target/allatori.xml</argument>
					</arguments>
				</configuration>
			</plugin>
			<!-- Allatori plugin end -->
		</plugins>
	</build>

	<dependencies>
		<!-- Test Begin -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- Test End -->
		<!-- springboot啓動 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
	</parent>
</project>
複製代碼

使用maven打包插件,Spring Boot構建的工程,Allatori的配置在上面也有說明,Allatori配置裏面比較重要的是:github

<argument>${basedir}/lib/allatori.jar</argument>
		<argument>${basedir}/target/allatori.xml</argument>
複製代碼

指定Allatori的allatori.jar文件路徑,若是你的工程是一個pom工程,能夠在父工程中放lib目錄,而後子工程只須要:web

<argument>../lib/allatori.jar</argument>
複製代碼

便可。spring

allatori.xml這個文件也很重要,看看其中的內容:
<config>
    <input>
        <jar in="confusion-0.0.1-SNAPSHOT.jar" out="confusion-0.0.1-SNAPSHOT-obfuscated.jar"/>
    </input>

    <keep-names>
        <class access="protected+">
            <field access="protected+"/>
            <method access="protected+"/>
        </class>
    </keep-names>

    <property name="log-file" value="log.xml"/>
</config>
複製代碼

便是對Allatori混淆器的具體配置,這裏能夠配置不少信息,不少種策略,也能夠指定哪些類不被混淆,具體的各類方式能夠在在文末附件裏面的文檔獲得。 這裏須要說明的是:apache

<input>
        <jar in="confusion-0.0.1-SNAPSHOT.jar" out="confusion-0.0.1-SNAPSHOT-obfuscated.jar"/>
 </input>
複製代碼

confusion-0.0.1-SNAPSHOT.jar這個是打包後的未被混淆的包,而confusion-0.0.1-SNAPSHOT-obfuscated.jar是混淆後的包,這個是咱們須要的。springboot

打包步驟

一、clean maven工程。bash

二、將resources下面的allatori.xml文件複製到target目錄下面。app

三、install maven工程,看到以下信息後表示成功:maven

################################################
# #
# ## # # ## ### ### ## ### #
# # # # # # # # # # # # # #
# ### # # ### # # # ## # #
# # # ### ### # # # ### # # ### #
# #
# DEMO VERSION! #
# NOT FOR COMMERCIAL USE! #
# #
# Demo version adds System.out's #
# and gives 'ALLATORI_DEMO' name #
# to some fields and methods. #
# #
# #
# Obfuscation by Allatori Obfuscator v6.4 DEMO #
# #
# http://www.allatori.com #
# #
################################################
複製代碼

四、成功後的工程:

這裏寫圖片描述

箭頭所指處便是咱們須要的包,此包代碼已被混淆。

效果查看

這裏使用反編譯工具對混淆後的包進行查看,我用的是jd-gui這個軟件,小巧實用。

TestApplication.java混淆前:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication {
	
	public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
	}
}
複製代碼

TestApplication.java混淆後:

import java.io.PrintStream;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestApplication
{
  public static String ALLATORIxDEMO(String a)
  {
    int tmp4_3 = 4;
    int tmp7_6 = 1;
    int tmp21_18 = a.length();
    int tmp25_24 = 1;
    tmp25_24;
    int j;
    int ? = tmp25_24;
    int k = tmp21_18;
    int tmp35_31 = (j = new char[tmp21_18] - 1);
    tmp35_31;
    int i = 5 << 4 ^ (0x2 ^ 0x5);
    (tmp4_3 << tmp4_3 ^ tmp7_6 << tmp7_6);
    if (tmp35_31 >= 0)
    {
      int tmp45_44 = j;
      j--;
      ?[tmp45_44] = ((char)(a.charAt(tmp45_44) ^ i));
      int tmp66_63 = (j--);
      ?[tmp66_63] = ((char)(a.charAt(tmp66_63) ^ k));
    }
    return new String(?);
  }

  public static void main(String[] a)
  {
    System.out.println("\n################################################\n# #\n# ## # # ## ### ### ## ### #\n# # # # # # # # # # # # # #\n# ### # # ### # # # ## # #\n# # # ### ### # # # ### # # ### #\n# #\n# Obfuscation by Allatori Obfuscator v6.4 DEMO #\n# #\n# http://www.allatori.com #\n# #\n################################################\n"); SpringApplication.run(TestApplication.class, a);
  }
}
複製代碼

TestController.java混淆前:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@GetMapping("/test")
	public String test(){
		return "88888888888888888";
	}
}
複製代碼

TestController.java混淆後:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController
{
  @GetMapping({"/test"})
  public String test()
  {
    return ALLATORIxDEMO("*]*]*]*]*]*]*]*]*");
  }

  public static String ALLATORIxDEMO(String a)
  {
    int tmp27_24 = a.length();
    int tmp31_30 = 1;
    tmp31_30;
    int j;
    int ? = tmp31_30;
    int k = tmp27_24;
    int tmp41_37 = (j = new char[tmp27_24] - 1);
    tmp41_37;
    int i = (0x3 ^ 0x5) << 4 ^ 0x5;
    (2 << 3 ^ 0x2);
    if (tmp41_37 >= 0)
    {
      int tmp51_50 = j;
      j--;
      ?[tmp51_50] = ((char)(a.charAt(tmp51_50) ^ i));
      int tmp72_69 = (j--);
      ?[tmp72_69] = ((char)(a.charAt(tmp72_69) ^ k));
    }
    return new String(?);
  }
}
複製代碼

哈哈哈,怎麼樣,是否是看不懂?而且混淆包照常運行,沒有任何問題。

-------》github 源碼與文檔地址《-------

相關文章
相關標籤/搜索