使用Proguard对FAT JAR包进行代码混淆

2024-04-25 18:58

本文主要是介绍使用Proguard对FAT JAR包进行代码混淆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用Proguard对FAT JAR包进行代码混淆

  • 1. 解压jar到目标目录
  • 2. 配置混淆文件
  • 3. 通过命令行执行代码混淆
  • 4. 对混淆后的jar包重新打包
  • 5. 将重新打包的JAR再次打包成FAT Jar
  • 参考

1. 解压jar到目标目录

jar xvf original.jar

original.jar为FAT Jar。假设解压目录为 D:/proguard/original

  • toman-service-1.0.jar
  • classes

2. 配置混淆文件

建议下载ProGuard通过GUI进行配置文件设置,设置完毕后通过Save configuration…保存配置文件。配置文件保存到D:/proguard/config/proguard-for-jar

为了更好的使用ProGuard,或者一开始就知道代码需要进行混淆的情况下,最好将@Component,@Controller,@Service,@RestController,@Repository,@Resource等注解都加上名字。

在我们的系统里DAO,API,常量等相关JAR包时没有进行混淆的。各系统可以根据自己的实际情况决定对哪进行混淆。
在这里插入图片描述
配置文件示例:

# 带混淆JAR以及对应输出jar
-injars original\toman-service-1.0.jar
-outjars target\toman-service-pro-1.0.jar
-injars classes(**.class)
-outjars target\classes-pro.jar# jdk jars
-libraryjars 'C:\Program Files\Java\jre1.8.0_191\lib\rt.jar'
-libraryjars 'C:\Program Files\Java\jre1.8.0_191\lib\jce.jar'# third-part jar
-libraryjars lib\ajdv-0.1.jar
-libraryjars lib\annotations-3.0.0.jar
-libraryjars lib\ant-1.8.0.jar
-libraryjars lib\ant-launcher-1.8.0.jar
-libraryjars lib\aspectjweaver-1.8.13.jar
-libraryjars lib\btf-1.2.jar
-libraryjars lib\c3p0-0.9.5.2.jar
# 应用中无需混淆JAR
-libraryjars lib\toman-api-1.0.0-SNAPSHOT.jar-dontshrink
-dontoptimize
# 定义输出mapping文件
-printmapping toman-mapping
# 混淆后类名不区分大小写(windows下不区分大小写)
-dontusemixedcaseclassnames
# 不做混淆的包名
-keeppackagenames com.toman.product
# 以下属性不做混淆
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,Synthetic,EnclosingMethod
-dontpreverify
# 忽略警告信息
-ignorewarnings# 保留@Configuration注解的类的方法名
# keep classes and class members,  @Configuration, com.toman.product.**, <methods>
-keep @org.springframework.context.annotation.Configuration class com.toman.product.** {<methods>;
}# 保留所有@Component注解的类的@Resource注解的字段以及@Pointcut注解的方法
# keep class members only, @Component, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Component class com.toman.product.** {@javax.annotation.Resource<fields>;@org.aspectj.lang.annotation.Pointcut<methods>;
}# 保留@RestController注解的类的@Resource注解的字段
# keep class members only, @RestController, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.web.bind.annotation.RestController class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@Service注解的类的@Resource注解的字段
# keep class members only, @Service, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Service class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@Controller注解的类的@Resource注解的字段
# keep class members only, @Controller, com.toman.product.**, <fields>
-keepclassmembers @org.springframework.stereotype.Controller class com.toman.product.** {@javax.annotation.Resource<fields>;
}# 保留@ConfigurationProperties注解的类的字段名和方法名
# keep class members only, @ConfigurationProperties, com.toman.product.**, <fields><methods>
-keepclassmembers @org.springframework.boot.context.properties.ConfigurationProperties class com.toman.product.** {<fields>;<methods>;
}# 保留所有领域对象的字段名和方法名
-keepclassmembers class com.toman.product.core.domain.** {<fields>;<methods>;
}# 以下为ProGuard自带配置
# Keep - Applications. Keep all application classes, along with their 'main' methods.
-keepclasseswithmembers public class * {public static void main(java.lang.String[]);
}# 以下部分为节省空间已删除
# enumeration classes.
# Also keep - Database drivers. Keep all implementations of java.sql.Driver.
# Also keep - Swing UI L&F. Keep all extensions of javax.swing.plaf.ComponentUI,
# along with the special 'createUI' method.
# Keep - Native method names. Keep all native class/method names.
# Remove - System method calls. Remove all invocations of System
# methods without side effects whose return values are not used.
# Remove - Math method calls. Remove all invocations of Math
# methods without side effects whose return values are not used.
# Remove - Number method calls. Remove all invocations of Number
# methods without side effects whose return values are not used.
# Remove - String method calls. Remove all invocations of String
# methods without side effects whose return values are not used.
# Remove - StringBuffer method calls. Remove all invocations of StringBuffer
# methods without side effects whose return values are not used.
# Remove - StringBuilder method calls. Remove all invocations of StringBuilder
# methods without side effects whose return values are not used.

3. 通过命令行执行代码混淆

%PROGUARD_HOME%\bin\proguard.bat -include D:/proguard/config/proguard-for-ja -basedirectory D:/proguard

混淆后的代码存储到D:/proguard/target。但是混淆后生成的jar包并不能直接使用,需要重新进行打包

  • toman-service-pro-1.0.jar
  • classes-pro.jar

4. 对混淆后的jar包重新打包

对混淆后的JAR包解压

jar xvf toman-service-pro-1.0.jar
jar xvf classes-pro.jar

将解压后的class文件重新打包成JAR

jar -cvfM0 toman-service-1.0.jar ../toman-service-pro-1.0
jar -cvfM0 classes.jar ../classes-pro

5. 将重新打包的JAR再次打包成FAT Jar

jar -cvfM0 target.jar .

参考

使用proguard进行javaweb代码混淆

这篇关于使用Proguard对FAT JAR包进行代码混淆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/935523

相关文章

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

vscode保存代码时自动eslint格式化图文教程

《vscode保存代码时自动eslint格式化图文教程》:本文主要介绍vscode保存代码时自动eslint格式化的相关资料,包括打开设置文件并复制特定内容,文中通过代码介绍的非常详细,需要的朋友... 目录1、点击设置2、选择远程--->点击右上角打开设置3、会弹出settings.json文件,将以下内

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P