-
Notifications
You must be signed in to change notification settings - Fork 39
/
jsObfuscate.js
169 lines (161 loc) · 4.74 KB
/
jsObfuscate.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
var Q = require('q');
var https = require('https');
var entities = require('entities');
var querystring = require('querystring');
var htmlparser = require('htmlparser2');
function request(method, postData, reqHeaders) {
var deferred = Q.defer();
reqHeaders = reqHeaders || {};
var headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit' +
'/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
};
for (var h in reqHeaders) {
headers[h] = reqHeaders[h];
}
var req = https.request({
host: 'www.javascriptobfuscator.com',
port: 443,
path: '/Javascript-Obfuscator.aspx',
method: method || 'GET',
headers: headers
}, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
deferred.resolve({
headers: res.headers,
body: body
});
});
});
req.on('error', function (err) {
deferred.reject(err);
});
if (postData) {
req.write(querystring.stringify(postData));
}
req.end();
return deferred.promise;
}
function getFormData(content) {
var keys = [
'__EVENTTARGET',
'__EVENTARGUMENT',
'__VIEWSTATE',
'__EVENTVALIDATION',
'uploader1',
'Button1'
];
var formData = {};
var deferred = Q.defer();
var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if (name === 'input') {
if (keys.indexOf(attribs.name) > -1) {
formData[attribs.name] = attribs.value || '';
}
}
},
onend: function(tagname) {
deferred.resolve(formData);
}
});
parser.write(content);
parser.end();
return deferred.promise;
}
function getResult(content) {
var getText;
var deferred = Q.defer();
var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if (name === 'textarea' && attribs.name === 'ctl00$MainContent$TextBox2') {
getText = true;
}
},
ontext: function(text) {
if (getText === true) {
getText = text;
}
},
onend: function(tagname) {
deferred.resolve(getText);
}
});
parser.write(content);
parser.end();
return deferred.promise;
}
function sanitizeOptions(options) {
var defaults = {
keepLinefeeds: false,
keepIndentations: false,
encodeStrings: true,
encodeNumbers: true,
moveStrings: true,
replaceNames: true,
variableExclusions: [ '^_get_', '^_set_', '^_mtd_' ]
};
var toStr = Object.prototype.toString;
if (toStr.call(options) !== '[object Object]') return defaults;
for (var def in defaults) {
if (toStr.call(defaults[def]) !== toStr.call(options[def])) continue;
defaults[def] = options[def];
}
return defaults;
}
function obfuscate(strInput, options) {
options = sanitizeOptions(options);
return request().
then(function(res) {
var headers = {};
if (res.headers['set-cookie']) {
var cookie = res.headers['set-cookie'][0];
var cookiedata = cookie.slice(0, cookie.indexOf(';'));
headers['Cookie'] = cookiedata;
}
headers['Content-Type'] = 'application/x-www-form-urlencoded';
return getFormData(res.body).then(function(formData) {
return {
formData: formData,
headers: headers
};
});
}).
then(function(bundle) {
if (options.keepLinefeeds) bundle.formData['ctl00$MainContent$cbLineBR'] = 'on';
if (options.keepIndentations) bundle.formData['ctl00$MainContent$cbIndent'] = 'on';
if (options.encodeStrings) bundle.formData['ctl00$MainContent$cbEncodeStr'] = 'on';
if (options.encodeNumbers) bundle.formData['ctl00$MainContent$cbEncodeNumber'] = 'on';
if (options.moveStrings) bundle.formData['ctl00$MainContent$cbMoveStr'] = 'on';
if (options.replaceNames) bundle.formData['ctl00$MainContent$cbReplaceNames'] = 'on';
bundle.formData['UploadLib_Uploader_js'] = '1';
bundle.formData['__EVENTTARGET'] = 'ctl00$MainContent$Button1';
bundle.formData['ctl00$MainContent$TextBox1'] = strInput;
bundle.formData['ctl00$MainContent$TextBox2'] = '';
bundle.formData['ctl00$MainContent$TextBox3'] = options.variableExclusions.join('\n');
return bundle;
}).
then(function(bundle) {
return request('POST', bundle.formData, bundle.headers);
}).
then(function(res) {
return getResult(res.body);
}).
then(function(result) {
result = entities.decodeHTML(result).trim();
if (result.length === 0) {
throw 'Empty result.';
}
if (result.slice(0, 33) === 'JScriptCodeDom.CodeParseException') {
throw result;
}
return result;
});
}
module.exports = obfuscate;
module.exports.defaultOptions = sanitizeOptions();