详解APK静态分析引擎`quark-engine`的5大功能

1. 引入

2020年8月,在DEFCON 28大会上(参考3),发布了一个开源的"Android malware analysis engine",名字叫quark-engine(参考1)。这是台湾“財團法人電信技術中心”(参考2)开发的一个工具。该项目的开发者列表见参考7,目前主要活跃的开发者(负责人)是JunWei Song(参考3,参考5)。

这个APK静态分析引擎,有以下几个特点:

1.scoring system,能直接给出恶意度得分
2.neglect certain cases of obfuscation,对特定的一些混淆有抗混淆能力
3.方便使用,windows和linux平台都兼容。目前是基于androguard实现的

本文在windows上安装quark-engine并进行测试,给出测试结果。因为笔者动手测试时,还未见有类似的中文相关资料,所以记录下来供参考。

2. 安装

安装系统为64位的 windows 10 。

1.用conda新建虚拟python3.8环境

conda create --name env_quark_py38 python=3.8 

2.安装quark-engine

pip install -U quark-engine 

3.安装graphviz

  • graphviz.org/download/,下… stable_windows_10_cmake_Release_x64_graphviz-install-2.46.1-win64.exe
  • 双击exe安装
  • 运行命令 : conda install -c anaconda graphviz

至此,quark-engine安装完毕,安装过程非常简单。

3. 功能1:给出apk分析概述报告

用一条命令,就可以给出summary report

quark -a xxx.apk -s 

-s,就是summary的缩写,命令运行后,结果如下(考虑篇幅有限,这里只给出部分结果示例):

[!] WARNING: Moderate Risk
[*] Total Score: 153
+---------------------------------------------------------------------------+------------+-------+--------+
| Rule| Confidence | Score | Weight |
+---------------------------------------------------------------------------+------------+-------+--------+
| Initialize bitmap object and compress data (e.g. JPEG) into bitmap object | 60%| 1 | 0.25 |
| Open the camera and take picture| 20%| 1 | 0.0625 |
| Put the compressed bitmap data into JSON object | 60%| 1 | 0.25 |
| Get filename and put it to JSON object| 60%| 1 | 0.25 |
| Get absolute path of file and put it to JSON object | 60%| 1 | 0.25 |
| Scheduling recording task | 40%| 1 | 0.125|
| Use absolute path of directory for the output media file path | 40%| 1 | 0.125|
| Check if successfully sending out SMS | 40%| 1 | 0.125|
| Put data in cursor to JSON object | 60%| 1 | 0.25 |
| Read sensitive data(SMS, CALLLOG) and put it into JSON object | 60%| 1 | 0.25 |
| Query data from URI (SMS, CALLLOGS) | 100% | 1 | 1.0|
| Read data and put it into a buffer stream | 100% | 1 | 1.0|
| Read file and put it into a stream| 60%| 1 | 0.25 |
| Read file into a stream and put it into a JSON object | 60%| 1 | 0.25 |
| Put buffer stream (data) to JSON object | 40%| 1 | 0.125|
| Get location info of the device and put it to JSON object | 100% | 1 | 1.0|
| Get Location of the device and append this info to a string | 100% | 1 | 1.0|
| Get JSON object prepared and fill in location info| 60%| 1 | 0.25 | 

输入命令到得出结果,大概用了20秒,可以说是静态分析中比较快的了。

这条命令能给出的结果有

  • Risk的等级,比如这里是"Moderate Risk"
  • Risk得分,比如这里是 153
  • 匹配上的各条rule,以及 Confidence 和 这条规则的权重Weight

4. 功能2:给出apk分析详细报告(静态规则匹配)

用一条命令,就可以给出summary report

quark -a xxx.apk -d 

-d,就是detail的缩写,命令运行后,结果如下(考虑篇幅有限,这里只给出部分结果示例):

C:\xxx/.quark-engine/quark-rules\00017.json

