裴大头-秦可爱

裴大头-秦可爱

使用Allatori混淆代码

发表于 2025-02-08
裴大头
阅读量 24
更新于 2025-02-08

使用Allatori混淆代码

在软件开发的世界中,保护你的知识产权和源代码是至关重要的。尤其是当你发布Java应用程序时,确保你的代码难以被反编译或理解显得尤为重要。这正是代码混淆工具如Allatori发挥作用的地方。本文将介绍如何使用Allatori来混淆你的Java代码,并提供一些实用的配置示例。

什么是Allatori?

Allatori是一个强大的Java混淆器,它能够通过重命名类、方法和变量名等方式来保护你的Java应用程序不被轻易理解和反编译。此外,Allatori还提供了诸如字符串加密、控制流混淆等高级功能,进一步增强了代码的安全性。

官方文档

Allatori文档

开始之前

下载Allatori的Demo => Demo下载

使用Allatori(本文才用的是Maven方式使用)

image.png

在自己的项目中引入

image.png 如上图,在项目中引入allatori.jar和allatori.xml

pom.xml配置

<plugins>
<!-- 正在将Allatori配置文件复制到“target”目录。目标文件将被过滤(配置文件中使用的Maven属性将被解析) -->
    <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>
    <!-- 运行 Allator -->
    <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}/allatori/allatori.jar</argument>
                <argument>${basedir}/target/allatori.xml</argument>
            </arguments>
        </configuration>
    </plugin>
    <!-- 替换jar(混淆后会有原始jar和混淆jar,如果需要直接deploy可以使用该插件替换jar包直接将混淆后的jar部署) -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
            <execution>
                <id>rename-encrypted-jar</id>
                <phase>package</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <tasks>
                        <move file="${project.build.directory}/${project.artifactId}-${project.version}-obfuscated.jar" tofile="${project.build.directory}/${project.artifactId}-${project.version}.jar" overwrite="true"/>
                    </tasks>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
xml

allatori.xml配置

基本配置

一个基本的Allatori配置文件看起来如下所示:

<config>
    <input>
        <jar in="your-app.jar" out="your-app-obfuscated.jar"/>
    </input>

    <keep-names>
        <!-- 这里可以指定你不希望被混淆的类、字段和方法 -->
    </keep-names>
</config>
xml

在这个例子中,in属性指定了输入的JAR文件路径,而out属性则定义了输出(即混淆后的)JAR文件路径。

保持特定类不被混淆

有时候,你需要保持某些类或包下的所有类不被混淆。例如,如果你有一个API接口或者需要保留反射机制使用的类名,你可以这样做:

<keep-names>
    <class template="class com/your/package/**">
        <field template="*"/>
        <method template="*(**)"/>
    </class>
</keep-names>
xml

