-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·205 lines (177 loc) · 4.66 KB
/
index.js
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
#!/usr/bin/env node
// CLI to print permisisons and packages from specified APK
'use strict';
const inquirer = require('inquirer');
const chalk = require('chalk');
const fs = require('fs');
const wrapper = require('./lib/wrapper');
const output = require('./lib/output');
const utility = require('./lib/utility');
var argv = require('yargs')
.usage('Usage: $0 [options]')
.option('l', {
alias: 'local-source',
nargs: 1,
describe: '<path_to_apk> path to local apk',
})
.option('r', {
alias: 'remote-source',
nargs: 1,
describe: '<url> remote apk URL',
})
.option('o', {
alias: 'output-type',
nargs: 1,
describe: '[json], [text], [both] - filetype of output',
default: 'json',
})
.option('c', {
alias: 'console-output',
describe: 'print output to console',
type: 'boolean',
default: false,
})
.option('p', {
alias: 'permissions-only',
describe: 'only output permissions',
type: 'boolean',
default: false,
})
.option('d', {
alias: 'dependencies-only',
describe: 'only output dependencies',
type: 'boolean',
default: false,
})
.option('x', {
alias: 'specify-permission',
nargs: 1,
describe: '<string> console log if permisison was found',
})
.option('y', {
alias: 'specify-dependency',
nargs: 1,
describe: '<string> console log if dependency was found',
})
.alias('v', 'version')
.describe('v', 'show version information')
.help('h')
.alias('h', 'help')
.example('$0')
.example('$0 -r https://pathtoapk.io -x camera')
.example('$0 -l ./Downloads/myapk.apk -d')
.argv;
output.printTitle();
let localOrRemote = {
type: 'list',
name: 'source',
message: 'Analyze local or remote source APK?',
choices: ['remote', 'local'],
}
let getLocalApk = {
type: 'input',
name: 'pathToApk',
message: 'Path to local APK:',
}
let getRemoteApk = {
type: 'input',
name: 'urlToApk',
message: 'URL for remote APK:',
}
main();
async function main() {
var pathToApk = '';
var urlToApk = '';
var pathToUnzippedApk = '';
var specificPermission = '';
var specificDependency = '';
var permissionsArray = null;
var dependenciesArray = null;
var root = __dirname;
/**
* If statments for command line arguments
*/
if (!argv.l && !argv.r) {
let answers = await inquirer.prompt([localOrRemote]);
if (answers.source == 'local') {
let local = await inquirer.prompt([getLocalApk]);
pathToApk = await utility.verifyPathToApk(local.pathToApk.trim());
console.log('\n');
}
else if (answers.source == 'remote') {
let remote = await inquirer.prompt([getRemoteApk]);
urlToApk = await utility.verifyUrlToApk(remote.urlToApk.trim());
pathToApk = await utility.downloadApk(root, urlToApk);
}
}
if (argv.l && argv.r) {
console.log(
chalk.red("APKI Error: Please select either --remote-source (-r) or --local-source (-l) but not both.")
);
console.log(
chalk.red("See more information with 'apki --help'")
);
return;
}
else if (argv.l) {
pathToApk = await utility.verifyPathToApk(argv.l);
}
else if (argv.r) {
urlToApk = await utility.verifyUrlToApk(argv.r);
pathToApk = await utility.downloadApk(root, urlToApk);
}
/**
* Unzip APK
*/
pathToUnzippedApk = await utility.unzipApk(pathToApk);
/**
* Check flags for only permissions
*/
if (argv.p) {
permissionsArray = wrapper.getPermissions(pathToUnzippedApk);
}
/**
* Check flag for only dependencies
*/
else if (argv.d) {
dependenciesArray = wrapper.getDependencies(root, pathToUnzippedApk);
}
/**
* Default case, run both permissions and dependencies
*/
else {
permissionsArray = wrapper.getPermissions(pathToUnzippedApk);
dependenciesArray = wrapper.getDependencies(root, pathToUnzippedApk);
}
/**
* Output permissions and dependencies to command line
*/
if (argv.c) {
output.print(permissionsArray, dependenciesArray);
}
/**
* Check for specific permission
*/
if (argv.x) {
output.printSpecificPermission(permissionsArray, argv.x);
}
/**
* Check for specific dependency
*/
if (argv.y) {
output.printSpecificDependency(dependenciesArray, argv.y);
}
/**
* If statements to handle file output types
*/
if (argv.o == 'json') {
output.writeToJSON(permissionsArray, dependenciesArray, 'output.json');
}
else if (argv.o == 'text') {
output.writeToText(permissionsArray, dependenciesArray, 'output.txt');
}
else if (argv.o == 'both') {
output.writeToJSON(permissionsArray, dependenciesArray, 'output.json');
output.writeToText(permissionsArray, dependenciesArray, 'output.txt');
}
}