java混淆代码
1.添加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>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>allatori/lib/allatori.jar</argument> <argument>${basedir}/target/allatori.xml</argument> </arguments> </configuration> </plugin>
2.添加配置文件与依赖包
其中allatori.xml
<config> <input> <jar in="原始包.jar" out="原始包-obfuscated.jar"/> </input> <keep-names> <class access="protected+"> <field access="private+"/> <method access="protected+"/> </class> <!-- 所有方法名保持不变,参数也不变 --> <method template="*(**)" parameters="keep"/> </keep-names> <property name="log-file" value="log.xml"/> <!-- <property name="string-encryption" value="enable" apply2class="class com.linkingthing"/> --> <!-- 接口形参名保持不变 --> <!-- <property name="local-variables-naming" value="keep-parameters"/> --> <ignore-classes> <!-- 配置启动类不被混淆 保证springBoot可以正常启动 --> <class template="class *Application*"/> <class template="class *springframework*"/> <class template="class com.xx.domain.*"/> <class template="class org.apache.*"/> <class template="class *mybatisplus.*"/> <!--防止mybatis出现没有@Param取不到参数的情况--> <class template="class *mapper.*"/> </ignore-classes> <!--这个是你项目打包好后的一个lib包你要引入进来不然会引起找不到类然后导致代码混淆较弱--> <classpath> <jar name="lib/*.jar"/> </classpath> </config>jar下载地址: https://allatori.com/downloads/Allatori-8.5-Demo.zip
3.操作步骤
mvn clean
mvn dependency:copy-dependencies -DoutputDirectory=target/lib
mvn package -X -Dmaven.test.skip=true
mv target/原始.jar target/原始-bak.jar
mv target/原始-obfuscated.jar target/原始.jar
#验证可用性
java -jar target/原始.jar
调用mybatis的方法
4.优化
第3步是需要手工操作的,怎么实现自动化呢?主要是jar名需要带上maven里配置的版本号,并且最好能一句maven package就能完成,那么把这些命令弄成maven配置文件吧。
4.1配置maven在package阶段生成依赖的lib文件夹
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <type>jar</type> <includeTypes>jar</includeTypes> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin>
4.2 配置改jar文件名。因为allatori.xml里面不能配置带变量的jar包名,所以这里把jar包名改成固定的。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copy1</id> <phase>package</phase> <configuration> <target> <move file="target/${project.build.finalName}.jar" tofile="target/webinfo.jar"></move> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin>
4.3 修改上文中的exec-maven-plugin让之前的一个步骤变为2两步骤,第2个步骤是将jar包变改回来,改成带版本号的。
pom.xml,所有的依赖:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <type>jar</type> <includeTypes>jar</includeTypes> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copy1</id> <phase>package</phase> <configuration> <target> <move file="target/${project.build.finalName}.jar" tofile="target/webinfo.jar"></move> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> <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>allatori</directory> <includes> <include>allatori.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
全量配置4个插件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <type>jar</type> <includeTypes>jar</includeTypes> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>copy1</id> <phase>package</phase> <configuration> <target> <move file="target/${project.build.finalName}.jar" tofile="target/webinfo.jar"></move> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> <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>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> <configuration> <executable>java</executable> <arguments> <argument>-Xms128m</argument> <argument>-Xmx512m</argument> <argument>-jar</argument> <argument>allatori/lib/allatori.jar</argument> <argument>${basedir}/target/allatori.xml</argument> </arguments> </configuration> </execution> <execution> <id>run-allatori2</id> <phase>package</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>mv</executable> <arguments> <argument>${basedir}/target/webinfo-obfuscated.jar</argument> <argument>${basedir}/target/${project.build.finalName}.jar</argument> </arguments> </configuration> </execution> </executions> </plugin>
这里有个问题,混淆包名想自定义,用了mv命令,在mac没问题,windows下就报错了。
想了几个办法,
1.添加一个ant插件,混淆包名是固定的,而目标包名为
${project.build.finalName}.jar测试了,不行,因为ant插件先于exec执行,报错。网上资料一般说是按顺序执行,看来不是。
2.使用exec改成windows的move,cmd 之类的命令,试了下,各种问题。也不能同时与mac兼容,放弃。
3.使用profile,这个需要在每类(test,prd)profile内添加这4个plugin,太冗余了。没试。
4.最后成功的,不使用mv命令,替换allatori.xml中的变量为包名。只是在resource的plugin里添加变量替换如delimit下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <delimiters> <delimit>${*}</delimit> </delimiters> </configuration> <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>allatori</directory> <includes> <include>allatori.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
注意这里尝试了ant插件的copy filter,replace等标签均无效,就这样在allatori.xml定义外面知道的变量名就行了。如下
<jar in="webinfo.jar" out="${project.build.finalName}.jar" />