Insecure implementation of deserialization in cryo
The cryo deserialisation library allows an attacker to set the __proto__ property and define custom functions during parsing of JSON. If the resulting object is later used in a way that calls its prototype methods, the attacker's code runs, potentially leading to OS command execution.
- Triaged in 9 days
- Fixed in 35 days
- Disclosed after 39 days
I would like to report code injection in serialization package cryo It allows execute arbitrary code using custom prototype.
Module
module name: cryo
version: 0.0.6
npm page: https://www.npmjs.com/package/cryo
Module Description
JSON on steroids. Built for node.js and browsers. Cryo is inspired by Python's pickle and works similarly to JSON.stringify() and JSON.parse(). Cryo.stringify() and Cryo.parse() improve on JSON in these circumstances:
Module Stats
37 downloads in the last week
Vulnerability
Vulnerability Description
If an application uses "cryo" package to deserialize JSON into an object and interacts with the object later in the code (convert to sting, for example) and if an attacker controls this JSON, then the attacker can get arbitrary code execution in the application.
To reconstruct an object from JSON, cryo uses square bracket notation ( obj[key]=value ). So there is an opportunity for an attacker to change __proto__ property for a new object. Also Cryo supports serialization of functions, so the attacker can set their own methods (toString, valueOf) for the new object.
It means that if later in the code the application interacts with the new object in the way which leads to invocation of the object's prototype functions, then the attacker malicious code are executed.
Steps To Reproduce:
PoC:
var Cryo = require('cryo');
var frozen = '{"root":"_CRYO_REF_3","references":[{"contents":{},"value":"_CRYO_FUNCTION_function () {console.log(\\"defconrussia\\"); return 1111;}"},{"contents":{},"value":"_CRYO_FUNCTION_function () {console.log(\\"defconrussia\\");return 2222;}"},{"contents":{"toString":"_CRYO_REF_0","valueOf":"_CRYO_REF_1"},"value":"_CRYO_OBJECT_"},{"contents":{"__proto__":"_CRYO_REF_2"},"value":"_CRYO_OBJECT_"}]}'
var hydrated = Cryo.parse(frozen);
console.log(hydrated);
console.log internally calls hydrated's vauleOf method, so an attacker's code are executed and we can see "defconrussia" in console.
Patch
I suggest to blacklist "proto" property in deserialization process.
Supporting Material/References:
- Ubuntu 16.04
- node v6.11.3
- npm 5.5.1
Wrap up
- I contacted the maintainer to let them know: N
- I opened an issue in the related repository: N
Hunter's comments and funny memes goes here Also I found a couple of other modules (for example, https://www.npmjs.com/package/kaiser) which use square bracket notation too, so it's possible to rewrite
__proto__with them too. But us they don't support serialization of functions, we cannot use the same attack as described here. Still we can set wrong values for prototype's methods, so if an application tries to convert a new object (deserialized from JSON) to string, it may crash with a TypeError exception. It could be a kind of DoS. What do you think if I should create similar reports for such cases?
Impact
An attacker can craft a special JSON file with malicious code which rewrites __proto__ of a new object. In some circumstances it may lead to execution of the code, so the attacker can achieve OS command execution.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the deserialiser assigns properties using square bracket notation without blocking __proto__, and it also supports reconstructing functions, so an attacker can replace prototype methods with their own code.
Impact
An attacker can craft a special JSON file with malicious code which rewrites __proto__ of a new object. In some circumstances it may lead to execution of the code, so the attacker can achieve OS command execution.
What the attacker needed
Application uses cryo to parse attacker-controlled JSON; parsed object is later used in a way that calls prototype…
The fix
The deserialisation process should block the __proto__ property.
How it was found
- Craft a JSON string that defines references containing attacker-controlled functions and an object whose __proto__ points to one of those references.
- Pass the JSON string to Cryo.parse to deserialise it.
- Use the resulting object in a way that invokes a prototype method, such as console.log calling valueOf, which executes the attacker's function.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program turns text back into an object, it should not let the text choose special built-in properties. Here the library allowed a property called __proto__ to be set, which changes how the object behaves. The attacker also supplied their own function, so when the program later printed the object, that function ran.
What is Prototype pollution?
Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.