Confidence: 100%[✓]1.Permission Request[✓]2.Native API Usage (Landroid/location/Location;, getLatitude) (Ljava/lang/StringBuilder;, append)[✓]3.Native API Combination (Landroid/location/Location;, getLatitude) (Ljava/lang/StringBuilder;, append)[✓]4.Native API Sequence Sequence show up in: Lorg/wikipedia/util/GeoUtil; sendGeoIntent (Landroid/app/Activity; Landroid/location/Location; Ljava/lang/String;)V[✓]5.Native API Use Same Parameter Lorg/wikipedia/util/GeoUtil; sendGeoIntent (Landroid/app/Activity; Landroid/location/Location; Ljava/lang/String;)V
[+] DONE: OK 

这条命令,会根据参考4中的quark-rules,去做静态匹配。比如这里匹配到100%的满足规则00017.json,而这条规则的内容如下:

{"crime": "Get Location of the device and append this info to a string","x1_permission": [],"x2n3n4_comb": [{"class": "Landroid/location/Location;","method": "getLatitude","descriptor": "()D"},{"class": "Ljava/lang/StringBuilder;","method": "append","descriptor": "(D)Ljava/lang/StringBuilder;"}],"yscore": 1,"label": ["location","collection"]
} 

各条json规则中,给出了详细的

  • API定义
  • 规则含义:“Get Location of the device and append this info to a string”
  • 规则的权重:yscore,行为越恶意,则yscore值越高(通过SMS泄露地理位置的yscore是4)

这条规则定义的是静态获取location的行为。

这条命令跑完需要1分钟左右。

5. 功能3:给出静态行为对应的代码位置

用一条命令,就可以给出各个静态行为,及其在DEX中的具体代码位置(借助JEB进行进一步分析)。

quark -a xxx.apk -s -c 

命令运行后,结果如下(考虑篇幅有限,这里只给出部分结果示例):

+-------------------+--------------------------------------------------------+
| Parent Function | Lnet/hockeyapp/android/PaintActivity;determineFilename |
+-------------------+--------------------------------------------------------+
| Crime Description | * Read sensitive data(SMS, CALLLOG, etc) |
+-------------------+--------------------------------------------------------+ 

可以得到各个行为对应到DEX代码中的具体位置。这个功能也是各个静态分析工具的必备功能(常用功能)。

6. 功能4:画出CFG图

用一条命令,就可以画出call graph

quark -a xxx.apk -s -g 

-g,就是graph的缩写,命令运行后,会生成一个call_graph_image文件夹,其中给出了各个CFG的png图片,这里给出一个示例:

[PIC-1]

png图片的文件名,也指定了这个CFG对应的行为,比如 sendGeoIntent_getLatitude_append.png。

7. 功能5:python支持

能直接在python代码里调用quark-engine进行分析,并获取分解结果,示例步骤如下:

1.pip安装quark-engine

pip install -U quark-engine==21.3.2 

2.下载quark-rules

下载地址见参考4,下载后保存到本地文件夹。 这里有个小坑,笔者已经提了issue(参考8),但只要按照本文的版本安装进行测试,就不会遇到这个问题了。

3.使用如下py代码

from quark.report import Report

APK_PATH = "xxx.apk"
RULE_PATH = "quark-rules-master/"

report = Report()

'''
RULE_PATH can be a directory with multiple rules inside
EX: "rules/"
OR special json rule file, such as "sendLocation_SMS.json"
'''
report.analysis(APK_PATH, RULE_PATH)
json_report = report.get_report("json")
print(json_report) 

得到的分析结果如下:

