使用allatori对SpringBoot多模块代码进行混淆
阅读原文时间:2021年04月21日阅读:1

使用allatori对SpringBoot多模块代码进行混淆

参考文档

http://www.allatori.com/doc.html
https://blog.csdn.net/rickiyeat/article/details/79386000
https://blog.csdn.net/u014795242/article/details/81204574
https://github.com/Lovnx/confusion
https://www.jianshu.com/p/9df4a0bb46be

操作步骤

1、在包含Controller类的module下(以下简称A)添加jar包

jar包可以在https://github.com/Lovnx/confusion这个示例中得到

2、在A的pom.xml中添加

<build>
        <finalName>${finalName}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>你的启动类</mainClass>
                    <includeSystemScope>true</includeSystemScope>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </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>

3、在A的resources目录下添加文件allatori.xml,内容如下

<config>
    <input>
        <!-- in中是待混淆的jar包,out是混淆后的jar包,路径是相对本配置文件所在位置 -->
        <!-- 这个示例中,A.jar是工程jar包,B.jar是子模块jar包 -->
        <jar in="A.jar" out="A-obfuscated.jar"/>
        <jar in="../../B/target/B.jar" out="B.jar"/> 
    </input>

    <!-- ignore-classes中配置不被混淆的类 -->
    <ignore-classes>
        <!-- 配置了启动相关类不被混淆,保证springboot可以正常启动 -->
        <class template="class 启动类"/>

        <class template="class *springframework*"/>
        <class template="class *alibaba*"/>
        <class template="class *persistence*"/>
        <class template="class *apache*"/>

        <class template="class *model*"/>
        <class template="class *enums*"/>
        <class template="class *repository*"/>
    </ignore-classes>

    <keep-names>
        <!-- 防止部分类、方法、变量找不到名称而报错 -->
        <!-- 所有方法名称不变,parameters="keep"表示方法参数名也不变 -->
        <method template="*(**)" parameters="keep"/>
        <!-- com.a.b.c中的类以及其子包中的类的名称不变 -->
        <class template="class com.a.b.c.*"/>
    </keep-names>

    <!-- 混淆前后的映射关系保存在log.xml中 -->
    <property name="log-file" value="log.xml"/>
</config>

更多配置请参考官方文档:http://www.allatori.com/doc.html

4、执行mvn clean,然后将A的resources目录下的文件allatori.xml复制到A的target目录下,再执行mvn install,执行成功后A的target目录如下

5、手动替换lib中包含的子模块的jar包



压缩软件我用的是Winrar,这里的压缩方式选择的是存储

至此,混淆完成

6、使用jd-gui查看混淆后的jar包,已经被混淆成功

7、运行一下混淆后的jar包,可以正常运行

java -jar A-obfuscated.jar

以上是根据自己的项目和网上的博客以及官方文档一步步摸索出来的,如有错误,请不吝指正!

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章