当前位置: 首页 > news >正文

Frida 动态 Hook 安卓 WebView 与第三方内核完全指南


Frida 动态 Hook 安卓 WebView 与第三方内核完全指南
使用了虚构的「DragonWeb 内核」作为示例,避免了任何真实厂商的包名和标识:

在移动应用安全分析、逆向工程和漏洞挖掘中,WebView 是一个极其重要的攻击面。无论是传统的系统 WebView 还是第三方内核,对其内部方法的监控都至关重要。本文将详细介绍如何使用 Frida 这一强大动态插桩工具来 Hook 系统 WebView 和第三方内核的关键方法。

一、环境准备

  1. 安装 Frida
# 安装 Frida 命令行工具
pip install frida-tools
  1. 设备端配置

  2. Root 设备或模拟器(推荐 Genymotion 或已 Root 的真机)

  3. 下载并推送 frida-server 到设备:

    # 从 https://github.com/frida/frida/releases 下载对应版本
    adb push frida-server /data/local/tmp/
    adb shell
    su
    cd /data/local/tmp
    chmod 755 frida-server
    ./frida-server &
    
  4. 验证连接:

    frida-ps -U
    

二、WebView 类结构对比

功能 系统 WebView (Android) DragonWeb 内核 (第三方)
核心组件 android.webkit.WebView com.dragon.webkit.engine.WebView
事件处理 android.webkit.WebViewClient com.dragon.webkit.engine.WebViewClient
浏览器行为 android.webkit.WebChromeClient com.dragon.webkit.engine.WebChromeClient
JS 接口 @JavascriptInterface 同名注解

三、Frida Hook 脚本大全

  1. 通用 Hook 脚本(同时捕获系统和第三方内核)
// universal_webview_hook.js
Java.perform(function () {console.log("[*] Starting Universal WebView Hook...");// 要 Hook 的类列表var targetClasses = [// 系统 WebView 类"android.webkit.WebView","android.webkit.WebViewClient", "android.webkit.WebChromeClient",// DragonWeb 内核类"com.dragon.webkit.engine.WebView","com.dragon.webkit.engine.WebViewClient","com.dragon.webkit.engine.WebChromeClient","com.dragonwebkit.engine.WebView", // 可能的变体"com.dragonwebkit.engine.WebViewClient"];targetClasses.forEach(function(className) {try {var targetClass = Java.use(className);// Hook loadUrl 方法if ('loadUrl' in targetClass) {targetClass.loadUrl.overload('java.lang.String').implementation = function(url) {console.log("\n[🌐] " + className + ".loadUrl() called");console.log("[📝] URL: " + url);return this.loadUrl(url);};}// Hook WebViewClient 方法if ('onPageStarted' in targetClass) {targetClass.onPageStarted.implementation = function(view, url, favicon) {console.log("\n[▶️] " + className + ".onPageStarted: " + url);return this.onPageStarted(view, url, favicon);};}} catch (e) {// 类不存在是正常的,静默跳过}});
});
  1. 专用第三方内核 Hook 脚本
// thirdparty_webview_hook.js
Java.perform(function () {console.log("[*] Starting Third-party WebView Hook...");// DragonWeb 内核类var DragonWebView = Java.use("com.dragon.webkit.engine.WebView");var DragonWebViewClient = Java.use("com.dragon.webkit.engine.WebViewClient");var DragonWebChromeClient = Java.use("com.dragon.webkit.engine.WebChromeClient");// Hook loadUrlDragonWebView.loadUrl.overload('java.lang.String').implementation = function(url) {console.log("\n[🎯DragonWeb] WebView.loadUrl() called!");console.log("[📝] URL: " + url);console.log("[🔍] Call Stack: " + Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Exception").$new()));return this.loadUrl(url);};// Hook 页面事件DragonWebViewClient.onPageStarted.implementation = function(view, url, favicon) {console.log("\n[🌐DragonWeb] onPageStarted: " + url);return this.onPageStarted(view, url, favicon);};DragonWebViewClient.onPageFinished.implementation = function(view, url) {console.log("\n[✅DragonWeb] onPageFinished: " + url);return this.onPageFinished(view, url);};// Hook URL 拦截DragonWebViewClient.shouldOverrideUrlLoading.overload('com.dragon.webkit.engine.WebView', 'com.dragon.webkit.engine.model.WebResourceRequest').implementation = function(view, request) {var url = request.getUrl().toString();console.log("\n[🛑DragonWeb] shouldOverrideUrlLoading: " + url);return false; // 让内核处理请求};// Hook Console 日志DragonWebChromeClient.onConsoleMessage.overload('com.dragon.webkit.engine.model.ConsoleMessage').implementation = function(consoleMessage) {var msg = consoleMessage.message();console.log("\n[💬DragonWeb Console] > " + msg);return this.onConsoleMessage(consoleMessage);};
});
  1. JSBridge 接口监控脚本
