Hello. Hello, everyone. Thank you for my introduction. So, I was so nervous before my first DEF CON talk that I prepared 62 slides for this 20-minute talk. We will try to manage it. So, we'll talk about prototype pollution vulnerabilities in JavaScript. This talk is based on the joint work with Musart Balio and Chris Tycho. In this paper, we study prototype pollution vulnerabilities and their gadget and implemented the tools to detect them. I already presented part of this talk, the exploits that we detected in the real application and details of the gadget on Black Hat Asia. And today, I want to present more details about our methodology, about the tools. And we let's start with some introduction in JavaScript and how inheritance work in JavaScript through an example. So, we run Node.js, which executes the code in index file. The first line of the code creates an empty JavaScript object. Then it allocates a new object with the building property that points to the object prototype. The object prototype has a bunch of functions that we can reuse. For example, to string. To implement inheritance, JavaScript allows to extend the prototype with a new property. In this example, we define the property X with value 42. Things get more interesting when we look at another object. In principle, unrelated from the first one. Both objects share the same prototype. When the runtime executes the last line of the code to print X property for the second, um, object, it tries to find prototype in the object itself. Since X is undefined for the second object, the runtime will look up the property X in the prototype. So, the runtime will look up the property X in the prototype. In this case, um, it prints 42 to the terminal. Well, let's consider the threat model of web applications to see how this feature can affect, uh, security properties of the application. Index.js file creates a simple web app that handles to request, update, and backup. The attacker in our threat model, uh, uses a shell, can send any request to the server. For example, update with any parameters. Let's see what happens when the parameters are in this figure. The code creates an empty object. It reads proto property. And then the attacker adds, uh, the property shell, uh, with the value calc to object prototype. Okay. So, let's see how this works. So, let's see how this works. So, let's see how this works. So, let's see how this works. So, this code pattern is called prototype pollution vulnerability. But how can it affect our application? Let's assume that this code can le- handles backup request. It just executes backup script by the helper function. Notice that the attacker cannot control any arguments of this helper function. In this function, we can give some options in terms of which shell you want to use. Uh if we don't specify this option it will use the default shell and then uh it will run a new process. If the attacker sends backup request after prototype pollution we execute the function and options dot shell reads the attacker controlled property from the prototype and runs the calculator. So, we get remote code execution and this code fragments uh call it prototype pollution gadget. If you find gadget in the code of node.js itself the impact is much higher because it's potentially affect of all applications. In summary, this is an example of a prototype pollution gadget. To achieve remote code execution we need two steps. Identifying prototype pollution and identifying gadgets. How to identify prototype pollution at scale? We implemented static analysis for node.js applications and uh NPM packages. We used stint analysis where we marked the attacker controller data by the input uh label. Uh however, we cannot define these things syntactically because not every property is syntactically. So, we need to identify the property assigned uh not prop not every property assignment uh leads to prototype pollution. Instead we use uh what we call multi-label paint analysis to find the sync. Let's see how it looks uh at this example. We assume that all arguments uh of the function are attacker controlled. Uh and mark uh them by input label. We propagate the input label and if we have a property read with the tainted property name we change the prototype to proto. It means attacker can potentially read uh object prototype um um here. When the analysis detect the property assignment with receivers that has a proto label like this one it reports the code fragment as potential prototype pollution vulnerability. This is for single npm package. For This is the main idea of our analysis. We implemented it based on the CodeQL analysis framework and evaluated our analysis on 100 vulnerable packages that we collected. The best result achieves 97% recall. It means we detected 97 vulnerabilities out of 100. And which is necessary to find vulnerabilities in the real applications. The second question is how to identify the gadgets. We analyzed Node.js code and used dynamic analysis to detect property reads from object prototype. Then we used static analysis to find flows from these properties to internal Node.js code. You can find all details of this analysis in the paper. In presentation I want to show some interesting results. So we detected 11 different gadgets in Node.js APIs. The first gadget is pawn function which executes new process. Let's look at this code. Details are not important here. But we see that the properties shell. And the property and can be undefined. And there is a flow from these properties to the internal function call. Which is actually vulnerable. It's simplified version of spawn function from Node.js API implementation. Let's see how remote code execution can be achieved. Suppose that the backup handler. Calls spawn with no attacker controlled arguments. The attacker first pollutes the prototype by update request as we saw earlier. They add property shell with value node to object prototype. The property ends by another request. And send backup request to execute spawn function. Let's see what happens. When spawn executes. It reads the value of the shell and end from the prototype. It allows the attacker to run Node.js in debugging mode. By controlling environment variables. And connect remotely to execute arbitrary code. For this we implemented the shell based on Node.js remote debugging protocol. You can see the short demo on the slide. The second gadget is the require function. The require function is used to include external packages to an application. So each application has a lot of require calls. This is a simplified snippet of require function. As we can see it reads package configuration file. Package JSON. And it evaluates the entry point. If this is defined in the property main. If main is undefined. Node.js uses a default value. Let's see how we can exploit it if the attacker pollutes the main property. To exploit the gadget we need to require a function call. For a package without the main property defined. An example of such a gadget is bytes. One of the popular gadgets. An NPM ecosystem. Let's see how it works when the attacker triggers this code to execute the require call. The attacker triggers the backup handler. Which pars a config file of byte package. Since the main property is undefined. It lookups the property from the prototype. To achieve remote code execution. Attacker should control this malicious file. As you can guess this is a strong requirement. The attacker should be able to upload some malicious file to the system. For example using some another vulnerability. Let's see how we can bypass this limitation. By combining these two gadgets. The key idea is to use require gadget to trigger the spawn gadget. To achieve this we need to find the existing file. That execute spawn function. If we identify a file in node.js default distribution. Then we increase the impact of the exploit. Because every application use node.js already have this file. An example of such a file is NPM.js. Which runs node instance. Let's look at the end to end exploit. The attacker pollutes the main property with the pass to NPM.js. And the end property as required by spawn gadget. Finally when backup is triggered. Node.js executes the require function. Loads NPM.js. That calls spawn function. The spawn function reads the attacker control at environment variable from the prototype. And runs node.js in debugging mode. As we saw earlier. So the attacker achieve remote code execution. For require call without additional requirements. The last question is. How to exploit real application using OR tools. And detected gadgets. We created the GitHub for node.js applications. And took 15 most popular ones. We ran OR tool. And got some prototype pollution cases. As you can see the prototype pollution pattern is rare. In practice. And manual verification is applicable. To the total number of detected cases. We confirmed it. Eight detected cases. As exploitable. And reported them to maintainers. You can see. We found two cases in NPM CLI. Everyone I think use this app. Parse server. And rocket chat messenger. The main problem with real exploitation of this vulnerability. To achieve remote code execution. It's that code of some code of the application. Can breaks your application. If you pollute object prototype. So the code does not expect any additional properties in the object prototype. It happened many times in these experiments. And I want to show one technique that I used. And that you can also. Apply for your research. Let's see a case for parse server. We have a parse server. It's a server that provides REST API out of the box. And use some database. For example MongoDB to store data from the request. It also use JSBSON. It's a library of the MongoDB. That can serialize and deserialize the data. To binary MongoDB format. So we detected prototype pollution case in parse server. This example of the code. Details is not interesting. It's not important. What's actually important for us. That attacker can control name. And the value of the polluted property. We also detected a gadget in JSBSON package. And this gadget can be triggered. When JSBSON deserialize some data from the MongoDB. So this gadget call evolve function. For function string from the MongoDB. That attacker can store to the MongoDB before. But the problem that this evolve call executed. Only if evolve function option is enabled. As you can guess. It's disabled by default in the configuration file. But it can be polluted. And the gadget reads it from the polluted prototype. And we can enable this feature in runtime. This is the main idea. So let's see on the naive way how we can exploit it. Attacker first sent a package. That should trigger prototype pollution vulnerability. The package serialize it to the MongoDB. Response deserialize it. And trigger prototype pollution. Good. We pollute the option. That we need to trigger a gadget. After that the attacker sent a package. That should trigger a gadget. But serializer of JavaScript BSON library throws an exception. And application crashed. It looks like unexploitable case. We cannot send any request after prototype pollution. Application crashed after the first request that we tried to send. How we can bypass it? I found a way. Some kind of race condition. Let's see. If the attacker sent the package that trigger remote code execution gadget first. This package serialize it. And MongoDB start to handle it for some long time. At this moment the attacker sent second gadget. That should trigger prototype pollution vulnerability. It's serialized and deserialized from the MongoDB. And successfully trigger prototype pollution. After that MongoDB decided to stop handling the first request. And pass it to the application. And we trigger remote code execution. This technique I used many times. Usually you need very short time gap to trigger remote code execution gadget. One of the technique that I use. I just sent. Like 20-50 requests to trigger remote code execution. And one request that trigger prototype pollution in the middle. Most likely we get some situation that we trigger remote code execution. Exactly after prototype pollution in the some thread. Okay. Let's see how it looks in practice. Some short demo. We run parse server. We need to implement a script. To exploit this vulnerability. Because the second request we need to send in 100-300 milliseconds. After the first one. So it's not possible to do manually. So this script prepared database to add some data to MongoDB. To make it a little bit slower. To handle the first request for the long time. And send these two requests. And you can see that we get calculator from the parse server. So it works. So in conclusion. We implemented and open sourced tools to detect prototype pollution vulnerabilities. And their gadget. Detected 11 new gadgets. In NodeJS APIs. You also can find them in the GitHub repo. Some gadgets already fixed. But not all combination of that. And we reported a remote code execution vulnerabilities in the popular open source applications. So the interesting question for the future work. One of that. What is the more efficient way. To detect gadgets. So we have options like static analysis. Dynamic analysis. We already used some hybrid approach. I think it's a good question for the future research. How many new gadgets in NPM packages? We did not consider in this research NPM packages. But it's also a large surface for the gadget. How many new gadgets in NodeJS APIs source code? If we find some efficient way to detect it. To explore it. Probably we can find some new ones. We continue research in this direction. If you're interested in this. Just follow me in the Twitter. And thank you for your attention.