P1sec/hermes-dec

GitHub: P1sec/hermes-dec

针对 React Native Hermes 字节码的逆向工程工具,支持将 HBC 格式反汇编和反编译为可读的伪代码。

Stars: 943 | Forks: 82

`hermes-dec` 是一款逆向工程工具,可用于对编译为 Hermes VM 字节码(HBC)格式的 React Native 文件进行反汇编和反编译。 关于其用途的更详细介绍,请参阅我们的[介绍博文](https://www.p1sec.com/blog/releasing-hermes-dec-an-open-source-disassembler-and-decompiler-for-the-react-native-hermes-bytecode)。 ## 背景 Facebook 于 [2019 年 7 月 12 日](https://engineering.fb.com/2019/07/12/android/hermes/)发布了针对 React Native 的 Hermes VM。Hermes 的初始版本为 0.1.0([字节码版本 59](https://github.com/facebook/hermes/blob/v0.1.0/include/hermes/BCGen/HBC/BytecodeFileFormat.h#L32)),Github 标签 0.0.1 到 0.0.3 对应公开发布前的版本。 然而,正如[此处](https://reactnative.dev/blog/2022/07/08/hermes-as-the-default)所解释的,直到 [React Native 0.70](https://reactnative.dev/blog/2022/09/05/version-070#hermes-as-default-engine)(2022 年 9 月 5 日发布),Hermes 才成为 Android 上 React Native 的默认编译目标。 在此之前,即自 React Native 0.60.2 起,Hermes VM 一直是一个[可选功能](https://reactnative.dev/docs/hermes#enabling-hermes)。自 [React Native 0.64 版本](https://reactnative.dev/blog/2021/03/12/version-0.64)(2021 年 3 月 12 日发布)起,开始支持 iOS 上的 Hermes。 **众多重要的移动应用程序都在使用 React Native**,这一点在 [React Native 展示页面](https://reactnative.dev/showcase)上有所展示。 关于 Hermes VM 的各类技术文档已作为 [开源](https://github.com/facebook/hermes/)项目(基于 BSD 许可证)发布,其中包括 [Markdown 格式的各种设计文档](https://github.com/facebook/hermes/tree/main/doc),这些文档既可以在 Github 上阅读,也可以在 [Facebook 的 React 文档网站](https://hermesengine.dev/docs/vm/)上找到。 在以 .APK 格式发布的 Android 应用程序中,Hermes 字节码(HBC)文件通常位于: ``` $ file assets/index.android.bundle assets/index.android.bundle: Hermes JavaScript bytecode, version 84 ``` 在非基于 Hermes 的 React Native 设置中,此文件通常包含压缩/Webpacked 后的 JavaScript 代码。 ## 安装 该应用程序目前仅依赖 Python 3.x 标准库。 您可以通过以下命令安装该工具: ``` sudo snap install --classic uv uv tool install hermes-dec # 或使用 pipx: pipx install hermes-dec ``` ## 用法 如果您没有使用上述命令在系统范围内安装该工具,也可以使用以下命令下载源代码,并在根目录下找到相同的实用程序: ``` $ git clone git@github.com:P1sec/hermes-dec.git $ cd hermes-dec/ ``` 对于 Android 应用程序,您应该使用您喜欢的解压工具从 .APK 文件中提取 Hermes bundle 文件(.APK 文件是一个重命名的 .ZIP 归档文件): ``` $ 7z x my_application.apk $ cd my_application ``` 然后检查 `assets/index.android.bundle` React Native bundle 文件的类型,在基于 React Native 的应用程序中,该文件应该是纯文本压缩 JavaScript bundle 文件或 Hermes 字节码文件: ``` $ file assets/index.android.bundle assets/index.android.bundle: Hermes JavaScript bytecode, version 84 ``` 如果相关文件确实是 Hermes JavaScript 字节码文件,则可以使用以下实用程序解码其大部分文件头(其输出可能会随时间变化而不稳定): ``` hbc-file-parser assets/index.android.bundle ``` 然后,您可以使用以下命令将 React Native 字节码文件的内容反汇编到 `/tmp/my_output_file.hasm` 输出文件中(省略第二个参数可将反汇编内容发送到标准输出): ``` hbc-disassembler assets/index.android.bundle /tmp/my_output_file.hasm ``` 并使用以下命令执行反编译至伪代码的操作(由于未还原循环/条件结构,因此还不是有效的 JavaScript): ``` hbc-decompiler assets/index.android.bundle /tmp/my_output_file.js ``` ## 更多文档 您可以在[此处](https://p1sec.github.io/hermes-dec/opcodes_table.html)找到 Hermes VM 操作码的自动生成文档。 这是理解生成的汇编代码的有用工具。
标签:Android安全, APK分析, CTF工具, DAST, HBC, Hermes, JavaScript引擎, React Native, VPS部署, 二进制发布, 云安全监控, 云资产清单, 反汇编器, 反编译器, 字节码, 开源工具, 恶意软件分析, 漏洞搜索, 目录枚举, 移动安全, 软件分析, 逆向工程, 静态分析