这里,com/your/package/**表示匹配该目录及其所有子目录下的所有类。**通配符用于匹配任意层级的子目录。

忽略的包或类

<!-- 忽略的包或类,这些文件将不被混淆 -->
    <ignore-classes>
        <!-- 不要混淆主类 -->
        <class template="class com.navi.akkaload.AkkaloadApplication" />
        <!-- 不要混淆第三方的代码,否则会运行jar包会报错java.lang.NoClassDefFoundError -->
        <class template="class org.dom4j.*" />
        <class template="class akka.actor.*" />
        <class template="class *alibaba*" />
        <class template="class *org*" />
        <class template="class *rabbitmq*" />
        <class template="class *springframework*" />
        <class template="class *lombok*" />
    </ignore-classes>
 
    <!-- 到期时间(到期后无法启动jar) 格式:yyyy/mm/dd-->
    <!--<expiry date="2021/04/03" string="SERVICE EXPIRED!"/>-->
    <!-- 随机命名混淆字符-->
    <!--<property name="random-seed" value="abcdef ghnljk svi"/>-->

高级选项

Allatori还支持更多高级功能,比如字符串加密、水印添加等。这些功能可以通过在配置文件中添加相应的标签来启用:

<property name="string-encryption" value="true"/>
<property name="watermark" value="Your Watermark Text"/>
xml

keep-names和ignore-classes区别

在Allatori Java混淆器中,keep-namesignore-classes这两个配置项虽然都用于指定不应被混淆的类、方法或字段,但它们的作用和使用场景有所不同。

keep-names

keep-names配置主要用于指定哪些类、方法或字段的名字应该保持原样,不进行混淆。这意味着这些元素将保留其原始名称,从而便于调试或者保证某些依赖于反射机制的功能正常工作。例如:

<keep-names>
    <class template="class com/example/MyClass">
        <field template="*"/>
        <method template="*(**)"/>
    </class>
</keep-names>
xml

在这个例子中,com.example.MyClass中的所有字段和方法都不会被混淆,保留其原始名称。

ignore-classes

相比之下,ignore-classes则用于完全排除某些类或包,使其不受混淆过程的影响。这意味着不仅这些类的名称不会被改变,而且其中的方法和字段也不会被混淆。这通常用于那些你确定不应该被混淆的第三方库或是你自己代码中的一些特定部分。例如:

<ignore-classes>
    <class template="class com/example/external/**"/>
</ignore-classes>
xml

这个配置会使得com.example.external包及其所有子包下的类都不参与混淆过程。

总结
  • keep-names:主要用于指定需要保留原始名称的类、方法或字段。它允许你精细地控制哪些具体的部分不需要被混淆,同时其他未指定的部分仍可以按照默认规则进行混淆。
  • ignore-classes:则是更广泛地排除整个类或包,使其完全避开混淆流程。这对于不想对某些外部库或特定模块进行任何修改的情况非常有用。

理解这两者的区别有助于更好地定制你的混淆策略,确保既能有效保护代码安全,又能维持必要的功能完整性。

结论

通过使用Allatori进行代码混淆,你可以有效地提高你的Java应用程序的安全性,减少代码被反编译的风险。虽然代码混淆不能完全阻止有决心的攻击者,但它确实增加了破解的难度和时间成本,为你的软件提供了一层额外的保护。 记住,正确的配置是成功的关键。花时间了解Allatori的各种选项,并根据自己的具体需求调整配置,将帮助你最大限度地利用这个强大的工具。


补充自动化部署

pom.xml中添加maven私仓地址

<!-- 发布jar包时需要有的maven私服配置 -->
<distributionManagement>
	<!--id要和setting文件中server节点中配置的一样-->
	<repository>
		<id>maven-releases</id>
		<name>maven-releases</name>
		<url>releases-私仓地址</url>
	</repository>
	<!--id要和setting文件中server节点中配置的一样-->
	<snapshotRepository>
		<id>maven-snapshots</id>
		<name>maven-snapshots-私仓地址</name>
		<url>snapshots</url>
	</snapshotRepository>
</distributionManagement>
xml

maven配置文件中配置私仓信息

<servers>
	<server>
		<id>releases</id>
		<username>用户名</username>
		<password>密码</password>
	</server>
	<server>
		<id>snapshots</id>
		<username>用户名</username>
		<password>密码</password>
	</server>
</servers>
xml

执行deploy指令即可

评论
来发一针见血的评论吧!
表情

快来发表评论吧~

推荐文章
  • JavaScript 的事件循环机制

    1点赞1评论

  • Element UI 级联选择器 el-cascader 实现懒加载和搜索功能

    1点赞0评论

  • Java 23种设计模式——适配器模式(Adapter)

    1点赞0评论

  • Vue项目代码规范

    1点赞1评论

  • Java 23种设计模式——组合模式(Composite Pattern)

    1点赞0评论

Crafted with by Pei你看雪

小破站居然运行了 1161 天访客 27545

© 2023 Pei你看雪鲁ICP备19037910号-2