标签: #WebAssembly #ReverseEngineering #Security #Wasm #GameHacking #CTF
🕵️♂️ 前言:Wasm 不是加密,只是二进制
WebAssembly 是一种基于堆栈虚拟机的二进制指令格式。它类似于汇编语言,但比 x86 汇编更抽象。
浏览器加载 .wasm 文件,编译为机器码运行。
逆向 Wasm 的两种核心思路:
Wasm 加载与逆向流程 (Mermaid):
#mermaid-svg-QLvWvwNIqhkqDb5Z{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QLvWvwNIqhkqDb5Z .error-icon{fill:#552222;}#mermaid-svg-QLvWvwNIqhkqDb5Z .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QLvWvwNIqhkqDb5Z .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .marker.cross{stroke:#333333;}#mermaid-svg-QLvWvwNIqhkqDb5Z svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QLvWvwNIqhkqDb5Z p{margin:0;}#mermaid-svg-QLvWvwNIqhkqDb5Z .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster-label text{fill:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster-label span{color:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster-label span p{background-color:transparent;}#mermaid-svg-QLvWvwNIqhkqDb5Z .label text,#mermaid-svg-QLvWvwNIqhkqDb5Z span{fill:#333;color:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .node rect,#mermaid-svg-QLvWvwNIqhkqDb5Z .node circle,#mermaid-svg-QLvWvwNIqhkqDb5Z .node ellipse,#mermaid-svg-QLvWvwNIqhkqDb5Z .node polygon,#mermaid-svg-QLvWvwNIqhkqDb5Z .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .rough-node .label text,#mermaid-svg-QLvWvwNIqhkqDb5Z .node .label text,#mermaid-svg-QLvWvwNIqhkqDb5Z .image-shape .label,#mermaid-svg-QLvWvwNIqhkqDb5Z .icon-shape .label{text-anchor:middle;}#mermaid-svg-QLvWvwNIqhkqDb5Z .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .rough-node .label,#mermaid-svg-QLvWvwNIqhkqDb5Z .node .label,#mermaid-svg-QLvWvwNIqhkqDb5Z .image-shape .label,#mermaid-svg-QLvWvwNIqhkqDb5Z .icon-shape .label{text-align:center;}#mermaid-svg-QLvWvwNIqhkqDb5Z .node.clickable{cursor:pointer;}#mermaid-svg-QLvWvwNIqhkqDb5Z .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .arrowheadPath{fill:#333333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QLvWvwNIqhkqDb5Z .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QLvWvwNIqhkqDb5Z .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QLvWvwNIqhkqDb5Z .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster text{fill:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z .cluster span{color:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QLvWvwNIqhkqDb5Z .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QLvWvwNIqhkqDb5Z rect.text{fill:none;stroke-width:0;}#mermaid-svg-QLvWvwNIqhkqDb5Z .icon-shape,#mermaid-svg-QLvWvwNIqhkqDb5Z .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QLvWvwNIqhkqDb5Z .icon-shape p,#mermaid-svg-QLvWvwNIqhkqDb5Z .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QLvWvwNIqhkqDb5Z .icon-shape rect,#mermaid-svg-QLvWvwNIqhkqDb5Z .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QLvWvwNIqhkqDb5Z .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QLvWvwNIqhkqDb5Z .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QLvWvwNIqhkqDb5Z :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
逆向攻击路径
Wasm 运行环境
1. 下载 game.wasm
解码
Imports (函数/内存)
读写
工具: wasm2wat
工具: Console
服务器
浏览器
Wasm 二进制
Wasm 模块
JS 胶水代码
实例化对象 (Instance)
线性内存 (ArrayBuffer)
可读汇编代码 (.wat)
直接修改数值
🛠️ 一、 兵器库:逆向工具准备
在开始之前,你需要准备以下工具:
- wasm2wat: 将二进制转为人类可读的文本格式(S-表达式)。
- wat2wasm: 将修改后的文本重新编译为二进制。
🔬 二、 实战第一步:捕获与反编译
假设我们正在玩一个网页小游戏,每次点击按钮,金币 +1。我们要把它改成 +1000。
1. 获取 Wasm 文件
打开 Chrome DevTools -> Network 面板,刷新页面,过滤 .wasm。找到 game.wasm 并下载。
2. 转化为可读代码 (WAT)
使用 wasm2wat 工具:
wasm2wat game.wasm -o game.wat
打开 game.wat,你会看到类似这样的代码:
(module
(import "env" "memory" (memory $0 1))
(func $add_gold (param $p0 i32) (result i32)
(local $l0 i32)
local.get $p0 ;; 获取参数(当前金币)
i32.const 1 ;; 加载常量 1
i32.add ;; 执行加法
local.set $l0 ;; 存入局部变量
local.get $l0 ;; 返回结果
)
(export "add_gold" (func $add_gold))
)
注:真实环境中的函数名通常被 stripped 掉了,只显示 func $f12,你需要根据上下文推断。
💉 三、 实战第二步:内存篡改 (Memory Hacking)
Wasm 的内存模型非常简单:它就是一个巨大的、线性的 JavaScript ArrayBuffer。
这意味着,JS 可以随意读写 Wasm 的内存!这是最简单的破解方式。
1. 定位内存对象
在 Chrome Console 中,寻找 Wasm 的实例对象。通常在全局变量或者 imports 对象中。
// 假设游戏把实例挂载到了 window.gameInstance
const memBuffer = window.gameInstance.exports.memory.buffer;
const memView = new Int32Array(memBuffer);
2. 搜索数值 (CE 搜内存法)
就像使用 Cheat Engine 一样:
3. 修改数值
// 直接把金币改成 999999
memView[1024] = 999999;
页面上的金币瞬间暴涨。
🔨 四、 实战第三步:二进制补丁 (Binary Patching)
如果金币逻辑没有暴露在全局内存中,或者是纯栈操作,我们就需要修改代码本身(Patch)。
1. 修改 WAT 代码
回到刚才反编译的 game.wat 文件。
找到加法逻辑:
local.get $p0
i32.const 1 ;; <— 这里是增加的数量
i32.add
我们将 1 修改为 1000:
local.get $p0
i32.const 1000 ;; <— 修改为 1000
i32.add
2. 重编译为 Wasm
wat2wasm game.wat -o game_hacked.wasm
3. 替换运行 (浏览器端 Hook)
我们不能直接替换服务器文件,但我们可以通过 Chrome 的 Local Overrides 功能,或者写一个 TamperMonkey 脚本来拦截网络请求。
TamperMonkey 脚本思路:
const originalFetch = window.fetch;
window.fetch = async function(url, options) {
if (url.includes('game.wasm')) {
// 拦截请求,返回我们修改过的二进制数据
const response = await originalFetch('http://localhost:8000/game_hacked.wasm');
return response;
}
return originalFetch(url, options);
};
刷新网页,点击按钮,金币直接 +1000!
🛡️ 五、 防御:开发者如何保护 Wasm?
看到这里,你可能会觉得 Wasm 在裸奔。作为开发者,如何防御?
🎯 总结
Wasm 逆向正处于一个蓝海阶段。
它既不像 x86 汇编那样指令繁杂,也不像 JS 那样容易被混淆成乱码。
掌握了 wasm2wat 和线性内存的原理,你就能看穿网页中那些“高性能黑盒”背后的秘密。
Next Step:
找一个简单的在线 CTF 题目(WebAssembly 类),尝试不看 Writeup,使用 Chrome DevTools 的断点功能,追踪一个加密函数的输入输出,逆向出它的 Flag。


