This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
214 lines (189 loc) · 7.62 KB
/
process.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
206
207
208
209
210
211
212
213
214
'use strict'
//const fs = require('fs');
const path = require('path');
//const Promise = require('promise');
const Promise = require("bluebird");
const join = Promise.join;
const fs = Promise.promisifyAll(require("fs"));
const spawn = require('child_process').spawn;
const japa = require("java-parser");
var protoBufModels = require('builder');
const jsmfjson = require('jsmf-json');
const io = require('./bootstrap.js').io;
const log_web_socket = require('./bootstrap.js').log_web_socket;
const conf = require('./conf.js');
var IC3Proto = require('./conf.js').IC3Proto,
IC3ProtoGrammar = require('./conf.js').IC3ProtoGrammar,
IC3EntryPoint = require('./conf.js').IC3EntryPoint,
BinaryAppProtoBuf = require('./conf.js').BinaryAppProtoBuf
var M = null;
var ICCmodelReady = false;
var APK_decompiled = false;
function start_process(file, should_generate_ast) {
ICCmodelReady = false;
APK_decompiled = false;
function ensureICCmodelReadyAndAPKdecompiled() {
return new Promise(function (resolve, reject) {
(function waitForContitions(){
if (ICCmodelReady && APK_decompiled) return resolve();
setTimeout(waitForContitions, 30);
})();
});
}
// Before launching the process, clean the folder where outputs of
// subprocesses are stored
//const spawn_sync = require('child_process').spawnSync;
//spawn_sync('rm', ['-Rf', conf.bin_outputs]);
// Generation of the models
var promises = [generate_ICC_model, generate_source_code_model]
.map(function(task) {
return new Promise(function(fullfill, reject) {
task(file);
fullfill();
})
})
Promise.all(promises)
.catch(console.error);
ensureICCmodelReadyAndAPKdecompiled().then(function(){
if (should_generate_ast) {
// Generation of the AST
generate_ast();
}
})
.then(function() {
// clean the uploads folder
// fs.readdir(conf.uploads_folder, (err, files) => {
// if (err) throw error;
//
// for (const file of files) {
// fs.unlink(path.join(conf.uploads_folder, file), err => {
// if (err) throw err;
// });
// }
// });
})
.catch(console.error);
}
function generate_ast() {
log_web_socket(io, "[CP-3] AST generation...");
Promise.map(M.modellingElements['Component'], function(component) {
// Promise.map awaits for returned promises as well.
var name = component.name || component.class_name;
if (name) {
var file = conf.bin_outputs + 'jdcmd/' + file.originalname + '/' +
name.replace(/\./g, '/') + '.java';
var content = fs.readFileAsync(file, 'utf-8').catch(function ignore() {});;
return join(name, content, function(name, content) {
return {
name: name,
content: content
}
});
}
}).then(function(result) {
var source_code_ast = {};
result.map(function (elem) {
try {
source_code_ast[elem.name] = japa.parse(elem.content);
}
catch (err) {
// console.log(err);
log_web_socket(io, "[CP-3] stderr: Unable to create AST for: " + elem.name);
}
})
var source_code_ast_serialized = JSON.stringify(source_code_ast);
fs.writeFile(conf.bin_outputs + 'apk_ast.json',
source_code_ast_serialized, function(err) {
if(err) {
return console.log(err);
}
log_web_socket(io, "[CP-3] AST generated.");
});
});
}
function generate_ICC_model(file) {
// Generation of the model of application's
// "Inter-Component Communication" representation.
//
log_web_socket(io, 'Launching a child process (CP-1) in order to ' +
'retarget and generate a binary proto file.');
log_web_socket(io, "[CP-1] analysis of the Inter-Component Communication with IC3...");
const cmd = spawn('bin/APK-analyzer/apk2icc.sh', [file.path, file.originalname]);
cmd.stderr.on('data', (data) => {
if (data && data.length > 1) {
//log_web_socket(io, `[CP-1] stderr: ${data}`);
console.log(`[CP-1] stderr: ${data}`);
}
});
cmd.stdout.on('data', (data) => {
//console.log(`stdout: ${data}`);
//io.emit('news', data);
});
cmd.on('close', (code) => {
if (code == 0)
{
BinaryAppProtoBuf = conf.bin_outputs + 'ic3/' +
file.filename + '/result.dat';
log_web_socket(io, "[CP-1] Inter-Component Communication analysis done.");
log_web_socket(io, "[CP-1] Building JSMF model from the Inter-Component Communication...");
protoBufModels.build(IC3Proto, IC3ProtoGrammar,
IC3EntryPoint, BinaryAppProtoBuf);
M = protoBufModels.model;
// Serialization of the Model for later use
var ICC_model_serialized = jsmfjson.stringify(protoBufModels.model);
fs.writeFile(conf.bin_outputs + file.originalname + '.json',
ICC_model_serialized, function(err) {
if(err) {
return console.log(err);
}
log_web_socket(io, "[CP-1] JSMF model serialized.");
}
);
module.exports.ICC_models.push(protoBufModels.model);
ICCmodelReady = true;
log_web_socket(io, "[CP-1] JSMF model built.");
}
log_web_socket(io, `[CP-1] child process exited with code ${code}`);
});
return true;
}
function generate_source_code_model(file) {
// Generation of the model of application's source code.
//
log_web_socket(io, 'Launching a child process (CP-2) in order to decompile the APK.');
log_web_socket(io, '[CP-2] convert .dex file to .class files (zipped as jar)...')
const cmd_decompile_step1 = spawn('bin/dex2jar/d2j-dex2jar.sh',
['--force','--output',
conf.bin_outputs+'/result-dex2jar.jar',
file.path]);
cmd_decompile_step1.stderr.on('data', (data) => {
if (data && data.length > 1) {
//log_web_socket(io, `[CP-2] stderr: ${data}`);
console.log(`[CP-2] stderr: ${data}`);
}
});
cmd_decompile_step1.on('close', (code) => {
if (code == 0)
{
log_web_socket(io, '[CP-2] decompiling .class files with jd-cmd...')
const cmd_decompile_step2 = spawn('java',
['-jar', 'bin/jd-cmd/jd-cli.jar',
'--outputDir', conf.bin_outputs+'/jdcmd/'+file.originalname,
conf.bin_outputs+'/result-dex2jar.jar']);
cmd_decompile_step2.stderr.on('data', (data) => {
if (data && data.length > 1) {
//log_web_socket(io, `[CP-2] stderr: ${data}`);
console.log(`[CP-2] stderr: ${data}`);
}
});
cmd_decompile_step2.on('close', (code) => {
APK_decompiled = true;
log_web_socket(io, '[CP-2] APK decompiled.');
log_web_socket(io, `[CP-2] child process exited with code ${code}`);
})
}
});
}
module.exports = {
start_process: start_process
};