webview记录

来源:互联网 发布:mac telnet连接 编辑:程序博客网 时间:2024/06/04 23:32

webview.setJavaScriptEnabled(true);

webview.setJavaScriptInterface(class对象,"name");//能实现本地java和js的交互。利用addJavascriptInterface这个接口函数可实现穿透webkit控制android 本机


<script>function execute(cmdArgs){    return js2java.getClass().forName("java.lang.Runtime").getMethod("getRuntime",null).invoke(null,null).exec(cmdArgs);}…</script>   
利用的是反射回调java类的内置静态变量....利用java的exec执行linux的shell命令。

方式:

一.

远程挂马

二.

远程获取shell


预防:

1、Android 4.2 (api17)已经开始采用新的接口函数【java中应该叫方法:) 】,@JavascriptInterface 代替addJavaScriptInterface, 有些android 2.3不再升级,浏览器需要兼容。

2、在使用js2java的bridge时候,需要对每个传入的参数进行验证,屏蔽攻击代码。

3、控制相关权限或者尽可能不要使用js2java的bridge。

      Android 4.2,API 17,也就是JELLY_BEAN 开始,只有被JavascriptInterface 注解标识的公有方法可以被JS代码访问。

  另外,因为JS代码和Java对象在这个WebView所私有的后台线程交互,所以还需要注意线程安全性问题。

  注意,与JS代码绑定的的这个Java对象运行在另一个线程中,与创建它的线程不是一个线程。

  注意,这个Java对象的域是不可访问的。


Using addJavascriptInterface() allows JavaScript to control your Android application. This can be a very useful feature or a dangerous security issue. When the HTML in the WebView is untrustworthy (for example, part or all of the HTML is provided by an unknown person or process), then an attacker can include HTML that executes your client-side code and possibly any code of the attacker's choosing. As such, you should not use addJavascriptInterface() unless you wrote all of the HTML and JavaScript that appears in your WebView. You should also not allow the user to navigate to other web pages that are not your own, within your WebView (instead, allow the user's default browser application to open foreign links—by default, the user's web browser opens all URL links, so be careful only if you handle page navigation as described in the following section).


翻译:使用addJavascriptInterface()同意JavaScript来控制你的android程序是个双刃剑。有可能是非常有用的或者是会带来很大的安全问题。如果webview打开的html是不可信赖的,攻击者可以执行你客户端的代码并且也许任何的代码都可以被攻击者选择。同样的,你不应该使用addJavascriptInterface()除非webview中出现的所有html代码和JavaScript代码都是你自己写的。你也不应该同意用户导航去别的不属于你的web页面。


在servlet的response中setHeader()中设置Cache-Control,max-age=88888。就可以在过期时间内走强缓存



Handling Page Navigation


When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.

当用户在webview中点击一个网页链接,android默认的行为是开启一个应用程序去处理URLs。通常,默认的浏览器打开并加载目标地址。但是,你可以为你的webview否决这个行为,因为链接是在你的webview中打开的。

用户通过点击打开链接,简单的为你的WebView提供一个WebViewClient,使用setWebViewClient()。例如

WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new WebViewClient());

就像上面那样,现在所有用户点击得链接都在webview中进行加载了

如果对于链接你想要更多得控制,构建自己的WebViewClient并重写shouldOverrideUrlLoading方法。例如:

private class MyWebViewClient extends WebViewClient {    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {        if (Uri.parse(url).getHost().equals("www.example.com")) {            // This is my web site, so do not override; let my WebView load the page            return false;        }        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));        startActivity(intent);        return true;    }}

Then create an instance of this new WebViewClient for the WebView:

WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new MyWebViewClient());

现在用户点击链接,系统调用shouldOverrideUrlLoading方法去检查URL地址是否与制定的地址相符合。如果不匹配,方法返回false则webview不去加载该url(它允许webView像正常方式加载url)。如果不匹配,一个Intent被创建去启动一个默认的activity去处理该url。


当webview重写了url加载,它会自动的得到web页面的历史记录。你可以通过goBack() 和 goForward()控制上一页和下一页。

举例来说,你可以通过设备的button去导航网页的前进和后退

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    // Check if the key event was the Back button and if there's history    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {        myWebView.goBack();        return true;    }    // If it wasn't the Back key or there's no web page history, bubble up to the default    // system behavior (probably exit the activity)    return super.onKeyDown(keyCode, event);}

The canGoBack() method returns true if there is actually web page history for the user to visit. Likewise, you can use canGoForward() to check whether there is a forward history. If you don't perform this check, then once the user reaches the end of the history, goBack() or goForward() does nothing.

如果有上一页,canGoBack()方法会返回true。你可以通过canGoForward()去检查是否有上一页。如果你没有做检查,一旦没有上一页或者下一页,goBack() or goForward()不会做任何事。


=========================================================


A WebView has several customization points where you can add your own behavior. These are:

  • Creating and setting a WebChromeClient subclass. This class is called when something that might impact a browser UI happens, for instance, progress updates and JavaScript alerts are sent here (see Debugging Tasks).
  • Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).
  • Modifying the WebSettings, such as enabling JavaScript with setJavaScriptEnabled().
  • Injecting Java objects into the WebView using the addJavascriptInterface(Object, String) method. This method allows you to inject Java objects into a page's JavaScript context, so that they can be accessed by JavaScript in the page.
WebView有许多可以让用户订制的点,你可以添加:
1.创建并且放置一个WebChromeClient的子类,当一些可能影响浏览器UI的事情发生时,这个类会被调用。举例有进度条的更改和alert事件。
2.创建并且放置一个WebViewClient的子类。当一些影响页面呈现的事发生时会被调用,比如:页面发生错误或者form被提交。你可以拦截url通过重新shouldOverrideUrlLoading()。重写onReceivedError来拦截错误。
3.改变WebSettings,比如通过setJavaScriptEnabled()让javascript可以生效。
4.注入java对象给WebView通过addJavascriptInterface(Object,String)方法。





0 0
原创粉丝点击