1 00:00:00,000 --> 00:00:07,600 My name is Mikhail Shcherbakov and today I want to tell you a few stories about Kibana backbounds. 2 00:00:08,000 --> 00:00:23,240 This first part of the talk is about vulnerabilities that I found in Kibana and I combined it only with remote code execution vulnerabilities in this talk, just because of time limits. 3 00:00:23,520 --> 00:00:26,720 A few words about me. 4 00:00:27,240 --> 00:00:41,580 Now I'm finishing my PhD studies at KTH University Stockholm and working on the thesis Code Reuse Attacks in Managed Programming Languages and Runtimes. 5 00:00:41,580 --> 00:00:52,620 My main research interest is static code analysis, dynamic program analysis, and language-based security in general. 6 00:00:52,620 --> 00:01:01,860 I participated in bug bounty programs of Microsoft, GitHub, some open source projects, and Elastic. 7 00:01:01,860 --> 00:01:11,360 And today we'll talk about Elastic bug bounty program, namely Kibana bug bounty program. 8 00:01:11,360 --> 00:01:13,360 Who is Kibana? 9 00:01:13,360 --> 00:01:24,300 Kibana is a part of popular ILCA stack that includes Elasticsearch, Logstash, and Kibana. 10 00:01:24,300 --> 00:01:32,140 It's used for data visualization and real-time analysis. 11 00:01:33,220 --> 00:01:35,700 So Kibana is an open source project. 12 00:01:35,700 --> 00:01:44,500 All code is available and it's possible to analyze statically or just make a manual review. 13 00:01:44,500 --> 00:01:50,180 It's definitely a large project, probably one of the largest. 14 00:01:50,180 --> 00:02:05,200 It's written in TypeScript and they have an interesting bug bounty program with 3,000-7,000 rewards for critical vulnerabilities. 15 00:02:05,200 --> 00:02:11,200 All of that makes Kibana a great target for bug hunters. 16 00:02:11,200 --> 00:02:16,620 And I contributed a lot last year in this program. 17 00:02:16,960 --> 00:02:26,980 And before starting to tell about vulnerabilities in Kibana, I would like to say my thanks to the Elastic security team. 18 00:02:27,180 --> 00:02:37,140 In my practice, in my experience, it's one of the best bug bounty programs that's managed by Elastic security team. 19 00:02:37,160 --> 00:02:49,880 Thank you guys for your work and the main reason that I contribute a lot in this bug bounty, it's just a good experience of this work. 20 00:02:50,490 --> 00:02:55,620 So story number one, how it all began. 21 00:02:57,190 --> 00:03:20,200 One year ago, in the last August, after DEFCON, I found that Elastic bug bounty started compiling for a new feature that's called synthetic monitoring in the Kibana product that can be used for some dashboard. 22 00:03:20,200 --> 00:03:37,500 The idea of the feature is that you can set up some monitors for websites and see how the website is available or some content was changed and make some reports based on such monitoring. 23 00:03:38,320 --> 00:03:44,120 Okay, let's see how this feature looks like in practice. 24 00:03:44,120 --> 00:03:47,340 It is a main page of the Kibana. 25 00:03:47,340 --> 00:03:51,500 You can find synthetics, the feature that we discussed. 26 00:03:51,500 --> 00:03:56,400 You can create a new monitor to monitor some website. 27 00:03:56,640 --> 00:04:04,480 You need to set a name of the monitor, location, when your agent will be run to make a request to the site. 28 00:04:04,480 --> 00:04:09,280 And look, you can define some script. 29 00:04:09,440 --> 00:04:13,260 So it's also interesting. 30 00:04:13,300 --> 00:04:18,480 We have some script editor and we can pass own script here. 31 00:04:18,480 --> 00:04:23,540 Let's check the documentation, how such scripts look like. 32 00:04:24,000 --> 00:04:40,420 It's one of the examples, a simple script that's just open the example.org and check some element, text in the element and check that it equals to example domain. 33 00:04:40,820 --> 00:04:45,140 Okay, but it looks like a JavaScript , right? 34 00:04:45,140 --> 00:04:57,040 So if you just replace the body of this method and write, for example, to run a reverse shell, probably we can execute it. 35 00:04:57,600 --> 00:05:02,120 But exactly this code doesn't work because of a query. 36 00:05:02,120 --> 00:05:09,480 If you did some JavaScript CTF, you probably already know about this problem. 37 00:05:09,480 --> 00:05:15,260 You don't have a query in the scope, but you can bypass it for Node.js, for example. 38 00:05:15,260 --> 00:05:34,400 You can use load, underscore load method in the process main module to load any module, including child process, and you can run reverse shell. 39 00:05:34,560 --> 00:05:36,260 Let's see on the demo. 40 00:05:36,260 --> 00:05:45,500 We paste this code, click on the run and see you have already a connection from the reverse shell, from the agent. 41 00:05:45,500 --> 00:05:53,680 We see the file system and the user on this agent, on the harebit engine. 42 00:05:53,680 --> 00:06:03,140 You can see the harebit name of the user that we get that monitor any websites. 43 00:06:03,280 --> 00:06:10,240 Okay, this remote code execution, I think, is the simplest remote code execution that I reported. 44 00:06:11,220 --> 00:06:15,040 And in the last August, I made a report. 45 00:06:15,040 --> 00:06:32,460 The program, the company company already finishing, and it was the last days, and also the rules for the post-exploitation was not clear, and I decided, okay, I found it, I will report it, and let's see. 46 00:06:32,460 --> 00:06:40,620 We discount this report with Elastic security team in September, and in October they came back with the feedback. 47 00:06:41,560 --> 00:06:53,280 Synthetics by design is arbitrary code execution as a service in which Elastic deploys a number of security features to prevent exploitation. 48 00:06:53,280 --> 00:07:01,920 For successful exploit, someone would have to do something beyond getting a shell. 49 00:07:02,520 --> 00:07:17,520 Okay, so it makes sense if you make such feature, probably you already isolated this code on the container, and what they actually did. 50 00:07:18,840 --> 00:07:30,160 Okay, we discussed it, and I decided to continue exploitation of this container and see what I can get in the end. 51 00:07:30,740 --> 00:07:32,280 Where we are? 52 00:07:32,320 --> 00:07:35,580 This is an isolated container. 53 00:07:35,580 --> 00:07:42,240 After some investigation, I see that it's run in the Kubernetes cluster on Google Cloud Platform. 54 00:07:42,420 --> 00:07:51,400 Probably it was managed Kubernetes cluster, but the details are not important here. 55 00:07:51,960 --> 00:08:10,760 The first idea was to try to affect another client on the same container, but the container was running paired tasks, so we don't have any persistent state after the execution of the code that we injected. 56 00:08:10,760 --> 00:08:15,660 The container just restarted and no any client there. 57 00:08:16,000 --> 00:08:19,400 Okay, try to escape the container. 58 00:08:19,500 --> 00:08:27,440 It was secure configuration of up-to-date Kubernetes version, so it looks like not possible. 59 00:08:27,440 --> 00:08:31,160 Access to Google Cloud Platform metadata API? 60 00:08:31,160 --> 00:08:37,900 No again, so I think it's one of the first thing that they checked. 61 00:08:37,900 --> 00:08:50,720 It looks like unexploitable case, so we found remote code execution, but we cannot bypass the container isolation. 62 00:08:55,290 --> 00:09:03,630 I continue investigated some things and found the same docker container for private Kubernetes cluster. 63 00:09:03,630 --> 00:09:08,090 It means we can run it in the own Kubernetes cluster. 64 00:09:08,090 --> 00:09:16,370 It makes threat model a little bit weak, because we don't have exactly the same settings. 65 00:09:16,370 --> 00:09:34,230 We can say if we can escape from container or affect some another services or features in the default configuration on the Kubernetes, it looks like a possible exploitation flaw. 66 00:09:34,430 --> 00:09:42,330 And I also found that this container already have natural capabilities. 67 00:09:42,950 --> 00:09:49,130 It means that we can send any raw package to the network. 68 00:09:49,170 --> 00:09:50,910 Any package. 69 00:09:50,910 --> 00:09:53,610 It means we can emulate any protocol. 70 00:09:53,610 --> 00:09:59,110 For example, we can say send ARP packages. 71 00:09:59,110 --> 00:10:06,250 I think you already understand my idea and get what I want to do. 72 00:10:06,250 --> 00:10:14,990 Yeah, let's try to do ARP spoofing attack in 2024. 73 00:10:15,230 --> 00:10:25,790 So, ARP protocol allows to send some request to match IP address to the MAC address of any computer. 74 00:10:25,790 --> 00:10:47,390 And for example, in the network, when Bob tried to connect to Mary and communicate by IP transport, we can say to Bob that Mary is an attacker computer and say to Mary that Bob is an attacker computer and intercept the traffic between these two machines. 75 00:10:47,390 --> 00:10:55,010 It's a simple, old, very old, old school attack. 76 00:10:55,070 --> 00:10:58,210 But what about Kubernetes? 77 00:10:58,870 --> 00:11:12,690 I already wanted to start to run Wireshark and investigate the traffic, what actually happens, and found a nice blog post from Nir Chacko. 78 00:11:12,690 --> 00:11:17,930 Attacking Kubernetes clusters throws your network plumbing. 79 00:11:18,090 --> 00:11:35,250 So, he described the same idea of ARP spoofing to replace DNS server that's available in the Kubernetes to the own DNS server. 80 00:11:35,250 --> 00:11:51,310 And in this case, you can hijack any DNS request and send the request to the own web server instead of any available web server. 81 00:11:51,430 --> 00:12:00,690 And the crucial component of this attack, this bridge, let's call it BR0 on this figure. 82 00:12:00,690 --> 00:12:20,710 So, this bridge supports ARP protocol and has own ARP table that has any IP addresses and MAC addresses of the ports that run on the same node. 83 00:12:20,710 --> 00:12:23,490 So, it looks like very restricted attack. 84 00:12:23,490 --> 00:12:28,010 We can affect only ports on the same node. 85 00:12:28,290 --> 00:12:38,870 But default network interface in Kubernetes allows us to exploit ARP spoofing. 86 00:12:38,870 --> 00:12:40,830 Let's see on the demo. 87 00:12:41,290 --> 00:12:48,750 So, the one of the idea to start this attack was to use some cloud environment. 88 00:12:48,750 --> 00:12:54,710 I check Microsoft Azure and they allow us to use default Kubernetes. 89 00:12:54,710 --> 00:12:56,630 So, you can see the console. 90 00:12:56,630 --> 00:13:05,310 I already started attacker at victim ports and you can see that it's run on the same node in Kubernetes cluster. 91 00:13:05,310 --> 00:13:12,150 It allows us to perform attack from the attacker to affect the victim. 92 00:13:12,390 --> 00:13:20,610 First of all, we install proof of concept and some additional proof of concept script and some additional dependencies on the attacker. 93 00:13:20,610 --> 00:13:23,910 At the same time, we open the victim machine. 94 00:13:23,950 --> 00:13:29,330 We will install KURL there to emulate some web requests. 95 00:13:29,410 --> 00:13:31,950 Yeah, it's the console of the victim machine. 96 00:13:33,910 --> 00:13:42,530 And after installation of the KURL, the idea that we try to get the data from the google.com. 97 00:13:42,530 --> 00:13:51,290 After that, we start the attack and try to use own web server instead of the google.com. 98 00:13:51,290 --> 00:13:57,630 So, now you see the usual, the valid response from the google.com. 99 00:13:57,630 --> 00:13:59,950 And this is the attacker port. 100 00:13:59,950 --> 00:14:09,050 We run DNS spoofing proof of concept that I found in that blog post. 101 00:14:09,050 --> 00:14:14,150 I also improved it to use it on Azure. 102 00:14:14,150 --> 00:14:17,250 By default, it did not support it. 103 00:14:17,670 --> 00:14:20,550 And we start the attack here. 104 00:14:20,550 --> 00:14:31,650 And now we do the same request and you can see that our web server responds instead of the google.com. 105 00:14:31,650 --> 00:14:38,590 So, it's a classic DNS spoofing that we demonstrated in this demo. 106 00:14:38,590 --> 00:14:43,850 And I updated the report to demonstrate this attack. 107 00:14:43,850 --> 00:14:46,770 Elastic changed the report. 108 00:14:47,330 --> 00:14:59,950 And a few days, one week ago, they rewarded with almost 4000 bounties for this attack. 109 00:15:00,510 --> 00:15:06,230 Not bad for an unexploitable case and a very exotic attack. 110 00:15:06,330 --> 00:15:16,450 And it was a very interesting experience to try to use ARP spoofing in the real bug bounty case. 111 00:15:17,530 --> 00:15:24,950 So, I missed a lot of the details of network configuration of the Kubernetes. 112 00:15:24,950 --> 00:15:29,390 If you're interested, you can read the blog post. 113 00:15:29,390 --> 00:15:30,710 I'm sure you'll enjoy it. 114 00:15:30,710 --> 00:15:37,650 It's one of the best blog posts about Kubernetes networking that I've read. 115 00:15:38,530 --> 00:15:41,460 All links will be in the end of the presentation. 116 00:15:42,260 --> 00:15:45,800 Let's see on another attacks. 117 00:15:46,120 --> 00:15:56,440 The main topic in my PhD studies for last two years was server-side prototype pollution vulnerabilities. 118 00:15:56,440 --> 00:15:59,060 We published three papers about that. 119 00:15:59,060 --> 00:16:22,280 When we implemented some toolchain to find prototype pollution vulnerabilities, we found prototype pollution gadgets that allow us to exploit prototype pollution and achieve a remote code execution attack in Node.js itself, in Deno, in npm packages. 120 00:16:22,280 --> 00:16:25,880 If you're interested in this, read the papers. 121 00:16:25,880 --> 00:16:42,260 And I also give us some short introduction of our research and prototype attack the last year on DEF CON and many technical details of the gadgets that we found in Node.js itself in another talk on the Black Hat Asia. 122 00:16:42,780 --> 00:16:52,980 And today I will tell you a short introduction what the prototype pollution is, just to be on the same page. 123 00:16:53,800 --> 00:17:01,060 So, let's assume that we have a Node.js application. 124 00:17:01,240 --> 00:17:03,420 We run some web server. 125 00:17:03,420 --> 00:17:04,980 You see index.js. 126 00:17:04,980 --> 00:17:11,040 It's very simple web server that can handle two requests, update and backup. 127 00:17:11,040 --> 00:17:14,100 For update request, we provide some arguments. 128 00:17:14,100 --> 00:17:18,320 For backup request, we just trigger the backup of database. 129 00:17:18,860 --> 00:17:20,700 Okay, it's our setup. 130 00:17:21,640 --> 00:17:24,800 So, we have attacker in this thread model. 131 00:17:24,800 --> 00:17:29,420 It's any user that can send any request to the web server. 132 00:17:29,640 --> 00:17:44,820 And if the attacker send such request with these parameters, and we have a handler for update request, the handler in the first line create empty object. 133 00:17:45,100 --> 00:18:02,520 And runtime, in the same time, create empty object with the built-in property proto that points to the object prototype that contains a lot of functions like toString, valueOf, that we can reuse. 134 00:18:02,520 --> 00:18:11,120 So, it's like inheritance implementation for JavaScript and TypeScript. 135 00:18:11,500 --> 00:18:23,800 In the second line, you can see that we read the property from the object that specified in the argument org. 136 00:18:23,800 --> 00:18:25,240 This is the proto. 137 00:18:25,240 --> 00:18:30,140 It means we get a reference to this object. 138 00:18:30,140 --> 00:18:44,120 In the last line of the code, we assign a value from details to another property of the prototype, the property shell. 139 00:18:44,400 --> 00:18:53,140 So, we read the proto, we assign a value calc in the shell, and we get it in the prototype. 140 00:18:53,300 --> 00:19:04,940 This code pattern is called prototype pollution, because we pollute, we add some property or replace some property in the object prototypes itself. 141 00:19:05,300 --> 00:19:08,820 Let's assume that we have such code for backup handler. 142 00:19:08,820 --> 00:19:24,580 This function tries to run the backup script, this one, and use the shell from the options and default shell if the shell is undefined. 143 00:19:24,580 --> 00:19:30,220 In our case, shell is undefined and it will be used as default shell. 144 00:19:30,220 --> 00:19:33,320 Okay, it's a very common pattern in the JavaScript. 145 00:19:33,320 --> 00:19:40,620 But what actually happens if we trigger this handler after prototype pollution? 146 00:19:40,620 --> 00:19:46,100 We create options that point to the same object prototype. 147 00:19:46,280 --> 00:19:58,380 We read the shell and the runtime lookups for the shell property in the prototype chain and reads the polluted shell property. 148 00:19:58,380 --> 00:20:11,100 And the exec command runs a calculator because we get a calc in the shell, in the cmd variable at the end. 149 00:20:11,100 --> 00:20:22,360 Okay, this is prototype pollution and remote code execution that we can achieve by prototype pollution in the end. 150 00:20:22,420 --> 00:20:28,640 And this code pattern is called a gadget, prototype pollution gadget. 151 00:20:28,880 --> 00:20:31,660 Let's start the story too. 152 00:20:32,300 --> 00:20:38,720 About prototype pollution and remote code execution based on that. 153 00:20:39,480 --> 00:20:42,480 So, I tried to use SilentSpring. 154 00:20:42,480 --> 00:20:55,380 It's our first research on the prototype pollution, the toolchain that we implemented based on the CodeQL to detect prototype pollution vulnerabilities in Kibana. 155 00:20:55,380 --> 00:21:05,940 So, the first attempt CodeQL terminated by timeout because of too large database that I tried to analyze. 156 00:21:06,460 --> 00:21:15,720 I implemented a script that rerun CodeQL for subfolders if the analysis is timed out. 157 00:21:15,720 --> 00:21:22,100 And this script reported 77 cases in the application itself. 158 00:21:22,100 --> 00:21:25,880 I will add the script in the same repository if you're interested. 159 00:21:25,880 --> 00:21:27,140 You can find it there. 160 00:21:27,140 --> 00:21:29,400 Try to play with that. 161 00:21:29,680 --> 00:21:39,380 So, we get 44 server-side cases including some false positive testing code. 162 00:21:39,380 --> 00:21:48,680 And today I will show you a couple cases from these that was exploited. 163 00:21:49,160 --> 00:21:58,300 So, the first case, this is prototype pollution pattern that was detected in the config parser. 164 00:21:58,300 --> 00:22:08,820 The config parser take some YAML document, main config of the Kibana when you restart Kibana. 165 00:22:08,820 --> 00:22:23,880 And after the parsing it also tried to expand dot separated names in this YAML config to such nested object. 166 00:22:23,880 --> 00:22:31,500 So, a.b becomes a object that contains nested object with property b. 167 00:22:31,500 --> 00:22:35,060 So, very simple idea. 168 00:22:35,540 --> 00:22:39,940 But the code that does it is vulnerable to prototype pollution. 169 00:22:39,940 --> 00:22:52,620 First of all, I take a key like a.b in our case, split it by the dot, so we get array a and b, and call walk function. 170 00:22:52,700 --> 00:23:07,740 In the walk function, we get the first key, a, and read the value of the a, and call walk recursively. 171 00:23:07,800 --> 00:23:24,940 So, okay, and for the next case, if we have a last case, vary the b case, it becomes an empty array now, and we assign the value to the b. 172 00:23:24,940 --> 00:23:37,920 So, it's absolutely the same pattern that I've shown you before in the prototype pollution, and such payload leads to prototype pollution vulnerabilities in this case. 173 00:23:37,920 --> 00:23:47,500 So, we can add any property, and we control the value of the polluted property. 174 00:23:47,500 --> 00:23:48,580 Good. 175 00:23:48,580 --> 00:24:05,240 I found it, and also Elastic Cloud Enterprise, it's an on-premise version of the ILCA stack, provides web UI to edit Kibana YAML. 176 00:24:05,240 --> 00:24:16,720 It means we can edit it with admin privileges, so we need some high-level privileges, but we can do this attack through the web. 177 00:24:20,580 --> 00:24:28,660 Attacker can store any payload in Kibana and restart Kibana to load all configs. 178 00:24:28,660 --> 00:24:43,020 The problem here is that Kibana crashes immediately after prototype pollution occurs, because other code does not expect some properties, some additional properties, than the object prototypes. 179 00:24:43,380 --> 00:24:54,480 It happens before we get started the web server. 180 00:24:54,480 --> 00:25:03,920 We cannot send any other request to trigger any gadget. 181 00:25:03,920 --> 00:25:25,480 It means we can only control the prototype pollution point, and it's some difficulty with that, because nothing dangerous happened between restarting Kibana and prototype pollution triggering, and the crash of the application. 182 00:25:25,790 --> 00:25:42,470 It just tried to load some modules, tried to load configs, prepare some caches, and nothing dangerous like starting a new process or evaluate some function. 183 00:25:42,980 --> 00:25:56,240 And the attack looks like unexploitable, so you can achieve denial of the service, if you are an administrator and you restart Kibana, it's nothing actually. 184 00:25:56,240 --> 00:25:59,320 What we can do in this case? 185 00:25:59,640 --> 00:26:04,320 Let's see what Kibana did before the crash. 186 00:26:04,320 --> 00:26:07,500 It tried to load some modules. 187 00:26:07,500 --> 00:26:30,860 It would be nice if we have a gadget in the require call that loads modules, because the require will read the code that it loads from the module, and if we can replace the module to the own code, we can get remote code execution before the crash. 188 00:26:30,960 --> 00:26:43,320 That's a nice idea, and we already found the gadget in the require when we worked on the first paper, Silent Spring, that I mentioned today. 189 00:26:44,340 --> 00:26:52,740 And yeah, actually we can run any malicious JS file through the prototype pollution. 190 00:26:53,000 --> 00:26:57,660 Let's see on the require gadget how it works. 191 00:26:57,660 --> 00:27:11,040 So let's assume that we have the same prototype pollution from the first example, and we can trigger and pollute some property main in the object prototype. 192 00:27:11,040 --> 00:27:19,940 Oh yeah, we can pollute one more property environment and trigger or handle backup. 193 00:27:19,940 --> 00:27:33,340 Handle backup has only one line, a require to the some module, and when require tries to load the module bytes, it has such code. 194 00:27:33,340 --> 00:27:37,980 It does loadConfig, and for loadConfig 195 00:27:42,160 --> 00:27:52,080 this function returns the parsed JSON, like this one, with all properties from the config. 196 00:27:52,340 --> 00:28:00,080 But some package JSON, the configuration file, does not have main. 197 00:28:00,080 --> 00:28:18,220 In this case, the default behavior should be getIndex.js, but we have main.read expression here, and it tries to read the main that we polluted before from the object prototype. 198 00:28:18,220 --> 00:28:26,520 It means we can read any JS file that exists in the system. 199 00:28:26,740 --> 00:28:33,300 And it's some limitation for the require gadget, but we combine it with another object. 200 00:28:33,300 --> 00:28:44,140 If we can find some JS file that, for example, start new process, do child process exec, we can combine it with another function. 201 00:28:44,140 --> 00:28:52,200 And npm.js, actually, when we load it, starts new process. 202 00:28:52,440 --> 00:28:56,180 It does such code. 203 00:28:56,180 --> 00:29:05,700 It executes spawn function with two arguments, non-executable process and some arguments. 204 00:29:05,840 --> 00:29:13,520 And the implementation on the spawn function, we have another gadget. 205 00:29:13,520 --> 00:29:20,900 Let's try to read property shell and env from the options. 206 00:29:21,580 --> 00:29:26,400 And options was empty, undefined. 207 00:29:26,400 --> 00:29:29,460 It means it becomes an empty object. 208 00:29:29,860 --> 00:29:35,120 And when we read env here, we read it from the empty object. 209 00:29:36,680 --> 00:29:40,640 And we actually read the value from the prototype. 210 00:29:41,040 --> 00:29:43,680 Okay, that's the idea of the require gadget. 211 00:29:44,140 --> 00:29:54,600 The problem is that this gadget has been fixed by Node.js team, and I already knew this when found it. 212 00:29:54,760 --> 00:30:00,300 And in parallel, I worked on some another tool, some dynamic analysis, it's called gHunters. 213 00:30:00,300 --> 00:30:09,660 I described it in the last paper that we published, and that will be presented in Usenix security in a few days, next week. 214 00:30:12,800 --> 00:30:20,160 And working on that tool, I investigated require gadget and the fix. 215 00:30:20,160 --> 00:30:23,400 And the require gadget looks like this. 216 00:30:23,400 --> 00:30:27,800 So the code that we already saw, we have a load config. 217 00:30:27,800 --> 00:30:42,440 So we have such code that read package JSON, try to parse it by JSON parse, and JSON parse return object that has a object prototype in the chain. 218 00:30:42,440 --> 00:30:55,400 And it tried to read the main property, and at that moment, it can read the polluted property if we already polluted main in the object prototype. 219 00:30:55,400 --> 00:30:57,380 So this is a gadget. 220 00:30:57,380 --> 00:30:59,660 And Node.js team fixed it. 221 00:30:59,660 --> 00:31:01,760 So the fix looks like this. 222 00:31:01,760 --> 00:31:14,280 They wrote some filter on properties that actually read this list of properties exactly from the object itself, does not check it in the prototype chain. 223 00:31:14,280 --> 00:31:21,480 So it means we cannot affect reading on the main property anymore after that fix. 224 00:31:22,000 --> 00:31:33,020 But at some moment, gHunters start detect the require gadget again in the new version of the Node.js. 225 00:31:33,500 --> 00:31:46,340 And I double-checked all experiments, and I see that require detected again and again for some cases, and I start to see on this fix more closely. 226 00:31:46,340 --> 00:31:50,300 And it has another gadget. 227 00:31:50,720 --> 00:31:52,760 I will zoom in. 228 00:31:53,100 --> 00:31:57,380 You can see exactly in these slides. 229 00:31:57,880 --> 00:32:00,600 We have this if statement. 230 00:32:00,600 --> 00:32:05,260 If JSON is undefined, the function returns false. 231 00:32:05,260 --> 00:32:09,760 The function load config returns false. 232 00:32:09,900 --> 00:32:15,720 ParseJSON becomes a false, and we try to read the main from the false. 233 00:32:15,720 --> 00:32:24,940 JsRuntime in this case tried to box primitive type, Boolean type, and add object prototype in the prototype chain. 234 00:32:24,940 --> 00:32:32,440 And we again can read the main, but from the false, actually. 235 00:32:32,440 --> 00:32:58,680 So, it's a new gadget, and when I figure out it, so I add the payload for the require in the original Kibana YAML file, and it allows us to exploit this prototype solution and achieve remote code execution. 236 00:32:58,680 --> 00:33:10,460 So, this vulnerability did not get high score because it required high privileged user, but still it's almost 4,000 bounces. 237 00:33:11,860 --> 00:33:18,940 Let's see on the next story about prototype pollution and remote code execution. 238 00:33:18,940 --> 00:33:34,280 Another case that was reported by our script, SilentSpring Node.js queries, was a prototype pollution in some handler, web handler. 239 00:33:34,320 --> 00:33:58,120 So, it's very simple prototype pollution, you can see we create empty object, it means this object has the prototype points to the object prototype, and we have this line of the code, if namespace is __prot__, and the key is the key controlled by the attacker, 240 00:33:58,120 --> 00:34:07,460 that's so, and the value also can be controlled by the attacker, it means we can add any key with controlled value to the object prototype. 241 00:34:07,480 --> 00:34:12,080 Very simple, you don't even need a script to find it. 242 00:34:14,860 --> 00:34:22,220 Okay, so when I started working on this script, the question, we need to find a gadget. 243 00:34:22,220 --> 00:34:33,500 We worked on the Dusty, it's another project that detected 49 gadgets leading to remote code execution in NPM packages. 244 00:34:33,500 --> 00:34:54,800 We took a thousand popular NPM packages and run dynamic analysis that try to instrument the package and investigate what the flows can lead to, read the properties from the prototype and can lead to remote code execution in the end. 245 00:34:54,800 --> 00:35:08,100 And one of the package that has a gadget was NotMailer, and Kibana uses this gadget to send emails. 246 00:35:08,580 --> 00:35:19,500 So, the idea, we have some SendMailer transport that's optional, but you also can pollute some option to enable it. 247 00:35:20,230 --> 00:35:28,280 And it has some options that's by default empty. 248 00:35:29,640 --> 00:35:48,400 And if we have path in the options, the path of the run program that should be SendMailer and args will be read from the options object and prototype, in our case, and send to the spawn function. 249 00:35:48,700 --> 00:35:59,380 Okay, so it means we can inject any command and execute, start any process on the victim machine. 250 00:36:03,210 --> 00:36:11,030 But the problem is that DeleteHandler crashes the application in less than one second after the prototype pollution. 251 00:36:11,030 --> 00:36:23,470 So, it also becomes immediately, because some code in the end of the handler, of the same handler, does not expect the property, some additional property, and throw exception that's not handled correctly. 252 00:36:23,490 --> 00:36:25,770 And we get denial of the service. 253 00:36:25,770 --> 00:36:30,730 That's not much interesting for such vulnerability. 254 00:36:31,070 --> 00:36:33,790 So, it looks like unexploitable. 255 00:36:33,790 --> 00:36:40,770 Denial of the service, I could not say that it's an interesting impact that we want to show. 256 00:36:40,830 --> 00:37:08,550 But if we try to emulate some race condition by sending a lot of gadget-triggered requests in parallel and one prototype-pollution-triggered request between them, we can try to get the situation that gadget-triggered request will be in the small time exactly after the prototype pollution request. 257 00:37:08,870 --> 00:37:14,270 Let me explain it based on the script, on the exploit script. 258 00:37:14,610 --> 00:37:23,870 So, we can implement such race condition exploit on the bash or any other language. 259 00:37:23,990 --> 00:37:41,450 So, the idea, we send 20 requests that trigger a gadget first, and after that we send the request that trigger a prototype pollution, and after that we send 50 requests that trigger a gadget again. 260 00:37:41,450 --> 00:37:51,550 And this ampersand means that all of these commands will be executed in parallel. 261 00:37:52,510 --> 00:37:55,210 So, we send a lot of requests. 262 00:37:55,310 --> 00:37:58,510 Let's see how it works in the demo. 263 00:37:59,670 --> 00:38:23,350 So, I run it from my laptop, through the Wi-Fi, and first of all I start some payload that created an email connector that we need to know to send the request to trigger a non-mailer package. 264 00:38:23,350 --> 00:38:36,210 So, we change in our exploit SH script this connector and run the exploit script. 265 00:38:36,210 --> 00:38:42,170 And I started this web page to see which requests we will get. 266 00:38:42,170 --> 00:38:50,010 So, I did not run a reverse shell, I just sent a request to this web page. 267 00:38:50,010 --> 00:38:55,910 You can see that five requests will be successfully received. 268 00:38:55,910 --> 00:39:07,230 It means that five cases will be for so short time that were executed before the crash. 269 00:39:07,810 --> 00:39:10,990 And this attack was run from the Wi-Fi. 270 00:39:10,990 --> 00:39:18,990 You can get more interesting, I guess, if you run it on the same server, in the same network, like in cloud or some cases. 271 00:39:19,490 --> 00:39:27,490 Okay, this attack gave me the biggest bounty that I got from the Elastic. 272 00:39:27,890 --> 00:39:35,370 And not only this prototype pollution that I reported was exploited, this case was closed a few days ago. 273 00:39:35,370 --> 00:39:41,090 I did not have time to edit in the presentation and also we don't have much time. 274 00:39:41,250 --> 00:39:49,710 And another case that was reported also, this is a chain that leads to account takeover. 275 00:39:49,850 --> 00:39:54,190 Maybe I will do it in the next presentation, let's see. 276 00:39:55,410 --> 00:40:10,830 As a bonus part, I want to explain some advanced technique for the prototype pollution when we pollute the objects in the prototype, not the prototype itself. 277 00:40:12,590 --> 00:40:24,410 When I understood this trick, it allows me to report a few new vulnerabilities that I actually thought were unexploitable before. 278 00:40:24,530 --> 00:40:26,690 Let's see the example. 279 00:40:26,970 --> 00:40:34,290 We have a prototype pollution from our original example, very simple code that we already know. 280 00:40:34,290 --> 00:40:35,630 We have a gadget. 281 00:40:35,630 --> 00:40:44,470 The gadget reads the options and checks the property runner. 282 00:40:44,470 --> 00:40:49,070 The options are empty object by default. 283 00:40:49,070 --> 00:40:58,890 If the runner is defined, we read paths and arcs and spawn a new process for that. 284 00:40:58,890 --> 00:41:05,010 It means we need to pollute a runner in the object prototype, like this. 285 00:41:05,770 --> 00:41:12,670 We add a runner with this object as a value. 286 00:41:14,070 --> 00:41:20,430 And after that, we trigger a handleback, of course, to trigger a gadget. 287 00:41:21,790 --> 00:41:28,870 What if this prototype pollution does not allow us to control the value? 288 00:41:28,870 --> 00:41:34,770 For example, it's some object with a predefined identifier and other properties. 289 00:41:34,770 --> 00:41:36,410 We cannot control it. 290 00:41:37,310 --> 00:41:40,010 Or it's an empty array. 291 00:41:40,010 --> 00:41:44,170 The array does not have any properties, right? 292 00:41:45,910 --> 00:41:50,110 It looks like unexploitable, at least for such a gadget. 293 00:41:50,110 --> 00:41:52,830 Maximum, we can get the denial of the service. 294 00:41:52,830 --> 00:41:58,010 But what if we have such code? 295 00:41:58,250 --> 00:42:01,750 It's a fixed, I call it a fixed prototype pollution. 296 00:42:01,750 --> 00:42:10,330 We check that arc does not equal a prototype and execute this code only for that. 297 00:42:10,330 --> 00:42:20,650 It cannot lead to prototype pollution, but we can combine these two code snippets. 298 00:42:20,650 --> 00:42:27,530 So it's absolutely safe code separately from this one. 299 00:42:27,530 --> 00:42:30,690 But what we can do together with these components? 300 00:42:30,690 --> 00:42:33,790 First of all, we need to pollute the prototype. 301 00:42:33,790 --> 00:42:42,050 We add a runner with a value of empty array in the prototype to trigger handle update. 302 00:42:42,050 --> 00:42:43,270 Good. 303 00:42:43,430 --> 00:42:55,150 After that, we trigger this save function with arc that has a runner value. 304 00:42:55,150 --> 00:43:00,930 It means we will try to read the runner from the prototype at this step. 305 00:43:00,930 --> 00:43:04,130 And we pollute the runner after that. 306 00:43:04,130 --> 00:43:12,390 We can't feel any additional properties in the runner by this call and the second call. 307 00:43:12,390 --> 00:43:15,970 And after that, we trigger backup handler. 308 00:43:15,970 --> 00:43:25,690 It gives us the remote code execution again by this chain of the calls, the web request that triggers such functions. 309 00:43:26,190 --> 00:43:48,070 If you think that it's unlikely that we find some fixed prototype pollution case and unfixed prototype pollution case in the same application, I can say you that, for example, merge functions are very typical in the fixed prototype pollutions. 310 00:43:48,070 --> 00:43:59,270 Like a merge function of the very popular package lodash allows us to pollute a runner with such payload. 311 00:43:59,270 --> 00:44:03,410 And you can find lodash merge calls in many applications. 312 00:44:03,410 --> 00:44:07,930 And that's important that these two parts can be unrelated. 313 00:44:07,930 --> 00:44:15,330 It could be absolutely different features in the application. 314 00:44:16,370 --> 00:44:20,790 So yeah, use it for fun and bounces. 315 00:44:21,290 --> 00:44:31,030 In conclusion, in this talk, I wanted to show the cases that look unexploitable and say, don't give up. 316 00:44:31,150 --> 00:44:36,890 Try to achieve remote code execution even when it looks impossible. 317 00:44:37,310 --> 00:44:46,430 If you're interested in prototype pollution research, check out our papers, SilentSpring, G-Hunter and Dusty that I mentioned in the talk. 318 00:44:46,730 --> 00:44:52,890 Check out the collection of server-side prototype pollution gadgets that we shared. 319 00:44:52,890 --> 00:45:02,170 This is a gadget in Node.js itself and dino and npm packages that I also use in my bug bounty cases. 320 00:45:02,690 --> 00:45:13,610 If you're interested in networking attacks and this story about ARP spoofing, I highly recommend to read this blog post. 321 00:45:13,610 --> 00:45:18,370 ARP spoofing in Kubernetes is not so easy as it looks like. 322 00:45:18,370 --> 00:45:27,510 It also has some difficulties with the IP table rules that were bypassed by this talk. 323 00:45:27,510 --> 00:45:30,570 Check out this blog post. 324 00:45:30,570 --> 00:45:32,930 I think you'll enjoy it. 325 00:45:32,930 --> 00:45:38,370 And follow me on Twitter if you're interested in some details of code reuse attacks. 326 00:45:38,370 --> 00:45:43,250 I want to share some findings that I am now including in my thesis. 327 00:45:43,850 --> 00:45:46,550 And thank you for your attention. 328 00:45:47,730 --> 00:45:48,890 Enjoy DEF CON. 329 00:45:48,890 --> 00:45:49,510 Bye-bye.