My name is Mikhail Shcherbakov and today I want to tell you a few stories about Kibana backbounds. 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. A few words about me. 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. My main research interest is static code analysis, dynamic program analysis, and language-based security in general. I participated in bug bounty programs of Microsoft, GitHub, some open source projects, and Elastic. And today we'll talk about Elastic bug bounty program, namely Kibana bug bounty program. Who is Kibana? Kibana is a part of popular ILCA stack that includes Elasticsearch, Logstash, and Kibana. It's used for data visualization and real-time analysis. So Kibana is an open source project. All code is available and it's possible to analyze statically or just make a manual review. It's definitely a large project, probably one of the largest. It's written in TypeScript and they have an interesting bug bounty program with 3,000-7,000 rewards for critical vulnerabilities. All of that makes Kibana a great target for bug hunters. And I contributed a lot last year in this program. And before starting to tell about vulnerabilities in Kibana, I would like to say my thanks to the Elastic security team. In my practice, in my experience, it's one of the best bug bounty programs that's managed by Elastic security team. 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. So story number one, how it all began. 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. 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. Okay, let's see how this feature looks like in practice. It is a main page of the Kibana. You can find synthetics, the feature that we discussed. You can create a new monitor to monitor some website. You need to set a name of the monitor, location, when your agent will be run to make a request to the site. And look, you can define some script. So it's also interesting. We have some script editor and we can pass own script here. Let's check the documentation, how such scripts look like. 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. Okay, but it looks like a JavaScript, right? So if you just replace the body of this method and write, for example, to run a reverse shell, probably we can execute it. But exactly this code doesn't work because of a query. If you did some JavaScript CTF, you probably already know about this problem. You don't have a query in the scope, but you can bypass it for Node.js, for example. 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. Let's see on the demo. We paste this code, click on the run and see you have already a connection from the reverse shell, from the agent. We see the file system and the user on this agent, on the harebit engine. You can see the harebit name of the user that we get that monitor any websites. Okay, this remote code execution, I think, is the simplest remote code execution that I reported. And in the last August, I made a report. 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. We discount this report with Elastic security team in September, and in October they came back with the feedback. Synthetics by design is arbitrary code execution as a service in which Elastic deploys a number of security features to prevent exploitation. For successful exploit, someone would have to do something beyond getting a shell. Okay, so it makes sense if you make such feature, probably you already isolated this code on the container, and what they actually did. Okay, we discussed it, and I decided to continue exploitation of this container and see what I can get in the end. Where we are? This is an isolated container. After some investigation, I see that it's run in the Kubernetes cluster on Google Cloud Platform. Probably it was managed Kubernetes cluster, but the details are not important here. 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. The container just restarted and no any client there. Okay, try to escape the container. It was secure configuration of up-to-date Kubernetes version, so it looks like not possible. Access to Google Cloud Platform metadata API? No again, so I think it's one of the first thing that they checked. It looks like unexploitable case, so we found remote code execution, but we cannot bypass the container isolation. I continue investigated some things and found the same docker container for private Kubernetes cluster. It means we can run it in the own Kubernetes cluster. It makes threat model a little bit weak, because we don't have exactly the same settings. 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. And I also found that this container already have natural capabilities. It means that we can send any raw package to the network. Any package. It means we can emulate any protocol. For example, we can say send ARP packages. I think you already understand my idea and get what I want to do. Yeah, let's try to do ARP spoofing attack in 2024. So, ARP protocol allows to send some request to match IP address to the MAC address of any computer. 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. It's a simple, old, very old, old school attack. But what about Kubernetes? I already wanted to start to run Wireshark and investigate the traffic, what actually happens, and found a nice blog post from Nir Chacko. Attacking Kubernetes clusters throws your network plumbing. So, he described the same idea of ARP spoofing to replace DNS server that's available in the Kubernetes to the own DNS server. 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. And the crucial component of this attack, this bridge, let's call it BR0 on this figure. 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. So, it looks like very restricted attack. We can affect only ports on the same node. But default network interface in Kubernetes allows us to exploit ARP spoofing. Let's see on the demo. So, the one of the idea to start this attack was to use some cloud environment. I check Microsoft Azure and they allow us to use default Kubernetes. So, you can see the console. I already started attacker at victim ports and you can see that it's run on the same node in Kubernetes cluster. It allows us to perform attack from the attacker to affect the victim. First of all, we install proof of concept and some additional proof of concept script and some additional dependencies on the attacker. At the same time, we open the victim machine. We will install KURL there to emulate some web requests. Yeah, it's the console of the victim machine. And after installation of the KURL, the idea that we try to get the data from the google.com. After that, we start the attack and try to use own web server instead of the google.com. So, now you see the usual, the valid response from the google.com. And this is the attacker port. We run DNS spoofing proof of concept that I found in that blog post. I also improved it to use it on Azure. By default, it did not support it. And we start the attack here. And now we do the same request and you can see that our web server responds instead of the google.com. So, it's a classic DNS spoofing that we demonstrated in this demo. And I updated the report to demonstrate this attack. Elastic changed the report. And a few days, one week ago, they rewarded with almost 4000 bounties for this attack. Not bad for an unexploitable case and a very exotic attack. And it was a very interesting experience to try to use ARP spoofing in the real bug bounty case. So, I missed a lot of the details of network configuration of the Kubernetes. If you're interested, you can read the blog post. I'm sure you'll enjoy it. It's one of the best blog posts about Kubernetes networking that I've read. All links will be in the end of the presentation. Let's see on another attacks. The main topic in my PhD studies for last two years was server-side prototype pollution vulnerabilities. We published three papers about that. 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. If you're interested in this, read the papers. 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. And today I will tell you a short introduction what the prototype pollution is, just to be on the same page. So, let's assume that we have a Node.js application. We run some web server. You see index.js. It's very simple web server that can handle two requests, update and backup. For update request, we provide some arguments. For backup request, we just trigger the backup of database. Okay, it's our setup. So, we have attacker in this thread model. It's any user that can send any request to the web server. 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. 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. So, it's like inheritance implementation for JavaScript and TypeScript. In the second line, you can see that we read the property from the object that specified in the argument org. This is the proto. It means we get a reference to this object. In the last line of the code, we assign a value from details to another property of the prototype, the property shell. So, we read the proto, we assign a value calc in the shell, and we get it in the prototype. This code pattern is called prototype pollution, because we pollute, we add some property or replace some property in the object prototypes itself. Let's assume that we have such code for backup handler. 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. In our case, shell is undefined and it will be used as default shell. Okay, it's a very common pattern in the JavaScript. But what actually happens if we trigger this handler after prototype pollution? We create options that point to the same object prototype. We read the shell and the runtime lookups for the shell property in the prototype chain and reads the polluted shell property. And the exec command runs a calculator because we get a calc in the shell, in the cmd variable at the end. Okay, this is prototype pollution and remote code execution that we can achieve by prototype pollution in the end. And this code pattern is called a gadget, prototype pollution gadget. Let's start the story too. About prototype pollution and remote code execution based on that. So, I tried to use SilentSpring. 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. So, the first attempt CodeQL terminated by timeout because of too large database that I tried to analyze. I implemented a script that rerun CodeQL for subfolders if the analysis is timed out. And this script reported 77 cases in the application itself. I will add the script in the same repository if you're interested. You can find it there. Try to play with that. So, we get 44 server-side cases including some false positive testing code. And today I will show you a couple cases from these that was exploited. So, the first case, this is prototype pollution pattern that was detected in the config parser. The config parser take some YAML document, main config of the Kibana when you restart Kibana. And after the parsing it also tried to expand dot separated names in this YAML config to such nested object. So, a.b becomes a object that contains nested object with property b. So, very simple idea. But the code that does it is vulnerable to prototype pollution. 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. In the walk function, we get the first key, a, and read the value of the a, and call walk recursively. 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. 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. So, we can add any property, and we control the value of the polluted property. Good. 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. 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. Attacker can store any payload in Kibana and restart Kibana to load all configs. 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. It happens before we get started the web server. We cannot send any other request to trigger any gadget. 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. 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. 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. What we can do in this case? Let's see what Kibana did before the crash. It tried to load some modules. 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. 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. And yeah, actually we can run any malicious JS file through the prototype pollution. Let's see on the require gadget how it works. 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. Oh yeah, we can pollute one more property environment and trigger or handle backup. 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. It does loadConfig, and for loadConfig this function returns the parsed JSON, like this one, with all properties from the config. But some package JSON, the configuration file, does not have main. 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. It means we can read any JS file that exists in the system. And it's some limitation for the require gadget, but we combine it with another object. If we can find some JS file that, for example, start new process, do child process exec, we can combine it with another function. And npm.js, actually, when we load it, starts new process. It does such code. It executes spawn function with two arguments, non-executable process and some arguments. And the implementation on the spawn function, we have another gadget. Let's try to read property shell and env from the options. And options was empty, undefined. It means it becomes an empty object. And when we read env here, we read it from the empty object. And we actually read the value from the prototype. Okay, that's the idea of the require gadget. The problem is that this gadget has been fixed by Node.js team, and I already knew this when found it. And in parallel, I worked on some another tool, some dynamic analysis, it's called gHunters. I described it in the last paper that we published, and that will be presented in Usenix security in a few days, next week. And working on that tool, I investigated require gadget and the fix. And the require gadget looks like this. So the code that we already saw, we have a load config. 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. 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. So this is a gadget. And Node.js team fixed it. So the fix looks like this. 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. So it means we cannot affect reading on the main property anymore after that fix. But at some moment, gHunters start detect the require gadget again in the new version of the Node.js. 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. And it has another gadget. I will zoom in. You can see exactly in these slides. We have this if statement. If JSON is undefined, the function returns false. The function load config returns false. ParseJSON becomes a false, and we try to read the main from the false. JsRuntime in this case tried to box primitive type, Boolean type, and add object prototype in the prototype chain. And we again can read the main, but from the false, actually. 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. So, this vulnerability did not get high score because it required high privileged user, but still it's almost 4,000 bounces. Let's see on the next story about prototype pollution and remote code execution. Another case that was reported by our script, SilentSpring Node.js queries, was a prototype pollution in some handler, web handler. 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, 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. Very simple, you don't even need a script to find it. Okay, so when I started working on this script, the question, we need to find a gadget. We worked on the Dusty, it's another project that detected 49 gadgets leading to remote code execution in NPM packages. 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. And one of the package that has a gadget was NotMailer, and Kibana uses this gadget to send emails. So, the idea, we have some SendMailer transport that's optional, but you also can pollute some option to enable it. And it has some options that's by default empty. 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. Okay, so it means we can inject any command and execute, start any process on the victim machine. But the problem is that DeleteHandler crashes the application in less than one second after the prototype pollution. 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. And we get denial of the service. That's not much interesting for such vulnerability. So, it looks like unexploitable. Denial of the service, I could not say that it's an interesting impact that we want to show. 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. Let me explain it based on the script, on the exploit script. So, we can implement such race condition exploit on the bash or any other language. 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. And this ampersand means that all of these commands will be executed in parallel. So, we send a lot of requests. Let's see how it works in the demo. 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. So, we change in our exploit SH script this connector and run the exploit script. And I started this web page to see which requests we will get. So, I did not run a reverse shell, I just sent a request to this web page. You can see that five requests will be successfully received. It means that five cases will be for so short time that were executed before the crash. And this attack was run from the Wi-Fi. 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. Okay, this attack gave me the biggest bounty that I got from the Elastic. And not only this prototype pollution that I reported was exploited, this case was closed a few days ago. I did not have time to edit in the presentation and also we don't have much time. And another case that was reported also, this is a chain that leads to account takeover. Maybe I will do it in the next presentation, let's see. 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. When I understood this trick, it allows me to report a few new vulnerabilities that I actually thought were unexploitable before. Let's see the example. We have a prototype pollution from our original example, very simple code that we already know. We have a gadget. The gadget reads the options and checks the property runner. The options are empty object by default. If the runner is defined, we read paths and arcs and spawn a new process for that. It means we need to pollute a runner in the object prototype, like this. We add a runner with this object as a value. And after that, we trigger a handleback, of course, to trigger a gadget. What if this prototype pollution does not allow us to control the value? For example, it's some object with a predefined identifier and other properties. We cannot control it. Or it's an empty array. The array does not have any properties, right? It looks like unexploitable, at least for such a gadget. Maximum, we can get the denial of the service. But what if we have such code? It's a fixed, I call it a fixed prototype pollution. We check that arc does not equal a prototype and execute this code only for that. It cannot lead to prototype pollution, but we can combine these two code snippets. So it's absolutely safe code separately from this one. But what we can do together with these components? First of all, we need to pollute the prototype. We add a runner with a value of empty array in the prototype to trigger handle update. Good. After that, we trigger this save function with arc that has a runner value. It means we will try to read the runner from the prototype at this step. And we pollute the runner after that. We can't feel any additional properties in the runner by this call and the second call. And after that, we trigger backup handler. It gives us the remote code execution again by this chain of the calls, the web request that triggers such functions. 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. Like a merge function of the very popular package lodash allows us to pollute a runner with such payload. And you can find lodash merge calls in many applications. And that's important that these two parts can be unrelated. It could be absolutely different features in the application. So yeah, use it for fun and bounces. In conclusion, in this talk, I wanted to show the cases that look unexploitable and say, don't give up. Try to achieve remote code execution even when it looks impossible. If you're interested in prototype pollution research, check out our papers, SilentSpring, G-Hunter and Dusty that I mentioned in the talk. Check out the collection of server-side prototype pollution gadgets that we shared. This is a gadget in Node.js itself and dino and npm packages that I also use in my bug bounty cases. If you're interested in networking attacks and this story about ARP spoofing, I highly recommend to read this blog post. ARP spoofing in Kubernetes is not so easy as it looks like. It also has some difficulties with the IP table rules that were bypassed by this talk. Check out this blog post. I think you'll enjoy it. And follow me on Twitter if you're interested in some details of code reuse attacks. I want to share some findings that I am now including in my thesis. And thank you for your attention. Enjoy DEF CON. Bye-bye.