GKLBB

当你经历了暴风雨,你也就成为了暴风雨

导航

软件研发 --- 文件格式分析 之 ASAR

 ASAR文件本质就是归档文件,类似TAR命令执行后的结果

核心概念速览

text
app.asar = Electron应用的"源码压缩包"
         = JS + HTML + CSS + 配置文件 + 资源
         ≠ 加密文件(默认情况下可直接解包)

一、环境准备

安装 Node.js

Bash
# 验证环境
node -v    # 需要 22.12.0+
npm -v

安装 asar 工具

Bash
# 全局安装官方工具
npm install -g @electron/asar

# 验证安装
asar --version

二、基本操作命令

查看内容(不解包)

Bash
asar list app.asar

输出示例:

text
/package.json
/main.js
/preload.js
/dist/renderer.js
/dist/index.html
/assets/logo.png
/node_modules/...

完整解包

Bash
# 语法
asar extract <asar文件> <输出目录>

# 示例
asar extract app.asar ./app_src

# Windows 示例
asar extract D:\app\resources\app.asar D:\app\app_src

提取单个文件

Bash
# 只提取 package.json
asar extract-file app.asar package.json

# 提取特定路径的文件
asar extract-file app.asar dist/main.js

三、解包后的分析流程

第一步:看 package.json

Bash
cat app_src/package.json

重点关注字段:

JSON
{
  "name": "应用名称",
  "version": "版本号",
  "main": "dist/main.js",      ← Electron主进程入口
  "scripts": { ... },
  "dependencies": { ... }
}

第二步:找主进程文件

Bash
# 根据 package.json 中 main 字段找到主进程
# 例如 "main": "dist/main.js"
cat app_src/dist/main.js

# 或者搜索 BrowserWindow(Electron主进程标志)
grep -r "BrowserWindow" app_src/ --include="*.js" -l

第三步:梳理文件结构

Bash
# Linux/macOS
find app_src -name "*.js" | head -30
find app_src -name "preload.js"
find app_src -name "index.html"

# 查看目录树
tree app_src -L 3

四、典型目录结构解读

text
app_src/
├─ package.json          ← 看这里找入口
├─ main.js               ← 主进程(Node.js环境)
├─ preload.js            ← 预加载脚本(桥接层)
├─ dist/                 ← 前端打包产物(常见)
│  ├─ main.js            ← 打包后的主进程
│  ├─ renderer.js        ← 打包后的渲染进程
│  └─ index.html
├─ src/                  ← 源代码(有时存在)
│  ├─ main/              ← 主进程源码
│  └─ renderer/          ← 渲染进程源码
├─ assets/               ← 静态资源
└─ node_modules/         ← 依赖包

五、app.asar.unpacked 说明

text
resources/
├─ app.asar              ← JS/HTML/CSS/配置
└─ app.asar.unpacked/    ← 原生模块/二进制文件
   └─ node_modules/
      └─ some-native-module/
         ├─ *.node       ← Node原生扩展
         ├─ *.so         ← Linux动态库
         └─ *.dll        ← Windows动态库

两个都要看,业务逻辑在 app.asar,原生能力在 app.asar.unpacked


六、常见问题

JS 代码被压缩混淆?

Bash
# 安装格式化工具
npm install -g js-beautify

# 格式化压缩的JS
js-beautify main.js -o main_formatted.js

遇到 Source Map?

text
# 如果存在 .map 文件,可以还原源码结构
find app_src -name "*.map"

asar 命令找不到?

Bash
# 检查 npm 全局路径
npm root -g

# 或者用 npx 直接运行
npx @electron/asar list app.asar
npx @electron/asar extract app.asar ./output

七、分析路径总结

text
app.asar
    ↓ 解包
package.json → 找 "main" 字段
主进程文件 (main.js)
  - 窗口创建逻辑
  - IPC 通信注册
  - 原生API调用
preload.js
  - 暴露给渲染进程的API
渲染进程 (renderer/index.html)
  - 界面逻辑
  - Vue/React 组件
  - 业务代码

posted on 2026-09-02 11:25  GKLBB  阅读(22)  评论(0)    收藏  举报