SuperAPI AI Directory

wasm-reverse

General & other

View repository

Updated 10-16 08:50

About this skill

wasm-reverse

For $ARGUMENTS , perform WASM reverse engineering analysis.

Toolchain:

  • wasm2wat / wasm-objdump / wasm-decompile (WABT tool suite)
  • ghidra-mcp(decompilation, optional)
  • js-reverse-mcp(dynamic analysis of JS↔WASM interaction)

Analysis Workflow

Step 1: Extract the WASM module

方式 A: 从网页提取
  1. list_network_requests → 筛选 .wasm 文件请求
  2. 或 search_in_sources("WebAssembly") → 找到 instantiate/compile 调用
  3. save_script_source 保存 WASM 文件(如果是通过脚本加载)
  4. 或直接 curl 下载 .wasm URL

方式 B: 从 JS bundle 中提取
  某些打包工具将 WASM 编码为 Base64 嵌入 JS 中:
  1. search_in_sources("data:application/wasm") → 找到嵌入的 WASM
  2. 提取 Base64 字符串 → 解码保存为 .wasm

方式 C: 本地文件
  直接分析用户提供的 .wasm 文件

Step 2: Static analysis

2.1 Basic information

# 查看 WASM 模块信息
wasm-objdump -h module.wasm      # 段信息
wasm-objdump -x module.wasm      # 导入/导出详情
wasm-objdump -d module.wasm      # 反汇编

# 转为文本格式 (WAT)
wasm2wat module.wasm -o module.wat

# 反编译为伪 C 代码
wasm-decompile module.wasm -o module.dcmp

2.2 Key information extraction

导出函数 (exports):
  - 这些是 JS 可以直接调用的函数
  - 通常加密函数会在这里暴露
  - wasm-objdump -j Export -x module.wasm

导入函数 (imports):
  - WASM 需要宿主(JS)提供的函数
  - 通常包含内存操作、环境 API
  - 需要在 instantiate 时提供

内存 (Memory):
  - WASM 的线性内存
  - 输入/输出数据通过内存传递

字符串:
  - wasm-objdump -j Data -x module.wasm
  - 数据段中可能包含密钥、算法常量

2.3 Identify encryption algorithms

Search for characteristic constants in the WASM decompilation/disassembly:

Constant Algorithm
0x67452301, 0xEFCDAB89 MD5 initial values
0x6A09E667, 0xBB67AE85 SHA-256 initial values
0x5A827999, 0x6ED9EBA1 SHA-1 K values
0x63, 0x7C, 0x77, 0x7B AES S-Box
0x428A2F98, 0x71374491 SHA-256 K values

Step 3: Dynamic analysis (JS ↔ WASM interaction)

Use js-reverse-mcp to trace how JS calls WASM:

1. search_in_sources("WebAssembly.instantiate")
   → 找到 WASM 加载和初始化代码

2. 分析 imports 对象:
   WebAssembly.instantiate(wasmBytes, {
     env: {
       memory: new WebAssembly.Memory(...),
       __memory_base: 0,
       // ... JS 提供给 WASM 的函数
     }
   })

3. 在 JS 调用 WASM 导出函数处设断点:
   set_breakpoint_on_text("instance.exports.encrypt")
   → 观察输入参数如何写入 WASM 内存
   → 观察输出如何从 WASM 内存读取

4. trace_function 追踪导出函数调用:
   trace_function(scriptId, lineNumber)
   → 监控每次调用的参数

3.1 Memory interaction analysis

JS and WASM communicate through linear memory:

// 典型模式: JS 写入输入 → 调用 WASM → 读取输出

// 写入字符串到 WASM 内存
var inputPtr = instance.exports.malloc(input.length);
var mem = new Uint8Array(instance.exports.memory.buffer);
for (var i = 0; i < input.length; i++) {
    mem[inputPtr + i] = input.charCodeAt(i);
}

// 调用 WASM 加密函数
var outputPtr = instance.exports.encrypt(inputPtr, input.length);

// 读取输出
var output = '';
for (var i = 0; outputPtr + i < mem.length && mem[outputPtr + i] !== 0; i++) {
    output += String.fromCharCode(mem[outputPtr + i]);
}

At the breakpointuse evaluate_script to inspect:

// 查看 WASM 内存中的数据
new Uint8Array(instance.exports.memory.buffer, ptr, length)

Step 4: Algorithm recovery or direct invocation

Option A: Recover the algorithm (suitable for standard encryption)

If a standard algorithm (MD5/SHA/AES) is identified, implement it directly with the Python standard library:

# 如果是 SHA-256
import hashlib
result = hashlib.sha256(data).hexdigest()

Option B: Directly invoke WASM (recommended, suitable for complex/custom algorithms)

Python (wasmer)
from wasmer import engine, Store, Module, Instance, ImportObject, Function, Memory, MemoryType
import ctypes

store = Store()
module = Module(store, open("module.wasm", "rb").read())

# 构建 imports(如果 WASM 需要)
import_object = ImportObject()
# import_object.register("env", {"log": Function(store, log_func)})

instance = Instance(module, import_object)
memory = instance.exports.memory

# 写入输入
input_data = b"data_to_encrypt"
ptr = instance.exports.malloc(len(input_data))
mem_view = memory.uint8_view(offset=ptr)
for i, b in enumerate(input_data):
    mem_view[i] = b

# 调用加密
result_ptr = instance.exports.encrypt(ptr, len(input_data))

# 读取输出
output = bytes(memory.uint8_view(offset=result_ptr)[:32])
print(output.hex())
Node.js
const fs = require('fs');
const wasmBuffer = fs.readFileSync('module.wasm');

(async () => {
    const module = await WebAssembly.compile(wasmBuffer);
    const instance = await WebAssembly.instantiate(module, {
        env: {
            memory: new WebAssembly.Memory({ initial: 256 }),
            // ... 其他 imports
        }
    });

    const { encrypt, memory, malloc } = instance.exports;
    
    const input = new TextEncoder().encode("data_to_encrypt");
    const ptr = malloc(input.length);
    new Uint8Array(memory.buffer, ptr, input.length).set(input);
    
    const resultPtr = encrypt(ptr, input.length);
    const result = new Uint8Array(memory.buffer, resultPtr, 32);
    console.log(Buffer.from(result).toString('hex'));
})();

Anti-patterns

  • Do not ignore imports — many WASM modules depend on host-provided functions (such as memory allocation and environment APIs), and direct invocation will fail.
  • Do not assume the memory layout — different compilers (Emscripten/Rust/Go) manage memory differently, and the JS glue code must be analyzed to understand the conventions.
  • Do not directly modify the WASM bytecode — unless you understand the WASM binary format very well, otherwise modify it in WAT text format and compile it back.
  • Prefer direct invocation over recovery — recovering custom algorithms in WASM is costly; directly loading the WASM module with Python/Node.js and invoking it is more efficient.

Completion Criteria

WASM 分析结果:
- 文件:module.wasm (XX KB)
- 导出函数:encrypt(ptr, len) → ptr
- 算法类型:[标准算法 | 自定义算法]
- 调用方案:[Python 还原 | Python/Node.js 直接调用 WASM]
- 验证:相同输入 → 相同输出

References

File When to read
references/wasm-tools.md WABT tool suite installation and detailed usage
references/wasm-js-bridge.md Detailed explanation of JS ↔ WASM communication patterns and memory management