-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApk.cpp
More file actions
441 lines (422 loc) · 18.4 KB
/
Copy pathApk.cpp
File metadata and controls
441 lines (422 loc) · 18.4 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "Apk.h"
#include <ValueVisitor.h>
#include <android-base/stringprintf.h>
#include <androidfw/AssetManager.h>
#include <dex/dex_file-inl.h>
#include <dex/dex_file.h>
#include <dex/dex_file_loader.h>
#include <io/StringStream.h>
#include <io/ZipArchive.h>
#include <text/Printer.h>
#include <utils/String8.h>
using ::android::ConfigDescription;
using ::android::base::StringPrintf;
namespace apkparser {
class XmlPrinter : public aapt::xml::ConstVisitor {
private:
aapt::text::Printer* printer_;
android::AssetManager* assetManager_;
android::ResTable_config config_;
std::map<std::string, std::string> displayNames_;
std::map<std::string, std::string> namespace_uri_prefix_; // 记录uri和prefix的对应关系
public:
explicit XmlPrinter(aapt::text::Printer* printer, android::AssetManager* assetManager)
: printer_(printer), assetManager_(assetManager) {
android::ResTable_config config;
memset(&config, 0, sizeof(android::ResTable_config));
config.language[0] = 'e';
config.language[1] = 'n';
config.country[0] = 'U';
config.country[1] = 'S';
config.orientation = android::ResTable_config::ORIENTATION_PORT;
config.density = android::ResTable_config::DENSITY_MEDIUM;
config.sdkVersion = 10000; // Very high.
config.screenWidthDp = 320;
config.screenHeightDp = 480;
config.smallestScreenWidthDp = 320;
config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
config_ = config;
assetManager_->setConfiguration(config_);
}
std::map<std::string, std::string> GetDisplayNames() { return displayNames_; }
std::string resolveAttribute(const aapt::xml::Attribute& attr, std::string* outError) {
// 资源条目的值没有映射到android::ResTable_entry(即:是内嵌到xml中的值). 直接返回
if (!attr.compiled_value) {
return attr.value;
}
aapt::Value* value = attr.compiled_value.get();
std::string attr_value;
if (aapt::ValueCast<aapt::String>(value)) { // in AndroidManifest.xml stringPool
attr_value = *(aapt::ValueCast<aapt::String>(value)->value)->data();
} else if (aapt::ValueCast<aapt::RawString>(value)) { // in AndroidManifest.xml stringPool
attr_value = *(aapt::ValueCast<aapt::RawString>(value)->value)->data();
} else if (aapt::ValueCast<aapt::BinaryPrimitive>(value)) { // 表示其它的android::Res_value.
aapt::io::StringOutputStream sout(&attr_value);
aapt::text::Printer p(&sout);
aapt::ValueCast<aapt::BinaryPrimitive>(value)->PrettyPrint(&p);
sout.Flush();
} else if (aapt::ValueCast<aapt::Reference>(value)) { // 引用其它资源
auto ref = aapt::ValueCast<aapt::Reference>(value);
// 先将attr_value设置为引用id:
// aapt::io::StringOutputStream sout(&attr_value);
// aapt::text::Printer p(&sout);
// ref->PrettyPrint(&p);
// sout.Flush();
if (!ref->id || !ref->id.value().is_valid()) {
if (outError != NULL) {
*outError = "reference id invalid";
}
return "";
}
// 转换成android::Res_value
android::Res_value resValue;
resValue.dataType = android::Res_value::TYPE_REFERENCE;
resValue.data = ref->id.value().id;
// 开始解决引用
if (!assetManager_) {
if (outError != NULL) {
*outError = "asset manager is null";
}
return "";
}
const android::ResTable& resTable = assetManager_->getResources(false);
if (resTable.getError() != android::NO_ERROR) {
if (outError != NULL) {
*outError = "resTable has err:" + android::statusToString(resTable.getError());
}
return "";
}
// 判断资源对应的package是否存在
auto resPackagExist = [](const android::ResTable& resTable,
const android::Res_value& resValue) {
for (size_t i = 0; i < resTable.getBasePackageCount(); i++) {
if (resTable.getBasePackageId(i) == (resValue.data >> 24)) {
return true;
}
}
return false;
};
if (!resPackagExist(resTable, resValue)) {
if (outError != NULL) {
*outError = "resource`s package not exist";
}
return "";
}
// 处理引用
ssize_t block = resTable.resolveReference(&resValue, 0);
if (block < 0) {
if (outError != NULL) {
*outError = "attribute value reference does not exist";
}
return "";
}
if (resValue.dataType != android::Res_value::TYPE_STRING) {
if (outError != NULL) {
*outError = "attribute is not a string value";
}
return "";
}
size_t len;
const char16_t* str =
resTable.valueToString(&resValue, static_cast<size_t>(block), NULL, &len);
attr_value = str ? android::String8(str, len).c_str() : "";
}
return attr_value;
}
std::map<std::string, std::string> getApplicationLabels(const aapt::xml::Attribute& attr,
std::string* outError) {
std::map<std::string, std::string> displayNames;
if (!assetManager_) {
if (outError != NULL) {
*outError = "asset manager is null";
}
return displayNames;
}
const android::ResTable& resTable = assetManager_->getResources(false);
if (resTable.getError() != android::NO_ERROR) {
if (outError != NULL) {
*outError = "resTable has err:" + android::statusToString(resTable.getError());
}
return displayNames;
}
android::Vector<android::String8> locales;
resTable.getLocales(&locales);
for (size_t i = 0; i < locales.size(); i++) {
const char* localeStr = locales[i].string();
assetManager_->setConfiguration(config_, localeStr != NULL ? localeStr : "");
std::string llabel = resolveAttribute(attr, outError);
if (llabel != "") {
if (localeStr == NULL || strlen(localeStr) == 0) {
displayNames["application-label"] =
android::ResTable::normalizeForOutput(llabel.c_str()).string();
} else {
std::string key = StringPrintf("application-label-%s", localeStr);
displayNames[key] =
android::ResTable::normalizeForOutput(llabel.c_str()).string();
}
}
}
assetManager_->setConfiguration(config_);
return displayNames;
}
void Visit(const aapt::xml::Element* el) override {
// 解析命名空间
printer_->Print(StringPrintf("<%s", el->name.c_str()));
for (const auto& decl : el->namespace_decls) {
if (decl.prefix.empty()) {
printer_->Print(StringPrintf(" xmlns=\"%s\"", decl.uri.c_str()));
} else {
printer_->Print(
StringPrintf(" xmlns:%s=\"%s\"", decl.prefix.c_str(), decl.uri.c_str()));
namespace_uri_prefix_[decl.uri] = decl.prefix;
}
}
// 解析属性
for (const auto& attr : el->attributes) {
// 属性命名空间
std::string attr_name;
if (attr.namespace_uri.empty()) {
attr_name = attr.name;
} else if (attr.namespace_uri == "http://schemas.android.com/apk/res/android") {
attr_name = "android:" + attr.name;
} else {
attr_name = "unknow:" + attr.name;
for (const auto& pair : this->namespace_uri_prefix_) {
if (pair.first.compare(attr.namespace_uri) == 0) {
attr_name = pair.second + ":" + attr.name;
break;
}
}
}
std::string attr_value = resolveAttribute(attr, NULL);
if (el->name == "application" && attr.name == "label") {
displayNames_ = getApplicationLabels(attr, NULL);
}
// xml转义字符, 替换字符串中的`“`为`"`
std::string::size_type pos = 0;
while ((pos = attr_value.find("\"", pos)) != std::string::npos) {
attr_value.replace(pos, 1, """);
pos += 6;
}
// 替换`&`为`&`
pos = 0;
while ((pos = attr_value.find("&", pos)) != std::string::npos) {
attr_value.replace(pos, 1, "&");
pos += 5;
}
printer_->Print(
StringPrintf(" %s=\"%s\"", attr_name.data(),
android::ResTable::normalizeForOutput(attr_value.data()).c_str()));
}
if (el->children.empty()) {
printer_->Println("/>");
} else {
printer_->Println(">");
}
// 遍历子节点
printer_->Indent();
printer_->Indent();
aapt::xml::ConstVisitor::Visit(el);
printer_->Undent();
printer_->Undent();
// 打印 end tag
if (!el->children.empty()) {
printer_->Println(StringPrintf("</%s>", el->name.c_str()));
}
}
};
std::unique_ptr<Apk> Apk::LoadApkFromPath(const std::string& path) {
// 加载zip文件
aapt::Source source(path);
std::string error;
std::unique_ptr<aapt::io::ZipFileCollection> collection =
aapt::io::ZipFileCollection::Create(path, &error);
if (!collection) {
std::cerr << "failed opening zip: " << error << std::endl;
return {};
}
// 创建AssetManager, 并加载resource.arsc
std::unique_ptr<android::AssetManager> assetManager(new android::AssetManager());
// addAssetPath 只要是规则的文件都能添加成功, 比如zip格式
// getResources(false) 如果没有资源,会先添加一个空资源包,然后返回空资源包, 所以不会得到ERROR
// 所以用getTableStringBlock函数判断是否有资源
// if (!assetManager.get()->addAssetPath(android::String8(path.c_str()), NULL) ||
// assetManager.get()->getResources(false).getError() != android::NO_ERROR ||
// assetManager.get()->getResources(false).getTableStringBlock(0)->getError() !=
// android::NO_ERROR) {
// std::cerr << "failed to load resource.arsc" << std::endl;
// return {};
// }
if (!assetManager.get()->addAssetPath(android::String8(path.c_str()), NULL)) {
std::cerr << "failed to load resource" << std::endl;
return {};
}
std::unique_ptr<Apk> result(new Apk(std::move(collection), std::move(assetManager)));
return result;
}
std::unique_ptr<std::pair<std::string, std::map<std::string, std::string>>> Apk::GetManifest()
const {
std::unique_ptr<std::pair<std::string, std::map<std::string, std::string>>> result(
new std::pair<std::string, std::map<std::string, std::string>>);
aapt::io::IFile* manifest_file = this->collection_.get()->FindFile(kAndroidManifestPath);
if (manifest_file == nullptr) {
return result;
}
// 判断是否存在resource.arsc, 如果不存在返回空对象
const android::ResStringPool* pool =
assetManager_.get()->getResources(false).getTableStringBlock(0);
if (pool->getError() == android::NO_INIT) { // 没有arsc
return result;
} else if (pool->getError() != android::NO_ERROR) {
std::cerr << "string pool is corrupt/invalid." << std::endl;
return {};
}
std::unique_ptr<aapt::io::IData> manifest_data = manifest_file->OpenAsData();
if (manifest_data == nullptr) {
std::cerr << "failed to read " << kAndroidManifestPath << std::endl;
return {};
}
std::string error;
std::unique_ptr<aapt::xml::XmlResource> manifest =
aapt::xml::Inflate(manifest_data->data(), manifest_data->size(), &error);
if (manifest == nullptr) {
std::cerr << "failed to parse " << kAndroidManifestPath << ": " << error << std::endl;
return {};
}
aapt::io::StringOutputStream sout(&result.get()->first);
aapt::text::Printer printer(&sout);
XmlPrinter xml_visitor(&printer, this->assetManager_.get());
manifest->root->Accept(&xml_visitor);
sout.Flush();
result.get()->second = xml_visitor.GetDisplayNames();
return result;
}
std::unique_ptr<std::list<std::string>> Apk::GetStrings() const {
std::unique_ptr<std::list<std::string>> result(new std::list<std::string>());
// 判断是否存在resource.arsc, 如果不存在返回空对象
const android::ResStringPool* pool =
assetManager_.get()->getResources(false).getTableStringBlock(0);
// 打印字符串
if (pool->getError() == android::NO_INIT) { // 没有arsc
return result;
} else if (pool->getError() != android::NO_ERROR) {
std::cerr << "string pool is corrupt/invalid." << std::endl;
return {};
}
for (size_t i = 0; i < pool->size(); i++) {
auto str = pool->string8ObjectAt(i);
if (str.has_value() && strlen(str.value().string()) > 0) {
result.get()->push_back(Apk::TrimString(str.value().string()));
}
}
return result;
}
std::unique_ptr<std::pair<std::set<std::string>, std::set<std::string>>> Apk::ParseDexes() const {
// 提取apk中的所有dex
std::vector<aapt::io::IFile*> dexes;
auto collection = this->collection_.get();
auto iter = collection->Iterator();
while (iter.get()->HasNext()) {
auto file = iter.get()->Next();
if (file->GetSource().path.rfind(".dex") != std::string::npos) {
dexes.push_back(file);
}
}
// 解析dex
std::unique_ptr<std::pair<std::set<std::string>, std::set<std::string>>> result(
new std::pair<std::set<std::string>, std::set<std::string>>);
for (auto&& file : dexes) {
std::unique_ptr<aapt::io::IData> data = file->OpenAsData();
if (data == nullptr || data->size() < 4) {
continue;
}
const uint8_t* base = reinterpret_cast<const uint8_t*>(data->data());
size_t size = data->size();
const std::string location = file->GetSource().path;
art::DexFileLoader dexFileLoader;
uint32_t magic = *reinterpret_cast<const uint32_t*>(base);
if (!dexFileLoader.IsMagicValid(magic)) {
continue;
}
std::string error_msg;
const art::DexFile::Header* dex_header =
reinterpret_cast<const art::DexFile::Header*>(base);
std::unique_ptr<const art::DexFile> dexFile =
dexFileLoader.Open(base, size, location, dex_header->checksum_,
/*oat_dex_file=*/nullptr, false, false, &error_msg);
if (dexFile == nullptr) {
continue;
}
// 遍历类
for (uint32_t i = 0; i < dexFile->NumClassDefs(); ++i) {
const char* descriptor = dexFile->GetClassDescriptor(dexFile->GetClassDef(i));
if (descriptor == nullptr || strlen(descriptor) == 0) {
continue;
}
// 去除首尾的 L 和; 将 / 转换为 .
std::string className(descriptor + 1, strlen(descriptor) - 2);
std::replace(className.begin(), className.end(), '/', '.');
// 截断匿名类, 存在多个匿名类的情况
auto pos = className.find("$");
if (pos != std::string::npos) {
className = className.substr(0, pos);
}
result.get()->first.insert(className);
}
// 遍历字符串
for (uint32_t i = 0; i < dexFile->NumStringIds(); ++i) {
const char* str =
dexFile->GetStringData(dexFile->GetStringId(art::dex::StringIndex(i)));
if (str == nullptr || strlen(str) == 0) {
continue;
}
std::string str2 = Apk::TrimString(str);
if (str2.empty()) continue;
result.get()->second.insert(str2);
}
}
return result;
}
std::unique_ptr<nlohmann::json> Apk::DoAllTasks() const {
// 解析manifest
// auto now = std::chrono::system_clock::now();
auto manifest = this->GetManifest();
if (!manifest) {
std::cerr << "parse manifest failed" << std::endl;
return {};
}
// auto end = std::chrono::system_clock::now();
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
// std::cout << "parse manifest cost " << duration.count() << "ms" << std::endl;
// 解析资源字符串
// now = std::chrono::system_clock::now();
auto strings = this->GetStrings();
if (!strings) {
std::cerr << "parse strings failed" << std::endl;
return {};
}
// end = std::chrono::system_clock::now();
// duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
// std::cout << "parse strings cost " << duration.count() << "ms" << std::endl;
// 解析dexes
// auto now = std::chrono::system_clock::now();
auto dexes = this->ParseDexes();
if (!dexes) {
std::cerr << "parse dexes failed" << std::endl;
return {};
}
// auto end = std::chrono::system_clock::now();
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - now);
// std::cout << "parse dexes cost " << duration.count() << "ms" << std::endl;
// 构造json
std::unique_ptr<nlohmann::json> result(new nlohmann::json());
result.get()->operator[]("manifest") = manifest.get()->first;
result.get()->operator[]("display_names") = manifest.get()->second;
nlohmann::json resStrings;
resStrings["strings"] = *strings.get();
result.get()->operator[]("resources_arsc") = resStrings;
result.get()->operator[]("dex_classes") = dexes.get()->first;
result.get()->operator[]("dex_strings") = dexes.get()->second;
return result;
}
} // namespace apkparser