Alright, hello! I'm not used to a mic so sensitive so I'm going to talk like back here and if that doesn't work then I'll adjust, whatever. This is Xiaomi the money, our Toronto Pwn2Own 2023 exploit and behind the scenes story. What this talk is about is our hack against the Xiaomi 13 Pro which includes the full exploit chain that we used and also issues encountered during the research itself. And then we're going to cover what happened during the Pwn2Own competition itself and for a little bit of background on who we are, I'm Ken Gannon, I'm a principal security consultant at NCC group and I usually focus on hacking mobile shit. This is Ilyas, at the time of the competition he was also part of NCC group, he is a veteran Android malware reverse engineer and this was his first time competing at Pwn2Own so we got first time Pwn2Own, first time Defcon, this is his first time in the US, this is a lot of stuff for this guy right here. So a little bit about us, so it was in 2022, no 2023 of last year, it was around May. Ilyas was looking to do Pwn2Own, for those of you that don't know what Pwn2Own is, it is a zero day competition where you try to hack some devices and some prize money involved and Ilyas was looking to do Pwn2Own this year, last year for his first time. And so he started researching previous vulnerabilities, previous competitions to see what that was like. And then he came across my name because I've done exploits, I've done previous attempts at Pwn2Own and he realized, oh shit, I work at the same company as Ken Gannon, I'm gonna reach out to him. So it was like May of last year, we get together, we talk over teams and we discuss what we want to hack. And we decided to hack the Samsung Galaxy S23. So that was as of May last, oh it's, hi Will, I didn't see you there. So that was May of last year. And then July of last year, ZDI announces their list of devices. And as you can see on the screen, it includes the Xiaomi 13 Pro. So the thing about the Xiaomi 13 Pro though is that the Xiaomi devices has not been a target of Pwn2Own since 2020, the COVID pandemic. And him and I got together again and we're like, oh, this sounds interesting. Maybe we should try to hack this new device or we should try to stick with Samsung Galaxy S23. And the thing about our discussions at the time is that we were discussing this on July 13th of last year, when the devices were announced. And as I said, we were thinking about Samsung Galaxy S23, but I have a history with Samsung at this point. A lot of traumatic experiences with Samsung, including a failed Pwn2Own attempt in 2021, because I am a very shitty programmer. So because of that, and we decided that maybe we should just go for the Samsung, for the Xiaomi 13 Pro instead. Something new, something fresh. We we just thought, you know, something that we both can learn together. What could possibly fucking go wrong, right? This is gonna be awesome. So let's talk about the Xiaomi 13 Pro. There's essentially two versions of this device. The global variant and the China variant. The global variant is available worldwide, wherever you can get a Xiaomi device, whereas the China variant is typically only found in China. At a hardware level, they're exactly the same thing. Same camera, same processor, same board, same chip set. The only minor difference between these two models is the type of firmware that can be installed on each device. Uh, the China model can have the China firmware as well as what's called the global variant firmware. Whereas the global model can only install the global variant firmware. Now, the unique part about the global firmware though, is that depending on the settings, you can have it emulate different regions. Like for example, in Europe, in India and in Taiwan, they have their own Taiwan, India and Europe firmware. Or you can tell the global mother one to emulate a different region and it will become that region. Why this is important is because certain regions have certain apps enabled or disabled. For example, if you set the region to United Kingdom, the get apps application, which is the Xiaomi uh application store, will be enabled if the phone is set to the UK region. Whereas if you were to set it to United States region, the get apps store is disabled. And this is just one example. Another one is that if you set the region to Russia, then the Yandex browser becomes enabled on the device. Keep in mind that this browser, the APK file, is in the firmware already, it's just that it gets enabled or disabled depending on what region the firmware is set to. Another example is that with the Spain region, the Opera browser becomes enabled and one more example, and one more example, the Indonesia region, the WhatsApp uh app becomes enabled or disabled. So at this point, we have multiple different regions, multiple apps being enabled, multiple apps being disabled. So we thought, well, why don't we determine what each region does, download all 400 plus applications from the phone, and start reverse engineering every single application. Except that's a really fucking bad idea because that's a lot of applications. So instead, we decided to just prioritize the Xiaomi developed applications, specifically the ones that were developed by Xiaomi for that phone. And so then 22 days later, we had 3 exploits for the GetApps application. And this is where Elias is going to talk about them real quick. Hey everyone. I'm going to be talking about the app we exploited. So we found 3 different exploits in the GetApps application. This application is also what the, what we ended up exploiting at point one. So we found 3 different ways to force install application. And the attack scenario is the following. Tapping a malicious hyperlink in a web browser would result in automatically install any application available on the GetApps store without user consent. Two of the methods were unfortunately patched before the competition. And I'm going to show here the exploit code of those 2 patch exploits. So here's the first one. Um, that was used to install WhatsApp automatically. On the first screenshot, you can see the browsable intent code. And if you've never created a browsable intent hyperlink before, we've also put the Java code which does the exact same thing. And there is an ID equal com.whatsapp parameter in there. And that's what could be used to install any application available in GetApps. Here's the second patch exploit. On the left, you can see the browsable intent code. And again, on the right, you can see the Java code which does the exact same thing. It's a lot chunkier as you can see. And if you base 64 decode that string, you'll be able to see the WhatsApp packet name, package name in there. Before we can get into the third exploit, we need to talk a little bit about WebView in GetApps. Um, GetApps had a WebView which contained a JavaScript interface. And for non-Android people, this means that JavaScript could be used to execute a set of Java function defined in the application. It's defined in the JavaScript interface class. And some of those JavaScript interface function were benign, like show toast function, for show toast functions, um, which literally just shows a toast, as you can see in the screenshot. But there are two interesting functions there for us, for point one. The install function, which installs any application available on the GetApps store without user consent. And the open app functions, which opens any specific application, again, without user consent. And this WebView is obviously dangerous, and Xiaomi knows it. So, they implemented some protection to ensure that attackers cannot obviously open this WebView and execute any of those JavaScript interface functions. And one of those protection was that whatever URL being loaded in the WebView, must be a valid URL. And by valid, I mean that it either starts with the HTTPS protocol, and the domain should belong to a defined set of, um, uh, white listed domains. All of them were owned by Xiaomi. And the other valid URL, um, should start with the file protocol, and the file should be located in the GetApps private directory. A GetApps private directory, um, names was always web hyphen raise, I hyphen some random numbers. So now, we have a new plan. First, we can launch the WebView and load a valid URL. Second, inject, inject some custom JavaScript code into the WebView, possibly through cross-site scripting. Third, having the JavaScript code execute the two JavaScript interface functions mentioned before. And fourth, got, get remote code execution. Okay, step one. To launch the WebView, we use the activity called join activity. This activity could be launched via browsable intent. Again, this means that this activity could be launched via hyperlink in a website. And it was also really simple to tell join activity what URL we wanted to load in the WebView. It was just a get parameter called URL. This is the proof of concept, uh, which launched join activity and loaded the file detail dot html that was in the GetApps private folder. That detail dot html is pretty blank, as you can see on the screenshot. Um, but that's just because we didn't give him the application's data to use. So now step one is done. Let's move on to step two. We had two options to inject custom JavaScript. We could either load a show me domain URL and then print this dot URL and look for usable vulnerabilities. Or, we can analyze the file in the private folder and look for user input vulnerabilities. We choose the second option. And now this research just became a JavaScript source code review job. And as you can see on the picture, this was my reaction when I realized what was going on. Um, point one will make you do stuff you really don't want to do, but we still did it. Um, in, in the, uh, the web hyphen res folder, um, you can see the folder. We had like HTML file and JavaScript file with similar names. As an example, we had the detail dot HTML file and this file would load the detail dot chunk dot JS. Some JavaScript file would accept user input via get parameters. But also, all of those JavaScript file had a building function to filter out any cross-scripting payloads. Well, except for this one. One that we found. There was a file called integral dialogue page dot chunk dot JS which failed to sanitize cross-scripting payloads in one specific area. So, let's try to get a dumb XSS payload working in this file. This was the initial payload that was used for testing. On the right, you can see the decoded version of it. So, we had different inputs and we had different payloads. All of them started with XSS test, 1, 2, 3, 4, 5, and they all ended with, um, two characters that was encoded. And those characters were double quote symbol and the less than symbol. On the left is what the web view looked like with the payloads. And on the right is a debugging window in Chrome. Our less than symbol character was unfiltered in one of the area, specifically the XSS test 2. All the others were filtered, filtered the right way. So, now, with this XSS test 2 payload, we can try to just pop a simple alert box. And then, what the hell? We got some errors. Locat was showing us disabled XSS text, XSS errors, um, errors. So, let's dig into that. Remember the protection against the web view that we mentioned earlier? There was another one, unfortunately. The GitOps application had a built in XSS filter in the Java code itself. And it looked for XSS payload in the URL. So, if the URL had any XSS payload found, then the URL would fail to load. And here is the list of the XSS filter. And as you can see, it detected our forward slash script string in our payload. At this point, we decided that maybe we shouldn't be using script string at all. Maybe because there might be other filters we haven't found yet. And also, script is just too obvious for an XSS payload. Okay, so going through the list and thinking about what could we use now, we realized that the Java XSS filter was just looking for string matches. So, we thought, what if we could encode, um, a less than symbol, um, a character in one of those payloads. Such as, the less than symbol SVG. So, that's what we tried. And we used, um, we used a SVG unload to run the JavaScript alert box and that, that said, yeah, dumb XSS here. And boom, it worked. We finally have an XSS in the web view now. So, step two is done. Let's move on to step three. Oops, sorry. Now, let's try to call the, those JavaScript interface function mentioned earlier. To test, we tested our payload just to see if we could run the show toast function. And on the screen shot on the right, you can see that we managed to do that. So now, after some time, we did some reverse engineering and we came up with this lovely JavaScript payload to install any application. On the screen shot, you can see that we first called the install with the package name of the application we want to install and then we run a loop, um, and wait for the, the application to install and then we use the open app with the package name again to open the application. So now, there is just one problem. We didn't know what to install. So now, what application should we install? Ideally, we want some malware that we can control in the application store, store somewhere. And I'm going to let Ken talk about his baby now. So, for those of you that don't know, I'm also an active contributor for this Android tool Drozer. Yay! For those of you that know what Drozer is, yay! Uh, and because I'm an active contributor, I know how to reskin it into an application called Sunfish which does literally the same exact thing as Drozer except it starts a bind shell on boot as well and then I uploaded it to this Xiaomi app store. So, for a little bit on the Xiaomi app store, there was a, a copy of Drozer called Sunfish up on the app store. That was fun. So, we had a new payload. In this case, the payload is going to step one, install Sunfish and then step two, open it after Sunfish is installed on the device. So, alright, all the pieces, all the puzzle pieces are in place. Let's uh, let's watch it do its thing real quick. We open up Google Chrome. Uh, in this case, we're going to, as you can see, it's a new copy of Chrome with all those popups. We browse to the web server I had at my house, uh, over HTTP because I'm lazy. But eventually what's going to happen is I will load the website and there's a yay POC hyperlink. Click that one link. This appears and then Sunfish gets installed and boom, Sunfish is now installed. The server has started as well. I put the phone down. At this point, I have the IP address of the phone that reached out to my web server as well. And so, to put the bind shell open, all I had to do was use my re-skinned Drozer console thingy whatever to connect to the Drozer application installed on the phone. And then for, per the rules of Pwn2Own, as long as you get RCE on the device, that is good enough. Including just RCE as a single application. So, in this case, I'm running ID and who am I as Sunfish? There we go. One click to, one click to RCE. Yay! So, we got RCE. All the steps are done. And like I said, that was only 22 days later. We had 22 days of research time. We had 77 more days to go to to do whatever the fuck we wanted. Everything was awesome. Nothing could have possibly gone wrong. Let's discuss what went wrong. So, late August of 2023, Xiaomi released Xiaomi made changes to their get app server. Uh, specifically, one of the changes that they made was that the install parameter, the install function that we were using, now required a shit ton more code. Our payload was now chonky as fuck. But wait, there's more. You see, this payload did not work in all parts of the world. So, for a little bit of background, last year and around August, late August 2023, I went on vacation to Japan and the Philippines. And this new chonky install payload would work in Japan and the United Kingdom. Thanks to Ilias here being based in the United Kingdom. But it fucking wouldn't work in the Philippines for some reason. And while in uh, Japan and the Philippines, I noticed that the get apps application was showing different content based on the physical location of the phone. Not the IP address. I'm talking about doing a factory reset and you, you know, you go, factory reset, you go through the whole setup, the cell phone towers are, the cell phone signal is now enabled on the phone. And for some reason, after you do a factory reset and you go through the initial setup, there was stuff on the uh, in the get app store that was exclusive to that one region. And what I also realized at the time, or as we were doing like research, like figuring out what is going on, uh, VPN did not affect this. The reason I was talking about factory reset is because during Pwn2Own specifically, uh, they typically factory reset their phone before every single uh, entry goes up. And so then during a factory reset process, the cell phone tower, the cell phone signal antenna is enabled. So we wanted to mimic that. And we realized that just really weird stuff was happening with the get app store depending on where I was in Japan and the Philippines. And so then after looking at the developer documentation and looking at uh, the, the developer portal I had access to since I uploaded Sunfish to their app store, I realized that there were some places that were launched. Uh, for example, get apps was not launched in the US, UK and Japan. But it was launched in the Philippines. Now I don't have definitive proof, but this is literally the only thing I could think of at the moment. Maybe we were seeing different behavior in the Philippines because the get app store was not, was launched in the Philippines but was not launched anywhere else that we've been to. Now, thankfully, cause we are awesome, eventually we found a way for the payload to work in the Philippines. But that payload would only work in the Philippines. It would not work in US, UK or Japan. We didn't know what to do. Because another issue came up, Pwn2Own that we were going to takes place in Canada. Do we have a payload that works in Canada? We, I like, it's a theory, this whole like launch not launch thing is just a theory. And technically, get apps was not launched in Canada. So maybe, maybe our get apps or non-launch payload would work in Canada. We're not entirely sure. It'd be nice to confirm. Well, NCC group is a global company. And we have offices in Canada. So surely we can just, no, no, we're not, we're not working with the Canada office for this talk. Because you cannot get the Xiaomi 13 Pro in Canada. So, if you try to get the Xiaomi 13 Pro here in the US, you're going to go through eBay. You're going to go through a, a seller on eBay or on Amazon or something to get the phone. Uh, in fact, my phone, I know for a fact, what, was a China model imported here from China, sold by a, a Chinese guy on eBay. I could not procure the Xiaomi 13 Pro in Canada. eBay Canada, Amazon Canada, none of the cell phone providers in Canada, like I could not get it in Canada. So, you know, I'm in North America, I might as well just send my phone to Canada. No, no, I'm not sending my phone to Canada. Because another issue came up with my schedule specifically. You see, in August of 2023, uh, or 20, 2023 in general, NCC announced that they were going to open an office in the Philippines. And I was chosen to go out to Manila to spend the rest of 2023 in the Philippines to help set it up. Uh, fun fact, I'm still in the Philippines. So, because of this, I had a very, very weird travel schedule last year. Because as I said earlier, I went on a vacation, right? I come back from vacation on September 10th. And then on September 11th through 14th, I'm at my house, I'm like packing, I'm like trying to get ready because I'm going to be spending the rest of the year in the Philippines. Uh, and then on Friday the 15th, and, and I do this thing in Vermont for like a race, I come back on the 17th. And then again, 18th through 20, I'm at home. And then the 21st of September, I fly to the Philippines. That leaves only seven business days where I am in the U.S. So that's the schedule. Uh, it's a little bit hectic, but it gets more hectic, I promise. But first, we want to, I want to talk about why those seven days sucks. Because it is too risky to send a phone to Canada, pay Canada import taxes, test and develop an exploit, send the phone back to U.S., and then pay U.S. import taxes all in seven days. That is not going to happen. Well, what if I just send my phone to Canada and keep it there? Well, I, I, the Philippines has a lot of Xiaomi stores. That's, there's literally a picture of a Xiaomi store that I sent to Italy, that's when I found my first one. And they were selling Xiaomi 13 Pros for 1,000 U.S. dollars. I can just go to the Philippines and get a new phone, right? Well, the problem is that now we're dealing with three different time zones. If I'm in the Philippines, he's in the U.K., and the Canada consultants are in Eastern Standard Time. Uh, for those of you that don't know, half of the time of the year, the Eastern Standard Time and the Philippines are literally 12 hours, 12 hour difference. And trying to manage those time zones on top of the consultants in Canada being available, that sounds like way too much fucking stress for a theory. On top of that, the biggest issue, the biggest threat to any pawn to own competition are the patches. So, at this point, we had a good idea as to how the patches worked. Uh, first, during the first week of, of every month, a firmware gets updated to the beta channel and in, and as, after that first week in the beta channel, the firmware also gets put in the stable channel. With that firmware update comes a new GetApps update. Every single month, there is a new update for GetApps. So, the real schedule now is that after I fly back to the Philippines, no one is in the U.S. to test the exploit, or no one's in North America to test the exploit a... Hey, you know what? If I try to shut... Alright. No one's in North America. Hey, we're back. Yay. Alright. Yeah, like I said, no one's back in, no one's in here in North America to test stuff and we have a new patch coming out sometime, supposedly October 9th, right? I really, really want to test this theory though. I really, really do. Because, here's the thing. We think that Canada is a non-launched region, right? Just like with U.K. or U.K. and U.S. uh, and Japan. If we can group, and this is just a theory we had at the moment, if we could group Canada into that non-launched region and test and make sure that we, that, that theory is true, then let's say that October rolls around and they release their patch, right? All we would need is Ilyas here to test his U.K., uh, his exploit in the U.K. And if it works, then in theory it should work in the U.S., in Japan and in Canada. So, earlier I mentioned that it costs 1,000 U.S. dollars to buy a new phone. Another thing that costs 1,000 U.S. dollars is to go to Canada, literally for a day, literally hang out in the Marriott Hotel at Toronto Airport and test the exploit. And then fly back the next day. So that's what I did. On, let's see, on September 17th I fly back from my race, on September 18th I fly back to Toronto to test the exploit, on September 19th uh, I fly back home and then on September 21st I fly to Manila. So many miles. And like a, and after a few hours in Toronto, it was confirmed. We did it. We, we confirmed that this was a non-launched region. And so now, if our, if October rolls around, our exploit works in the U.K., we should be good. Everything is awesome. Uh, we are, uh, we are so excited for everything to happen and we are going to get our, uh, get our first phone to own wind. Right Elias? Yeah! So, October rolls around. As expected, get apps releases their, their update in October. It's literally October 9th. I have it marked down in my calendar and we confirm that the exploit works. We have our books flight for September 20th. I'm flying from the Philippines, he's flying from the U.K. We, all this is good to go. We're going to fucking do this. No, hold on. One week before phone to own, Xiaomi releases an out of band update to get apps. Which happened to fucking remove the file that we were going to exploit. Specifically, the file was removed from the APK file itself. For those of you that are on Android, uh, that means that the file was removed from the installer file of the app itself. It was no longer there. But thankfully they are, Xiaomi also has shitty programmers. Because they forgot to remove the file from the device if it was already there. So, in theory, assuming that we show up to phone to own and we're one of the first few people to hack the phone. And like I said earlier, uh, uh, ZDI usually factory resets the target device per, uh, competition. They usually have multiple devices. So, as long as we could hack one of the initial devices before they factory reset it again. Again, it's a dice roll, but there's still a chance. We still follow through. We still decide, okay, this is not that big a deal. Let's fucking do this. So, we fly out to Canada. It is now October 20th. Him and I are both on our flights. Literally while I'm on the plane. And the Saturday before phone to own, there is another get apps update. Now, this is the most interesting part because they introduced a new thing in get apps where they can modify what files are on the device using Google Firebase. Which is really, really cool. And for some reason they pushed our file back to the device. Like if you were moved, if you went in and manually removed the file that we were going to exploit, they just pushed it back for us. That's so fucking convenient. Thank you, Xiaomi. Except they added one more thing. So, we were going to exploit the activity, join activity, as we, as Ilya said. But they happened to add code to that one activity where if the intent came from any of these four browsers, the uh, Xiaomi browser, uh, the Android HTML viewer, the Android NFC, or Google fucking Chrome, then join activity will not launch. What the fuck, Xiaomi? As you all saw in my video earlier, we use specifically Google Chrome. We can't, there's no other browsers on this phone that we can use. Except there is. Because if you set the region to Spain, the Opera browser is enabled on the phone. So technically we had an exploit for the country of Spain specifically. Which is still technically a zero day, because now it's a zero day in Spain. So we told ZDI this. We told them, hey, Xiaomi is doing some shitty stuff, but Spain. And ZDI goes, Spain. And we're good to go. And we're good to go. Alright. We dodged another bullet. We can now, we can do this. We can hack a Xiaomi 13 Pro. The first day of the competition, Xiaomi starts disabling the ability to install apps from their app store. Now, I don't know if this was a global thing. I genuinely don't know. I don't talk to many Xiaomi users after this. I don't want to. But the very least, the people in Toronto area that were, that were also going to hack the Xiaomi 13 Pro, they corroborated it. They all saw the same issue that we saw. It was a hit or miss, whether an application wouldn't install. It was like a 20% chance that we could install an application. And they killed my boy. This, this really genuinely hurts. That I could not install Sunfish at all. It was a 0% success rate. So, on that day, there was 2 teams that were supposed to go and hack the Xiaomi 13 Pro. Uh, it was us at NCC group and it was, uh, the awesome people at Team Viettel. Uh, Team Viettel went first, we went second. Uh, we discussed with ZDI what happened and we're like, what the fuck do we do? Oh wait, can we please just install one random app from the app store? And they go, okay. And we did. We did it. Technically that works because we showed ZDI that we had malware up on the app store. ZDI already heard from other contestants around the area that Xiaomi was doing something very funky with their app store and they're like, fine, good enough. So, Team Viettel managed to install Discord and I, we installed no pics color by number and pixel. Yeah, it was that app. I literally just found another random app on the app store and we installed it. And we did it! We got a win! Yay! Except that's not the end of the story because the next day, Xiaomi releases another update. First, they removed the file that we exploited and now nothing installs from the app store. Period. The day before, those that got 20 percent success rate on installing something, no. The next day, nothing gets installed. Period. And on top of that, they added code to block Yandex and Opera. You can't make this shit up. So, the rest of the week, it was just failure after failure after failure after failure. Unfortunately, it's pretty obvious why nothing would install. They removed a bunch of other stuff from the app. And, you know, the lesson of this story is that during Pwn2Own specifically, Xiaomi is the most secure phone in the world. If there was one phone I would have on me during Pwn2Own specifically, especially here at DEF CON where nothing bad happens, it's a Xiaomi 13 Pro during Pwn2Own. There is one more side to this story that I think everyone here should check out eventually. Uh, my friend Maxploit, who was also a competitor last year at Pwn2Own, uh, he says, and I quote, props to the Xiaomi crew who proved you can patch RCE bugs completely remotely in only a few hours and also break your flagship device across the country as long as you're doing it to be a dick. Not because you actually care about fixing things. If you guys want, he actually talked earlier this year at OffensiveCon about his side of the story as well as his exploit. I do recommend you all check it out. It's also a fun time. So, after Pwn2Own, what happened? The browser blocking code got removed for some reason and applications can install just like normal. Everything works. Everything is back to normal. Now, both us and team Vietel never got credit from Xiaomi. Usually with a vendor they are like, hey we want to acknowledge so and so working together with, with ZDI to do so and so. No, they said fuck you, we don't, we don't want to acknowledge you. So, Zero Day Initiative had to be the one to give us CVE numbers. And they said something in their posting that was possibly my favorite quote of this entire time. Xiaomi informed ZDI they would assign a CVE but never followed through. Except they did, specifically to team Vietel a few days later. But not us. Where's my fucking CVE Xiaomi? And that's it. That's the end of our story. Yay! I hope they get the worst vendor response at Pony Awards. Yay! Uh, we're also very introverted so we're gonna take questions out there instead of up here. If you guys don't mind, alright? Alright, thank you for coming! Yay!