Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.2.0] Creates variables in wrong order #745

Closed
JakeHamix opened this issue Sep 15, 2020 · 6 comments · Fixed by #746
Closed

[2.2.0] Creates variables in wrong order #745

JakeHamix opened this issue Sep 15, 2020 · 6 comments · Fixed by #746

Comments

@JakeHamix
Copy link

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;
@JakeHamix JakeHamix changed the title [2.2.0] Creates nonexisting variables or in wrong order [2.2.0] Creates variables in wrong order Sep 15, 2020
@sanex3339
Copy link
Member

As a temporary solution set stringArrayWrappersCount option value to 0.
I'll check this

@JakeHamix
Copy link
Author

Thanks, I temporarily downgraded to [2.1.0].
Seems like an issue related to default (fallback) argument values in functions in general.

@sanex3339
Copy link
Member

The problem with the default function parameter here:

static readFile(filePath, encoding = 'utf-8') {

@sanex3339
Copy link
Member

sanex3339 commented Sep 15, 2020

I'll fix it later today. Thank you for a good example!

@sanex3339
Copy link
Member

PR is ready.

sanex3339 added a commit that referenced this issue Sep 15, 2020
@sanex3339
Copy link
Member

Released as 2.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants