text
stringlengths
0
1.99k
> Object.groupBy([['H'],['e'],['l'],['l'],['o']], () => "raw")
{
raw: [ [ 'H' ], [ 'e' ], [ 'l' ], [ 'l' ], [ 'o' ] ]
}
To invoke String.raw() we use Object.groupBy() again, but it means we have
to wrap our object (the argument to String.raw()) in an array. One way to
achieve this is by obtaining an existing array containing a single object,
and using Object.assign() to merge our own object into that inner object.
All in all, we end up at the following WASM for $list_to_string:
;; Turns a list such as [["a"],["b"],["c"],["d"]] into ["a0bcd"]
;; (yes, the "0" is added due to how we use Object.groupBy)
;; (yes, it returns an array, but this is fine for our usecase)
(func $list_to_string (param $list externref) (result externref)
(local $arrwithobj externref)
(local $innerobj externref)
(local $res externref)
;; Create an object with the key "raw", with as value our input list
;; because String.raw uses this key to build the string. (groupBy wraps
;; grouped elements in a list, but they all group to the same key so it
;; gives back the original list)
(global.get $s_raw)
(global.set $g_val_e)
(call $groupBy_i (local.get $list) (ref.func $return_val_e))
(local.set $res)
;; This is just a way to get an array with a single object. We need
;; this as we want to give the object as a parameter to String.raw(),
;; but it needs to be wrapped in an array for the Object.groupBy trick.
(call $values (call $getOwnPropertyDescriptors (local.get $res)))
(local.set $arrwithobj)
;; Grab the inner object, for the purpose of modifying it
(call $array_get_nth_element (local.get $arrwithobj) (i32.const 0))
(local.set $innerobj)
;; Merge 'res' into it (our object with the raw key)
(call $assign (local.get $innerobj) (local.get $res))
;; Now we can invoke String.raw via Object.groupBy
(call $groupBy_e (local.get $arrwithobj)
(global.get $String_constructor_raw)
)
;; The result is in the key of the returned object, so extract that
(call $keys)
)
---[ 6 - Keeping things JS engine-agnostic
Important to our "exploit" is the ability to obtain a known property from
an object instance. We did this by taking the n-th element of the object's
property descriptors, but this order is not specified by the standard, and
hence differs per JavaScript engine.
Luckily there's a way to generalize this. As long as our desired property
name has a unique length (for example, "raw" is the only property of String
with a name of length 3), we can search through the property list for a
name with that expected length.
To get the length of a string we can use Object.getOwnPropertyDescriptor()
with the argument "length", for example:
// "length" is the only string we get for free (only prop of an array)
> Object.getOwnPropertyDescriptor("raw", "length")
{ value: 3, writable: false, enumerable: false, configurable: false }
Then, it's a matter of obtaining the "value" property of the descriptor,
but this leads us back to the original problem... There's a workaround
though: for the property descriptor of "length".length, we know that its
value is 6:
> Object.getOwnPropertyDescriptor("length","length")
{ value: 6, writable: false, enumerable: false, configurable: false }
> Object.values(Object.getOwnPropertyDescriptor("length","length"))
[ 6, false, false, false ]
Hence, we can use Object.groupBy() to find the index of 6 within this list
(save the index whose element equals 6): this is the index of the "value"
key in all property descriptors! Are you still following? ;)
This completes the circle, and gives us a fully browser/engine-independent
method of accessing object properties (as long as their length is unique).
---[ 7 - Exploit recap
To recap, our exploit consists of the following steps:
0. Import a bunch of static methods under Object using the prototype-
inherited "constructor" namespace, e.g. `"constructor" "groupBy"`.
1. Obtain "length" and use it to obtain references to
String.fromCharCode(), String.raw(), and the string "raw".
2. Use String.fromCharCode() combined with Object.groupBy(),
Object.assign(), Object.keys() and Object.values() (among others) to
turn individual numbers into a list of characters making up our payload.