Repository navigation
[2.2.0] Creates variables in wrong order #745
Copy link
Copy link
Closed
Description
Everything has been running smootly up to version [2.1.0] but after updating to version [2.2.0], the code no longer compiles sucessfully.
Expected Behavior
The files should obfuscate and compile sucessfully.
Current Behavior
They don't.
Steps to Reproduce (for bugs)
With the help of the gulp module
gulp.task('obfuscate', function() {
return gulp.src('./api/**/*.js')
.pipe(javascriptObfuscator({
compact: true,
seed: 1875,
reservedNames: [
'pass',
'password',
'credentials',
'PDFPager',
'undefined'
],
reservedStrings: [
'pass',
'password',
'credentials',
'PDFPager',
'undefined'
],
}))
.pipe(gulp.dest('./api'));
});
Your Environment
- Obfuscator version used: 2.2.0
- Node version used: 12.10.0
- Gulp obfuscator version used: 1.1.6
Stack trace
There is no stack trace. The obfuscation terminates sucessfully, but the files themselves are invalid.
After trying to run the obfuscated file, error _0x5aa2cd is not defined is raised.
Minimal working example that will help to reproduce issue
Original:
const fs = require('fs');
const Buffer = require('buffer').Buffer;
const iconv = require('iconv-lite');
const InternalException = require('./../exceptions/InternalException');
const path = require('path');
class TextFileReader {
/**
*
* @param {string} filePath Path to file. Path is sanitized.
* @param {string} encoding utf-8/utf8ascii/base64/binary/hex/ucs2/ucs-2/utf16le/utf-16le/cp1250. Default utf-8.
* @returns {string} Returns content of file.
*/
static readFile(filePath, encoding = 'utf-8') {
// Validate encoding
if (!encoding || typeof encoding !== 'string') {
throw new InternalException('Encoding is not defined or not typeof string.');
}
// Validate encoding
const allowedEncodings = ['ascii', 'base64', 'binary', 'hex', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'cp1250', 'utf-8', 'utf8'];
const encodingLowerCase = encoding.toLowerCase();
if (allowedEncodings.indexOf(encodingLowerCase) === -1) {
throw new InternalException(`Encoding is not allowed. Current encoding: ${encodingLowerCase} should be one of ${allowedEncodings.join(', ')}`);
}
// Sanitize file path, check file exists.
const filePathSanitized = path.normalize(filePath);
if (!fs.existsSync(filePathSanitized)) {
throw new InternalException(`File ${filePathSanitized} not found.`);
}
// Read content
let content = null;
// NodeJs doesn't support cp1250. Convert bytes via Iconv.
if (encodingLowerCase === 'cp1250') {
const bytes = fs.readFileSync(filePath);
const buf = iconv.decode(Buffer.from(bytes, 'binary'), 'CP1250');
content = iconv.encode(buf, 'UTF8').toString();
}
else {
content = fs.readFileSync(filePath, encodingLowerCase);
}
return content;
}
}
module.exports = TextFileReader;
Obfuscated:
const _0x2875 = ['existsSync', 'buffer', 'ascii', 'indexOf', './../exceptions/InternalException', 'File\x20', '\x20not\x20found.', 'toString', 'readFileSync', 'Encoding\x20is\x20not\x20defined\x20or\x20not\x20typeof\x20string.', 'base64', 'normalize', 'UTF8', 'join', 'from', 'binary', 'encode', 'CP1250', 'toLowerCase', 'path', 'ucs-2', 'ucs2', '\x20should\x20be\x20one\x20of\x20', 'cp1250', 'Buffer', 'utf-8', 'utf8'];
(function (_0x357494, _0x2875fc) {
const _0x5895b0 = function (_0x44e9e2) {
while (--_0x44e9e2) {
_0x357494['push'](_0x357494['shift']());
}
};
_0x5895b0(++_0x2875fc);
}(_0x2875, 0x112));
const _0x5895 = function (_0x357494, _0x2875fc) {
_0x357494 = _0x357494 - 0x0;
let _0x5895b0 = _0x2875[_0x357494];
return _0x5895b0;
};
const _0x5b107d = _0x5895, fs = require('fs'), Buffer = require(_0x5b107d('0x18'))[_0x5b107d('0x14')],
iconv = require('iconv-lite'), InternalException = require(_0x5b107d('0x0')), path = require(_0x5b107d('0xf'));
class TextFileReader {
static ['readFile'](_0x44e9e2, _0x3c2313 = _0x5aa2cd('0x15')) {
const _0x5aa2cd = _0x5b107d;
if (!_0x3c2313 || typeof _0x3c2313 !== 'string') throw new InternalException(_0x5aa2cd('0x5'));
const _0xbf4263 = [_0x5aa2cd('0x19'), _0x5aa2cd('0x6'), _0x5aa2cd('0xb'), 'hex', _0x5aa2cd('0x11'), _0x5aa2cd('0x10'), 'utf16le', 'utf-16le', 'cp1250', _0x5aa2cd('0x15'), _0x5aa2cd('0x16')],
_0x34ac40 = _0x3c2313[_0x5aa2cd('0xe')]();
if (_0xbf4263[_0x5aa2cd('0x1a')](_0x34ac40) === -0x1) throw new InternalException('Encoding\x20is\x20not\x20allowed.\x20Current\x20encoding:\x20' + _0x34ac40 + _0x5aa2cd('0x12') + _0xbf4263[_0x5aa2cd('0x9')](',\x20'));
const _0x43ce1e = path[_0x5aa2cd('0x7')](_0x44e9e2);
if (!fs[_0x5aa2cd('0x17')](_0x43ce1e)) throw new InternalException(_0x5aa2cd('0x1') + _0x43ce1e + _0x5aa2cd('0x2'));
let _0x4f9f7a = null;
if (_0x34ac40 === _0x5aa2cd('0x13')) {
const _0x51d19c = fs[_0x5aa2cd('0x4')](_0x44e9e2),
_0x578b00 = iconv['decode'](Buffer[_0x5aa2cd('0xa')](_0x51d19c, _0x5aa2cd('0xb')), _0x5aa2cd('0xd'));
_0x4f9f7a = iconv[_0x5aa2cd('0xc')](_0x578b00, _0x5aa2cd('0x8'))[_0x5aa2cd('0x3')]();
} else _0x4f9f7a = fs['readFileSync'](_0x44e9e2, _0x34ac40);
return _0x4f9f7a;
}
}
module['exports'] = TextFileReader;
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-][2.2.0] Creates nonexisting variables or in wrong order[/-][+][2.2.0] Creates variables in wrong order[/+]sanex3339 commentedon Sep 15, 2020
As a temporary solution set
stringArrayWrappersCountoption value to0.I'll check this
JakeHamix commentedon Sep 15, 2020
Thanks, I temporarily downgraded to [2.1.0].
Seems like an issue related to default (fallback) argument values in functions in general.
sanex3339 commentedon Sep 15, 2020
The problem with the
defaultfunction parameter here:sanex3339 commentedon Sep 15, 2020
I'll fix it later today. Thank you for a good example!
Fixed #745
sanex3339 commentedon Sep 15, 2020
PR is ready.
Merge pull request #746 from javascript-obfuscator/string-array-wrapp…
sanex3339 commentedon Sep 15, 2020
Released as
2.2.1