Google Play Service中安全检测API的使用说明
来源:互联网 发布:qq阅读网络异常怎么办 编辑:程序博客网 时间:2024/05/16 05:08
本篇博客主要记录一下如何利用Google Play Service进行安全检测。
1 Google Play Service的使用原理
Google Play Service提供了大量的API供用户使用,
以便让应用低成本地集成Google最新的产品及功能。
应用使用Google Play Service时,基本的原理如下图所示,其中:
1、应用在使用Google Play Service时,
必须集成Google提供的Client Library,
通过Client Library,应用才能与Google Play Service进行IPC通信。
2、Google Play Service是独立运行在系统后台的服务,
与Google Play Store共同发布。
3、Google Play Store负责更新Google Play Service。
可以看出,Google通过引入Client Library后,
将应用与Google Play Service解耦。
这样即使不修改应用,只要更新了Google Play Services,
应用就能够使用到Google提供功能的最新版本。
同时,Client Library还可以进行准入控制等相关操作。
2 集成Client Library
为了使用Client Library,首先我们需要利用Android Studio的SDK Manager加载Google Repository。
如上图所示,主要是勾选其中的红线部分,然后点击下载即可。
下载完Google Repository后,只需要修改应用Module对应的build.gradle文件,
加载对应的库文件即可。
例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
这里需要注意的是,com.google.android.gms:play-services引入的是Google Play Service全体API的集合。
当我们仅需要特定API接口时,可以仅引入独立的库,
这样可以减小应用的体积,避免应用中方法数过多。
例如,仅需要SafetyNet时,可以仅compile:
- 1
目前,Play Service定义的所有独立API库可以参考如下链接:
https://developers.google.com/android/guides/setup
3 确保设备上的Google Play Service可用
当应用导入了Client Library后,我们就可以在应用中使用Client Library提供的接口了。
不过,我们知道一个应用将被安装到不同厂商的机器上,
部分厂商的ROM中并没有集成Google Play Store,更别提Google Play Service了。
因此,当应用需要使用Google Play Service的功能时,
首先需要做的就是判断当前设备是否具有对应的能力。
目前从文档来看,判断Google Play Service是否可用,
最好的办法就是使用Client Library中GoogleApiAvailability提供的接口。
具体的做法类似于:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
4 利用Google API Client访问服务
当device支持Google Play Service时,应用就可以使用对应的功能了。
如下图所示,应用实际上必须使用Client Library中的Google API Client来访问具体的服务。
从图中可以看出,Google API Client作为应用与服务的桥梁,负责具体的通信细节。
在应用中获取Google API Client的方法如下所示:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
一旦应用创建出GoogleApiClient,同时成功连接Google Play Service后,
就可以通过对应的API,使用相应的功能了。
5 SafetyNet的安全检测功能
接下来我们以SafetyNet为例,看看如何使用Google Play Service提供的安全检测功能。
5.1 Attestation API
从文档和测试返回的结果来看,Attestation API主要用于检测:
应用所在设备Android运行环境的安全性和兼容性,有点类似CTS测试的味道。
需要注意的是:
在使用这个API前,需要进入 Google Developers Console,
开通 Android Device Verification API 并申请对应的Apk key。
具体的网址可以自行搜索,有Google邮箱即可申请。
申请到key值后,需要在应用的AndroidManifest.xml中添加如下内容:
- 1
- 2
- 3
当添加完key值后,应用就可以使用SafetyNet的attest接口了。
这里我们以一种老式的调用方式为例,介绍一下API的使用方式:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
需要说明的是:
该API每日访问Google服务的次数是受限制的(估计因为用的是免费的,所以才受限吧)。
实际上,Google API控制台会统计每个API的请求次数,如下图所示:
5.2 Safe Browsing API
Safe Browsing API主要用于检测:
某个url对应的网址是否具有潜在威胁。
与Attestation API一样,使用该API前同样需要进入Google Developers Console,
开通Safe Browsing API并申请对应的App key值。
在使用时,需要在AndroidManifest.xml中添加类似如下内容:
- 1
- 2
- 3
- 4
应用使用该API的方式类似于:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
5.3 Verify Apps API
最后,我们看看Google Play Service的Verify Apps API。
这个API主要用于检测:
手机上已安装的应用是否有害。
与前面两个API不同的是,在使用这个API前,必须开启终端对应的Feature。
对应的代码类似于:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
一旦Verify App对应的Feature开启后,我们就可以利用Verify App的API了,其中老式的用法类似于:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
最后,我们来看看官方文档推荐的新的调用方式:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
需要说明的是:
自己测试时发现,listHarmfulApps接口并不稳定,在Android 6.0上偶尔会遇到编码为12004的错误。
参考Google的API文档,发现这是Verify App API的internal error。
此外,该接口在Android 7.0平台上,似乎也没法有效使用(不知是否与具体厂商有关)。
最后,我不得不吐槽一下,这个接口的能力简直是渣渣,居然几乎难以有效检测出威胁样本。
6 总结
本篇文档主要介绍了Google Play Service的原理和集成方法,
并以SafetyNet API为例,介绍了应用如何使用Google Play Service提供的功能。
参考文献
https://developers.google.com/android/guides/overview
https://developer.android.com/training/safetynet
原文地址: http://blog.csdn.net/gaugamela/article/details/73658554
- Google Play Service中安全检测API的使用说明
- Google Play Service中安全检测API的使用说明
- 谷歌发布基于机器学习的Android APP安全检测系统:Google Play Protect
- 安装 Google play service
- google play app 权限使用说明
- google play app 权限使用说明
- google-play-services_lib的应用检查是否有google service
- 关于Google Map API的使用说明
- 国产手机不支持google play Service 的解决方案(小米2)
- Google Maps API v2 android版本开发 国内手机不支持google play Service相关问题解决--图文教程
- 免费Google地图API使用说明
- Google脏话检测API
- 检测设备是否支持Google Play服务
- Google Play服务中的人脸检测
- 检测设备是否支持Google Play服务
- Google Play服务中的人脸检测
- 检测设备是否支持Google Play服务
- Google Play Service out of date问题解决
- Hadoop/MapReduce 及 Spark KMeans聚类算法实现
- Codeforces Round #446 (Div. 2) B. Wrath(线段树,RMQ,区间最值)
- 多个div 缓冲运动—变宽
- 干货:FLAG的风头过后,哪一家才是最好的公司?
- iOS开发-测量APP启动耗时
- Google Play Service中安全检测API的使用说明
- 感恩节到来,还在找一个和家人聚聚朋友叙叙的地方吗?
- daterangepick时间控件双日历设置时分秒
- 数据结构线性表顺序存储C++实现
- SDUT-3378&&SDUT2040
- 格式化输出(常量练习)
- 强化学习
- 第6章 数据注解 System.ComponentModel.DataAnnotations 、注解后台原理、自定义验证ValidationAttribute、IValidatableObject
- Magento中getData和getOrigData的区别