1 00:00:00,000 --> 00:00:09,800 Hello. Hello, everyone. Thank you for my introduction. So, I was so nervous before my 2 00:00:09,800 --> 00:00:18,480 first DEF CON talk that I prepared 62 slides for this 20-minute talk. We will try to manage 3 00:00:18,480 --> 00:00:29,280 it. So, we'll talk about prototype pollution vulnerabilities in JavaScript. This talk is 4 00:00:29,280 --> 00:00:36,300 based on the joint work with Musart Balio and Chris Tycho. In this paper, we study prototype 5 00:00:36,300 --> 00:00:44,820 pollution vulnerabilities and their gadget and implemented the tools to detect them. I 6 00:00:44,820 --> 00:00:52,420 already presented part of this talk, the exploits that we detected in the real application and 7 00:00:52,420 --> 00:00:59,080 details of the gadget on Black Hat Asia. And today, I want to present more details about 8 00:00:59,280 --> 00:01:10,120 our methodology, about the tools. And we let's start with some introduction in JavaScript 9 00:01:10,460 --> 00:01:20,360 and how inheritance work in JavaScript through an example. So, we run Node.js, which executes 10 00:01:20,360 --> 00:01:28,260 the code in index file. The first line of the code creates an empty JavaScript object. 11 00:01:29,280 --> 00:01:35,320 Then it allocates a new object with the building property that points to the object prototype. 12 00:01:36,200 --> 00:01:42,260 The object prototype has a bunch of functions that we can reuse. For example, to string. 13 00:01:43,600 --> 00:01:48,420 To implement inheritance, JavaScript allows to extend the prototype with a new property. 14 00:01:48,680 --> 00:01:57,360 In this example, we define the property X with value 42. Things get more interesting when we 15 00:01:59,280 --> 00:02:06,760 look at another object. In principle, unrelated from the first one. Both objects share the same 16 00:02:06,760 --> 00:02:13,860 prototype. When the runtime executes the last line of the code to print X property for the 17 00:02:13,860 --> 00:02:24,780 second, um, object, it tries to find prototype in the object itself. Since X is undefined for the 18 00:02:24,780 --> 00:02:29,260 second object, the runtime will look up the property X in the prototype. So, the runtime will 19 00:02:29,280 --> 00:02:34,700 look up the property X in the prototype. In this case, um, it prints 42 to the terminal. 20 00:02:37,260 --> 00:02:45,180 Well, let's consider the threat model of web applications to see how this feature can affect, 21 00:02:45,380 --> 00:02:53,180 uh, security properties of the application. Index.js file creates a simple web app that 22 00:02:53,180 --> 00:02:59,260 handles to request, update, and backup. The attacker in our threat model, uh, uses a 23 00:02:59,280 --> 00:03:06,720 shell, can send any request to the server. For example, update with any parameters. Let's see 24 00:03:06,720 --> 00:03:16,280 what happens when the parameters are in this figure. The code creates an empty object. It reads 25 00:03:16,280 --> 00:03:26,600 proto property. And then the attacker adds, uh, the property shell, uh, with the value calc to object 26 00:03:26,600 --> 00:03:29,260 prototype. Okay. So, let's see how this works. So, let's see how this works. So, let's see how this 27 00:03:29,260 --> 00:03:33,920 works. So, let's see how this works. So, this code pattern is called prototype pollution 28 00:03:33,920 --> 00:03:42,380 vulnerability. But how can it affect our application? Let's assume that this code can le- 29 00:03:42,380 --> 00:03:50,700 handles backup request. It just executes backup script by the helper function. Notice that the 30 00:03:50,700 --> 00:03:57,640 attacker cannot control any arguments of this helper function. In this function, we can give some 31 00:03:57,640 --> 00:04:06,660 options in terms of which shell you want to use. Uh if we don't specify this option it will use 32 00:04:06,660 --> 00:04:14,500 the default shell and then uh it will run a new process. If the attacker sends backup 33 00:04:14,500 --> 00:04:23,340 request after prototype pollution we execute the function and options dot shell reads the 34 00:04:23,340 --> 00:04:32,080 attacker controlled property from the prototype and runs the calculator. So, we get remote 35 00:04:32,080 --> 00:04:41,320 code execution and this code fragments uh call it prototype pollution gadget. If you find 36 00:04:41,320 --> 00:04:47,520 gadget in the code of node.js itself the impact is much higher because it's potentially 37 00:04:47,520 --> 00:04:53,320 affect of all applications. In summary, this is an example of a prototype pollution gadget. 38 00:04:53,340 --> 00:04:57,720 To achieve remote code execution we need two steps. Identifying prototype pollution and 39 00:04:57,720 --> 00:05:04,660 identifying gadgets. How to identify prototype pollution at scale? We implemented static 40 00:05:04,660 --> 00:05:12,840 analysis for node.js applications and uh NPM packages. We used stint analysis where we 41 00:05:12,840 --> 00:05:19,960 marked the attacker controller data by the input uh label. Uh however, we cannot define 42 00:05:19,960 --> 00:05:23,320 these things syntactically because not every property is syntactically. So, we need to 43 00:05:23,340 --> 00:05:30,000 identify the property assigned uh not prop not every property assignment uh leads to prototype 44 00:05:30,000 --> 00:05:37,980 pollution. Instead we use uh what we call multi-label paint analysis to find the sync. Let's 45 00:05:37,980 --> 00:05:44,460 see how it looks uh at this example. We assume that all arguments uh of the function are 46 00:05:44,460 --> 00:05:53,320 attacker controlled. Uh and mark uh them by input label. We propagate the input label and 47 00:05:53,340 --> 00:06:01,940 if we have a property read with the tainted property name we change the prototype to proto. It 48 00:06:01,940 --> 00:06:11,460 means attacker can potentially read uh object prototype um um here. When the analysis detect 49 00:06:11,460 --> 00:06:19,260 the property assignment with receivers that has a proto label like this one it reports the code 50 00:06:19,260 --> 00:06:23,320 fragment as potential prototype pollution vulnerability. This is for single npm package. For 51 00:06:23,340 --> 00:06:39,140 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. 52 00:06:39,440 --> 00:06:48,200 The best result achieves 97% recall. It means we detected 97 vulnerabilities out of 100. 53 00:06:48,200 --> 00:06:53,080 And which is necessary to find vulnerabilities in the real applications. 54 00:06:54,400 --> 00:06:58,300 The second question is how to identify the gadgets. 55 00:06:59,160 --> 00:07:09,180 We analyzed Node.js code and used dynamic analysis to detect property reads from object prototype. 56 00:07:10,180 --> 00:07:18,180 Then we used static analysis to find flows from these properties to internal Node.js code. 57 00:07:18,200 --> 00:07:25,420 You can find all details of this analysis in the paper. In presentation I want to show some interesting results. 58 00:07:27,120 --> 00:07:32,260 So we detected 11 different gadgets in Node.js APIs. 59 00:07:33,880 --> 00:07:38,400 The first gadget is pawn function which executes new process. 60 00:07:40,000 --> 00:07:44,600 Let's look at this code. Details are not important here. 61 00:07:44,980 --> 00:07:47,260 But we see that the properties shell. 62 00:07:47,260 --> 00:07:51,800 And the property and can be undefined. 63 00:07:52,500 --> 00:07:59,160 And there is a flow from these properties to the internal function call. 64 00:07:59,800 --> 00:08:01,500 Which is actually vulnerable. 65 00:08:02,460 --> 00:08:08,620 It's simplified version of spawn function from Node.js API implementation. 66 00:08:10,560 --> 00:08:14,280 Let's see how remote code execution can be achieved. 67 00:08:15,020 --> 00:08:16,940 Suppose that the backup handler. 68 00:08:16,940 --> 00:08:21,120 Calls spawn with no attacker controlled arguments. 69 00:08:22,620 --> 00:08:27,720 The attacker first pollutes the prototype by update request as we saw earlier. 70 00:08:28,460 --> 00:08:33,420 They add property shell with value node to object prototype. 71 00:08:33,740 --> 00:08:36,840 The property ends by another request. 72 00:08:37,540 --> 00:08:41,540 And send backup request to execute spawn function. 73 00:08:42,800 --> 00:08:44,160 Let's see what happens. 74 00:08:44,460 --> 00:08:45,940 When spawn executes. 75 00:08:45,940 --> 00:08:52,560 It reads the value of the shell and end from the prototype. 76 00:08:53,440 --> 00:08:57,380 It allows the attacker to run Node.js in debugging mode. 77 00:08:57,540 --> 00:08:59,580 By controlling environment variables. 78 00:09:00,160 --> 00:09:04,840 And connect remotely to execute arbitrary code. 79 00:09:05,620 --> 00:09:11,020 For this we implemented the shell based on Node.js remote debugging protocol. 80 00:09:11,700 --> 00:09:14,120 You can see the short demo on the slide. 81 00:09:15,940 --> 00:09:19,200 The second gadget is the require function. 82 00:09:19,680 --> 00:09:25,540 The require function is used to include external packages to an application. 83 00:09:25,960 --> 00:09:30,520 So each application has a lot of require calls. 84 00:09:32,720 --> 00:09:35,640 This is a simplified snippet of require function. 85 00:09:36,060 --> 00:09:39,800 As we can see it reads package configuration file. 86 00:09:40,020 --> 00:09:41,120 Package JSON. 87 00:09:41,520 --> 00:09:44,320 And it evaluates the entry point. 88 00:09:44,320 --> 00:09:48,080 If this is defined in the property main. 89 00:09:48,940 --> 00:09:50,720 If main is undefined. 90 00:09:50,840 --> 00:09:52,260 Node.js uses a default value. 91 00:09:53,200 --> 00:09:58,300 Let's see how we can exploit it if the attacker pollutes the main property. 92 00:09:59,200 --> 00:10:03,000 To exploit the gadget we need to require a function call. 93 00:10:03,240 --> 00:10:06,880 For a package without the main property defined. 94 00:10:07,540 --> 00:10:12,660 An example of such a gadget is bytes. 95 00:10:12,780 --> 00:10:14,200 One of the popular gadgets. 96 00:10:14,320 --> 00:10:16,180 An NPM ecosystem. 97 00:10:16,900 --> 00:10:22,760 Let's see how it works when the attacker triggers this code to execute the require call. 98 00:10:23,920 --> 00:10:26,700 The attacker triggers the backup handler. 99 00:10:27,300 --> 00:10:30,660 Which pars a config file of byte package. 100 00:10:31,500 --> 00:10:34,060 Since the main property is undefined. 101 00:10:34,140 --> 00:10:37,140 It lookups the property from the prototype. 102 00:10:37,680 --> 00:10:39,160 To achieve remote code execution. 103 00:10:39,520 --> 00:10:42,940 Attacker should control this malicious file. 104 00:10:44,320 --> 00:10:46,880 As you can guess this is a strong requirement. 105 00:10:47,400 --> 00:10:52,040 The attacker should be able to upload some malicious file to the system. 106 00:10:52,200 --> 00:10:53,900 For example using some another vulnerability. 107 00:10:55,720 --> 00:10:58,820 Let's see how we can bypass this limitation. 108 00:10:59,200 --> 00:11:02,240 By combining these two gadgets. 109 00:11:03,100 --> 00:11:08,620 The key idea is to use require gadget to trigger the spawn gadget. 110 00:11:09,400 --> 00:11:12,200 To achieve this we need to find the existing file. 111 00:11:12,200 --> 00:11:14,720 That execute spawn function. 112 00:11:15,000 --> 00:11:19,540 If we identify a file in node.js default distribution. 113 00:11:20,060 --> 00:11:24,120 Then we increase the impact of the exploit. 114 00:11:24,300 --> 00:11:30,360 Because every application use node.js already have this file. 115 00:11:31,800 --> 00:11:34,960 An example of such a file is NPM.js. 116 00:11:35,160 --> 00:11:38,440 Which runs node instance. 117 00:11:40,060 --> 00:11:43,180 Let's look at the end to end exploit. 118 00:11:43,920 --> 00:11:48,780 The attacker pollutes the main property with the pass to NPM.js. 119 00:11:49,560 --> 00:11:55,780 And the end property as required by spawn gadget. 120 00:11:58,520 --> 00:12:01,240 Finally when backup is triggered. 121 00:12:02,380 --> 00:12:07,080 Node.js executes the require function. 122 00:12:08,180 --> 00:12:09,960 Loads NPM.js. 123 00:12:10,940 --> 00:12:13,920 That calls spawn function. 124 00:12:14,940 --> 00:12:19,320 The spawn function reads the attacker control at environment variable from the prototype. 125 00:12:19,640 --> 00:12:21,980 And runs node.js in debugging mode. 126 00:12:22,200 --> 00:12:23,640 As we saw earlier. 127 00:12:24,260 --> 00:12:26,700 So the attacker achieve remote code execution. 128 00:12:26,820 --> 00:12:30,280 For require call without additional requirements. 129 00:12:33,260 --> 00:12:35,100 The last question is. 130 00:12:35,100 --> 00:12:38,960 How to exploit real application using OR tools. 131 00:12:39,380 --> 00:12:40,940 And detected gadgets. 132 00:12:43,000 --> 00:12:47,900 We created the GitHub for node.js applications. 133 00:12:48,520 --> 00:12:51,720 And took 15 most popular ones. 134 00:12:52,260 --> 00:12:53,460 We ran OR tool. 135 00:12:53,760 --> 00:12:56,380 And got some prototype pollution cases. 136 00:12:57,740 --> 00:13:00,560 As you can see the prototype pollution pattern is rare. 137 00:13:00,960 --> 00:13:02,300 In practice. 138 00:13:02,460 --> 00:13:04,420 And manual verification is applicable. 139 00:13:05,100 --> 00:13:06,840 To the total number of detected cases. 140 00:13:08,600 --> 00:13:10,380 We confirmed it. 141 00:13:10,440 --> 00:13:12,280 Eight detected cases. 142 00:13:12,820 --> 00:13:14,260 As exploitable. 143 00:13:14,580 --> 00:13:16,820 And reported them to maintainers. 144 00:13:16,900 --> 00:13:17,540 You can see. 145 00:13:17,640 --> 00:13:20,520 We found two cases in NPM CLI. 146 00:13:20,720 --> 00:13:22,680 Everyone I think use this app. 147 00:13:23,900 --> 00:13:25,500 Parse server. 148 00:13:25,780 --> 00:13:28,320 And rocket chat messenger. 149 00:13:29,780 --> 00:13:34,760 The main problem with real exploitation of this vulnerability. 150 00:13:35,100 --> 00:13:36,400 To achieve remote code execution. 151 00:13:36,760 --> 00:13:43,820 It's that code of some code of the application. 152 00:13:43,960 --> 00:13:46,660 Can breaks your application. 153 00:13:46,920 --> 00:13:49,120 If you pollute object prototype. 154 00:13:49,440 --> 00:13:53,400 So the code does not expect any additional properties in the object prototype. 155 00:13:54,340 --> 00:13:57,860 It happened many times in these experiments. 156 00:13:58,320 --> 00:14:01,200 And I want to show one technique that I used. 157 00:14:01,960 --> 00:14:04,080 And that you can also. 158 00:14:05,580 --> 00:14:07,580 Apply for your research. 159 00:14:08,320 --> 00:14:10,140 Let's see a case for parse server. 160 00:14:10,360 --> 00:14:11,220 We have a parse server. 161 00:14:11,420 --> 00:14:14,980 It's a server that provides REST API out of the box. 162 00:14:15,080 --> 00:14:16,160 And use some database. 163 00:14:16,440 --> 00:14:19,380 For example MongoDB to store data from the request. 164 00:14:22,040 --> 00:14:26,100 It also use JSBSON. 165 00:14:26,320 --> 00:14:27,900 It's a library of the MongoDB. 166 00:14:28,040 --> 00:14:31,880 That can serialize and deserialize the data. 167 00:14:31,960 --> 00:14:33,660 To binary MongoDB format. 168 00:14:34,700 --> 00:14:39,180 So we detected prototype pollution case in parse server. 169 00:14:39,680 --> 00:14:42,180 This example of the code. 170 00:14:42,360 --> 00:14:43,560 Details is not interesting. 171 00:14:44,620 --> 00:14:45,920 It's not important. 172 00:14:46,120 --> 00:14:49,180 What's actually important for us. 173 00:14:49,220 --> 00:14:51,120 That attacker can control name. 174 00:14:51,320 --> 00:14:54,900 And the value of the polluted property. 175 00:14:55,520 --> 00:15:01,300 We also detected a gadget in JSBSON package. 176 00:15:01,700 --> 00:15:03,940 And this gadget can be triggered. 177 00:15:04,080 --> 00:15:07,700 When JSBSON deserialize some data from the MongoDB. 178 00:15:07,940 --> 00:15:11,260 So this gadget call evolve function. 179 00:15:11,400 --> 00:15:13,460 For function string from the MongoDB. 180 00:15:13,700 --> 00:15:17,080 That attacker can store to the MongoDB before. 181 00:15:17,340 --> 00:15:21,580 But the problem that this evolve call executed. 182 00:15:22,540 --> 00:15:26,480 Only if evolve function option is enabled. 183 00:15:27,100 --> 00:15:28,100 As you can guess. 184 00:15:28,220 --> 00:15:30,940 It's disabled by default in the configuration file. 185 00:15:31,220 --> 00:15:34,060 But it can be polluted. 186 00:15:34,080 --> 00:15:38,080 And the gadget reads it from the polluted prototype. 187 00:15:38,320 --> 00:15:40,420 And we can enable this feature in runtime. 188 00:15:40,800 --> 00:15:41,800 This is the main idea. 189 00:15:42,780 --> 00:15:47,420 So let's see on the naive way how we can exploit it. 190 00:15:48,220 --> 00:15:50,040 Attacker first sent a package. 191 00:15:50,320 --> 00:15:52,520 That should trigger prototype pollution vulnerability. 192 00:15:53,160 --> 00:15:55,740 The package serialize it to the MongoDB. 193 00:15:56,020 --> 00:15:57,660 Response deserialize it. 194 00:15:57,740 --> 00:15:59,300 And trigger prototype pollution. 195 00:15:59,640 --> 00:16:00,100 Good. 196 00:16:00,760 --> 00:16:02,440 We pollute the option. 197 00:16:02,440 --> 00:16:04,880 That we need to trigger a gadget. 198 00:16:05,100 --> 00:16:07,880 After that the attacker sent a package. 199 00:16:08,060 --> 00:16:09,560 That should trigger a gadget. 200 00:16:09,840 --> 00:16:14,780 But serializer of JavaScript BSON library throws an exception. 201 00:16:15,500 --> 00:16:16,860 And application crashed. 202 00:16:17,620 --> 00:16:20,100 It looks like unexploitable case. 203 00:16:20,340 --> 00:16:23,780 We cannot send any request after prototype pollution. 204 00:16:24,100 --> 00:16:27,300 Application crashed after the first request that we tried to send. 205 00:16:28,160 --> 00:16:30,420 How we can bypass it? 206 00:16:30,420 --> 00:16:32,540 I found a way. 207 00:16:32,740 --> 00:16:35,280 Some kind of race condition. 208 00:16:35,900 --> 00:16:36,660 Let's see. 209 00:16:37,140 --> 00:16:42,020 If the attacker sent the package that trigger remote code execution gadget first. 210 00:16:42,340 --> 00:16:46,040 This package serialize it. 211 00:16:46,160 --> 00:16:49,520 And MongoDB start to handle it for some long time. 212 00:16:50,740 --> 00:16:54,080 At this moment the attacker sent second gadget. 213 00:16:54,360 --> 00:16:56,600 That should trigger prototype pollution vulnerability. 214 00:16:57,320 --> 00:17:00,340 It's serialized and deserialized from the MongoDB. 215 00:17:00,340 --> 00:17:02,940 And successfully trigger prototype pollution. 216 00:17:03,300 --> 00:17:09,360 After that MongoDB decided to stop handling the first request. 217 00:17:09,720 --> 00:17:12,620 And pass it to the application. 218 00:17:13,000 --> 00:17:14,720 And we trigger remote code execution. 219 00:17:15,500 --> 00:17:17,680 This technique I used many times. 220 00:17:17,880 --> 00:17:25,180 Usually you need very short time gap to trigger remote code execution gadget. 221 00:17:25,420 --> 00:17:26,700 One of the technique that I use. 222 00:17:26,800 --> 00:17:27,440 I just sent. 223 00:17:28,460 --> 00:17:32,640 Like 20-50 requests to trigger remote code execution. 224 00:17:33,060 --> 00:17:36,060 And one request that trigger prototype pollution in the middle. 225 00:17:36,340 --> 00:17:42,220 Most likely we get some situation that we trigger remote code execution. 226 00:17:43,360 --> 00:17:46,860 Exactly after prototype pollution in the some thread. 227 00:17:47,600 --> 00:17:48,240 Okay. 228 00:17:48,380 --> 00:17:50,880 Let's see how it looks in practice. 229 00:17:51,160 --> 00:17:51,900 Some short demo. 230 00:17:52,240 --> 00:17:53,740 We run parse server. 231 00:17:54,040 --> 00:17:55,820 We need to implement a script. 232 00:17:55,820 --> 00:17:58,820 To exploit this vulnerability. 233 00:17:59,080 --> 00:18:06,400 Because the second request we need to send in 100-300 milliseconds. 234 00:18:06,940 --> 00:18:07,940 After the first one. 235 00:18:08,120 --> 00:18:09,580 So it's not possible to do manually. 236 00:18:10,540 --> 00:18:15,360 So this script prepared database to add some data to MongoDB. 237 00:18:15,560 --> 00:18:17,220 To make it a little bit slower. 238 00:18:17,540 --> 00:18:20,340 To handle the first request for the long time. 239 00:18:20,480 --> 00:18:23,400 And send these two requests. 240 00:18:23,400 --> 00:18:30,660 And you can see that we get calculator from the parse server. 241 00:18:30,880 --> 00:18:31,720 So it works. 242 00:18:40,020 --> 00:18:41,780 So in conclusion. 243 00:18:42,540 --> 00:18:48,280 We implemented and open sourced tools to detect prototype pollution vulnerabilities. 244 00:18:48,440 --> 00:18:49,780 And their gadget. 245 00:18:50,800 --> 00:18:53,380 Detected 11 new gadgets. 246 00:18:53,380 --> 00:18:55,260 In NodeJS APIs. 247 00:18:55,820 --> 00:19:00,220 You also can find them in the GitHub repo. 248 00:19:01,440 --> 00:19:03,600 Some gadgets already fixed. 249 00:19:04,000 --> 00:19:07,600 But not all combination of that. 250 00:19:08,260 --> 00:19:16,280 And we reported a remote code execution vulnerabilities in the popular open source applications. 251 00:19:17,320 --> 00:19:20,320 So the interesting question for the future work. 252 00:19:20,640 --> 00:19:21,660 One of that. 253 00:19:21,700 --> 00:19:23,360 What is the more efficient way. 254 00:19:23,380 --> 00:19:24,720 To detect gadgets. 255 00:19:25,920 --> 00:19:28,980 So we have options like static analysis. 256 00:19:29,200 --> 00:19:29,920 Dynamic analysis. 257 00:19:30,120 --> 00:19:32,060 We already used some hybrid approach. 258 00:19:32,500 --> 00:19:34,900 I think it's a good question for the future research. 259 00:19:35,240 --> 00:19:39,240 How many new gadgets in NPM packages? 260 00:19:39,820 --> 00:19:42,880 We did not consider in this research NPM packages. 261 00:19:43,220 --> 00:19:47,440 But it's also a large surface for the gadget. 262 00:19:47,820 --> 00:19:52,100 How many new gadgets in NodeJS APIs source code? 263 00:19:52,100 --> 00:19:56,680 If we find some efficient way to detect it. 264 00:19:56,780 --> 00:19:57,660 To explore it. 265 00:19:58,000 --> 00:20:00,680 Probably we can find some new ones. 266 00:20:01,020 --> 00:20:02,620 We continue research in this direction. 267 00:20:03,040 --> 00:20:05,460 If you're interested in this. 268 00:20:05,740 --> 00:20:07,240 Just follow me in the Twitter. 269 00:20:07,860 --> 00:20:09,940 And thank you for your attention.