text
stringlengths
0
1.99k
To demonstrate this, we can modify our example to import
importObject.toString.constructor (the Function constructor) as a global
object, and pass that to $logger instead of 42. We also have to slightly
change the import of $logger such that it takes an externref instead of an
i32: this can be used to represent arbitrary external (JavaScript) values;
WASM cannot operate on them, but they can be passed along.
(module
(import "ns" "logger" (func $logger (param externref)))
(import "toString" "constructor" (global $oops externref))
(func $main
global.get $oops
call $logger
)
(start $main)
)
Running this module now logs the following to console:
The answer is: function Function() { [native code] }
...which is indeed the (string form) of the Function constructor!
This is problematic for someone trying to limit the module's interface to
the outside world as it gives the attacker a bunch of "bonus" imports to
play with. Even if importObject is entirely empty ({}).
---[ 4 - Planning our escape
Obviously the next step is to figure out which extra powers this gives us.
Above, we imported importObj.toString.constructor as a value, but of course
we could also import it as a function. The Function constructor is actually
quite interesting as it behaves similarly to eval(), though with an extra
step of indirection:
> x = Function("console.log(42)")
[Function: anonymous]
> x()
42
So, if we can somehow (1) pass a string argument containing our JavaScript
payload and (2) invoke the returned value as a function, then this would
give us a full escape to JavaScript. This is not that easy however.
For problem (1), the issue is that WASM does not have a string type. At
best we can specify importObj.toString.constructor to take an i32 as
argument, and this will work, but this does not get us very far (the
integer will be converted to a string, but "42" is not a very useful piece
of JavaScript). This means that we need to find a way to use the other
available "gadgets" from the Object prototype to craft arbitrary strings.
Once we have a way to get a JavaScript string, we can pass it to the
Function constructor as an externref.
For problem (2), the challenge is that WASM does not really have a concept
of external function pointers. Or at least, not in the sense that we can
take the return value of an external function and call that directly.
The standard keeps evolving and this might be possible in the near future,
but for now we're stuck with an "externref" which we cannot invoke
directly. Hence, we also need to find a gadget that can do this for us.
Let's have a look at what we have available.
---[ 5 - Available gadgets
We can group the set of prototype-inherited properties of importObject
(i.e., of the Object prototype) into the following categories:
- hasOwnProperty, isPrototypeOf, propertyIsEnumerable,
toLocaleString, toString, valueOf
- Regular instance methods, each containing the same second-level
properties provided by the Function prototype (e.g.
importObject.hasOwnProperty.apply)
- constructor
- the Object constructor, containing the same as the other functions
above, but also a bunch of static methods
- __proto__
- the only non-function property, containing all of the above on the
second level (e.g., importObject.__proto__.hasOwnProperty)
In JavaScript, methods are invoked on an object instance using the dot
syntax (foo.bar()), which implicitly sets "this". When you take a method
by itself and call it separately, the value of "this" is not retained:
> x = "hello";
> x.toString();
"hello"
> y = x.toString;
> y();
Uncaught TypeError: String.prototype.toString requires that 'this' be a
String at toString (<anonymous>)
This same holds for our imported methods. While we can for example import
and call importObj.__proto__.toString, it is of not much use to us, as we
cannot control the value of "this" (it will be undefined). Hence, the only
useful functions that remain are static ones. Namely, the Function