// jsbridge_hook.js
Java.perform(function () {console.log("[*] Starting JSBridge Hook...");var classList = ["android.webkit.WebView","com.dragon.webkit.engine.WebView","com.dragonwebkit.engine.WebView","com.custom.webengine.WebView" // 其他可能的包名];classList.forEach(function(className) {try {var WebViewClass = Java.use(className);WebViewClass.addJavascriptInterface.overload('java.lang.Object', 'java.lang.String').implementation = function(obj, interfaceName) {console.log("\n[🤝JSBridge] " + className + ".addJavascriptInterface");console.log("[📝] Interface Name: " + interfaceName);console.log("[📝] Object Class: " + obj.$className);// 枚举所有可调用方法try {var clazz = obj.getClass ? obj.getClass() : obj.$class;var methods = clazz.getDeclaredMethods();console.log("[🔧] Exposed Methods:");for (var i = 0; i < methods.length; i++) {console.log("    - " + methods[i].getName());}} catch (e) {console.log("[❌] Error enumerating methods: " + e.message);}return this.addJavascriptInterface(obj, interfaceName);};} catch (e) {// 静默处理类不存在的情况}});
});

四、自动识别第三方内核

// detect_webview_engine.js
Java.perform(function () {console.log("[*] Detecting WebView Engines...");var thirdPartyIndicators = ["dragon", "webkit", "xengine", "uc", "quark", "crosswalk", "gecko", "blink", "custom", "webengine"];Java.enumerateLoadedClasses({onMatch: function(className) {// 检测系统 WebViewif (className.includes("android.webkit")) {console.log("[✓] System WebView detected: " + className);}// 检测第三方内核thirdPartyIndicators.forEach(function(indicator) {if (className.toLowerCase().includes(indicator) && className.includes("web") && !className.includes("android")) {console.log("[🎯] Third-party WebView detected: " + className);}});},onComplete: function() {console.log("[*] WebView detection completed");}});
});

五、使用方法

  1. 启动时注入
# 检测使用的内核
frida -U -f com.target.app -l detect_webview_engine.js --no-pause# 通用 Hook
frida -U -f com.target.app -l universal_webview_hook.js --no-pause# 专用第三方内核 Hook  
frida -U -f com.target.app -l thirdparty_webview_hook.js --no-pause# JSBridge 监控
frida -U -f com.target.app -l jsbridge_hook.js --no-pause
  1. 附加到运行中进程
frida -U com.target.app -l universal_webview_hook.js

六、实战技巧

  1. 动态扩展检测列表

如果发现新的第三方内核,可以动态添加到检测列表中:

// 在 detect_webview_engine.js 中添加
var additionalEngines = ["neweb", "fastweb", "smartweb"];
thirdPartyIndicators = thirdPartyIndicators.concat(additionalEngines);
  1. 过滤特定 URL
// 在 Hook 方法中添加过滤逻辑
var sensitiveKeywords = ["login", "auth", "token", "password"];DragonWebViewClient.shouldOverrideUrlLoading.implementation = function(view, request) {var url = request.getUrl().toString();if (sensitiveKeywords.some(keyword => url.toLowerCase().includes(keyword))) {console.log("\n[🔒敏感请求] " + url);// 进行深度分析...}return false;
};

七、常见问题排查

  1. 类找不到错误:正常现象,使用 try-catch 静默处理
  2. 方法签名变化:不同版本内核的方法签名可能不同
  3. 多版本兼容:使用 overload() 明确指定参数类型
  4. 性能优化:避免在频繁调用的方法中执行复杂操作

八、总结

通过 Frida 动态 Hook WebView,安全研究人员可以:

· 监控所有页面加载行为
· 捕获 JavaScript 与原生代码交互
· 分析 JSBridge 暴露的攻击面
· 记录 Console 日志和错误信息
· 发现 URL 跳转漏洞和协议处理问题

无论是系统 WebView 还是第三方内核,Frida 都能提供强大的动态分析能力,是移动应用安全测试中不可或缺的工具。


免责声明:本文仅用于安全研究和学习目的。请勿在未授权的情况下对任何应用进行测试。所有示例中的包名和类名均为虚构,如有雷同,纯属巧合。

http://www.dtcms.com/a/341440.html

相关文章:

  • 一种数字相机中的自动曝光算法
  • 01-Docker概述
  • 多摄像头多算法智能监控系统设计与实现
  • 关于 preprocessing.scale 函数
  • 机器语言、操作系统与硬件执行:深入解析计算机的底层逻辑
  • 【C++】模版(初阶)
  • 从“怀疑作弊”到“实锤取证”:在线面试智能监考重塑招聘公信力
  • CLEAN 函数
  • HTML 简明教程
  • Python 属性封装(Attribute Encapsulation)
  • Docker在Linux中安装与使用教程
  • ubuntu privileged cont 一直在读取硬盘
  • ubuntu24.04 frps服务器端自动启动设置【2025-08-20】
  • JUC之CompletableFuture【下】
  • 内网安全——出网协议端口探测
  • RAG拓展、变体、增强版(一)
  • 【深度学习-Day 43】解密LSTM:深入理解长短期记忆网络如何克服RNN的遗忘症
  • 8.20网络编程——sqlite3数据库
  • 计算机视觉(二):视觉的处理流程
  • Promise.all 速查与扩展实战
  • 基于SpringBoot的蜗牛兼职网平台
  • React框架超详细入门到实战项目演练【前端】【React】
  • Spring Retry实战指南_让你的应用更具韧性
  • PyTorch API 2
  • 漫漫长夜 全DLC(The Long Dark)免安装中文版
  • Docker之MySQL安装
  • Redis(以Django为例,含具体操作步骤)
  • 数字人制作全流程解析:从概念到落地的完整步骤​
  • 实战:本地大模型+function Calling,获取北京天气
  • uniapp学习【上手篇】