-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapkAnalyzer.ts
More file actions
354 lines (300 loc) · 10.2 KB
/
Copy pathapkAnalyzer.ts
File metadata and controls
354 lines (300 loc) · 10.2 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// src/services/apkAnalyzer.ts
// APK 分析主逻辑 - 整合所有模块
import JSZip from 'jszip';
import { convertAxmlToXml, extractManifestInfo, extractComponents } from '../utils/axmlParser';
import { scanApk, ScanResult } from './sdkScanner';
import { loadRules } from './rulesLoader';
import { fuzzyMatchLibraryWithCache } from '../utils/fuzzyMatcher';
import { AnalysisResult, Library, RulesBundle, AnalysisProgress } from '../types';
/**
* 分析 APK 文件
* @param file - APK 文件
* @param onProgress - 进度回调
* @returns 分析结果
*/
export async function analyzeApk(
file: File,
onProgress?: (progress: AnalysisProgress) => void
): Promise<AnalysisResult> {
console.log(`🚀 开始分析 APK: ${file.name} (${(file.size / 1024 / 1024).toFixed(2)} MB)`);
try {
// 阶段 1: 提取 APK 文件
onProgress?.({
stage: 'extracting',
message: '正在提取 APK 文件...',
progress: 10,
});
const zip = await JSZip.loadAsync(file);
console.log('✓ APK 文件提取成功');
// 阶段 2: 解析 AndroidManifest.xml
onProgress?.({
stage: 'parsing',
message: '正在解析 AndroidManifest.xml...',
progress: 30,
});
const manifestFile = zip.file('AndroidManifest.xml');
if (!manifestFile) {
throw new Error('未找到 AndroidManifest.xml 文件');
}
const manifestBuffer = await manifestFile.async('arraybuffer');
const xmlText = convertAxmlToXml(manifestBuffer);
const basicInfo = extractManifestInfo(xmlText);
const components = extractComponents(xmlText);
const parsedManifest = {
xmlText,
...basicInfo,
...components,
};
console.log('✓ AndroidManifest.xml 解析成功');
console.log(` - 包名: ${basicInfo.packageName}`);
console.log(` - 版本: ${basicInfo.versionName} (${basicInfo.versionCode})`);
// 阶段 3: 扫描 SDK 库和组件
onProgress?.({
stage: 'scanning',
message: '正在扫描 SDK 库和组件...',
progress: 50,
});
const scanResult = await scanApk(zip, parsedManifest);
console.log('✓ SDK 扫描完成');
// 阶段 4: 加载规则库
onProgress?.({
stage: 'matching',
message: '正在加载规则库...',
progress: 60,
});
const rules = await loadRules();
if (!rules) {
throw new Error('加载规则库失败');
}
console.log('✓ 规则库加载成功');
console.log(` - 版本: ${rules.version}`);
console.log(` - 总规则数: ${rules.totalRules}`);
// 阶段 5: 匹配规则库
onProgress?.({
stage: 'matching',
message: '正在匹配 SDK 库...',
progress: 80,
});
const libraries = await matchLibraries(scanResult, rules);
console.log('✓ 规则库匹配完成');
console.log(` - 匹配到 ${libraries.length} 个库`);
// 阶段 6: 生成分析结果
onProgress?.({
stage: 'completed',
message: '分析完成',
progress: 100,
});
const stats = calculateStats(libraries);
const result: AnalysisResult = {
basic: basicInfo,
libraries,
stats,
manifestXml: xmlText,
timestamp: new Date().toISOString(),
};
console.log('✅ APK 分析完成!');
return result;
} catch (error) {
console.error('❌ APK 分析失败:', error);
onProgress?.({
stage: 'error',
message: `分析失败: ${error instanceof Error ? error.message : '未知错误'}`,
progress: 0,
});
throw error;
}
}
/**
* 匹配规则库
*/
async function matchLibraries(
scanResult: ScanResult,
rules: RulesBundle
): Promise<Library[]> {
const libraries: Library[] = [];
const libraryMap = new Map<string, Library>(); // 用于去重
// 1. 匹配 Native 库
console.log('🔍 开始匹配 Native 库...');
for (const libName of scanResult.nativeLibs) {
const matched = fuzzyMatchLibraryWithCache(libName, rules.rules.native);
// 获取这个库的详细信息(包含 count、locations、architectures)
const libInfo = scanResult.nativeLibsMap.get(libName);
if (matched) {
// 统一使用 UUID 作为唯一标识,实现跨文件名合并
const libraryKey = matched.uuid || matched.id;
// 如果已存在,合并信息
if (libraryMap.has(libraryKey)) {
const existing = libraryMap.get(libraryKey)!;
// 合并检出次数
if (libInfo) {
existing.count = (existing.count || 0) + libInfo.count;
// 合并位置信息
existing.locations = existing.locations || [];
existing.locations.push(...libInfo.locations);
// 合并架构信息
existing.architectures = existing.architectures || [];
libInfo.architectures.forEach(arch => {
if (!existing.architectures!.includes(arch)) {
existing.architectures!.push(arch);
}
});
}
} else {
// 新库,直接使用 libInfo 中的信息
const library: Library = {
...matched,
count: libInfo?.count || 0,
locations: libInfo?.locations || [],
architectures: libInfo?.architectures || [],
hasMetadata: true,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
} else {
// 未匹配到元数据
const libraryKey = libName;
if (!libraryMap.has(libraryKey)) {
const library: Library = {
id: libraryKey,
uuid: '',
name: libName,
label: libName,
category: 'other',
categoryLabel: '其他',
categoryIcon: '📦',
developer: 'Unknown',
description: '未识别的库',
sourceLink: '',
type: 'native',
count: libInfo?.count || 0,
locations: libInfo?.locations || [],
architectures: libInfo?.architectures || [],
hasMetadata: false,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
}
}
console.log(`✓ Native 库匹配完成: ${libraryMap.size} 个库`);
// 2. 匹配 Activity 组件
console.log('🔍 开始匹配 Activity 组件...');
for (const activityName of scanResult.activities) {
const matched = fuzzyMatchLibraryWithCache(activityName, rules.rules.activities);
if (matched) {
// 使用 UUID 作为唯一标识,而不是 ID
// 同一个 SDK 的多个组件会有相同的 UUID
const libraryKey = matched.uuid || matched.id;
if (libraryMap.has(libraryKey)) {
// 已存在,增加检出次数
const existing = libraryMap.get(libraryKey)!;
existing.count = (existing.count || 0) + 1;
// 可以选择记录具体的组件名称到 locations
if (!existing.locations) existing.locations = [];
existing.locations.push(`Activity: ${activityName}`);
} else {
// 新 SDK
const library: Library = {
...matched,
count: 1,
locations: [`Activity: ${activityName}`],
hasMetadata: true,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
}
}
// 3. 匹配 Service 组件
console.log('🔍 开始匹配 Service 组件...');
for (const serviceName of scanResult.services) {
const matched = fuzzyMatchLibraryWithCache(serviceName, rules.rules.services);
if (matched) {
const libraryKey = matched.uuid || matched.id;
if (libraryMap.has(libraryKey)) {
const existing = libraryMap.get(libraryKey)!;
existing.count = (existing.count || 0) + 1;
if (!existing.locations) existing.locations = [];
existing.locations.push(`Service: ${serviceName}`);
} else {
const library: Library = {
...matched,
count: 1,
locations: [`Service: ${serviceName}`],
hasMetadata: true,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
}
}
// 4. 匹配 Provider 组件
console.log('🔍 开始匹配 Provider 组件...');
for (const providerName of scanResult.providers) {
const matched = fuzzyMatchLibraryWithCache(providerName, rules.rules.providers);
if (matched) {
const libraryKey = matched.uuid || matched.id;
if (libraryMap.has(libraryKey)) {
const existing = libraryMap.get(libraryKey)!;
existing.count = (existing.count || 0) + 1;
if (!existing.locations) existing.locations = [];
existing.locations.push(`Provider: ${providerName}`);
} else {
const library: Library = {
...matched,
count: 1,
locations: [`Provider: ${providerName}`],
hasMetadata: true,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
}
}
// 5. 匹配 Receiver 组件
console.log('🔍 开始匹配 Receiver 组件...');
for (const receiverName of scanResult.receivers) {
const matched = fuzzyMatchLibraryWithCache(receiverName, rules.rules.receivers);
if (matched) {
const libraryKey = matched.uuid || matched.id;
if (libraryMap.has(libraryKey)) {
const existing = libraryMap.get(libraryKey)!;
existing.count = (existing.count || 0) + 1;
if (!existing.locations) existing.locations = [];
existing.locations.push(`Receiver: ${receiverName}`);
} else {
const library: Library = {
...matched,
count: 1,
locations: [`Receiver: ${receiverName}`],
hasMetadata: true,
expanded: false,
};
libraryMap.set(libraryKey, library);
}
}
}
// 转换为数组并排序
libraries.push(...Array.from(libraryMap.values()));
libraries.sort((a, b) => a.label.localeCompare(b.label));
return libraries;
}
/**
* 计算统计信息
*/
function calculateStats(libraries: Library[]): AnalysisResult['stats'] {
const byCategory: Record<string, number> = {};
const byType: Record<string, number> = {};
libraries.forEach(lib => {
// 按分类统计
byCategory[lib.category] = (byCategory[lib.category] || 0) + 1;
// 按类型统计
byType[lib.type] = (byType[lib.type] || 0) + 1;
});
return {
total: libraries.length,
byCategory,
byType,
};
}