一、Androguard的下载
pip install androgard
若报错,可以在网站上下载androguard的包,在代码中导入。androguard网站:GitHub - androguard/androguard: Reverse engineering and pentesting for Android applications
然后执行命令:
python setup.py install
二、apk特征提取
首先我们需要导入androguard的其中一个方法:AnalyzeAPK
from androguard.misc import AnalyzeAPK
接着就可以调用提取特征的方法,我列举几个比较常用的:
# 提取权限
permissions = a.get_permissions()
# 提取活动
activities = a.get_activities()
# 提取服务
services = a.get_services()
# 提取接收器
receivers = a.get_receivers()
# 提取提供者
providers = a.get_providers()
# 判断 APK 是否有效
valid = a.is_valid_APK()
# 获取 APK 文件名,只保留文件名称部分
filename = os.path.basename(a.get_filename())
# 获取 APP 名
appname = a.get_app_name()
# 获取 package 名
package = a.get_package()
# 获取 android 版本名
version = a.get_androidversion_code()
# 获取 APK 文件列表
filelist = a.get_files()
注:apicalls的提取相对复杂,因为有格式的要求,我的方法是:
# 提取 API 调用信息
api_calls = set()
for method in dx.get_methods():
for _, calls, _ in method.get_xref_to():
class_name = calls.class_name[1:]
method_name = calls.name
descriptor = calls.descriptor
api_call = f"{class_name}->{method_name}{descriptor}"
api_calls.add(api_call)
这是androguard的基本方法,附上我的原代码:
# coding:utf-8
import os
import sys
import json
# 引入androguard的路径,根据个人存放的位置而定
androguard_module_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'androguard')
if androguard_module_path not in sys.path:
sys.path.append(androguard_module_path)
from androguard.misc import AnalyzeAPK
def extract_features(apk_path):
try:
out = AnalyzeAPK(apk_path)
a, d, dx = out
# 提取权限
permissions = a.get_permissions()
# 提取活动
activities = a.get_activities()
# 提取服务
services = a.get_services()
# 提取接收器
receivers = a.get_receivers()
# 提取提供者
providers = a.get_providers()
# 判断 APK 是否有效
valid = a.is_valid_APK()
# 获取 APK 文件名,只保留文件名称部分
filename = os.path.basename(a.get_filename())
# 获取 APP 名
appname = a.get_app_name()
# 获取 package 名
package = a.get_package()
# 获取 android 版本名
version = a.get_androidversion_code()
# 获取 APK 文件列表
filelist = a.get_files()
# 提取 API 调用信息
api_calls = set()
for method in dx.get_methods():
for _, calls, _ in method.get_xref_to():
class_name = calls.class_name[1:]
method_name = calls.name
descriptor = calls.descriptor
api_call = f"{class_name}->{method_name}{descriptor}"
api_calls.add(api_call)
features = {
"File": filename,
"Permissions": permissions,
"Activities": activities,
"Services": services,
"Receivers": receivers,
"Providers": providers,
"Valid": valid,
"Filename": filename,
"Appname": appname,
"Package": package,
"Version": version,
"Filelist": filelist,
"API_calls": list(api_calls)
}
return features
except Exception as e:
print(f"Error processing {apk_path}: {e}")
return None
def main():
apk_folder = "/exthome/dataset/az"
output_file = "apk_many_features.json"
all_features = []
for root, dirs, files in os.walk(apk_folder):
for file in files:
if file.endswith(".apk"):
apk_path = os.path.join(root, file)
features = extract_features(apk_path)
if features:
all_features.append(features)
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(all_features, f, indent=4)
print("APK features have been saved to:", output_file)
if __name__ == '__main__':
main()