-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
405 lines (330 loc) · 10.6 KB
/
main.cpp
File metadata and controls
405 lines (330 loc) · 10.6 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
/*
* File: main.cpp
* Author : angelToms
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <string.h>
#include <errno.h>
#include "mixer/Mixer.h"
#include "mixer/ArscMixer.h"
#include "zipapk/ApkZipOp.h"
#include "utils/FileScan.h"
#include "Common.h"
#include "QupLog.h"
//#define ONLY_PARSE_NOT_MIXUP
static void usage() {
QUP_LOGI("Usage :\n"
" arscmaker -t [python | bin] -p path -o [mixup | parse]\n"
" -t: type, -p: path, -o: operate\n"
" -t python:\n"
" use python zip tools\n"
" -t bin:\n"
" use system bin zip tools\n"
" path:\n"
" like /tmp/test/a.apk(mixup apk) or /tmp/test/(parse resources.arsc)\n"
" -o mixup:\n"
" mixup apk res and create a new apk, path must contain apk, like a.apk\n"
" -o parse:\n"
" only parse resources.arsc,if path contains apk, it will unzip and parse res, and remove it at last\n"
" ex:\n"
" arscmaker -t python -p /tmp/test/a.apk -o mixup");
exit(-1);
}
static void onlyParse(std::string ppath) {
std::string rstrdir(ppath);
rstrdir.append("resources.arsc");
std::string rfstrdir(ppath);
rfstrdir.append("res");
const char* rdir = rstrdir.c_str();
const char* rfdir = rfstrdir.c_str();
bool isCopy = false;
//parse全局日志开关
bool isLogAll = true;
//creator 全局日志开关, parse 二级日志开关(parse打印更详细的日志)
bool isLog = true;
//是否仅仅解析资源文件,而不是解析+混淆
bool isOnlyParse = true;
#ifdef ONLY_PARSE_NOT_MIXUP
isOnlyParse = true;
#endif
Mixer* mixer = new ArscMixer(TYPE_RESOURCE_ARSC,
isCopy,
isLogAll,
isLog,
isOnlyParse,
rdir,
rfdir);
if (!mixer) {
QUP_LOGI("[-] calloc mixer fail");
exit(-1);
}
if (!mixer->mixer()) {
exit(-1);
}
delete mixer;
mixer = NULL;
}
static void useBinParse(std::string apkpath) {
std::string apkstring(apkpath);
apkstring = apkstring.substr(0, apkstring.find(".apk"));
QUP_LOGI("[*] apk path = %s", apkstring.c_str());
std::string command("unzip -o -d ");
command.append(apkstring).append(" ").append(apkpath);
QUP_LOGI("[*] unzip command = %s", command.c_str());
if (system(command.c_str()) < 0) {
QUP_LOGI("[-] unzip apk fail %s", strerror(errno));
exit(-1);
}
const char* basedir = apkstring.c_str();
std::string rstrdir(basedir);
rstrdir.append("/");
rstrdir.append("resources.arsc");
std::string rfstrdir(basedir);
rfstrdir.append("/");
rfstrdir.append("res");
const char* rdir = rstrdir.c_str();
const char* rfdir = rfstrdir.c_str();
QUP_LOGI("[*] resource dir = %s, res file dir = %s,"
, rdir, rfdir);
bool isCopy = false;
//parse全局日志开关
bool isLogAll = true;
//creator 全局日志开关, parse 二级日志开关(parse打印更详细的日志)
bool isLog = true;
//是否仅仅解析资源文件,而不是解析+混淆
bool isOnlyParse = true;
#ifdef ONLY_PARSE_NOT_MIXUP
isOnlyParse = true;
#endif
Mixer* mixer = new ArscMixer(TYPE_RESOURCE_ARSC,
isCopy,
isLogAll,
isLog,
isOnlyParse,
rdir,
rfdir);
if (!mixer) {
QUP_LOGI("[-] calloc mixer fail");
exit(-1);
}
if (!mixer->mixer()) {
exit(-1);
}
QUP_LOGI("[*] iter remove %s ", basedir);
//iter remove unzip file
if (!iter_del_dir(basedir, 0)) {
QUP_LOGI("[-] iter remove %s fail", basedir);
exit(-1);
}
rmdir(basedir);
delete mixer;
mixer = NULL;
}
static void usePythonZipParse(std::string ppath) {
onlyParse(ppath);
QUP_LOGI("[*] iter remove %s ", ppath.c_str());
//iter remove unzip file
if (!iter_del_dir(ppath.c_str(), 0)) {
QUP_LOGI("[-] iter remove %s fail", ppath.c_str());
exit(-1);
}
rmdir(ppath.c_str());
}
static void useBinMixup(std::string apkpath) {
std::string filename(apkpath);
filename = filename.substr((filename.rfind("/") + 1));
filename = filename.substr(0, filename.rfind(".")).append(".new.apk");
QUP_LOGI("[*] new apk name = %s", filename.c_str());
std::string apkstring(apkpath);
apkstring = apkstring.substr(0, apkstring.find(".apk"));
QUP_LOGI("[*] apk path = %s", apkstring.c_str());
std::string command("unzip -o -d ");
command.append(apkstring).append(" ").append(apkpath);
QUP_LOGI("[*] unzip command = %s", command.c_str());
if (system(command.c_str()) < 0) {
QUP_LOGI("[-] unzip apk fail %s", strerror(errno));
exit(-1);
}
const char* basedir = apkstring.c_str();
std::string rstrdir(basedir);
rstrdir.append("/");
rstrdir.append("resources.arsc");
std::string rfstrdir(basedir);
rfstrdir.append("/");
rfstrdir.append("res");
std::string metadir(basedir);
metadir.append("/");
metadir.append("META-INF");
const char* rdir = rstrdir.c_str();
const char* rfdir = rfstrdir.c_str();
QUP_LOGI("[*] resource dir = %s, res file dir = %s,"
"META-INF dir = %s", rdir, rfdir, metadir.c_str());
bool isCopy = false;
//parse全局日志开关
bool isLogAll = false;
//creator 全局日志开关, parse 二级日志开关(parse打印更详细的日志)
bool isLog = true;
//是否仅仅解析资源文件,而不是解析+混淆
bool isOnlyParse = false;
#ifdef ONLY_PARSE_NOT_MIXUP
isOnlyParse = true;
#endif
Mixer* mixer = new ArscMixer(TYPE_RESOURCE_ARSC,
isCopy,
isLogAll,
isLog,
isOnlyParse,
rdir,
rfdir);
if (!mixer) {
QUP_LOGI("[-] calloc mixer fail");
exit(-1);
}
if (!mixer->mixer()) {
exit(-1);
}
//remove resources.arsc and rename resources.arsc.1 to resources.arsc
if (unlink("resources.arsc") != 0) {
QUP_LOGI("[-] delete backed resources.arsc fail %s", strerror(errno));
exit(-1);
}
if (rename("resources.arsc.1", "resources.arsc") != 0) {
QUP_LOGI("[-] rename resources.arsc.1 to resources.arsc fail %s", strerror(errno));
exit(-1);
}
QUP_LOGI("[*] delete META-INF");
//iter remove META-INF
if (!iter_del_dir(metadir.c_str(), 0)) {
QUP_LOGI("[-] iter remove META-INF fail");
exit(-1);
}
rmdir(metadir.c_str());
// zip -r sgsearch.zip *
if (chdir(basedir) != 0) {
QUP_LOGI("[-] cd dir %s fail %s", basedir, strerror(errno));
exit(-1);
}
command = std::string("zip -r ").append(filename).append(" *");
QUP_LOGI("[*] zip command = %s", command.c_str());
if (system(command.c_str()) < 0) {
QUP_LOGI("[-] zip apk fail %s", strerror(errno));
exit(-1);
}
command = std::string("cp ").append(filename).append(" ../").append(filename);
QUP_LOGI("[*] cp command = %s", command.c_str());
if (system(command.c_str()) < 0) {
QUP_LOGI("[-] cp new apk fail %s", strerror(errno));
exit(-1);
}
QUP_LOGI("[*] iter remove %s ", basedir);
//iter remove unzip file
if (!iter_del_dir(basedir, 0)) {
QUP_LOGI("[-] iter remove %s fail", basedir);
exit(-1);
}
rmdir(basedir);
delete mixer;
mixer = NULL;
}
static void usePythonZipMixup(std::string ppath) {
std::string rstrdir(ppath);
rstrdir.append("resources.arsc");
std::string rfstrdir(ppath);
rfstrdir.append("res");
const char* rdir = rstrdir.c_str();
const char* rfdir = rfstrdir.c_str();
bool isCopy = false;
//parse全局日志开关
bool isLogAll = false;
//creator 全局日志开关, parse 二级日志开关(parse打印更详细的日志)
bool isLog = true;
//是否仅仅解析资源文件,而不是解析+混淆
bool isOnlyParse = false;
#ifdef ONLY_PARSE_NOT_MIXUP
isOnlyParse = true;
#endif
Mixer* mixer = new ArscMixer(TYPE_RESOURCE_ARSC,
isCopy,
isLogAll,
isLog,
isOnlyParse,
rdir,
rfdir);
if (!mixer) {
QUP_LOGI("[-] calloc mixer fail");
exit(-1);
}
if (!mixer->mixer()) {
exit(-1);
}
delete mixer;
mixer = NULL;
}
int main(int argc, char** argv) {
if (argc < 7) {
usage();
}
bool isPythonTools = true;
bool isMixup = true;
bool isPathConApk = false;
std::string path("");
int opt;
char *optstring = (char*) "t:p:o:";
while ((opt = getopt(argc, argv, optstring)) != -1) {
QUP_LOGI("[*] opt = %c, optarg = %s", opt, optarg);
switch (opt) {
case 't':
if (strcmp(optarg, "python") != 0 && strcmp(optarg, "bin") != 0)
usage();
if (strcmp(optarg, "bin") == 0) {
isPythonTools = false;
}
break;
case 'p':
path = std::string(optarg);
break;
case 'o':
if (strcmp(optarg, "mixup") != 0 && strcmp(optarg, "parse") != 0)
usage();
if ((strcmp(optarg, "mixup") == 0 && !isPythonTools) && path.find(".apk") == std::string::npos) {
usage();
}
if (strcmp(optarg, "parse") == 0) {
isMixup = false;
}
break;
}
}
if (path.find(".apk") != std::string::npos)
isPathConApk = true;
QUP_LOGI("[*] use python zip tools = %d, "
"path = %s, operate mixup = %d,"
" path contain .apk = %d",
isPythonTools, path.c_str(), isMixup, isPathConApk);
std::string apkPath("");
if (isPathConApk) {
apkPath.append(path.substr(0, (path.rfind("/") + 1)));
} else {
apkPath.append(path).append("/");
}
QUP_LOGI("[*] parent path = %s", apkPath.c_str());
if (!isMixup) {
if (isPathConApk) {
if (!isPythonTools)
useBinParse(path);
else
usePythonZipParse(apkPath);
} else
onlyParse(apkPath);
} else {
if (!isPythonTools)
useBinMixup(path);
else
usePythonZipMixup(apkPath);
}
return 0;
}