forked from txase/maximize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximize.js
executable file
·145 lines (123 loc) · 4 KB
/
maximize.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
#!/usr/bin/env node
var http = require('http');
var https = require('https');
var url = require('url');
var ArgumentParser = require('argparse').ArgumentParser;
var falafel = require('falafel');
var beautify = require('js-beautify');
var Seq = require('seq');
var sourceMap = require('source-map');
var parser = new ArgumentParser({
addHelp: true,
description: 'Deobfuscate and beautify JavaScript code with source maps',
});
parser.addArgument(['url'], {help: 'URL of javascript to maximize', nargs: 1});
parser.addArgument(['-b', '--beautify-opts'], {help: 'JS Beautifier options in JSON format', nargs: 1, defaultValue: '{}'});
var args = parser.parseArgs();
try {
args.beautify_opts = JSON.parse(args.beautify_opts);
} catch (e) {
console.error('Failed to parse JS Beautifier options: ' + e.message);
process.exit(-1);
}
function fetch(src) {
switch (url.parse(src).protocol) {
case 'http:':
http.get(src, this.ok).on('error', this);
break;
case 'https:':
http.get(src, this.ok).on('error', this);
break;
default:
console.error('Bad URL: URL must be an HTTP(S) resource');
process.exit(-1);
}
}
Seq()
.seq('fetch_src', function() {
fetch.call(this, args.url[0]);
})
.seq('code', function(res) {
var self = this;
if (res.statusCode < 200 || res.statusCode > 300) {
console.error('Failed to fetch script: status code ' + res.statusCode);
process.exit(-1);
}
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('end', function() {
var code = Buffer.concat(data).toString();
if ('SourceMap' in res.headers)
self.ok(code, res.headers.SourceMap);
else if ('X-SourceMap' in res.headers)
self.ok(code, res.headers['X-SourceMap']);
else
self.ok(code);
});
res.on('close', function() {
console.error('Failed to retrieve entire script');
process.exit(-1);
});
})
.seq('mapUrl', function(code, mapUrl) {
if (mapUrl)
return this.ok(mapUrl);
var sourceMappingURLRE = /^\/\/[@#] sourceMappingURL=(.*)$/m;
var info = sourceMappingURLRE.exec(code);
if (!info) {
console.error('Failed to find sourceMappingURL tag in script');
process.exit(-1);
}
var srcmap = info[1];
var srcmapUri = url.parse(srcmap);
var srcUri = url.parse(args.url[0]);
if (!srcmapUri.protocol) {
srcmapUri.protocol = srcUri.protocol;
srcmapUri.auth = srcUri.auth;
srcmapUri.host = srcUri.host;
}
if (srcmap[0] !== '/')
srcmapUri.pathname = srcUri.pathname.slice(0, srcUri.pathname.lastIndexOf('/') + 1) + srcmap;
this.ok(url.format(srcmapUri));
})
.seq('fetch_map', fetch)
.seq('map', function(res) {
var self = this;
if (res.statusCode < 200 || res.statusCode > 300) {
console.error('Failed to fetch source map: status code ' + res.statusCode);
process.exit(-1);
}
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
});
res.on('end', function() {
self.ok(new sourceMap.SourceMapConsumer(Buffer.concat(data).toString()));
});
res.on('close', function() {
console.error('Failed to retrieve entire source map');
process.exit(-1);
});
})
.seq('process', function(map) {
var deobfuscated = falafel(this.vars.code, {loc: true}, function(node) {
var orig;
if (node.id) {
orig = map.originalPositionFor({line: node.id.loc.start.line, column: node.id.loc.start.column});
if (orig.name)
node.id.update(orig.name);
} else if (node.type === 'Identifier') {
orig = map.originalPositionFor({line: node.loc.start.line, column: node.loc.start.column});
if (orig.name)
node.update(orig.name);
}
});
var beautified = beautify(deobfuscated.toString(), args.beautify_opts);
console.log(beautified);
})
['catch'](function(err, stage) {
console.error('Failed in stage ' + stage + ': ' + err.stack);
process.exit(-1);
});