DEF CON SG1 - CSIT Village - Kang Hao - Android Security Javascript Bridge Is Falling Down
Transcript
Machine-transcribed by YouTube. Expect errors in names, commands and version numbers.
Round of applause. >> Thank you. Thanks for the introduction. Uh yeah, so today's topic is going to be Android security uh specifically on insecurely coded JavaScript breach.
So who am I? I'm my name is Kunghao. I'm a security researcher/enthusiast. My interest is in offensive security on web IoT and mobile app. I'm currently focused in Android security and uh I'm
working in in know labs. Yeah. Below are my socials, my LinkedIn and on my on the right is my tech write up where I write
about technical stuff with a cute comic strip that will cover it with a story line that covers what happened in a technical write up. Yeah. Uh in fact today's talk is actually from my blog. Yeah. So uh if you have time you can go and check it out. Currently we I only have two write up. Yeah I'll trying to find more time so I can write more in the future. Yeah. Yeah. So this is the overview. uh we are before diving into
JavaScript bridge uh I will go into the fundamentals where I will introduce the introduction of Android security understanding what are the Android attack surface and then go deeper to understand the web view attack surface after which then we can finally explain what is JavaScript bridge next will be the investigators playbook on JSP where we will highlight the high
level uh step-by-step methodology on how
we actually analyze a mobile app to
understand uh the JSB features. Yeah. So
I will cover from APA extraction static analysis with JEX and then using dynamic analysis to prove some of the theory that we thought. Yeah. Uh huge caveat this finding is actually found before the AI took over. So all these things are preAI. Uh I wouldn't be using any AI
tools on my analysis but I think there are still some value in sharing how people in the ancient time does their research without AI. Yeah. Then after
uncovering the uh the step then we will
be identifying the vulnerable code challenge uh the challenge is to try to bypass some of the white le protection and then finally crafting the final payload. Yeah. Then the last part will be our conclusion. >> Yeah. So this is what I'm talking about. Uh my blog will always have this cute comic strip the title as clickbait and switch. Uh so over here we have a penguin character. I call it Tux. He was awakened by a notification. And then uh
in his message he has a link with a title not scammer. So Tux click on it.
the uh redirection happens to launch into the Insta Green application and then the message there is just a loser plain text. Yeah. A a while a few hours later,
Insta Green contacted Tux saying that his account has been compromised. And he attempt to log in to his Instagram account, he say is the wrong password. And then his friend Lucia also has been noticing he has been posting a lot of weird stuff. Yeah. So that is actually
the what we are going to be talking about where our goal here is to attempt to make an account take over. The attack vector is by sending a malicious link to our victim. So when TS click on it, we
compromise the account. The exploit is actually by abusing vulnerable GSB function to perform unauthorized actions. Yeah. So before we go into deep into it, uh let's go back to the fundamentals. For those who are unfamiliar with Android security there, I try to give
you guys a visualization by splitting it into three categories. The first one is HTTP API, second is web view, then third is the Android components. So traditionally for mobile
pen tests usually they will always be looking at only the HTTP API where we have our app that will be ported over to a Burp proxy and then that's where we analyze the HTTP traffic. Yeah. So things to look out for for this case is usually idle issues and business logic issues. And then we have our web view. And uh lastly is our Android components. For web view technically it is a Android components but because web view has such a massive thing going on so I decided to
separate them into a individual category. Yeah. So for Android
components uh so Android component is basically like a Lego brick block that make Android app possible. Yeah. Uh why this is interesting because in some cases these components are exported. It means that yeah it is exposed for another Android application to access it. So you will you will have many interesting attack scenario like intent redirection te of arbitrary file via content providers and dealing exploits. Yeah. But today focus is actually on web view.
But if you are interested in Android components you can check out my blog. Remember I only got two write up. One of my write up will be talking about the Android components. If you are interested uh my blog will write about how I use leverage Android components to exploit data go browser. Yeah. So sorry
I digress. Yeah. So now going deeper into web view attack surface. So where will the attack be coming from? The first attack vector will be deep link redirection. Uh so in some cases deep link actually accepts URL params where
where if you key in the relevant HTML HTTP page you will be loaded into the app's web view. Yeah the entry point are usually the your browser another apps or even the QR code the some of you guys actually scan just now. Yeah but don't worry it's safe. Yeah nothing going to happen. The second entry point is actually from a H5 based screen. H5 screen is actually
just a Android screen that uses web view
to display the UI instead of Android components. And uh usually they will be fetching from a trusted server. And uh these types of attack is not direct because how the attacker would exploit this is you will inject like a XSS payload into the server and then uh corrupt or poison the server with the payload. So when the app actually fetch the H5 screen, it will actually load the payload XSS to it. Yeah. So there's a second entry point. Third is an inapp browser. So apps like
maybe Facebook, they actually have their own inapp browsers when you actually click on the link and then uh instead of launching an external browser, you actually load within it. Yeah. So uh usually for this entry point are the IM chat where your friend probably send you a link and then you click on it and it launch the app browser. Yeah. And today we will be focusing specifically on JSB vulnerabilities.
Okay. So what the hell is JSB? So JavaScript bridge is actually a mechanism that allows JavaScript code in web view to interact with the native code. So the one on the left is actually just a JavaScript code where we have Android bridge as the JavaScript interface and the function as showtoast.
And if uh once this is loaded in the web view, Android will perform some magic and will maps to the relevant native
Android code where we have our Android bridge as uh initialized in the add JavaScript interface and then the function for the GSB will always have the JavaScript annotation on top. Yeah. So this is how the syntax looks like.
Yeah. So now you're an expert. Maybe we can look at some real life examples on an Android app. Yeah. But wait. Okay. So
unfortunately this finding has is my past research and it was never really publicly disclosed. So all the thing the code and some of the screenshot will be heavily redacted and some will be renamed for clarity. Yeah. But so for now we are going to refer our examples for our JavaScript interface as Xbridge and the target app as bound app.
Okay. So to keep things simple let's assume that we found a deep limb redirection like this where our evil.com
will be loaded into the app's web view.
So how do we begin? So first we extract the app using the ADB pool and then from
here with our PC we will try to decompile it with JEX.
So to begin understanding looking for GSB vulnerability the we use the search
function by typing at JavaScript interface. Yeah. So while we type the at JavaScript
interface we will face a problem. There will be a lot of JavaScript interface here and uh some of them is not even reachable via deep link. Uh yeah. So
example let's look at this where we have s bridge. How do we know that this javascript interface is actually loaded in our deep link.
Yeah. So maybe we can try to do a deep
uh trace on the code from the source to the sync and see whether this actually got loaded. But obviously I mean if you're using AI like clot yeah that's not a problem. But since this is the ancient time remember so this approach is not practical. Yeah. So what is a more practical way? Dynamic analysis.
So one way we can do is to use Freda hook where we hook this function to see whether it got caught as we redu when you click the deep link and it redirects to the app's web view. But the way the
approach I usually like to do is to use Chrome developer tools. Yeah. So, so for example our Xbridge you
can see that our JavaScript interface is Xbridge and looking into it we have actually two JSP functions show toast and invoke method. So uh we can actually use Chrome developers tools to try to see if this is got this got caught. But uh the prerequisite is you you have to use Freda to turn on the web view debugable mode to true. Yeah. But you can ask your
AI how to do that. It's very easy. Yeah. So once you did uh you set that up and then you plug in your mobile device into your PC. You open up your Chrome. You you type in Chrome/ Inspect.
From here you will see those web view that is already loaded in the app and you can click on the inspect.
So when you click inspect they will actually show you a console that we can interact with. This is where we can type the JavaScript interface Xbridge and then from here you see that when we enter you will see the result the two the results of two GSP function. So this is a way we can tell that actually our web view has actually um
invoked the JavaScript interface. So we can actually proceed on doing our analysis.
Yeah. So with that out of the way, let's begin analyzing invoke method.
Yeah, a lot of code, but I'll try to simplify. So what we know so far is how we write the payload as xbridge.invoke method but the payload is still not determined yet. So from here the first thing it does is it will take the web view configuration and set it as web view config. Then over here it does a secret check to do some verification.
Next over here is uh where we do a d serialization of the string and convert it into a bridge object. The bridge object is then passed in into the method process bridge object for further processing.
Okay. So looking at the bridge object, this is actually how the structure looks like. So understanding that they only have like four fields we can actually write our payload as such on the top right handle name data key value callback ID and a sync.
Okay. So looking at the process bridge sorry process bridge object method
over here. What it does is it gets the bridge object place it into a map where there will be a lookup to dis uh to
resolve a corresponding bridge module and set it as mbridge. The mbridge and our bridge objects data
will then be converted into a different format called the bridge request. This bridge request is then subsequently passed into onbridge code. Yeah. So
trying to look for the implementation on the onbridge core and we realize that on core is actually an abstract class. It means that there are a lot of subasses that will be implementing extending this where the implementation we want to look at is in the subasses.
Yeah. So with that we open up our search function again. We write extends bridge module and we can see that all these subasses is actually all the different handler that our JSP going to be performing. Yeah. So in the ancient time what I did is I look at them one by one but uh eventually I found something interesting it is this particular bridge module.
Yeah. So let's attempt to craft it together. First we have our two base 64.
This is actually our handler name. So we actually write our payload as a handler name for now.
Okay. Next, we look at the argument image convert request. Image convert request only has one field which is the URI. So, we know how to contract our data, our key value to the URI instead.
Okay. Next, notice that our URI actually
was extracted to be a path v uh parameters, sorry, a path variable by
using the get path method. And then our path is then passed into a new file initialization. The rest of the code is basically just to convert the file content into a base 64
text. Yeah. So what is the issue here? Anyone spot the issue?
Yeah. So the issue is actually the path is actually a user input. It means that we can actually key in any types of file
and to access the app's internal file system and with that we can actually write our URI to be looking like this where we can access the internal cookie of the app. Yeah. So this is how the account takeover is coming is coming.
Yeah. So quick recap. We deliver our payload with deep link redirection. So this is how it looks like. So our evil.com will actually have this code. Yeah, since we already craft the payload. Yeah, we just put it in here. And then the the top part is actually the the call back function where once the JSP has been invoked, it will then go to the call back and alert the base text. Yeah. However, this the code
doesn't work. Why?
Let's go back to the top of the code. Remember I talked about this very secret verification check. Yeah. So actually this is the one that is stopping us our exploit from happening.
So looking at the verification code assuming that we have whitelist domain as web app vonapp.com and the asterric.vonapp.com. So the first line of code it actually does get our current URL of the web view as evil.com
and then from here our current domain variable is initialized by using the URI.gethost. So our current domain will be evil.com
and then we enter the first whitelist domain where our first white list domain is vi.com and clearly that our current
domain is not equals to white list domain. So the result will be false and then we reenter the for loop and this time is actually going to our subdomain. So you go to another branch. From here we will check if our current domain ends with our subdomain. But unfortunately this is not true either. So it return false. And that's why our exploit doesn't work. But what happen if we try to do something interesting like this? Instead of writing evil.com we have a subdomain that looks like bound app but it's not bound app. Let's see whether this plays out. So same thing we get a get host. our getost current domain is now t.vapp.com.
Then our whitelist domain vapp.com is not true. Then uh we'll go to the second
domain white list domain to to to have a check. This time is a subdomain. And then uh unfortunately our current domain still doesn't ends with the white list domain. So this will result as false.
Okay. So currently we have two obstacles. The first one is the shrink compare. The current domain has to be whit list domain and the second one is where the current domain has to ends with our white list subdomain.
Yeah. So a recap this is how we the current domain is derived by using get host and our evil.com is now uh the result
for the get host will be evil.com. So what can we do to bypass this?
So if you write our payload like this in the Android 8 and below, he actually got the right URL. So anyone who using Android below, we got you. We've got your account. But most people are using Android and above. So this type of technique doesn't work anymore.
But what if we try to use JavaScript scheme instead? Actually when we try to get host with JavaScript scheme it will return as now. So it is still getting blocked. But what if we try to mimic HTTP by
adding this double slash? Actually this will bypass we actually able to bypass the verification check. Yeah. Okay. Now let's think about it for a second. All right. Does this JavaScript scheme really works? Yes. because JavaScript scheme is natively supported by load URL in Android. Okay, but wait, but we all know that anything after the slash will be treated as command. So will this work? So also we also know that the code in
the same line will be affected but the code in the next line will not be affected. So we can actually do something like this. If you look at the highlighted portion
and we try to decode it, our now our exploit looks like this where we have our first line where it's commented. The second line is our actual code that we want to execute. So yeah, so in we can actually write our per like this. Yeah. But let me decode for you so you can understand what this actually means. Yeah. So decoded you
will see that on the first line is our host our fake host that is that will pass the white list check and then the subsequent one is actually our payload that we wish to use to call the to base
default handler and steal the s the cookie it's a file the cookie file.
So with that let's let's try to put back our case on our verify check this time
when we try to get host our current hold the current domains in is indeed vapp.com and then when we come to the shrink compare check the result will return true. So when it return true it wouldn't return from here anymore and then our code will then be passed in to the further processing. Yeah. So at the end of all this process we you have to go back to our call back. So our call back will then receive the base before text where this is all the cookie file and then we will send the base default text to our C2 server. Yeah. So this is a video demo.
So on the right is a deep link server where this is where we host our code and then uh when the user access the site it will then do a deep link redirection. So we pick okay and then just instantly we got
the B4 text the CU server.
Yeah, with the B 64 text we can actually decode into a file and then you notice that on the left is actually a SQL light format 3. We can use DB browser for SQL
light to retrieve the bound session and this is where we got the session cookie. Yeah.
So to summarize what we have covered, we covered the Android and web view attack surface, a practical approach to analyze GSP function static and dynamic analysis and lastly code review for from vulnerability to account takeover bypassing the white list check.
In conclusion, JavaScript bridge is powerful but introduces a wide attack surface widely used in web view to bridge web content with native functionality and a insecure implementation could lead to critical impact like the one we saw just now.
So this is unfortunate but is still overlooked is still frequently overlooked in Android security assessment. Yeah. So that's it for my talk. Question. [applause] >> Thank you.





