Guardsquare/proguard-core

GitHub: Guardsquare/proguard-core

一个功能强大的 Java 字节码处理库,提供读取、分析、修改和生成类文件的完整能力,是构建代码混淆、静态分析和字节码工具的核心基础。

Stars: 334 | Forks: 73



ProGuardCORE

一个用于解析、修改和分析 Java 类文件的库。


快速入门功能特性相关项目参与贡献开源许可


ProGuardCORE 是一个免费的库,用于读取、分析、修改和编写 Java 类文件。它是著名的压缩器、优化器和混淆器 [ProGuard](https://www.guardsquare.com/proguard)、[ProGuard 汇编器和反汇编器](https://github.com/guardsquare/proguard-assembler) 以及 [Kotlin 元数据打印工具](https://github.com/Guardsquare/kotlin-metadata-printer) 的核心。 典型应用: - 读写类文件,包括任何 Kotlin 元数据。 - 搜索指令模式。 - 创建字节码插桩工具。 - 使用抽象评估进行代码分析。 - 构建静态代码分析工具。 - 像 ProGuard 本身一样进行优化和混淆。 - ... 以及更多功能! ProGuard core 附带了简短的[手册](https://guardsquare.github.io/proguard-core/)和相当不错的 [API 文档](https://guardsquare.github.io/proguard-core/api/)。 ## ❓ 获取帮助 请使用 **问题追踪器** 来报告实际的 **Bug 🐛、崩溃** 等问题。

## 🚀 快速入门 **ProGuardCORE** 需要 Java 1.8 或更高版本。你可以从以下位置下载预构建的构件: - [Maven Central](https://search.maven.org/search?q=g:com.guardsquare)。 只需将其添加到你的项目依赖中即可,例如 Gradle: ``` dependencies { compile 'com.guardsquare:proguard-core:9.3.2' } ``` 或者 Maven: ``` com.guardsquare proguard-core 9.3.2 ``` ### 本地构建 当然,你也可以手动克隆并构建此代码库。使用 Gradle 的话,只需在项目根目录中执行 assemble 任务: ``` gradle clean assemble ``` ## ✨ 功能特性 该代码库在 [examples](examples) 目录中包含一些示例代码。结合完整的手册页面,它们非常详细地涵盖了该库的用法及其功能:

👉👉👉    手册页面    👈 👈 👈

下面我们将仅重点介绍其中的一部分,以便更好地展示其功能。 - [以编程方式创建类](#creating-classes-programmatically-manual) - [替换指令序列](#replacing-instruction-sequences-manual) - [Kotlin 元数据](#kotlin-metadata-manual) - [使用抽象评估进行代码分析](#abstract-evaluation-manual) ### 以编程方式创建类 ([手册](https://guardsquare.github.io/proguard-core/creating.html)) 使用流式 API,从头开始以编程方式创建类变得非常容易。数据结构直接对应于字节码规范,并且 ProGuardCORE 甚至可以为您预验证代码。 您可以创建带有字段、方法和指令序列的类。以下简洁的代码创建了经典的 `HelloWorld` 类: ``` ProgramClass programClass = new ClassBuilder( VersionConstants.CLASS_VERSION_1_8, AccessConstants.PUBLIC, "HelloWorld", ClassConstants.NAME_JAVA_LANG_OBJECT) .addMethod( AccessConstants.PUBLIC | AccessConstants.STATIC, "main", "([Ljava/lang/String;)V", 50, code -> code .getstatic("java/lang/System", "out", "Ljava/io/PrintStream;") .ldc("Hello, world!") .invokevirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V") .return_()) .getProgramClass(); ``` ### 替换指令序列 ([手册](https://guardsquare.github.io/proguard-core/patternmatching.html)) ProGuardCORE 拥有一个出色的指令模式匹配引擎,该引擎还可以替换匹配到的字节码指令序列。 例如,下面的代码片段定义了一个包含通配符的指令序列,并将其替换为等效的指令序列,此操作应用于类池中所有类的所有方法的所有代码属性中。 ``` Instruction[][] replacements = { ____.putstatic(X) .getstatic(X).__(), ____.dup() .putstatic(X).__() }; ... programClassPool.classesAccept( new AllMethodVisitor( new AllAttributeVisitor( new PeepholeEditor(branchTargetFinder, codeAttributeEditor, new InstructionSequenceReplacer(constants, replacements, branchTargetFinder, codeAttributeEditor))))); ``` ### Kotlin 元数据 ([手册](https://guardsquare.github.io/proguard-core/kotlin.html)) 该库使读取、写入和修改附加到 Java 类的 Kotlin 元数据变得十分容易。以下示例打印了附加到 Java 类 `Foo` 的元数据中所有 Kotlin 函数的名称: ``` programClassPool.classesAccept( new ClassNameFilter("Foo", new ReferencedKotlinMetadataVisitor( new AllFunctionsVisitor( (clazz, container, function) -> System.out.println(function.name))))); ``` ### 抽象评估 ([手册](https://guardsquare.github.io/proguard-core/analyzing.html)) ProGuardCORE 提供了多种分析代码的方法。最强大的技术之一是抽象评估(与符号执行密切相关)。它**执行代码**,但不是计算具体的值,而是计算**抽象值**。 考虑以下 `getAnswer` 方法: ``` private static int getAnswer(int a, int b) { return 2 * a + b; } ``` 下表展示了字节码指令在其偏移量处的执行情况,以及随之演变的栈和局部变量。每个值槽都有一个数值及其来源(指令偏移量),表示为 `origin:value`。 | 指令 | 栈 | v0 | v1 | |------------------|-------------------|-------|-------| | [0] iconst\_2 | [0:2] | P0:i0 | P1:i1 | | [1] iload\_0 v0 | [0:2][1:i0] | P0:i0 | P1:i1 | | [2] imul | [2:(2*i0)] | P0:i0 | P1:i1 | | [3] iload\_1 v1 | [2:(2*i0)] [3:i1] | P0:i0 | P1:i1 | | [4] iadd | [4:((2*i0)+i1)] | P0:i0 | P1:i1 | | [5] ireturn | | P0:i0 | P1:i1 | 在每一步中,ProGuardCORE 都提供对符号表达式的访问;或者在可能且被请求的情况下,提供具体的计算结果。 ## ⚙️ 相关项目 一些使用 ProGuardCORE 的项目: - [ProGuard](https://github.com/Guardsquare/proguard) - [Kotlin Metadata Printer](https://github.com/Guardsquare/kotlin-metadata-printer) - [ProGuard Assembler/Disassembler](https://github.com/Guardsquare/proguard-assembler) - [Log4Shell Detector](https://github.com/Guardsquare/log4shell-detector) - [klox compiler](https://github.com/mrjameshamilton/klox) 如果您也创建了自己的项目,请务必通过 `github _ at _ guardsquare.com` 与我们联系,我们会将其添加到列表中! ## 🤝 参与贡献 欢迎贡献、提出问题和功能请求。 如果您愿意做出贡献,请随时查看 [issues](issues) 页面和[贡献指南](blob/master/CONTRIBUTING.md)。 ## 📝 开源许可 版权所有 (c) 2002-2022 [Guardsquare NV](https://www.guardsquare.com/)。 ProGuardCORE 基于 [Apache 2 许可证](LICENSE) 发布。
标签:class文件, DNS 反向解析, JS文件枚举, Kotlin, Maven, ProGuard, Wayback Machine, 云安全监控, 代码优化, 代码修改, 代码分析, 代码精简, 代码解析, 凭证管理, 反汇编, 域名枚举, 威胁情报, 字节码, 安全专业人员, 开发者工具, 开源库, 搜索引擎爬虫, 汇编, 混淆器, 漏洞验证, 编程语言处理, 软件逆向, 静态分析