{
	'md5': 'xxx',
	'apk_filename': 'xxx.apk',
	'size_bytes': 22842706,
	'threat_level': 'Moderate Risk',
	'total_score': 153,
	'crimes': [{
		'crime': 'Initialize bitmap object and compress data (e.g. JPEG) into bitmap object',
		'score': 1,
		'weight': 0.25,
		'confidence': '60%',
		'permissions': [],
		'native_api': [{
			'class': 'Landroid/graphics/BitmapFactory;',
			'method': 'decodeByteArray'
		}, {
			'class': 'Landroid/graphics/Bitmap;',
			'method': 'compress'
		}],
		'combination': [{
			'class': 'Landroid/graphics/BitmapFactory;',
			'method': 'decodeByteArray',
			'descriptor': '([B I I)Landroid/graphics/Bitmap;'
		}, {
			'class': 'Landroid/graphics/Bitmap;',
			'method': 'compress',
			'descriptor': '(Landroid/graphics/Bitmap$CompressFormat; I Ljava/io/OutputStream;)Z'
		}],
		'sequence': [],
		'register': []
	}, {
		'crime': 'Open the camera and take picture',
		'score': 1,
		'weight': 0.0625,
		'confidence': '20%',
		'permissions': [],
		'native_api': [],
		'combination': [],
		'sequence': [],
		'register': []
	}, {
		'crime': 'Put the compressed bitmap data into JSON object',
		'score': 1,
		'weight': 0.25,
		'confidence': '60%',
		'permissions': [],
		'native_api': [{
			'class': 'Landroid/graphics/Bitmap;',
			'method': 'compress'
		}, {
			'class': 'Lorg/json/JSONObject;',
			'method': 'put'
		}],
		'combination': [{
			'class': 'Landroid/graphics/Bitmap;',
			'method': 'compress',
			'descriptor': '(Landroid/graphics/Bitmap$CompressFormat; I Ljava/io/OutputStream;)Z'
		}, {
			'class': 'Lorg/json/JSONObject;',
			'method': 'put',
			'descriptor': '(Ljava/lang/String; Z)Lorg/json/JSONObject;'
		}],
		'sequence': [],
		'register': []
	}, {
		'crime': 'Send binary data over HTTP',
		'score': 1,
		'weight': 0,
		'confidence': '0%',
		'permissions': [],
		'native_api': [],
		'combination': [],
		'sequence': [],
		'register': []
	}]
} 

能很清晰的看到它的结果,方便用python做批量测试。关于这个python包更具体的用法,见参考6。

8. 参考

1.github.com/quark-engin…
2.www.ttc.org.tw/mobile/inde…
3.forum.defcon.org/node/234086
4.github.com/quark-engin…
5.krnick.github.io/about/
6.quark-engine.readthedocs.io/en/latest/i…
7.quark-engine.readthedocs.io/en/latest/c…
ps://quark-engine.readthedocs.io/en/latest/integration.html")
7.quark-engine.readthedocs.io/en/latest/c…
8.github.com/quark-engin…## 网络安全工程师(白帽子)企业级学习路线

第一阶段:安全基础(入门)

img

第二阶段:Web渗透(初级网安工程师)

img

第三阶段:进阶部分(中级网络安全工程师)

img

如果你对网络安全入门感兴趣,那么你需要的话可以点击这里👉网络安全重磅福利:入门&进阶全套282G学习资源包免费分享!

学习资源分享

"pk-client-error-quark:e:failed to fetch"是一个错误消息,通常出现在使用包管理器(如apt、yum或dnf)时。这个错误消息表明系统无法从软件源中获取所需的软件包或更新。 造成这个错误的原因可能有很多,包括: 1. 无法连接到远程软件源:网络连接可能存在问题,或者软件源服务器可能无法访问。这可能是由于网络问题、服务器故障或软件源配置错误等原因造成的。 2. 软件源配置错误:软件源列表中的某些源的URL可能不正确,或者软件源可能已被删除或更改。 解决这个问题的方法可能因具体情况而异,但以下是一些常见的解决方法: 1. 检查网络连接:确保你的网络连接正常工作,并且可以访问互联网。尝试使用浏览器访问一些常见的网站,以确保网络连接正常。 2. 检查软件源配置:查看软件源列表文件(如/etc/apt/sources.list或/etc/yum.repos.d/)中的URL,确保没有拼写错误或错误的URL。如果需要,可以手动更改软件源配置文件来使用正常可用的软件源。 3. 更换软件源:如果软件源无法访问或出现其他问题,可以尝试切换到其他正常工作的软件源。根据不同的包管理器,可以找到并选择其他可用的软件源,并更新软件包列表。 综上所述,"pk-client-error-quark:e:failed to fetch"错误是由于系统无法从软件源中获取所需的软件包或更新所致,可能由网络连接问题或软件源配置错误引起。通过检查网络连接、修改软件源配置或更换软件源等方法,可以尝试解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值