Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute arbitrary code via JSON and how to sanitize the input

In the documentation of Python's jsonpickle module for JSON serialization and deserialization it states that

Loading a JSON string from an untrusted source represents a potential security vulnerability. jsonpickle makes no attempt to sanitize the input

But I wonder how is it possible for an attacker to execute arbitrary code via JSON messages?

Also, what is the best way to sanitize the input as suggested in the documentation? JSON data in my application is not trust-worthy (it came from the clients that send JSON messages).

like image 781
FrozenHeart Avatar asked Aug 07 '16 07:08

FrozenHeart


People also ask

Is JSON a security risk?

JSON alone is not much of a threat. After all, it's only a data-interchange format. The real security concerns with JSON arise in the way that it is used. If misused, JSON-based applications can become vulnerable to attacks such as JSON hijacking and JSON injection.

What is JSON security?

JavaScript Object Notation (JSON) security performs deep inspection of incoming packets/requests for web applications that use the JSON protocol to exchange data over HTTP.

Is JSON parse vulnerable?

The built-in functions (JSON. parse()) are not vulnerable so you can use them safely. However, custom deserialization packages for JavaScript have different types of vulnerabilities, depending on the approach used to deserialize data.

What is JSON loads in Python?

loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.


1 Answers

jsonpickle is not JSON. jsonpickle allows to create arbitrary Python-Objects that potentially do harmful things. Sanitizing means, that the JSON objects only contain data, that can be interpreted by jsonpickle. Normally wrong data would lead to exceptions, but can may be used to trigger unwanted behavior.

The __reduce__ exploit (see, for example Into The Jar | Exploitation of jsonpickle)

jsonpickle.decode('{"py/object": "list", "py/reduce":[{"py/type": "subprocess.Popen"}, ["ls"], null, null, null]}')

is only one direct way to execute any command. More subtle ways depend on your actual code.

So the short answer is, not to use jsonpickle in an untrusted environment. Use normal JSON and check the input before using it.

like image 178
Daniel Avatar answered Oct 26 '22 01:10

Daniel